comparison java-genomics-toolkit/gui/edu/unc/genomics/ToolRunnerFrame.java @ 0:1daf3026d231

Upload alpha version
author timpalpant
date Mon, 13 Feb 2012 21:55:55 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1daf3026d231
1 package edu.unc.genomics;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Component;
6 import java.awt.Container;
7 import java.awt.Dimension;
8 import java.awt.Font;
9
10 import javax.swing.JFrame;
11 import javax.swing.JPanel;
12
13 import javax.swing.BorderFactory;
14 import javax.swing.JComponent;
15 import javax.swing.JLabel;
16 import javax.swing.JList;
17 import javax.swing.JMenuBar;
18 import javax.swing.JMenu;
19 import javax.swing.JMenuItem;
20 import javax.swing.JButton;
21 import javax.swing.JOptionPane;
22 import javax.swing.JPopupMenu;
23 import javax.swing.JScrollPane;
24 import javax.swing.SwingConstants;
25
26 import javax.swing.JProgressBar;
27 import javax.swing.JSplitPane;
28 import javax.swing.BoxLayout;
29 import javax.swing.JTabbedPane;
30 import javax.swing.JTextPane;
31
32 import javax.swing.event.TreeSelectionListener;
33 import javax.swing.event.TreeSelectionEvent;
34 import javax.xml.parsers.ParserConfigurationException;
35
36 import org.apache.log4j.Logger;
37 import org.simplericity.macify.eawt.Application;
38 import org.simplericity.macify.eawt.ApplicationEvent;
39 import org.simplericity.macify.eawt.ApplicationListener;
40 import org.simplericity.macify.eawt.DefaultApplication;
41 import org.xml.sax.SAXException;
42
43 import com.beust.jcommander.JCommander;
44
45 import java.awt.event.ActionListener;
46 import java.awt.event.ActionEvent;
47 import java.io.IOException;
48
49 /**
50 * The main ToolRunner window
51 * and controller for creating, running, and managing Jobs
52 *
53 * @author timpalpant
54 *
55 */
56 public class ToolRunnerFrame extends JFrame implements ApplicationListener {
57
58 private static final long serialVersionUID = 6454774196137357898L;
59 private static final Logger log = Logger.getLogger(ToolRunnerFrame.class);
60
61 private final Application application = new DefaultApplication();
62
63 private final JPanel contentPane = new JPanel();
64 private final JSplitPane splitPane = new JSplitPane();
65 private final JPanel mainPane = new JPanel();
66 private final JProgressBar progressBar = new JProgressBar();
67 private final JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP);
68 private final JobConfigPanel configurationPanel = new JobConfigPanel();
69 private final JTextPane helpTextPanel = new JTextPane();
70 private final ToolsTree toolsTree = new ToolsTree();
71
72 private final JobQueue queue = new JobQueue();
73 private final JobQueueManager queueManager = new JobQueueManager(queue);
74 private final JList<SubmittedJob> queueList = new JList<>(queue);
75
76 /**
77 * Create the frame.
78 */
79 public ToolRunnerFrame() {
80 //application.addPreferencesMenuItem();
81 //application.setEnabledPreferencesMenu(true);
82 application.addApplicationListener(this);
83
84 // set OS X-specific properties
85 if (application.isMac()) {
86 setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
87 //toolsTree.putClientProperty("Quaqua.Tree.style", "sourceList");
88 } else {
89 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
90 }
91 setTitle("Genomics Toolkit Tool Runner");
92 setBounds(100, 100, 1000, 600);
93
94 contentPane.setBorder(BorderFactory.createEmptyBorder());
95 contentPane.setLayout(new BorderLayout(0, 0));
96 setContentPane(contentPane);
97
98 initializeChildren();
99 initializeMenuBar();
100 }
101
102 private void initializeChildren() {
103 splitPane.setBorder(BorderFactory.createEmptyBorder());
104 contentPane.add(splitPane, BorderLayout.CENTER);
105
106 initializeQueuePanel();
107 initializeToolsTree();
108
109 mainPane.setLayout(new BorderLayout(0, 0));
110 mainPane.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
111 mainPane.add(tabbedPane, BorderLayout.CENTER);
112
113 JPanel runPanel = new JPanel();
114 runPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
115 runPanel.setLayout(new BoxLayout(runPanel, BoxLayout.X_AXIS));
116 runPanel.add(progressBar);
117 JButton btnRun = new JButton("Run");
118 btnRun.addActionListener(new ActionListener() {
119 public void actionPerformed(ActionEvent e) {
120 addJobToQueue();
121 }
122 });
123 runPanel.add(btnRun);
124 mainPane.add(runPanel, BorderLayout.SOUTH);
125 splitPane.setRightComponent(mainPane);
126
127 initializeConfigurationPanel();
128 initializeHelpPanel();
129 }
130
131 private void initializeMenuBar() {
132 JMenuBar menuBar = new JMenuBar();
133 setJMenuBar(menuBar);
134
135 JMenu mnFileMenu = new JMenu("File");
136 menuBar.add(mnFileMenu);
137
138 JMenuItem mntmAssemblyManager = new JMenuItem("Assembly manager");
139 mntmAssemblyManager.addActionListener(new ActionListener() {
140 public void actionPerformed(ActionEvent e) {
141 JMenuItem menuItem = (JMenuItem) e.getSource();
142 JPopupMenu popupMenu = (JPopupMenu) menuItem.getParent();
143 Component invoker = popupMenu.getInvoker(); //this is the JMenu (in my code)
144 JComponent invokerAsJComponent = (JComponent) invoker;
145 Container topLevel = invokerAsJComponent.getTopLevelAncestor();
146 AssemblyManagerDialog dialog = new AssemblyManagerDialog((JFrame) topLevel);
147 //dialog.getRootPane().putClientProperty("Window.style", "small");
148 dialog.setVisible(true);
149 }
150 });
151 mnFileMenu.add(mntmAssemblyManager);
152
153 JMenu mnHelpMenu = new JMenu("Help");
154 menuBar.add(mnHelpMenu);
155
156 JMenuItem mntmHelpContents = new JMenuItem("Help Contents");
157 mnHelpMenu.add(mntmHelpContents);
158
159 if (!application.isMac()) {
160 JMenuItem mntmAbout = new JMenuItem("About");
161 mnHelpMenu.add(mntmAbout);
162 mnHelpMenu.addActionListener(new ActionListener() {
163 public void actionPerformed(ActionEvent e) {
164 handleAbout(null);
165 }
166 });
167 }
168 }
169
170 private void initializeQueuePanel() {
171 JPanel queuePanel = new JPanel();
172 queuePanel.setLayout(new BoxLayout(queuePanel, BoxLayout.PAGE_AXIS));
173 queuePanel.setBorder(BorderFactory.createEmptyBorder());
174 contentPane.add(queuePanel, BorderLayout.EAST);
175
176 JLabel queueLabel = new JLabel("Job Queue");
177 queueLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
178 queueLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
179 queuePanel.add(queueLabel);
180
181 queueList.setBackground(contentPane.getBackground());
182 queueList.setCellRenderer(new JobQueueCellRenderer());
183 JScrollPane queueListScrollPane = new JScrollPane(queueList);
184 queueListScrollPane.setBorder(BorderFactory.createEmptyBorder());
185 queueListScrollPane.setBackground(contentPane.getBackground());
186 queueListScrollPane.setPreferredSize(new Dimension(200, Integer.MAX_VALUE));
187 queuePanel.add(queueListScrollPane);
188 }
189
190 private void initializeConfigurationPanel() {
191 JScrollPane configScrollPane = new JScrollPane(configurationPanel);
192 configScrollPane.setBorder(BorderFactory.createEmptyBorder());
193 tabbedPane.addTab("Tool Configuration", null, configScrollPane, "Configure tool");
194 }
195
196 private void initializeHelpPanel() {
197 JPanel helpPanel = new JPanel();
198 tabbedPane.addTab("Help", null, helpPanel, null);
199 helpPanel.setLayout(new BorderLayout(0, 0));
200
201 helpTextPanel.setEditable(false);
202 helpTextPanel.setBackground(tabbedPane.getBackground());
203 Font mono = new Font("Monospaced", helpTextPanel.getFont().getStyle(), helpTextPanel.getFont().getSize());
204 helpTextPanel.setFont(mono);
205 JScrollPane helpScrollPane = new JScrollPane(helpTextPanel);
206 helpScrollPane.setBorder(BorderFactory.createEmptyBorder());
207 helpPanel.add(helpScrollPane);
208 }
209
210 private void initializeToolsTree() {
211 try {
212 ToolsTreeModel model = ToolsTreeModel.loadDefaultConfig();
213 toolsTree.setModel(model);
214 } catch (ParserConfigurationException | SAXException | IOException e1) {
215 log.error("Error loading tool configuration file");
216 e1.printStackTrace();
217 System.exit(-1);
218 } catch (ClassNotFoundException e) {
219 log.error("Error loading tool: " + e.getMessage());
220 e.printStackTrace();
221 System.exit(-1);
222 }
223
224 toolsTree.addTreeSelectionListener(new TreeSelectionListener() {
225 public void valueChanged(TreeSelectionEvent e) {
226 changeTool();
227 }
228 });
229
230 JScrollPane toolsTreeScrollPane = new JScrollPane(toolsTree);
231 toolsTreeScrollPane.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.LIGHT_GRAY));
232 splitPane.setLeftComponent(toolsTreeScrollPane);
233 }
234
235 /**
236 * Change the configuration panel to the currently selected tool
237 * to configure a new Job
238 */
239 private void changeTool() {
240 // Returns the last path element of the selection.
241 Object node = toolsTree.getLastSelectedPathComponent();
242 // Nothing is selected
243 if (node == null) {
244 return;
245 }
246
247 if (node instanceof ToolsTreeNode) {
248 ToolsTreeNode toolNode = (ToolsTreeNode) node;
249 try {
250 Class<? extends CommandLineTool> tool = toolNode.getClazz();
251
252 // Set up the configuration panel to configure this tool
253 Job job = new Job(tool);
254 configurationPanel.setJob(job);
255 // Set the help text to the usage
256 helpTextPanel.setText(job.getUsageText());
257 } catch (InstantiationException | IllegalAccessException e) {
258 log.error("Error initializing Job");
259 e.printStackTrace();
260 JOptionPane.showMessageDialog(this, "Error initializing job", "Job Initialization Error", JOptionPane.ERROR_MESSAGE);
261 }
262
263 }
264 }
265
266 private void addJobToQueue() {
267 Job currentJob = configurationPanel.getJob();
268 if (currentJob == null) return;
269
270 // Validate the required parameters
271 log.info("Validating parameters for tool");
272 if (!currentJob.validateArguments()) {
273 configurationPanel.highlightInvalidArguments();
274 return;
275 }
276
277 // Add the job to the queue
278 try {
279 queueManager.submitJob(currentJob);
280 configurationPanel.setJob(null);
281 } catch (JobException e) {
282 log.error("Error adding Job to queue");
283 e.printStackTrace();
284 JOptionPane.showMessageDialog(this, "Error adding job to queue", "Job Queue Error", JOptionPane.ERROR_MESSAGE);
285 }
286 }
287
288 public void handleAbout(ApplicationEvent event) {
289 JOptionPane.showMessageDialog(this, "Java Genomics Toolkit v1.0");
290 if (event != null) {
291 event.setHandled(true);
292 }
293 }
294
295 public void handleOpenApplication(ApplicationEvent event) {
296 // Application was opened
297 }
298
299 public void handleOpenFile(ApplicationEvent event) {
300 //JOptionPane.showMessageDialog(frmToolRunner, "OS X told us to open " + event.getFilename());
301 }
302
303 public void handlePreferences(ApplicationEvent event) {
304 //JOptionPane.showMessageDialog(frmToolRunner, "No preferences available");
305 }
306
307 public void handlePrintFile(ApplicationEvent event) {
308 //JOptionPane.showMessageDialog(frmToolRunner, "OS X told us to print " + event.getFilename());
309 }
310
311 public void handleQuit(ApplicationEvent event) {
312 boolean confirm = true;
313 if (queueManager.isRunning()) {
314 int result = JOptionPane.showConfirmDialog(this, "Jobs are currently running. Are you sure you want to quit?", "Confirm Quit", JOptionPane.OK_CANCEL_OPTION);
315 confirm = (result == JOptionPane.OK_OPTION);
316 }
317
318 if (confirm) {
319 dispose();
320 System.exit(0);
321 }
322 }
323
324 public void handleReOpenApplication(ApplicationEvent event) {
325 //JOptionPane.showMessageDialog(frmToolRunner, "OS X told the application was reopened");
326 setVisible(true);
327 }
328 }