1 41 42 package com.mullassery.act.gui; 43 44 import java.awt.BorderLayout ; 45 import java.awt.Color ; 46 import java.awt.Toolkit ; 47 import java.awt.event.ActionEvent ; 48 import java.awt.event.ActionListener ; 49 import java.awt.event.WindowAdapter ; 50 import java.awt.event.WindowEvent ; 51 import java.io.BufferedInputStream ; 52 import java.io.File ; 53 54 import javax.swing.BorderFactory ; 55 import javax.swing.Box ; 56 import javax.swing.ImageIcon ; 57 import javax.swing.JFrame ; 58 import javax.swing.JLabel ; 59 import javax.swing.JMenu ; 60 import javax.swing.JMenuBar ; 61 import javax.swing.JMenuItem ; 62 import javax.swing.JProgressBar ; 63 import javax.swing.JWindow ; 64 import javax.swing.Timer ; 65 import javax.swing.ToolTipManager ; 66 import javax.swing.border.Border ; 67 import javax.swing.event.MenuEvent ; 68 import javax.swing.event.MenuListener ; 69 70 import org.w3c.dom.Element ; 71 import org.w3c.dom.Node ; 72 73 import com.mullassery.act.ACTException; 74 import com.mullassery.act.TaskMaster; 75 import com.mullassery.act.util.GUIUtil; 76 import com.mullassery.act.util.ResourceUtil; 77 import com.mullassery.act.util.XMLUtil; 78 79 83 public class ACT extends JFrame { 84 85 public static final String ACT_FRAME_TITLE = "ACT.frameTitle"; private JMenuItem aboutMenuItem; 87 private JMenuItem contentsMenuItem; 88 private JMenuItem exitMenuItem; 89 private JMenu fileMenu; 90 private TaskPanel focusedTP; 91 private JMenu helpMenu; 92 private JMenuBar menuBar; 93 94 private JMenu taskMenu; 95 private JMenuItem openMenuItem; 96 private JMenuItem saveMenuItem; 97 98 private final SettingsManager sm = new SettingsManager(this); 99 private final MRUManager mru = new MRUManager(this); 100 private final TaskMaster taskMaster = new TaskMaster(); 101 private final TaskManager tm = new TaskManager(this); 102 private int loadingPercentage; 103 private String loadingMessage = "Starting ACT.."; 104 105 109 public ACT() throws Exception { 110 this(null); 111 } 112 120 public ACT(String mainTask) throws Exception { 121 super(GUIUtil.getString(ACT_FRAME_TITLE)); 122 new Splash(); 123 initComponents(); 124 if (mainTask != null) { 125 Element elm = 126 XMLUtil.getDocumentBuilder().newDocument().createElement( 127 GUIUtil.TASK); 128 elm.setAttribute(GUIUtil.TASK_NAME, mainTask); 129 elm.setAttribute(GUIUtil.TASK_DISPLAY, mainTask); 130 createTaskPanel(elm); 131 } 132 setLoadingProgress(100, "ACT Loaded"); 133 } 134 135 private void setLoadingProgress(int percentage, String message) { 136 synchronized (this) { 137 if (percentage <= 100) 138 loadingPercentage = percentage; 139 else 140 loadingPercentage = 100; 141 loadingMessage = message; 142 } 143 } 144 145 public static void main(String [] args) throws Exception { 146 ACT act = null; 147 if (args.length == 0) { 148 act = new ACT(); 149 } else { 150 if (args[0].equals("-dir")) { 151 if (args.length < 2) { 152 System.err.println( 153 "Usage: ACT [-dir parent_dir_of_act-setting.xml] [taskname]"); 154 return; 155 } 156 File dir = new File (args[1]); 157 if (dir.exists() && dir.isDirectory()) { 158 ResourceUtil.actDir = args[1]; 159 } else { 160 System.err.println( 161 args[1]+" is not a valid directory!"); 162 return; 163 } 164 if (args.length > 2) { 165 act = new ACT(args[2]); 166 } else { 167 act = new ACT(); 168 } 169 } else { 170 act = new ACT(args[0]); 171 } 172 } 173 } 174 175 public TaskPanel createTaskPanel(Element tsk) throws ACTException { 176 final TaskPanel tp = new TaskPanel(ACT.this, taskMaster, tsk); 177 tp.addWindowListener(new TaskWindowListener()); 178 focusedTP = tp; 179 return tp; 180 } 181 182 void exitForm() { 183 dispose(); 184 } 185 186 public TaskPanel getFocusedTP() { 187 return focusedTP; 188 } 189 190 public TaskMaster getTaskMaster() { 191 return taskMaster; 192 } 193 194 197 private void initComponents() { 198 setLoadingProgress(10, "Opening Application"); 199 addWindowListener(new WindowAdapter () { 200 public void windowClosing(WindowEvent evt) { 201 exitForm(); 202 } 203 public void windowClosed(WindowEvent evt) { 204 System.exit(0); 205 } 206 }); 207 setLoadingProgress(30, "Loading Tasks.."); 208 ToolTipManager.sharedInstance().setEnabled(true); 209 ToolTipManager.sharedInstance().setInitialDelay(300); 210 setLoadingProgress(70, "Populating Task menu"); 211 initMenu(); 212 setLoadingProgress(90, "Completed loading menu"); 213 pack(); 214 GUIUtil.centralize(this); 215 show(); 216 } 217 218 221 private void initMenu() { 222 menuBar = new JMenuBar (); 223 fileMenu = new JMenu ("File"); openMenuItem = new JMenuItem ("Open"); fileMenu.add(openMenuItem); 226 saveMenuItem = new JMenuItem ("Save"); saveMenuItem.setEnabled(false); 228 fileMenu.add(saveMenuItem); 229 exitMenuItem = new JMenuItem ("Exit"); fileMenu.add(exitMenuItem); 231 menuBar.add(fileMenu); 232 233 taskMenu = sm.getTaskMenu(); 234 menuBar.add(taskMenu); 235 236 helpMenu = new JMenu ("Help"); contentsMenuItem = new JMenuItem ("Contents"); helpMenu.add(contentsMenuItem); 239 aboutMenuItem = new JMenuItem ("About"); helpMenu.add(aboutMenuItem); 241 242 menuBar.add(Box.createHorizontalGlue()); 243 menuBar.add(helpMenu); 244 245 setJMenuBar(menuBar); 246 247 openMenuItem.addActionListener(new ActionListener () { 248 public void actionPerformed(ActionEvent evt) { 249 tm.openTask(); 250 } 251 }); 252 253 saveMenuItem.addActionListener(new ActionListener () { 254 public void actionPerformed(ActionEvent evt) { 255 tm.saveTask(getFocusedTP()); 256 } 257 }); 258 259 exitMenuItem.addActionListener(new ActionListener () { 260 public void actionPerformed(ActionEvent evt) { 261 exitForm(); 262 } 263 }); 264 265 aboutMenuItem.addActionListener(new ActionListener () { 266 public void actionPerformed(ActionEvent evt) { 267 showAbout(); 268 } 269 }); 270 271 fileMenu.addMenuListener(new MenuListener () { 272 public void menuCanceled(MenuEvent e) { 273 } 274 public void menuDeselected(MenuEvent e) { 275 } 276 public void menuSelected(MenuEvent e) { 277 mru.updateMRU(fileMenu); 278 } 279 }); 280 } 281 282 286 public void reStart() { 287 sm.saveSettings(); 288 menuBar.remove(taskMenu); 289 menuBar.remove(helpMenu); 290 menuBar.add(sm.getTaskMenu()); 291 menuBar.add(helpMenu); 292 this.pack(); 293 } 294 295 void showAbout() { 296 GUIUtil.showInfoMessage(this, GUIUtil.getString("ACT.aboutMessage")); } 298 299 303 private class TaskWindowListener extends WindowAdapter { 304 305 public void windowClosing(WindowEvent evt) { 306 mru.addToMRU((TaskPanel) evt.getSource()); 307 Element tv = focusedTP.getTaskValues(); 308 Node tskChild = 309 tv.getElementsByTagName( 310 tv.getAttribute(GUIUtil.TASK_NAME)).item( 311 0); 312 if ((tskChild != null 313 && (tskChild.getAttributes().getLength() > 0 314 || tskChild.getChildNodes().getLength() > 0)) 315 && GUIUtil.getConfirmation( 316 focusedTP, 317 "Would you like to save this task and its values to a file?")) { 318 if (!tm.saveTask(focusedTP)) { 319 return; 320 } 321 } 322 focusedTP.dispose(); 323 focusedTP = null; 324 saveMenuItem.setEnabled(false); 325 326 } 327 328 public void windowGainedFocus(WindowEvent evt) { 329 updateFocusedTP(evt); 330 } 331 332 public void windowOpened(WindowEvent evt) { 333 updateFocusedTP(evt); 334 } 335 336 public void windowActivated(WindowEvent evt) { 337 updateFocusedTP(evt); 338 } 339 340 private void updateFocusedTP(WindowEvent evt) { 341 focusedTP = (TaskPanel) evt.getSource(); 342 saveMenuItem.setEnabled(true); 343 } 344 } 345 346 private class Splash extends JWindow { 347 Timer timer; 348 Splash() { 349 init(); 350 } 351 352 void init() { 353 ImageIcon ic = null; 354 JLabel imagePanel = null; 355 final JProgressBar progressBar = new JProgressBar (0, 100); 356 357 try { 358 BufferedInputStream bin = new BufferedInputStream (ACT.class.getResourceAsStream("splash.jpg")); byte[] img = new byte[bin.available()]; 360 bin.read(img); 361 ic = new ImageIcon (img); 362 imagePanel = new JLabel (ic); 363 } catch (Exception e) { 364 imagePanel = new JLabel (); 365 } 366 367 progressBar.setStringPainted(true); 368 progressBar.setString("Starting ACT...."); 369 final Border bdr = BorderFactory.createLineBorder(Color.black, 1); 370 imagePanel.setBorder(bdr); 371 progressBar.setBorder(bdr); 372 373 this.getContentPane().setLayout(new BorderLayout ()); 374 this.getContentPane().add(imagePanel, BorderLayout.CENTER); 375 this.getContentPane().add(progressBar, BorderLayout.SOUTH); 376 ActionListener poller = new ActionListener () { 378 public void actionPerformed(ActionEvent evt) { 379 progressBar.setValue(loadingPercentage); 380 progressBar.setString(loadingMessage); 381 if (loadingPercentage >= 100) { 382 Toolkit.getDefaultToolkit().beep(); 383 timer.stop(); 384 timer = null; 385 Splash.this.dispose(); 386 } 387 } 388 }; 389 timer = new Timer (200, poller); 390 timer.setRepeats(true); 391 timer.start(); 392 393 pack(); 394 GUIUtil.centralize(this); 395 show(); 396 } 397 } 398 399 } 400
| Popular Tags
|