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.util.Vector; |
26 |
|
|
|
27 |
|
|
import javax.swing.event.ChangeEvent; |
28 |
|
|
import javax.swing.event.ChangeListener; |
29 |
|
|
|
30 |
|
|
import org.jsampler.CC; |
31 |
|
|
|
32 |
|
|
import org.linuxsampler.lscp.DbDirectoryInfo; |
33 |
|
|
import org.linuxsampler.lscp.DbInstrumentInfo; |
34 |
|
|
|
35 |
|
|
|
36 |
|
|
/** |
37 |
|
|
* |
38 |
|
|
* @author Grigor Iliev |
39 |
|
|
*/ |
40 |
|
|
public class DbClipboard { |
41 |
|
|
public static enum Operation { |
42 |
|
|
CUT, COPY, NONE |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
private Operation operation = Operation.NONE; |
46 |
|
|
private DbDirectoryInfo[] directories = new DbDirectoryInfo[0]; |
47 |
|
|
private DbInstrumentInfo[] instruments = new DbInstrumentInfo[0]; |
48 |
|
|
|
49 |
|
|
private final Vector<ChangeListener> listeners = new Vector<ChangeListener>(); |
50 |
|
|
|
51 |
|
|
/** Creates a new instance of <code>DbClipboard</code> */ |
52 |
|
|
public |
53 |
|
|
DbClipboard() { |
54 |
|
|
|
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
/** |
58 |
|
|
* Registers the specified listener to be notified |
59 |
|
|
* when the content of the clipboard is changed. |
60 |
|
|
*/ |
61 |
|
|
public void |
62 |
|
|
addChangeListener(ChangeListener l) { |
63 |
|
|
listeners.add(l); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
/** |
67 |
|
|
* Removes the specified listener. |
68 |
|
|
*/ |
69 |
|
|
public void |
70 |
|
|
removeChangeListener(ChangeListener l) { |
71 |
|
|
listeners.remove(l); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
public Operation |
75 |
|
|
getOperation() { return operation; } |
76 |
|
|
|
77 |
|
|
public void |
78 |
|
|
setOperation(Operation operation) { this.operation = operation; } |
79 |
|
|
|
80 |
|
|
/** |
81 |
|
|
* Gets the directories placed in the clipboard. |
82 |
|
|
*/ |
83 |
|
|
public DbDirectoryInfo[] |
84 |
|
|
getDirectories() { |
85 |
|
|
boolean b = false; |
86 |
|
|
InstrumentsDbTreeModel m = CC.getInstrumentsDbTreeModel(); |
87 |
|
|
for(int i = 0; i < directories.length; i++) { |
88 |
|
|
if(m.getNodeByPath(directories[i].getDirectoryPath()) == null) { |
89 |
|
|
b = true; |
90 |
|
|
directories[i] = null; |
91 |
|
|
} |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
if(!b) return directories; |
95 |
|
|
|
96 |
|
|
Vector<DbDirectoryInfo> v = new Vector<DbDirectoryInfo>(); |
97 |
|
|
|
98 |
|
|
for(DbDirectoryInfo dir : directories) { |
99 |
|
|
if(dir != null) v.add(dir); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
instruments = v.toArray(new DbInstrumentInfo[v.size()]); |
103 |
|
|
return directories; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
/** |
107 |
|
|
* Sets the directories placed in the clipboard. |
108 |
|
|
*/ |
109 |
|
|
public void |
110 |
|
|
setDirectories(DbDirectoryInfo[] directories) { |
111 |
|
|
this.directories = directories; |
112 |
|
|
fireChangeEvent(); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
/** |
116 |
|
|
* Gets the instruments in the clipboard. |
117 |
|
|
*/ |
118 |
|
|
public DbInstrumentInfo[] |
119 |
|
|
getInstruments() { |
120 |
|
|
boolean b = false; |
121 |
|
|
InstrumentsDbTreeModel m = CC.getInstrumentsDbTreeModel(); |
122 |
|
|
for(int i = 0; i < instruments.length; i++) { |
123 |
|
|
DbDirectoryTreeNode n = m.getNodeByPath(instruments[i].getDirectoryPath()); |
124 |
|
|
if(n == null) { |
125 |
|
|
b = true; |
126 |
|
|
instruments[i] = null; |
127 |
|
|
} else if(n.getInstrument(instruments[i].getName()) == null) { |
128 |
|
|
b = true; |
129 |
|
|
instruments[i] = null; |
130 |
|
|
} |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
if(!b) return instruments; |
134 |
|
|
|
135 |
|
|
Vector<DbInstrumentInfo> v = new Vector<DbInstrumentInfo>(); |
136 |
|
|
|
137 |
|
|
for(DbInstrumentInfo instr : instruments) { |
138 |
|
|
if(instr != null) v.add(instr); |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
instruments = v.toArray(new DbInstrumentInfo[v.size()]); |
142 |
|
|
return instruments; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
/** |
146 |
|
|
* Sets the instruments in the clipboard. |
147 |
|
|
*/ |
148 |
|
|
public void |
149 |
|
|
setInstruments(DbInstrumentInfo[] instruments) { |
150 |
|
|
this.instruments = instruments; |
151 |
|
|
fireChangeEvent(); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
private void |
155 |
|
|
fireChangeEvent() { |
156 |
|
|
ChangeEvent e = new ChangeEvent(this); |
157 |
|
|
for(ChangeListener l : listeners) l.stateChanged(e); |
158 |
|
|
} |
159 |
|
|
} |