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