View Javadoc

1   // Copyright 2007, by the California Institute of Technology.
2   // ALL RIGHTS RESERVED. United States Government Sponsorship acknowledged.
3   // Any commercial use must be negotiated with the Office of Technology Transfer
4   // at the California Institute of Technology.
5   //
6   // This software is subject to U. S. export control laws and regulations
7   // (22 C.F.R. 120-130 and 15 C.F.R. 730-774). To the extent that the software
8   // is subject to U.S. export control laws and regulations, the recipient has
9   // the responsibility to obtain export licenses or other export authority as
10  // may be required before exporting such information to foreign countries or
11  // providing access to foreign nationals.
12  //
13  // $Id: ElementDialog.java 3200 2008-02-29 02:37:49Z jwang $
14  
15  package gov.nasa.pds.ltdt.gui;
16  
17  import gov.nasa.pds.ltdt.gui.configuration.LTDTKeys;
18  import gov.nasa.pds.ltdt.gui.util.DictionaryUtility;
19  import gov.nasa.pds.ltdt.gui.util.Utility;
20  import gov.nasa.pds.tools.dict.ElementDefinition;
21  
22  import java.awt.Dimension;
23  import java.awt.FlowLayout;
24  import java.awt.Font;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import java.text.Format;
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.Properties;
31  import java.lang.Number;
32  import java.net.MalformedURLException;
33  
34  import javax.swing.BorderFactory;
35  import javax.swing.Box;
36  import javax.swing.JButton;
37  import javax.swing.JDialog;
38  import javax.swing.JLabel;
39  import javax.swing.JList;
40  import javax.swing.JOptionPane;
41  import javax.swing.JScrollPane;
42  import javax.swing.JTextArea;
43  import javax.swing.JTextField;
44  import javax.swing.SwingConstants;
45  import javax.swing.event.ListSelectionEvent;
46  
47  public class ElementDialog extends JDialog {
48  	
49  	private Properties props;
50  	private MainWindow window;
51  	private String title;
52  	private String longID;
53  	
54  	private Box boxMain, box1, box2, box3, box4, box5, box6, box7, box8, box9, box10, box11;
55  	private JLabel jlabElementName;	
56  	private JTextField jtfElementName ;
57  	
58  	private JTextField jtfGenDataType;
59  	private JTextField jtfUnitId;
60  	
61  	private JTextField jtfMin, jtfMax, jtfMinLength, jtfMaxLength =null;
62  	private JTextField jtfStandardValueType;
63  	private JTextArea jtaStandardValueSet;
64  	private JTextArea jtaDescription;
65  	private JScrollPane jspDescription, jspStandardValueSet;
66  	
67  	private JButton  jbtnSave, jbtnCancel;
68  	
69  	private int status;
70  	private Dimension btnDim = new Dimension(100,25);
71  	
72  	private KeywordProperty origkp;
73  	
74  	private boolean isModify = false;
75  	
76  	public ElementDialog (Properties props, MainWindow window, String title, String longID) {
77  	
78  		super (window, title, true);
79  		
80  		this.props = props;
81  		this.window = window;
82  		this.title = title;
83  		this.longID = longID;
84  		
85  		if ((LTDTKeys.MODIFYELEMENTTITLE).equals(title)) {
86  			this.isModify = true;
87  		}
88  		status = constructPanel (props, window);
89  		if (this.isModify) {
90  			this.longID = longID;
91  			status = populateElementInfo();			
92  		}
93  		
94  		this.setVisible(true);
95  		
96  	}
97  	
98  	private int constructPanel (Properties props, MainWindow window) {
99  		
100 		
101 		this.setSize(540,780);
102 		this.getContentPane().setLayout(new FlowLayout());
103 		
104 		boxMain = Box.createVerticalBox();
105 		boxMain.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
106 		
107 		box1 = Box.createVerticalBox();
108 		//box1.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
109 		box1.setBorder(BorderFactory.createTitledBorder("Element Name"));
110 		
111 		box2 = Box.createVerticalBox();
112 		box2.setBorder(BorderFactory.createTitledBorder("Standard Value Type"));
113 		
114 		box3 = Box.createVerticalBox();
115 		box3.setBorder(BorderFactory.createTitledBorder("Standard Value Set"));
116 		
117 		box4 = Box.createVerticalBox();
118 		box4.setBorder(BorderFactory.createTitledBorder("Minimum"));
119 		
120 		box5 = Box.createVerticalBox();
121 		box5.setBorder(BorderFactory.createTitledBorder("Maximum"));
122 		
123 		box6 = Box.createVerticalBox();
124 		box6.setBorder(BorderFactory.createTitledBorder("Minimum Length"));
125 		
126 		box7 = Box.createVerticalBox();
127 		box7.setBorder(BorderFactory.createTitledBorder("Maximum Length"));
128 	
129 		box8 = Box.createVerticalBox();
130 		box8.setBorder(BorderFactory.createTitledBorder("General Data Type"));
131 		
132 		box9 = Box.createVerticalBox();
133 		box9.setBorder(BorderFactory.createTitledBorder("Unit ID"));
134 
135 		box10 = Box.createVerticalBox();
136 		box10.setBorder(BorderFactory.createTitledBorder("Description"));
137 	
138 		box11 = Box.createHorizontalBox();
139 		box11.setBorder(BorderFactory.createEmptyBorder());
140 
141 		
142 		jtfElementName = new JTextField(20);
143 		jtfElementName.setHorizontalAlignment(SwingConstants.LEFT);
144 		jtfElementName.setFont(new Font("monospaced",Font.PLAIN, 12));
145 		
146 		if ( this.isModify ) {
147 			jtfElementName.setEditable(false);
148 		}
149 		else {			
150 			jtfElementName.setEditable(true);
151 			jtfElementName.setDragEnabled(true);
152 		}
153 		
154 		box1.add(jtfElementName);
155 
156 
157 		jtfStandardValueType = new JTextField(20);
158 		jtfStandardValueType.setEditable(true);
159 		jtfStandardValueType.setDragEnabled(true);
160 		jtfStandardValueType.setFocusable(true);
161 		jtfStandardValueType.setFont(new Font("monospaced",Font.PLAIN, 12));
162 		
163 		if (this.isModify) {
164 			jtfStandardValueType.setEditable(false);
165 		} 
166 		else {
167 			jtfStandardValueType.setEditable(true);
168 			jtfStandardValueType.setDragEnabled(true);
169 			jtfStandardValueType.setText(LTDTKeys.WDDDEFAULTSTANDARDVALUETYPE);
170 		}
171 		box2.add(jtfStandardValueType);
172 		
173 
174 		jtaStandardValueSet = new JTextArea();
175 		jtaStandardValueSet.setEditable(true);
176 		jtaStandardValueSet.setLineWrap(false);
177 		jtaStandardValueSet.setDragEnabled(true);
178 		jtaStandardValueSet.setFocusable(true);
179 		jtaStandardValueSet.setFont(new Font("monospaced",Font.PLAIN, 12));
180 		
181 		jspStandardValueSet = new JScrollPane(jtaStandardValueSet);
182 		jspStandardValueSet.setPreferredSize(new Dimension(480,70));
183 		
184 
185 		box3.add(jspStandardValueSet);
186 		
187 		jtfMin = new JTextField(20);
188 		jtfMin.setEditable(true);
189 		jtfMin.setDragEnabled(true);
190 		jtfMin.setFocusable(true);
191 		jtfMin.setFont(new Font("monospaced",Font.PLAIN, 12));
192 		
193 		box4.add(jtfMin);
194 
195 		// TODO maximum minimum_length maximum_length
196 		jtfMax = new JTextField(20);
197 		jtfMax.setEditable(true);
198 		jtfMax.setDragEnabled(true);
199 		jtfMax.setFocusable(true);
200 		jtfMax.setFont(new Font("monospaced",Font.PLAIN, 12));
201 		
202 		box5.add(jtfMax);
203 		
204 
205 		jtfMinLength = new JTextField(20);
206 		jtfMinLength.setEditable(true);
207 		jtfMinLength.setDragEnabled(true);
208 		jtfMinLength.setFocusable(true);
209 		jtfMinLength.setFont(new Font("monospaced",Font.PLAIN, 12));
210 		
211 		box6.add(jtfMinLength);
212 
213 		// TODO maximum minimum_length maximum_length
214 		jtfMaxLength = new JTextField(20);
215 		jtfMaxLength.setEditable(true);
216 		jtfMaxLength.setDragEnabled(true);
217 		jtfMaxLength.setFocusable(true);
218 		jtfMaxLength.setFont(new Font("monospaced",Font.PLAIN, 12));
219 		
220 		box7.add(jtfMaxLength);
221 
222 		jtfGenDataType = new JTextField(20);
223 		jtfGenDataType.setEditable(true);
224 		jtfGenDataType.setDragEnabled(true);
225 		jtfGenDataType.setFocusable(true);
226 		jtfGenDataType.setFont(new Font("monospaced",Font.PLAIN, 12));
227 		
228 		if (this.isModify) {
229 			jtfGenDataType.setEditable(false);
230 		} 
231 		else {
232 			jtfGenDataType.setEditable(true);
233 			jtfGenDataType.setDragEnabled(true);
234 			jtfGenDataType.setText(LTDTKeys.WDDDEFAULTGENERALDATATYPE);
235 		}
236 		box8.add(jtfGenDataType);
237 		
238 		jtfUnitId = new JTextField(20);
239 		jtfUnitId.setEditable(true);
240 		jtfUnitId.setDragEnabled(true);
241 		jtfUnitId.setFocusable(true);
242 		jtfUnitId.setFont(new Font("monospaced",Font.PLAIN, 12));
243 		
244 		box9.add(jtfUnitId);
245 
246 		jtaDescription = new JTextArea();
247 		jtaDescription.setEditable(true);
248 		jtaDescription.setLineWrap(false);
249 		jtaDescription.setDragEnabled(true);
250 		jtaDescription.setFocusable(true);
251 		jtaDescription.setFont(new Font("monospaced",Font.PLAIN, 12));
252 		
253 		jspDescription = new JScrollPane(jtaDescription);
254 		jspDescription.setPreferredSize(new Dimension(480,150));
255 		
256 
257 		box10.add(jspDescription);
258 				
259 		jbtnSave = new JButton("Save");
260 		jbtnSave.setMinimumSize(btnDim);
261 		jbtnSave.setSelected(true);
262 		jbtnSave.addActionListener(new ActionListener() {
263 
264 			public void actionPerformed(ActionEvent ae) {
265 				saveElementChange();				
266 			}
267 			
268 		} );
269 				
270 		jbtnCancel = new JButton("Cancel");
271 		jbtnCancel.setMinimumSize(btnDim);
272 		jbtnCancel.addActionListener(new ActionListener() {
273 
274 			public void actionPerformed(ActionEvent ae) {
275 				disableDialog();
276 				
277 			}
278 			
279 		} );
280 		
281 		box11.add(jbtnSave);
282 		box11.add(jbtnCancel);
283 		
284 		boxMain.add(box1);
285 		boxMain.add(box2);
286 		boxMain.add(box3);
287 		boxMain.add(box4);
288 		boxMain.add(box5);
289 		boxMain.add(box6);
290 		boxMain.add(box7);
291 		boxMain.add(box8);
292 		boxMain.add(box9);
293 		boxMain.add(box10);
294 		boxMain.add(box11);
295 		
296 		this.getContentPane().add(boxMain);
297 		
298 		
299 		return LTDTKeys.SUCCESS;
300 	}
301 	/***
302 	 * Given a long id, this method gathers element definitions and display them to the gui
303 	 * @return
304 	 */
305 	private int populateElementInfo () {
306 		
307 		origkp = (KeywordProperty) window.dictionaryMap.get(longID);
308 		ElementDefinition origDef = (ElementDefinition) origkp.getKwdDef();
309 		
310 		// element name
311 		this.jtfElementName.setText(origDef.getIdentifier().toString());
312 		
313 		// standard value type
314 		if (origDef.getValueType()!=null) {
315 			jtfStandardValueType.setText(origDef.getValueType());
316 		}
317 		
318 		// standard value set
319 		if (origDef.hasValidValues()) {
320 			jtaStandardValueSet.setText(Utility.list2quotedSet((List)origDef.getValues()));
321 		}
322 		// minimum
323 		if (origDef.hasMinimum())
324 			jtfMin.setText(origDef.getMinimum().toString());
325 		
326 		// maximum
327 		if (origDef.hasMaximum())
328 			jtfMax.setText(origDef.getMaximum().toString());
329 		
330 		// minimum_length
331 		jtfMinLength.setText( new Integer(origDef.getMinLength()).toString());
332 		
333 		// maximum_length
334 		jtfMaxLength.setText(new Integer(origDef.getMaxLength()).toString());
335 		
336 		// general data type 
337 		if (origDef.getDataType()!=null) {
338 			jtfGenDataType.setText(origDef.getDataType());
339 		}
340 		
341 		// unit id
342 		if (origDef.getUnitId()!=null) {
343 			jtfUnitId.setText(origDef.getUnitId());
344 		}
345 		
346 		// description
347 		jtaDescription.setText(origDef.getDescription());
348 
349 		return LTDTKeys.SUCCESS;
350 	}
351 
352 	/***
353 	 * save modified/added definitions to WDD
354 	 * @return SUCCESS if saved. ERROR if input value incorrect
355 	 */
356 	private int saveElementChange() {
357 		
358 		// check if this is an existing WDD entry
359 		String elementName = jtfElementName.getText().toString().toUpperCase();
360 		String dictElementName = null;
361 		ElementDefinition newElementDef = null; // for WDD
362 		ElementDefinition newElementDef2 = null; // for mDictionary
363 		ElementDefinition newElementDef3 = null; // for treeMap
364 		boolean newEntry = false;
365 		String tempStr=null;
366 		String message = "";
367 		KeywordProperty kp = null;
368 		
369 		if (elementName.trim().length()==0) {
370 			JOptionPane.showMessageDialog(window, "Please enter an Element Name", "Element Name Not Found", JOptionPane.WARNING_MESSAGE);
371 			return LTDTKeys.ERROR;
372 		}
373 		
374 		
375 		// if this is an Add event but the object has been defined already
376 		if ( !isModify && window.mDictionary.getDefinition(elementName)!=null) {
377 			JOptionPane.showMessageDialog(window, "Please enter a new Element Name.", "Element Exists", JOptionPane.WARNING_MESSAGE);
378 			return LTDTKeys.ERROR;
379 		}
380 		
381 		// there are three runtime cache required addition/modification as result of add/modify elements
382 		// -- WDD cache (window.WDD)
383 		// -- temporary dictionary cache (window.mDictionary)
384 		// -- keyword map (window.dictionaryMap)
385 		
386 
387 		dictElementName = elementName;
388 
389 
390 		// if this element exist in WDD, get a reference
391 		// otherwise, create a new element definition
392 		if (window.WDD.getDefinition(elementName)!=null) {
393 			newElementDef = (ElementDefinition) window.WDD.getDefinition(elementName);
394 		}
395 		else {
396 			newElementDef = new ElementDefinition(dictElementName);
397 			
398 			newEntry=true;
399 			longID = DictionaryUtility.swap(elementName)+"--WDD";
400 		}
401 		
402 		// mDictionary
403 		if (window.mDictionary.getDefinition(elementName) != null) {
404 			newElementDef2 = (ElementDefinition)window.mDictionary.getDefinition(elementName);
405 		} 
406 		else {			
407 			newElementDef2 = new ElementDefinition(dictElementName);
408 		}
409 		
410 		// dictionary tree
411 		if (!newEntry && window.dictionaryMap.get(this.longID) !=null) {
412 			kp = (KeywordProperty)window.dictionaryMap.get(this.longID);
413 			newElementDef3 = (ElementDefinition)kp.kwdDef;
414 		}
415 		else {
416 			
417 			newElementDef3 = new ElementDefinition(elementName);
418 		}
419 
420 
421 		// set Minimum
422 		try {
423 			tempStr=jtfMin.getText().trim();	
424 			if (tempStr.length()>0) {
425 				if (Double.valueOf(tempStr).isNaN()==false) {
426 					newElementDef.setMinimum(Double.valueOf(tempStr));
427 					newElementDef2.setMinimum(Double.valueOf(tempStr));
428 					newElementDef3.setMinimum(Double.valueOf(tempStr));
429 				}
430 				
431 			}
432 		}
433 		catch (NumberFormatException ex) {
434 			message = message+"\n"+tempStr+" is not a number.";
435 		}
436 		
437 		// set Maximum
438 		try {
439 			tempStr=jtfMax.getText().trim();
440 			if (tempStr.length()>0) {
441 				if (Double.valueOf(tempStr).isNaN()==false) {
442 					newElementDef.setMaximum(Double.valueOf(tempStr));
443 					newElementDef2.setMaximum(Double.valueOf(tempStr));
444 					newElementDef3.setMaximum(Double.valueOf(tempStr));
445 				}
446 			}
447 		}
448 		catch (NumberFormatException ex) {
449 			message = message+"\n"+tempStr+" is not a number.";			
450 		}
451 		
452 		// set minimum length
453 		try {
454 			tempStr=jtfMinLength.getText().trim();
455 			if (tempStr.length()>0) {					
456 				newElementDef.setMinLength(Integer.parseInt(this.jtfMinLength.getText().toString()));
457 				newElementDef2.setMinLength(Integer.parseInt(this.jtfMinLength.getText().toString()));
458 				newElementDef3.setMinLength(Integer.parseInt(this.jtfMinLength.getText().toString()));
459 			}
460 		}
461 		catch (NumberFormatException ex) {
462 			message = message+"\n"+tempStr+" is not an integer.";			
463 		}
464 		
465 		// set maximum length
466 		try {
467 			tempStr=jtfMaxLength.getText().trim();
468 			if (tempStr.length()>0) {
469 				newElementDef.setMaxLength(Integer.parseInt(this.jtfMaxLength.getText().toString()));
470 				newElementDef2.setMaxLength(Integer.parseInt(this.jtfMaxLength.getText().toString()));
471 				newElementDef3.setMaxLength(Integer.parseInt(this.jtfMaxLength.getText().toString()));
472 			}
473 		}
474 		catch (NumberFormatException ex) {
475 			message = message+"\n"+tempStr+" is not an integer.";			
476 		}
477 		
478 		
479 		if (message.length()>0) {
480 			
481 			JOptionPane.showMessageDialog (window, "Input error: "+message);
482 			return LTDTKeys.ERROR;
483 		}
484 		
485 		// set description
486 		tempStr=this.jtaDescription.getText();
487 		if (tempStr.length()>0) {
488 			newElementDef.setDescription(tempStr);
489 			newElementDef2.setDescription(tempStr);
490 			newElementDef3.setDescription(tempStr);
491 		}
492 		else {
493 			newElementDef.setDescription("");
494 			newElementDef2.setDescription("");
495 			newElementDef3.setDescription("");
496 		}
497 
498 		// set standard value type
499 		tempStr=this.jtfStandardValueType.getText();
500 		if (tempStr.trim().length()>0) {
501 			newElementDef.setValueType(tempStr.toUpperCase());
502 			newElementDef2.setValueType(tempStr.toUpperCase());
503 			newElementDef3.setValueType(tempStr.toUpperCase());
504 		}
505 		else {
506 			newElementDef.setValueType(LTDTKeys.WDDDEFAULTSTANDARDVALUETYPE);
507 			newElementDef2.setValueType(LTDTKeys.WDDDEFAULTSTANDARDVALUETYPE);
508 			newElementDef3.setValueType(LTDTKeys.WDDDEFAULTSTANDARDVALUETYPE);
509 		}
510 	
511 		// set general data type
512 		tempStr=this.jtfGenDataType.getText();
513 		if (tempStr.trim().length()>0) {
514 			newElementDef.setDataType(tempStr.toUpperCase());
515 			newElementDef2.setDataType(tempStr.toUpperCase());
516 			newElementDef3.setDataType(tempStr.toUpperCase());
517 		}
518 		else {
519 			newElementDef.setDataType(LTDTKeys.WDDDEFAULTGENERALDATATYPE);
520 			newElementDef2.setDataType(LTDTKeys.WDDDEFAULTGENERALDATATYPE);
521 			newElementDef3.setDataType(LTDTKeys.WDDDEFAULTGENERALDATATYPE);
522 		}
523 		
524 		// set Unit Id
525 		tempStr=this.jtfUnitId.getText();
526 		if (tempStr.trim().length()>0) {
527 			newElementDef.setUnitId(tempStr.toUpperCase());
528 			newElementDef2.setUnitId(tempStr.toUpperCase());
529 			newElementDef3.setUnitId(tempStr.toUpperCase());
530 		}
531 		else {
532 			newElementDef.setUnitId("");
533 			newElementDef2.setUnitId("");
534 			newElementDef3.setUnitId("");
535 		}
536 		
537 		// set standard value set
538 		tempStr=this.jtaStandardValueSet.getText();
539 		if (tempStr.trim().length()>0) {
540 			newElementDef.setValues(Utility.rowstring2listNotQuoted(tempStr.toUpperCase()));
541 			newElementDef2.setValues(Utility.rowstring2listNotQuoted(tempStr.toUpperCase()));
542 			newElementDef3.setValues(Utility.rowstring2listNotQuoted(tempStr.toUpperCase()));
543 		}
544 		else {
545 			newElementDef.setValues(new ArrayList());
546 			newElementDef2.setValues(new ArrayList());
547 			newElementDef3.setValues(new ArrayList());
548 		}
549 		
550 		// the Status Type should be WDD
551 		newElementDef.setStatusType(LTDTKeys.WDDSTATUSTYPE);
552 		newElementDef2.setStatusType(LTDTKeys.WDDSTATUSTYPE);
553 		newElementDef3.setStatusType(LTDTKeys.WDDSTATUSTYPE);
554 
555 		// if it's a new element keyword
556 		// - add one entry to WDD
557 		// - add one entry to mDictionary
558 		// - add one entry to dictionaryMap
559 		
560 		if (newEntry) {
561 			
562 			window.WDD.addDefinition(newElementDef);
563 			
564 			window.mDictionary.addDefinition(newElementDef2);
565 			
566 			KeywordProperty newkp = new KeywordProperty();
567 			newkp.setIdentifier(elementName);
568 			newkp.setKwdDef(newElementDef3);
569 			newkp.setKwdType(LTDTKeys.ELEMENT_TYPE);
570 			try {
571 				newkp.setDictURL(Utility.toURL(System.getProperty("user.dir"))); // spaceholder
572 			} catch (MalformedURLException me) {}						
573 			window.dictionaryMap.put(longID, newkp);
574 			
575 			
576 		}
577 		// refresh all the lists
578 		DictionaryUtility.populateDictionaryFullListingPane(window);
579 		
580 		// submit a Search internally and display the object type
581 		window.dictListPane.jtfSearchString.setText(elementName);
582 		window.dictListPane.jbtnSearch.doClick();
583 		DictionaryListPane.displaySingleTypeList(window, LTDTKeys.ELEMENTONLY);
584 		//window.dictListPane.jbtnElement.doClick();
585 		//window.requestedType=LTDTKeys.ELEMENT_TYPE;
586 		
587 		// update flags
588 		props.setProperty(LTDTKeys.WDDEXISTS, "true");
589 		props.setProperty(LTDTKeys.PROJECTALTERED, "true");
590 		
591 		this.setVisible(false);
592 		
593 		return LTDTKeys.SUCCESS;
594 	} 
595 	
596 	
597 	
598 	/***
599 	 * disable this dialog window
600 	 *
601 	 */
602 	private void disableDialog() {
603 		this.setVisible(false);
604 	}
605 }