View Javadoc

1   // Copyright 2003-2005, FreeHEP.
2   package hep.wired.heprep.edit;
3   
4   import java.awt.*;
5   import java.awt.geom.*;
6   import java.util.logging.*;
7   import javax.swing.*;
8   import javax.swing.undo.*;
9   
10  import hep.wired.services.RecordPlot;
11  import hep.wired.services.GraphicsPanel;
12  import hep.wired.edit.WiredEdit;
13  import hep.wired.edit.GraphicsPanelEdit;
14  
15  import hep.wired.heprep.feature.NamedVariables;
16  
17  /***
18   *
19   * @author Mark Donszelmann
20   * @version $Id: SetVariable.java 1929 2005-06-19 05:04:46Z duns $
21   */
22  public class SetVariable extends WiredEdit implements GraphicsPanelEdit {
23  
24      private static Logger logger = Logger.getLogger(SetVariable.class.getPackage().getName());
25      private String name;
26      private double v;
27      private double previousV;
28  
29      /***
30       * Creates a variable edit.
31       */
32      public SetVariable(String name) {
33          this(name, 0);
34      }
35  
36      /***
37       * Creates a variable edit.
38       */
39      public SetVariable(String name, double v) {
40          super();
41          this.name = name;
42          this.v = v;
43      }
44      
45      public WiredEdit copy(RecordPlot plot) {
46          WiredEdit copy = new SetVariable(name, v);
47          copy.setRecordPlot(plot);
48          return copy;
49      }
50      
51      public String getPresentationName() {
52          StringBuffer s = new StringBuffer();
53          s.append(toString());
54          s.append(" ");
55          s.append(name);
56          s.append("=");
57          s.append(format.format(v));
58          return s.toString();
59      }
60  
61      public String toString() {
62          return "SetVariable";
63      }
64  
65      public boolean addEdit(UndoableEdit edit) {
66          if (edit instanceof SetVariable) {
67              SetVariable setVariable = (SetVariable)edit;
68              if (name.equals(setVariable.name)) {
69                  v = setVariable.v;
70                  return true;
71              }
72          }
73          return false;
74      }
75  
76      public boolean isSupportedBy(GraphicsPanel p) {
77          NamedVariables variables = (NamedVariables)p.getFeature(NamedVariables.class);
78          if (variables != null) {
79              return (variables.exists(name));
80          }
81          return false;
82      }
83  
84      protected void redoEdit() {
85          GraphicsPanel panel = getRecordPlot().getGraphicsPanel();
86          NamedVariables variables = (NamedVariables)panel.getFeature(NamedVariables.class);
87          if ((variables != null) && variables.exists(name)) {
88              previousV = variables.getDoubleVariable(name);
89              variables.setVariable(name, v);
90          } else {
91              System.err.println("Could not redo: "+this);
92              return;
93          }
94          getRecordPlot().repaint();
95          logger.finer(toString());
96      }
97  
98      protected void undoEdit() {
99          GraphicsPanel panel = getRecordPlot().getGraphicsPanel();
100         NamedVariables variables = (NamedVariables)panel.getFeature(NamedVariables.class);
101         if ((variables != null) && variables.exists(name)) {
102             variables.setVariable(name, previousV);
103         } else {
104             System.err.println("Could not undo: "+this);
105             return;
106         }
107         getRecordPlot().repaint();
108         logger.finer(toString());
109     }
110     
111     public String getName() {
112         return name;
113     }
114 }