JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / fr / orsay / lri / varna / applications / templateEditor / TemplateEdits.java
1 package fr.orsay.lri.varna.applications.templateEditor;
2
3 import java.awt.geom.Point2D;
4
5 import javax.swing.undo.AbstractUndoableEdit;
6 import javax.swing.undo.CannotRedoException;
7 import javax.swing.undo.CannotUndoException;
8 import javax.swing.undo.UndoableEdit;
9
10 public class TemplateEdits {
11         public static final double MAX_DISTANCE= 15.0;
12                 
13         public static  class ElementAddTemplateEdit extends AbstractUndoableEdit
14         {
15                 private GraphicalTemplateElement _h;
16                 private TemplatePanel _p;
17                 public ElementAddTemplateEdit(GraphicalTemplateElement h,TemplatePanel p)
18                 {
19                         _h = h;
20                         _p = p;
21                 }
22                 public void undo() throws CannotUndoException {
23                         _p.removeElement(_h);
24                         _p.repaint();                   
25                 }
26                 public void redo() throws CannotRedoException {
27                         _p.addElement(_h);
28                         _p.repaint();
29                 }
30                 public boolean canUndo() { return true; }
31                 public boolean canRedo() { return true; }
32                 public String getPresentationName() { return "Graphical element added"; }
33         };
34         public static  class ElementRemoveTemplateEdit extends AbstractUndoableEdit
35         {
36                 private GraphicalTemplateElement _h;
37                 private TemplatePanel _p;
38                 public ElementRemoveTemplateEdit(GraphicalTemplateElement h,TemplatePanel p)
39                 {
40                         _h = h;
41                         _p = p;
42                 }
43                 public void undo() throws CannotUndoException {
44                         _p.addElement(_h);
45                         _p.repaint();                   
46                 }
47                 public void redo() throws CannotRedoException {
48                         _p.removeElement(_h);
49                         _p.repaint();
50                 }
51                 public boolean canUndo() { return true; }
52                 public boolean canRedo() { return true; }
53                 public String getPresentationName() { return "Graphical element removed"; }
54         };
55         public static  class ElementAttachTemplateEdit extends AbstractUndoableEdit
56         {
57                 Connection _c;
58                 private TemplatePanel _p;
59                 public ElementAttachTemplateEdit(Connection c,
60                                 TemplatePanel p)
61                 {
62                         _c = c;
63                         _p = p;
64                 }
65                 public void undo() throws CannotUndoException {
66                         _p.removeConnection(_c);
67                         _p.repaint();                   
68                 }
69                 public void redo() throws CannotRedoException {
70                         _c = _p.addConnection(_c._h1,_c._edge1,_c._h2,_c._edge2);
71                         _p.repaint();
72                 }
73                 public boolean canUndo() { return true; }
74                 public boolean canRedo() { return true; }
75                 public String getPresentationName() { return "Graphical elements attached"; }
76         };
77         public static  class ElementDetachTemplateEdit extends AbstractUndoableEdit
78         {
79                 Connection _c;
80                 private TemplatePanel _p;
81                 public ElementDetachTemplateEdit(Connection c,
82                                 TemplatePanel p)
83                 {
84                         _c = c;
85                         _p = p;
86                 }
87                 public void undo() throws CannotUndoException {
88                         _c = _p.addConnection(_c._h1,_c._edge1,_c._h2,_c._edge2);
89                         _p.repaint();                   
90                 }
91                 public void redo() throws CannotRedoException {
92                         _p.removeConnection(_c);
93                         _p.repaint();
94                 }
95                 public boolean canUndo() { return true; }
96                 public boolean canRedo() { return true; }
97                 public String getPresentationName() { return "Graphical elements detached"; }
98         };
99         
100         public static  class ElementEdgeMoveTemplateEdit extends AbstractUndoableEdit
101         {
102                 private GraphicalTemplateElement _ur;
103                 GraphicalTemplateElement.RelativePosition _edge;
104                 private double _ox; 
105                 private double _oy;
106                 private double _nx; 
107                 private double _ny;
108                 private TemplatePanel _p;
109                 public ElementEdgeMoveTemplateEdit(GraphicalTemplateElement ur, GraphicalTemplateElement.RelativePosition edge, double nx, double ny, TemplatePanel p)
110                 {
111                         _ur = ur;
112                         _edge = edge;
113                         _ox = ur.getEdgePosition(edge).x;
114                         _oy = ur.getEdgePosition(edge).y;
115                         _nx = nx;
116                         _ny = ny;
117                         _p = p;
118                 }
119                 public void undo() throws CannotUndoException {
120                         _ur.setEdgePosition(_edge,new Point2D.Double(_ox,_oy));
121                         _p.repaint();                   
122                 }
123                 public void redo() throws CannotRedoException {
124                         _ur.setEdgePosition(_edge,new Point2D.Double(_nx,_ny));
125                         _p.repaint();
126                 }
127                 public boolean canUndo() { return true; }
128                 public boolean canRedo() { return true; }
129                 public String getPresentationName() { return "Edge moved "+_edge; }
130                 public boolean addEdit(UndoableEdit anEdit)
131                 {
132                         if (anEdit instanceof ElementEdgeMoveTemplateEdit)
133                         {
134                                 ElementEdgeMoveTemplateEdit e = (ElementEdgeMoveTemplateEdit) anEdit;
135                                 if (e._edge==_edge)
136                                 {
137                                         Point2D.Double po1 = new Point2D.Double(_ox,_oy);
138                                         Point2D.Double pn1 = new Point2D.Double(_nx,_ny);
139                                         Point2D.Double po2 = new Point2D.Double(e._ox,e._oy);
140                                         Point2D.Double pn2 = new Point2D.Double(e._nx,e._ny);
141                                         if ((_ur==e._ur)&&(pn1.equals(po2))&&(po1.distance(pn2)<MAX_DISTANCE))
142                                         {
143                                                 _nx = e._nx;
144                                                 _ny = e._ny;
145                                                 return true;
146                                         }
147                                 }
148                         }
149                         return false;
150                 }
151         };
152         public static  class HelixFlipTemplateEdit extends AbstractUndoableEdit
153         {
154                 private Helix _h;
155                 private TemplatePanel _p;
156                 public HelixFlipTemplateEdit(Helix h, TemplatePanel p)
157                 {
158                         _h = h;
159                         _p = p;
160                 }
161                 public void undo() throws CannotUndoException {
162                         _h.toggleFlipped();
163                         _p.repaint();                   
164                 }
165                 public void redo() throws CannotRedoException {
166                         _h.toggleFlipped();
167                         _p.repaint();
168                 }
169                 public boolean canUndo() { return true; }
170                 public boolean canRedo() { return true; }
171                 public String getPresentationName() { return "Helix flipped "; }
172         };
173         
174 }