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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1688 by iliev, Thu Feb 14 16:52:36 2008 UTC revision 1864 by iliev, Sat Mar 14 20:44:58 2009 UTC
# Line 1  Line 1 
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-2009 Grigor Iliev <grigor@grigoriliev.com>
5   *   *
6   *   This file is part of JSampler.   *   This file is part of JSampler.
7   *   *
# Line 23  Line 23 
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    
44  import org.jsampler.CC;  import org.jsampler.CC;
45  import org.jsampler.JSampler;  import org.jsampler.JSampler;
46  import org.jsampler.Prefs;  import org.jsampler.SamplerChannelModel;
47  import org.jsampler.Server;  import org.jsampler.Server;
48    
49  import org.jsampler.event.SamplerChannelListEvent;  import org.jsampler.event.SamplerChannelListEvent;
50  import org.jsampler.event.SamplerChannelListListener;  import org.jsampler.event.SamplerChannelListListener;
51    
52    import org.jsampler.view.SessionViewConfig.ChannelConfig;
53    
54    
55  /**  /**
56   * Defines the skeleton of a JSampler's main frame.   * Defines the skeleton of a JSampler's main frame.
# Line 49  import org.jsampler.event.SamplerChannel Line 58  import org.jsampler.event.SamplerChannel
58   */   */
59  public abstract class JSMainFrame extends JFrame {  public abstract class JSMainFrame extends JFrame {
60          private final Vector<JSChannelsPane> chnPaneList = new Vector<JSChannelsPane>();          private final Vector<JSChannelsPane> chnPaneList = new Vector<JSChannelsPane>();
61            private boolean autoUpdateChannelListUI = true;
62                    
63          /** Creates a new instance of <code>JSMainFrame</code>. */          /** Creates a new instance of <code>JSMainFrame</code>. */
64          public          public
# Line 62  public abstract class JSMainFrame extend Line 72  public abstract class JSMainFrame extend
72                  });                  });
73                                    
74                  CC.getSamplerModel().addSamplerChannelListListener(new EventHandler());                  CC.getSamplerModel().addSamplerChannelListListener(new EventHandler());
75                    
76                    getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put (
77                            KeyStroke.getKeyStroke(KeyEvent.VK_G, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK),
78                            "RunGarbageCollector"
79                    );
80                    
81                    getRootPane().getActionMap().put ("RunGarbageCollector", new AbstractAction() {
82                            public void
83                            actionPerformed(ActionEvent e) {
84                                    System.gc();
85                            }
86                    });
87          }          }
88                    
89          /**          /**
# Line 69  public abstract class JSMainFrame extend Line 91  public abstract class JSMainFrame extend
91           * Don't forget to call <code>super.onWindowClose()</code> at the end,           * Don't forget to call <code>super.onWindowClose()</code> at the end,
92           * when override this method.           * when override this method.
93           */           */
94          protected void          public void
95          onWindowClose() {          onWindowClose() {
96                  CC.cleanExit();                  CC.cleanExit();
97          }          }
# Line 95  public abstract class JSMainFrame extend Line 117  public abstract class JSMainFrame extend
117           */           */
118          public abstract void showDetailedErrorMessage(Dialog owner, String err, String details);          public abstract void showDetailedErrorMessage(Dialog owner, String err, String details);
119                    
120            protected Vector<ListSelectionListener> channelsPaneListeners =
121                    new Vector<ListSelectionListener>();
122            
123            /**
124             * Registers the specified listener for receiving event messages.
125             * @param l The <code>ListSelectionListener</code> to register.
126             */
127            public void
128            addChannelsPaneSelectionListener(ListSelectionListener l) {
129                    channelsPaneListeners.add(l);
130            }
131            
132            /**
133             * Removes the specified listener.
134             * @param l The <code>ListSelectionListener</code> to remove.
135             */
136            public void
137            removeChannelsPaneSelectionListener(ListSelectionListener l) {
138                    channelsPaneListeners.remove(l);
139            }
140            
141            protected void
142            fireChannelsPaneSelectionChanged() {
143                    int i = getChannelsPaneIndex(getSelectedChannelsPane());
144                    ListSelectionEvent e = new ListSelectionEvent(this, i, i, false);
145                    for(ListSelectionListener l : channelsPaneListeners) l.valueChanged(e);
146            }
147            
148          /**          /**
149           * 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.
150           * @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.
# Line 138  public abstract class JSMainFrame extend Line 188  public abstract class JSMainFrame extend
188          getChannelsPaneCount() { return chnPaneList.size(); }          getChannelsPaneCount() { return chnPaneList.size(); }
189                    
190          /**          /**
191             * Returns the index of the specified channels pane, or -1 if
192             * the specified channels pane is not found.
193             */
194            public int
195            getChannelsPaneIndex(JSChannelsPane chnPane) {
196                    return chnPaneList.indexOf(chnPane);
197            }
198            
199            /**
200           * Inserts the specified <code>JSChannelsPane</code> at the specified position           * Inserts the specified <code>JSChannelsPane</code> at the specified position
201           * in the view and in the code>JSChannelsPane</code> list.           * in the view and in the code>JSChannelsPane</code> list.
202           * 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.
# Line 173  public abstract class JSMainFrame extend Line 232  public abstract class JSMainFrame extend
232                    
233          /**          /**
234           * Sets the <code>JSChannelsPane</code> to be selected.           * Sets the <code>JSChannelsPane</code> to be selected.
235             * Note that all registered listeners should be notified
236             * when the selection is changed.
237           * @param pane The <code>JSChannelsPane</code> to be shown.           * @param pane The <code>JSChannelsPane</code> to be shown.
238             * @see #fireChannelsPaneSelectionChanged
239           */           */
240          public abstract void setSelectedChannelsPane(JSChannelsPane pane);          public abstract void setSelectedChannelsPane(JSChannelsPane pane);
241                    
# Line 183  public abstract class JSMainFrame extend Line 245  public abstract class JSMainFrame extend
245                   * @param e A <code>SamplerChannelListEvent</code>                   * @param e A <code>SamplerChannelListEvent</code>
246                   * instance providing the event information.                   * instance providing the event information.
247                   */                   */
248                    @Override
249                  public void                  public void
250                  channelAdded(SamplerChannelListEvent e) {                  channelAdded(SamplerChannelListEvent e) {
251                            if(e.getChannelModel() == null) return;
252                          Integer id = e.getChannelModel().getChannelId();                          Integer id = e.getChannelModel().getChannelId();
253                          if(findChannel(id) != null) {                          if(findChannel(id) != null) {
254                                  CC.getLogger().log(Level.WARNING, "JSMainFrame.channelExist!", id);                                  CC.getLogger().log(Level.WARNING, "JSMainFrame.channelExist!", id);
255                                  return;                                  return;
256                          }                          }
257                                                    
258                          getSelectedChannelsPane().addChannel(e.getChannelModel());                          ChannelConfig config = null;
259                            JSViewConfig viewConfig = CC.getViewConfig();
260                            if(viewConfig != null && viewConfig.getSessionViewConfig() != null) {
261                                    config = viewConfig.getSessionViewConfig().pollChannelConfig();
262                            }
263                            
264                            if(config == null) {
265                                    getSelectedChannelsPane().addChannel(e.getChannelModel());
266                            } else {
267                                    int i = config.channelsPanel;
268                                    if(i >= 0 && i < getChannelsPaneCount()) {
269                                            getChannelsPane(i).addChannel(e.getChannelModel(), config);
270                                    } else {
271                                            getSelectedChannelsPane().addChannel(e.getChannelModel(), config);
272                                    }
273                            }
274                  }                  }
275                    
276                  /**                  /**
# Line 199  public abstract class JSMainFrame extend Line 278  public abstract class JSMainFrame extend
278                   * @param e A <code>SamplerChannelListEvent</code>                   * @param e A <code>SamplerChannelListEvent</code>
279                   * instance providing the event information.                   * instance providing the event information.
280                   */                   */
281                    @Override
282                  public void                  public void
283                  channelRemoved(SamplerChannelListEvent e) {                  channelRemoved(SamplerChannelListEvent e) {
284                          removeChannel(e.getChannelModel().getChannelId());                          removeChannel(e.getChannelModel().getChannelId());
# Line 242  public abstract class JSMainFrame extend Line 322  public abstract class JSMainFrame extend
322                                    
323                  return null;                  return null;
324          }          }
325            
326            /**
327             * Gets the zero-based position of the specified sampler channel
328             * in the channels pane, to which the channel is added.
329             * Note that the position may change when adding/removing sampler channels.
330             * @return The zero-based position of the specified sampler channel
331             * in the channels pane, or -1 if the specified channels is not found.
332             */
333            public int
334            getChannelNumber(SamplerChannelModel channel) {
335                    if(channel == null) return -1;
336                    
337                    for(int i = 0; i < getChannelsPaneCount(); i++) {
338                            JSChannelsPane chnPane = getChannelsPane(i);
339                            for(int j = 0; j < chnPane.getChannelCount(); j++) {
340                                    if(chnPane.getChannel(j).getChannelId() == channel.getChannelId()) {
341                                            return j;
342                                    }
343                            }
344                    }
345                    
346                    return -1;
347            }
348            
349            /**
350             * Returns a string in the format <code>channelPaneNumber/channelNumber</code>,
351             * where <code>channelPaneNumber</code> is the one-based number of the channels
352             * pane containing the specified channel and <code>channelNumber</code> is the
353             * one-based number of the channel's position in the channels pane.
354             * Note that this path may change when adding/removing channels/channels panes.
355             * @return The channels path, or <code>null</code> if the specified channels is not found.
356             */
357            public String
358            getChannelPath(SamplerChannelModel channel) {
359                    if(channel == null) return null;
360                    
361                    for(int i = 0; i < getChannelsPaneCount(); i++) {
362                            JSChannelsPane chnPane = getChannelsPane(i);
363                            for(int j = 0; j < chnPane.getChannelCount(); j++) {
364                                    if(chnPane.getChannel(j).getChannelId() == channel.getChannelId()) {
365                                            return (i + 1) + "." + (j + 1);
366                                    }
367                            }
368                    }
369                    
370                    return null;
371            }
372            
373            /**
374             * Gets the zero-based number of the channels pane,
375             * to which the specified sampler channel is added.
376             * Note that the can be moved from one channels pane to another.
377             * @return The zero-based index of the channels pane,
378             * to which the specified sampler channel is added, or
379             * -1 if the specified channels is not found.
380             */
381            public int
382            getChannelsPaneNumber(SamplerChannelModel channel) {
383                    if(channel == null) return -1;
384                    
385                    for(int i = 0; i < getChannelsPaneCount(); i++) {
386                            JSChannelsPane chnPane = getChannelsPane(i);
387                            for(int j = 0; j < chnPane.getChannelCount(); j++) {
388                                    if(chnPane.getChannel(j).getChannelId() == channel.getChannelId()) {
389                                            return i;
390                                    }
391                            }
392                    }
393                    
394                    return -1;
395            }
396            
397            /**
398             * Sends the specified script to the backend.
399             * @param script The file name of the script to run.
400             */
401            public abstract void runScript(String script);
402            
403            /**
404             * Determines whether the channel list UI should be automatically updated
405             * when channel is added/removed. The default value is <code>true</code>.
406             */
407            public boolean
408            getAutoUpdateChannelListUI() { return autoUpdateChannelListUI; }
409            
410            /**
411             * Determines whether the channel list UI should be automatically updated
412             * when channel is added/removed.
413             */
414            public void
415            setAutoUpdateChannelListUI(boolean b) {
416                    if(b == autoUpdateChannelListUI) return;
417                    
418                    autoUpdateChannelListUI = b;
419                    for(JSChannelsPane cp : getChannelsPaneList()) {
420                            cp.setAutoUpdate(b);
421                    }
422            }
423            
424            /**
425             * Updates the channel list UI.
426             */
427            public void
428            updateChannelListUI() {
429                    for(JSChannelsPane cp : getChannelsPaneList()) {
430                            cp.updateChannelListUI();
431                    }
432            }
433  }  }

Legend:
Removed from v.1688  
changed lines
  Added in v.1864

  ViewVC Help
Powered by ViewVC