Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

MeasurementControlsPanel.java

Go to the documentation of this file.
00001 /*
00002  * MeasurementControlsPanel.java
00003  *
00004  * Copyright (C) 2005 Project SQUID, http://www.cs.helsinki.fi/group/squid/
00005  *
00006  * This file is part of Ikayaki.
00007  *
00008  * Ikayaki is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Ikayaki is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Ikayaki; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021  */
00022 
00023 package ikayaki.gui;
00024 
00025 import ikayaki.MeasurementEvent;
00026 import ikayaki.Project;
00027 import ikayaki.ProjectEvent;
00028 
00029 import javax.swing.*;
00030 import java.awt.*;
00031 import java.awt.event.ActionEvent;
00032 import java.awt.event.ActionListener;
00033 import java.awt.event.KeyEvent;
00034 
00044 public class MeasurementControlsPanel extends ProjectComponent {
00045 
00046     private final JButton measureButton;
00047     private final JButton pauseButton;
00048     private final JButton stepButton;
00049     private final JButton abortButton;
00050 
00051     // error-flashers for buttons
00052     private final ComponentFlasher measureButtonFlasher;
00053     private final ComponentFlasher pauseButtonFlasher;
00054     private final ComponentFlasher stepButtonFlasher;
00055     private final ComponentFlasher abortButtonFlasher;
00056 
00060     private final ButtonGroup zButtonGroup;
00061     private final JRadioButton zPlusRadioButton;
00062     private final JRadioButton zMinusRadioButton;
00063 
00067     private final JPanel sampleInsertPanel;
00068     private final JLabel sampleInsertTextLabel;
00069     private final Icon sampleInsertZPlusIcon;
00070     private final Icon sampleInsertZMinusIcon;
00071     private final JLabel sampleInsertIconLabel;
00072 
00076     private final MagnetometerStatusPanel.ManualControlsPanel manualControlsPanel;
00077 
00081     private final MagnetometerStatusPanel magnetometerStatusPanel;
00082 
00083     /* Swing Actions */
00084     private Action autoStepAction;
00085     private Action singleStepAction;
00086     private Action calibrateAction;
00087     private Action pauseAction;
00088     private Action abortAction;
00089 
00090     public MeasurementControlsPanel() {
00091 
00092         measureButton = new JButton(getAutoStepAction());
00093         pauseButton = new JButton(getPauseAction());
00094         stepButton = new JButton(getSingleStepAction());
00095         abortButton = new JButton(getAbortAction());
00096 
00097         measureButtonFlasher = new ComponentFlasher(measureButton);
00098         pauseButtonFlasher = new ComponentFlasher(pauseButton);
00099         stepButtonFlasher = new ComponentFlasher(stepButton);
00100         abortButtonFlasher = new ComponentFlasher(abortButton);
00101 
00102         JPanel buttonPanel = new JPanel(new GridLayout(2, 2, 2, 2)); // prevents button resize, looks a bit ugly
00103         buttonPanel.add(measureButton);
00104         buttonPanel.add(stepButton);
00105         buttonPanel.add(pauseButton);
00106         buttonPanel.add(abortButton);
00107 
00108         zButtonGroup = new ButtonGroup();
00109         zPlusRadioButton = new JRadioButton("+Z");
00110         zMinusRadioButton = new JRadioButton("-Z");
00111         zButtonGroup.add(zPlusRadioButton);
00112         zButtonGroup.add(zMinusRadioButton);
00113 
00114         JPanel zButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 8, 0));
00115         zButtonPanel.add(zPlusRadioButton);
00116         zButtonPanel.add(zMinusRadioButton);
00117 
00118         sampleInsertTextLabel = new JLabel("Put sample in holder arrow up.");
00119         sampleInsertZPlusIcon = new ImageIcon(ClassLoader.getSystemResource("resources/zplus.png"));
00120         sampleInsertZMinusIcon = new ImageIcon(ClassLoader.getSystemResource("resources/zminus.png"));
00121         sampleInsertIconLabel = new JLabel();
00122 
00123         sampleInsertPanel = new JPanel(new BorderLayout(4, 4));
00124         sampleInsertPanel.add(sampleInsertTextLabel, BorderLayout.NORTH);
00125         sampleInsertPanel.add(zButtonPanel, BorderLayout.CENTER);
00126         sampleInsertPanel.add(sampleInsertIconLabel, BorderLayout.SOUTH);
00127 
00128         JPanel topPanel = new JPanel(new BorderLayout(0, 8));
00129         topPanel.add(buttonPanel, BorderLayout.CENTER);
00130         topPanel.add(sampleInsertPanel, BorderLayout.SOUTH);
00131 
00132         magnetometerStatusPanel = new MagnetometerStatusPanel();
00133         manualControlsPanel = magnetometerStatusPanel.manualControlsPanel;
00134 
00135         JPanel contentPane = new JPanel(new BorderLayout(0, 8));
00136         contentPane.setBorder(BorderFactory.createEmptyBorder(0, 4, 4, 4));
00137         contentPane.add(topPanel, BorderLayout.NORTH);
00138         contentPane.add(magnetometerStatusPanel, BorderLayout.CENTER);
00139         contentPane.add(manualControlsPanel, BorderLayout.SOUTH);
00140         this.setLayout(new BorderLayout());
00141         this.add(contentPane);
00142 
00147         zPlusRadioButton.addActionListener(new ActionListener() {
00148             public void actionPerformed(ActionEvent e) {
00149                 getProject().setOrientation(Project.Orientation.PLUS_Z);
00150                 sampleInsertIconLabel.setIcon(sampleInsertZPlusIcon);
00151             }
00152         });
00153         zMinusRadioButton.addActionListener(new ActionListener() {
00154             public void actionPerformed(ActionEvent e) {
00155                 getProject().setOrientation(Project.Orientation.MINUS_Z);
00156                 sampleInsertIconLabel.setIcon(sampleInsertZMinusIcon);
00157             }
00158         });
00159 
00160         // initialize with no project
00161         setProject(null);
00162     }
00163 
00169     @Override public void setProject(Project project) {
00170         super.setProject(project);
00171         updateActions();
00172         if (project != null) {
00173             setOrientation(project.getOrientation());
00174             if (project.getSquid() != null) {
00175                 magnetometerStatusPanel.setSquid(project.getSquid());
00176             }
00177         }
00178         manualControlsPanel.setProject(project);
00179     }
00180 
00186     @Override public void projectUpdated(ProjectEvent event) {
00187         updateActions();
00188         manualControlsPanel.setEnabled();
00189     }
00190 
00196     @Override public void measurementUpdated(MeasurementEvent event) {
00197         magnetometerStatusPanel.measurementUpdated(event);
00198     }
00199 
00203     private void setOrientation(Project.Orientation orientation) {
00204         if (orientation == Project.Orientation.PLUS_Z) {
00205             zPlusRadioButton.setSelected(true);
00206             sampleInsertIconLabel.setIcon(sampleInsertZPlusIcon);
00207         } else {
00208             zMinusRadioButton.setSelected(true);
00209             sampleInsertIconLabel.setIcon(sampleInsertZMinusIcon);
00210         }
00211     }
00212 
00216     private void updateActions() {
00217         if (getProject() != null) {
00218             getAutoStepAction().setEnabled(getProject().isAutoStepEnabled());
00219             getSingleStepAction().setEnabled(getProject().isSingleStepEnabled());
00220             getCalibrateAction().setEnabled(getProject().isSingleStepEnabled()
00221                     && getProject().getType() == Project.Type.CALIBRATION);
00222             getPauseAction().setEnabled(getProject().isPauseEnabled());
00223             getAbortAction().setEnabled(getProject().isAbortEnabled());
00224 
00225             sampleInsertTextLabel.setEnabled(true);
00226             sampleInsertIconLabel.setEnabled(true);
00227             zPlusRadioButton.setEnabled(true);
00228             zMinusRadioButton.setEnabled(true);
00229         } else {
00230             getAutoStepAction().setEnabled(false);
00231             getSingleStepAction().setEnabled(false);
00232             getCalibrateAction().setEnabled(false);
00233             getPauseAction().setEnabled(false);
00234             getAbortAction().setEnabled(false);
00235 
00236             sampleInsertTextLabel.setEnabled(false);
00237             sampleInsertIconLabel.setEnabled(false);
00238             zPlusRadioButton.setEnabled(false);
00239             zMinusRadioButton.setEnabled(false);
00240         }
00241     }
00242 
00243     /* Getters for Swing Actions */
00244 
00249     public Action getAutoStepAction() {
00250         if (autoStepAction == null) {
00251             autoStepAction = new AbstractAction() {
00252                 public void actionPerformed(ActionEvent e) {
00253                     if (!getProject().doAutoStep()) {
00254                         if (e.getSource() == measureButton) {
00255                             measureButtonFlasher.flash();
00256                         } else {
00257                             JOptionPane.showMessageDialog(getParentFrame(),
00258                                     "Unable to measure.", "Squid Error", JOptionPane.ERROR_MESSAGE);
00259                         }
00260                     }
00261                 }
00262             };
00263             autoStepAction.putValue(Action.NAME, "Measure");
00264             //autoStepAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_M);
00265             autoStepAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_MASK));
00266             autoStepAction.putValue(Action.SMALL_ICON,
00267                     new ImageIcon(ClassLoader.getSystemResource("resources/play.png")));
00268         }
00269         return autoStepAction;
00270     }
00271 
00275     public Action getSingleStepAction() {
00276         if (singleStepAction == null) {
00277             singleStepAction = new AbstractAction() {
00278                 public void actionPerformed(ActionEvent e) {
00279                     if (!getProject().doSingleStep()) {
00280                         if (e.getSource() == stepButton) {
00281                             stepButtonFlasher.flash();
00282                         } else {
00283                             JOptionPane.showMessageDialog(getParentFrame(),
00284                                     "Unable to single step.", "Squid Error", JOptionPane.ERROR_MESSAGE);
00285                         }
00286                     }
00287                 }
00288             };
00289             singleStepAction.putValue(Action.NAME, "Single Step");
00290             //singleStepAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_I);
00291             singleStepAction.putValue(Action.ACCELERATOR_KEY,
00292                     KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.CTRL_MASK));
00293             singleStepAction.putValue(Action.SMALL_ICON,
00294                     new ImageIcon(ClassLoader.getSystemResource("resources/step.png")));
00295         }
00296         return singleStepAction;
00297     }
00298 
00299     public Action getCalibrateAction() {
00300         if (calibrateAction == null) {
00301             calibrateAction = new AbstractAction() {
00302                 public void actionPerformed(ActionEvent e) {
00303                     if (!getProject().doSingleStep()) {
00304                         if (e.getSource() instanceof JButton) {
00305                             new ComponentFlasher((JComponent) e.getSource()).flash();
00306                         } else {
00307                             JOptionPane.showMessageDialog(getParentFrame(),
00308                                     "Unable to calibrate.", "Squid Error", JOptionPane.ERROR_MESSAGE);
00309                         }
00310                     }
00311                 }
00312             };
00313             calibrateAction.putValue(Action.NAME, "Calibrate");
00314             //calibrateAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_I);
00315             //calibrateAction.putValue(Action.ACCELERATOR_KEY,
00316             //        KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.CTRL_MASK));
00317             calibrateAction.putValue(Action.SMALL_ICON,
00318                     new ImageIcon(ClassLoader.getSystemResource("resources/step.png")));
00319         }
00320         return calibrateAction;
00321     }
00322 
00323     public Action getPauseAction() {
00324         if (pauseAction == null) {
00325             pauseAction = new AbstractAction() {
00326                 public void actionPerformed(ActionEvent e) {
00327                     if (!getProject().doPause()) {
00328                         if (e.getSource() == pauseButton) {
00329                             pauseButtonFlasher.flash();
00330                         } else if (e.getSource() == measureButton) {
00331                             measureButtonFlasher.flash();
00332                         } else {
00333                             JOptionPane.showMessageDialog(getParentFrame(),
00334                                     "Unable to pause.", "Squid Error", JOptionPane.ERROR_MESSAGE);
00335                         }
00336                     }
00337                 }
00338             };
00339             pauseAction.putValue(Action.NAME, "Pause");
00340             //pauseAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_P);
00341             pauseAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_P, KeyEvent.CTRL_MASK));
00342             pauseAction.putValue(Action.SMALL_ICON,
00343                     new ImageIcon(ClassLoader.getSystemResource("resources/pause.png")));
00344         }
00345         return pauseAction;
00346     }
00347 
00351     public Action getAbortAction() {
00352         if (abortAction == null) {
00353             abortAction = new AbstractAction() {
00354                 public void actionPerformed(ActionEvent e) {
00355                     if (!getProject().doAbort()) {
00356                         if (e.getSource() == abortButton) {
00357                             abortButtonFlasher.flash();
00358                         } else {
00359                             JOptionPane.showMessageDialog(getParentFrame(),
00360                                     "Unable to abort!", "Squid Error", JOptionPane.ERROR_MESSAGE);
00361                         }
00362                     }
00363                 }
00364             };
00365             abortAction.putValue(Action.NAME, "Stop Now!");
00366             //abortAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_T);
00367             abortAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_T, KeyEvent.CTRL_MASK));
00368             abortAction.putValue(Action.SMALL_ICON, new ImageIcon(ClassLoader.getSystemResource("resources/stop.png")));
00369         }
00370         return abortAction;
00371     }
00372 }

Generated on Fri May 6 16:00:31 2005 for Squid by  doxygen 1.4.1