/[svn]/jsampler/trunk/src/org/jsampler/view/JSMainFrame.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/view/JSMainFrame.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1737 - (show annotations) (download)
Thu May 8 17:26:19 2008 UTC (15 years, 11 months ago) by iliev
File size: 9487 byte(s)
* Major memory optimizations when too many sampler channels are present

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2007 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.view;
24
25 import java.awt.Dialog;
26 import java.awt.Dimension;
27 import java.awt.Frame;
28
29 import java.awt.event.ActionEvent;
30 import java.awt.event.KeyEvent;
31 import java.awt.event.WindowAdapter;
32 import java.awt.event.WindowEvent;
33
34 import java.util.Vector;
35 import java.util.logging.Level;
36
37 import javax.swing.AbstractAction;
38 import javax.swing.JComponent;
39 import javax.swing.JFrame;
40 import javax.swing.KeyStroke;
41
42 import org.jsampler.CC;
43 import org.jsampler.JSampler;
44 import org.jsampler.Prefs;
45 import org.jsampler.Server;
46
47 import org.jsampler.event.SamplerChannelListEvent;
48 import org.jsampler.event.SamplerChannelListListener;
49
50
51 /**
52 * Defines the skeleton of a JSampler's main frame.
53 * @author Grigor Iliev
54 */
55 public abstract class JSMainFrame extends JFrame {
56 private final Vector<JSChannelsPane> chnPaneList = new Vector<JSChannelsPane>();
57 private boolean autoUpdateChannelListUI = true;
58
59 /** Creates a new instance of <code>JSMainFrame</code>. */
60 public
61 JSMainFrame() {
62 super(JSampler.NAME + ' ' + JSampler.VERSION);
63
64 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
65 addWindowListener(new WindowAdapter() {
66 public void
67 windowClosing(WindowEvent we) { onWindowClose(); }
68 });
69
70 CC.getSamplerModel().addSamplerChannelListListener(new EventHandler());
71
72 getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put (
73 KeyStroke.getKeyStroke(KeyEvent.VK_G, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK),
74 "RunGarbageCollector"
75 );
76
77 getRootPane().getActionMap().put ("RunGarbageCollector", new AbstractAction() {
78 public void
79 actionPerformed(ActionEvent e) {
80 System.gc();
81 }
82 });
83 }
84
85 /**
86 * Invoked when this window is about to close.
87 * Don't forget to call <code>super.onWindowClose()</code> at the end,
88 * when override this method.
89 */
90 protected void
91 onWindowClose() {
92 CC.cleanExit();
93 }
94
95 /**
96 * Invoked on startup when no JSampler home directory is specified
97 * or the specified JSampler home directory doesn't exist.
98 * This method should ask the user to specify a JSampler
99 * home directory and then set the specified JSampler home directory using
100 * {@link org.jsampler.CC#setJSamplerHome} method.
101 * @see org.jsampler.CC#getJSamplerHome
102 * @see org.jsampler.CC#setJSamplerHome
103 */
104 public abstract void installJSamplerHome();
105
106 /**
107 * Shows a detailed error information about the specified exception.
108 */
109 public abstract void showDetailedErrorMessage(Frame owner, String err, String details);
110
111 /**
112 * Shows a detailed error information about the specified exception.
113 */
114 public abstract void showDetailedErrorMessage(Dialog owner, String err, String details);
115
116 /**
117 * Returns a list containing all <code>JSChannelsPane</code>s added to the view.
118 * @return A list containing all <code>JSChannelsPane</code>s added to the view.
119 * @see #addChannelsPane
120 * @see #removeChannelsPane
121 */
122 public Vector<JSChannelsPane>
123 getChannelsPaneList() { return chnPaneList; }
124
125 /**
126 * Return the <code>JSChannelsPane</code> at the specified position.
127 * @param idx The position of the <code>JSChannelsPane</code> to be returned.
128 * @return The <code>JSChannelsPane</code> at the specified position.
129 */
130 public JSChannelsPane
131 getChannelsPane(int idx) { return chnPaneList.get(idx); }
132
133 /**
134 * Adds the specified <code>JSChannelsPane</code> to the view.
135 * @param chnPane The <code>JSChannelsPane</code> to be added.
136 */
137 public void
138 addChannelsPane(JSChannelsPane chnPane) { chnPaneList.add(chnPane); }
139
140 /**
141 * Removes the specified <code>JSChannelsPane</code> from the view.
142 * Override this method to remove <code>chnPane</code> from the view,
143 * and don't forget to call <code>super.removeChannelsPane(chnPane);</code>.
144 * @param chnPane The <code>JSChannelsPane</code> to be removed.
145 * @return <code>true</code> if the specified code>JSChannelsPane</code>
146 * is actually removed from the view, <code>false</code> otherwise.
147 */
148 public boolean
149 removeChannelsPane(JSChannelsPane chnPane) { return chnPaneList.remove(chnPane); }
150
151 /**
152 * Gets the current number of <code>JSChannelsPane</code>s added to the view.
153 * @return The current number of <code>JSChannelsPane</code>s added to the view.
154 */
155 public int
156 getChannelsPaneCount() { return chnPaneList.size(); }
157
158 /**
159 * Inserts the specified <code>JSChannelsPane</code> at the specified position
160 * in the view and in the code>JSChannelsPane</code> list.
161 * Where and how this pane will be shown depends on the view/GUI implementation.
162 * Note that some GUI implementation may have only one pane containing sampler channels.
163 * @param pane The <code>JSChannelsPane</code> to be inserted.
164 * @param idx Specifies the position of the <code>JSChannelsPane</code>.
165 * @see #getChannelsPaneList
166 */
167 public abstract void insertChannelsPane(JSChannelsPane pane, int idx);
168
169 /**
170 * Gets the <code>JSChannelsPane</code> that is currently shown,
171 * or has the focus if more than one channels' panes are shown.
172 * If the GUI implementation has only one pane containing sampler channels,
173 * than this method should always return that pane (the <code>JSChannelsPane</code>
174 * with index 0).
175 * @return The selected <code>JSChannelsPane</code>.
176 */
177 public abstract JSChannelsPane getSelectedChannelsPane();
178
179 /**
180 * Gets the server address to which to connect. If the server should be
181 * manually selected, a dialog asking the user to choose a server is displayed.
182 */
183 public abstract Server getServer();
184
185 /**
186 * Gets the server address to which to connect. If the server should be
187 * manually selected, a dialog asking the user to choose a server is displayed.
188 * @param manualSelect Determines whether the server should be manually selected.
189 */
190 public abstract Server getServer(boolean manualSelect);
191
192 /**
193 * Sets the <code>JSChannelsPane</code> to be selected.
194 * @param pane The <code>JSChannelsPane</code> to be shown.
195 */
196 public abstract void setSelectedChannelsPane(JSChannelsPane pane);
197
198 private class EventHandler implements SamplerChannelListListener {
199 /**
200 * Invoked when a new sampler channel is created.
201 * @param e A <code>SamplerChannelListEvent</code>
202 * instance providing the event information.
203 */
204 public void
205 channelAdded(SamplerChannelListEvent e) {
206 if(e.getChannelModel() == null) return;
207 Integer id = e.getChannelModel().getChannelId();
208 if(findChannel(id) != null) {
209 CC.getLogger().log(Level.WARNING, "JSMainFrame.channelExist!", id);
210 return;
211 }
212
213 getSelectedChannelsPane().addChannel(e.getChannelModel());
214 }
215
216 /**
217 * Invoked when a sampler channel is removed.
218 * @param e A <code>SamplerChannelListEvent</code>
219 * instance providing the event information.
220 */
221 public void
222 channelRemoved(SamplerChannelListEvent e) {
223 removeChannel(e.getChannelModel().getChannelId());
224 }
225 }
226
227 /**
228 * Searches for the first occurence of a channel with numerical ID <code>id</code>.
229 * @return The first occurence of a channel with numerical ID <code>id</code> or
230 * <code>null</code> if there is no channel with numerical ID <code>id</code>.
231 */
232 public JSChannel
233 findChannel(int id) {
234 if(id < 0) return null;
235
236 for(JSChannelsPane cp : getChannelsPaneList()) {
237 for(JSChannel c : cp.getChannels()) if(c.getChannelId() == id) return c;
238 }
239
240 return null;
241 }
242
243 /**
244 * Removes the first occurence of a channel with numerical ID <code>id</code>.
245 * This method is invoked when a sampler channel is removed in the back-end.
246 * @return The removed channel or <code>null</code>
247 * if there is no channel with numerical ID <code>id</code>.
248 */
249 public JSChannel
250 removeChannel(int id) {
251 if(id < 0) return null;
252
253 for(JSChannelsPane cp : getChannelsPaneList()) {
254 for(JSChannel c : cp.getChannels()) {
255 if(c.getChannelId() == id) {
256 cp.removeChannel(c);
257 return c;
258 }
259 }
260 }
261
262 return null;
263 }
264
265 /**
266 * Determines whether the channel list UI should be automatically updated
267 * when channel is added/removed. The default value is <code>true</code>.
268 */
269 public boolean
270 getAutoUpdateChannelListUI() { return autoUpdateChannelListUI; }
271
272 /**
273 * Determines whether the channel list UI should be automatically updated
274 * when channel is added/removed.
275 */
276 public void
277 setAutoUpdateChannelListUI(boolean b) {
278 if(b == autoUpdateChannelListUI) return;
279
280 autoUpdateChannelListUI = b;
281 for(JSChannelsPane cp : getChannelsPaneList()) {
282 cp.setAutoUpdate(b);
283 }
284 }
285
286 /**
287 * Updates the channel list UI.
288 */
289 public void
290 updateChannelListUI() {
291 for(JSChannelsPane cp : getChannelsPaneList()) {
292 cp.updateChannelListUI();
293 }
294 }
295 }

  ViewVC Help
Powered by ViewVC