Source code: org/embl/ebi/escience/scuflui/workbench/Workbench.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
8 import java.awt.Dimension;
9 import java.awt.Toolkit;
10 import java.awt.event.ActionEvent;
11 import java.awt.event.ActionListener;
12 import java.awt.event.WindowAdapter;
13 import java.awt.event.WindowEvent;
14 import javax.swing.*;
15 import org.embl.ebi.escience.scufl.ScuflModel;
16 import org.embl.ebi.escience.scufl.parser.XScuflParser;
17 import org.embl.ebi.escience.scufl.view.DotView;
18 import org.embl.ebi.escience.scufl.view.XScuflView;
19 import org.embl.ebi.escience.scuflui.DotTextArea;
20 import org.embl.ebi.escience.scuflui.EnactorLaunchPanel;
21 import org.embl.ebi.escience.scuflui.ScuflDiagram;
22 import org.embl.ebi.escience.scuflui.ScuflModelExplorer;
23 import org.embl.ebi.escience.scuflui.XScuflTextArea;
24
25 // Utility Imports
26 import java.util.Enumeration;
27 import java.util.Properties;
28 import java.util.ResourceBundle;
29
30 // IO Imports
31 import java.io.File;
32 import java.io.FileWriter;
33 import java.io.PrintWriter;
34
35 // Network Imports
36 import java.net.URL;
37
38 import org.embl.ebi.escience.scuflui.workbench.GenericUIComponentFrame;
39 import org.embl.ebi.escience.scuflui.workbench.ScavengerTree;
40 import org.embl.ebi.escience.scuflui.workbench.SplashScreen;
41 import java.lang.Class;
42 import java.lang.ClassNotFoundException;
43 import java.lang.Exception;
44 import java.lang.RuntimeException;
45 import java.lang.String;
46 import java.lang.System;
47
48
49
50 /**
51 * A sample workbench application to allow editing and visualization
52 * of Scufl workflows
53 * @author Tom Oinn
54 */
55 public class Workbench extends JFrame {
56
57 public static ImageIcon openIcon, deleteIcon, importIcon, saveIcon, openurlIcon;
58
59 static {
60 try {
61 Class c = Class.forName("org.embl.ebi.escience.scuflui.workbench.Workbench");
62 openIcon = new ImageIcon(c.getResource("open.gif"));
63 deleteIcon = new ImageIcon(c.getResource("delete.gif"));
64 saveIcon = new ImageIcon(c.getResource("save.gif"));
65 importIcon = new ImageIcon(c.getResource("import.gif"));
66 openurlIcon = new ImageIcon(c.getResource("openurl.gif"));
67 }
68 catch (ClassNotFoundException cnfe) {
69 //
70 }
71 // Initialize the proxy settings etc.
72 ResourceBundle rb = ResourceBundle.getBundle("mygrid");
73 Properties sysProps = System.getProperties();
74 Enumeration keys = rb.getKeys();
75 while (keys.hasMoreElements()) {
76 String key = (String) keys.nextElement();
77 String value = (String) rb.getString(key);
78 sysProps.put(key, value);
79 }
80 }
81
82 JDesktopPane desktop;
83
84 ScuflModel model;
85
86 final JFileChooser fc = new JFileChooser();
87
88 /**
89 * Launch the model workbench, shows the default set of UI components
90 * in internal frames and waits for the user to load a model from file
91 */
92 public static void main(String[] args) {
93 new SplashScreen(6000);
94 Workbench workbench = new Workbench();
95 // Treat any command line arguments as files to import into the workbench
96 for (int i = 0; i < args.length; i++) {
97 try {
98 File inputFile = new File(args[i]);
99 XScuflParser.populate(inputFile.toURL().openStream(), workbench.model, null);
100 }
101 catch (Exception e) {
102 System.out.println(e.getMessage());
103 }
104 }
105
106 // Add instances of all the components just for fun
107 GenericUIComponentFrame xscufl = new GenericUIComponentFrame(workbench.model,
108 new XScuflTextArea());
109 xscufl.setSize(600,300);
110 xscufl.setLocation(50,50);
111 workbench.desktop.add(xscufl);
112 GenericUIComponentFrame diagram = new GenericUIComponentFrame(workbench.model,
113 new ScuflDiagram());
114 diagram.setSize(600,600);
115 diagram.setLocation(50,400);
116 workbench.desktop.add(diagram);
117 GenericUIComponentFrame explorer = new GenericUIComponentFrame(workbench.model,
118 new ScuflModelExplorer());
119 explorer.setSize(300,300);
120 explorer.setLocation(700,50);
121 workbench.desktop.add(explorer);
122
123 GenericUIComponentFrame scavenger = new GenericUIComponentFrame(workbench.model,
124 new ScavengerTree());
125 scavenger.setSize(300,600);
126 scavenger.setLocation(700,400);
127 workbench.desktop.add(scavenger);
128
129 workbench.setVisible(true);
130 }
131
132 /**
133 * Create a new top level application. This contains a menu bar and
134 * desktop pane which in turn acts as the container for the views
135 * and controllers. A single ScuflModel is shared between all these
136 * contained components.
137 */
138 public Workbench() {
139 super("Scufl Workbench");
140 int inset = 50;
141 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
142 setBounds(inset, inset,
143 screenSize.width - inset*2,
144 screenSize.height-inset*2);
145
146 // Initialise the scufl model
147 this.model = new ScuflModel();
148
149 //Quit this app when the big window closes.
150 addWindowListener(new WindowAdapter() {
151 public void windowClosing(WindowEvent e) {
152 System.exit(0);
153 }
154 });
155
156 // Create the desktop pane and menu
157 desktop = new JDesktopPane();
158 setContentPane(desktop);
159 setJMenuBar(createMenuBar());
160 }
161
162 /**
163 * Create the menus required by the application
164 */
165 JMenuBar createMenuBar() {
166 JMenuBar menuBar = new JMenuBar();
167
168 // Menu to handle opening XScufl files, clearing the model and saving
169 JMenu fileMenu = new JMenu("File");
170 JMenuItem openScufl = new JMenuItem("Import XScufl Definition", importIcon);
171 openScufl.addActionListener(new ActionListener() {
172 public void actionPerformed(ActionEvent e) {
173 // Load an XScufl definition here
174 int returnVal = fc.showOpenDialog(Workbench.this);
175 if (returnVal == JFileChooser.APPROVE_OPTION) {
176 File file = fc.getSelectedFile();
177 try {
178 XScuflParser.populate(file.toURL().openStream(), Workbench.this.model, null);
179 }
180 catch (Exception ex) {
181 JOptionPane.showMessageDialog(null,
182 "Problem opening XScufl from file : \n"+ex.getMessage(),
183 "Exception!",
184 JOptionPane.ERROR_MESSAGE);
185 }
186 }
187 }
188 });
189 fileMenu.add(openScufl);
190 // Load from web
191 JMenuItem openScuflURL = new JMenuItem("Import XScufl Definition from web",openurlIcon);
192 openScuflURL.addActionListener(new ActionListener() {
193 public void actionPerformed(ActionEvent ae) {
194 try {
195 String name = (String)JOptionPane.showInputDialog(null,
196 "URL of an XScufl definition to open?",
197 "URL Required",
198 JOptionPane.QUESTION_MESSAGE,
199 null,
200 null,
201 "http://");
202 if (name != null) {
203 XScuflParser.populate((new URL(name)).openStream(), Workbench.this.model, null);
204 }
205 }
206 catch (Exception ex) {
207 JOptionPane.showMessageDialog(null,
208 "Problem opening XScufl from web : \n"+ex.getMessage(),
209 "Exception!",
210 JOptionPane.ERROR_MESSAGE);
211 }
212 }
213 });
214
215 fileMenu.add(openScuflURL);
216 JMenuItem saveScufl = new JMenuItem("Save as XScufl", saveIcon);
217 saveScufl.addActionListener(new ActionListener() {
218 public void actionPerformed(ActionEvent e) {
219 // Save to XScufl
220 try {
221 int returnVal = fc.showSaveDialog(Workbench.this);
222 if (returnVal == JFileChooser.APPROVE_OPTION) {
223 File file = fc.getSelectedFile();
224 XScuflView xsv = new XScuflView(Workbench.this.model);
225 PrintWriter out = new PrintWriter(new FileWriter(file));
226 out.println(xsv.getXMLText());
227 Workbench.this.model.removeListener(xsv);
228 out.flush();
229 out.close();
230 }
231 }
232 catch (Exception ex) {
233 throw new RuntimeException(ex.getMessage());
234 }
235 }
236 });
237 fileMenu.add(saveScufl);
238
239 // Sub menu for the various dot save options
240 JMenu dotSubMenu = new JMenu("Save as Dot");
241 dotSubMenu.setIcon(saveIcon);
242 fileMenu.add(dotSubMenu);
243
244 JMenuItem noPorts = new JMenuItem("No ports shown", saveIcon);
245 JMenuItem boundPorts = new JMenuItem("Bound ports only", saveIcon);
246 JMenuItem allPorts = new JMenuItem("All ports shown", saveIcon);
247 dotSubMenu.add(noPorts);
248 dotSubMenu.add(boundPorts);
249 dotSubMenu.add(allPorts);
250 noPorts.addActionListener(new ActionListener() {
251 public void actionPerformed(ActionEvent ae) {
252 try {
253 int returnVal = fc.showSaveDialog(Workbench.this);
254 if (returnVal == JFileChooser.APPROVE_OPTION) {
255 File file = fc.getSelectedFile();
256 DotView dv = new DotView(Workbench.this.model);
257 dv.setPortDisplay(DotView.NONE);
258 PrintWriter out = new PrintWriter(new FileWriter(file));
259 out.println(dv.getDot());
260 Workbench.this.model.removeListener(dv);
261 out.flush();
262 out.close();
263 }
264 }
265 catch (Exception ex) {
266 throw new RuntimeException(ex.getMessage());
267 }
268 }
269 });
270 boundPorts.addActionListener(new ActionListener() {
271 public void actionPerformed(ActionEvent ae) {
272 try {
273 int returnVal = fc.showSaveDialog(Workbench.this);
274 if (returnVal == JFileChooser.APPROVE_OPTION) {
275 File file = fc.getSelectedFile();
276 DotView dv = new DotView(Workbench.this.model);
277 dv.setPortDisplay(DotView.BOUND);
278 PrintWriter out = new PrintWriter(new FileWriter(file));
279 out.println(dv.getDot());
280 Workbench.this.model.removeListener(dv);
281 out.flush();
282 out.close();
283 }
284 }
285 catch (Exception ex) {
286 throw new RuntimeException(ex.getMessage());
287 }
288 }
289 });
290 allPorts.addActionListener(new ActionListener() {
291 public void actionPerformed(ActionEvent ae) {
292 try {
293 int returnVal = fc.showSaveDialog(Workbench.this);
294 if (returnVal == JFileChooser.APPROVE_OPTION) {
295 File file = fc.getSelectedFile();
296 DotView dv = new DotView(Workbench.this.model);
297 dv.setPortDisplay(DotView.ALL);
298 PrintWriter out = new PrintWriter(new FileWriter(file));
299 out.println(dv.getDot());
300 Workbench.this.model.removeListener(dv);
301 out.flush();
302 out.close();
303 }
304 }
305 catch (Exception ex) {
306 throw new RuntimeException(ex.getMessage());
307 }
308 }
309 });
310
311 JMenuItem clearModel = new JMenuItem("Reset model data", deleteIcon);
312 clearModel.addActionListener(new ActionListener() {
313 public void actionPerformed(ActionEvent e) {
314 Workbench.this.model.clear();
315 }
316 });
317 fileMenu.add(clearModel);
318
319 // Menu to show different UI widgets
320 JMenu windowMenu = new JMenu("Views");
321 JMenuItem explorerView = new JMenuItem("Scufl Explorer");
322 explorerView.addActionListener(new ActionListener() {
323 public void actionPerformed(ActionEvent e) {
324 // Show a scufl explorer panel
325 ScuflModelExplorer thing = new ScuflModelExplorer();
326 GenericUIComponentFrame frame = new GenericUIComponentFrame(Workbench.this.model, thing);
327 Workbench.this.desktop.add(frame);
328 frame.moveToFront();
329 }
330 });
331 windowMenu.add(explorerView);
332 JMenuItem diagramView = new JMenuItem("Workflow Diagram");
333 diagramView.addActionListener(new ActionListener() {
334 public void actionPerformed(ActionEvent e) {
335 // Show a scufl diagram panel
336 ScuflDiagram thing = new ScuflDiagram();
337 GenericUIComponentFrame frame = new GenericUIComponentFrame(Workbench.this.model, thing);
338 Workbench.this.desktop.add(frame);
339 frame.moveToFront();
340 }
341 });
342 windowMenu.add(diagramView);
343 JMenuItem xscuflView = new JMenuItem("XScufl View");
344 xscuflView.addActionListener(new ActionListener() {
345 public void actionPerformed(ActionEvent e) {
346 // Show an XScufl panel
347 XScuflTextArea thing = new XScuflTextArea();
348 GenericUIComponentFrame frame = new GenericUIComponentFrame(Workbench.this.model, thing);
349 Workbench.this.desktop.add(frame);
350 frame.moveToFront();
351 }
352 });
353 windowMenu.add(xscuflView);
354 JMenuItem dotView = new JMenuItem("Dot View");
355 dotView.addActionListener(new ActionListener() {
356 public void actionPerformed(ActionEvent e) {
357 // Show a Dot panel
358 DotTextArea thing = new DotTextArea();
359 GenericUIComponentFrame frame = new GenericUIComponentFrame(Workbench.this.model, thing);
360 Workbench.this.desktop.add(frame);
361 frame.moveToFront();
362 }
363 });
364 windowMenu.add(dotView);
365 JMenuItem servicePanel = new JMenuItem("Service Panel");
366 servicePanel.addActionListener(new ActionListener() {
367 public void actionPerformed(ActionEvent e) {
368 // Show a service selection panel
369 ScavengerTree thing = new ScavengerTree();
370 GenericUIComponentFrame frame = new GenericUIComponentFrame(Workbench.this.model, thing);
371 Workbench.this.desktop.add(frame);
372 frame.moveToFront();
373 }
374 });
375 windowMenu.add(servicePanel);
376 JMenuItem inputPanel = new JMenuItem("Workflow Input Panel");
377 inputPanel.addActionListener(new ActionListener() {
378 public void actionPerformed(ActionEvent e) {
379 // Show a workflow input panel
380 EnactorLaunchPanel thing = new EnactorLaunchPanel();
381 GenericUIComponentFrame frame = new GenericUIComponentFrame(Workbench.this.model, thing);
382 Workbench.this.desktop.add(frame);
383 frame.moveToFront();
384 }
385 });
386 windowMenu.add(inputPanel);
387
388
389 menuBar.add(fileMenu);
390 menuBar.add(windowMenu);
391 return menuBar;
392
393 }
394
395 }