1
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: AddVariable.java 1929 2005-06-19 05:04:46Z duns $
21 */
22 public class AddVariable extends WiredEdit implements GraphicsPanelEdit {
23
24 private static Logger logger = Logger.getLogger(AddVariable.class.getPackage().getName());
25 private String name;
26 private double v;
27
28 /***
29 * Creates a variable edit.
30 */
31 public AddVariable(String name) {
32 this(name, 0);
33 }
34
35 /***
36 * Creates a variable edit.
37 */
38 public AddVariable(String name, double v) {
39 super();
40 this.name = name;
41 this.v = v;
42 }
43
44 public WiredEdit copy(RecordPlot plot) {
45 WiredEdit copy = new AddVariable(name, v);
46 copy.setRecordPlot(plot);
47 return copy;
48 }
49
50 public String getPresentationName() {
51 StringBuffer s = new StringBuffer();
52 s.append(toString());
53 s.append(" ");
54 s.append(name);
55 s.append("=");
56 s.append(format.format(v));
57 return s.toString();
58 }
59
60 public String toString() {
61 return "AddVariable";
62 }
63
64 public boolean addEdit(UndoableEdit edit) {
65 if (edit instanceof AddVariable) {
66 AddVariable addVariable = (AddVariable)edit;
67 if (name.equals(addVariable.name)) {
68 v += addVariable.v;
69 return true;
70 }
71 }
72 return false;
73 }
74
75 public boolean isSupportedBy(GraphicsPanel p) {
76 NamedVariables variables = (NamedVariables)p.getFeature(NamedVariables.class);
77 if (variables != null) {
78 return (variables.exists(name));
79 }
80 return false;
81 }
82
83 protected void redoEdit() {
84 GraphicsPanel panel = getRecordPlot().getGraphicsPanel();
85 NamedVariables variables = (NamedVariables)panel.getFeature(NamedVariables.class);
86 if ((variables != null) && variables.exists(name)) {
87 variables.addVariable(name, v);
88 } else {
89 System.err.println("Could not redo: "+this);
90 return;
91 }
92 getRecordPlot().repaint();
93 logger.finer(toString());
94 }
95
96 protected void undoEdit() {
97 GraphicsPanel panel = getRecordPlot().getGraphicsPanel();
98 NamedVariables variables = (NamedVariables)panel.getFeature(NamedVariables.class);
99 if ((variables != null) && variables.exists(name) && (v != 0)) {
100 variables.addVariable(name, -v);
101 } else {
102 System.err.println("Could not undo: "+this);
103 return;
104 }
105 getRecordPlot().repaint();
106 logger.finer(toString());
107 }
108
109 public String getName() {
110 return name;
111 }
112 }