1 | /* |
---|
2 | Copyright 2006 by Sean Paus |
---|
3 | Licensed under the Academic Free License version 3.0 |
---|
4 | See the file "LICENSE" for more information |
---|
5 | */ |
---|
6 | |
---|
7 | |
---|
8 | package ec.display; |
---|
9 | |
---|
10 | import java.awt.*; |
---|
11 | import java.awt.event.*; |
---|
12 | import java.io.*; |
---|
13 | import java.util.*; |
---|
14 | import javax.swing.*; |
---|
15 | import javax.swing.event.*; |
---|
16 | import javax.swing.table.*; |
---|
17 | import javax.swing.tree.*; |
---|
18 | import ec.util.*; |
---|
19 | |
---|
20 | /* |
---|
21 | * Created on Apr 4, 2005 8:54:38 PM |
---|
22 | * |
---|
23 | * By: spaus |
---|
24 | */ |
---|
25 | |
---|
26 | /** |
---|
27 | * @author spaus |
---|
28 | */ |
---|
29 | public class ParametersPanel |
---|
30 | extends JPanel |
---|
31 | { |
---|
32 | |
---|
33 | private final Console console; |
---|
34 | |
---|
35 | private JScrollPane parameterTreeScrollPane = null; |
---|
36 | private JTree parameterTree = null; |
---|
37 | |
---|
38 | private JScrollPane parameterTableScrollPane = null; |
---|
39 | private JTable parameterTable = null; |
---|
40 | private JSplitPane jSplitPane = null; |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | /** |
---|
45 | * This method initializes jScrollPane |
---|
46 | * |
---|
47 | * @return javax.swing.JScrollPane |
---|
48 | */ |
---|
49 | private JScrollPane getParameterTreeScrollPane() |
---|
50 | { |
---|
51 | if (parameterTreeScrollPane == null) |
---|
52 | { |
---|
53 | parameterTreeScrollPane = new JScrollPane(); |
---|
54 | parameterTreeScrollPane.setViewportView(getParameterTree()); |
---|
55 | parameterTreeScrollPane.setPreferredSize(new java.awt.Dimension(150,363)); |
---|
56 | } |
---|
57 | return parameterTreeScrollPane; |
---|
58 | } |
---|
59 | /** |
---|
60 | * This method initializes jTree |
---|
61 | * |
---|
62 | * @return javax.swing.JTree |
---|
63 | */ |
---|
64 | private JTree getParameterTree() |
---|
65 | { |
---|
66 | if (parameterTree == null) |
---|
67 | { |
---|
68 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(); |
---|
69 | DefaultTreeModel model = new DefaultTreeModel(root); |
---|
70 | parameterTree = new JTree(model); |
---|
71 | parameterTree.setRootVisible(false); |
---|
72 | parameterTree.setShowsRootHandles(false); |
---|
73 | parameterTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() |
---|
74 | { |
---|
75 | public void valueChanged(javax.swing.event.TreeSelectionEvent e) |
---|
76 | { |
---|
77 | DefaultTableModel tableModel = (DefaultTableModel)parameterTable.getModel(); |
---|
78 | int rows = tableModel.getRowCount(); |
---|
79 | for (int row = rows-1; row >= 0; --row) |
---|
80 | { |
---|
81 | tableModel.removeRow(row); |
---|
82 | } |
---|
83 | |
---|
84 | TreePath path = e.getPath(); |
---|
85 | Object[] oPath = path.getPath(); |
---|
86 | StringBuffer sBuff = new StringBuffer(); |
---|
87 | // Ignore the root node. |
---|
88 | for (int i = 1; i < oPath.length; ++i) |
---|
89 | { |
---|
90 | sBuff.append(oPath[i]); |
---|
91 | if (i < oPath.length-1) |
---|
92 | { |
---|
93 | sBuff.append('.'); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | String[] newRow = |
---|
98 | {"", "", ""}; |
---|
99 | DefaultMutableTreeNode selected = (DefaultMutableTreeNode)oPath[oPath.length-1]; |
---|
100 | if (!selected.equals(oPath[0])) |
---|
101 | { |
---|
102 | newRow[0] = sBuff.toString(); |
---|
103 | Parameter param = new Parameter(newRow[0]); |
---|
104 | newRow[1] = console.parameters.getString(param,null); |
---|
105 | newRow[2] = console.parameters.getLocation(param); |
---|
106 | |
---|
107 | if (newRow[1] != null) |
---|
108 | { |
---|
109 | tableModel.addRow(newRow); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | if (!newRow[0].equals("")) |
---|
114 | { |
---|
115 | newRow[0] = newRow[0]+"."; |
---|
116 | } |
---|
117 | |
---|
118 | if(!selected.isLeaf()) |
---|
119 | { |
---|
120 | int children = selected.getChildCount(); |
---|
121 | for (int ch = 0; ch < children; ++ch) |
---|
122 | { |
---|
123 | TreeNode child = selected.getChildAt(ch); |
---|
124 | String childRow[] = new String[3]; |
---|
125 | childRow[0] = newRow[0]+child; |
---|
126 | Parameter param = new Parameter(childRow[0]); |
---|
127 | childRow[1] = console.parameters.getString(param,null); |
---|
128 | childRow[2] = console.parameters.getLocation(param); |
---|
129 | |
---|
130 | if (childRow[1] != null) |
---|
131 | { |
---|
132 | tableModel.addRow(childRow); |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | } |
---|
137 | }); |
---|
138 | } |
---|
139 | return parameterTree; |
---|
140 | } |
---|
141 | /** |
---|
142 | * This method initializes jScrollPane1 |
---|
143 | * |
---|
144 | * @return javax.swing.JScrollPane |
---|
145 | */ |
---|
146 | private JScrollPane getParameterTableScrollPane() |
---|
147 | { |
---|
148 | if (parameterTableScrollPane == null) |
---|
149 | { |
---|
150 | parameterTableScrollPane = new JScrollPane(); |
---|
151 | parameterTableScrollPane.setViewportView(getParameterTable()); |
---|
152 | } |
---|
153 | return parameterTableScrollPane; |
---|
154 | } |
---|
155 | /** |
---|
156 | * This method initializes jTable |
---|
157 | * |
---|
158 | * @return javax.swing.JTable |
---|
159 | */ |
---|
160 | private JTable getParameterTable() |
---|
161 | { |
---|
162 | if (parameterTable == null) |
---|
163 | { |
---|
164 | String[] cn = |
---|
165 | {"Parameter", "Value", "Source"}; |
---|
166 | DefaultTableModel model = new DefaultTableModel(cn,0); |
---|
167 | model.addTableModelListener(new TableModelListener() |
---|
168 | { |
---|
169 | public void tableChanged(TableModelEvent evt) |
---|
170 | { |
---|
171 | if (evt.getColumn() == 1 && |
---|
172 | evt.getType() == TableModelEvent.UPDATE) |
---|
173 | { |
---|
174 | |
---|
175 | int row = evt.getFirstRow(); |
---|
176 | DefaultTableModel model = (DefaultTableModel)evt.getSource(); |
---|
177 | String key = (String)model.getValueAt(row,0); |
---|
178 | String value = (String)model.getValueAt(row,1); |
---|
179 | Parameter parameter = new Parameter(key); |
---|
180 | if (!console.parameters.getString(parameter,null).equals(value)) |
---|
181 | { |
---|
182 | System.out.println("setting parameter "+parameter+" to "+value); |
---|
183 | console.parameters.set(parameter, value); |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | }); |
---|
188 | |
---|
189 | parameterTable = new JTable(model); |
---|
190 | parameterTable.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION); |
---|
191 | parameterTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF); |
---|
192 | parameterTable.getColumnModel().getColumn(0).setPreferredWidth(150); |
---|
193 | parameterTable.getColumnModel().getColumn(1).setPreferredWidth(150); |
---|
194 | parameterTable.getColumnModel().getColumn(2).setPreferredWidth(450); |
---|
195 | |
---|
196 | final JComboBox valueEditor = new JComboBox(); |
---|
197 | // valueEditor.setEditable(true); |
---|
198 | parameterTable.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(valueEditor)); |
---|
199 | valueEditor.addFocusListener(new FocusAdapter() |
---|
200 | { |
---|
201 | public void focusGained(FocusEvent evt) |
---|
202 | { |
---|
203 | int row = parameterTable.getSelectedRow(); |
---|
204 | String pName = (String)parameterTable.getValueAt(row,0); |
---|
205 | Set values = console.parameters.getShadowedValues(new Parameter(pName)); |
---|
206 | DefaultComboBoxModel model = new DefaultComboBoxModel(values.toArray()); |
---|
207 | valueEditor.setModel(model); |
---|
208 | } |
---|
209 | }); |
---|
210 | } |
---|
211 | |
---|
212 | return parameterTable; |
---|
213 | } |
---|
214 | /** |
---|
215 | * This method initializes jSplitPane |
---|
216 | * |
---|
217 | * @return javax.swing.JSplitPane |
---|
218 | */ |
---|
219 | private JSplitPane getJSplitPane() |
---|
220 | { |
---|
221 | if (jSplitPane == null) |
---|
222 | { |
---|
223 | jSplitPane = new JSplitPane(); |
---|
224 | jSplitPane.setLeftComponent(getParameterTreeScrollPane()); |
---|
225 | jSplitPane.setRightComponent(getParameterTableScrollPane()); |
---|
226 | jSplitPane.setDividerSize(5); |
---|
227 | jSplitPane.setDividerLocation(200); |
---|
228 | } |
---|
229 | return jSplitPane; |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * This is the default constructor |
---|
234 | */ |
---|
235 | public ParametersPanel(Console console) |
---|
236 | { |
---|
237 | super(); |
---|
238 | initialize(); |
---|
239 | this.console = console; |
---|
240 | } |
---|
241 | /** |
---|
242 | * This method initializes this |
---|
243 | * |
---|
244 | * @return void |
---|
245 | */ |
---|
246 | private void initialize() |
---|
247 | { |
---|
248 | this.setLayout(new BorderLayout()); |
---|
249 | this.setSize(645, 321); |
---|
250 | this.add(getJSplitPane(), java.awt.BorderLayout.CENTER); |
---|
251 | } |
---|
252 | |
---|
253 | void loadParameters() |
---|
254 | { |
---|
255 | parameterTree.setModel(console.parameters.buildTreeModel()); |
---|
256 | parameterTree.setRootVisible(true); |
---|
257 | } |
---|
258 | |
---|
259 | } |
---|