MOA 12.03
Real Time Analytics for Data Streams
OptionsConfigurationPanel.java
Go to the documentation of this file.
00001 /*
00002  *    OptionsConfigurationPanel.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.gui;
00021 
00022 import java.awt.BorderLayout;
00023 import java.awt.Component;
00024 import java.awt.Dimension;
00025 import java.awt.FlowLayout;
00026 import java.awt.Font;
00027 import java.awt.GridBagConstraints;
00028 import java.awt.GridBagLayout;
00029 import java.awt.Insets;
00030 import java.awt.event.ActionEvent;
00031 import java.awt.event.ActionListener;
00032 import java.util.LinkedList;
00033 import java.util.List;
00034 
00035 import javax.swing.BorderFactory;
00036 import javax.swing.JButton;
00037 import javax.swing.JComponent;
00038 import javax.swing.JFrame;
00039 import javax.swing.JLabel;
00040 import javax.swing.JOptionPane;
00041 import javax.swing.JPanel;
00042 import javax.swing.JScrollPane;
00043 import javax.swing.JTextArea;
00044 import javax.swing.ScrollPaneConstants;
00045 import javax.swing.UIManager;
00046 
00047 import moa.classifiers.trees.HoeffdingTree;
00048 import moa.options.Option;
00049 import moa.options.OptionHandler;
00050 import moa.options.Options;
00051 
00058 public class OptionsConfigurationPanel extends JPanel {
00059 
00060     private static final long serialVersionUID = 1L;
00061 
00062     public static final int FIXED_PANEL_WIDTH = 400;
00063 
00064     public static final int MAX_PANEL_HEIGHT = 300;
00065 
00066     protected Options options;
00067 
00068     protected List<OptionEditComponent> editComponents = new LinkedList<OptionEditComponent>();
00069 
00070     protected JButton helpButton = new JButton("Help");
00071 
00072     protected JButton resetButton = new JButton("Reset to defaults");
00073 
00074     public OptionsConfigurationPanel(String purposeString, Options options) {
00075         this.options = options;
00076         setLayout(new BorderLayout());
00077         if (purposeString != null) {
00078             JTextArea purposeTextArea = new JTextArea(purposeString, 3, 0);
00079             purposeTextArea.setEditable(false);
00080             purposeTextArea.setLineWrap(true);
00081             purposeTextArea.setWrapStyleWord(true);
00082             purposeTextArea.setEnabled(false);
00083             purposeTextArea.setBorder(BorderFactory.createTitledBorder("Purpose"));
00084             purposeTextArea.setBackground(getBackground());
00085             JScrollPane scrollPanePurpose = new JScrollPane(purposeTextArea,
00086             ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
00087             ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
00088             scrollPanePurpose.setBorder(null);
00089             add(scrollPanePurpose, BorderLayout.NORTH);
00090         }
00091         JPanel optionsPanel = createLabelledOptionComponentListPanel(options.getOptionArray(), this.editComponents);
00092         JScrollPane scrollPane = new JScrollPane(optionsPanel,
00093                 ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
00094                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
00095         scrollPane.setBorder(null);
00096         int optionPanelHeight = (int) optionsPanel.getPreferredSize().getHeight();
00097         int scrollPaneHeight = (int) scrollPane.getPreferredSize().getHeight();
00098         scrollPane.setPreferredSize(new Dimension(FIXED_PANEL_WIDTH,
00099                 scrollPaneHeight > MAX_PANEL_HEIGHT ? MAX_PANEL_HEIGHT
00100                 : scrollPaneHeight));
00101         optionsPanel.setPreferredSize(new Dimension(0, optionPanelHeight));
00102         add(scrollPane, BorderLayout.CENTER);
00103         JPanel lowerButtons = new JPanel();
00104         lowerButtons.setLayout(new FlowLayout());
00105         lowerButtons.add(this.helpButton);
00106         lowerButtons.add(this.resetButton);
00107         add(lowerButtons, BorderLayout.SOUTH);
00108         this.helpButton.addActionListener(new ActionListener() {
00109 
00110             @Override
00111             public void actionPerformed(ActionEvent arg0) {
00112                 showHelpDialog();
00113             }
00114         });
00115         this.resetButton.addActionListener(new ActionListener() {
00116 
00117             @Override
00118             public void actionPerformed(ActionEvent arg0) {
00119                 resetToDefaults();
00120             }
00121         });
00122     }
00123 
00124     public static boolean showEditOptionsDialog(Component parent, String title,
00125             OptionHandler optionHandler) {
00126         OptionsConfigurationPanel panel = new OptionsConfigurationPanel(
00127                 optionHandler.getPurposeString(), optionHandler.getOptions());
00128         if (JOptionPane.showOptionDialog(parent, panel, title,
00129                 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null,
00130                 null, null) == JOptionPane.OK_OPTION) {
00131             panel.applyChanges();
00132             return true;
00133         }
00134         return false;
00135     }
00136 
00137     public String getHelpText() {
00138         return this.options.getHelpString();
00139     }
00140 
00141     public void showHelpDialog() {
00142         JTextArea helpTextArea = new JTextArea(getHelpText(), 20, 80);
00143         helpTextArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
00144         helpTextArea.setEditable(false);
00145         JOptionPane.showMessageDialog(this, new JScrollPane(helpTextArea),
00146                 "Options Help", JOptionPane.INFORMATION_MESSAGE);
00147     }
00148 
00149     public void resetToDefaults() {
00150         for (OptionEditComponent editor : this.editComponents) {
00151             editor.setEditState(editor.getEditedOption().getDefaultCLIString());
00152         }
00153     }
00154 
00155     public void applyChanges() {
00156         for (OptionEditComponent editor : this.editComponents) {
00157             try {
00158                 editor.applyState();
00159             } catch (Exception ex) {
00160                 GUIUtils.showExceptionDialog(this, "Problem with option "
00161                         + editor.getEditedOption().getName(), ex);
00162             }
00163         }
00164     }
00165 
00166     protected static JPanel createLabelledOptionComponentListPanel(
00167             Option[] options, List<OptionEditComponent> editComponents) {
00168         JPanel panel = new JPanel();
00169         if ((options != null) && (options.length > 0)) {
00170             GridBagLayout gbLayout = new GridBagLayout();
00171             GridBagConstraints gbc = new GridBagConstraints();
00172             panel.setLayout(gbLayout);
00173             for (int i = 0; i < options.length; i++) {
00174                 JLabel label = new JLabel(options[i].getName());
00175                 label.setToolTipText(options[i].getPurpose());
00176                 gbc.gridx = 0;
00177                 gbc.fill = GridBagConstraints.NONE;
00178                 gbc.anchor = GridBagConstraints.EAST;
00179                 gbc.weightx = 0;
00180                 gbc.insets = new Insets(5, 5, 5, 5);
00181                 gbLayout.setConstraints(label, gbc);
00182                 panel.add(label);
00183                 JComponent editor = options[i].getEditComponent();
00184                 label.setLabelFor(editor);
00185                 if (editComponents != null) {
00186                     editComponents.add((OptionEditComponent) editor);
00187                 }
00188                 gbc.gridx = 1;
00189                 gbc.fill = GridBagConstraints.HORIZONTAL;
00190                 gbc.anchor = GridBagConstraints.CENTER;
00191                 gbc.weightx = 1;
00192                 gbc.insets = new Insets(5, 5, 5, 5);
00193                 gbLayout.setConstraints(editor, gbc);
00194                 panel.add(editor);
00195             }
00196         } else {
00197             panel.add(new JLabel("No options."));
00198         }
00199         return panel;
00200     }
00201 
00202     private static void createAndShowGUI() {
00203 
00204         // Create and set up the window.
00205         JFrame frame = new JFrame("Test");
00206         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00207 
00208         // Create and set up the content pane.
00209         Options options = new HoeffdingTree().getOptions();
00210         JPanel panel = new OptionsConfigurationPanel(null, options);
00211         // createLabelledOptionComponentListPanel(options
00212         // .getOptionArray(), null);
00213         panel.setOpaque(true); // content panes must be opaque
00214         frame.setContentPane(panel);
00215 
00216         // Display the window.
00217         frame.pack();
00218         // frame.setSize(400, 400);
00219         frame.setVisible(true);
00220     }
00221 
00222     public static void main(String[] args) {
00223         try {
00224             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
00225             javax.swing.SwingUtilities.invokeLater(new Runnable() {
00226 
00227                 @Override
00228                 public void run() {
00229                     createAndShowGUI();
00230                 }
00231             });
00232         } catch (Exception e) {
00233             e.printStackTrace();
00234         }
00235     }
00236 }
 All Classes Namespaces Files Functions Variables Enumerations