1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005 Grigor Kirilov Iliev |
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.view; |
24 |
|
25 |
import java.awt.event.WindowAdapter; |
26 |
import java.awt.event.WindowEvent; |
27 |
|
28 |
import java.util.Vector; |
29 |
import java.util.logging.Level; |
30 |
|
31 |
import javax.swing.JFrame; |
32 |
|
33 |
import org.jsampler.CC; |
34 |
import org.jsampler.JSampler; |
35 |
|
36 |
import org.jsampler.event.SamplerChannelListEvent; |
37 |
import org.jsampler.event.SamplerChannelListListener; |
38 |
|
39 |
/** |
40 |
* |
41 |
* @author Grigor Iliev |
42 |
*/ |
43 |
public abstract class JSMainFrame extends JFrame { |
44 |
private final Vector<JSChannelsPane> chnPaneList = new Vector<JSChannelsPane>(); |
45 |
|
46 |
public |
47 |
JSMainFrame() { |
48 |
super(JSampler.NAME + ' ' + JSampler.VERSION); |
49 |
|
50 |
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); |
51 |
addWindowListener(new WindowAdapter() { |
52 |
public void |
53 |
windowClosing(WindowEvent we) { CC.cleanExit(); } |
54 |
}); |
55 |
|
56 |
CC.getSamplerModel().addSamplerChannelListListener(new EventHandler()); |
57 |
} |
58 |
|
59 |
public Vector<JSChannelsPane> |
60 |
getChannelsPaneList() { return chnPaneList; } |
61 |
|
62 |
public JSChannelsPane |
63 |
getChannelsPane(int idx) { return chnPaneList.get(idx); } |
64 |
|
65 |
public void |
66 |
addChannelsPane(JSChannelsPane chnPane) { chnPaneList.add(chnPane); } |
67 |
|
68 |
public boolean |
69 |
removeChannelsPane(JSChannelsPane chnPane) { return chnPaneList.remove(chnPane); } |
70 |
|
71 |
public int |
72 |
getChannelsPaneCount() { return chnPaneList.size(); } |
73 |
|
74 |
public abstract void insertChannelsPane(JSChannelsPane pane, int idx); |
75 |
public abstract JSChannelsPane getSelectedChannelsPane(); |
76 |
public abstract void setSelectedChannelsPane(JSChannelsPane pane); |
77 |
|
78 |
private class EventHandler implements SamplerChannelListListener { |
79 |
/** |
80 |
* Invoked when a new sampler channel is created. |
81 |
* @param e A <code>SamplerChannelListEvent</code> |
82 |
* instance providing the event information. |
83 |
*/ |
84 |
public void |
85 |
channelAdded(SamplerChannelListEvent e) { |
86 |
Integer id = e.getChannelModel().getChannelID(); |
87 |
if(findChannel(id) != null) { |
88 |
CC.getLogger().log(Level.WARNING, "JSMainFrame.channelExist!", id); |
89 |
return; |
90 |
} |
91 |
|
92 |
getSelectedChannelsPane().addChannel(e.getChannelModel()); |
93 |
} |
94 |
|
95 |
/** |
96 |
* Invoked when a sampler channel is removed. |
97 |
* @param e A <code>SamplerChannelListEvent</code> |
98 |
* instance providing the event information. |
99 |
*/ |
100 |
public void |
101 |
channelRemoved(SamplerChannelListEvent e) { |
102 |
removeChannel(e.getChannelModel().getChannelID()); |
103 |
} |
104 |
} |
105 |
|
106 |
/** |
107 |
* Searches for the first occurence of a channel with numerical ID <code>id</code>. |
108 |
* @return The first occurence of a channel with numerical ID <code>id</code> or |
109 |
* <code>null</code> if there is no channel with numerical ID <code>id</code>. |
110 |
*/ |
111 |
public JSChannel |
112 |
findChannel(int id) { |
113 |
if(id < 0) return null; |
114 |
|
115 |
for(JSChannelsPane cp : getChannelsPaneList()) { |
116 |
for(JSChannel c : cp.getChannels()) if(c.getChannelID() == id) return c; |
117 |
} |
118 |
|
119 |
return null; |
120 |
} |
121 |
|
122 |
/** |
123 |
* Removes the first occurence of a channel with numerical ID <code>id</code>. |
124 |
* This method is invoked when a sampler channel is removed in the back-end. |
125 |
* @return The removed channel or <code>null</code> |
126 |
* if there is no channel with numerical ID <code>id</code>. |
127 |
*/ |
128 |
public JSChannel |
129 |
removeChannel(int id) { |
130 |
if(id < 0) return null; |
131 |
|
132 |
for(JSChannelsPane cp : getChannelsPaneList()) { |
133 |
for(JSChannel c : cp.getChannels()) { |
134 |
if(c.getChannelID() == id) { |
135 |
cp.removeChannel(c); |
136 |
return c; |
137 |
} |
138 |
} |
139 |
} |
140 |
|
141 |
return null; |
142 |
} |
143 |
} |