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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1204 - (show annotations) (download)
Thu May 24 21:43:45 2007 UTC (16 years, 11 months ago) by iliev
File size: 9492 byte(s)
upgrading to version 0.5a

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2007 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 import java.awt.Container;
27 import java.awt.Dimension;
28
29 import java.beans.PropertyChangeEvent;
30 import java.beans.PropertyChangeListener;
31
32 import java.util.Comparator;
33 import java.util.Vector;
34
35 import javax.swing.DefaultCellEditor;
36 import javax.swing.JTable;
37 import javax.swing.JTextField;
38 import javax.swing.JViewport;
39
40 import javax.swing.event.ChangeEvent;
41 import javax.swing.event.ListSelectionEvent;
42 import javax.swing.event.TableColumnModelEvent;
43 import javax.swing.event.TableColumnModelListener;
44
45 import javax.swing.table.TableCellEditor;
46 import javax.swing.table.TableColumn;
47 import javax.swing.table.TableRowSorter;
48
49 import org.linuxsampler.lscp.DbDirectoryInfo;
50 import org.linuxsampler.lscp.DbInstrumentInfo;
51
52 import org.linuxsampler.lscp.event.InstrumentsDbAdapter;
53 import org.linuxsampler.lscp.event.InstrumentsDbEvent;
54
55
56 /**
57 *
58 * @author Grigor Iliev
59 */
60 public abstract class AbstractInstrumentsDbTable extends JTable {
61 private final DefaultCellEditor nameEditor;
62 private final JTextField tfEditor = new JTextField();
63
64 private DbDirectoryTreeNode directoryNode;
65 private final InstrumentsDbTableRowSorter rowSorter;
66
67 private String createdDirectoryName = null;
68
69 /**
70 * Creates a new instance of <code>AbstractInstrumentsDbTable</code>
71 */
72 public AbstractInstrumentsDbTable() {
73 this(new InstrumentsDbTableModel());
74 }
75
76 /**
77 * Creates a new instance of <code>AbstractInstrumentsDbTable</code>
78 */
79 public AbstractInstrumentsDbTable(InstrumentsDbTableModel model) {
80 super(model);
81
82 setAutoResizeMode(AUTO_RESIZE_OFF);
83
84 rowSorter = new InstrumentsDbTableRowSorter(getModel());
85 setRowSorter(rowSorter);
86
87 putClientProperty("JTable.autoStartsEdit", false);
88
89 nameEditor = new DefaultCellEditor(tfEditor);
90 nameEditor.setClickCountToStart(5);
91 TableColumn dummy = getColumnModel().getColumn(getModel().getDummyColumnIndex());
92 dummy.setPreferredWidth(10);
93 }
94
95 public InstrumentsDbTableModel
96 getModel() { return (InstrumentsDbTableModel)super.getModel(); }
97
98 public TableCellEditor
99 getCellEditor(int row, int column) {
100 if(column == 0) return nameEditor;
101 return super.getCellEditor(row, column);
102 }
103
104 public DbDirectoryTreeNode
105 getSelectedDirectoryNode() {
106 int idx = getSelectedRow();
107 if(idx == -1) return null;
108 idx = convertRowIndexToModel(idx);
109 return getModel().getDirectoryNode(idx);
110 }
111
112 /**
113 * Selects the specified directory.
114 * The current selection is not altered if missing directory is specified.
115 * @param dir The name of the directory to select.
116 */
117 public void
118 setSelectedDirectory(String dir) {
119 int i = getModel().getDirectoryRowIndex(dir);
120 if(i == -1) return;
121 i = convertRowIndexToView(i);
122 if(i == -1) return;
123
124 getSelectionModel().setSelectionInterval(i, i);
125 }
126
127 /**
128 * Selects the specified instrument.
129 * The current selection is not altered if missing instrument is specified.
130 * @param instr The name of the instrument to select.
131 */
132 public void
133 setSelectedInstrument(String instr) {
134 int i = getModel().getInstrumentRowIndex(instr);
135 if(i == -1) return;
136 i = convertRowIndexToView(i);
137 if(i == -1) return;
138 getSelectionModel().setSelectionInterval(i, i);
139 }
140
141 /**
142 * Gets all selected directories.
143 */
144 public DbDirectoryInfo[]
145 getSelectedDirectories() {
146 int[] rows = getSelectedRows();
147 if(rows.length == 0) return new DbDirectoryInfo[0];
148
149 DbDirectoryTreeNode dir;
150 Vector<DbDirectoryInfo> v = new Vector<DbDirectoryInfo>();
151 for(int i : rows) {
152 dir = getModel().getDirectoryNode(convertRowIndexToModel(i));
153 if(dir != null) v.add(dir.getInfo());
154 }
155
156 return v.toArray(new DbDirectoryInfo[v.size()]);
157 }
158
159 /**
160 * Gets all selected instruments.
161 */
162 public DbInstrumentInfo[]
163 getSelectedInstruments() {
164 int[] rows = getSelectedRows();
165 if(rows.length == 0) return new DbInstrumentInfo[0];
166
167 DbInstrumentInfo instr;
168 Vector<DbInstrumentInfo> v = new Vector<DbInstrumentInfo>();
169 for(int i : rows) {
170 instr = getModel().getInstrument(convertRowIndexToModel(i));
171 if(instr != null) v.add(instr);
172 }
173
174 return v.toArray(new DbInstrumentInfo[v.size()]);
175 }
176
177 public boolean
178 editCellAt(int row, int column) {
179 if(!super.editCellAt(row, column)) return false;
180
181 Component c = getEditorComponent();
182 if(c != null) c.requestFocusInWindow();
183
184 return true;
185 }
186
187 /**
188 * Gets the directory node, which
189 * content is represented by this table.
190 */
191 public DbDirectoryTreeNode
192 getParentDirectoryNode() { return directoryNode; }
193
194 /**
195 * Sets the directory node, which
196 * content will be represented by this table.
197 */
198 public void
199 setParentDirectoryNode(DbDirectoryTreeNode node) {
200 getModel().setParentDirectoryNode(node);
201
202 if(directoryNode != null) directoryNode.removeInstrumentsDbListener(getHandler());
203 directoryNode = node;
204 if(directoryNode != null) directoryNode.addInstrumentsDbListener(getHandler());
205 }
206
207 /**
208 * Gets the name of the directory created by this frontend.
209 * This method is used to determine the directories created
210 * by this frontend.
211 * @return The name of the directory created by this frontend.
212 */
213 public String
214 getCreatedDirectoryName() { return createdDirectoryName; }
215
216 /**
217 * Sets the name of the created by this frontend.
218 * @param name The name of the directory created by this frontend.
219 */
220 public void
221 setCreatedDirectoryName(String name) { createdDirectoryName = name; }
222
223 public Object
224 getLeadObject() {
225 if(getSelectionModel().isSelectionEmpty()) return null;
226 int idx = getSelectionModel().getLeadSelectionIndex();
227 if(idx < 0) return null;
228 idx = this.convertRowIndexToModel(idx);
229
230 return getModel().getValueAt(idx, 0);
231 }
232
233 /*public void
234 columnMarginChanged(ChangeEvent e) {
235 if(isEditing()) removeEditor();
236 TableColumn resizingColumn = null;
237 if(getTableHeader() != null) resizingColumn = getTableHeader().getResizingColumn();
238 if (resizingColumn != null && autoResizeMode == AUTO_RESIZE_OFF) {
239 resizingColumn.setPreferredWidth(resizingColumn.getWidth());
240 }
241 setAutoResizeMode();
242 resizeAndRepaint();
243 }
244
245 public boolean
246 getScrollableTracksViewportWidth() {
247 return getPreferredSize().width < getParent().getWidth();
248 }
249
250 private void
251 setAutoResizeMode() {
252 Container c = getParent();
253 if (!(c instanceof JViewport)) return;
254
255 JViewport vp = (JViewport)c;
256 int w = vp.getWidth();
257 TableColumn dummy = getColumnModel().getColumn(getModel().getDummyColumnIndex());
258 int i = w - (getColumnModel().getTotalColumnWidth() - dummy.getWidth());
259 if(i > 5) {
260 if(getAutoResizeMode() != AUTO_RESIZE_LAST_COLUMN)
261 setAutoResizeMode(AUTO_RESIZE_LAST_COLUMN);
262 } else {
263 if(getAutoResizeMode() != AUTO_RESIZE_OFF)
264 setAutoResizeMode(AUTO_RESIZE_OFF);
265 }
266 }*/
267
268 private class InstrumentsDbTableRowSorter extends TableRowSorter<InstrumentsDbTableModel> {
269 InstrumentsDbTableRowSorter(InstrumentsDbTableModel model) {
270 super(model);
271 }
272
273 public Comparator<?>
274 getComparator(int column) {
275 Comparator c = getModel().getComparator(column);
276 if(c != null) return c;
277
278 return super.getComparator(column);
279 }
280
281 protected boolean
282 useToString(int column) {
283 return getModel().getComparator(column) == null;
284 }
285
286 public boolean
287 isSortable(int column) {
288 return getModel().isSortable(column);
289 }
290 }
291
292 private final EventHandler eventHandler = new EventHandler();
293
294 private EventHandler
295 getHandler() { return eventHandler; }
296
297 private class EventHandler extends InstrumentsDbAdapter {
298
299 /**
300 * Invoked when the number of instrument
301 * directories in a specific directory has changed.
302 */
303 public void
304 directoryCountChanged(InstrumentsDbEvent e) {
305 String rd = getModel().getRenamedDirectory();
306 int idx = getModel().getDirectoryRowIndex(rd);
307 if(idx != -1) {
308 setSelectedDirectory(rd);
309 getModel().setRenamedDirectory(null);
310 }
311
312 idx = getModel().getDirectoryRowIndex(getCreatedDirectoryName());
313 if(idx != -1) {
314 idx = convertRowIndexToView(idx);
315 if(idx != -1) {
316 getSelectionModel().setSelectionInterval(idx, idx);
317 editCellAt(idx, 0);
318 Component c = nameEditor.getComponent();
319 if(c instanceof JTextField) ((JTextField)c).selectAll();
320 setCreatedDirectoryName(null);
321 }
322 }
323 }
324
325 /**
326 * Invoked when the number of instruments
327 * in a specific directory has changed.
328 */
329 public void
330 instrumentCountChanged(InstrumentsDbEvent e) {
331 String ri = getModel().getRenamedInstrument();
332 int idx = getModel().getInstrumentRowIndex(ri);
333 if(idx != -1) {
334 setSelectedInstrument(ri);
335 getModel().setRenamedInstrument(null);
336 }
337 }
338 }
339 }

  ViewVC Help
Powered by ViewVC