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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1285 - (show annotations) (download)
Fri Aug 10 19:55:03 2007 UTC (16 years, 7 months ago) by iliev
File size: 20546 byte(s)
* Updated to version 0.6a. The Fantasia distribution is now
  capable of controlling all features available in LinuxSampler

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

  ViewVC Help
Powered by ViewVC