View Javadoc

1   package org.opensync.engine.admin.gui;
2   
3   import java.awt.event.MouseAdapter;
4   import java.awt.event.MouseEvent;
5   import java.awt.event.MouseListener;
6   import java.util.Collection;
7   import java.util.Enumeration;
8   import java.util.Iterator;
9   
10  import javax.swing.BorderFactory;
11  import javax.swing.JPopupMenu;
12  import javax.swing.JTree;
13  import javax.swing.tree.DefaultMutableTreeNode;
14  import javax.swing.tree.DefaultTreeModel;
15  import javax.swing.tree.TreePath;
16  import javax.swing.tree.TreeSelectionModel;
17  
18  import org.opensync.engine.server.Agenda;
19  import org.opensync.engine.server.Agendas;
20  import org.opensync.engine.server.Source;
21  import org.opensync.engine.server.Sources;
22  import org.opensync.engine.server.Synchronizations;
23  import org.opensync.engine.server.Task;
24  import org.opensync.engine.server.View;
25  import org.opensync.engine.util.I18n;
26  
27  /***
28   *
29   * @version	1.0
30   * @author	SOFTMED
31   *
32   */
33  
34  public class OpenSyncTree extends JTree {
35  
36    /****/
37    protected DefaultMutableTreeNode root;
38    private AppPnl appPnl;
39  
40    class PopupListener extends MouseAdapter {
41      public void mousePressed(MouseEvent e) {
42          maybeShowPopup(e);
43      }
44      public void mouseReleased(MouseEvent e) {
45          maybeShowPopup(e);
46      }
47      private void maybeShowPopup(MouseEvent e) {
48        if (e.isPopupTrigger()) {
49          //select if right button
50          TreePath treePath = getPathForLocation(e.getX(),e.getY());
51          if(treePath != null){
52            setSelectionPath(treePath);
53          }
54          treePath = getSelectionPath();
55          if(treePath != null){
56            DefaultMutableTreeNode node = (DefaultMutableTreeNode)treePath.getLastPathComponent();
57            if(node instanceof OpenSyncTreeNode){
58              JPopupMenu popup = ((OpenSyncTreeNode)node).getPopup();
59              if(popup != null){
60                popup.show(e.getComponent(),e.getX(), e.getY());
61              }
62            }
63          }
64        }
65      }
66    }
67  
68    /***
69     * @param	appPnl
70     */
71    public OpenSyncTree(AppPnl appPnl) {
72      super();
73      this.appPnl = appPnl;
74      setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
75      setCellRenderer(new OpenSyncTreeCellRenderer());
76      getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
77      createRootNode();
78      MouseListener popupListener = new PopupListener();
79      addMouseListener(popupListener);
80    }
81    /****/
82    protected void createRootNode() {
83      root = new DefaultMutableTreeNode(
84        I18n.getInstance().get("gui.product")
85      );
86      root.add(new LogTreeNode());
87      setModel(new DefaultTreeModel(root));
88    }
89    /***
90     * @param	synchronizations ?sources? ?agendas?
91     */
92    public void createNode(Synchronizations synchronizations) {
93      SynchronizationsTreeNode node = new SynchronizationsTreeNode(synchronizations);
94      ((DefaultTreeModel)getModel()).insertNodeInto(node,root,root.getChildCount());
95    }
96    /***
97     * @param	sources
98     */
99    public void createNode(Sources sources) {
100     DefaultMutableTreeNode node = new DefaultMutableTreeNode(
101       I18n.getInstance().get("gui.sources.label")
102     );
103     Enumeration enumeration = sources.elements();
104     while(enumeration.hasMoreElements()){
105       Source source = (Source)enumeration.nextElement();
106       node.add(createNode(source));
107     }
108     ((DefaultTreeModel)getModel()).insertNodeInto(node,root,root.getChildCount());
109   }
110   /***
111    * @param	source
112    */
113   protected SourceTreeNode createNode(Source source){
114     SourceTreeNode node = new SourceTreeNode(source);
115     //Enumeration enumeration = source.getViews().elements();
116     Collection values = source.getViews().values();
117     Iterator iter = values.iterator();
118     /*while(enumeration.hasMoreElements()){
119       View view = (View)enumeration.nextElement();
120       DefaultMutableTreeNode child = new ViewTreeNode(view);
121       node.add(child);
122     }
123     */
124     while(iter.hasNext()){
125         View view = (View)iter.next();
126         DefaultMutableTreeNode child = new ViewTreeNode(view);
127         node.add(child);
128       }
129     return node;
130   }
131   /***
132    * @param	agendas
133    */
134   public void createNode(Agendas agendas){
135     DefaultMutableTreeNode node = new DefaultMutableTreeNode(
136       I18n.getInstance().get("gui.agendas.label")
137     );
138     Enumeration enumeration = agendas.elements();
139     while(enumeration.hasMoreElements()){
140       Agenda agenda = (Agenda)enumeration.nextElement();
141       node.add(createNode(agenda));
142     }
143     ((DefaultTreeModel)getModel()).insertNodeInto(node,root,root.getChildCount());
144   }
145   /***
146    * @param	task
147    */
148   protected TaskTreeNode createNode(Task task){
149     TaskTreeNode node = new TaskTreeNode(task);
150     Iterator iterator = task.getSubTasks().iterator();
151     while(iterator.hasNext()){
152       node.add(createNode((Task)iterator.next()));
153     }
154     return node;
155   }
156   /***
157    * @param	agenda
158    */
159   protected AgendaTreeNode createNode(Agenda agenda){
160     AgendaTreeNode node = new AgendaTreeNode(agenda);
161     Iterator iterator = agenda.getTasks().iterator();
162     while(iterator.hasNext()){
163       node.add(createNode((Task)iterator.next()));
164     }
165     return node;
166   }
167 }
168 
169 
170 
171 
172 
173