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.Component;
8   import java.awt.Dimension;
9   import java.awt.FlowLayout;
10  import java.awt.Font;
11  import java.awt.event.ActionEvent;
12  import java.awt.event.ActionListener;
13  import java.net.MalformedURLException;
14  import java.net.URL;
15  import java.util.Properties;
16  
17  import javax.swing.BorderFactory;
18  import javax.swing.Box;
19  import javax.swing.BoxLayout;
20  import javax.swing.ButtonGroup;
21  import javax.swing.JButton;
22  import javax.swing.JLabel;
23  import javax.swing.JPanel;
24  import javax.swing.JRadioButton;
25  import javax.swing.JScrollPane;
26  
27  /***
28   * Generate a panel to handle template creation from a pre-defined skeleton template.
29   * @author jwang
30   *
31   */
32  public class NewProjectFromPresetTemplatePane extends JPanel {
33  	
34  	private Box mainBox, box1, box2, box2_1, box2_2, box2_3;
35  	private Box box3, box3_1,  box3_3;
36  	
37  	private JLabel jlabDescription;
38  	private JLabel spacer1, spacer2, spacer3;
39  	
40  	public JRadioButton jrbSpacecraft, jrbEarth;
41  	public JRadioButton jrbAttached, jrbDetached;
42  	public JRadioButton jrbImage, jrbTable, jrbOther;
43  	private ButtonGroup bgObservationType, bgAttachmentType, bgObjectType;
44  	
45  	public JButton jbtnBlank, jbtnCancel, jbtnOK;
46  	
47  	private ProjectGalleryDialog pd;
48  	private MainWindow window;
49  	private Properties props;
50  	
51  	private URL presetTemplateURL;
52  	
53  	/***
54  	 * Construtor
55  	 * @param props Tool properties
56  	 * @param window Main Window
57  	 * @param pd    Project Gallery
58  	 */
59  	public NewProjectFromPresetTemplatePane (Properties props, MainWindow window, ProjectGalleryDialog pd) {
60  		
61  		this.props = props;
62  		this.window = window;
63  		this.pd = pd;
64  		
65  		this.setOpaque(true);
66  		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
67  		
68  		this.setBorder(BorderFactory.createEmptyBorder());
69  		this.setMaximumSize (new Dimension(300,280));
70  		
71  		constructTemplatePane();		
72  	}
73  	
74  	private void constructTemplatePane() {
75  		
76  		spacer1 = new JLabel(" ");
77  		spacer1.setFont(new Font("monospaced",Font.PLAIN, 80));
78  		spacer2 = new JLabel(" ");
79  		spacer2.setFont(new Font("monospaced",Font.PLAIN, 80));
80  		spacer3 = new JLabel(" ");
81  		spacer3.setFont(new Font("monospaced",Font.PLAIN, 60));
82  		
83  		mainBox = Box.createVerticalBox();
84  		mainBox.setBorder(BorderFactory.createEmptyBorder());
85  
86  		box1 = Box.createHorizontalBox();
87  		box1.setBorder(BorderFactory.createEmptyBorder(0, 0, 60, 0));
88  		
89  		jlabDescription = new JLabel();
90  		jlabDescription.setText("Create a new project with a predefined template. ");
91  
92  		box1.add(jlabDescription);
93  		
94  		box2 = Box.createHorizontalBox();
95  		box2.setBorder(BorderFactory.createEmptyBorder());
96  		
97  		box2_1 = Box.createVerticalBox();
98  		//box2_1.setAlignmentX(Component.TOP_ALIGNMENT);
99  		box2_1.setBorder(BorderFactory.createTitledBorder("Observation Type"));
100 		
101 		
102 		box2_2 = Box.createVerticalBox();
103 		//box2_2.setAlignmentX(Component.TOP_ALIGNMENT);
104 		box2_2.setBorder(BorderFactory.createTitledBorder("Attachment Type"));
105 		
106 		
107 		
108 		box2_3 = Box.createVerticalBox();
109 		//box2_3.setAlignmentX(Component.TOP_ALIGNMENT);
110 		box2_3.setBorder(BorderFactory.createTitledBorder("Object Type"));
111 		
112 		
113 		jrbSpacecraft = new JRadioButton("Spacecraft Based", true);
114 		jrbSpacecraft.setEnabled(true);
115 		jrbEarth = new JRadioButton("Earth Based");
116 		jrbEarth.setEnabled(true);
117 		bgObservationType = new ButtonGroup();
118 		bgObservationType.add(jrbSpacecraft);
119 		bgObservationType.add(jrbEarth);
120 		
121 		box2_1.add(jrbSpacecraft);
122 		box2_1.add(jrbEarth);
123 		box2_1.add(spacer1);
124 
125 		
126 
127 		jrbAttached = new JRadioButton("Attached          ",true);
128 		jrbAttached.setEnabled(true);
129 		jrbDetached = new JRadioButton("Detached");
130 		jrbDetached.setEnabled(true);
131 		bgAttachmentType = new ButtonGroup();
132 		bgAttachmentType.add(jrbAttached);
133 		bgAttachmentType.add(jrbDetached);
134 		
135 		box2_2.add(jrbAttached);
136 		box2_2.add(jrbDetached);
137 		box2_2.add(spacer2);
138 	
139 		
140 		jrbImage = new JRadioButton("Image               ",true);
141 		jrbImage.setEnabled(true);
142 		jrbTable = new JRadioButton("Table");
143 		jrbTable.setEnabled(true);
144 		jrbOther = new JRadioButton("Other");
145 		//jrbOther.setEnabled(true);
146 		jrbOther.setEnabled(false);
147 		bgObjectType = new ButtonGroup();
148 		bgObjectType.add(jrbImage);
149 		bgObjectType.add(jrbTable);
150 		bgObjectType.add(jrbOther);
151 		
152 		box2_3.add(jrbImage);
153 		box2_3.add(jrbTable);
154 		box2_3.add(jrbOther);
155 		box2_3.add(spacer3);
156 		
157 		box2.add(box2_1);
158 		box2.add(box2_2);
159 		box2.add(box2_3);
160 		
161 		box3= Box.createHorizontalBox();
162 		box3.setBorder(BorderFactory.createEmptyBorder(150, 0, 0, 0));
163 
164 		box3_1 = Box.createHorizontalBox();
165 		box3_1.setBorder(BorderFactory.createEmptyBorder(0,0,0,155));
166 		jbtnBlank = new JButton("Blank");
167 		jbtnBlank.addActionListener(new ActionListener() {
168 
169 			public void actionPerformed(ActionEvent ae) {
170 				
171 				createBlankTemplate();
172 			}
173 			
174 		});
175 		box3_1.add(jbtnBlank);
176 		
177 		box3_3 = Box.createHorizontalBox();
178 		box3_3.setBorder(BorderFactory.createEmptyBorder(0,100,0,0));
179 		jbtnCancel = new JButton("Cancel");
180 		jbtnCancel.addActionListener(new ActionListener() {
181 
182 			public void actionPerformed(ActionEvent ae) {
183 				cancelCreateNewFromTemplate();	
184 			}
185 			
186 		});
187 		
188 		jbtnOK = new JButton("OK");
189 		jbtnOK.setSelected(true);
190 		jbtnOK.addActionListener (new ActionListener(){
191 
192 			public void actionPerformed(ActionEvent ae) {
193 				createNewFromTemplate();
194 				
195 			}
196 			
197 		});
198 		box3_3.add(jbtnCancel);
199 		box3_3.add(jbtnOK);
200 		
201 		box3.add(box3_1);
202 		box3.add(box3_3);
203 		
204 		mainBox.add(box1);
205 		mainBox.add(box2);
206 		mainBox.add(box3);
207 		
208 		this.add(mainBox);
209 		
210 		
211 	}
212 	
213 	private void createBlankTemplate() {
214 		Utility.initializeEmptyProject(props, window);
215 		ProjectUtility.openBlankPage(props, window);
216 		pd.setVisible(false);
217 		window.editorPane.jta.setCaretPosition(0);
218 		window.editorPane.jta.grabFocus();
219 		// the editor template has not been touch
220 		// so we change this setting manually
221 		props.setProperty(LTDTKeys.PROJECTALTERED, "true");
222 		
223 	}
224 
225 	private void cancelCreateNewFromTemplate() {
226 		pd.setVisible(false);
227 		window.editorPane.jta.setCaretPosition(0);
228 		window.editorPane.jta.grabFocus();
229 		
230 	}
231 	
232 	private void createNewFromTemplate() {
233 		Utility.initializeEmptyProject(props, window);
234 			
235 		if (this.jrbSpacecraft.isSelected()) {
236 				
237 			if (this.jrbAttached.isSelected()) {
238 				if (this.jrbImage.isSelected()) {
239 					this.presetTemplateURL = this.getClass().getResource("SPACECRAFT_SCIENCE-ATT-FIXED-IMAGE.LBT");
240 				}
241 							
242 				else if (this.jrbTable.isSelected()) {
243 					this.presetTemplateURL = this.getClass().getResource("SPACECRAFT_SCIENCE-ATT-FIXED-TABLE.LBT");		
244 				}
245 								
246 				else if (this.jrbOther.isSelected()) {
247 					this.presetTemplateURL = null;
248 				}
249 			}
250 					
251 			else if (this.jrbDetached.isSelected()) {
252 				if (this.jrbImage.isSelected()) {
253 					this.presetTemplateURL = this.getClass().getResource("SPACECRAFT_SCIENCE-DET-FIXED-IMAGE.LBT");
254 				}
255 						
256 				else if (this.jrbTable.isSelected()) {
257 					this.presetTemplateURL = this.getClass().getResource("SPACECRAFT_SCIENCE-DET-FIXED-TABLE.LBT");
258 				}
259 								
260 				else if (this.jrbOther.isSelected()) {
261 					this.presetTemplateURL = null;
262 				}
263 			}
264 				
265 		}
266 				
267 		else if (this.jrbEarth.isSelected()) {
268 				
269 			if (this.jrbAttached.isSelected()) {
270 				if (this.jrbImage.isSelected()) {
271 					this.presetTemplateURL = this.getClass().getResource("EARTHBASED_SCIENCE-ATT-FIXED-IMAGE.LBT");
272 				}
273 						
274 				else if (this.jrbTable.isSelected()) {
275 					this.presetTemplateURL = this.getClass().getResource("EARTHBASED_SCIENCE-ATT-FIXED-TABLE.LBT");
276 				}
277 							
278 				else if (this.jrbOther.isSelected()) {
279 					this.presetTemplateURL = null;	
280 				}
281 			}
282 					
283 			else if (this.jrbDetached.isSelected()) {
284 				if (this.jrbImage.isSelected()) {
285 					this.presetTemplateURL = this.getClass().getResource("EARTHBASED_SCIENCE-DET-FIXED-IMAGE.LBT");
286 				}
287 						
288 				else if (this.jrbTable.isSelected()) {
289 					this.presetTemplateURL = this.getClass().getResource("EARTHBASED_SCIENCE-DET-FIXED-TABLE.LBT");
290 				}
291 							
292 				else if (this.jrbOther.isSelected()) {
293 					this.presetTemplateURL = null;	
294 				}
295 			}
296 				
297 		}
298 			
299 			
300 		ProjectUtility.openURL (props, window, presetTemplateURL);
301 			
302 		pd.setVisible(false);
303 		window.editorPane.jta.setCaretPosition(0);
304 		window.editorPane.jta.grabFocus();
305 
306 		
307 	}
308 	
309 	public URL getTemplateURL() {
310 				
311 		return presetTemplateURL;
312 		
313 	}
314 }