KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > GUI > VisualElement


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch.GUI;
25
26 import net.fenyo.gnetwatch.*;
27 import net.fenyo.gnetwatch.activities.*;
28 import net.fenyo.gnetwatch.targets.*;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import java.net.*;
34 import java.util.*;
35
36 import org.eclipse.swt.*;
37 import org.eclipse.swt.widgets.*;
38 import org.eclipse.swt.layout.*;
39 import org.eclipse.swt.custom.*;
40 import org.eclipse.swt.events.*;
41 import org.eclipse.swt.graphics.*;
42
43 /**
44  * Class derivated from VisualElement can be displayed in the main tree of the GNetWatch GUI.
45  * @author Alexandre Fenyo
46  * @version $Id: VisualElement.java,v 1.37 2007/03/12 05:04:15 fenyo Exp $
47  */

48
49 // pour la synchro : ne pas s'appuyer sur le fait qu'on fait qqchose dans thread GUI car il y a pas
50
// forcément un thread GUI
51

52 public class VisualElement {
53   private static Log log = LogFactory.getLog(VisualElement.class);
54
55   // Since many classes may run in a VM without a GUI (GNetWatch server mode),
56
// we do not instanciate at startup the private fields to save memory & CPU.
57

58   // initialized protects agains two initializations at the same time
59
private Boolean JavaDoc initialized = false;
60   private GUI gui = null;
61
62   private Boolean JavaDoc disposed;
63
64   private java.util.List JavaDoc<TreeItem> treeItems = null;
65   private java.util.List JavaDoc<VisualElement> children = null;
66   private java.util.List JavaDoc<VisualElement> parents = null;
67
68   private String JavaDoc item = null;
69   private String JavaDoc type = null;
70   private String JavaDoc description = null;
71   private Image image = null;
72
73   private boolean is_selected = false;
74
75   private int progress = 0;
76
77   /**
78    * Constructor.
79    * @param none.
80    */

81   // GUI & main thread
82
public VisualElement() {}
83
84   /**
85    * Initializes this element.
86    * @param gui current GUI instance.
87    * @return void.
88    */

89   // We do not make this job at construction time since an object that is derived
90
// from a VisualElement may never be seen (background mode without GUI).
91
// main, Queue & GUI threads
92
// sync path: initialize() << initialized
93
protected void initialize(final GUI gui) {
94     synchronized (initialized) {
95       if (initialized == false) {
96         this.gui = gui;
97         if (item == null) item = "uninitialized";
98         if (type == null) type = "";
99         if (description == null) description = "";
100         treeItems = new LinkedList<TreeItem>();
101         parents = new LinkedList<VisualElement>();
102         children = new LinkedList<VisualElement>();
103         initialized = true;
104         disposed = false;
105       }
106       // needed when the text fields are set before a GUI is defined
107
else if (this.gui == null) this.gui = gui;
108     }
109   }
110
111   /**
112    * Expands or merge the associated tree items.
113    * @param doit true to expand.
114    * @return void.
115    */

116   public void expandTreeItems(final boolean doit) {
117     for (final TreeItem item : treeItems) item.setExpanded(doit);
118   }
119
120   /**
121    * Checks that this element is disposed.
122    * @param none.
123    * @return boolean true if this element is disposed.
124    */

125   public boolean isDisposed() {
126     return disposed;
127   }
128
129   /**
130    * Returns the current GUI instance.
131    * @param none.
132    * @return GUI current GUI instance.
133    */

134   // Queue & GUI threads
135
// could be any thread
136
final protected GUI getGUI() {
137     return gui;
138   }
139
140   /**
141    * Sets the "exec" icon to this element.
142    * @param none.
143    * @return void.
144    */

145   public void setImageExec() {
146     setImage(getGUI().getImageExec());
147   }
148
149   /**
150    * Sets the "folder" icon to this element.
151    * @param none.
152    * @return void.
153    */

154   public void setImageFolder() {
155     setImage(getGUI().getImageFolder());
156   }
157
158   /**
159    * Sets the "oscilloscope" icon to this element.
160    * @param none.
161    * @return void.
162    */

163   public void setImageOscillo() {
164     setImage(getGUI().getImageOscillo());
165   }
166
167   /**
168    * Sets the "multirow" icon to this element.
169    * @param none.
170    * @return void.
171    */

172   public void setImageMultiRow() {
173     setImage(getGUI().getImageMultiRow());
174   }
175
176   /**
177    * Sets the "watch" icon to this element.
178    * @param none.
179    * @return void.
180    */

181   public void setImageWatch() {
182     setImage(getGUI().getImageWatch());
183   }
184
185   /**
186    * Sets the "IPv4 host" icon to this element.
187    * @param none.
188    * @return void.
189    */

190   public void setImageHost() {
191     setImage(getGUI().getImageHost());
192   }
193
194   /**
195    * Sets the "IPv6 host" icon to this element.
196    * @param none.
197    * @return void.
198    */

199   public void setImageHost6() {
200     setImage(getGUI().getImageHost6());
201   }
202
203   /**
204    * Sets the "inteface" icon to this element.
205    * @param none.
206    * @return void.
207    */

208   public void setImageInterface() {
209     setImage(getGUI().getImageInterface());
210   }
211
212   /**
213    * Sets the "queue" icon to this element.
214    * @param none.
215    * @return void.
216    */

217   public void setImageQueue() {
218     setImage(getGUI().getImageQueue());
219   }
220
221   /**
222    * Sets the "network" icon to this element.
223    * @param none.
224    * @return void.
225    */

226   public void setImageNetwork() {
227     setImage(getGUI().getImageNetwork());
228   }
229
230   /**
231    * Sets the "snmp ipv4 host" icon to this element.
232    * @param none.
233    * @return void.
234    */

235   public void setImageHostSNMP() {
236     setImage(getGUI().getImageHostSNMP());
237   }
238
239   /**
240    * Sets the "snmp ipv6 host" icon to this element.
241    * @param none.
242    * @return void.
243    */

244   public void setImageHost6SNMP() {
245     setImage(getGUI().getImageHost6SNMP());
246   }
247
248   /**
249    * Returns the list of tree items that represent this element.
250    * @param none.
251    * @return java.util.List<TreeItem> list of tree items.
252    */

253   // GUI thread
254
protected java.util.List JavaDoc<TreeItem> getTreeItems() {
255     return treeItems;
256   }
257
258   /**
259    * Checks that a tree item represents this element.
260    * @param item tree item.
261    * @return boolean true if this tree item represents this element.
262    */

263   protected boolean isThisOurTreeItem(final TreeItem item) {
264     return treeItems.contains(item);
265   }
266
267   /**
268    * Adds a tree item to represent this element.
269    * @param treeItem tree item to add.
270    * @return void.
271    */

272   // GUI thread
273
private void addTreeItem(final TreeItem treeItem) {
274     treeItem.setData(VisualElement.class.toString(), this);
275     if (image != null) treeItem.setImage(image);
276     else treeItem.setImage(gui.getImageFolder());
277     treeItems.add(treeItem);
278     treeItem.setText(new String JavaDoc [] { item, type, description });
279   }
280
281   /**
282    * Recursively duplicates this element and its descendants under each of the destination tree items.
283    * @param destination_tree_items destination tree items.
284    * @return void.
285    */

286   private void duplicateTreeItem(final java.util.List JavaDoc<TreeItem> destination_tree_items) {
287     final java.util.List JavaDoc<TreeItem> new_tree_items = new LinkedList<TreeItem>();
288     
289     for (final TreeItem destination_tree_item : destination_tree_items) {
290       final TreeItem my_tree_item_copy = new TreeItem(destination_tree_item, SWT.NONE);
291       new_tree_items.add(my_tree_item_copy);
292       addTreeItem(my_tree_item_copy);
293     }
294
295     for (final VisualElement child : children) child.duplicateTreeItem(new_tree_items);
296   }
297
298   /**
299    * Attaches this element to the root of a tree.
300    * @param gui current GUI instance.
301    * @param parent root of the destination tree.
302    * @return void.
303    */

304   // GUI thread
305
protected void setParent(final GUI gui, final Tree parent) {
306     initialize(gui);
307     addTreeItem(new TreeItem(parent, SWT.NONE));
308   }
309
310   /**
311    * Attaches this item under another element.
312    * @param gui current GUI instance.
313    * @param parent parent element.
314    * @return void.
315    */

316   // GUI thread
317
public void setParent(final GUI gui, final VisualElement parent) {
318     initialize(gui);
319
320     duplicateTreeItem(parent.getTreeItems());
321
322     parents.add(parent);
323     parent.addChild(this);
324   }
325
326   /**
327    * Recursively detaches an item from this element and its descendants.
328    * @param item item to detach.
329    * @return void.
330    */

331   private void disposeSubItems(final TreeItem item) {
332     for (final VisualElement child : children)
333       for (final TreeItem child_item : new LinkedList<TreeItem>(child.getTreeItems()))
334         if (item.equals(child_item.getParentItem()))
335           child.disposeSubItems(child_item);
336     item.dispose();
337     treeItems.remove(item);
338   }
339
340   /**
341    * Detaches this element from one of its parents.
342    * @param parent parent.
343    * @return void.
344    */

345   private void unsetParent(final VisualElement parent) {
346     parent.getChildren().remove(this);
347     parents.remove(parent);
348
349     for (final TreeItem item : new LinkedList<TreeItem>(treeItems))
350       if (parent.isThisOurTreeItem(item.getParentItem()))
351         disposeSubItems(item);
352
353     if (parents.size() == 0) disposed();
354   }
355
356   /**
357    * Adds a sub element.
358    * @param child element to add.
359    * @return void.
360    */

361   // GUI thread
362
private void addChild(VisualElement child) {
363     children.add(child);
364   }
365
366   /**
367    * Returns children elements.
368    * @param none.
369    * @return java.util.List<VisualElement> children.
370    */

371   // GUI thread
372
public java.util.List JavaDoc<VisualElement> getChildren() {
373     return children;
374   }
375
376   /**
377    * Returns parents of this element.
378    * @return java.util.List<VisualElement> parents.
379    */

380   public java.util.List JavaDoc<VisualElement> getParents() {
381     return parents;
382   }
383
384   /**
385    * Checks that the parameter is a children.
386    * @param elt element to check.
387    * @return true if the parameter is a children.
388    */

389   // GUI thread
390
public boolean contains(final VisualElement elt) {
391     return children.contains(elt);
392   }
393
394   /**
395    * Updates the displayed text (item name) of this element.
396    * @param none.
397    * @return void.
398    */

399   // main, Queue & GUI threads
400
private void updateText() {
401     initialize(null);
402
403     final Runnable JavaDoc r = new Runnable JavaDoc() {
404       public void run() {
405         try {
406           final Object JavaDoc sync = (gui != null) ? gui.sync_tree : new Object JavaDoc();
407           synchronized (sync) {
408             for (final TreeItem tree_item : treeItems)
409               if (tree_item != null)
410                 tree_item.setText(new String JavaDoc [] { item, type, description});
411           }
412         } catch (final SWTException ex) {
413           // widget is disposed
414
}
415       }
416     };
417
418     if (gui != null) gui.asyncExecIfNeeded(r);
419     else r.run();
420   }
421
422   /**
423    * Updates the displayed icon of this element.
424    * @param none.
425    * @return void.
426    */

427   // GUI thread
428
private void updateImage() {
429     initialize(null);
430
431     final Runnable JavaDoc r = new Runnable JavaDoc() {
432       public void run() {
433         try {
434           synchronized (gui.sync_tree) {
435             for (final TreeItem tree_item : treeItems)
436               if (tree_item != null)
437                 tree_item.setImage(image);
438           }
439         } catch (final SWTException ex) {
440           // widget is disposed
441
}
442       }
443     };
444
445     if (gui != null) gui.asyncExecIfNeeded(r);
446     else r.run();
447   }
448
449   /**
450    * Sets the item name (displayed text).
451    * @param item item name.
452    * @return void.
453    */

454   // main, Queue & GUI threads
455
protected void setItem(final String JavaDoc item) {
456     this.item = item;
457     updateText();
458   }
459
460   /**
461    * Returns the item name.
462    * @param none.
463    * @return void.
464    */

465   public String JavaDoc getItem() {
466     return item;
467   }
468
469   /**
470    * Sets the displayed type.
471    * @param type type to display.
472    * @return void.
473    */

474   // main, Queue & GUI threads
475
public void setType(final String JavaDoc type) {
476     this.type = type;
477     updateText();
478   }
479
480   /**
481    * Sets the description.
482    * @param description description.
483    * @return void.
484    */

485   // main, Queue & GUI threads
486
public void setDescription(final String JavaDoc description) {
487     this.description = description;
488     updateText();
489   }
490
491   /**
492    * Sets the icon.
493    * @param image icon.
494    * @return void.
495    */

496   // GUI thread
497
protected void setImage(final Image image) {
498     initialize(null);
499     this.image = image;
500     updateImage();
501   }
502
503   /**
504    * Called when selected by the user.
505    * @param none.
506    * @return void.
507    */

508   // GUI thread
509
public void informSelected() {}
510
511   /**
512    * Called when disposed.
513    * @param none.
514    * @return void.
515    */

516   // GUI thread
517
protected void disposed() {
518     disposed = true;
519   }
520
521   /**
522    * Gets sub elements of a given type.
523    * @param clazz type.
524    * @param elts list that will be updated.
525    * @return void.
526    */

527   private void getSubElements(final Class JavaDoc clazz, final java.util.List JavaDoc<VisualElement> elts) {
528     for (final VisualElement elt : getChildren()) elt.getSubElements(clazz, elts);
529     if (clazz.isInstance(this)) elts.add(this);
530   }
531
532   /**
533    * Gets sub elements of a given type.
534    * @param clazz type.
535    * @return java.util.List<VisualElement> elts list that will be updated.
536    */

537   // les éléments sont retournés tels que les plus bas dans l'arbre sont après ceux plus haut
538
public java.util.List JavaDoc<VisualElement> getSubElements(final Class JavaDoc clazz) {
539     final java.util.List JavaDoc<VisualElement> elts = new java.util.LinkedList JavaDoc<VisualElement>();
540     getSubElements(clazz, elts);
541     return elts;
542   }
543
544   /**
545    * Gets sub elements of a given type.
546    * @param clazz type.
547    * @return java.util.List<VisualElement> elts list that will be updated.
548    */

549   // les éléments sont retournés tels que les plus bas dans l'arbre sont après ceux plus haut
550
static public java.util.List JavaDoc<VisualElement> getSubElements(final TreeItem item, final Class JavaDoc clazz) {
551     return ((VisualElement) item.getData(VisualElement.class.toString())).getSubElements(clazz);
552   }
553
554   /**
555    * Returns every ascendant of this element, restricted to a given type.
556    * @param clazz type.
557    * @param elts elts list that will be updated.
558    */

559   private void getAllParents(final Class JavaDoc clazz, final java.util.List JavaDoc<VisualElement> elts) {
560     for (final VisualElement elt : getParents()) elt.getAllParents(clazz, elts);
561     if (clazz.isInstance(this)) elts.add(this);
562   }
563
564   /**
565    * Returns every ascendant of this element, restricted to a given type.
566    * @param clazz type.
567    * @return java.util.List<VisualElement> elts list that will be updated to give the results.
568    */

569   public java.util.List JavaDoc<VisualElement> getAllParents(final Class JavaDoc clazz) {
570     final java.util.List JavaDoc<VisualElement> elts = new java.util.LinkedList JavaDoc<VisualElement>();
571     getAllParents(clazz, elts);
572     return elts;
573   }
574
575   /**
576    * Checks that the parameter can be attached to this element.
577    * @param visual_element parameter to check.
578    * @return true if the parameter can be attached to this element.
579    */

580   public boolean canManageThisChild(final VisualElement visual_element) {
581     return true;
582   }
583
584   /**
585    * Builds the subgraph rooted at the parent parameter.
586    * @param parent root of the subgraph.
587    * @param graph empty graph that will be updated to give the results.
588    * @return void.
589    */

590   private void getSubGraph(final VisualElement parent, final java.util.List JavaDoc<Pair<VisualElement, VisualElement>> graph) {
591     for (final VisualElement child : children) child.getSubGraph(this, graph);
592     final Pair<VisualElement, VisualElement> link = new Pair<VisualElement, VisualElement>(this, parent);
593     if (!graph.contains(link)) graph.add(new Pair<VisualElement, VisualElement>(this, parent));
594   }
595
596   /**
597    * Builds the subgraph rooted at the parent parameter.
598    * @param parent root of the subgraph.
599    * @return java.util.List<Pair<VisualElement, VisualElement>> resulting graph.
600    */

601   private java.util.List JavaDoc<Pair<VisualElement, VisualElement>> getSubGraph(final VisualElement parent) {
602     final java.util.List JavaDoc<Pair<VisualElement, VisualElement>> graph = new LinkedList<Pair<VisualElement, VisualElement>>();
603
604     getSubGraph(parent, graph);
605     return graph;
606   }
607
608   /**
609    * Builds the subgraph rooted at the parent parameter, but not containing some links of another graph.
610    * @param parent root of the subgraph.
611    * @param graph empty graph that will be updated to give the results.
612    * @param except_links another graph.
613    * @return void.
614    */

615   private void getSubGraphExceptLinks(final VisualElement parent, final java.util.List JavaDoc<Pair<VisualElement, VisualElement>> graph, final java.util.List JavaDoc<Pair<VisualElement, VisualElement>> except_links) {
616     for (final VisualElement child : children)
617       if (!except_links.contains(new Pair<VisualElement, VisualElement>(child, this)))
618         child.getSubGraphExceptLinks(this, graph, except_links);
619
620     final Pair<VisualElement, VisualElement> link = new Pair<VisualElement, VisualElement>(this, parent);
621     if (!graph.contains(link)) graph.add(link);
622   }
623
624   /**
625    * Builds the subgraph rooted at the parent parameter, but not containing some links of another graph.
626    * @param parent root of the subgraph.
627    * @param graph empty graph that will be updated to give the results.
628    * @return java.util.List<Pair<VisualElement, VisualElement>> resulting graph.
629    */

630   private java.util.List JavaDoc<Pair<VisualElement, VisualElement>> getSubGraphExceptLinks(final VisualElement parent, final java.util.List JavaDoc<Pair<VisualElement, VisualElement>> except_links) {
631     final java.util.List JavaDoc<Pair<VisualElement, VisualElement>> graph = new LinkedList<Pair<VisualElement, VisualElement>>();
632
633     getSubGraphExceptLinks(parent, graph, except_links);
634     return graph;
635   }
636
637   /**
638    * Detaches this element from one of its parents and removes children that have become orphan.
639    * @param visual_parent parent.
640    * @return void.
641    */

642   public void removeVisualElements(final VisualElement visual_parent) {
643     removeVisualElements(visual_parent, false);
644   }
645
646   /**
647    * Detaches this element from one of its parents and removes children that have become orphan.
648    * @param visual_parent parent.
649    * @param bypass bypass some verifications.
650    * @return void.
651    */

652   private void removeVisualElements(final VisualElement visual_parent, final boolean bypass) {
653     if (bypass == false) {
654       if (equals(gui.getVisualTransient())) return;
655       if (equals(gui.getVisualThisHost())) return;
656       if (equals(gui.getVisualTransientAll())) return;
657       if (equals(gui.getVisualTransientNetworks())) return;
658       if (visual_parent.equals(gui.getVisualThisHost())) return;
659       if (visual_parent.equals(gui.getVisualTransientAll())) return;
660       if (visual_parent.equals(gui.getVisualTransientNetworks())) return;
661     }
662
663     final java.util.List JavaDoc<Pair<VisualElement, VisualElement>> subgraph = getSubGraph(visual_parent);
664
665     final java.util.List JavaDoc<Pair<VisualElement, VisualElement>> except_links =
666       new LinkedList<Pair<VisualElement, VisualElement>>();
667     except_links.add(new Pair<VisualElement, VisualElement>(this, visual_parent));
668
669     final java.util.List JavaDoc<Pair<VisualElement, VisualElement>> graph =
670       gui.getVisualTransient().getSubGraphExceptLinks(null, except_links);
671
672     GenericTools.substractGraph(subgraph, graph);
673
674     for (final Pair<VisualElement, VisualElement> p : subgraph)
675       p.former().unsetParent(p.latter());
676
677     for (final VisualElement host : new LinkedList<VisualElement>(gui.getVisualTransientAll().getChildren()))
678       if (host.getParents().size() == 1) host.removeVisualElements(gui.getVisualTransientAll(), true);
679   }
680
681   /**
682    * Sets the progress bar position.
683    * @param progress position.
684    * @return void.
685    */

686   public void setProgress(final int progress) {
687     this.progress = progress;
688     if (is_selected == true && gui != null) gui.setProgress(progress);
689   }
690
691   /**
692    * Gets the progress bar position for this visual element.
693    * @param none.
694    * @return progress position.
695    */

696   public int getProgress() {
697     return progress;
698   }
699
700   /**
701    * Called when this element has been selected.
702    * @param none.
703    * @return void.
704    */

705   public void selected() {
706     is_selected = true;
707   }
708
709   /**
710    * Called when this element has been unselected.
711    * @param none.
712    * @return void.
713    */

714   public void unselected() {
715     is_selected = false;
716   }
717 }
718
Popular Tags