src/org/math/plot/components/SetScalesFrame.java

00001 package org.math.plot.components;
00002 
00003 import java.awt.*;
00004 import java.awt.event.*;
00005 
00006 import javax.swing.*;
00007 
00008 import org.math.io.*;
00009 import org.math.plot.canvas.*;
00010 import org.math.plot.plotObjects.*;
00011 
00018 public class SetScalesFrame extends JFrame implements WindowFocusListener, WindowListener {
00019 
00023         private static final long serialVersionUID = 1L;
00024 
00025         private PlotCanvas plotCanvas;
00026 
00027         private JPanel scalespanel;
00028 
00029         private ScalePanel[] scalepanels;
00030 
00031         public SetScalesFrame(PlotCanvas p) {
00032                 super("scales settings");
00033                 plotCanvas = p;
00034                 setPanel();
00035                 setContentPane(scalespanel);
00036 
00037                 setResizable(false);
00038                 setVisible(true);
00039 
00040                 addWindowFocusListener(this);
00041                 addWindowListener(this);
00042         }
00043 
00044         private void setPanel() {
00045                 int nbAxes = plotCanvas.base.dimension;
00046 
00047                 this.setSize(nbAxes * 300, 200);
00048 
00049                 scalespanel = new JPanel();
00050                 scalespanel.setLayout(new GridLayout(1, nbAxes));
00051 
00052                 scalepanels = new ScalePanel[nbAxes];
00053                 for (int i = 0; i < nbAxes; i++) {
00054                         scalepanels[i] = new ScalePanel(plotCanvas, i);
00055                         scalespanel.add(scalepanels[i]);
00056                 }
00057         }
00058 
00059         /*
00060          * private void buildConstraints(GridBagConstraints gbc, int gx, int gy, int
00061          * gw, int gh, int wx, int wy) { gbc.gridx = gx; gbc.gridy = gy;
00062          * gbc.gridwidth = gw; gbc.gridheight = gh; gbc.weightx = wx; gbc.weighty =
00063          * wy; }
00064          */
00065 
00066         public void setDefaultCloseOperation(int operation) {
00067                 for (int i = 0; i < scalepanels.length; i++) {
00068                         scalepanels[i].updateBoundsFields();
00069                 }
00070                 super.setDefaultCloseOperation(operation);
00071         }
00072 
00073         public void windowGainedFocus(WindowEvent e) {
00074                 for (int i = 0; i < scalepanels.length; i++) {
00075                         scalepanels[i].update();
00076                 }
00077         }
00078 
00079         public void windowLostFocus(WindowEvent e) {
00080         }
00081 
00082         public void windowOpened(WindowEvent e) {
00083         }
00084 
00085         public void windowClosing(WindowEvent e) {
00086         }
00087 
00088         public void windowClosed(WindowEvent e) {
00089         }
00090 
00091         public void windowIconified(WindowEvent e) {
00092         }
00093 
00094         public void windowDeiconified(WindowEvent e) {
00095         }
00096 
00097         public void windowActivated(WindowEvent e) {
00098                 for (int i = 0; i < scalepanels.length; i++) {
00099                         scalepanels[i].update();
00100                 }
00101         }
00102 
00103         public void windowDeactivated(WindowEvent e) {
00104         }
00105 
00106         public class ScalePanel extends JPanel implements StringPrintable {
00107 
00108                 private static final long serialVersionUID = 1L;
00109 
00110                 // private PlotCanvas plotCanvas;
00111 
00112                 private int numAxe;
00113 
00114                 private String title;
00115 
00116                 private String scaleType;
00117 
00118                 private double min;
00119 
00120                 private double max;
00121 
00122                 private JLabel title_label = new JLabel("Title");
00123 
00124                 private JTextField title_field = new JTextField();
00125 
00126                 private JLabel scale_label = new JLabel("Scale");
00127 
00128                 private ButtonGroup scale_group = new ButtonGroup();
00129 
00130                 private JRadioButton linear_check = new JRadioButton("Linear");
00131 
00132                 private JRadioButton log_check = new JRadioButton("Logarithmic");
00133 
00134                 // private JCheckBox gridVisible = new JCheckBox("Grid visible");
00135 
00136                 private JLabel bounds_label = new JLabel("Bounds");
00137 
00138                 private JLabel min_label = new JLabel("Min");
00139 
00140                 private JLabel max_label = new JLabel("Max");
00141 
00142                 private JTextField min_field = new JTextField();
00143 
00144                 private JTextField max_field = new JTextField();
00145 
00146                 private JButton bounds_auto = new JButton("Automatic");
00147 
00148                 public ScalePanel(PlotCanvas p, int i) {
00149                         numAxe = i;
00150                         plotCanvas = p;
00151 
00152                         update();
00153 
00154                         addComponents();
00155                         setListeners();
00156                 }
00157 
00158                 public void update() {
00159                         title = plotCanvas.getGrid().getAxis(numAxe).getLegend();// getLegend(numAxe);
00160                         title_field.setText(title);
00161 
00162                         scaleType = plotCanvas.getAxisScales()[numAxe];
00163                         log_check.setSelected(scaleType.equals(Base.LOGARITHM));
00164                         linear_check.setSelected(scaleType.equals(Base.LINEAR));
00165                         if (scaleType.equals(Base.STRINGS)) {log_check.setEnabled(false); linear_check.setEnabled(false);}
00166 
00167                         updateBoundsFields();
00168                 }
00169 
00170                 private void addComponents() {
00171                         this.setSize(300, 200);
00172 
00173                         scale_group.add(linear_check);
00174                         scale_group.add(log_check);
00175 
00176                         GridBagLayout gbl = new GridBagLayout();
00177                         GridBagConstraints c = new GridBagConstraints();
00178                         this.setLayout(gbl);
00179 
00180                         buildConstraints(c, 0, 0, 1, 1, 40, 20);
00181                         c.fill = GridBagConstraints.CENTER;
00182                         c.anchor = GridBagConstraints.CENTER;
00183                         gbl.setConstraints(title_label, c);
00184                         this.add(title_label);
00185 
00186                         buildConstraints(c, 1, 0, 2, 1, 60, 20);
00187                         c.fill = GridBagConstraints.HORIZONTAL;
00188                         gbl.setConstraints(title_field, c);
00189                         this.add(title_field);
00190 
00191                         buildConstraints(c, 0, 1, 1, 1, 40, 20);
00192                         c.fill = GridBagConstraints.CENTER;
00193                         c.anchor = GridBagConstraints.CENTER;
00194                         gbl.setConstraints(scale_label, c);
00195                         this.add(scale_label);
00196 
00197                         buildConstraints(c, 1, 1, 2, 1, 60, 20);
00198                         c.fill = GridBagConstraints.HORIZONTAL;
00199                         gbl.setConstraints(linear_check, c);
00200                         this.add(linear_check);
00201 
00202                         buildConstraints(c, 1, 2, 2, 1, 60, 20);
00203                         c.fill = GridBagConstraints.HORIZONTAL;
00204                         gbl.setConstraints(log_check, c);
00205                         this.add(log_check);
00206 
00207                         buildConstraints(c, 0, 3, 1, 1, 40, 20);
00208                         c.fill = GridBagConstraints.CENTER;
00209                         c.anchor = GridBagConstraints.CENTER;
00210                         gbl.setConstraints(bounds_label, c);
00211                         this.add(bounds_label);
00212 
00213                         buildConstraints(c, 1, 3, 1, 1, 20, 20);
00214                         c.fill = GridBagConstraints.CENTER;
00215                         c.fill = GridBagConstraints.CENTER;
00216                         gbl.setConstraints(min_label, c);
00217                         this.add(min_label);
00218 
00219                         buildConstraints(c, 2, 3, 1, 1, 50, 20);
00220                         c.fill = GridBagConstraints.HORIZONTAL;
00221                         gbl.setConstraints(min_field, c);
00222                         this.add(min_field);
00223 
00224                         buildConstraints(c, 1, 4, 1, 1, 20, 20);
00225                         c.fill = GridBagConstraints.CENTER;
00226                         c.fill = GridBagConstraints.CENTER;
00227                         gbl.setConstraints(max_label, c);
00228                         this.add(max_label);
00229 
00230                         buildConstraints(c, 2, 4, 1, 1, 50, 20);
00231                         c.fill = GridBagConstraints.HORIZONTAL;
00232                         gbl.setConstraints(max_field, c);
00233                         this.add(max_field);
00234 
00235                         buildConstraints(c, 1, 5, 2, 1, 60, 20);
00236                         c.fill = GridBagConstraints.CENTER;
00237                         gbl.setConstraints(bounds_auto, c);
00238                         this.add(bounds_auto);
00239 
00240                         // this.add(gridVisible);
00241                 }
00242 
00243                 private void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) {
00244                         gbc.gridx = gx;
00245                         gbc.gridy = gy;
00246                         gbc.gridwidth = gw;
00247                         gbc.gridheight = gh;
00248                         gbc.weightx = wx;
00249                         gbc.weighty = wy;
00250                 }
00251 
00252                 private void setListeners() {
00253                         title_field.addKeyListener(new KeyListener() {
00254                                 public void keyReleased(KeyEvent e) {
00255                                         setTitle();
00256                                 }
00257 
00258                                 public void keyPressed(KeyEvent e) {
00259                                 }
00260 
00261                                 public void keyTyped(KeyEvent e) {
00262                                 }
00263                         });
00264 
00265                         log_check.addActionListener(new ActionListener() {
00266                                 public void actionPerformed(ActionEvent e) {
00267                                         setScale();
00268                                         updateBoundsFields();
00269                                 }
00270                         });
00271                         linear_check.addActionListener(new ActionListener() {
00272                                 public void actionPerformed(ActionEvent e) {
00273                                         setScale();
00274                                         updateBoundsFields();
00275                                 }
00276                         });
00277                         /*
00278                          * gridVisible.addChangeListener(new ChangeListener() { public void
00279                          * stateChanged(ChangeEvent e) {
00280                          * plotPanel.getGrid().getAxe(numAxe).setVisible(
00281                          * gridVisible.isSelected()); plotPanel.repaint(); } });
00282                          */
00283 
00284                         min_field.addKeyListener(new KeyListener() {
00285                                 public void keyReleased(KeyEvent e) {
00286                                         setBounds();
00287                                 }
00288 
00289                                 public void keyPressed(KeyEvent e) {
00290                                 }
00291 
00292                                 public void keyTyped(KeyEvent e) {
00293                                 }
00294                         });
00295 
00296                         max_field.addKeyListener(new KeyListener() {
00297                                 public void keyReleased(KeyEvent e) {
00298                                         setBounds();
00299                                 }
00300 
00301                                 public void keyPressed(KeyEvent e) {
00302                                 }
00303 
00304                                 public void keyTyped(KeyEvent e) {
00305                                 }
00306                         });
00307 
00308                         bounds_auto.addActionListener(new ActionListener() {
00309                                 public void actionPerformed(ActionEvent e) {
00310                                         setBoundsAuto();
00311                                 }
00312                         });
00313 
00314                 }
00315 
00316                 public String getText() {
00317                         return "title = " + title + "\nscaleType = " + scaleType + "\nmin = " + min + "\nmax = " + max;
00318                 }
00319 
00320                 private void setTitle() {
00321                         // System.out.println("title setting n�" + numAxe + " : " +
00322                         // title_field.getText());
00323                         plotCanvas.setAxisLabel(numAxe, title_field.getText());
00324                 }
00325 
00326                 private void setBounds() {
00327                         // System.out.println("bounds setting n�" + numAxe + " : " +
00328                         // min_field.getText() + " - " + max_field.getText());
00329                         try {
00330                                 double min1 = Double.parseDouble(min_field.getText());
00331                                 double max1 = Double.parseDouble(max_field.getText());
00332                                 plotCanvas.setFixedBounds(numAxe, min1, max1);
00333                         } catch (IllegalArgumentException iae) {
00334                                 // JOptionPane.showConfirmDialog(null, iae.getMessage(),
00335                                 // "Error", JOptionPane.DEFAULT_OPTION,
00336                                 // JOptionPane.ERROR_MESSAGE);
00337                                 // updateBoundsFields();
00338                         }
00339                 }
00340 
00341                 private void setScale() {
00342                         // System.out.println("scale setting n�" + numAxe + " : " + (
00343                         // (log_check.isSelected()) ? ("LOG") : ("LINEAR")));
00344                         try {
00345                                 plotCanvas.setAxiScale(numAxe, (log_check.isSelected()) ? (Base.LOGARITHM) : (Base.LINEAR));
00346                         } catch (IllegalArgumentException iae) {
00347                                 JOptionPane.showConfirmDialog(null, iae.getMessage(), "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
00348                                 updateBoundsFields();
00349                         }
00350                 }
00351 
00352                 private void setBoundsAuto() {
00353                         plotCanvas.setAutoBounds(numAxe);
00354                         updateBoundsFields();
00355                         // System.out.println("auto-Bounds setting n�"+numAxe+" :
00356                         // "+plotPanel.getBase().getMinBounds()[numAxe]+" -
00357                         // "+plotPanel.getBase().getMaxBounds()[numAxe]);
00358                 }
00359 
00360                 private void updateBoundsFields() {
00361                         min = plotCanvas.base.getMinBounds()[numAxe];
00362                         max = plotCanvas.base.getMaxBounds()[numAxe];
00363                         min_field.setText("" + min);
00364                         max_field.setText("" + max);
00365                         // log_check.setSelected(plotCanvas.base.getAxeScale(numAxe)==Base.LOG);
00366                 }
00367 
00368         }
00369 
00370 }

Generated on Wed Sep 5 21:44:01 2007 for jmathplot by  doxygen 1.5.1