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.tools.dict.ObjectDefinition;
6   
7   import java.awt.Dimension;
8   import java.awt.Font;
9   import java.awt.event.ActionEvent;
10  import java.awt.event.ActionListener;
11  import java.net.URL;
12  import java.util.ArrayList;
13  import java.util.List;
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.ImageIcon;
20  import javax.swing.JButton;
21  import javax.swing.JPanel;
22  import javax.swing.JScrollPane;
23  import javax.swing.JTabbedPane;
24  import javax.swing.JTextArea;
25  import javax.swing.JTextField;
26  import javax.swing.event.ChangeEvent;
27  import javax.swing.event.ChangeListener;
28  
29  
30  /***
31   * Display and manages the dictionary keyword listing panel
32   * 
33   * @author jwang
34   *
35   */
36  public class DictionaryListPane  extends JPanel  {
37  	
38  	//JPanel dlPanel; // an area to list, search, group
39  	               // names of objects and elements
40  	
41  	//JPanel searchPane; // an area to hold search string and control icons
42  	Box box1, box2;
43  	public JTextField jtfSearchString;    // text box to list object/element names
44  	public JButton jbtnSearch;
45  	public JButton jbtnClear; 
46  	ImageIcon searchIcon, clearIcon;
47  	URL searchIconURL, clearIconURL;  
48  	
49  	//JButton jbtnObject, jbtnElement;
50  	
51  	public JTextArea jta;	
52  	
53  	public ListingPane objectPanel;
54  	public ListingPane elementPanel;
55  	public ListingPane fullPanel;
56  	public JTabbedPane jtp;
57  	public JScrollPane jspTab;
58  	
59  	private Properties props;
60  	private MainWindow window;
61  	
62  	private Dimension btnDim = new Dimension(100,25);
63  	
64  	private KeywordProperty tempkp;
65  	ObjectDefinition ed;
66  	
67  	public DictionaryListPane (MainWindow window) {	
68  		this.props = props;
69  		this.window = window;
70  
71  		this.setOpaque(true);		//dlPanel.setLayout(new BoxLayout(this.dlPane, BoxLayout.LINE_AXIS));
72  		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
73          this.setBorder(BorderFactory.createTitledBorder("Data Dictionary Listing"));
74  		
75          constructSearchArea();
76          constructListingTabbedPanelArea();      
77  		
78  		this.add(box1);
79  		this.add(box2);
80  	}	
81  		
82  	private void constructSearchArea() {
83  			
84  		jtfSearchString = new JTextField();
85  		jtfSearchString.setMaximumSize(new Dimension(200,25));
86  		jtfSearchString.setFont(new Font("monospaced",Font.PLAIN, 12));
87  		
88  		// if the user enter <CR>
89  		// make sure the process is the same as jbtnSearch actionPeroformed
90  		jtfSearchString.addActionListener(new ActionListener() { 
91  
92  			public void actionPerformed(ActionEvent ae) {
93  				searchKeyword();
94  				resetDefinitionPanel();
95  				
96  				switch (window.requestedType) {
97  					case LTDTKeys.OBJECT_TYPE:
98  						DictionaryListPane.displaySingleTypeList(window, LTDTKeys.OBJECTONLY);
99  						break;
100 					case LTDTKeys.ELEMENT_TYPE:
101 						DictionaryListPane.displaySingleTypeList(window, LTDTKeys.ELEMENTONLY);
102 						break;
103 					case LTDTKeys.ALL_TYPE:
104 						DictionaryListPane.displaySingleTypeList(window, LTDTKeys.ALLTYPES);
105 						break;
106 
107 				}
108 			
109 			}
110 			
111 		});
112 
113 		searchIconURL = this.getClass().getResource("system-search.png");
114 		searchIcon = new ImageIcon(searchIconURL);
115 		jbtnSearch = new JButton(searchIcon);
116 		jbtnSearch.setToolTipText("Start Search");
117 		jbtnSearch.addActionListener(new ActionListener() {
118 
119 			public void actionPerformed(ActionEvent ae) {
120 				
121 				searchKeyword();					
122 				resetDefinitionPanel();	
123 				
124 				switch (window.requestedType) {
125 					case LTDTKeys.OBJECT_TYPE:
126 						DictionaryListPane.displaySingleTypeList(window, LTDTKeys.OBJECTONLY);
127 						break;
128 					case LTDTKeys.ELEMENT_TYPE:
129 						DictionaryListPane.displaySingleTypeList(window, LTDTKeys.ELEMENTONLY);
130 						break;
131 					case LTDTKeys.ALL_TYPE:
132 						DictionaryListPane.displaySingleTypeList(window, LTDTKeys.ALLTYPES);
133 						break;
134 
135 				}
136 				
137 				
138 			}
139 			
140 		});
141 			
142 		clearIconURL = this.getClass().getResource("process-stop.png");
143 		clearIcon = new ImageIcon(clearIconURL);
144 		jbtnClear = new JButton(clearIcon);
145 		jbtnClear.setToolTipText("Clear Search");
146 		jbtnClear.addActionListener( new ActionListener() {
147 
148 			public void actionPerformed(ActionEvent ae) {	
149 				
150 				window.dictListPane.jtfSearchString.setText("");
151 				if (window.keywordFullShort!=null) {
152 					resetKeywordDisplay();
153 					resetDefinitionPanel();
154 					
155 					switch (window.requestedType) {
156 						case LTDTKeys.OBJECT_TYPE:
157 							DictionaryListPane.displaySingleTypeList(window, LTDTKeys.OBJECTONLY);
158 							break;
159 						case LTDTKeys.ELEMENT_TYPE:
160 							DictionaryListPane.displaySingleTypeList(window, LTDTKeys.ELEMENTONLY);
161 							break;
162 						case LTDTKeys.ALL_TYPE:
163 							DictionaryListPane.displaySingleTypeList(window, LTDTKeys.ALLTYPES);
164 							break;
165 	
166 					}
167 				}
168 			}
169 			
170 		});
171 
172 		box1 = Box.createHorizontalBox();
173 		box1.setBorder(BorderFactory.createEmptyBorder());
174 		box1.add(jbtnSearch);
175 		box1.add(jtfSearchString);
176 		box1.add(jbtnClear);
177 	}
178 		
179 	private void constructListingTabbedPanelArea() {
180 		
181 		jtp = new JTabbedPane (JTabbedPane.TOP,
182 				JTabbedPane.SCROLL_TAB_LAYOUT);
183 		
184 		objectPanel = new ListingPane(window);
185 		elementPanel = new ListingPane(window);
186 		fullPanel = new ListingPane(window);
187 		jtp.addChangeListener(new ChangeListener() {
188 			public void stateChanged(ChangeEvent ce) {
189 				tabSelectionHandler();
190 			}			
191 		});
192 				
193 		jtp.addTab(LTDTKeys.OBJECTTABNAME, objectPanel);
194 		jtp.addTab(LTDTKeys.ELEMENTTABNAME, elementPanel);
195 		jtp.addTab(LTDTKeys.ALLTABNAME, fullPanel);
196 				
197 		JPanel jpnl = new JPanel();
198 		jpnl.setOpaque(true);
199 		jpnl.setLayout(new BoxLayout(jpnl, BoxLayout.PAGE_AXIS));
200 		jpnl.setBorder(BorderFactory.createEmptyBorder());
201 		jpnl.add(jtp);
202 		
203 		box2 = Box.createHorizontalBox();
204 		box2.setBorder(BorderFactory.createEmptyBorder());
205 		box2.add(jpnl);		
206 	}
207 
208 	
209 	/***
210 	 * search the entire list of keywords for matching entries
211 	 *
212 	 */
213 	private void searchKeyword () {
214 		
215 		String searchStr = window.dictListPane.jtfSearchString.getText().trim();
216 		
217 		searchKeyword(searchStr);
218 	}
219 	
220 	private void searchKeyword(String searchStr) {
221 		if (searchStr.length()>0 ) {
222 
223 			// loop through the full list to gather the matches, 
224 			// whenever there's a match put into the new set of Long and Short full display array
225 			
226 			List arrayS = new ArrayList(); // to keep matching short ids
227 			List arrayL = new ArrayList(); // to keep matching full ids	
228 
229 
230 			for (int i=0; i<window.keywordFullShort.length;i++) {
231 				if (window.keywordFullShort[i].toLowerCase().indexOf(searchStr.toLowerCase())!=-1) {
232 					arrayS.add(window.keywordFullShort[i]);
233 					arrayL.add(window.keywordFullLong[i]);
234 				}
235 			}
236 			
237 			window.keywordDisplayShort=null;
238 			window.keywordDisplayLong=null;
239 			window.keywordSubShort = null;
240 			window.keywordSubLong = null;
241 			
242 			// transfer the result string to string array to work with the list component
243 			window.keywordSubShort=(String[])arrayS.toArray(new String[0]);
244 			window.keywordSubLong=(String[])arrayL.toArray(new String[0]);
245 			window.keywordDisplayShort=(String[])arrayS.toArray(new String[0]);
246 			window.keywordDisplayLong=(String[])arrayL.toArray(new String[0]);
247 						
248 			
249 			arrayS.clear();
250 			arrayL.clear();			
251 		}
252 		
253 		// if no search string specified, display the full list
254 		else {
255 			
256 			if (window.keywordFullShort!=null) {
257 				
258 				window.keywordDisplayLong=null;
259 				window.keywordDisplayShort=null;				
260 				window.keywordSubLong=null;
261 				window.keywordSubShort=null;
262 				
263 				window.keywordDisplayShort = (String[])window.keywordFullShort.clone();
264 				window.keywordDisplayLong = (String[])window.keywordFullLong.clone();
265 				
266 				window.keywordSubShort = (String[])window.keywordFullShort.clone();
267 				window.keywordSubLong = (String[])window.keywordFullLong.clone();
268 				
269 				// window.dictListPane.keywordList.setListData(window.keywordDisplayShort);
270 				
271 				window.dictListPane.jtfSearchString.setText("");
272 			}
273 			
274 		}
275 		
276 	}
277 	
278 	private void resetKeywordDisplay() {
279 		
280 		window.keywordDisplayLong=null;
281 		window.keywordDisplayShort=null;				
282 		window.keywordSubLong=null;
283 		window.keywordSubShort=null;
284 		
285 		window.keywordDisplayShort = (String[])window.keywordFullShort.clone();
286 		window.keywordDisplayLong = (String[])window.keywordFullLong.clone();
287 		
288 		window.keywordSubShort = (String[])window.keywordFullShort.clone();
289 		window.keywordSubLong = (String[])window.keywordFullLong.clone();
290 		
291 		window.dictListPane.jtfSearchString.setText("");
292 	}
293 	
294 	/***
295 	 * Bases on tab index to identify display selection, and to invoke the display method
296 	 */
297 	private void tabSelectionHandler() {
298 	
299 		resetDefinitionPanel();
300 		
301 		if (window.windowVisible) {
302 		
303 			int selectionIdx = window.dictListPane.jtp.getSelectedIndex();
304 
305 			switch (selectionIdx) {
306 				case LTDTKeys.OBJECTTABINDEX:	
307 					displaySingleTypeList(window, LTDTKeys.OBJECTONLY);
308 					break;
309 				case LTDTKeys.ELEMENTTABINDEX:				
310 					displaySingleTypeList(window, LTDTKeys.ELEMENTONLY);
311 					break;
312 				case LTDTKeys.ALLTABINDEX:
313 					displaySingleTypeList(window, LTDTKeys.ALLTYPES);
314 					break;
315 				default: return;
316 			}
317 			
318 			
319 		}
320 		
321 		return;
322 	}
323 	
324 	/***
325 	 * Display one a keywords bases on 
326 	 * -- search result list if there was a search performed prior to this call
327 	 * -- full list of keywords if there was no search performed prior to this call
328 	 * @param window
329 	 * @param typeSelected
330 	 */
331 	public static void displaySingleTypeList (MainWindow window, String typeSelected) {
332 		
333 		
334 		if (window.dictionaryMap.size() >0 ) {
335 
336 			List arrayShortIDList = new ArrayList(); // for matching short ids
337 			List arrayLongIDList = new ArrayList(); // for matching full ids
338 			List arrayFullIDList = new ArrayList(); // all IDs from the dictionaryMap			
339 			
340 			// set the highlight on the tabs
341 			if ((LTDTKeys.ALLTYPES).equals(typeSelected)) {	
342 				window.requestedType=LTDTKeys.ALL_TYPE;
343 			}
344 			else if ((LTDTKeys.OBJECTONLY).equals(typeSelected)) {				
345 				window.requestedType=LTDTKeys.OBJECT_TYPE;
346 			} 
347 			else if ((LTDTKeys.ELEMENTONLY).equals(typeSelected)) {			
348 				window.requestedType=LTDTKeys.ELEMENT_TYPE;
349 			} 
350 			 
351 			
352 			//loop through the displayed list and pull out the type of keyword
353 			//user requested
354 			
355 			int s=0;
356 
357 			
358 			if ((LTDTKeys.ALLTYPES).equals(typeSelected)) {
359 
360 				if (window.keywordSubShort!=null) {
361 					window.keywordDisplayShort=(String[])window.keywordSubShort.clone();
362 					window.keywordDisplayLong=(String[])window.keywordSubLong.clone();
363 				}
364 				
365 				
366 			}
367 			else {
368 				
369 				if (window.keywordSubShort!=null) {
370 				for (int i=0; i<window.keywordSubLong.length; i++) {
371 					String id = (String)window.keywordSubLong[i];
372 					KeywordProperty kp= (KeywordProperty)window.dictionaryMap.get(id);				
373 					if (kp.getKwdType() == window.requestedType) {
374 						arrayShortIDList.add(s, DictionaryUtility.swap(id).split("--")[0]);
375 						arrayLongIDList.add(s, id);
376 						s++;
377 					}
378 	
379 				}
380 				
381 				window.keywordDisplayShort=(String[])arrayShortIDList.toArray(new String[0]);
382 				window.keywordDisplayLong=(String[])arrayLongIDList.toArray(new String[0]);
383 				
384 				}
385 			}	
386 			
387 			if ((LTDTKeys.ALLTYPES).equals(typeSelected)) {
388 				if (window.keywordDisplayShort!=null) {
389 					window.dictListPane.fullPanel.keywordList.setListData(window.keywordDisplayShort);
390 					window.dictListPane.jtp.setSelectedIndex(LTDTKeys.ALLTABINDEX);
391 					highlightLastSelectedKeyword(window);
392 				}
393 			}
394 			else if ((LTDTKeys.OBJECTONLY).equals(typeSelected)) {
395 				if (window.keywordDisplayShort!=null) {
396 					window.dictListPane.objectPanel.keywordList.setListData(window.keywordDisplayShort);
397 					window.dictListPane.jtp.setSelectedIndex(LTDTKeys.OBJECTTABINDEX);
398 					highlightLastSelectedKeyword(window);
399 				}
400 			} 
401 			else if ((LTDTKeys.ELEMENTONLY).equals(typeSelected)) {
402 				if (window.keywordDisplayShort!=null) {
403 					window.dictListPane.elementPanel.keywordList.setListData(window.keywordDisplayShort);
404 					window.dictListPane.jtp.setSelectedIndex(LTDTKeys.ELEMENTTABINDEX);
405 					highlightLastSelectedKeyword(window);
406 				}
407 			} 
408 				
409 			
410 			arrayShortIDList.clear();
411 			arrayLongIDList.clear();
412 			arrayFullIDList.clear();
413 							
414 		} // if dictionaryMap is not empty	
415 		
416 		else {
417 
418 			window.keywordDisplayShort=new String[0];
419 			window.keywordDisplayLong=new String[0];
420 
421 			window.dictListPane.fullPanel.keywordList.setListData(window.keywordDisplayShort);
422 			window.dictListPane.objectPanel.keywordList.setListData(window.keywordDisplayShort);
423 			window.dictListPane.elementPanel.keywordList.setListData(window.keywordDisplayShort);
424 			if ((LTDTKeys.ALLTYPES).equals(typeSelected)) {
425 				window.dictListPane.jtp.setSelectedIndex(LTDTKeys.ALLTABINDEX);
426 			}
427 			else if ((LTDTKeys.OBJECTONLY).equals(typeSelected)) {
428 				window.dictListPane.jtp.setSelectedIndex(LTDTKeys.OBJECTTABINDEX);
429 			}
430 			else if ((LTDTKeys.ELEMENTONLY).equals(typeSelected)) {
431 				window.dictListPane.jtp.setSelectedIndex(LTDTKeys.ELEMENTTABINDEX);
432 			}
433 		
434 		}
435 
436 
437 	}
438 	
439 	private static void highlightLastSelectedKeyword(MainWindow window) {
440 		
441 		boolean found = false;
442 		int index = 0;
443 		
444 		for ( index=0; index<window.keywordDisplayLong.length;index++) {
445 			if (window.lastSelectedKeyword.equals(window.keywordDisplayLong[index]))  {
446 				found = true;
447 				break;
448 			}
449 			
450 		}
451 		if (found) {
452 			
453 			if (window.requestedType==LTDTKeys.ALL_TYPE) {	
454 				window.dictListPane.fullPanel.keywordList.setSelectedIndex(index);
455 				window.dictListPane.fullPanel.keywordList.ensureIndexIsVisible(index);
456 			}
457 			else if (window.requestedType==LTDTKeys.OBJECT_TYPE) {				
458 				window.dictListPane.objectPanel.keywordList.setSelectedIndex(index);
459 				window.dictListPane.objectPanel.keywordList.ensureIndexIsVisible(index);
460 			} 
461 			else if (window.requestedType==LTDTKeys.ELEMENT_TYPE) {	
462 				window.dictListPane.elementPanel.keywordList.setSelectedIndex(index);
463 				window.dictListPane.elementPanel.keywordList.ensureIndexIsVisible(index);
464 			}	
465 				
466 		}
467 	}
468 	
469 	/***
470 	 * convenient method to reset definition displays with generic title, and
471 	 * blank display area
472 	 *
473 	 */
474 	private void resetDefinitionPanel() {
475 		
476 		// do this only after all components are set and ui is visible
477 		if (window.windowVisible) {
478 			window.dictDefPane.setBorder(BorderFactory.createTitledBorder(LTDTKeys.GENERICDEFINITIONTITLE));		
479 			window.dictDefPane.jta.setText("");
480 		}
481 		
482 		return;
483 
484 	}
485 
486 }