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