View Javadoc

1   package gov.nasa.pds.ltdt.gui;
2   
3   import gov.nasa.pds.ltdt.gui.configuration.LTDTKeys;
4   import gov.nasa.pds.ltdt.gui.util.ProjectUtility;
5   import gov.nasa.pds.ltdt.gui.util.Utility;
6   
7   import java.awt.Dimension;
8   import java.awt.event.ActionEvent;
9   import java.awt.event.ActionListener;
10  import java.io.File;
11  import java.io.IOException;
12  import java.net.MalformedURLException;
13  import java.net.URL;
14  import java.util.Properties;
15  
16  import javax.swing.BorderFactory;
17  import javax.swing.Box;
18  import javax.swing.BoxLayout;
19  import javax.swing.JFileChooser;
20  import javax.swing.JLabel;
21  import javax.swing.JOptionPane;
22  import javax.swing.JPanel;
23  
24  /***
25   * Generate a panel to handle selection of template from file system.
26   * @author jwang
27   *
28   */
29  public class NewProjectFromFilePane extends JPanel {
30  	
31  	private Box mainBox, box1, box2;
32  	public JFileChooser jfc;
33  	
34  	private JLabel jlabDescription;
35  	private ProjectGalleryDialog pd;
36  	private Properties props;
37  	private MainWindow window;
38  	private File inFile=null;
39  	private String directory;
40  	private int status;
41  	
42  	/***
43  	 * Constructor. 
44  	 * @param props Tool properties
45  	 * @param window Main Window
46  	 * @param pd  Project Gallery dialog
47  	 * @param directory Template location/directory (optional)
48  	 */
49  	public NewProjectFromFilePane (Properties props, MainWindow window, ProjectGalleryDialog pd, String directory) {
50  		
51  		this.props = props;
52  		this.window = window;
53  		this.pd = pd;
54  		this.directory = directory;
55  		
56  		this.setOpaque(true);
57  		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
58  		
59  		this.setBorder(BorderFactory.createEmptyBorder());
60  		this.setMaximumSize (new Dimension(300,280));
61  		
62  		constructFileChooserPane();
63  				
64  	}
65  	
66  	private void constructFileChooserPane() {
67  		
68  		mainBox = Box.createVerticalBox();
69  		mainBox.setBorder(BorderFactory.createEmptyBorder());
70  		
71  		box1 = Box.createHorizontalBox();
72  		box1.setBorder(BorderFactory.createEmptyBorder(0, 0, 15, 0));
73  		
74  		jlabDescription = new JLabel();
75  		jlabDescription.setText("Create a new project from a file. ");
76  
77  		box1.add(jlabDescription);
78  
79  		box2 = Box.createHorizontalBox();
80  		box2.setBorder(BorderFactory.createEmptyBorder());
81  		
82  		jfc = new JFileChooser();
83  		jfc.setSize(new Dimension(260,260));
84  		if (directory!=null) {
85  			jfc.setCurrentDirectory(new File(directory));
86  			
87  		} else {
88  			jfc.setCurrentDirectory(new File(System.getProperty("user.dir")));
89  		}
90  		jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
91  		jfc.addActionListener(new ActionListener() {
92  
93  			public void actionPerformed(ActionEvent ae) {	
94  		
95  				if ("ApproveSelection".equals(ae.getActionCommand())) {					
96  					createNewFromFile();				
97  				}
98  				// anything other than selecting OK, indicates a cancellation
99  				else {
100 					cancelCreateNewFromFile();
101 				}				
102 			}
103 			
104 		});
105 				
106 		box2.add(jfc);
107 		
108 		mainBox.add(box1);
109 		mainBox.add(box2);
110 				
111 		this.add(mainBox);
112 	}
113 	
114 	private void createNewFromFile()  {
115 		Utility.initializeEmptyProject(props, window);
116 		
117 		inFile=jfc.getSelectedFile();
118 			
119 		URL fileURL=null;
120 		try {
121 			fileURL = getSelectedFileNameURL();
122 			
123 			if (ProjectUtility.openURL(props,window,fileURL)==LTDTKeys.ERROR) {
124 				ProjectUtility.openFile(props, window, true);
125 			}
126 							
127 		}
128 		catch (MalformedURLException e) {
129 	 		JOptionPane.showMessageDialog(
130 	 				window, "MalformedURL Exception when opening file "+fileURL.toString()+" "+e.getMessage());
131 	       	status = LTDTKeys.ERROR;
132 	       	this.pd.setVisible(false);
133 	    	window.editorPane.jta.setCaretPosition(0);
134 	    	window.editorPane.jta.grabFocus();
135 	       	return;
136 	 	}
137 		catch (IOException e2) {
138 			JOptionPane.showMessageDialog(
139 	 				window, "IO Exception when opening file "+getSelectedFileName()+" "+e2.getMessage());
140 			status = LTDTKeys.ERROR;
141 			this.pd.setVisible(false);
142 			window.editorPane.jta.setCaretPosition(0);
143 			window.editorPane.jta.grabFocus();
144 			return;
145 		}
146 			
147 		status = LTDTKeys.SUCCESS;
148 		this.pd.setVisible(false);
149 		window.editorPane.jta.setCaretPosition(0);
150 		window.editorPane.jta.grabFocus();
151 		
152 	}
153 	
154 	/***
155 	 * Retrieves name of the user selected template file.
156 	 * @return fully qualified file name selected 
157 	 */
158 	public String getSelectedFileName() {
159 		return (inFile.getPath());
160 	}
161 	/***
162 	 * Retrieves name of the user selected template file in URL
163 	 * @return fully qualified file name selected, in URL form
164 	 * @throws MalformedURLException
165 	 * @throws IOException
166 	 */
167 	public URL getSelectedFileNameURL() throws MalformedURLException, IOException {
168 		return Utility.toURL(inFile.getPath());
169 	}
170 	
171 	private void cancelCreateNewFromFile() {
172 		status = LTDTKeys.CANCELLED;
173 		this.pd.setVisible(false);
174 		window.editorPane.jta.setCaretPosition(0);
175 		window.editorPane.jta.grabFocus();
176 	}
177 
178 	public int getFileSelectionStatus() {
179 		return status; 
180 	}
181 }