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.Dimension; |
26 |
|
27 |
import java.awt.event.WindowAdapter; |
28 |
import java.awt.event.WindowEvent; |
29 |
|
30 |
import java.util.Vector; |
31 |
import java.util.logging.Level; |
32 |
|
33 |
import javax.swing.JFrame; |
34 |
|
35 |
import org.jsampler.CC; |
36 |
import org.jsampler.JSampler; |
37 |
import org.jsampler.Prefs; |
38 |
|
39 |
import org.jsampler.event.SamplerChannelListEvent; |
40 |
import org.jsampler.event.SamplerChannelListListener; |
41 |
|
42 |
/** |
43 |
* Defines the skeleton of a JSampler's main frame. |
44 |
* @author Grigor Iliev |
45 |
*/ |
46 |
public abstract class JSMainFrame extends JFrame { |
47 |
private final Vector<JSChannelsPane> chnPaneList = new Vector<JSChannelsPane>(); |
48 |
|
49 |
/** Creates a new instance of <code>JSMainFrame</code>. */ |
50 |
public |
51 |
JSMainFrame() { |
52 |
super(JSampler.NAME + ' ' + JSampler.VERSION); |
53 |
|
54 |
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); |
55 |
addWindowListener(new WindowAdapter() { |
56 |
public void |
57 |
windowClosing(WindowEvent we) { onWindowClose(); } |
58 |
}); |
59 |
|
60 |
CC.getSamplerModel().addSamplerChannelListListener(new EventHandler()); |
61 |
} |
62 |
|
63 |
/** |
64 |
* Invoked when this window is about to close. |
65 |
* Don't forget to call <code>super.onWindowClose()</code> at the end, |
66 |
* when override this method. |
67 |
*/ |
68 |
protected void |
69 |
onWindowClose() { |
70 |
CC.cleanExit(); |
71 |
} |
72 |
|
73 |
/** |
74 |
* Returns a list containing all <code>JSChannelsPane</code>s added to the view. |
75 |
* @return A list containing all <code>JSChannelsPane</code>s added to the view. |
76 |
* @see #addChannelsPane |
77 |
* @see #removeChannelsPane |
78 |
*/ |
79 |
public Vector<JSChannelsPane> |
80 |
getChannelsPaneList() { return chnPaneList; } |
81 |
|
82 |
/** |
83 |
* Return the <code>JSChannelsPane</code> at the specified position. |
84 |
* @param idx The position of the <code>JSChannelsPane</code> to be returned. |
85 |
* @return The <code>JSChannelsPane</code> at the specified position. |
86 |
*/ |
87 |
public JSChannelsPane |
88 |
getChannelsPane(int idx) { return chnPaneList.get(idx); } |
89 |
|
90 |
/** |
91 |
* Adds the specified <code>JSChannelsPane</code> to the view. |
92 |
* @param chnPane The <code>JSChannelsPane</code> to be added. |
93 |
*/ |
94 |
public void |
95 |
addChannelsPane(JSChannelsPane chnPane) { chnPaneList.add(chnPane); } |
96 |
|
97 |
/** |
98 |
* Removes the specified <code>JSChannelsPane</code> from the view. |
99 |
* Override this method to remove <code>chnPane</code> from the view, |
100 |
* and don't forget to call <code>super.removeChannelsPane(chnPane);</code>. |
101 |
* @param chnPane The <code>JSChannelsPane</code> to be removed. |
102 |
* @return <code>true</code> if the specified code>JSChannelsPane</code> |
103 |
* is actually removed from the view, <code>false</code> otherwise. |
104 |
*/ |
105 |
public boolean |
106 |
removeChannelsPane(JSChannelsPane chnPane) { return chnPaneList.remove(chnPane); } |
107 |
|
108 |
/** |
109 |
* Gets the current number of <code>JSChannelsPane</code>s added to the view. |
110 |
* @return The current number of <code>JSChannelsPane</code>s added to the view. |
111 |
*/ |
112 |
public int |
113 |
getChannelsPaneCount() { return chnPaneList.size(); } |
114 |
|
115 |
/** |
116 |
* Inserts the specified <code>JSChannelsPane</code> at the specified position |
117 |
* in the view and in the code>JSChannelsPane</code> list. |
118 |
* Where and how this pane will be shown depends on the view/GUI implementation. |
119 |
* Note that some GUI implementation may have only one pane containing sampler channels. |
120 |
* @param pane The <code>JSChannelsPane</code> to be inserted. |
121 |
* @param idx Specifies the position of the <code>JSChannelsPane</code>. |
122 |
* @see #getChannelsPaneList |
123 |
*/ |
124 |
public abstract void insertChannelsPane(JSChannelsPane pane, int idx); |
125 |
|
126 |
/** |
127 |
* Gets the <code>JSChannelsPane</code> that is currently shown, |
128 |
* or has the focus if more than one channels' panes are shown. |
129 |
* If the GUI implementation has only one pane containing sampler channels, |
130 |
* than this method should always return that pane (the <code>JSChannelsPane</code> |
131 |
* with index 0). |
132 |
* @return The selected <code>JSChannelsPane</code>. |
133 |
*/ |
134 |
public abstract JSChannelsPane getSelectedChannelsPane(); |
135 |
|
136 |
/** |
137 |
* Sets the <code>JSChannelsPane</code> to be selected. |
138 |
* @param pane The <code>JSChannelsPane</code> to be shown. |
139 |
*/ |
140 |
public abstract void setSelectedChannelsPane(JSChannelsPane pane); |
141 |
|
142 |
private class EventHandler implements SamplerChannelListListener { |
143 |
/** |
144 |
* Invoked when a new sampler channel is created. |
145 |
* @param e A <code>SamplerChannelListEvent</code> |
146 |
* instance providing the event information. |
147 |
*/ |
148 |
public void |
149 |
channelAdded(SamplerChannelListEvent e) { |
150 |
Integer id = e.getChannelModel().getChannelID(); |
151 |
if(findChannel(id) != null) { |
152 |
CC.getLogger().log(Level.WARNING, "JSMainFrame.channelExist!", id); |
153 |
return; |
154 |
} |
155 |
|
156 |
getSelectedChannelsPane().addChannel(e.getChannelModel()); |
157 |
} |
158 |
|
159 |
/** |
160 |
* Invoked when a sampler channel is removed. |
161 |
* @param e A <code>SamplerChannelListEvent</code> |
162 |
* instance providing the event information. |
163 |
*/ |
164 |
public void |
165 |
channelRemoved(SamplerChannelListEvent e) { |
166 |
removeChannel(e.getChannelModel().getChannelID()); |
167 |
} |
168 |
} |
169 |
|
170 |
/** |
171 |
* Searches for the first occurence of a channel with numerical ID <code>id</code>. |
172 |
* @return The first occurence of a channel with numerical ID <code>id</code> or |
173 |
* <code>null</code> if there is no channel with numerical ID <code>id</code>. |
174 |
*/ |
175 |
public JSChannel |
176 |
findChannel(int id) { |
177 |
if(id < 0) return null; |
178 |
|
179 |
for(JSChannelsPane cp : getChannelsPaneList()) { |
180 |
for(JSChannel c : cp.getChannels()) if(c.getChannelID() == id) return c; |
181 |
} |
182 |
|
183 |
return null; |
184 |
} |
185 |
|
186 |
/** |
187 |
* Removes the first occurence of a channel with numerical ID <code>id</code>. |
188 |
* This method is invoked when a sampler channel is removed in the back-end. |
189 |
* @return The removed channel or <code>null</code> |
190 |
* if there is no channel with numerical ID <code>id</code>. |
191 |
*/ |
192 |
public JSChannel |
193 |
removeChannel(int id) { |
194 |
if(id < 0) return null; |
195 |
|
196 |
for(JSChannelsPane cp : getChannelsPaneList()) { |
197 |
for(JSChannel c : cp.getChannels()) { |
198 |
if(c.getChannelID() == id) { |
199 |
cp.removeChannel(c); |
200 |
return c; |
201 |
} |
202 |
} |
203 |
} |
204 |
|
205 |
return null; |
206 |
} |
207 |
} |