Merge branch 'Jalview-JS/jim/JAL-3253-JAL-3418' into Jalview-JS/JAL-3253-applet
[jalview.git] / srcjar / fr / orsay / lri / varna / applications / templateEditor / TemplateEditorPanelUI.java
1 package fr.orsay.lri.varna.applications.templateEditor;
2
3 import java.awt.geom.Point2D;
4
5 import javax.swing.undo.UndoManager;
6 import javax.swing.undo.UndoableEditSupport;
7
8 import fr.orsay.lri.varna.applications.templateEditor.GraphicalTemplateElement.RelativePosition;
9 import fr.orsay.lri.varna.models.templates.RNATemplate;
10
11 public class TemplateEditorPanelUI {
12
13         private UndoableEditSupport _undoableEditSupport;
14         private TemplatePanel _tp;
15         private Tool selectedTool = Tool.CREATE_HELIX;
16         
17
18
19         public enum Tool {
20                 SELECT, CREATE_HELIX, CREATE_UNPAIRED
21         }
22
23         public TemplateEditorPanelUI(TemplatePanel tp)
24         {
25                 _tp = tp;
26                  _undoableEditSupport = new UndoableEditSupport(tp);
27         }
28         
29         public Tool getSelectedTool() {
30                 return selectedTool;
31         }
32
33         public void setSelectedTool(Tool selectedTool) {
34                 this.selectedTool = selectedTool;
35         }
36         
37         /* Generic undoable event firing for edge movement */
38         public void undoableEdgeMove(GraphicalTemplateElement h, GraphicalTemplateElement.RelativePosition edge,double nx, double ny)
39         {
40                 _undoableEditSupport.postEdit(new TemplateEdits.ElementEdgeMoveTemplateEdit( h,edge,nx,ny,_tp));
41                 h.setEdgePosition(edge, new Point2D.Double(nx,ny));
42                 _tp.repaint();          
43         }
44         
45         public void setEdge5UI(GraphicalTemplateElement h, double nx, double ny)
46         { undoableEdgeMove(h,GraphicalTemplateElement.RelativePosition.RP_CONNECT_START5, nx,ny); }     
47         public void setEdge3UI(UnpairedRegion h, double nx, double ny)
48         { undoableEdgeMove(h,GraphicalTemplateElement.RelativePosition.RP_CONNECT_END3, nx,ny); }       
49         public void setEdge5TangentUI(UnpairedRegion h, double nx, double ny)
50         { undoableEdgeMove(h,GraphicalTemplateElement.RelativePosition.RP_EDIT_TANGENT_5, nx,ny); }
51         public void setEdge3TangentUI(UnpairedRegion h, double nx, double ny)
52         { undoableEdgeMove(h,GraphicalTemplateElement.RelativePosition.RP_EDIT_TANGENT_3, nx,ny); }     
53         public void moveUnpairedUI(UnpairedRegion u, double nx, double ny)
54         { undoableEdgeMove(u,GraphicalTemplateElement.RelativePosition.RP_INNER_MOVE, nx,ny); } 
55         public void moveHelixUI(Helix h, double nx, double ny)
56         { undoableEdgeMove(h,GraphicalTemplateElement.RelativePosition.RP_INNER_MOVE, nx,ny); } 
57         public void setHelixPosUI(Helix h, double nx, double ny)
58         { undoableEdgeMove(h,GraphicalTemplateElement.RelativePosition.RP_EDIT_START, nx,ny); } 
59         public void setHelixExtentUI(Helix h, double nx, double ny)
60         { undoableEdgeMove(h,GraphicalTemplateElement.RelativePosition.RP_EDIT_END, nx,ny); }   
61         
62
63         public void addElementUI(GraphicalTemplateElement h)
64         {
65                 _undoableEditSupport.postEdit(new TemplateEdits.ElementAddTemplateEdit( h,_tp));
66                 _tp.addElement(h);
67         }
68
69         public void removeElementUI(GraphicalTemplateElement h)
70         {
71                 _undoableEditSupport.postEdit(new TemplateEdits.ElementRemoveTemplateEdit( h,_tp));
72                 _tp.removeElement(h);
73         }
74         
75
76         public void addUndoableEditListener(UndoManager manager)
77         {
78                 _undoableEditSupport.addUndoableEditListener(manager);
79         }
80         
81         public void addConnectionUI(GraphicalTemplateElement h1,
82                         GraphicalTemplateElement.RelativePosition e1,  
83                         GraphicalTemplateElement h2,
84                         GraphicalTemplateElement.RelativePosition e2)
85         {
86                 if (GraphicalTemplateElement.canConnect(h1, e1,h2, e2))
87                 {
88                 Connection c = _tp.addConnection(h1,e1,h2,e2);
89                 _undoableEditSupport.postEdit(new TemplateEdits.ElementAttachTemplateEdit(c,_tp));
90                 }
91         }
92
93         public void removeConnectionUI(Connection c)
94         {
95                 _undoableEditSupport.postEdit(new TemplateEdits.ElementDetachTemplateEdit(c,_tp));
96                 _tp.removeConnection(c);
97         }
98
99         public void flipHelixUI(Helix h)
100         {
101                           _undoableEditSupport.postEdit(new TemplateEdits.HelixFlipTemplateEdit(h,_tp));
102                           _tp.flip(h);
103                           _tp.repaint();
104         }
105         
106         public RNATemplate getTemplate()
107         {
108                 return _tp.getTemplate();
109         }
110         
111         
112 }