--- jsampler/trunk/src/org/jsampler/view/JSMainFrame.java 2011/11/23 18:58:46 2287 +++ jsampler/trunk/src/org/jsampler/view/JSMainFrame.java 2011/11/23 21:19:44 2288 @@ -1,7 +1,7 @@ /* * JSampler - a java front-end for LinuxSampler * - * Copyright (C) 2005-2009 Grigor Iliev + * Copyright (C) 2005-2011 Grigor Iliev * * This file is part of JSampler. * @@ -22,80 +22,26 @@ package org.jsampler.view; -import java.awt.Dialog; -import java.awt.Frame; - -import java.awt.event.ActionEvent; -import java.awt.event.KeyEvent; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; - import java.util.Vector; -import java.util.logging.Level; - -import javax.swing.AbstractAction; -import javax.swing.JComponent; -import javax.swing.JFrame; -import javax.swing.KeyStroke; - -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; import org.jsampler.CC; -import org.jsampler.JSampler; import org.jsampler.LSConsoleModel; import org.jsampler.SamplerChannelModel; import org.jsampler.Server; - -import org.jsampler.event.SamplerChannelListEvent; -import org.jsampler.event.SamplerChannelListListener; - -import org.jsampler.view.SessionViewConfig.ChannelConfig; +import org.jsampler.event.ListSelectionListener; /** * Defines the skeleton of a JSampler's main frame. * @author Grigor Iliev */ -public abstract class JSMainFrame extends JFrame { - private final Vector chnPaneList = new Vector(); - private boolean autoUpdateChannelListUI = true; - - /** Creates a new instance of JSMainFrame. */ - public - JSMainFrame() { - super(JSampler.NAME + ' ' + JSampler.VERSION); - - setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); - addWindowListener(new WindowAdapter() { - public void - windowClosing(WindowEvent we) { onWindowClose(); } - }); - - CC.getSamplerModel().addSamplerChannelListListener(new EventHandler()); - - getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put ( - KeyStroke.getKeyStroke(KeyEvent.VK_G, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK), - "RunGarbageCollector" - ); - - getRootPane().getActionMap().put ("RunGarbageCollector", new AbstractAction() { - public void - actionPerformed(ActionEvent e) { - System.gc(); - } - }); - } - +public interface JSMainFrame { /** * Invoked when this window is about to close. * Don't forget to call super.onWindowClose() at the end, * when override this method. */ - public void - onWindowClose() { - CC.cleanExit(); - } + public void onWindowClose(); /** * Invoked on startup when no JSampler home directory is specified @@ -106,217 +52,144 @@ * @see org.jsampler.CC#getJSamplerHome * @see org.jsampler.CC#setJSamplerHome */ - public abstract void installJSamplerHome(); - - /** - * Shows a detailed error information about the specified exception. - */ - public abstract void showDetailedErrorMessage(Frame owner, String err, String details); + public void installJSamplerHome(); - /** - * Shows a detailed error information about the specified exception. - */ - public abstract void showDetailedErrorMessage(Dialog owner, String err, String details); - - public abstract void handleConnectionFailure(); + /** Shows a detailed error information about the specified exception. */ + public void showDetailedErrorMessage(String err, String details); - protected Vector channelsPaneListeners = - new Vector(); + public void handleConnectionFailure(); /** * Registers the specified listener for receiving event messages. * @param l The ListSelectionListener to register. */ public void - addChannelsPaneSelectionListener(ListSelectionListener l) { - channelsPaneListeners.add(l); - } + addChannelsPaneSelectionListener(ListSelectionListener l); /** * Removes the specified listener. * @param l The ListSelectionListener to remove. */ public void - removeChannelsPaneSelectionListener(ListSelectionListener l) { - channelsPaneListeners.remove(l); - } - - protected void - fireChannelsPaneSelectionChanged() { - int i = getChannelsPaneIndex(getSelectedChannelsPane()); - ListSelectionEvent e = new ListSelectionEvent(this, i, i, false); - for(ListSelectionListener l : channelsPaneListeners) l.valueChanged(e); - } + removeChannelsPaneSelectionListener(ListSelectionListener l); /** - * Returns a list containing all JSChannelsPanes added to the view. - * @return A list containing all JSChannelsPanes added to the view. + * Returns a list containing all channels' panes added to the view. + * @return A list containing all channels' panes added to the view. * @see #addChannelsPane * @see #removeChannelsPane */ - public Vector - getChannelsPaneList() { return chnPaneList; } + public Vector getChannelsPaneList(); /** - * Return the JSChannelsPane at the specified position. - * @param idx The position of the JSChannelsPane to be returned. - * @return The JSChannelsPane at the specified position. + * Return the channels' pane at the specified position. + * @param idx The position of the channels' pane to be returned. + * @return The channels' pane at the specified position. */ - public JSChannelsPane - getChannelsPane(int idx) { return chnPaneList.get(idx); } + public CP getChannelsPane(int idx); /** - * Adds the specified JSChannelsPane to the view. - * @param chnPane The JSChannelsPane to be added. + * Adds the specified channels' pane to the view. + * @param chnPane The channels' pane to be added. */ - public void - addChannelsPane(JSChannelsPane chnPane) { - chnPaneList.add(chnPane); - firePropertyChange("channelLaneAdded", null, chnPane); - } + public void addChannelsPane(CP chnPane); /** - * Removes the specified JSChannelsPane from the view. + * Removes the specified channels' pane from the view. * Override this method to remove chnPane from the view, * and don't forget to call super.removeChannelsPane(chnPane);. - * @param chnPane The JSChannelsPane to be removed. + * @param chnPane The channels' pane to be removed. * @return true if the specified code>JSChannelsPane * is actually removed from the view, false otherwise. */ - public boolean - removeChannelsPane(JSChannelsPane chnPane) { - boolean b = chnPaneList.remove(chnPane); - firePropertyChange("channelLaneRemoved", null, chnPane); - return b; - } + public boolean removeChannelsPane(CP chnPane); /** - * Gets the current number of JSChannelsPanes added to the view. - * @return The current number of JSChannelsPanes added to the view. + * Gets the current number of channels' panes added to the view. + * @return The current number of channels' panes added to the view. */ - public int - getChannelsPaneCount() { return chnPaneList.size(); } + public int getChannelsPaneCount(); /** * Returns the index of the specified channels pane, or -1 if * the specified channels pane is not found. */ - public int - getChannelsPaneIndex(JSChannelsPane chnPane) { - return chnPaneList.indexOf(chnPane); - } + public int getChannelsPaneIndex(CP chnPane); + + public void setVisible(boolean b); /** - * Inserts the specified JSChannelsPane at the specified position - * in the view and in the JSChannelsPane list. + * Inserts the specified channels' pane at the specified position + * in the view and in the channels' pane list. * Where and how this pane will be shown depends on the view/GUI implementation. * Note that some GUI implementation may have only one pane containing sampler channels. - * @param pane The JSChannelsPane to be inserted. - * @param idx Specifies the position of the JSChannelsPane. + * @param pane The channels' pane to be inserted. + * @param idx Specifies the position of the channels' pane. * @see #getChannelsPaneList */ - public abstract void insertChannelsPane(JSChannelsPane pane, int idx); + public void insertChannelsPane(CP pane, int idx); /** - * Gets the JSChannelsPane that is currently shown, + * Gets the channels' pane that is currently shown, * or has the focus if more than one channels' panes are shown. * If the GUI implementation has only one pane containing sampler channels, - * than this method should always return that pane (the JSChannelsPane + * than this method should always return that pane (the channels' pane * with index 0). - * @return The selected JSChannelsPane. + * @return The selected channels' pane. */ - public abstract JSChannelsPane getSelectedChannelsPane(); + public CP getSelectedChannelsPane(); + + /** + * Get the server address to which to connect (asynchronously if needed) + * and pass it to r.run(). + * If the server should be manually selected, a dialog asking + * the user to choose a server is displayed. + */ + public void getServer(CC.Run r); + + /** + * Get the server address to which to connect (asynchronously if needed) + * and pass it to r.run(). + * If the server should be manually selected, a dialog asking + * the user to choose a server is displayed. + * @param manualSelect Determines whether the server should be manually selected. + */ + public void getServer(CC.Run r, boolean manualSelect); /** * Gets the server address to which to connect. If the server should be * manually selected, a dialog asking the user to choose a server is displayed. */ - public abstract Server getServer(); + public Server getServer(); /** * Gets the server address to which to connect. If the server should be * manually selected, a dialog asking the user to choose a server is displayed. * @param manualSelect Determines whether the server should be manually selected. */ - public abstract Server getServer(boolean manualSelect); + public Server getServer(boolean manualSelect); /** * Gets the LS Console model. * @return The LS Console model or null. */ - public LSConsoleModel - getLSConsoleModel() { return null; } + public LSConsoleModel getLSConsoleModel(); /** - * Sets the JSChannelsPane to be selected. + * Sets the channels' pane to be selected. * Note that all registered listeners should be notified * when the selection is changed. - * @param pane The JSChannelsPane to be shown. + * @param pane The channels' pane to be shown. * @see #fireChannelsPaneSelectionChanged */ - public abstract void setSelectedChannelsPane(JSChannelsPane pane); - - private class EventHandler implements SamplerChannelListListener { - /** - * Invoked when a new sampler channel is created. - * @param e A SamplerChannelListEvent - * instance providing the event information. - */ - @Override - public void - channelAdded(SamplerChannelListEvent e) { - if(e.getChannelModel() == null) return; - Integer id = e.getChannelModel().getChannelId(); - if(findChannel(id) != null) { - CC.getLogger().log(Level.WARNING, "JSMainFrame.channelExist!", id); - return; - } - - ChannelConfig config = null; - JSViewConfig viewConfig = CC.getViewConfig(); - if(viewConfig != null && viewConfig.getSessionViewConfig() != null) { - config = viewConfig.getSessionViewConfig().pollChannelConfig(); - } - - if(config == null) { - getSelectedChannelsPane().addChannel(e.getChannelModel()); - } else { - int i = config.channelsPanel; - if(i >= 0 && i < getChannelsPaneCount()) { - getChannelsPane(i).addChannel(e.getChannelModel(), config); - } else { - getSelectedChannelsPane().addChannel(e.getChannelModel(), config); - } - } - } - - /** - * Invoked when a sampler channel is removed. - * @param e A SamplerChannelListEvent - * instance providing the event information. - */ - @Override - public void - channelRemoved(SamplerChannelListEvent e) { - removeChannel(e.getChannelModel().getChannelId()); - } - } + public void setSelectedChannelsPane(CP pane); /** * Searches for the first occurence of a channel with numerical ID id. * @return The first occurence of a channel with numerical ID id or * null if there is no channel with numerical ID id. */ - public JSChannel - findChannel(int id) { - if(id < 0) return null; - - for(JSChannelsPane cp : getChannelsPaneList()) { - for(JSChannel c : cp.getChannels()) if(c.getChannelId() == id) return c; - } - - return null; - } + public JSChannel findChannel(int id); /** * Removes the first occurrence of a channel with numerical ID id. @@ -324,22 +197,7 @@ * @return The removed channel or null * if there is no channel with numerical ID id. */ - public JSChannel - removeChannel(int id) { - if(id < 0) return null; - - for(JSChannelsPane cp : getChannelsPaneList()) { - for(JSChannel c : cp.getChannels()) { - if(c.getChannelId() == id) { - cp.removeChannel(c); - firePropertyChange("channelRemoved", null, c); - return c; - } - } - } - - return null; - } + public JSChannel removeChannel(int id); /** * Gets the zero-based position of the specified sampler channel @@ -348,21 +206,7 @@ * @return The zero-based position of the specified sampler channel * in the channels pane, or -1 if the specified channels is not found. */ - public int - getChannelNumber(SamplerChannelModel channel) { - if(channel == null) return -1; - - for(int i = 0; i < getChannelsPaneCount(); i++) { - JSChannelsPane chnPane = getChannelsPane(i); - for(int j = 0; j < chnPane.getChannelCount(); j++) { - if(chnPane.getChannel(j).getChannelId() == channel.getChannelId()) { - return j; - } - } - } - - return -1; - } + public int getChannelNumber(SamplerChannelModel channel); /** * Returns a string in the format channelPaneNumber.channelNumber, @@ -372,21 +216,7 @@ * Note that this path may change when adding/removing channels/channels panes. * @return The channels path, or null if the specified channels is not found. */ - public String - getChannelPath(SamplerChannelModel channel) { - if(channel == null) return null; - - for(int i = 0; i < getChannelsPaneCount(); i++) { - JSChannelsPane chnPane = getChannelsPane(i); - for(int j = 0; j < chnPane.getChannelCount(); j++) { - if(chnPane.getChannel(j).getChannelId() == channel.getChannelId()) { - return (i + 1) + "." + (j + 1); - } - } - } - - return null; - } + public String getChannelPath(SamplerChannelModel channel); /** * Gets the zero-based number of the channels pane, @@ -396,56 +226,28 @@ * to which the specified sampler channel is added, or * -1 if the specified channels is not found. */ - public int - getChannelsPaneNumber(SamplerChannelModel channel) { - if(channel == null) return -1; - - for(int i = 0; i < getChannelsPaneCount(); i++) { - JSChannelsPane chnPane = getChannelsPane(i); - for(int j = 0; j < chnPane.getChannelCount(); j++) { - if(chnPane.getChannel(j).getChannelId() == channel.getChannelId()) { - return i; - } - } - } - - return -1; - } + public int getChannelsPaneNumber(SamplerChannelModel channel); /** * Sends the specified script to the backend. * @param script The file name of the script to run. */ - public abstract void runScript(String script); + public void runScript(String script); /** * Determines whether the channel list UI should be automatically updated * when channel is added/removed. The default value is true. */ - public boolean - getAutoUpdateChannelListUI() { return autoUpdateChannelListUI; } + public boolean getAutoUpdateChannelListUI(); /** * Determines whether the channel list UI should be automatically updated * when channel is added/removed. */ - public void - setAutoUpdateChannelListUI(boolean b) { - if(b == autoUpdateChannelListUI) return; - - autoUpdateChannelListUI = b; - for(JSChannelsPane cp : getChannelsPaneList()) { - cp.setAutoUpdate(b); - } - } + public void setAutoUpdateChannelListUI(boolean b); /** * Updates the channel list UI. */ - public void - updateChannelListUI() { - for(JSChannelsPane cp : getChannelsPaneList()) { - cp.updateChannelListUI(); - } - } + public void updateChannelListUI(); }