1 |
/* |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
* |
4 |
* Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com> |
* Copyright (C) 2005-2008 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
* |
6 |
* This file is part of JSampler. |
* This file is part of JSampler. |
7 |
* |
* |
23 |
package org.jsampler.view; |
package org.jsampler.view; |
24 |
|
|
25 |
import java.awt.Dialog; |
import java.awt.Dialog; |
|
import java.awt.Dimension; |
|
26 |
import java.awt.Frame; |
import java.awt.Frame; |
27 |
|
|
28 |
|
import java.awt.event.ActionEvent; |
29 |
|
import java.awt.event.KeyEvent; |
30 |
import java.awt.event.WindowAdapter; |
import java.awt.event.WindowAdapter; |
31 |
import java.awt.event.WindowEvent; |
import java.awt.event.WindowEvent; |
32 |
|
|
33 |
import java.util.Vector; |
import java.util.Vector; |
34 |
import java.util.logging.Level; |
import java.util.logging.Level; |
35 |
|
|
36 |
|
import javax.swing.AbstractAction; |
37 |
|
import javax.swing.JComponent; |
38 |
import javax.swing.JFrame; |
import javax.swing.JFrame; |
39 |
|
import javax.swing.KeyStroke; |
40 |
|
|
41 |
|
import javax.swing.event.ListSelectionEvent; |
42 |
|
import javax.swing.event.ListSelectionListener; |
43 |
import org.jsampler.CC; |
import org.jsampler.CC; |
44 |
import org.jsampler.JSampler; |
import org.jsampler.JSampler; |
45 |
import org.jsampler.Prefs; |
import org.jsampler.SamplerChannelModel; |
46 |
import org.jsampler.Server; |
import org.jsampler.Server; |
47 |
|
|
48 |
import org.jsampler.event.SamplerChannelListEvent; |
import org.jsampler.event.SamplerChannelListEvent; |
55 |
*/ |
*/ |
56 |
public abstract class JSMainFrame extends JFrame { |
public abstract class JSMainFrame extends JFrame { |
57 |
private final Vector<JSChannelsPane> chnPaneList = new Vector<JSChannelsPane>(); |
private final Vector<JSChannelsPane> chnPaneList = new Vector<JSChannelsPane>(); |
58 |
|
private boolean autoUpdateChannelListUI = true; |
59 |
|
|
60 |
/** Creates a new instance of <code>JSMainFrame</code>. */ |
/** Creates a new instance of <code>JSMainFrame</code>. */ |
61 |
public |
public |
69 |
}); |
}); |
70 |
|
|
71 |
CC.getSamplerModel().addSamplerChannelListListener(new EventHandler()); |
CC.getSamplerModel().addSamplerChannelListListener(new EventHandler()); |
72 |
|
|
73 |
|
getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put ( |
74 |
|
KeyStroke.getKeyStroke(KeyEvent.VK_G, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK), |
75 |
|
"RunGarbageCollector" |
76 |
|
); |
77 |
|
|
78 |
|
getRootPane().getActionMap().put ("RunGarbageCollector", new AbstractAction() { |
79 |
|
public void |
80 |
|
actionPerformed(ActionEvent e) { |
81 |
|
System.gc(); |
82 |
|
} |
83 |
|
}); |
84 |
} |
} |
85 |
|
|
86 |
/** |
/** |
114 |
*/ |
*/ |
115 |
public abstract void showDetailedErrorMessage(Dialog owner, String err, String details); |
public abstract void showDetailedErrorMessage(Dialog owner, String err, String details); |
116 |
|
|
117 |
|
protected Vector<ListSelectionListener> channelsPaneListeners = |
118 |
|
new Vector<ListSelectionListener>(); |
119 |
|
|
120 |
|
/** |
121 |
|
* Registers the specified listener for receiving event messages. |
122 |
|
* @param l The <code>ListSelectionListener</code> to register. |
123 |
|
*/ |
124 |
|
public void |
125 |
|
addChannelsPaneSelectionListener(ListSelectionListener l) { |
126 |
|
channelsPaneListeners.add(l); |
127 |
|
} |
128 |
|
|
129 |
|
/** |
130 |
|
* Removes the specified listener. |
131 |
|
* @param l The <code>ListSelectionListener</code> to remove. |
132 |
|
*/ |
133 |
|
public void |
134 |
|
removeChannelsPaneSelectionListener(ListSelectionListener l) { |
135 |
|
channelsPaneListeners.remove(l); |
136 |
|
} |
137 |
|
|
138 |
|
protected void |
139 |
|
fireChannelsPaneSelectionChanged() { |
140 |
|
int i = getChannelsPaneIndex(getSelectedChannelsPane()); |
141 |
|
ListSelectionEvent e = new ListSelectionEvent(this, i, i, false); |
142 |
|
for(ListSelectionListener l : channelsPaneListeners) l.valueChanged(e); |
143 |
|
} |
144 |
|
|
145 |
/** |
/** |
146 |
* Returns a list containing all <code>JSChannelsPane</code>s added to the view. |
* Returns a list containing all <code>JSChannelsPane</code>s added to the view. |
147 |
* @return A list containing all <code>JSChannelsPane</code>s added to the view. |
* @return A list containing all <code>JSChannelsPane</code>s added to the view. |
185 |
getChannelsPaneCount() { return chnPaneList.size(); } |
getChannelsPaneCount() { return chnPaneList.size(); } |
186 |
|
|
187 |
/** |
/** |
188 |
|
* Returns the index of the specified channels pane, or -1 if |
189 |
|
* the specified channels pane is not found. |
190 |
|
*/ |
191 |
|
public int |
192 |
|
getChannelsPaneIndex(JSChannelsPane chnPane) { |
193 |
|
return chnPaneList.indexOf(chnPane); |
194 |
|
} |
195 |
|
|
196 |
|
/** |
197 |
* Inserts the specified <code>JSChannelsPane</code> at the specified position |
* Inserts the specified <code>JSChannelsPane</code> at the specified position |
198 |
* in the view and in the code>JSChannelsPane</code> list. |
* in the view and in the code>JSChannelsPane</code> list. |
199 |
* Where and how this pane will be shown depends on the view/GUI implementation. |
* Where and how this pane will be shown depends on the view/GUI implementation. |
229 |
|
|
230 |
/** |
/** |
231 |
* Sets the <code>JSChannelsPane</code> to be selected. |
* Sets the <code>JSChannelsPane</code> to be selected. |
232 |
|
* Note that all registered listeners should be notified |
233 |
|
* when the selection is changed. |
234 |
* @param pane The <code>JSChannelsPane</code> to be shown. |
* @param pane The <code>JSChannelsPane</code> to be shown. |
235 |
|
* @see #fireChannelsPaneSelectionChanged |
236 |
*/ |
*/ |
237 |
public abstract void setSelectedChannelsPane(JSChannelsPane pane); |
public abstract void setSelectedChannelsPane(JSChannelsPane pane); |
238 |
|
|
242 |
* @param e A <code>SamplerChannelListEvent</code> |
* @param e A <code>SamplerChannelListEvent</code> |
243 |
* instance providing the event information. |
* instance providing the event information. |
244 |
*/ |
*/ |
245 |
|
@Override |
246 |
public void |
public void |
247 |
channelAdded(SamplerChannelListEvent e) { |
channelAdded(SamplerChannelListEvent e) { |
248 |
|
if(e.getChannelModel() == null) return; |
249 |
Integer id = e.getChannelModel().getChannelId(); |
Integer id = e.getChannelModel().getChannelId(); |
250 |
if(findChannel(id) != null) { |
if(findChannel(id) != null) { |
251 |
CC.getLogger().log(Level.WARNING, "JSMainFrame.channelExist!", id); |
CC.getLogger().log(Level.WARNING, "JSMainFrame.channelExist!", id); |
260 |
* @param e A <code>SamplerChannelListEvent</code> |
* @param e A <code>SamplerChannelListEvent</code> |
261 |
* instance providing the event information. |
* instance providing the event information. |
262 |
*/ |
*/ |
263 |
|
@Override |
264 |
public void |
public void |
265 |
channelRemoved(SamplerChannelListEvent e) { |
channelRemoved(SamplerChannelListEvent e) { |
266 |
removeChannel(e.getChannelModel().getChannelId()); |
removeChannel(e.getChannelModel().getChannelId()); |
304 |
|
|
305 |
return null; |
return null; |
306 |
} |
} |
307 |
|
|
308 |
|
/** |
309 |
|
* Gets the zero-based position of the specified sampler channel |
310 |
|
* in the channels pane, to which the channel is added. |
311 |
|
* Note that the position may change when adding/removing sampler channels. |
312 |
|
* @return The zero-based position of the specified sampler channel |
313 |
|
* in the channels pane, or -1 if the specified channels is not found. |
314 |
|
*/ |
315 |
|
public int |
316 |
|
getChannelNumber(SamplerChannelModel channel) { |
317 |
|
if(channel == null) return -1; |
318 |
|
|
319 |
|
for(int i = 0; i < getChannelsPaneCount(); i++) { |
320 |
|
JSChannelsPane chnPane = getChannelsPane(i); |
321 |
|
for(int j = 0; j < chnPane.getChannelCount(); j++) { |
322 |
|
if(chnPane.getChannel(j).getChannelId() == channel.getChannelId()) { |
323 |
|
return j; |
324 |
|
} |
325 |
|
} |
326 |
|
} |
327 |
|
|
328 |
|
return -1; |
329 |
|
} |
330 |
|
|
331 |
|
/** |
332 |
|
* Returns a string in the format <code>channelPaneNumber/channelNumber</code>, |
333 |
|
* where <code>channelPaneNumber</code> is the one-based number of the channels |
334 |
|
* pane containing the specified channel and <code>channelNumber</code> is the |
335 |
|
* one-based number of the channel's position in the channels pane. |
336 |
|
* Note that this path may change when adding/removing channels/channels panes. |
337 |
|
* @return The channels path, or <code>null</code> if the specified channels is not found. |
338 |
|
*/ |
339 |
|
public String |
340 |
|
getChannelPath(SamplerChannelModel channel) { |
341 |
|
if(channel == null) return null; |
342 |
|
|
343 |
|
for(int i = 0; i < getChannelsPaneCount(); i++) { |
344 |
|
JSChannelsPane chnPane = getChannelsPane(i); |
345 |
|
for(int j = 0; j < chnPane.getChannelCount(); j++) { |
346 |
|
if(chnPane.getChannel(j).getChannelId() == channel.getChannelId()) { |
347 |
|
return (i + 1) + "/" + (j + 1); |
348 |
|
} |
349 |
|
} |
350 |
|
} |
351 |
|
|
352 |
|
return null; |
353 |
|
} |
354 |
|
|
355 |
|
/** |
356 |
|
* Gets the zero-based number of the channels pane, |
357 |
|
* to which the specified sampler channel is added. |
358 |
|
* Note that the can be moved from one channels pane to another. |
359 |
|
* @return The zero-based index of the channels pane, |
360 |
|
* to which the specified sampler channel is added, or |
361 |
|
* -1 if the specified channels is not found. |
362 |
|
*/ |
363 |
|
public int |
364 |
|
getChannelsPaneNumber(SamplerChannelModel channel) { |
365 |
|
if(channel == null) return -1; |
366 |
|
|
367 |
|
for(int i = 0; i < getChannelsPaneCount(); i++) { |
368 |
|
JSChannelsPane chnPane = getChannelsPane(i); |
369 |
|
for(int j = 0; j < chnPane.getChannelCount(); j++) { |
370 |
|
if(chnPane.getChannel(j).getChannelId() == channel.getChannelId()) { |
371 |
|
return i; |
372 |
|
} |
373 |
|
} |
374 |
|
} |
375 |
|
|
376 |
|
return -1; |
377 |
|
} |
378 |
|
|
379 |
|
/** |
380 |
|
* Sends the specified script to the backend. |
381 |
|
* @param script The file name of the script to run. |
382 |
|
*/ |
383 |
|
public abstract void runScript(String script); |
384 |
|
|
385 |
|
/** |
386 |
|
* Determines whether the channel list UI should be automatically updated |
387 |
|
* when channel is added/removed. The default value is <code>true</code>. |
388 |
|
*/ |
389 |
|
public boolean |
390 |
|
getAutoUpdateChannelListUI() { return autoUpdateChannelListUI; } |
391 |
|
|
392 |
|
/** |
393 |
|
* Determines whether the channel list UI should be automatically updated |
394 |
|
* when channel is added/removed. |
395 |
|
*/ |
396 |
|
public void |
397 |
|
setAutoUpdateChannelListUI(boolean b) { |
398 |
|
if(b == autoUpdateChannelListUI) return; |
399 |
|
|
400 |
|
autoUpdateChannelListUI = b; |
401 |
|
for(JSChannelsPane cp : getChannelsPaneList()) { |
402 |
|
cp.setAutoUpdate(b); |
403 |
|
} |
404 |
|
} |
405 |
|
|
406 |
|
/** |
407 |
|
* Updates the channel list UI. |
408 |
|
*/ |
409 |
|
public void |
410 |
|
updateChannelListUI() { |
411 |
|
for(JSChannelsPane cp : getChannelsPaneList()) { |
412 |
|
cp.updateChannelListUI(); |
413 |
|
} |
414 |
|
} |
415 |
} |
} |