PRViewer.java
001 /*
002  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
003  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004  *
005  *  This file is part of GATE (see http://gate.ac.uk/), and is free
006  *  software, licenced under the GNU Library General Public License,
007  *  Version 2, June 1991 (in the distribution as file licence.html,
008  *  and also available at http://gate.ac.uk/gate/licence.html).
009  *
010  *  Valentin Tablan 27/02/2002
011  *
012  *  $Id: PRViewer.java 12286 2010-02-18 17:27:11Z valyt $
013  *
014  */
015 package gate.gui;
016 
017 import java.awt.BorderLayout;
018 import java.awt.Component;
019 import java.awt.Toolkit;
020 import java.awt.datatransfer.Clipboard;
021 import java.awt.datatransfer.StringSelection;
022 import java.awt.event.ActionEvent;
023 import java.awt.event.MouseAdapter;
024 import java.awt.event.MouseEvent;
025 
026 import javax.swing.AbstractAction;
027 import javax.swing.JPopupMenu;
028 import javax.swing.JScrollPane;
029 
030 import gate.Gate;
031 import gate.Resource;
032 import gate.creole.AbstractVisualResource;
033 import gate.creole.ResourceData;
034 import gate.creole.ResourceInstantiationException;
035 import gate.creole.metadata.CreoleResource;
036 import gate.creole.metadata.GuiType;
037 import gate.swing.XJTable;
038 import gate.util.GateRuntimeException;
039 
040 @CreoleResource(name = "Initialisation Parameters", guiType = GuiType.LARGE,
041     resourceDisplayed = "gate.Resource", mainViewer = true)
042 public class PRViewer extends AbstractVisualResource {
043 
044   public PRViewer() {
045   }
046 
047   
048   @Override
049   public Resource init() throws ResourceInstantiationException {
050     initLocalData();
051     initGuiComponents();
052     initListeners();
053     return this;
054   }
055 
056 
057   protected void initLocalData(){
058   }
059 
060   protected void initGuiComponents(){
061     setLayout(new BorderLayout());
062     editor = new ResourceParametersEditor();
063     editor.setEditable(false);
064     editor.setAutoResizeMode(XJTable.AUTO_RESIZE_LAST_COLUMN);
065     JScrollPane scroller = new JScrollPane(editor);
066     scroller.setAlignmentX(Component.LEFT_ALIGNMENT);
067     scroller.setAlignmentY(Component.TOP_ALIGNMENT);
068     add(scroller, BorderLayout.CENTER);
069   }
070 
071   protected void initListeners(){
072     editor.addMouseListener(new MouseAdapter() {
073       private void handleMouseEvent(MouseEvent e){
074         if(e.isPopupTrigger()){
075           int row = editor.rowAtPoint(e.getPoint());
076           int col = editor.columnAtPoint(e.getPoint());
077           JPopupMenu popup =new JPopupMenu();
078           popup.add(new CopyValueAction(row, col));
079           popup.show(editor,e.getX(), e.getY());
080         }
081       }
082       
083       @Override
084       public void mousePressed(MouseEvent e) {
085         handleMouseEvent(e);
086       }
087 
088       @Override
089       public void mouseReleased(MouseEvent e) {
090         handleMouseEvent(e);
091       }
092 
093       @Override
094       public void mouseClicked(MouseEvent e) {
095         handleMouseEvent(e);
096       }
097     });
098   }
099 
100   public void cleanup(){
101     super.cleanup();
102     editor.cleanup();
103   }
104 
105   public void setTarget(Object target){
106     if(target == nullreturn;
107     if(!(target instanceof Resource)){
108       throw new GateRuntimeException(this.getClass().getName() +
109                                      " can only be used to display " +
110                                      Resource.class.getName() +
111                                      "\n" + target.getClass().getName() +
112                                      " is not a " +
113                                      Resource.class.getName() "!");
114     }
115     if(target != null){
116       Resource pr = (Resource)target;
117       ResourceData rData = (ResourceData)Gate.getCreoleRegister().
118                                               get(pr.getClass().getName());
119       if(rData != null){
120         editor.init(pr, rData.getParameterList().getInitimeParameters());
121       }else{
122         editor.init(pr, null);
123       }
124     }else{
125       editor.init(null, null);
126     }
127     editor.removeCreoleListenerLink();
128   }
129 
130   protected ResourceParametersEditor editor;
131   
132   protected class CopyValueAction extends AbstractAction{
133     private int row, column;
134     
135     public CopyValueAction (int row, int column){
136       super("Copy value");
137       putValue(SHORT_DESCRIPTION, 
138               "Copies the value of the cell to the clipboard.");
139       this.row = row;
140       this.column = column;
141     }
142     
143     public void actionPerformed(ActionEvent e) {
144       Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
145       Object value = editor.getValueAt(row, column);
146       String valStr;
147       if(value instanceof ParameterDisjunction){
148         valStr = ((ParameterDisjunction)value).getName();
149       }else if(value instanceof Boolean){
150         valStr = ((Boolean)value"Required parameter" "Optional parameter";
151       }else{
152         valStr = value.toString();
153       }
154       StringSelection data = new StringSelection(valStr);
155       clipboard.setContents(data, data);
156     }
157   }
158 }