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.DictionaryUtility;
5   import gov.nasa.pds.ltdt.gui.util.Utility;
6   import gov.nasa.pds.tools.dict.DictionaryTokens;
7   import gov.nasa.pds.tools.dict.ElementDefinition;
8   import gov.nasa.pds.tools.dict.GroupDefinition;
9   import gov.nasa.pds.tools.dict.ObjectDefinition;
10  
11  import java.awt.Dimension;
12  import java.awt.FlowLayout;
13  import java.awt.Font;
14  import java.awt.event.ActionEvent;
15  import java.awt.event.ActionListener;
16  import java.net.MalformedURLException;
17  import java.net.URL;
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Properties;
22  
23  import javax.swing.BorderFactory;
24  import javax.swing.Box;
25  import javax.swing.JButton;
26  import javax.swing.JDialog;
27  import javax.swing.JLabel;
28  import javax.swing.JOptionPane;
29  import javax.swing.JScrollPane;
30  import javax.swing.JTextArea;
31  import javax.swing.JTextField;
32  import javax.swing.SwingConstants;
33  /***
34   * Handling of adding a single entry of new object keyword
35   * @author jwang
36   *
37   */
38  public class ObjectDialog extends JDialog implements DictionaryTokens {
39  	private Properties props;
40  	private MainWindow window;
41  	private String title=null;
42  	private String longID=null;
43  	
44  	private Box boxMain, box1, box2, box3, box4, box5, box6, box7, box8;
45  	private JLabel jlabObjName;
46  	private JTextField jtfObjectName, jtfObjectType;
47  	//private JList jlstReqObject, jlstReqElement, jlstOptObject, jlstOptElement =null;
48  	private JTextArea jtaRequiredObject, jtaRequiredElement, jtaOptionalObject, jtaOptionalElement =null;
49  	private JTextArea jtaDescription;
50  	private JScrollPane jspRequiredObject, jspRequiredElement, jspOptionalObject, jspOptionalElement;
51  	private JScrollPane jspDescription;
52  	
53  	private JLabel jlabRequiredObject, jlabRequiredElement, jlabOptionalObject, jlabOptionalElement;
54  	private JButton jbtnSave, jbtnCancel;
55  	
56  	private int status;
57  	private Dimension btnDim = new Dimension(100,25);
58  	
59  	private ObjectDefinition origDef = null;
60  	private KeywordProperty origkp, newkp = null;
61  	
62  	private boolean isModify = false;
63  	
64  	public ObjectDialog (Properties props, MainWindow window, String title, String longID) {
65  	
66  		super (window, title, true);
67  		
68  		this.props = props;
69  		this.window = window;
70  		this.title = title;
71  		
72  		if ((LTDTKeys.MODIFYOBJECTTITLE).equals(title)) {
73  			this.isModify = true;
74  		}
75  		
76  		status = constructPanel (props, window);
77  		
78  		if (this.isModify) {
79  			this.longID = longID;
80  			
81  			status = populateObjectInfo();
82  		}
83  		
84  		this.setVisible(true);
85  	}
86  	
87  	private int constructPanel (Properties props, MainWindow window) {
88  		
89  		
90  		this.setSize(540,800);
91  		
92  		this.getContentPane().setLayout(new FlowLayout());
93  		
94  		boxMain = Box.createVerticalBox();
95  		boxMain.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
96  		
97  		box1 = Box.createVerticalBox();
98  		box1.setBorder(BorderFactory.createTitledBorder("Object Name"));
99  		
100 		box2 = Box.createVerticalBox();
101 		box2.setBorder(BorderFactory.createTitledBorder("Object Type"));
102 		
103 		box3 = Box.createVerticalBox();
104 		box3.setBorder(BorderFactory.createTitledBorder("Required Object Set"));
105 		
106 		box4 = Box.createVerticalBox();
107 		box4.setBorder(BorderFactory.createTitledBorder("Required Element Set"));
108 		
109 		box5 = Box.createVerticalBox();
110 		box5.setBorder(BorderFactory.createTitledBorder("Optional Object Set"));
111 		
112 		box6 = Box.createVerticalBox();
113 		box6.setBorder(BorderFactory.createTitledBorder("Optional Element Set"));
114 		
115 		box7 = Box.createVerticalBox();
116 		box7.setBorder(BorderFactory.createTitledBorder("Description"));
117 	
118 		box8 = Box.createHorizontalBox();
119 		box8.setBorder(BorderFactory.createEmptyBorder());
120 
121 		jtfObjectName = new JTextField(20);
122 		jtfObjectName.setHorizontalAlignment(SwingConstants.LEFT);
123 		jtfObjectName.setFont(new Font("monospaced",Font.PLAIN, 12));
124 		
125 		if ( isModify ) {
126 			jtfObjectName.setEditable(false);
127 		}
128 		else {			
129 			jtfObjectName.setEditable(true);
130 			jtfObjectName.setDragEnabled(true);
131 		}
132 		
133 		box1.add(jtfObjectName);
134 		
135 		jtfObjectType = new JTextField(20);
136 		jtfObjectType.setHorizontalAlignment(SwingConstants.LEFT);
137 		jtfObjectType.setFont(new Font("monospaced",Font.PLAIN, 12));
138 		if ( isModify ) {
139 			jtfObjectType.setEditable(false);
140 		}
141 		else {			
142 			jtfObjectType.setEditable(true);
143 			jtfObjectType.setDragEnabled(true);
144 			jtfObjectType.setText(LTDTKeys.WDDDEFAULTOBJECTTYPE);
145 		}
146 		
147 		box2.add(jtfObjectType);
148 		
149 		jtaRequiredObject = new JTextArea();
150 		jtaRequiredObject.setEditable(true);
151 		jtaRequiredObject.setLineWrap(false);
152 		jtaRequiredObject.setDragEnabled(true);
153 		jtaRequiredObject.setFocusable(true);
154 		jtaRequiredObject.setFont(new Font("monospaced",Font.PLAIN, 12));
155 		
156 		jspRequiredObject = new JScrollPane(jtaRequiredObject);
157 		jspRequiredObject.setPreferredSize(new Dimension(300,80));
158 		
159 		box3.add(jspRequiredObject);
160 				
161 		jtaRequiredElement = new JTextArea();
162 		jtaRequiredElement.setEditable(true);
163 		jtaRequiredElement.setLineWrap(false);
164 		jtaRequiredElement.setDragEnabled(true);
165 		jtaRequiredElement.setFocusable(true);
166 		jtaRequiredElement.setFont(new Font("monospaced",Font.PLAIN, 12));
167 		
168 		jspRequiredElement = new JScrollPane(jtaRequiredElement);
169 		jspRequiredElement.setPreferredSize(new Dimension(300,80));
170 		
171 		box4.add(jspRequiredElement);
172 		
173 		
174 		
175 		jtaOptionalObject = new JTextArea();
176 		jtaOptionalObject.setEditable(true);
177 		jtaOptionalObject.setLineWrap(false);
178 		jtaOptionalObject.setDragEnabled(true);
179 		jtaOptionalObject.setFocusable(true);
180 		jtaOptionalObject.setFont(new Font("monospaced",Font.PLAIN, 12));
181 		
182 		jspOptionalObject = new JScrollPane(jtaOptionalObject);
183 		jspOptionalObject.setPreferredSize(new Dimension(300,80));
184 		
185 		box5.add(jspOptionalObject);
186 		
187 		
188 		jtaOptionalElement = new JTextArea();
189 		jtaOptionalElement.setEditable(true);
190 		jtaOptionalElement.setLineWrap(false);
191 		jtaOptionalElement.setDragEnabled(true);
192 		jtaOptionalElement.setFocusable(true);
193 		jtaOptionalElement.setFont(new Font("monospaced",Font.PLAIN, 12));
194 		
195 		jspOptionalElement = new JScrollPane(jtaOptionalElement);
196 		jspOptionalElement.setPreferredSize(new Dimension(300,80));
197 		
198 		box6.add(jspOptionalElement);
199 		
200 		
201 		
202 		jtaDescription = new JTextArea();
203 		jtaDescription.setEditable(true);
204 		jtaDescription.setLineWrap(false);
205 		//jtaDescription.setWrapStyleWord(true);
206 		jtaDescription.setDragEnabled(true);
207 		jtaDescription.setFocusable(true);
208 		jtaDescription.setFont(new Font("monospaced",Font.PLAIN, 12));
209 		
210 		jspDescription = new JScrollPane(jtaDescription);
211 		jspDescription.setPreferredSize(new Dimension(480,150));
212 		
213 		box7.add(jspDescription);
214 				
215 		jbtnSave = new JButton("Save");
216 		jbtnSave.setMinimumSize(btnDim);
217 		jbtnSave.setSelected(true);
218 		jbtnSave.addActionListener(new ActionListener() {
219 
220 			public void actionPerformed(ActionEvent ae) {
221 				saveObjectChange();	
222 			}
223 			
224 		} );
225 				
226 		jbtnCancel = new JButton("Cancel");
227 		jbtnCancel.setMinimumSize(btnDim);
228 		jbtnCancel.addActionListener(new ActionListener() {
229 
230 			public void actionPerformed(ActionEvent ae) {
231 				disableDialog();
232 			}
233 			
234 		} );
235 				
236 		box8.add(jbtnSave);
237 		box8.add(jbtnCancel);
238 		
239 		boxMain.add(box1);
240 		boxMain.add(box2);
241 		boxMain.add(box3);
242 		boxMain.add(box4);
243 		boxMain.add(box5);
244 		boxMain.add(box6);
245 		boxMain.add(box7);
246 		boxMain.add(box8);
247 		
248 		this.getContentPane().add(boxMain);
249 		
250 		return LTDTKeys.SUCCESS;
251 	}
252 	
253 	/***
254 	 * given a long id, this method gathers object definitions and display onto the gui
255 	 * @return
256 	 */
257 	private int populateObjectInfo() {
258 		
259 		List tempList = null;
260 		String tempString = null;
261 		
262 		origkp = (KeywordProperty) window.dictionaryMap.get(longID);		
263 		ObjectDefinition origDef = (ObjectDefinition)origkp.getKwdDef();
264 
265 		// object name
266 		jtfObjectName.setText(origDef.getIdentifier().toString());
267 		
268 		// object type
269 		jtfObjectType.setText(origDef.getObjectType().toString());
270 		
271 		// required objects
272 		jtaRequiredObject.setText(Utility.list2stringNLF(origDef.getRequiredObjects()));
273 		
274 		// required elements
275 		jtaRequiredElement.setText(Utility.list2stringNLF(origDef.getRequiredElements()));
276 		
277 		// optional objects
278 		jtaOptionalObject.setText(Utility.list2stringNLF(origDef.getOptionalObjects()));
279 
280 		// optional elements		
281 		jtaOptionalElement.setText(Utility.list2stringNLF(origDef.getOptionalElements()));
282 		
283 		// description
284 		jtaDescription.setText(origDef.getDescription());
285 	
286 		return LTDTKeys.SUCCESS;
287 	}
288 	
289 	
290 
291 	/***
292 	 * save modified/added definitions to WDD
293 	 * @throws MalformedURLException 
294 	 *
295 	 */
296 	private void saveObjectChange()  {
297 
298 		// check if this is an existing WDD entry
299 		String objectName = jtfObjectName.getText().toString().toUpperCase();
300 		String dictObjectName = null;
301 		String objectType = jtfObjectType.getText().toString().toUpperCase();
302 		ObjectDefinition newObjDef = null; // for WDD
303 		ObjectDefinition newObjDef2 = null; // for mDictionary
304 		ObjectDefinition newObjDef3 = null; // for treeMap
305 		KeywordProperty kp = null;
306 		boolean newEntry=false;
307 
308 		if (objectName.trim().length()==0) {
309 			JOptionPane.showMessageDialog(window, "Please enter an Object Name.", "Object Name Not Found", JOptionPane.WARNING_MESSAGE);
310 			return;
311 		}
312 		
313 		// if this is an Add event but the object has been defined already
314 		if ( !isModify && window.mDictionary.getDefinition(objectName)!=null) {
315 			JOptionPane.showMessageDialog(window, "Please enter a new Object Name.", "Object Exists", JOptionPane.WARNING_MESSAGE);
316 			return;
317 		}
318 		
319 		dictObjectName = objectName; 
320 
321 		// if the identifier exist in WDD, get a reference 
322 		// otherwise, create a new dictionary object definition to hold the new values
323 		// also, add this entry to the tree map
324 		if (window.WDD.getDefinition(objectName)!=null) {
325 
326 			newObjDef = (ObjectDefinition) window.WDD.getDefinition(objectName);
327 		} 
328 		else {
329 
330 			newObjDef = new ObjectDefinition(dictObjectName);
331 			
332 			newEntry=true;
333 			longID = DictionaryUtility.swap(objectName)+"--WDD";
334 		}
335 	
336 		// mDictionary 
337 		if (window.mDictionary.getDefinition(objectName)!=null) {
338 			newObjDef2 = (ObjectDefinition) window.mDictionary.getDefinition(objectName);
339 		} 
340 		else {
341 			newObjDef2 = new ObjectDefinition(dictObjectName);
342 		}
343 		
344 		// dictionary tree 
345 		if (!newEntry && window.dictionaryMap.get(this.longID)!=null) {
346 			kp = (KeywordProperty)window.dictionaryMap.get(this.longID);
347 			newObjDef3 = (ObjectDefinition) kp.kwdDef;
348 		} 
349 		else {
350 			newObjDef3 = new ObjectDefinition(objectName);
351 		}
352 		
353 		// set object type, if we can recognize it as either GENERIC or SPECIFIC, set it as it is
354 		// otherwise, go for the WDD default object type
355 		if ( (LTDTKeys.DICTIONARYGENERICOBJECTTYPE).equals(objectType)) {
356 			newObjDef.setObjectType(OBJECT_TYPE_GENERIC);
357 			newObjDef2.setObjectType(OBJECT_TYPE_GENERIC);
358 			newObjDef3.setObjectType(OBJECT_TYPE_GENERIC);			
359 		}
360 		else if ( (LTDTKeys.DICTIONARYSPECIFICOBJECTTYPE).equals(objectType)) {
361 			newObjDef.setObjectType(OBJECT_TYPE_SPECIFIC);
362 			newObjDef2.setObjectType(OBJECT_TYPE_SPECIFIC);
363 			newObjDef3.setObjectType(OBJECT_TYPE_SPECIFIC);
364 			
365 		}
366 		else {
367 			newObjDef.setObjectType(OBJECT_TYPE_SPECIFIC);
368 			newObjDef2.setObjectType(OBJECT_TYPE_SPECIFIC);
369 			newObjDef3.setObjectType(OBJECT_TYPE_SPECIFIC);
370 		}
371 		
372 		
373 		// set all four types of definitions. This will replace definition in WDD and mDictionary if exists
374 		
375 		List temp = Utility.rowstring2list(this.jtaRequiredObject.getText().toString().toUpperCase());	
376 		
377 		newObjDef.setRequiredObjects(temp);	
378 		newObjDef2.setRequiredObjects(temp);
379 		newObjDef3.setRequiredObjects(temp);
380 		
381 		temp = Utility.rowstring2list(this.jtaRequiredElement.getText().toString().toUpperCase());	
382 		newObjDef.setRequiredElements(temp);
383 		newObjDef2.setRequiredElements(temp);
384 		newObjDef3.setRequiredElements(temp);
385 		
386 		temp = Utility.rowstring2list(this.jtaOptionalObject.getText().toString().toUpperCase());	
387 		newObjDef.setOptionalObjects(temp);	
388 		newObjDef2.setOptionalObjects(temp);
389 		newObjDef3.setOptionalObjects(temp);
390 		
391 		temp = Utility.rowstring2list(this.jtaOptionalElement.getText().toString().toUpperCase());
392 		newObjDef.setOptionalElements(temp);
393 		newObjDef2.setOptionalElements(temp);
394 		newObjDef3.setOptionalElements(temp);
395 		
396 		newObjDef.setDescription(this.jtaDescription.getText().toString());
397 		newObjDef2.setDescription(this.jtaDescription.getText().toString());
398 		newObjDef3.setDescription(this.jtaDescription.getText().toString());
399 		
400 		newObjDef.setStatusType(LTDTKeys.WDDSTATUSTYPE);
401 		newObjDef2.setStatusType(LTDTKeys.WDDSTATUSTYPE);
402 		newObjDef3.setStatusType(LTDTKeys.WDDSTATUSTYPE);
403 		
404 		
405 		
406 		// if it's a new object keyword 
407 		// - add one entry to WDD
408 		// - add one entry to the dictionary map
409 		// - add one entry to rea dictionary cache
410 		//
411 		// for existing entries in WDD, dictionaryMap, mDictionary, 
412 		// the values have altered.
413 		
414 		if (newEntry) {
415 			
416 			// add to WDD
417 			window.WDD.addDefinition(newObjDef);
418 			
419 			// add to online merged dictionary
420 			window.mDictionary.addDefinition(newObjDef2);			
421 			
422 			// add to dictionary map tree
423 			KeywordProperty newkp=new KeywordProperty();
424 			newkp.setIdentifier(objectName);
425 			newkp.setKwdDef(newObjDef3);	
426 			newkp.setKwdType(LTDTKeys.OBJECT_TYPE);
427 			try {
428 				newkp.setDictURL(Utility.toURL(System.getProperty("user.dir"))); // spaceholder
429 			} catch (MalformedURLException me) {}					
430 			window.dictionaryMap.put(longID, newkp);
431 	
432 		}
433 		
434 		// refresh all the lists to include the modification/addition		
435 		DictionaryUtility.populateDictionaryFullListingPane(window);
436 
437 		// submit a Search internally and simulate an 'object' selection to 
438 		// display the object list
439 		
440 		window.dictListPane.jtfSearchString.setText(objectName);
441 		window.dictListPane.jbtnSearch.doClick();
442 		DictionaryListPane.displaySingleTypeList(window, LTDTKeys.OBJECTONLY);
443 		//window.dictListPane.jbtnObject.doClick();
444 		//window.requestedType=LTDTKeys.OBJECT_TYPE;
445 				
446 		// update flags
447 		props.setProperty(LTDTKeys.WDDEXISTS, "true");
448 		props.setProperty(LTDTKeys.PROJECTALTERED, "true");
449 
450 		this.setVisible(false);
451 		
452 	}
453 	
454 	/***
455 	 * disable this dialog window
456 	 *
457 	 */
458 	private void disableDialog() {
459 		this.setVisible(false);
460 	}
461 	
462 
463 }