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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 iliev 787 /*
2     * JSampler - a java front-end for LinuxSampler
3     *
4 iliev 1285 * Copyright (C) 2005-2007 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.view;
24    
25 iliev 1204 import java.awt.Dialog;
26 iliev 842 import java.awt.Dimension;
27 iliev 1204 import java.awt.Frame;
28 iliev 842
29 iliev 1734 import java.awt.event.ActionEvent;
30     import java.awt.event.KeyEvent;
31 iliev 787 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 iliev 1734 import javax.swing.AbstractAction;
38     import javax.swing.JComponent;
39 iliev 787 import javax.swing.JFrame;
40 iliev 1734 import javax.swing.KeyStroke;
41 iliev 787
42     import org.jsampler.CC;
43     import org.jsampler.JSampler;
44 iliev 842 import org.jsampler.Prefs;
45 iliev 1688 import org.jsampler.Server;
46 iliev 787
47     import org.jsampler.event.SamplerChannelListEvent;
48     import org.jsampler.event.SamplerChannelListListener;
49    
50 iliev 1204
51 iliev 787 /**
52 iliev 911 * Defines the skeleton of a JSampler's main frame.
53 iliev 787 * @author Grigor Iliev
54     */
55     public abstract class JSMainFrame extends JFrame {
56     private final Vector<JSChannelsPane> chnPaneList = new Vector<JSChannelsPane>();
57 iliev 1734 private boolean autoUpdateChannelListUI = true;
58 iliev 787
59 iliev 911 /** Creates a new instance of <code>JSMainFrame</code>. */
60 iliev 787 public
61     JSMainFrame() {
62     super(JSampler.NAME + ' ' + JSampler.VERSION);
63    
64     setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
65     addWindowListener(new WindowAdapter() {
66     public void
67 iliev 842 windowClosing(WindowEvent we) { onWindowClose(); }
68 iliev 787 });
69    
70     CC.getSamplerModel().addSamplerChannelListListener(new EventHandler());
71 iliev 1734
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 iliev 787 }
84    
85 iliev 911 /**
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 iliev 842 onWindowClose() {
92     CC.cleanExit();
93     }
94    
95 iliev 911 /**
96 iliev 1143 * 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 iliev 1204 * 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 iliev 911 * 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 iliev 787 public Vector<JSChannelsPane>
123     getChannelsPaneList() { return chnPaneList; }
124    
125 iliev 911 /**
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 iliev 787 public JSChannelsPane
131     getChannelsPane(int idx) { return chnPaneList.get(idx); }
132    
133 iliev 911 /**
134     * Adds the specified <code>JSChannelsPane</code> to the view.
135     * @param chnPane The <code>JSChannelsPane</code> to be added.
136     */
137 iliev 787 public void
138     addChannelsPane(JSChannelsPane chnPane) { chnPaneList.add(chnPane); }
139    
140 iliev 911 /**
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 iliev 787 public boolean
149     removeChannelsPane(JSChannelsPane chnPane) { return chnPaneList.remove(chnPane); }
150    
151 iliev 911 /**
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 iliev 787 public int
156     getChannelsPaneCount() { return chnPaneList.size(); }
157    
158 iliev 911 /**
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 iliev 787 public abstract void insertChannelsPane(JSChannelsPane pane, int idx);
168 iliev 911
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 iliev 787 public abstract JSChannelsPane getSelectedChannelsPane();
178 iliev 911
179     /**
180 iliev 1688 * 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 iliev 911 * Sets the <code>JSChannelsPane</code> to be selected.
194     * @param pane The <code>JSChannelsPane</code> to be shown.
195     */
196 iliev 787 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 iliev 1737 if(e.getChannelModel() == null) return;
207 iliev 1143 Integer id = e.getChannelModel().getChannelId();
208 iliev 787 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 iliev 1143 removeChannel(e.getChannelModel().getChannelId());
224 iliev 787 }
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 iliev 1143 for(JSChannel c : cp.getChannels()) if(c.getChannelId() == id) return c;
238 iliev 787 }
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 iliev 1143 if(c.getChannelId() == id) {
256 iliev 787 cp.removeChannel(c);
257     return c;
258     }
259     }
260     }
261    
262     return null;
263     }
264 iliev 1734
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 iliev 787 }

  ViewVC Help
Powered by ViewVC