1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.tool;
19
20 import org.eclipse.gef.GraphicalEditPart;
21 import org.eclipse.gef.editparts.ZoomListener;
22 import org.eclipse.gef.editparts.ZoomManager;
23 import org.eclipse.gef.tools.DirectEditManager;
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.viewers.CellEditor;
26 import org.eclipse.jface.viewers.TextCellEditor;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.graphics.Font;
29 import org.eclipse.swt.graphics.FontData;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Text;
32 import org.eclipse.ui.IActionBars;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.actions.ActionFactory;
35 import org.eclipse.ui.part.CellEditorActionHandler;
36
37 import pl.edu.agh.cast.figure.NodeFigure;
38
39
40
41
42
43
44 public class LabelEditManager extends DirectEditManager {
45
46 private IActionBars actionBars;
47
48 private CellEditorActionHandler actionHandler;
49
50 private IAction copy;
51
52 private IAction cut;
53
54 private IAction paste;
55
56 private IAction undo;
57
58 private IAction redo;
59
60 private IAction find;
61
62 private IAction selectAll;
63
64 private IAction delete;
65
66 private double cachedZoom = -1.0;
67
68 private Font scaledFont;
69
70
71
72
73 private NodeFigure figure;
74
75 private ZoomListener zoomListener = new ZoomListener() {
76 public void zoomChanged(double newZoom) {
77 updateScaledFont(newZoom);
78 }
79 };
80
81
82
83
84
85
86
87
88
89 public LabelEditManager(GraphicalEditPart source, LabelCellEditorLocator locator) {
90 super(source, null, locator);
91 figure = locator.getNodeFigure();
92 }
93
94
95
96
97 @Override
98 protected void bringDown() {
99 ZoomManager zoomMgr = (ZoomManager)getEditPart().getViewer().getProperty(ZoomManager.class.toString());
100 if (zoomMgr != null) {
101 zoomMgr.removeZoomListener(zoomListener);
102 }
103
104 if (actionHandler != null) {
105 actionHandler.dispose();
106 actionHandler = null;
107 }
108 if (actionBars != null) {
109 restoreSavedActions(actionBars);
110 actionBars.updateActionBars();
111 actionBars = null;
112 }
113
114 super.bringDown();
115
116 disposeScaledFont();
117 }
118
119 @Override
120 protected CellEditor createCellEditorOn(Composite composite) {
121 return new TextCellEditor(composite, SWT.SINGLE
122 }
123
124 private void disposeScaledFont() {
125 if (scaledFont != null) {
126 scaledFont.dispose();
127 scaledFont = null;
128 }
129 }
130
131 @Override
132 protected void initCellEditor() {
133
134 NodeFigure stickyNote = figure;
135 getCellEditor().setValue(stickyNote.getLabel());
136
137 ZoomManager zoomMgr = (ZoomManager)getEditPart().getViewer().getProperty(ZoomManager.class.toString());
138 if (zoomMgr != null) {
139
140 cachedZoom = -1.0;
141 updateScaledFont(zoomMgr.getZoom());
142 zoomMgr.addZoomListener(zoomListener);
143 } else {
144 getCellEditor().getControl().setFont(stickyNote.getFont());
145 }
146
147
148
149
150 actionBars = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()
151 .getEditorSite().getActionBars();
152 saveCurrentActions(actionBars);
153 actionHandler = new CellEditorActionHandler(actionBars);
154 actionHandler.addCellEditor(getCellEditor());
155 actionBars.updateActionBars();
156 }
157
158 private void restoreSavedActions(IActionBars actionBar) {
159 actionBar.setGlobalActionHandler(ActionFactory.COPY.getId(), copy);
160 actionBar.setGlobalActionHandler(ActionFactory.PASTE.getId(), paste);
161 actionBar.setGlobalActionHandler(ActionFactory.DELETE.getId(), delete);
162 actionBar.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), selectAll);
163 actionBar.setGlobalActionHandler(ActionFactory.CUT.getId(), cut);
164 actionBar.setGlobalActionHandler(ActionFactory.FIND.getId(), find);
165 actionBar.setGlobalActionHandler(ActionFactory.UNDO.getId(), undo);
166 actionBar.setGlobalActionHandler(ActionFactory.REDO.getId(), redo);
167 }
168
169 private void saveCurrentActions(IActionBars actionBar) {
170 copy = actionBar.getGlobalActionHandler(ActionFactory.COPY.getId());
171 paste = actionBar.getGlobalActionHandler(ActionFactory.PASTE.getId());
172 delete = actionBar.getGlobalActionHandler(ActionFactory.DELETE.getId());
173 selectAll = actionBar.getGlobalActionHandler(ActionFactory.SELECT_ALL.getId());
174 cut = actionBar.getGlobalActionHandler(ActionFactory.CUT.getId());
175 find = actionBar.getGlobalActionHandler(ActionFactory.FIND.getId());
176 undo = actionBar.getGlobalActionHandler(ActionFactory.UNDO.getId());
177 redo = actionBar.getGlobalActionHandler(ActionFactory.REDO.getId());
178 }
179
180 private void updateScaledFont(double zoom) {
181 if (cachedZoom == zoom) {
182 return;
183 }
184
185 Text text = (Text)getCellEditor().getControl();
186 Font font = figure.getFont();
187
188 disposeScaledFont();
189 cachedZoom = zoom;
190 if (zoom == 1.0) {
191 text.setFont(font);
192 } else {
193 FontData fd = font.getFontData()[0];
194 fd.setHeight((int)(fd.getHeight() * zoom));
195 scaledFont = new Font(null, fd);
196 text.setFont(scaledFont);
197 }
198 }
199 }