/[svn]/jsampler/trunk/src/org/jsampler/view/ParameterTableModel.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/view/ParameterTableModel.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2148 - (show annotations) (download)
Thu Oct 14 14:45:42 2010 UTC (8 years ago) by iliev
File size: 15164 byte(s)
* Add/Edit Instrument dialog and New MIDI Instrument wizard
  are now resizable
* Using multicolumn menus for loading instruments from
  Instruments Database and selecting values in string list parameters

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2010 Grigor Iliev <grigor@grigoriliev.com>
5 *
6 * This file is part of JSampler.
7 *
8 * JSampler is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * JSampler is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with JSampler; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23 package org.jsampler.view;
24
25 import java.awt.Component;
26
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.ItemEvent;
30 import java.awt.event.ItemListener;
31
32 import java.util.Vector;
33
34 import javax.swing.AbstractCellEditor;
35 import javax.swing.BorderFactory;
36 import javax.swing.DefaultCellEditor;
37 import javax.swing.JButton;
38 import javax.swing.JCheckBox;
39 import javax.swing.JCheckBoxMenuItem;
40 import javax.swing.JComboBox;
41 import javax.swing.JPopupMenu;
42 import javax.swing.JTable;
43 import javax.swing.SpinnerNumberModel;
44 import javax.swing.UIManager;
45
46 import javax.swing.border.Border;
47
48 import javax.swing.event.PopupMenuEvent;
49 import javax.swing.event.PopupMenuListener;
50
51 import javax.swing.table.AbstractTableModel;
52 import javax.swing.table.DefaultTableCellRenderer;
53 import javax.swing.table.TableCellRenderer;
54 import javax.swing.table.TableCellEditor;
55 import org.jsampler.CC;
56
57 import org.jsampler.event.ParameterEvent;
58 import org.jsampler.event.ParameterListener;
59
60 import org.linuxsampler.lscp.Parameter;
61 import org.linuxsampler.lscp.ParameterType;
62 import org.linuxsampler.lscp.StringListParameter;
63
64
65 /**
66 * A tabular data model for representing LSCP parameters.
67 * @see ParameterTable
68 * @author Grigor Iliev
69 */
70 public class ParameterTableModel extends AbstractTableModel {
71 public final static int PARAMETER_NAME_COLUMN = 0;
72 public final static int PARAMETER_VALUE_COLUMN = 1;
73
74 private Parameter[] parameters;
75
76 private final BooleanCellRenderer booleanRenderer = new BooleanCellRenderer();
77 private final BooleanCellEditor booleanEditor = new BooleanCellEditor();
78
79 private final IntegerCellRenderer integerRenderer = new IntegerCellRenderer();
80 private final IntegerCellEditor integerEditor = new IntegerCellEditor();
81
82 private final FloatCellRenderer floatRenderer = new FloatCellRenderer();
83 private final FloatCellEditor floatEditor = new FloatCellEditor();
84
85 private final StringListCellRenderer stringListRenderer = new StringListCellRenderer();
86 private final StringListCellEditor stringListEditor = new StringListCellEditor();
87
88 private boolean editFixedParameters;
89
90
91 /**
92 * Creates a new instance of <code>ParameterTableModel</code>.
93 * @param parameters The parameters to be provided by this model.
94 */
95 public
96 ParameterTableModel(Parameter[] parameters) {
97 this(parameters, false);
98 }
99
100 /**
101 * Creates a new instance of <code>ParameterTableModel</code>.
102 * @param parameters The parameters to be provided by this model.
103 * @param editFixedParameters Determines whether fixed parameters are editable.
104 * Specify <code>true</code> to enable the editing of fixed parameters.
105 */
106 public
107 ParameterTableModel(Parameter[] parameters, boolean editFixedParameters) {
108 setParameters(parameters);
109 setEditFixedParameters(editFixedParameters);
110 }
111
112 private final Vector<ParameterListener> parameterListeners =
113 new Vector<ParameterListener>();
114
115 /**
116 * Registers the specified listener for receiving event messages.
117 * @param l The <code>ParameterListener</code> to register.
118 */
119 public void
120 addParameterListener(ParameterListener l) { parameterListeners.add(l); }
121
122 /**
123 * Removes the specified listener.
124 * @param l The <code>ParameterListener</code> to remove.
125 */
126 public void
127 removeParameterListener(ParameterListener l) { parameterListeners.remove(l); }
128
129 /**
130 * Sets whether fixed parameters are editable.
131 * @param b Specify <code>true</code> to enable the editing of fixed parameters.
132 * @see org.linuxsampler.lscp.Parameter#isFixed
133 */
134 public void
135 setEditFixedParameters(boolean b) { editFixedParameters = b; }
136
137 /**
138 * Determines whether fixed parameters are editable.
139 * @return <code>true</code> if the fixed parameters are
140 * editable, <code>false</code> otherwise.
141 * @see org.linuxsampler.lscp.Parameter#isFixed
142 */
143 public boolean
144 canEditFixedParameters() { return editFixedParameters; }
145
146 /**
147 * Gets the parameter at the specified row.
148 * @param row The row number of the parameter to be provided.
149 * @return The parameter at the specified row.
150 */
151 public Parameter
152 getParameter(int row) { return parameters[row]; }
153
154 /**
155 * Returns an appropriate renderer for the cell specified by
156 * <code>row</code> and <code>column</code>.
157 * @param row The row of the cell to render, where 0 is the first row.
158 * @param column The column of the cell to render, where 0 is the first column.
159 */
160 public TableCellRenderer
161 getCellRenderer(int row, int column) {
162 if(column != PARAMETER_VALUE_COLUMN) return null;
163
164 if(parameters[row].getType() == ParameterType.BOOL) return booleanRenderer;
165 else if(parameters[row].getType() == ParameterType.INT) return integerRenderer;
166 else if(parameters[row].getType() == ParameterType.FLOAT) return floatRenderer;
167 else if(parameters[row].getType() == ParameterType.STRING_LIST) {
168 return stringListRenderer;
169 }
170
171 return null;
172 }
173
174 /**
175 * Returns an appropriate editor for the cell specified by
176 * <code>row</code> and <code>column</code>.
177 * @param row The row of the cell to edit, where 0 is the first row.
178 * @param column The column of the cell to edit, where 0 is the first column.
179 */
180 public TableCellEditor
181 getCellEditor(int row, int column) {
182 if(column != PARAMETER_VALUE_COLUMN) return null;
183
184 Parameter p = parameters[row];
185 if(p.getType() == ParameterType.BOOL) return booleanEditor;
186
187 if(p.getType() == ParameterType.BOOL_LIST) {
188
189 } if(p.getType() == ParameterType.FLOAT_LIST) {
190
191 } if(p.getType() == ParameterType.INT_LIST) {
192
193 } if(p.getType() == ParameterType.STRING_LIST) {
194 // TODO: string list editor with no possibilities
195 /*StringListParameter slp = (StringListParameter)p;
196 if(slp.hasPossibilities()) {
197
198 }*/
199 return stringListEditor;
200 } else if(p.hasPossibilities()) {
201 JComboBox cb = new JComboBox(p.getPossibilities());
202 cb.setSelectedItem(null);
203 return new DefaultCellEditor(cb);
204 } else if(p.getType() == ParameterType.INT) {
205 Integer i = p.hasRangeMin() ? p.getRangeMin().intValue() : null;
206 integerEditor.setMinimum(i);
207
208 i = p.hasRangeMax() ? p.getRangeMax().intValue() : null;
209 integerEditor.setMaximum(i);
210
211 return integerEditor;
212 } else if(p.getType() == ParameterType.FLOAT) {
213 Float f = p.hasRangeMin() ? p.getRangeMin().floatValue() : null;
214 floatEditor.setMinimum(f);
215
216 f = p.hasRangeMax() ? p.getRangeMax().floatValue() : null;
217 floatEditor.setMaximum(f);
218
219 return floatEditor;
220 }
221
222 return null;
223 }
224
225 /** Gets the parameters that are shown in the table. */
226 public Parameter[]
227 getParameters() { return parameters; }
228
229 /**
230 * Sets the parameters to be shown in the table.
231 * @param parameters The parameters to be shown in the table.
232 */
233 public void
234 setParameters(Parameter[] parameters) {
235 this.parameters = parameters;
236 fireTableDataChanged();
237 }
238
239 /**
240 * Gets the number of columns in the model.
241 * @return The number of columns in the model.
242 */
243 public int
244 getColumnCount() { return 2; }
245
246 /**
247 * Gets the number of rows in the model.
248 * @return The number of rows in the model.
249 */
250 public int
251 getRowCount() { return parameters.length; }
252
253 /**
254 * Gets the name of the column at <code>columnIndex</code>.
255 * @return The name of the column at <code>columnIndex</code>.
256 */
257 public String
258 getColumnName(int col) { return " "; }
259
260 /**
261 * Gets the value for the cell at <code>columnIndex</code> and
262 * <code>rowIndex</code>.
263 * @param row The row whose value is to be queried.
264 * @param col The column whose value is to be queried.
265 * @return The value for the cell at <code>columnIndex</code> and
266 * <code>rowIndex</code>.
267 */
268 public Object
269 getValueAt(int row, int col) {
270 switch(col) {
271 case PARAMETER_NAME_COLUMN:
272 return parameters[row].getName();
273 case PARAMETER_VALUE_COLUMN:
274 return parameters[row].getValue();
275 }
276
277 return null;
278 }
279
280 /**
281 * Sets the value in the cell at <code>col</code>
282 * and <code>row</code> to <code>value</code>.
283 */
284 public void
285 setValueAt(Object value, int row, int col) {
286 if(col != PARAMETER_VALUE_COLUMN) return;
287
288 parameters[row].setValue(value);
289 fireTableCellUpdated(row, col);
290 fireParameterChanged(parameters[row]);
291 }
292
293 /**
294 * Returns <code>true</code> if the cell at
295 * <code>row</code> and <code>col</code> is editable.
296 */
297 public boolean
298 isCellEditable(int row, int col) {
299 switch(col) {
300 case PARAMETER_NAME_COLUMN:
301 return false;
302 case PARAMETER_VALUE_COLUMN:
303 return canEditFixedParameters() || !parameters[row].isFixed();
304 default: return false;
305 }
306 }
307
308 private void
309 fireParameterChanged(Parameter p) {
310 ParameterEvent e = new ParameterEvent(this, p);
311 for(ParameterListener l : parameterListeners) l.parameterChanged(e);
312 }
313
314 class BooleanCellRenderer extends JCheckBox implements TableCellRenderer {
315 private final Border emptyBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1);
316
317 BooleanCellRenderer() {
318 setHorizontalAlignment(CENTER);
319 setBorderPainted(true);
320 }
321
322 public Component
323 getTableCellRendererComponent (
324 JTable table,
325 Object value,
326 boolean isSelected,
327 boolean hasFocus,
328 int row,
329 int column
330 ) {
331 if (isSelected) {
332 setBackground(table.getSelectionBackground());
333 setForeground(table.getSelectionForeground());
334 } else {
335 setBackground(table.getBackground());
336 setForeground(table.getForeground());
337 }
338
339 Boolean b = (Boolean) value;
340 setSelected(b != null && b);
341
342 if(hasFocus) {
343 setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
344 } else { setBorder(emptyBorder); }
345
346 return this;
347 }
348 }
349
350 class BooleanCellEditor extends DefaultCellEditor {
351 BooleanCellEditor() {
352 super(new JCheckBox());
353 JCheckBox cb = (JCheckBox)getComponent();
354 cb.setHorizontalAlignment(JCheckBox.CENTER);
355 }
356 }
357
358 class IntegerCellEditor extends NumberCellEditor {
359
360 }
361
362 class IntegerCellRenderer extends DefaultTableCellRenderer {
363 IntegerCellRenderer() {
364 setHorizontalAlignment(RIGHT);
365 }
366 }
367
368 class FloatCellEditor extends NumberCellEditor {
369 FloatCellEditor() {
370 SpinnerNumberModel model = new SpinnerNumberModel(0.0, 0.0, 0.0, 1.0);
371 setModel(model);
372 }
373 }
374
375 class FloatCellRenderer extends DefaultTableCellRenderer {
376 FloatCellRenderer() {
377 setHorizontalAlignment(RIGHT);
378 }
379 }
380
381 class StringListCellEditor extends AbstractCellEditor implements TableCellEditor {
382 private final JButton editor = new JButton();
383 private final JPopupMenu menu = CC.getViewConfig().createMultiColumnPopupMenu();
384 private final Vector<JCheckBoxMenuItem> menuItems = new Vector<JCheckBoxMenuItem>();
385
386 StringListCellEditor() {
387 editor.setBorderPainted(false);
388 editor.setContentAreaFilled(false);
389 editor.setFocusPainted(false);
390 editor.setMargin(new java.awt.Insets(0, 0, 0, 0));
391 editor.setFont(editor.getFont().deriveFont(java.awt.Font.PLAIN));
392
393 editor.addActionListener(new ActionListener() {
394 public void
395 actionPerformed(ActionEvent e) { menu.show(editor, 0, 0); }
396 });
397
398 menu.addPopupMenuListener(new PopupMenuListener() {
399 public void
400 popupMenuCanceled(PopupMenuEvent e) {
401 StringListCellEditor.this.cancelCellEditing();
402 }
403
404 public void
405 popupMenuWillBecomeInvisible(PopupMenuEvent e) { }
406
407 public void
408 popupMenuWillBecomeVisible(PopupMenuEvent e) { }
409 });
410 }
411
412 private final Vector<String> strings = new Vector<String>();
413
414 public Object
415 getCellEditorValue() {
416 strings.removeAllElements();
417
418 for(JCheckBoxMenuItem i : menuItems) {
419 if(i.isSelected()) strings.add(i.getText());
420 }
421
422 return strings.toArray(new String[strings.size()]);
423 }
424
425 private final StringBuffer sb = new StringBuffer();
426
427 public Component
428 getTableCellEditorComponent (
429 JTable table,
430 Object value,
431 boolean isSelected,
432 int row,
433 int column
434 ) {
435 StringListParameter slp = (StringListParameter)parameters[row];
436 if(slp.getPossibilities().length == 0) {
437 editor.setText("");
438 return editor;
439 }
440 String[] poss = slp.getPossibilities()[0];
441 String[] vals = (String[])value;
442
443 sb.setLength(0);
444
445 if(vals != null) {
446 if(vals.length > 0) sb.append(vals[0]);
447
448 for(int i = 1; i < vals.length; i++) {
449 sb.append(", ").append(vals[i]);
450 }
451 }
452
453 editor.setText(sb.toString());
454
455 menu.removeAll();
456 menuItems.removeAllElements();
457
458 for(String s : poss) {
459 JCheckBoxMenuItem item = new JCheckBoxMenuItem(s);
460 setListener(item);
461
462 for(String s2 : vals) {
463 if(s2.equals(s)) item.setSelected(true);
464 }
465
466 menu.add(item);
467 menuItems.add(item);
468 }
469
470 return editor;
471 }
472
473 private void
474 setListener(JCheckBoxMenuItem i) {
475 i.addItemListener(new ItemListener() {
476 public void
477 itemStateChanged(ItemEvent e) {
478 StringListCellEditor.this.stopCellEditing();
479 }
480 });
481 }
482 }
483
484 /*private static JComboBox comboBox = new JComboBox();
485 class StringListCellEditor extends DefaultCellEditor {
486 StringListCellEditor() {
487 super(comboBox);
488 }
489
490 public Object
491 getCellEditorValue() {
492 Object o = comboBox.getSelectedItem();
493 if(o == null) return null;
494 String[] sS = new String[1];
495 sS[0] = (String)o;
496 return sS;
497 }
498
499 public Component
500 getTableCellEditorComponent (
501 JTable table,
502 Object value,
503 boolean isSelected,
504 int row,
505 int column
506 ) {
507 StringListParameter slp = (StringListParameter)parameters[row];
508 comboBox.removeAllItems();
509 for(String s : slp.getPossibilities()[0]) comboBox.addItem(s);
510 return comboBox;
511 }
512 }*/
513
514 class StringListCellRenderer extends DefaultTableCellRenderer {
515 private final StringBuffer sb = new StringBuffer();
516
517 public Component
518 getTableCellRendererComponent (
519 JTable table,
520 Object value,
521 boolean isSelected,
522 boolean hasFocus,
523 int row,
524 int column
525 ) {
526 if(value instanceof String) return super.getTableCellRendererComponent (
527 table, value, isSelected, hasFocus, row, column
528 );
529
530 String[] s = (String[])value;
531
532 sb.setLength(0);
533
534 if(s != null) {
535 if(s.length > 0) sb.append(s[0]);
536 for(int i = 1; i < s.length; i++) sb.append(", ").append(s[i]);
537 }
538
539 return super.getTableCellRendererComponent (
540 table, sb.toString(), isSelected, hasFocus, row, column
541 );
542 }
543 }
544 }

  ViewVC Help
Powered by ViewVC