1 |
iliev |
787 |
/* |
2 |
|
|
* JSampler - a java front-end for LinuxSampler |
3 |
|
|
* |
4 |
iliev |
1143 |
* Copyright (C) 2005-2006 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 java.util.logging.Level; |
26 |
|
|
|
27 |
|
|
import org.jsampler.CC; |
28 |
|
|
import org.jsampler.HF; |
29 |
iliev |
1143 |
import org.jsampler.SamplerChannelModel; |
30 |
iliev |
787 |
import org.jsampler.SamplerModel; |
31 |
|
|
|
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 |
|
|
} |
48 |
|
|
|
49 |
iliev |
911 |
/** The entry point of the task. */ |
50 |
iliev |
787 |
public void |
51 |
|
|
run() { |
52 |
|
|
try { |
53 |
|
|
SamplerModel sm = CC.getSamplerModel(); |
54 |
|
|
Integer[] chnIDs = CC.getClient().getSamplerChannelIDs(); |
55 |
|
|
|
56 |
|
|
boolean found = false; |
57 |
iliev |
1734 |
|
58 |
|
|
javax.swing.SwingUtilities.invokeAndWait(new Runnable() { |
59 |
|
|
public void |
60 |
|
|
run() { CC.getMainFrame().setAutoUpdateChannelListUI(false); } |
61 |
|
|
}); |
62 |
|
|
|
63 |
iliev |
1204 |
for(SamplerChannelModel m : sm.getChannels()) { |
64 |
iliev |
787 |
for(int i = 0; i < chnIDs.length; i++) { |
65 |
iliev |
1143 |
if(m.getChannelId() == chnIDs[i]) { |
66 |
iliev |
787 |
chnIDs[i] = -1; |
67 |
|
|
found = true; |
68 |
|
|
} |
69 |
|
|
} |
70 |
|
|
|
71 |
iliev |
1204 |
if(!found) sm.removeChannelById(m.getChannelId()); |
72 |
iliev |
787 |
found = false; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
for(int id : chnIDs) { |
76 |
|
|
if(id >= 0) sm.addChannel(CC.getClient().getSamplerChannelInfo(id)); |
77 |
|
|
} |
78 |
iliev |
1734 |
|
79 |
|
|
manageAutoUpdate(false); |
80 |
iliev |
787 |
} catch(Exception x) { |
81 |
|
|
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
82 |
|
|
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
83 |
iliev |
1734 |
manageAutoUpdate(true); |
84 |
iliev |
787 |
} |
85 |
|
|
} |
86 |
iliev |
1734 |
|
87 |
|
|
private void |
88 |
|
|
manageAutoUpdate(boolean force) { |
89 |
|
|
if(!force) { |
90 |
|
|
Task[] tasks = CC.getTaskQueue().getPendingTasks(); |
91 |
|
|
for(Task t : tasks) if(t.equals(this)) return; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
javax.swing.SwingUtilities.invokeLater(new Runnable() { |
95 |
|
|
public void |
96 |
|
|
run() { |
97 |
|
|
CC.getMainFrame().setAutoUpdateChannelListUI(true); |
98 |
|
|
CC.getMainFrame().updateChannelListUI(); |
99 |
|
|
} |
100 |
|
|
}); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* Used to decrease the traffic. All task in the queue |
105 |
|
|
* equal to this are removed if added using {@link org.jsampler.CC#scheduleTask}. |
106 |
|
|
* @see org.jsampler.CC#addTask |
107 |
|
|
*/ |
108 |
|
|
public boolean |
109 |
|
|
equals(Object obj) { |
110 |
|
|
if(obj == null) return false; |
111 |
|
|
if(!(obj instanceof UpdateChannels)) return false; |
112 |
|
|
|
113 |
|
|
return true; |
114 |
|
|
} |
115 |
iliev |
787 |
} |