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.util.Comparator; |
26 |
import java.util.Date; |
27 |
import java.util.Vector; |
28 |
|
29 |
import javax.swing.table.AbstractTableModel; |
30 |
|
31 |
import net.sf.juife.Task; |
32 |
import net.sf.juife.event.TaskEvent; |
33 |
import net.sf.juife.event.TaskListener; |
34 |
|
35 |
import org.jsampler.CC; |
36 |
import org.jsampler.task.InstrumentsDb; |
37 |
|
38 |
import org.linuxsampler.lscp.DbDirectoryInfo; |
39 |
import org.linuxsampler.lscp.DbInstrumentInfo; |
40 |
|
41 |
import org.linuxsampler.lscp.event.InstrumentsDbEvent; |
42 |
import org.linuxsampler.lscp.event.InstrumentsDbListener; |
43 |
|
44 |
import static org.jsampler.JSI18n.i18n; |
45 |
import static org.linuxsampler.lscp.Parser.*; |
46 |
|
47 |
/** |
48 |
* |
49 |
* @author Grigor Iliev |
50 |
*/ |
51 |
public class InstrumentsDbTableModel extends AbstractTableModel { |
52 |
public static enum ColumnType { |
53 |
NAME (i18n.getLabel("InstrumentsDbTableModel.NAME")), |
54 |
SIZE (i18n.getLabel("InstrumentsDbTableModel.SIZE")), |
55 |
FORMAT_FAMILY (i18n.getLabel("InstrumentsDbTableModel.FORMAT_FAMILY")), |
56 |
FORMAT_VERSION (i18n.getLabel("InstrumentsDbTableModel.FORMAT_VERSION")), |
57 |
IS_DRUM (i18n.getLabel("InstrumentsDbTableModel.IS_DRUM")), |
58 |
CREATED (i18n.getLabel("InstrumentsDbTableModel.CREATED")), |
59 |
MODIFIED (i18n.getLabel("InstrumentsDbTableModel.MODIFIED")), |
60 |
PRODUCT (i18n.getLabel("InstrumentsDbTableModel.PRODUCT")), |
61 |
ARTISTS (i18n.getLabel("InstrumentsDbTableModel.ARTISTS")), |
62 |
INSTRUMENT_FILE (i18n.getLabel("InstrumentsDbTableModel.INSTRUMENT_FILE")), |
63 |
INSTRUMENT_NR (i18n.getLabel("InstrumentsDbTableModel.INSTRUMENT_NR")), |
64 |
KEYWORDS (i18n.getLabel("InstrumentsDbTableModel.KEYWORDS")), |
65 |
DUMMY (""); |
66 |
|
67 |
private final String name; |
68 |
|
69 |
ColumnType(String name) { this.name = name; } |
70 |
|
71 |
public String |
72 |
toString() { return name; } |
73 |
} |
74 |
|
75 |
private Vector<ColumnType> columns = new Vector<ColumnType>(); |
76 |
|
77 |
private boolean showSizeColumn = true; |
78 |
private boolean showFormatFamilyColumn = true; |
79 |
private boolean showFormatVersionColumn = false; |
80 |
private boolean showIsDrumColumn = false; |
81 |
private boolean showCreatedColumn = false; |
82 |
private boolean showModifiedColumn = true; |
83 |
private boolean showProductColumn = false; |
84 |
private boolean showArtistsColumn = false; |
85 |
private boolean showInstrumentFileColumn = false; |
86 |
private boolean showInstrumentNrColumn = false; |
87 |
private boolean showKeywordsColumn = false; |
88 |
private boolean showDummyColumn = false; |
89 |
|
90 |
private DbDirectoryTreeNode directoryNode; |
91 |
|
92 |
|
93 |
/** Creates a new instance of <code>InstrumentsDbTableModel</code>. */ |
94 |
public |
95 |
InstrumentsDbTableModel() { |
96 |
this(null); |
97 |
} |
98 |
|
99 |
/** Creates a new instance of <code>InstrumentsDbTableModel</code>. */ |
100 |
public |
101 |
InstrumentsDbTableModel(DbDirectoryTreeNode node) { |
102 |
directoryNode = node; |
103 |
updateColumns(); |
104 |
} |
105 |
|
106 |
/** |
107 |
* Gets the type of the specified column. |
108 |
* @param index The index of the column. |
109 |
*/ |
110 |
public ColumnType |
111 |
getColumnType(int index) { return columns.get(index); } |
112 |
|
113 |
/** |
114 |
* Gets the index of the dummy column. |
115 |
*/ |
116 |
public int |
117 |
getDummyColumnIndex() { |
118 |
return columns.indexOf(ColumnType.DUMMY); |
119 |
} |
120 |
|
121 |
/** Sets only the <b>Name</b> column to be shown. */ |
122 |
public void |
123 |
showNameColumnOnly() { |
124 |
showSizeColumn = false; |
125 |
showFormatFamilyColumn = false; |
126 |
showFormatVersionColumn = false; |
127 |
showIsDrumColumn = false; |
128 |
showCreatedColumn = false; |
129 |
showModifiedColumn = false; |
130 |
showProductColumn = false; |
131 |
showArtistsColumn = false; |
132 |
showInstrumentFileColumn = false; |
133 |
showInstrumentNrColumn = false; |
134 |
showKeywordsColumn = false; |
135 |
updateColumns(); |
136 |
} |
137 |
|
138 |
/** Gets whether the <b>Size</b> column is shown. */ |
139 |
public boolean |
140 |
getShowSizeColumn() { return showSizeColumn; } |
141 |
|
142 |
/** Sets whether the <b>Size</b> column should be shown. */ |
143 |
public void |
144 |
setShowSizeColumn(boolean b) { |
145 |
if(b == showSizeColumn) return; |
146 |
showSizeColumn = b; |
147 |
updateColumns(); |
148 |
} |
149 |
|
150 |
/** Gets whether the <b>Format</b> column is shown. */ |
151 |
public boolean |
152 |
getShowFormatFamilyColumn() { return showFormatFamilyColumn; } |
153 |
|
154 |
/** Sets whether the <b>Format</b> column should be shown. */ |
155 |
public void |
156 |
setShowFormatFamilyColumn(boolean b) { |
157 |
if(b == showFormatFamilyColumn) return; |
158 |
showFormatFamilyColumn = b; |
159 |
updateColumns(); |
160 |
} |
161 |
|
162 |
/** Gets whether the <b>Version</b> column is shown. */ |
163 |
public boolean |
164 |
getShowFormatVersionColumn() { return showFormatVersionColumn; } |
165 |
|
166 |
/** Sets whether the <b>Version</b> column should be shown. */ |
167 |
public void |
168 |
setShowFormatVersionColumn(boolean b) { |
169 |
if(b == showFormatVersionColumn) return; |
170 |
showFormatVersionColumn = b; |
171 |
updateColumns(); |
172 |
} |
173 |
|
174 |
/** Gets whether the <b>Type</b> column is shown. */ |
175 |
public boolean |
176 |
getShowIsDrumColumn() { return showIsDrumColumn; } |
177 |
|
178 |
/** Sets whether the <b>Type</b> column should be shown. */ |
179 |
public void |
180 |
setShowIsDrumColumn(boolean b) { |
181 |
if(b == showIsDrumColumn) return; |
182 |
showIsDrumColumn = b; |
183 |
updateColumns(); |
184 |
} |
185 |
|
186 |
/** Gets whether the <b>Date Created</b> column is shown. */ |
187 |
public boolean |
188 |
getShowCreatedColumn() { return showCreatedColumn; } |
189 |
|
190 |
/** Sets whether the <b>Date Created</b> column should be shown. */ |
191 |
public void |
192 |
setShowCreatedColumn(boolean b) { |
193 |
if(b == showCreatedColumn) return; |
194 |
showCreatedColumn = b; |
195 |
updateColumns(); |
196 |
} |
197 |
|
198 |
/** Gets whether the <b>Date Modified</b> column is shown. */ |
199 |
public boolean |
200 |
getShowModifiedColumn() { return showModifiedColumn; } |
201 |
|
202 |
/** Sets whether the <b>Date Modified</b> column should be shown. */ |
203 |
public void |
204 |
setShowModifiedColumn(boolean b) { |
205 |
if(b == showModifiedColumn) return; |
206 |
showModifiedColumn = b; |
207 |
updateColumns(); |
208 |
} |
209 |
|
210 |
/** Gets whether the <b>Product</b> column is shown. */ |
211 |
public boolean |
212 |
getShowProductColumn() { return showProductColumn; } |
213 |
|
214 |
/** Sets whether the <b>Product</b> column should be shown. */ |
215 |
public void |
216 |
setShowProductColumn(boolean b) { |
217 |
if(b == showProductColumn) return; |
218 |
showProductColumn = b; |
219 |
updateColumns(); |
220 |
} |
221 |
|
222 |
/** Gets whether the <b>Artists</b> column is shown. */ |
223 |
public boolean |
224 |
getShowArtistsColumn() { return showArtistsColumn; } |
225 |
|
226 |
/** Sets whether the <b>Artists</b> column should be shown. */ |
227 |
public void |
228 |
setShowArtistsColumn(boolean b) { |
229 |
if(b == showArtistsColumn) return; |
230 |
showArtistsColumn = b; |
231 |
updateColumns(); |
232 |
} |
233 |
|
234 |
/** Gets whether the <b>Instrument File</b> column is shown. */ |
235 |
public boolean |
236 |
getShowInstrumentFileColumn() { return showInstrumentFileColumn; } |
237 |
|
238 |
/** Sets whether the <b>Instrument File</b> column should be shown. */ |
239 |
public void |
240 |
setShowInstrumentFileColumn(boolean b) { |
241 |
if(b == showInstrumentFileColumn) return; |
242 |
showInstrumentFileColumn = b; |
243 |
updateColumns(); |
244 |
} |
245 |
|
246 |
/** Gets whether the <b>Index</b> column is shown. */ |
247 |
public boolean |
248 |
getShowInstrumentNrColumn() { return showInstrumentNrColumn; } |
249 |
|
250 |
/** Sets whether the <b>Index</b> column should be shown. */ |
251 |
public void |
252 |
setShowInstrumentNrColumn(boolean b) { |
253 |
if(b == showInstrumentNrColumn) return; |
254 |
showInstrumentNrColumn = b; |
255 |
updateColumns(); |
256 |
} |
257 |
|
258 |
/** Gets whether the <b>Keywords</b> column is shown. */ |
259 |
public boolean |
260 |
getShowKeywordsColumn() { return showKeywordsColumn; } |
261 |
|
262 |
/** Sets whether the <b>Keywords</b> column should be shown. */ |
263 |
public void |
264 |
setShowKeywordsColumn(boolean b) { |
265 |
if(b == showKeywordsColumn) return; |
266 |
showKeywordsColumn = b; |
267 |
updateColumns(); |
268 |
} |
269 |
|
270 |
/** Gets whether the <b>Dummy</b> column is shown. */ |
271 |
public boolean |
272 |
getShowDummyColumn() { return showDummyColumn; } |
273 |
|
274 |
/** Sets whether the <b>Dummy</b> column should be shown. */ |
275 |
public void |
276 |
setShowDummyColumn(boolean b) { |
277 |
if(b == showDummyColumn) return; |
278 |
showDummyColumn = b; |
279 |
updateColumns(); |
280 |
} |
281 |
|
282 |
/** |
283 |
* Returns a comparator for the specified column or <code>null</code> |
284 |
* if there is no suitable comparator for the specified column. |
285 |
*/ |
286 |
public Comparator |
287 |
getComparator(int col) { |
288 |
if(columns.get(col) == ColumnType.NAME) return nameComparator; |
289 |
if(columns.get(col) == ColumnType.CREATED) return dateComparator; |
290 |
if(columns.get(col) == ColumnType.MODIFIED) return dateComparator; |
291 |
if(columns.get(col) == ColumnType.SIZE) return sizeComparator; |
292 |
if(columns.get(col) == ColumnType.INSTRUMENT_NR) return numberComparator; |
293 |
return null; |
294 |
} |
295 |
|
296 |
/** |
297 |
* Determines whether the specified column is sortable. |
298 |
* @return <code>true</code> if the specified column is |
299 |
* sortable, <code>false</code> otherwise. |
300 |
*/ |
301 |
public boolean |
302 |
isSortable(int col) { |
303 |
if(columns.get(col) == ColumnType.DUMMY) return false; |
304 |
return true; |
305 |
} |
306 |
|
307 |
private NameComparator nameComparator = new NameComparator(); |
308 |
|
309 |
private class NameComparator implements Comparator { |
310 |
public int |
311 |
compare(Object o1, Object o2) { |
312 |
if (o1 instanceof DbInstrumentInfo && o2 instanceof DbDirectoryInfo) { |
313 |
return 1; |
314 |
} |
315 |
if (o1 instanceof DbDirectoryInfo && o2 instanceof DbInstrumentInfo) { |
316 |
return -1; |
317 |
} |
318 |
|
319 |
return o1.toString().compareToIgnoreCase(o2.toString()); |
320 |
} |
321 |
} |
322 |
|
323 |
private DateComparator dateComparator = new DateComparator(); |
324 |
|
325 |
private class DateComparator implements Comparator { |
326 |
public int |
327 |
compare(Object o1, Object o2) { |
328 |
if (o1 instanceof Date && o2 instanceof Date) { |
329 |
return ((Date)o1).compareTo((Date)o2); |
330 |
} |
331 |
|
332 |
return o1.toString().compareToIgnoreCase(o2.toString()); |
333 |
} |
334 |
} |
335 |
|
336 |
private SizeComparator sizeComparator = new SizeComparator(); |
337 |
|
338 |
private class SizeComparator implements Comparator { |
339 |
public int |
340 |
compare(Object o1, Object o2) { |
341 |
if (o1 instanceof InstrumentSize && o2 instanceof InstrumentSize) { |
342 |
long l1 = ((InstrumentSize)o1).getSize(); |
343 |
long l2 = ((InstrumentSize)o2).getSize(); |
344 |
if(l1 < l2) return -1; |
345 |
if(l1 > l2) return 1; |
346 |
return 0; |
347 |
} |
348 |
|
349 |
return o1.toString().compareToIgnoreCase(o2.toString()); |
350 |
} |
351 |
} |
352 |
|
353 |
private NumberComparator numberComparator = new NumberComparator(); |
354 |
|
355 |
private class NumberComparator implements Comparator { |
356 |
public int |
357 |
compare(Object o1, Object o2) { |
358 |
if (o1 instanceof Integer && o2 instanceof Integer) { |
359 |
int i1 = (Integer)o1; |
360 |
int i2 = (Integer)o2; |
361 |
if(i1 < i2) return -1; |
362 |
if(i1 > i2) return 1; |
363 |
return 0; |
364 |
} |
365 |
|
366 |
return o1.toString().compareToIgnoreCase(o2.toString()); |
367 |
} |
368 |
} |
369 |
|
370 |
private class InstrumentSize { |
371 |
private DbInstrumentInfo instrument; |
372 |
|
373 |
InstrumentSize(DbInstrumentInfo instr) { |
374 |
instrument = instr; |
375 |
} |
376 |
|
377 |
public long |
378 |
getSize() { return instrument.getSize(); } |
379 |
|
380 |
public String |
381 |
toString() { return instrument.getFormatedSize(); } |
382 |
} |
383 |
|
384 |
private void |
385 |
updateColumns() { |
386 |
columns.removeAllElements(); |
387 |
columns.add(ColumnType.NAME); |
388 |
if(showSizeColumn) columns.add(ColumnType.SIZE); |
389 |
if(showFormatFamilyColumn) columns.add(ColumnType.FORMAT_FAMILY); |
390 |
if(showFormatVersionColumn) columns.add(ColumnType.FORMAT_VERSION); |
391 |
if(showIsDrumColumn) columns.add(ColumnType.IS_DRUM); |
392 |
if(showCreatedColumn) columns.add(ColumnType.CREATED); |
393 |
if(showModifiedColumn) columns.add(ColumnType.MODIFIED); |
394 |
if(showProductColumn) columns.add(ColumnType.PRODUCT); |
395 |
if(showArtistsColumn) columns.add(ColumnType.ARTISTS); |
396 |
if(showInstrumentFileColumn) columns.add(ColumnType.INSTRUMENT_FILE); |
397 |
if(showInstrumentNrColumn) columns.add(ColumnType.INSTRUMENT_NR); |
398 |
if(showKeywordsColumn) columns.add(ColumnType.KEYWORDS); |
399 |
if(showDummyColumn) columns.add(ColumnType.DUMMY); |
400 |
|
401 |
fireTableStructureChanged(); |
402 |
} |
403 |
|
404 |
/** |
405 |
* Gets the directory node, which |
406 |
* content is represented by this table model. |
407 |
*/ |
408 |
protected DbDirectoryTreeNode |
409 |
getParentDirectoryNode() { return directoryNode; } |
410 |
|
411 |
/** |
412 |
* Sets the directory node, which |
413 |
* content will be represented by this table model. |
414 |
*/ |
415 |
protected void |
416 |
setParentDirectoryNode(DbDirectoryTreeNode node) { |
417 |
if(directoryNode != null) directoryNode.removeInstrumentsDbListener(getHandler()); |
418 |
directoryNode = node; |
419 |
if(directoryNode != null) directoryNode.addInstrumentsDbListener(getHandler()); |
420 |
fireTableDataChanged(); |
421 |
} |
422 |
|
423 |
private String renamedInstrument = null; |
424 |
|
425 |
/** |
426 |
* Gets the name of the last renamed instrument through this table model. |
427 |
*/ |
428 |
public String |
429 |
getRenamedInstrument() { return renamedInstrument; } |
430 |
|
431 |
/** |
432 |
* Sets the name of the last |
433 |
* renamed instrument through this table model. |
434 |
*/ |
435 |
public void |
436 |
setRenamedInstrument(String instr) { renamedInstrument = instr; } |
437 |
|
438 |
private String renamedDirectory = null; |
439 |
|
440 |
/** |
441 |
* Gets the name of the last |
442 |
* renamed directory through this table model. |
443 |
*/ |
444 |
public String |
445 |
getRenamedDirectory() { return renamedDirectory; } |
446 |
|
447 |
/** |
448 |
* Sets the name of the last |
449 |
* renamed directory through this table model. |
450 |
*/ |
451 |
public void |
452 |
setRenamedDirectory(String dir) { renamedDirectory = dir; } |
453 |
|
454 |
/** |
455 |
* Gets the number of columns in the model. |
456 |
* @return The number of columns in the model. |
457 |
*/ |
458 |
public int |
459 |
getColumnCount() { return columns.size(); } |
460 |
|
461 |
/** |
462 |
* Gets the number of rows in the model. |
463 |
* @return The number of rows in the model. |
464 |
*/ |
465 |
public int |
466 |
getRowCount() { |
467 |
if(directoryNode == null) return 0; |
468 |
return directoryNode.getChildCount() + directoryNode.getInstrumentCount(); |
469 |
} |
470 |
|
471 |
/** |
472 |
* Gets the name of the column at <code>columnIndex</code>. |
473 |
* @return The name of the column at <code>columnIndex</code>. |
474 |
*/ |
475 |
public String |
476 |
getColumnName(int col) { |
477 |
return columns.get(col).toString(); |
478 |
} |
479 |
|
480 |
/** |
481 |
* Gets the directory at the specified row index or |
482 |
* <code>null</code> there is no directory at the specified index. |
483 |
*/ |
484 |
public DbDirectoryTreeNode |
485 |
getDirectoryNode(int index) { |
486 |
if(directoryNode == null) return null; |
487 |
if(index >= directoryNode.getChildCount()) return null; |
488 |
return directoryNode.getChildAt(index); |
489 |
} |
490 |
|
491 |
/** |
492 |
* Gets the instrument at the specified row index or |
493 |
* <code>null</code> there is no instrument at the specified index. |
494 |
*/ |
495 |
public DbInstrumentInfo |
496 |
getInstrument(int index) { |
497 |
index -= directoryNode.getChildCount(); |
498 |
if(index < 0) return null; |
499 |
return directoryNode.getInstrumentAt(index); |
500 |
} |
501 |
|
502 |
/** |
503 |
* Gets the row index of the specified directory. |
504 |
* @param dir The name of the directory. |
505 |
* @return The row index of the specified directory or |
506 |
* <code>-1</code> if the specified directory is not found. |
507 |
*/ |
508 |
public int |
509 |
getDirectoryRowIndex(String dir) { |
510 |
if(dir == null || dir.length() == 0) return -1; |
511 |
|
512 |
for(int i = 0; i < directoryNode.getChildCount(); i++) { |
513 |
if(dir.equals(directoryNode.getChildAt(i).getInfo().getName())) return i; |
514 |
} |
515 |
|
516 |
return -1; |
517 |
} |
518 |
|
519 |
/** |
520 |
* Gets the row index of the specified instrument. |
521 |
* @param instr The name of the instrument. |
522 |
* @return The row index of the specified instrument or |
523 |
* <code>-1</code> if the specified instrument is not found. |
524 |
*/ |
525 |
public int |
526 |
getInstrumentRowIndex(String instr) { |
527 |
if(instr == null || instr.length() == 0) return -1; |
528 |
|
529 |
for(int i = 0; i < directoryNode.getInstrumentCount(); i++) { |
530 |
if(instr.equals(directoryNode.getInstrumentAt(i).getName())) { |
531 |
return i + directoryNode.getChildCount(); |
532 |
} |
533 |
} |
534 |
|
535 |
return -1; |
536 |
} |
537 |
|
538 |
/** |
539 |
* Gets the value for the cell at <code>columnIndex</code> and |
540 |
* <code>rowIndex</code>. |
541 |
* @param row The row whose value is to be queried. |
542 |
* @param col The column whose value is to be queried. |
543 |
* @return The value for the cell at <code>columnIndex</code> and |
544 |
* <code>rowIndex</code>. |
545 |
*/ |
546 |
public Object |
547 |
getValueAt(int row, int col) { |
548 |
if(directoryNode.getChildCount() > row) { |
549 |
DbDirectoryInfo info = directoryNode.getChildAt(row).getInfo(); |
550 |
|
551 |
if(columns.get(col) == ColumnType.NAME) return info; |
552 |
if(columns.get(col) == ColumnType.MODIFIED) { |
553 |
return info.getDateModified(); |
554 |
} |
555 |
if(columns.get(col) == ColumnType.CREATED) { |
556 |
return info.getDateCreated(); |
557 |
} |
558 |
|
559 |
return ""; |
560 |
} |
561 |
|
562 |
DbInstrumentInfo instr; |
563 |
instr = directoryNode.getInstrumentAt(row - directoryNode.getChildCount()); |
564 |
|
565 |
switch(columns.get(col)) { |
566 |
case NAME: |
567 |
return instr; |
568 |
|
569 |
case SIZE: |
570 |
return new InstrumentSize(instr); |
571 |
|
572 |
case FORMAT_FAMILY: |
573 |
return instr.getFormatFamily(); |
574 |
|
575 |
case FORMAT_VERSION: |
576 |
return instr.getFormatVersion(); |
577 |
|
578 |
case IS_DRUM: |
579 |
if(instr.isDrum()) return i18n.getLabel("InstrumentsDbTableModel.drumkit"); |
580 |
return i18n.getLabel("InstrumentsDbTableModel.chromatic"); |
581 |
|
582 |
case CREATED: |
583 |
return instr.getDateCreated(); |
584 |
|
585 |
case MODIFIED: |
586 |
return instr.getDateModified(); |
587 |
|
588 |
case PRODUCT: |
589 |
return instr.getProduct(); |
590 |
|
591 |
case ARTISTS: |
592 |
return instr.getArtists(); |
593 |
|
594 |
case INSTRUMENT_FILE: |
595 |
return instr.getFilePath(); |
596 |
|
597 |
case INSTRUMENT_NR: |
598 |
return instr.getInstrumentIndex(); |
599 |
|
600 |
case KEYWORDS: |
601 |
return instr.getKeywords(); |
602 |
|
603 |
case DUMMY: |
604 |
return ""; |
605 |
|
606 |
} |
607 |
|
608 |
return null; |
609 |
} |
610 |
|
611 |
/** |
612 |
* Sets the value in the cell at <code>col</code> |
613 |
* and <code>row</code> to <code>value</code>. |
614 |
*/ |
615 |
public void |
616 |
setValueAt(Object value, final int row, final int col) { |
617 |
String s = value.toString(); |
618 |
final String oldName; |
619 |
final Task t; |
620 |
if(directoryNode.getChildCount() > row) { |
621 |
final DbDirectoryInfo info = directoryNode.getChildAt(row).getInfo(); |
622 |
oldName = info.getName(); |
623 |
if(oldName.equals(s)) return; |
624 |
t = new InstrumentsDb.RenameDirectory(info.getDirectoryPath(), s); |
625 |
info.setName(s); |
626 |
setRenamedDirectory(info.getName()); |
627 |
fireTableCellUpdated(row, col); |
628 |
t.addTaskListener(new TaskListener() { |
629 |
public void |
630 |
taskPerformed(TaskEvent e) { |
631 |
if(!t.doneWithErrors()) return; |
632 |
info.setName(oldName); |
633 |
fireTableCellUpdated(row, col); |
634 |
} |
635 |
}); |
636 |
CC.getTaskQueue().add(t); |
637 |
return; |
638 |
} |
639 |
|
640 |
final DbInstrumentInfo instr; |
641 |
instr = directoryNode.getInstrumentAt(row - directoryNode.getChildCount()); |
642 |
oldName = instr.getName(); |
643 |
if(oldName.equals(s)) return; |
644 |
t = new InstrumentsDb.RenameInstrument(instr.getInstrumentPath(), s); |
645 |
instr.setName(s); |
646 |
setRenamedInstrument(instr.getName()); |
647 |
fireTableCellUpdated(row, col); |
648 |
t.addTaskListener(new TaskListener() { |
649 |
public void |
650 |
taskPerformed(TaskEvent e) { |
651 |
if(!t.doneWithErrors()) return; |
652 |
instr.setName(oldName); |
653 |
fireTableCellUpdated(row, col); |
654 |
} |
655 |
}); |
656 |
CC.getTaskQueue().add(t); |
657 |
} |
658 |
|
659 |
/** |
660 |
* Returns <code>true</code> if the cell at |
661 |
* <code>row</code> and <code>col</code> is editable. |
662 |
*/ |
663 |
public boolean |
664 |
isCellEditable(int row, int col) { |
665 |
if(columns.get(col) == ColumnType.NAME) return true; |
666 |
return false; |
667 |
} |
668 |
|
669 |
private final EventHandler eventHandler = new EventHandler(); |
670 |
|
671 |
private EventHandler |
672 |
getHandler() { return eventHandler; } |
673 |
|
674 |
private class EventHandler implements InstrumentsDbListener { |
675 |
/** |
676 |
* Invoked when the number of instrument |
677 |
* directories in a specific directory has changed. |
678 |
*/ |
679 |
public void |
680 |
directoryCountChanged(final InstrumentsDbEvent e) { |
681 |
fireTableDataChanged(); |
682 |
} |
683 |
|
684 |
/** |
685 |
* Invoked when the settings of an instrument directory are changed. |
686 |
*/ |
687 |
public void |
688 |
directoryInfoChanged(InstrumentsDbEvent e) { |
689 |
|
690 |
} |
691 |
|
692 |
/** |
693 |
* Invoked when an instrument directory is renamed. |
694 |
*/ |
695 |
public void |
696 |
directoryNameChanged(InstrumentsDbEvent e) { |
697 |
String d = e.getPathName(); |
698 |
DbDirectoryInfo dir = getParentDirectoryNode().getInfo(); |
699 |
if(dir == null || !d.startsWith(dir.getDirectoryPath())) return; |
700 |
d = d.substring(dir.getDirectoryPath().length(), d.length()); |
701 |
if(d.length() == 0) return; |
702 |
if(d.charAt(0) == '/') d = d.substring(1, d.length()); |
703 |
int row = getDirectoryRowIndex(toNonEscapedFileName(d)); |
704 |
if(row == -1) return; |
705 |
fireTableRowsUpdated(row, row); |
706 |
} |
707 |
|
708 |
/** |
709 |
* Invoked when the number of instruments |
710 |
* in a specific directory has changed. |
711 |
*/ |
712 |
public void |
713 |
instrumentCountChanged(final InstrumentsDbEvent e) { |
714 |
fireTableDataChanged(); |
715 |
} |
716 |
|
717 |
/** |
718 |
* Invoked when the settings of an instrument are changed. |
719 |
*/ |
720 |
public void |
721 |
instrumentInfoChanged(InstrumentsDbEvent e) { |
722 |
String instr = e.getPathName(); |
723 |
DbDirectoryInfo dir = getParentDirectoryNode().getInfo(); |
724 |
if(dir == null || !instr.startsWith(dir.getDirectoryPath())) return; |
725 |
instr = instr.substring(dir.getDirectoryPath().length(), instr.length()); |
726 |
if(instr.length() == 0) return; |
727 |
if(instr.charAt(0) == '/') instr = instr.substring(1, instr.length()); |
728 |
int row = getInstrumentRowIndex(instr); |
729 |
if(row == -1) return; |
730 |
fireTableRowsUpdated(row, row); |
731 |
} |
732 |
|
733 |
/** |
734 |
* Invoked when an instrument is renamed. |
735 |
*/ |
736 |
public void |
737 |
instrumentNameChanged(InstrumentsDbEvent e) { |
738 |
|
739 |
} |
740 |
|
741 |
/** Invoked when the status of particular job has changed. */ |
742 |
public void |
743 |
jobStatusChanged(InstrumentsDbEvent e) { } |
744 |
} |
745 |
} |