View Javadoc

1   /*
2    * $Id: OpenPreviousProjectAction.java,v 1.9 2008/03/05 22:13:43 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  package org.xmlhammer.gui.actions;
22  
23  import java.net.URI;
24  
25  import javax.swing.SwingUtilities;
26  import javax.xml.bind.JAXBException;
27  import javax.xml.transform.TransformerException;
28  
29  import org.apache.log4j.Logger;
30  import org.bounce.RunnableAction;
31  import org.bounce.util.URIUtils;
32  import org.xmlhammer.gui.XMLHammer;
33  import org.xmlhammer.model.project.Project;
34  
35  /***
36   * An action that can be used to open previously opened XML Hammer projects.
37   *
38   * @version	$Revision: 1.9 $, $Date: 2008/03/05 22:13:43 $
39   * @author Edwin Dankert <edankert@gmail.com>
40   */
41  public class OpenPreviousProjectAction extends RunnableAction {
42   	private static final long serialVersionUID = -702933390366968177L;
43      
44      private URI uri = null;
45   	private XMLHammer parent = null;
46  
47   	/***
48  	 * The constructor for the open previous XML Hammer projects action.
49  	 *
50  	 * @param parent the parent that opens the project.
51  	 */
52   	public OpenPreviousProjectAction( XMLHammer parent, URI uri, int index) {
53   		super( index+" "+URIUtils.getName( uri));
54  		
55  		char[] chars = String.valueOf( index).toCharArray();
56  
57   		putValue( MNEMONIC_KEY, new Integer( chars[0]));
58   		
59   		this.parent = parent;
60  	 	this.uri = uri;
61   	}
62   	
63  	/***
64  	 * The implementation of the copy action.
65  	 */
66   	public void run() {
67  		parent.setWait( true);
68          parent.getStatusPanel().showView(null);
69          parent.getStatusPanel().getBase().setStatus("Opening "+uri.toString()+" ...");
70  //		parent.setStatus( "Opening ...");
71  
72  		// Run in Thread!!!
73   		Runnable runner = new Runnable() {
74   			public void run()  {
75                 Project project = null;
76  
77                 try {
78                      project = OpenAction.open(uri);
79                 } catch ( JAXBException e) {
80  	               	Logger.getLogger(getClass()).error( "JAXB Error", e);
81                 } catch (TransformerException e) {
82  	               	Logger.getLogger(getClass()).error( "Transformer Error", e);
83  			   } finally {
84  				    openProject(uri, project);
85  			   }
86   			}
87   		};
88   		
89   		// Create and start the thread ...
90   		Thread thread = new Thread( runner);
91   		thread.start();
92   	}
93      
94      private void openProject(final URI uri, final Project project) {
95          SwingUtilities.invokeLater( new Runnable() {
96              public void run() {
97                  try {
98                      if (project != null) {
99                          parent.openProject( uri, project);
100                     }
101                 } finally {
102                     parent.getStatusPanel().getBase().setStatus("Done");
103                     parent.getStatusPanel().showView(parent.getProjectsView().getSelectedView());
104                     parent.setWait(false);
105                 }
106             }
107         });
108     }
109 }