View Javadoc

1   /*
2    * $Id: ExecuteAction.java,v 1.26 2008/01/21 22:04:44 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.actions;
23  
24  import java.awt.Toolkit;
25  import java.awt.event.KeyEvent;
26  import java.text.DateFormat;
27  import java.util.Date;
28  
29  import javax.swing.ImageIcon;
30  import javax.swing.JOptionPane;
31  import javax.swing.KeyStroke;
32  
33  import org.bounce.RunnableAction;
34  import org.xmlhammer.Module;
35  import org.xmlhammer.ModuleThread;
36  import org.xmlhammer.ModuleThreadListener;
37  import org.xmlhammer.gui.ProjectView;
38  import org.xmlhammer.gui.XMLHammer;
39  import org.xmlhammer.model.project.Project;
40  
41  
42  /***
43   * An action that can be used to execute a XML Hammer project.
44   *
45   * @version	$Revision: 1.26 $, $Date: 2008/01/21 22:04:44 $
46   * @author Edwin Dankert <edankert@gmail.com>
47   */
48   public class ExecuteAction extends RunnableAction implements ModuleThreadListener {
49      private static final long serialVersionUID = 950916505478822172L;
50  
51      private XMLHammer parent = null;
52      private ModuleThread thread = null;
53  
54   	/***
55  	 * The constructor for the action which executes a project.
56  	 *
57  	 * @param parent the parent frame.
58  	 */
59   	public ExecuteAction( XMLHammer parent) {
60   		super( "Execute");
61  		
62  		this.parent = parent;
63  
64  		putValue( MNEMONIC_KEY, new Integer( 'N'));
65  		putValue( ACCELERATOR_KEY, KeyStroke.getKeyStroke( KeyEvent.VK_N, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
66  		putValue( SMALL_ICON, new ImageIcon( getClass().getResource( "/org/xmlhammer/gui/icons/etool16/run_exc.gif")));
67  		putValue( SHORT_DESCRIPTION, "Execute Project");
68  		
69  		setEnabled( false);
70   	}
71  	
72  	/***
73  	 * The implementation of the execute action.
74  	 */
75   	public void run() {
76   		parent.repaint();
77   		ProjectView view = parent.getProjectsView().getSelectedView();
78          
79          if (view.hasError()) {
80              int result = JOptionPane.showConfirmDialog( parent, 
81                      "Errors exist in project \""+view.getName()+"\".\n"+
82                      "Continue execution?", "Errors in Project", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE);
83  
84              if (result == JOptionPane.NO_OPTION) {
85                  return;
86              }
87          }
88          
89          try {
90              view.getLogger().info( "Start time: "+DateFormat.getDateTimeInstance().format(new Date()));
91  
92              Module module = view.getModule();
93              
94              if (module != null) {
95                  if (module.hasFatal()) {
96                      JOptionPane.showMessageDialog( parent, 
97                              "Unable to initialise the project.\n"+
98                              "See log for more information.", "Fatal Initialization Error", JOptionPane.ERROR_MESSAGE);
99          
100                     return;
101                 } else if (module.hasErrors()) {
102                     int result = JOptionPane.showConfirmDialog( parent, 
103                             "Errors occurred during initialization, please see log for more information.\n"+
104                             "Continue execution?", "Initialization Errors", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE);
105         
106                     if (result == JOptionPane.NO_OPTION) {
107                         return;
108                     }
109                 } else if (module.hasWarnings()) {
110                         int result = JOptionPane.showConfirmDialog(parent, 
111                                 "Warnings occurred during initialization, please see log for more information.\n"+
112                                 "Continue execution?", "Initialization Warnings", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE);
113             
114                         if (result == JOptionPane.NO_OPTION) {
115                             return;
116                         }
117                 }
118     
119                 Project project = view.getProject(null);
120     
121     			ModuleThread thread = new ModuleThread( view.getLogger(), view.getStatusBar(), view.getResult(), project.getInput(), module);
122     
123                 // Create and start the thread ...
124                 setModuleThread(thread);
125                 parent.getStopAction().setModuleThread(thread);
126                 view.setModuleThread(thread);
127     
128                 thread.start();
129             }
130         } catch (Throwable t) {
131             view.getStatusBar().setStatus("Execution Error");
132             view.getLogger().error("Module Execution Error", t);
133         }
134 	}
135     
136     public ModuleThread getModuleThread() {
137         return this.thread;
138     }
139     
140     public void setModuleThread( ModuleThread thread) {
141         if (this.thread != null) {
142             this.thread.removeListener( this);
143         }
144         
145         this.thread = thread;
146 
147         if (thread != null) {
148             setEnabled( false);
149             thread.addListener( this);
150         } else {
151             setEnabled( true);
152             thread = null;
153         }
154     }
155     
156     public void threadFinished( ModuleThread thread) {
157         setEnabled( true);
158         thread.removeListener( this);
159         thread = null;
160     }
161 
162     public void threadStarted( ModuleThread thread) {
163         setEnabled(false);
164     }
165 }