1 2 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 48 49 52 public class VisualElement { 53 private static Log log = LogFactory.getLog(VisualElement.class); 54 55 58 private Boolean initialized = false; 60 private GUI gui = null; 61 62 private Boolean disposed; 63 64 private java.util.List <TreeItem> treeItems = null; 65 private java.util.List <VisualElement> children = null; 66 private java.util.List <VisualElement> parents = null; 67 68 private String item = null; 69 private String type = null; 70 private String description = null; 71 private Image image = null; 72 73 private boolean is_selected = false; 74 75 private int progress = 0; 76 77 81 public VisualElement() {} 83 84 89 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 else if (this.gui == null) this.gui = gui; 108 } 109 } 110 111 116 public void expandTreeItems(final boolean doit) { 117 for (final TreeItem item : treeItems) item.setExpanded(doit); 118 } 119 120 125 public boolean isDisposed() { 126 return disposed; 127 } 128 129 134 final protected GUI getGUI() { 137 return gui; 138 } 139 140 145 public void setImageExec() { 146 setImage(getGUI().getImageExec()); 147 } 148 149 154 public void setImageFolder() { 155 setImage(getGUI().getImageFolder()); 156 } 157 158 163 public void setImageOscillo() { 164 setImage(getGUI().getImageOscillo()); 165 } 166 167 172 public void setImageMultiRow() { 173 setImage(getGUI().getImageMultiRow()); 174 } 175 176 181 public void setImageWatch() { 182 setImage(getGUI().getImageWatch()); 183 } 184 185 190 public void setImageHost() { 191 setImage(getGUI().getImageHost()); 192 } 193 194 199 public void setImageHost6() { 200 setImage(getGUI().getImageHost6()); 201 } 202 203 208 public void setImageInterface() { 209 setImage(getGUI().getImageInterface()); 210 } 211 212 217 public void setImageQueue() { 218 setImage(getGUI().getImageQueue()); 219 } 220 221 226 public void setImageNetwork() { 227 setImage(getGUI().getImageNetwork()); 228 } 229 230 235 public void setImageHostSNMP() { 236 setImage(getGUI().getImageHostSNMP()); 237 } 238 239 244 public void setImageHost6SNMP() { 245 setImage(getGUI().getImageHost6SNMP()); 246 } 247 248 253 protected java.util.List <TreeItem> getTreeItems() { 255 return treeItems; 256 } 257 258 263 protected boolean isThisOurTreeItem(final TreeItem item) { 264 return treeItems.contains(item); 265 } 266 267 272 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 [] { item, type, description }); 279 } 280 281 286 private void duplicateTreeItem(final java.util.List <TreeItem> destination_tree_items) { 287 final java.util.List <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 304 protected void setParent(final GUI gui, final Tree parent) { 306 initialize(gui); 307 addTreeItem(new TreeItem(parent, SWT.NONE)); 308 } 309 310 316 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 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 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 361 private void addChild(VisualElement child) { 363 children.add(child); 364 } 365 366 371 public java.util.List <VisualElement> getChildren() { 373 return children; 374 } 375 376 380 public java.util.List <VisualElement> getParents() { 381 return parents; 382 } 383 384 389 public boolean contains(final VisualElement elt) { 391 return children.contains(elt); 392 } 393 394 399 private void updateText() { 401 initialize(null); 402 403 final Runnable r = new Runnable () { 404 public void run() { 405 try { 406 final Object sync = (gui != null) ? gui.sync_tree : new Object (); 407 synchronized (sync) { 408 for (final TreeItem tree_item : treeItems) 409 if (tree_item != null) 410 tree_item.setText(new String [] { item, type, description}); 411 } 412 } catch (final SWTException ex) { 413 } 415 } 416 }; 417 418 if (gui != null) gui.asyncExecIfNeeded(r); 419 else r.run(); 420 } 421 422 427 private void updateImage() { 429 initialize(null); 430 431 final Runnable r = new Runnable () { 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 } 442 } 443 }; 444 445 if (gui != null) gui.asyncExecIfNeeded(r); 446 else r.run(); 447 } 448 449 454 protected void setItem(final String item) { 456 this.item = item; 457 updateText(); 458 } 459 460 465 public String getItem() { 466 return item; 467 } 468 469 474 public void setType(final String type) { 476 this.type = type; 477 updateText(); 478 } 479 480 485 public void setDescription(final String description) { 487 this.description = description; 488 updateText(); 489 } 490 491 496 protected void setImage(final Image image) { 498 initialize(null); 499 this.image = image; 500 updateImage(); 501 } 502 503 508 public void informSelected() {} 510 511 516 protected void disposed() { 518 disposed = true; 519 } 520 521 527 private void getSubElements(final Class clazz, final java.util.List <VisualElement> elts) { 528 for (final VisualElement elt : getChildren()) elt.getSubElements(clazz, elts); 529 if (clazz.isInstance(this)) elts.add(this); 530 } 531 532 537 public java.util.List <VisualElement> getSubElements(final Class clazz) { 539 final java.util.List <VisualElement> elts = new java.util.LinkedList <VisualElement>(); 540 getSubElements(clazz, elts); 541 return elts; 542 } 543 544 549 static public java.util.List <VisualElement> getSubElements(final TreeItem item, final Class clazz) { 551 return ((VisualElement) item.getData(VisualElement.class.toString())).getSubElements(clazz); 552 } 553 554 559 private void getAllParents(final Class clazz, final java.util.List <VisualElement> elts) { 560 for (final VisualElement elt : getParents()) elt.getAllParents(clazz, elts); 561 if (clazz.isInstance(this)) elts.add(this); 562 } 563 564 569 public java.util.List <VisualElement> getAllParents(final Class clazz) { 570 final java.util.List <VisualElement> elts = new java.util.LinkedList <VisualElement>(); 571 getAllParents(clazz, elts); 572 return elts; 573 } 574 575 580 public boolean canManageThisChild(final VisualElement visual_element) { 581 return true; 582 } 583 584 590 private void getSubGraph(final VisualElement parent, final java.util.List <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 601 private java.util.List <Pair<VisualElement, VisualElement>> getSubGraph(final VisualElement parent) { 602 final java.util.List <Pair<VisualElement, VisualElement>> graph = new LinkedList<Pair<VisualElement, VisualElement>>(); 603 604 getSubGraph(parent, graph); 605 return graph; 606 } 607 608 615 private void getSubGraphExceptLinks(final VisualElement parent, final java.util.List <Pair<VisualElement, VisualElement>> graph, final java.util.List <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 630 private java.util.List <Pair<VisualElement, VisualElement>> getSubGraphExceptLinks(final VisualElement parent, final java.util.List <Pair<VisualElement, VisualElement>> except_links) { 631 final java.util.List <Pair<VisualElement, VisualElement>> graph = new LinkedList<Pair<VisualElement, VisualElement>>(); 632 633 getSubGraphExceptLinks(parent, graph, except_links); 634 return graph; 635 } 636 637 642 public void removeVisualElements(final VisualElement visual_parent) { 643 removeVisualElements(visual_parent, false); 644 } 645 646 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 <Pair<VisualElement, VisualElement>> subgraph = getSubGraph(visual_parent); 664 665 final java.util.List <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 <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 686 public void setProgress(final int progress) { 687 this.progress = progress; 688 if (is_selected == true && gui != null) gui.setProgress(progress); 689 } 690 691 696 public int getProgress() { 697 return progress; 698 } 699 700 705 public void selected() { 706 is_selected = true; 707 } 708 709 714 public void unselected() { 715 is_selected = false; 716 } 717 } 718 | Popular Tags |