Source code: org/embl/ebi/escience/scuflui/workbench/ScavengerTree.java
1 /**
2 * This file is a component of the Taverna project,
3 * and is licensed under the GNU LGPL.
4 * Copyright Tom Oinn, EMBL-EBI
5 */
6 package org.embl.ebi.escience.scuflui.workbench;
7 import javax.swing.JTree;
8 import javax.swing.tree.DefaultMutableTreeNode;
9 import javax.swing.tree.DefaultTreeModel;
10 import javax.swing.tree.MutableTreeNode;
11 import javax.swing.tree.TreePath;
12 import javax.swing.tree.TreeSelectionModel;
13 import org.embl.ebi.escience.scufl.Processor;
14 import org.embl.ebi.escience.scufl.ScuflModel;
15 import org.embl.ebi.escience.scufl.SoaplabProcessor;
16 import org.embl.ebi.escience.scufl.TalismanProcessor;
17 import org.embl.ebi.escience.scufl.WSDLBasedProcessor;
18 import org.embl.ebi.escience.scuflui.ScuflUIComponent;
19
20 // Utility Imports
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.Map;
25
26 import org.embl.ebi.escience.scuflui.workbench.Scavenger;
27 import org.embl.ebi.escience.scuflui.workbench.ScavengerCreationException;
28 import org.embl.ebi.escience.scuflui.workbench.ScavengerTreePopupHandler;
29 import org.embl.ebi.escience.scuflui.workbench.ScavengerTreeRenderer;
30 import org.embl.ebi.escience.scuflui.workbench.SoaplabScavenger;
31 import org.embl.ebi.escience.scuflui.workbench.TalismanScavenger;
32 import org.embl.ebi.escience.scuflui.workbench.WSDLBasedScavenger;
33 import java.lang.String;
34
35
36
37 /**
38 * A JTree subclass showing available processors from some
39 * set of external libraries or searches. Nodes corresponding
40 * to a single potential processor instance should contain
41 * a user object implementing ProcessorFactory.
42 * @author Tom Oinn
43 */
44 public class ScavengerTree extends JTree
45 implements ScuflUIComponent {
46
47 /**
48 * The model that this scavenger will create processor for
49 */
50 protected ScuflModel model = null;
51
52 /**
53 * The root node
54 */
55 DefaultMutableTreeNode root = null;
56
57 /**
58 * The tree model
59 */
60 DefaultTreeModel treeModel = null;
61
62 /**
63 * A list of the names of all the scavengers contained within this tree
64 */
65 ArrayList scavengerList = null;
66
67 /**
68 * A private count to avoid name duplication on created nodes
69 */
70 private int count = 0;
71
72 /**
73 * Get the next available count and increment the counter
74 */
75 public int getNextCount() {
76 return count++;
77 }
78
79 /**
80 * Create a new scavenger tree
81 */
82 public ScavengerTree() {
83 super();
84 scavengerList = new ArrayList();
85 root = new DefaultMutableTreeNode("Available Processors");
86 treeModel = (DefaultTreeModel)this.getModel();
87 treeModel.setRoot(this.root);
88 putClientProperty("JTree.lineStyle","Angled");
89 getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
90 ScavengerTreeRenderer renderer = new ScavengerTreeRenderer();
91 this.setCellRenderer(renderer);
92 this.addMouseListener(new ScavengerTreePopupHandler(this));
93 }
94
95 /**
96 * Examine the model, create any scavengers that would have been required
97 * to populate the model with its existing processors. Now handles all three
98 * processor types.
99 */
100 public void addScavengersFromModel()
101 throws ScavengerCreationException {
102 if (this.model != null) {
103 // Get all WSDL processors
104 Map wsdlLocations = new HashMap();
105 Map talismanLocations = new HashMap();
106 Map soaplabInstallations = new HashMap();
107 Processor[] p = model.getProcessors();
108 for (int i = 0; i < p.length; i++) {
109 // If the processor is a WSDLBasedProcessor then get
110 // the wsdl location and add it to the map.
111 if (p[i] instanceof WSDLBasedProcessor) {
112 String wsdlLocation = ((WSDLBasedProcessor)p[i]).getWSDLLocation();
113 wsdlLocations.put(wsdlLocation,null);
114 }
115 else if (p[i] instanceof TalismanProcessor) {
116 String tscriptLocation = ((TalismanProcessor)p[i]).getTScriptURL();
117 talismanLocations.put(tscriptLocation,null);
118 }
119 else if (p[i] instanceof SoaplabProcessor) {
120 String endpoint = ((SoaplabProcessor)p[i]).getEndpoint().toString();
121 String[] parts = endpoint.split("/");
122 String base = "";
123 for (int j = 0; j < parts.length -1; j++) {
124 base = base + parts[j] + "/";
125 }
126 soaplabInstallations.put(base,null);
127 }
128 }
129 // Now iterate over all the wsdl locations found and
130 // create new WSDL scavengers, adding them to the
131 // scavenger tree.
132 for (Iterator i = wsdlLocations.keySet().iterator(); i.hasNext(); ) {
133 String wsdlLocation = (String)i.next();
134 addScavenger(new WSDLBasedScavenger(wsdlLocation));
135 }
136 for (Iterator i = talismanLocations.keySet().iterator(); i.hasNext(); ) {
137 String tscriptURL = (String)i.next();
138 addScavenger(new TalismanScavenger(tscriptURL));
139 }
140 for (Iterator i = soaplabInstallations.keySet().iterator(); i.hasNext(); ) {
141 String base = (String)i.next();
142 addScavenger(new SoaplabScavenger(base));
143 }
144 }
145 }
146
147 /**
148 * Add a new scavenger to the tree, firing appropriate
149 * model events as we do.
150 */
151 public void addScavenger(Scavenger theScavenger) {
152 // Check to see we don't already have a scavenger with this name
153 String newName = theScavenger.getUserObject().toString();
154 for (Iterator i = scavengerList.iterator(); i.hasNext(); ) {
155 String name = (String)i.next();
156 if (name.equals(newName)) {
157 // Exit if we already have a scavenger by that name
158 return;
159 }
160 }
161 this.scavengerList.add(theScavenger.getUserObject().toString());
162 this.treeModel.insertNodeInto(theScavenger,
163 (MutableTreeNode)this.treeModel.getRoot(),
164 this.treeModel.getChildCount(this.treeModel.getRoot()));
165 // Set the visibility sensibly so that the root node
166 // is expanded and visible
167 TreePath path = new TreePath(this.root);
168 expandPath(path);
169 }
170
171 /**
172 * Listen for model bind requests to set the internal
173 * ScuflModel field
174 */
175 public void attachToModel(ScuflModel theModel) {
176 this.model = theModel;
177 }
178
179 /**
180 * When unbound from a model, set internal model field
181 * to null
182 */
183 public void detachFromModel() {
184 this.model = null;
185 }
186
187 /**
188 * Return an apppropriate title for windows
189 */
190 public String getName() {
191 return "Available services";
192 }
193
194 }
195