Back to index

wims  3.65+svn20090927
PlotFormatter.java
Go to the documentation of this file.
00001 /* A panel for controlling the format of a plotter.
00002 
00003  Copyright (c) 1998-2005 The Regents of the University of California.
00004  All rights reserved.
00005  Permission is hereby granted, without written agreement and without
00006  license or royalty fees, to use, copy, modify, and distribute this
00007  software and its documentation for any purpose, provided that the above
00008  copyright notice and the following two paragraphs appear in all copies
00009  of this software.
00010 
00011  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
00012  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
00013  ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
00014  THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
00015  SUCH DAMAGE.
00016 
00017  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00018  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
00020  PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
00021  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
00022  ENHANCEMENTS, OR MODIFICATIONS.
00023 
00024  PT_COPYRIGHT_VERSION_2
00025  COPYRIGHTENDKEY
00026  */
00027 package ptolemy.plot;
00028 
00029 import java.awt.BorderLayout;
00030 import java.awt.Container;
00031 import java.awt.Frame;
00032 import java.util.Vector;
00033 
00034 import javax.swing.JPanel;
00035 
00036 import ptolemy.gui.ComponentDialog;
00037 import ptolemy.gui.Query;
00038 import ptolemy.gui.QueryListener;
00039 
00042 
00056 public class PlotFormatter extends JPanel {
00059     public PlotFormatter(PlotBox plot) {
00060         super();
00061         _plot = plot;
00062 
00063         setLayout(new BorderLayout());
00064         _wideQuery = new Query();
00065         add(_wideQuery, BorderLayout.WEST);
00066         _narrowQuery = new Query();
00067         add(_narrowQuery, BorderLayout.EAST);
00068 
00069         // Populate the wide query.
00070         _wideQuery.setTextWidth(20);
00071         _originalTitle = plot.getTitle();
00072         _wideQuery.addLine("title", "Title", _originalTitle);
00073         _originalXLabel = plot.getXLabel();
00074         _wideQuery.addLine("xlabel", "X Label", _originalXLabel);
00075         _originalYLabel = plot.getYLabel();
00076         _wideQuery.addLine("ylabel", "Y Label", _originalYLabel);
00077         _originalXRange = plot.getXRange();
00078         _wideQuery.addLine("xrange", "X Range", "" + _originalXRange[0] + ", "
00079                 + _originalXRange[1]);
00080         _originalYRange = plot.getYRange();
00081         _wideQuery.addLine("yrange", "Y Range", "" + _originalYRange[0] + ", "
00082                 + _originalYRange[1]);
00083 
00084         String[] marks = { "none", "points", "dots", "various", "pixels" };
00085         _originalMarks = "none";
00086 
00087         if (plot instanceof Plot) {
00088             _originalMarks = ((Plot) plot).getMarksStyle();
00089             _wideQuery.addRadioButtons("marks", "Marks", marks, _originalMarks);
00090         }
00091 
00092         _originalXTicks = plot.getXTicks();
00093         _originalXTicksSpec = "";
00094 
00095         if (_originalXTicks != null) {
00096             StringBuffer buffer = new StringBuffer();
00097             Vector positions = _originalXTicks[0];
00098             Vector labels = _originalXTicks[1];
00099 
00100             for (int i = 0; i < labels.size(); i++) {
00101                 if (buffer.length() > 0) {
00102                     buffer.append(", ");
00103                 }
00104 
00105                 buffer.append(labels.elementAt(i).toString());
00106                 buffer.append(" ");
00107                 buffer.append(positions.elementAt(i).toString());
00108             }
00109 
00110             _originalXTicksSpec = buffer.toString();
00111         }
00112 
00113         _wideQuery.addLine("xticks", "X Ticks", _originalXTicksSpec);
00114 
00115         _originalYTicks = plot.getYTicks();
00116         _originalYTicksSpec = "";
00117 
00118         if (_originalYTicks != null) {
00119             StringBuffer buffer = new StringBuffer();
00120             Vector positions = _originalYTicks[0];
00121             Vector labels = _originalYTicks[1];
00122 
00123             for (int i = 0; i < labels.size(); i++) {
00124                 if (buffer.length() > 0) {
00125                     buffer.append(", ");
00126                 }
00127 
00128                 buffer.append(labels.elementAt(i).toString());
00129                 buffer.append(" ");
00130                 buffer.append(positions.elementAt(i).toString());
00131             }
00132 
00133             _originalYTicksSpec = buffer.toString();
00134         }
00135 
00136         _wideQuery.addLine("yticks", "Y Ticks", _originalYTicksSpec);
00137 
00138         _originalGrid = plot.getGrid();
00139         _narrowQuery.addCheckBox("grid", "Grid", _originalGrid);
00140         _originalStems = false;
00141         _originalConnected = null;
00142 
00143         if (plot instanceof Plot) {
00144             _originalStems = ((Plot) plot).getImpulses();
00145             _narrowQuery.addCheckBox("stems", "Stems", _originalStems);
00146             _saveConnected();
00147             _narrowQuery.addCheckBox("connected", "Connect", ((Plot) plot)
00148                     .getConnected());
00149         }
00150 
00151         _originalColor = plot.getColor();
00152         _narrowQuery.addCheckBox("color", "Use Color", _originalColor);
00153 
00154         // FIXME: setXLog() and setYLog() cause problems with
00155         // dropped data if they are toggled after data is read in.
00156         // This is because the log axis facility modifies the datasets
00157         // in addPlotPoint() in Plot.java.  When this is fixed
00158         // we can add the XLog and YLog facility to the Format menu
00159         //
00160         // _originalXLog = plot.getXLog();
00161         //_narrowQuery.addCheckBox("xlog", "X Log", _originalXLog);
00162         //if (_originalXTicks != null) {
00163         //    _narrowQuery.setBoolean("xlog", false);
00164         //    _narrowQuery.setEnabled("xlog", false);
00165         //}
00166         // _originalYLog = plot.getYLog();
00167         //_narrowQuery.addCheckBox("ylog", "Y Log", _originalYLog);
00168         //if (_originalYTicks != null) {
00169         //    _narrowQuery.setBoolean("ylog", false);
00170         //    _narrowQuery.setEnabled("ylog", false);
00171         //}
00172         // Attach listeners.
00173         _wideQuery.addQueryListener(new QueryListener() {
00174             public void changed(String name) {
00175                 if (name.equals("title")) {
00176                     _plot.setTitle(_wideQuery.getStringValue("title"));
00177                 } else if (name.equals("xlabel")) {
00178                     _plot.setXLabel(_wideQuery.getStringValue("xlabel"));
00179                 } else if (name.equals("ylabel")) {
00180                     _plot.setYLabel(_wideQuery.getStringValue("ylabel"));
00181                 } else if (name.equals("xrange")) {
00182                     _plot
00183                             .read("XRange: "
00184                                     + _wideQuery.getStringValue("xrange"));
00185                 } else if (name.equals("xticks")) {
00186                     String spec = _wideQuery.getStringValue("xticks").trim();
00187                     _plot.read("XTicks: " + spec);
00188 
00189                     // FIXME: log axis format temporarily
00190                     // disabled, see above.
00191                     // if (spec.equals("")) {
00192                     //    _narrowQuery.setEnabled("xlog", true);
00193                     // } else {
00194                     //    _narrowQuery.setBoolean("xlog", false);
00195                     //    _narrowQuery.setEnabled("xlog", false);
00196                     // }
00197                 } else if (name.equals("yticks")) {
00198                     String spec = _wideQuery.getStringValue("yticks").trim();
00199                     _plot.read("YTicks: " + spec);
00200 
00201                     // FIXME: log axis format temporarily
00202                     // disabled, see above.
00203                     // if (spec.equals("")) {
00204                     //    _narrowQuery.setEnabled("ylog", true);
00205                     // } else {
00206                     //    _narrowQuery.setBoolean("ylog", false);
00207                     //    _narrowQuery.setEnabled("ylog", false);
00208                     // }
00209                 } else if (name.equals("yrange")) {
00210                     _plot
00211                             .read("YRange: "
00212                                     + _wideQuery.getStringValue("yrange"));
00213                 } else if (name.equals("marks")) {
00214                     ((Plot) _plot).setMarksStyle(_wideQuery
00215                             .getStringValue("marks"));
00216                 }
00217 
00218                 _plot.repaint();
00219             }
00220         });
00221 
00222         _narrowQuery.addQueryListener(new QueryListener() {
00223             public void changed(String name) {
00224                 if (name.equals("grid")) {
00225                     _plot.setGrid(_narrowQuery.getBooleanValue("grid"));
00226                 } else if (name.equals("stems")) {
00227                     ((Plot) _plot).setImpulses(_narrowQuery
00228                             .getBooleanValue("stems"));
00229                     _plot.repaint();
00230                 } else if (name.equals("color")) {
00231                     _plot.setColor(_narrowQuery.getBooleanValue("color"));
00232 
00233                     // FIXME: log axis format temporarily
00234                     // disabled, see above.
00235                     // } else if (name.equals("xlog")) {
00236                     //    _plot.setXLog(_narrowQuery.getBooleanValue("xlog"));
00237                     // } else if (name.equals("ylog")) {
00238                     //    _plot.setYLog(_narrowQuery.getBooleanValue("ylog"));
00239                 } else if (name.equals("connected")) {
00240                     _setConnected(_narrowQuery.getBooleanValue("connected"));
00241                 }
00242 
00243                 _plot.repaint();
00244             }
00245         });
00246     }
00247 
00250 
00254     public void apply() {
00255         // Apply current values.
00256         _plot.setTitle(_wideQuery.getStringValue("title"));
00257         _plot.setXLabel(_wideQuery.getStringValue("xlabel"));
00258         _plot.setYLabel(_wideQuery.getStringValue("ylabel"));
00259         _plot.read("XRange: " + _wideQuery.getStringValue("xrange"));
00260         _plot.read("YRange: " + _wideQuery.getStringValue("yrange"));
00261         _plot.setGrid(_narrowQuery.getBooleanValue("grid"));
00262         _plot.setColor(_narrowQuery.getBooleanValue("color"));
00263 
00264         // FIXME: log axis format temporarily disable, see above.
00265         // _plot.setXLog(_narrowQuery.getBooleanValue("xlog"));
00266         // _plot.setYLog(_narrowQuery.getBooleanValue("ylog"));
00267         if (_plot instanceof Plot) {
00268             Plot cplot = (Plot) _plot;
00269             cplot.setMarksStyle(_wideQuery.getStringValue("marks"));
00270             cplot.setImpulses(_narrowQuery.getBooleanValue("stems"));
00271             _setConnected(_narrowQuery.getBooleanValue("connected"));
00272         }
00273 
00274         // FIXME: log axis format temporarily disable, see above.
00275         // String spec = _wideQuery.getStringValue("xticks").trim();
00276         // _plot.read("XTicks: " + spec);
00277         // if (spec.equals("")) {
00278         //    _narrowQuery.setEnabled("xlog", true);
00279         // } else {
00280         //    _narrowQuery.setBoolean("xlog", false);
00281         //    _narrowQuery.setEnabled("xlog", false);
00282         // }
00283         // spec = _wideQuery.getStringValue("yticks").trim();
00284         // _plot.read("YTicks: " + spec);
00285         // if (spec.equals("")) {
00286         //    _narrowQuery.setEnabled("ylog", true);
00287         // } else {
00288         //    _narrowQuery.setBoolean("ylog", false);
00289         //    _narrowQuery.setEnabled("ylog", false);
00290         // }
00291         _plot.repaint();
00292     }
00293 
00296     public void openModal() {
00297         String[] buttons = { "Apply", "Cancel" };
00298 
00299         // NOTE: If the plot is in a top-level container that is a Frame
00300         // (as opposed to an applet), then tell the dialog that the Frame
00301         // owns the dialog.
00302         Container toplevel = _plot.getTopLevelAncestor();
00303         Frame frame = null;
00304 
00305         if (toplevel instanceof Frame) {
00306             frame = (Frame) toplevel;
00307         }
00308 
00309         ComponentDialog dialog = new ComponentDialog(frame, "Set plot format",
00310                 this, buttons);
00311 
00312         if (dialog.buttonPressed().equals("Apply")) {
00313             apply();
00314         } else {
00315             restore();
00316         }
00317     }
00318 
00322     public void restore() {
00323         // Restore _original values.
00324         _plot.setTitle(_originalTitle);
00325         _plot.setXLabel(_originalXLabel);
00326         _plot.setYLabel(_originalYLabel);
00327         _plot.setXRange(_originalXRange[0], _originalXRange[1]);
00328         _plot.setYRange(_originalYRange[0], _originalYRange[1]);
00329         _plot.setGrid(_originalGrid);
00330         _plot.setColor(_originalColor);
00331 
00332         // FIXME: log axis format temporarily disable, see above.
00333         // _plot.setXLog(_originalXLog);
00334         // _plot.setYLog(_originalYLog);
00335         if (_plot instanceof Plot) {
00336             Plot cplot = (Plot) _plot;
00337             cplot.setMarksStyle(_originalMarks);
00338             cplot.setImpulses(_originalStems);
00339             _restoreConnected();
00340         }
00341 
00342         // FIXME: log axis format temporarily disabled, see above.
00343         // _plot.read("XTicks: " + _originalXTicksSpec);
00344         // if (_originalXTicksSpec.equals("")) {
00345         //    _narrowQuery.setEnabled("xlog", true);
00346         // } else {
00347         //   _narrowQuery.setBoolean("xlog", false);
00348         //    _narrowQuery.setEnabled("xlog", false);
00349         // }
00350         // _plot.read("YTicks: " + _originalYTicksSpec);
00351         // if (_originalYTicksSpec.equals("")) {
00352         //    _narrowQuery.setEnabled("ylog", true);
00353         // } else {
00354         //    _narrowQuery.setBoolean("ylog", false);
00355         //    _narrowQuery.setEnabled("ylog", false);
00356         // }
00357         _plot.repaint();
00358     }
00359 
00362 
00364     protected final PlotBox _plot;
00365 
00368     // Save the current connected state of all the point currently in the
00369     // plot.  NOTE: This method reaches into the protected members of
00370     // the Plot class, taking advantage of the fact that this class is
00371     // in the same package.
00372     private void _saveConnected() {
00373         Vector points = ((Plot) _plot)._points;
00374         _originalConnected = new boolean[points.size()][];
00375         _originalPoints = new PlotPoint[points.size()][];
00376 
00377         for (int dataset = 0; dataset < points.size(); dataset++) {
00378             Vector pts = (Vector) points.elementAt(dataset);
00379             _originalConnected[dataset] = new boolean[pts.size()];
00380             _originalPoints[dataset] = new PlotPoint[pts.size()];
00381 
00382             for (int i = 0; i < pts.size(); i++) {
00383                 PlotPoint pt = (PlotPoint) pts.elementAt(i);
00384                 _originalConnected[dataset][i] = pt.connected;
00385                 _originalPoints[dataset][i] = pt;
00386             }
00387         }
00388     }
00389 
00390     // Set the current connected state of all the point in the
00391     // plot.  NOTE: This method reaches into the protected members of
00392     // the Plot class, taking advantage of the fact that this class is
00393     // in the same package.
00394     private void _setConnected(boolean value) {
00395         Vector points = ((Plot) _plot)._points;
00396 
00397         // Make sure the default matches.
00398         ((Plot) _plot).setConnected(value);
00399 
00400         boolean[][] result = new boolean[points.size()][];
00401 
00402         for (int dataset = 0; dataset < points.size(); dataset++) {
00403             Vector pts = (Vector) points.elementAt(dataset);
00404             result[dataset] = new boolean[pts.size()];
00405 
00406             boolean first = true;
00407 
00408             for (int i = 0; i < pts.size(); i++) {
00409                 PlotPoint pt = (PlotPoint) pts.elementAt(i);
00410                 pt.connected = value && !first;
00411                 first = false;
00412             }
00413         }
00414     }
00415 
00416     // Restore the connected state of all the point that were in the
00417     // plot when their connected state was saved.
00418     // NOTE: This method reaches into the protected members of
00419     // the plot class, taking advantage of the fact that this class is
00420     // in the same package.
00421     private void _restoreConnected() {
00422         for (int dataset = 0; dataset < _originalPoints.length; dataset++) {
00423             for (int i = 0; i < _originalPoints[dataset].length; i++) {
00424                 PlotPoint pt = _originalPoints[dataset][i];
00425                 pt.connected = _originalConnected[dataset][i];
00426             }
00427         }
00428     }
00429 
00432     // Query widgets.
00433     private Query _wideQuery;
00434 
00437     // Query widgets.
00438     private Query _narrowQuery;
00439 
00440     // Original configuration of the plot.
00441     private String _originalTitle;
00442 
00443     // Original configuration of the plot.
00444     private String _originalXLabel;
00445 
00446     // Original configuration of the plot.
00447     private String _originalYLabel;
00448 
00449     // Original configuration of the plot.
00450     private String _originalMarks;
00451 
00452     // Original configuration of the plot.
00453     private String _originalXTicksSpec;
00454 
00455     // Original configuration of the plot.
00456     private String _originalYTicksSpec;
00457 
00458     private double[] _originalXRange;
00459 
00460     private double[] _originalYRange;
00461 
00462     private Vector[] _originalXTicks;
00463 
00464     private Vector[] _originalYTicks;
00465 
00466     private boolean _originalGrid;
00467 
00468     private boolean _originalStems;
00469 
00470     private boolean _originalColor;
00471 
00472     private boolean[][] _originalConnected;
00473 
00474     private PlotPoint[][] _originalPoints;
00475 }