001 package com.ontotext.gate.vr;
002
003 import javax.swing.*;
004 import javax.swing.tree.*;
005 import java.awt.*;
006 import java.awt.event.*;
007 import java.util.*;
008 import gate.gui.*;
009 import gate.util.*;
010 import gate.creole.gazetteer.*;
011 import gate.creole.ontology.*;
012
013
014
015 /**
016 * Mapping Tree View extends {@link javax.swing.JTree}
017 * in order to represent the mapping information.
018 * To be used with Gaze.
019 * borislav popov 18/04/2002 */
020 public class MappingTreeView extends JTree {
021
022 private static final long serialVersionUID = 3257568420999410744L;
023
024 /**the name of the default gazetteer icon*/
025 private final static String GAZ_ICON = "lr";
026
027 /**Mapping Node Edit Action */
028 private final static int EDIT_ACTION = 1;
029 /**Mapping Node Insert Action */
030 private final static int INSERT_ACTION = 2;
031 /**Mapping Node Remove Action */
032 private final static int REMOVE_ACTION = 3;
033
034 /** reference to the mapping definition represented by this view */
035 private MappingDefinition mapping = null;
036
037 /** reference to the Gaze VR */
038 private Gaze gaze = null;
039
040 /**mapping popup menu */
041 private JPopupMenu pupMenu = new JPopupMenu();
042
043 /**insert popup action*/
044 private JMenuItem insertPuP = new JMenuItem("insert mapping");
045 /**remove popup action*/
046 private JMenuItem removePuP = new JMenuItem("remove mapping");
047
048 /**@param model the tree model
049 * @param mappingDef the mapping definition
050 * @param gazeVR gaze (gazetteer editor) visual resource */
051 public MappingTreeView(OntoTreeModel model,
052 MappingDefinition mappingDef, Gaze gazeVR) {
053
054 super(model);
055
056 if (null == mappingDef)
057 throw new LazyProgrammerException(
058 "Mapping Def cannot be null on contructing MappingTreeView");
059
060 if (null == gazeVR)
061 throw new LazyProgrammerException(
062 "Gazetteer Editor - Gaze VR - cannot be null on contructing MappingTreeView");
063
064 mapping = mappingDef;
065 gaze = gazeVR;
066
067 init();
068
069 }// constructor
070
071 /** Initialization */
072 private void init() {
073 getSelectionModel().setSelectionMode(
074 TreeSelectionModel.SINGLE_TREE_SELECTION);
075 DefaultTreeCellRenderer renderer = new MappingTreeCR();
076 renderer.setLeafIcon(renderer.getDefaultClosedIcon());
077 this.setCellRenderer(renderer);
078
079 // listeners
080 addMouseListener(new MyMouseAdapter());
081
082 removePuP.addActionListener(new RemoveAL());
083 insertPuP.addActionListener(new InsertAL());
084
085 pupMenu.add(insertPuP);
086 pupMenu.add(removePuP);
087
088 } // init
089
090 /** Mapping Tree Cell Renderer distinguishes nodes, originating from ontology classes from
091 * nodes, originating from gazetteer lists. */
092 class MappingTreeCR extends DefaultTreeCellRenderer {
093
094 private static final long serialVersionUID = 3546924666926085169L;
095
096 /**
097 * Sets the value of the current tree cell to <code>value</code>.
098 * If <code>selected</code> is true, the cell will be drawn as if
099 * selected. If <code>expanded</code> is true the node is currently
100 * expanded and if <code>leaf</code> is true the node represets a
101 * leaf anf if <code>hasFocus</code> is true the node currently has
102 * focus. <code>tree</code> is the JTree the receiver is being
103 * configured for.
104 * Returns the Component that the renderer uses to draw the value.
105 *
106 * @return Component that the renderer uses to draw the value.
107 */
108 public Component getTreeCellRendererComponent(JTree tree, Object value,
109 boolean selected, boolean expanded,
110 boolean leaf, int row, boolean hasFocus) {
111 super.getTreeCellRendererComponent(
112 tree, value, selected, expanded, leaf, row, hasFocus);
113
114 if ( value instanceof ClassNode ) {
115 ClassNode cn = (ClassNode) value;
116 Object source = cn.getSource();
117 if ( source instanceof MappingNode ) {
118 setIcon(MainFrame.getIcon(GAZ_ICON));
119 } // if gaz list
120 } // if node
121 return this;
122 } // getTreeCellRendererComponent()
123
124 } // class MappingTreeCR
125
126 /**The mouse adapter listens to the entire mouse activity and invokes a popup menu if
127 * right click. */
128 class MyMouseAdapter extends MouseAdapter{
129
130 public MyMouseAdapter(){
131 }
132
133 public void mouseClicked(MouseEvent e){
134 TreePath path=MappingTreeView.this.getSelectionPath();
135 javax.swing.JTree tree = new javax.swing.JTree();
136
137 ClassNode node =null;
138 if (SwingUtilities.isLeftMouseButton(e)) {
139 if (2 == e.getClickCount()) {
140 if( path != null){
141 node = (ClassNode) path.getLastPathComponent();
142 if ( node.getSource() instanceof MappingNode ) {
143 MappingNode mn = (MappingNode)node.getSource();
144 gaze.displayList(mn.getList());
145 }
146 } // if !=null
147 } // double click
148 } // left
149
150 if(SwingUtilities.isRightMouseButton(e)){
151 if( path != null){
152 node = (ClassNode) path.getLastPathComponent();
153 if ( node.getSource() instanceof MappingNode ) {
154 removePuP.setEnabled(true);
155 insertPuP.setEnabled(false);
156 } else {
157 removePuP.setEnabled(false);
158 insertPuP.setEnabled(true);
159 }
160 pupMenu.show(MappingTreeView.this,e.getX(),e.getY());
161 }
162 }
163 }
164 } //class MyMouseAdapter
165
166
167 /*Action Listener of the remove pop up menu item */
168 class RemoveAL implements ActionListener{
169 public void actionPerformed(ActionEvent e) {
170 JMenuItem item = (JMenuItem)e.getSource();
171 ClassNode node = (ClassNode)MappingTreeView.this.getLastSelectedPathComponent();
172 Object source = node.getSource();
173 if (source instanceof MappingNode) {
174 TreePath pp = MappingTreeView.this.getAnchorSelectionPath().getParentPath();
175 if (null!=pp) {
176 ClassNode pNode = (ClassNode)pp.getLastPathComponent();
177 Vector kids = pNode.children();
178 kids.remove(node);
179 pNode.setChildren(kids);
180 mapping.remove(source);
181 MappingTreeView.this.updateUI();
182 gaze.updateMappingUI();
183 } // pp ! null
184 }// if map node
185 } // actionPerformed()
186 } // class RemoveAL
187
188
189 /*Action Listener of the insert pop up menu item */
190 class InsertAL implements ActionListener {
191 public void actionPerformed(ActionEvent e) {
192 JMenuItem item = (JMenuItem)e.getSource();
193 ClassNode node = (ClassNode)MappingTreeView.this.getLastSelectedPathComponent();
194 Object source = node.getSource();
195 if (source instanceof OClass) {
196 java.util.List lists = gaze.getLists();
197 Collections.sort(lists);
198
199 Object result = JOptionPane.showInputDialog(MappingTreeView.this,
200 "Map selected ontology class to a gazetteer list:",
201 "Insert Mapping Node",
202 JOptionPane.PLAIN_MESSAGE,
203 null,lists.toArray(),null);
204 if (null != result) {
205 OClass oc = (OClass) source;
206 MappingNode mn = new MappingNode((String)result,
207 oc.getOntology().getURL().toString(),
208 node.toString());
209 mapping.add(mn);
210 ClassNode cn = new ClassNode(mn);
211 Vector kids = node.children();
212 kids.add(cn);
213 MappingTreeView.this.updateUI();
214 gaze.updateMappingUI();
215 } // null!=result
216 }// if map node
217 } // actionPerformed()
218 } // class InsertAL
219
220
221 } //MappingTreeView
|