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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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

  ViewVC Help
Powered by ViewVC