1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2009 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.task; |
24 |
|
25 |
import java.util.Vector; |
26 |
|
27 |
import org.jsampler.CC; |
28 |
import org.jsampler.SamplerChannelModel; |
29 |
import org.jsampler.SamplerModel; |
30 |
|
31 |
import org.linuxsampler.lscp.SamplerChannel; |
32 |
|
33 |
import net.sf.juife.Task; |
34 |
|
35 |
import static org.jsampler.JSI18n.i18n; |
36 |
|
37 |
|
38 |
/** |
39 |
* This task updates the sampler channel list. |
40 |
* @author Grigor Iliev |
41 |
*/ |
42 |
public class UpdateChannels extends EnhancedTask { |
43 |
/** Creates a new instance of <code>UpdateChannels</code>. */ |
44 |
public |
45 |
UpdateChannels() { |
46 |
setTitle("UpdateChannels_task"); |
47 |
setDescription(i18n.getMessage("UpdateChannels.description")); |
48 |
} |
49 |
|
50 |
/** The entry point of the task. */ |
51 |
@Override |
52 |
public void |
53 |
exec() throws Exception { |
54 |
SamplerModel sm = CC.getSamplerModel(); |
55 |
Integer[] chnIDs = CC.getClient().getSamplerChannelIDs(); |
56 |
|
57 |
boolean found = false, changed = false; |
58 |
|
59 |
boolean isAdjustingOld = CC.getSamplerModel().getChannelListIsAdjusting(); |
60 |
|
61 |
javax.swing.SwingUtilities.invokeAndWait(new Runnable() { |
62 |
public void |
63 |
run() { |
64 |
CC.getSamplerModel().setChannelListIsAdjusting(true); |
65 |
CC.getMainFrame().setAutoUpdateChannelListUI(false); |
66 |
} |
67 |
}); |
68 |
|
69 |
for(SamplerChannelModel m : sm.getChannels()) { |
70 |
for(int i = 0; i < chnIDs.length; i++) { |
71 |
if(m.getChannelId() == chnIDs[i]) { |
72 |
chnIDs[i] = -1; |
73 |
found = true; |
74 |
} |
75 |
} |
76 |
|
77 |
if(!found) { |
78 |
sm.removeChannelById(m.getChannelId()); |
79 |
changed = true; |
80 |
} |
81 |
found = false; |
82 |
} |
83 |
|
84 |
Vector<SamplerChannel> v = new Vector<SamplerChannel>(); |
85 |
for(int id : chnIDs) { |
86 |
if(id >= 0) v.add(CC.getClient().getSamplerChannelInfo(id)); |
87 |
} |
88 |
|
89 |
for(int i = 0; i < v.size() - 1; i++) sm.addChannel(v.elementAt(i)); |
90 |
|
91 |
manageAutoUpdate(false); |
92 |
|
93 |
if(v.size() > 0) sm.addChannel(v.elementAt(v.size() - 1)); |
94 |
else if(!CC.getSamplerModel().getChannelListIsAdjusting()) { |
95 |
if(!isAdjustingOld && !changed); // do nothing if nothing is changed |
96 |
else sm.addChannel(null); // fire dummy event to end an adjusting sequence |
97 |
} |
98 |
} |
99 |
|
100 |
@Override |
101 |
public void |
102 |
onError(Exception e) { |
103 |
manageAutoUpdate(true); |
104 |
} |
105 |
|
106 |
private void |
107 |
manageAutoUpdate(boolean force) { |
108 |
if(!force) { |
109 |
Task[] tasks = CC.getTaskQueue().getPendingTasks(); |
110 |
for(Task t : tasks) if(t.equals(this)) return; |
111 |
} |
112 |
|
113 |
try { |
114 |
javax.swing.SwingUtilities.invokeAndWait(new Runnable() { |
115 |
public void |
116 |
run() { |
117 |
CC.getSamplerModel().setChannelListIsAdjusting(false); |
118 |
CC.getMainFrame().setAutoUpdateChannelListUI(true); |
119 |
CC.getMainFrame().updateChannelListUI(); |
120 |
} |
121 |
}); |
122 |
} catch(Exception x) { |
123 |
x.printStackTrace(); |
124 |
} |
125 |
} |
126 |
|
127 |
/** |
128 |
* Used to decrease the traffic. All task in the queue |
129 |
* equal to this are removed if added using {@link org.jsampler.CC#scheduleTask}. |
130 |
* @see org.jsampler.CC#addTask |
131 |
*/ |
132 |
@Override |
133 |
public boolean |
134 |
equals(Object obj) { |
135 |
if(obj == null) return false; |
136 |
if(!(obj instanceof UpdateChannels)) return false; |
137 |
|
138 |
return true; |
139 |
} |
140 |
} |