|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| package edu.rice.cs.drjava.ui; |
|
38 |
| |
|
39 |
| import edu.rice.cs.drjava.config.OptionConstants; |
|
40 |
| import edu.rice.cs.drjava.config.PropertyMaps; |
|
41 |
| import edu.rice.cs.drjava.config.DrJavaProperty; |
|
42 |
| import edu.rice.cs.plt.lambda.Runnable1; |
|
43 |
| import edu.rice.cs.plt.lambda.LambdaUtil; |
|
44 |
| import edu.rice.cs.plt.concurrent.CompletionMonitor; |
|
45 |
| import edu.rice.cs.plt.tuple.Pair; |
|
46 |
| import edu.rice.cs.util.swing.SwingFrame; |
|
47 |
| import edu.rice.cs.util.swing.Utilities; |
|
48 |
| |
|
49 |
| import javax.swing.*; |
|
50 |
| import javax.swing.event.*; |
|
51 |
| import javax.swing.table.DefaultTableModel; |
|
52 |
| import java.awt.*; |
|
53 |
| import java.awt.event.*; |
|
54 |
| import java.util.*; |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| public class InsertVariableDialog extends SwingFrame implements OptionConstants { |
|
60 |
| |
|
61 |
| JTabbedPane _tabbedPane = new JTabbedPane(); |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| private Map<String, JTable> _varTable = new HashMap<String, JTable>(); |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| private Map<String, DefaultTableModel> _varTableModel = new HashMap<String, DefaultTableModel>(); |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| private JTextField _varValueField; |
|
74 |
| |
|
75 |
| |
|
76 |
| private JTextPane _helpPane; |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| private JButton _okBtn; |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| private JButton _cancelBtn; |
|
85 |
| |
|
86 |
| |
|
87 |
| private MainFrame _mainFrame; |
|
88 |
| |
|
89 |
| |
|
90 |
| private Pair<String,DrJavaProperty> _selected = null; |
|
91 |
| |
|
92 |
| |
|
93 |
| private CompletionMonitor _cm; |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
38
| public InsertVariableDialog(MainFrame mf, CompletionMonitor cm) {
|
|
99 |
38
| super("Insert Variable");
|
|
100 |
38
| _mainFrame = mf;
|
|
101 |
38
| _cm = cm;
|
|
102 |
38
| initComponents();
|
|
103 |
38
| initDone();
|
|
104 |
38
| pack();
|
|
105 |
38
| Utilities.setPopupLoc(InsertVariableDialog.this, _mainFrame);
|
|
106 |
| } |
|
107 |
| |
|
108 |
| |
|
109 |
38
| private void initComponents() {
|
|
110 |
38
| super.getContentPane().setLayout(new GridLayout(1,1));
|
|
111 |
| |
|
112 |
38
| Action okAction = new AbstractAction("Select") {
|
|
113 |
0
| public void actionPerformed(ActionEvent e) {
|
|
114 |
0
| _okCommand();
|
|
115 |
| } |
|
116 |
| }; |
|
117 |
38
| _okBtn = new JButton(okAction);
|
|
118 |
| |
|
119 |
38
| Action cancelAction = new AbstractAction("Cancel") {
|
|
120 |
0
| public void actionPerformed(ActionEvent e) {
|
|
121 |
0
| _cancelCommand();
|
|
122 |
| } |
|
123 |
| }; |
|
124 |
38
| _cancelBtn = new JButton(cancelAction);
|
|
125 |
| |
|
126 |
38
| JPanel buttons = new JPanel();
|
|
127 |
38
| buttons.add(_okBtn);
|
|
128 |
38
| buttons.add(_cancelBtn);
|
|
129 |
| |
|
130 |
38
| _helpPane = new JTextPane();
|
|
131 |
38
| _helpPane.setToolTipText("Description of the variable.");
|
|
132 |
38
| _helpPane.setEditable(false);
|
|
133 |
38
| _helpPane.setPreferredSize(new Dimension(500,150));
|
|
134 |
38
| _helpPane.setBorder(new javax.swing.border.EmptyBorder(0,10,0,10));
|
|
135 |
38
| JScrollPane helpPaneSP = new JScrollPane(_helpPane);
|
|
136 |
38
| helpPaneSP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
|
|
137 |
| |
|
138 |
38
| _varValueField = new JTextField();
|
|
139 |
38
| updatePanes();
|
|
140 |
38
| _tabbedPane.addChangeListener(new ChangeListener() {
|
|
141 |
0
| public void stateChanged(ChangeEvent e) {
|
|
142 |
0
| if (_tabbedPane.getSelectedIndex() < 0) { return; }
|
|
143 |
0
| String category = _tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex());
|
|
144 |
0
| Map<String, DrJavaProperty> properties = PropertyMaps.TEMPLATE.getProperties(category);
|
|
145 |
0
| int row = _varTable.get(category).getSelectedRow();
|
|
146 |
0
| if (row < 0) { return; }
|
|
147 |
0
| String key = _varTableModel.get(category).getValueAt(row,0).toString();
|
|
148 |
0
| DrJavaProperty value = properties.get(key);
|
|
149 |
0
| _varValueField.setText(value.toString());
|
|
150 |
0
| _helpPane.setText(value.getHelp());
|
|
151 |
0
| _helpPane.setCaretPosition(0);
|
|
152 |
0
| _selected = Pair.make(key, value);
|
|
153 |
| } |
|
154 |
| }); |
|
155 |
| |
|
156 |
38
| JPanel main = new JPanel(new BorderLayout());
|
|
157 |
| |
|
158 |
38
| JPanel bottom = new JPanel(new BorderLayout());
|
|
159 |
38
| bottom.add(_varValueField, BorderLayout.CENTER);
|
|
160 |
38
| bottom.add(buttons, BorderLayout.SOUTH);
|
|
161 |
38
| main.add(bottom, BorderLayout.SOUTH);
|
|
162 |
| |
|
163 |
38
| GridBagLayout gridbag = new GridBagLayout();
|
|
164 |
38
| GridBagConstraints c = new GridBagConstraints();
|
|
165 |
38
| JPanel top = new JPanel(gridbag);
|
|
166 |
38
| Insets insets = new Insets(0, 10, 5, 10);
|
|
167 |
| |
|
168 |
| |
|
169 |
38
| c.fill = GridBagConstraints.BOTH;
|
|
170 |
38
| c.weightx = 1.0;
|
|
171 |
38
| c.weighty = 3.0;
|
|
172 |
38
| c.gridwidth = GridBagConstraints.REMAINDER;
|
|
173 |
38
| gridbag.setConstraints(_tabbedPane, c);
|
|
174 |
38
| top.add(_tabbedPane);
|
|
175 |
| |
|
176 |
38
| c.fill = GridBagConstraints.BOTH;
|
|
177 |
38
| c.weighty = 1.0;
|
|
178 |
38
| c.insets = insets;
|
|
179 |
38
| gridbag.setConstraints(helpPaneSP, c);
|
|
180 |
38
| top.add(helpPaneSP);
|
|
181 |
38
| main.add(top, BorderLayout.CENTER);
|
|
182 |
| |
|
183 |
| |
|
184 |
38
| _tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
|
|
185 |
| |
|
186 |
| |
|
187 |
38
| _tabbedPane.addFocusListener(new FocusAdapter() {
|
|
188 |
0
| public void focusLost(FocusEvent e) {
|
|
189 |
0
| if (e.getOppositeComponent() == _varValueField) {
|
|
190 |
0
| _tabbedPane.getSelectedComponent().requestFocus();
|
|
191 |
| } |
|
192 |
| } |
|
193 |
| }); |
|
194 |
| |
|
195 |
38
| super.getContentPane().add(main);
|
|
196 |
38
| super.setResizable(false);
|
|
197 |
| } |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
236
| protected JScrollPane createPane(final String category, final Map<String, DrJavaProperty> props) {
|
|
203 |
236
| _varTableModel.put(category,new DefaultTableModel(0,1) {
|
|
204 |
236
| public String getColumnName(int column) {
|
|
205 |
236
| switch(column) {
|
|
206 |
236
| case 0: return "Variable";
|
|
207 |
0
| default: return super.getColumnName(column);
|
|
208 |
| } |
|
209 |
| } |
|
210 |
| |
|
211 |
0
| public Class<?> getColumnClass(int columnIndex) {
|
|
212 |
0
| switch(columnIndex) {
|
|
213 |
0
| case 0: return String.class;
|
|
214 |
0
| default: return super.getColumnClass(columnIndex);
|
|
215 |
| } |
|
216 |
| } |
|
217 |
0
| public boolean isCellEditable(int row, int column) { return false; }
|
|
218 |
| }); |
|
219 |
| |
|
220 |
236
| _varTable.put(category, new JTable(_varTableModel.get(category)));
|
|
221 |
236
| JScrollPane varTableSP = new JScrollPane(_varTable.get(category));
|
|
222 |
236
| varTableSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
|
223 |
236
| varTableSP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
|
|
224 |
236
| _varTable.get(category).setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
|
225 |
236
| _varTable.get(category).setDragEnabled(false);
|
|
226 |
236
| _varTable.get(category).setPreferredScrollableViewportSize(new Dimension(500,250));
|
|
227 |
236
| _varTable.get(category).putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
|
|
228 |
236
| ListSelectionModel lsm = _varTable.get(category).getSelectionModel();
|
|
229 |
236
| lsm.addListSelectionListener(new ListSelectionListener() {
|
|
230 |
236
| public void valueChanged(ListSelectionEvent e) {
|
|
231 |
236
| int row = _varTable.get(category).getSelectedRow();
|
|
232 |
0
| if (row < 0) { return; }
|
|
233 |
236
| String key = _varTableModel.get(category).getValueAt(row,0).toString();
|
|
234 |
236
| DrJavaProperty value = PropertyMaps.TEMPLATE.getProperty(category,key);
|
|
235 |
236
| _selected = Pair.make(key, value);
|
|
236 |
236
| _varValueField.setText(value.getLazy(PropertyMaps.TEMPLATE));
|
|
237 |
236
| _helpPane.setText(value.getHelp());
|
|
238 |
236
| _helpPane.setCaretPosition(0);
|
|
239 |
| } |
|
240 |
| }); |
|
241 |
236
| _varTable.get(category).setSelectionModel(lsm);
|
|
242 |
| |
|
243 |
236
| TreeSet<String> sorted = new TreeSet<String>();
|
|
244 |
236
| for(DrJavaProperty p: PropertyMaps.TEMPLATE.getProperties(category).values()) {
|
|
245 |
16952
| sorted.add(p.getName());
|
|
246 |
| } |
|
247 |
| |
|
248 |
236
| for(String key: sorted) {
|
|
249 |
16952
| Vector<String> row = new Vector<String>();
|
|
250 |
16952
| row.add(key);
|
|
251 |
16952
| _varTableModel.get(category).addRow(row);
|
|
252 |
| } |
|
253 |
| |
|
254 |
236
| _varTable.get(category).setRowSelectionInterval(0,0);
|
|
255 |
| |
|
256 |
236
| return varTableSP;
|
|
257 |
| } |
|
258 |
| |
|
259 |
| |
|
260 |
0
| protected void _okCommand() {
|
|
261 |
0
| setVisible(false);
|
|
262 |
0
| _cm.signal();
|
|
263 |
| } |
|
264 |
| |
|
265 |
| |
|
266 |
0
| protected void _cancelCommand() {
|
|
267 |
0
| _selected = null;
|
|
268 |
0
| setVisible(false);
|
|
269 |
0
| _cm.signal();
|
|
270 |
| } |
|
271 |
| |
|
272 |
| |
|
273 |
38
| protected void updatePanes() {
|
|
274 |
38
| Pair<String,DrJavaProperty> sel = getSelected();
|
|
275 |
38
| String selCategory = null;
|
|
276 |
38
| if (sel != null) {
|
|
277 |
0
| selCategory = _tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex());
|
|
278 |
| } |
|
279 |
38
| _tabbedPane.removeAll();
|
|
280 |
38
| for (String category: PropertyMaps.TEMPLATE.getCategories()) {
|
|
281 |
236
| _tabbedPane.addTab(category, createPane(category, PropertyMaps.TEMPLATE.getProperties(category)));
|
|
282 |
| } |
|
283 |
38
| if (sel != null) {
|
|
284 |
0
| if (selCategory == null) { sel = null; } else {
|
|
285 |
0
| int i;
|
|
286 |
0
| for (i = 0; i < _tabbedPane.getTabCount(); ++i) {
|
|
287 |
0
| if (_tabbedPane.getTitleAt(i).equals(selCategory)) { _tabbedPane.setSelectedIndex(i); break; }
|
|
288 |
| } |
|
289 |
0
| if (i == _tabbedPane.getTabCount()) { sel = null; } else {
|
|
290 |
0
| DefaultTableModel tm = _varTableModel.get(selCategory);
|
|
291 |
0
| for (i = 0; i < tm.getRowCount(); ++i) {
|
|
292 |
0
| String key = tm.getValueAt(i,0).toString();
|
|
293 |
0
| if (key.equals(sel.second().getName())) {
|
|
294 |
0
| _varTable.get(selCategory).getSelectionModel().setSelectionInterval(i,i);
|
|
295 |
0
| break;
|
|
296 |
| } |
|
297 |
| } |
|
298 |
0
| if (i==tm.getRowCount()) {
|
|
299 |
| |
|
300 |
0
| _varTable.get(selCategory).getSelectionModel().setSelectionInterval(0,0);
|
|
301 |
| } |
|
302 |
0
| _varValueField.setText(sel.second().toString());
|
|
303 |
0
| _helpPane.setText(sel.second().getHelp());
|
|
304 |
0
| _helpPane.setCaretPosition(0);
|
|
305 |
0
| _selected = sel;
|
|
306 |
| } |
|
307 |
| } |
|
308 |
| } |
|
309 |
38
| if (sel == null) {
|
|
310 |
38
| _tabbedPane.setSelectedIndex(0);
|
|
311 |
38
| String category = _tabbedPane.getTitleAt(_tabbedPane.getSelectedIndex());
|
|
312 |
38
| Map<String, DrJavaProperty> properties = PropertyMaps.TEMPLATE.getProperties(category);
|
|
313 |
38
| _varTable.get(category).getSelectionModel().setSelectionInterval(0,0);
|
|
314 |
38
| int row = _varTable.get(category).getSelectedRow();
|
|
315 |
38
| if (row >= 0) {
|
|
316 |
38
| String key = _varTableModel.get(category).getValueAt(row,0).toString();
|
|
317 |
38
| DrJavaProperty value = properties.get(key);
|
|
318 |
38
| _varValueField.setText(value.toString());
|
|
319 |
38
| _helpPane.setText(value.getHelp());
|
|
320 |
38
| _helpPane.setCaretPosition(0);
|
|
321 |
38
| _selected = Pair.make(key, value);
|
|
322 |
| } |
|
323 |
| } |
|
324 |
| } |
|
325 |
| |
|
326 |
| |
|
327 |
38
| public Pair<String,DrJavaProperty> getSelected() { return _selected; }
|
|
328 |
| |
|
329 |
| |
|
330 |
| protected final Runnable1<WindowEvent> CANCEL = new Runnable1<WindowEvent>() { |
|
331 |
0
| public void run(WindowEvent e) { _cancelCommand(); }
|
|
332 |
| }; |
|
333 |
| |
|
334 |
| |
|
335 |
0
| public void setVisible(boolean vis) {
|
|
336 |
0
| assert EventQueue.isDispatchThread();
|
|
337 |
0
| validate();
|
|
338 |
0
| if (vis) {
|
|
339 |
0
| updatePanes();
|
|
340 |
0
| _mainFrame.hourglassOn();
|
|
341 |
0
| _mainFrame.installModalWindowAdapter(this, LambdaUtil.NO_OP, CANCEL);
|
|
342 |
| } |
|
343 |
| else { |
|
344 |
0
| _mainFrame.removeModalWindowAdapter(this);
|
|
345 |
0
| _mainFrame.hourglassOff();
|
|
346 |
0
| _mainFrame.toFront();
|
|
347 |
| } |
|
348 |
0
| super.setVisible(vis);
|
|
349 |
| } |
|
350 |
| } |