1   package com.ontotext.gate.vr;
2   
3   import javax.swing.*;
4   import javax.swing.tree.*;
5   import javax.swing.event.*;
6   
7   import java.awt.*;
8   import java.awt.event.*;
9   
10  import java.util.*;
11  
12  import gate.gui.*;
13  import gate.util.*;
14  import gate.creole.gazetteer.*;
15  import gate.creole.ontology.*;
16  
17  import com.ontotext.gate.vr.*;
18  
19  
20  /**
21   * Mapping Tree View extends {@link javax.swing.JTree}
22   * in order to represent the mapping information.
23   * To be used with Gaze.
24   * borislav popov 18/04/2002 */
25  public class MappingTreeView extends JTree {
26    /**the name of the default gazetteer icon*/
27    private final static String GAZ_ICON = "lr.gif";
28  
29    /**Mapping Node Edit Action */
30    private final static int EDIT_ACTION = 1;
31    /**Mapping Node Insert Action */
32    private final static int INSERT_ACTION = 2;
33    /**Mapping Node Remove Action */
34    private final static int REMOVE_ACTION = 3;
35  
36    /** reference to the mapping definition represented by this view */
37    private MappingDefinition mapping = null;
38  
39    /** reference to the Gaze VR */
40    private Gaze gaze = null;
41  
42    /**mapping popup menu */
43    private JPopupMenu pupMenu = new JPopupMenu();
44  
45    /**insert popup action*/
46    private JMenuItem insertPuP = new JMenuItem("insert mapping");
47    /**remove popup action*/
48    private JMenuItem removePuP = new JMenuItem("remove mapping");
49  
50    /**@param model the tree model
51     * @param mappingDef the mapping definition
52     * @param gazeVR gaze (gazetteer editor) visual resource */
53    public MappingTreeView(OntoTreeModel model,
54          MappingDefinition mappingDef, Gaze gazeVR) {
55  
56      super(model);
57  
58      if (null == mappingDef)
59        throw new LazyProgrammerException(
60          "Mapping Def cannot be null on contructing MappingTreeView");
61  
62      if (null == gazeVR)
63        throw new LazyProgrammerException(
64          "Gazetteer Editor - Gaze VR - cannot be null on contructing MappingTreeView");
65  
66      mapping = mappingDef;
67      gaze = gazeVR;
68  
69      init();
70  
71    }// constructor
72  
73    /** Initialization */
74    private void init() {
75      getSelectionModel().setSelectionMode(
76        TreeSelectionModel.SINGLE_TREE_SELECTION);
77      DefaultTreeCellRenderer renderer = new MappingTreeCR();
78      renderer.setLeafIcon(renderer.getDefaultClosedIcon());
79      this.setCellRenderer(renderer);
80  
81      // listeners
82      addMouseListener(new MyMouseAdapter());
83  
84      removePuP.addActionListener(new RemoveAL());
85      insertPuP.addActionListener(new InsertAL());
86  
87      pupMenu.add(insertPuP);
88      pupMenu.add(removePuP);
89  
90    } // init
91  
92    /** Mapping Tree Cell Renderer distinguishes nodes, originating from ontology classes from
93     *  nodes, originating from gazetteer lists. */
94    class MappingTreeCR extends DefaultTreeCellRenderer {
95      /**
96       * Sets the value of the current tree cell to <code>value</code>.
97       * If <code>selected</code> is true, the cell will be drawn as if
98       * selected. If <code>expanded</code> is true the node is currently
99       * expanded and if <code>leaf</code> is true the node represets a
100      * leaf anf if <code>hasFocus</code> is true the node currently has
101      * focus. <code>tree</code> is the JTree the receiver is being
102      * configured for.
103      * Returns the Component that the renderer uses to draw the value.
104      *
105      * @return  Component that the renderer uses to draw the value.
106      */
107     public Component getTreeCellRendererComponent(JTree tree, Object value,
108            boolean selected, boolean expanded,
109            boolean leaf, int row, boolean hasFocus) {
110       super.getTreeCellRendererComponent(
111         tree, value, selected, expanded, leaf, row, hasFocus);
112 
113       if ( value instanceof ClassNode ) {
114         ClassNode cn = (ClassNode) value;
115         Object source = cn.getSource();
116         if ( source instanceof MappingNode ) {
117           setIcon(MainFrame.getIcon(GAZ_ICON));
118         } // if gaz list
119       } // if node
120       return this;
121     } // getTreeCellRendererComponent()
122 
123   } // class MappingTreeCR
124 
125   /**The mouse adapter listens to the entire mouse activity and invokes a popup menu if
126    * right click. */
127   class MyMouseAdapter extends MouseAdapter{
128 
129       public MyMouseAdapter(){
130       }
131 
132       public void mouseClicked(MouseEvent e){
133           TreePath path=MappingTreeView.this.getSelectionPath();
134           javax.swing.JTree tree = new javax.swing.JTree();
135 
136           ClassNode node =null;
137           if (SwingUtilities.isLeftMouseButton(e)) {
138             if (2 == e.getClickCount()) {
139               if( path != null){
140                 node = (ClassNode) path.getLastPathComponent();
141                 if ( node.getSource() instanceof MappingNode ) {
142                   MappingNode mn = (MappingNode)node.getSource();
143                   gaze.displayList(mn.getList());
144                 }
145               } // if !=null
146             } // double click
147           }  // left
148 
149           if(SwingUtilities.isRightMouseButton(e)){
150             if( path != null){
151               node = (ClassNode) path.getLastPathComponent();
152               if ( node.getSource() instanceof MappingNode ) {
153                 removePuP.setEnabled(true);
154                 insertPuP.setEnabled(false);
155               } else {
156                 removePuP.setEnabled(false);
157                 insertPuP.setEnabled(true);
158               }
159               pupMenu.show(MappingTreeView.this,e.getX(),e.getY());
160             }
161           }
162       }
163   } //class MyMouseAdapter
164 
165 
166   /*Action Listener of the remove pop up menu item */
167   class RemoveAL implements ActionListener{
168     public void actionPerformed(ActionEvent e) {
169       JMenuItem item = (JMenuItem)e.getSource();
170       ClassNode node = (ClassNode)MappingTreeView.this.getLastSelectedPathComponent();
171       Object source = node.getSource();
172       if (source instanceof MappingNode) {
173         TreePath pp = MappingTreeView.this.getAnchorSelectionPath().getParentPath();
174         if (null!=pp) {
175           ClassNode pNode = (ClassNode)pp.getLastPathComponent();
176           Vector kids = pNode.children();
177           kids.remove(node);
178           pNode.setChildren(kids);
179           mapping.remove(source);
180           MappingTreeView.this.updateUI();
181           gaze.updateMappingUI();
182         } // pp ! null
183       }// if map node
184     } // actionPerformed()
185   } // class RemoveAL
186 
187 
188   /*Action Listener of the insert pop up menu item */
189   class InsertAL implements ActionListener {
190     public void actionPerformed(ActionEvent e) {
191       JMenuItem item = (JMenuItem)e.getSource();
192       ClassNode node = (ClassNode)MappingTreeView.this.getLastSelectedPathComponent();
193       Object source = node.getSource();
194       if (source instanceof OClass) {
195         java.util.List lists = gaze.getLists();
196         Collections.sort(lists);
197 
198         Object result = JOptionPane.showInputDialog(MappingTreeView.this,
199                           "Map selected ontology class to a gazetteer list:",
200                           "Insert Mapping Node",
201                           JOptionPane.PLAIN_MESSAGE,
202                           null,lists.toArray(),null);
203         if (null != result) {
204           OClass oc = (OClass) source;
205           oc.getOntology().getURL();
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