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.ListTransferHandler;
6   import gov.nasa.pds.ltdt.gui.util.Utility;
7   
8   import java.awt.Dimension;
9   import java.awt.FlowLayout;
10  import java.awt.Font;
11  import java.util.ArrayList;
12  import java.util.Iterator;
13  import java.util.List;
14  
15  import javax.swing.BorderFactory;
16  import javax.swing.BoxLayout;
17  import javax.swing.JList;
18  import javax.swing.JPanel;
19  import javax.swing.JScrollPane;
20  import javax.swing.ListSelectionModel;
21  import javax.swing.event.ListSelectionEvent;
22  import javax.swing.event.ListSelectionListener;
23  
24  /***
25   * Panel to hold listing of dictionary keywords, and how this tool behaves 
26   * when a keyword is selected from the list.
27   * @author jwang
28   *
29   */
30  public class ListingPane extends JPanel implements ListSelectionListener {
31  	
32  	public JList keywordList;
33  	
34  	private MainWindow window;
35  	
36  	public ListingPane (MainWindow window) {
37  		
38  		this.window = window;
39  		
40  		this.setOpaque(true);
41  		this.setLayout (new BoxLayout(this, BoxLayout.PAGE_AXIS));
42  		this.setBorder(BorderFactory.createEmptyBorder());
43  		
44  		keywordList = new JList();
45  		keywordList.setVisibleRowCount(25);
46  		keywordList.setOpaque(true);
47  		keywordList.setFont(new Font("monospaced",Font.PLAIN, 12));
48          keywordList.setDragEnabled(true);
49          keywordList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
50          
51          // handles list selection
52          keywordList.addListSelectionListener(this);
53          
54          // handles drag and drop
55          keywordList.setTransferHandler(new ListTransferHandler(window));
56          
57          JScrollPane jsp = new JScrollPane (keywordList);
58          jsp.setPreferredSize(new Dimension(300,200));
59          
60          this.add(jsp);	
61          //this.add(keywordList);
62  	}
63  
64  	/***
65  	 * called when a selection is made to a list. When occur, definition of 
66  	 * the selected keyword is displayed.
67  	 */
68  	public void valueChanged(ListSelectionEvent le) {
69  		if (le.getValueIsAdjusting()==false) { // we only care about the actual selection
70  			int idx = keywordList.getSelectedIndex();
71  			if (idx==-1) {
72  				// if no selection made
73  				// action can be added here to disable selection button
74  			}
75  			else {
76  				displayKeywordDefinition(window, idx);
77  			}
78  		}		
79  	}
80  	
81  	/***
82  	 * Collects and display definitions associated with a selected keyword
83  	 * @param idx
84  	 */
85  	public static void displayKeywordDefinition(MainWindow window, int idx) {
86  			
87  		List definitionText = new ArrayList();
88  		int rowNum =0;
89  		String defStr = null;	
90  
91  		String selectedString = window.keywordDisplayLong[idx];
92  		
93  		window.lastSelectedKeyword = selectedString;
94  		KeywordProperty kp = (KeywordProperty)window.dictionaryMap.get(selectedString);
95  			
96  		//display WDD instead of actual location
97  		if ((LTDTKeys.WDDSTATUSTYPE).equals(kp.getKwdDef().getStatusType())) {		
98  			//definitionText.add(rowNum++,"Source: WDD");
99  			definitionText.add(rowNum++,"Source: Working Data Dictionary");
100 			definitionText.add(rowNum++, "");
101 		}
102 		else {
103 			defStr = kp.getDictURL().toString();
104 			definitionText.add(rowNum++,"Source: "+defStr);
105 			definitionText.add(rowNum++, "");
106 		}
107 		
108 		defStr = kp.getKwdDef().getIdentifier();
109 		definitionText.add(rowNum++,"NAME = "+defStr);
110 		definitionText.add(rowNum++,"");
111 	
112 		/*	keep this commented out 	
113 		defStr = kp.getKwdDef().getStatusType();
114 		definitionText.add(rowNum++, "STATUS_TYPE = "+defStr);
115 	*/	
116 		// gather information about required/optional object/element associating
117 		// with this keyword
118 
119 
120 		List definitionArray = DictionaryUtility.buildKeywordDefinitionList(kp.getKwdDef());
121 
122 		if (definitionArray.size() > 0) {		
123 			
124 			for (Iterator it=definitionArray.iterator(); it.hasNext();) {
125 				definitionText.add(rowNum++, it.next());
126 			}
127 			definitionArray.clear();
128 		}
129 		
130 		defStr = kp.getKwdDef().getDescription();
131 		
132 		defStr = Utility.reformatDescription(defStr);
133 		
134 		definitionText.add(rowNum++, "DESCRIPTION = \""+ defStr+"\"");
135 		
136 		window.dictDefPane.jta.setText("");
137 		for (Iterator it=definitionText.iterator(); it.hasNext();) {
138 			String tempStr = (String)it.next();
139 			//window.dictDefPane.jta.append((String)it.next()+"\n");
140 			window.dictDefPane.jta.append(tempStr+System.getProperty("line.separator"));
141 
142 		}		
143 		definitionText.clear();
144 
145 		// add proper panel title
146 		if (kp.getKwdType()==LTDTKeys.OBJECT_TYPE) window.dictDefPane.setBorder(BorderFactory.createTitledBorder(LTDTKeys.OBJECTDEFINITIONTITLE));
147 		else if (kp.getKwdType()==LTDTKeys.ELEMENT_TYPE) window.dictDefPane.setBorder(BorderFactory.createTitledBorder(LTDTKeys.ELEMENTDEFINITIONTITLE));
148 		else if (kp.getKwdType()==LTDTKeys.GROUP_TYPE) window.dictDefPane.setBorder(BorderFactory.createTitledBorder(LTDTKeys.GROUPDEFINITIONTITLE));
149 	
150 		window.dictDefPane.jta.setCaretPosition(0);
151 	}
152 
153 }