/[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 1352 - (show annotations) (download)
Sun Sep 16 23:24:15 2007 UTC (16 years, 7 months ago) by iliev
File size: 20614 byte(s)
* instruments db: slashes-in-names are now escaped with \x2f
* some bugfixes

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

  ViewVC Help
Powered by ViewVC