1 | /* |
2 | * Copyright 2009, SDQ, IPD, Uni Karlsruhe (TH) |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.resource.edit.policies; |
5 | |
6 | import java.util.Collections; |
7 | import java.util.List; |
8 | |
9 | import org.eclipse.draw2d.ColorConstants; |
10 | import org.eclipse.draw2d.Figure; |
11 | import org.eclipse.draw2d.FigureListener; |
12 | import org.eclipse.draw2d.Graphics; |
13 | import org.eclipse.draw2d.IFigure; |
14 | import org.eclipse.draw2d.Label; |
15 | import org.eclipse.draw2d.RectangleFigure; |
16 | import org.eclipse.draw2d.geometry.Rectangle; |
17 | import org.eclipse.gef.GraphicalEditPart; |
18 | import org.eclipse.gef.handles.MoveHandle; |
19 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.NonResizableEditPolicyEx; |
20 | import org.eclipse.gmf.runtime.diagram.ui.tools.DragEditPartsTrackerEx; |
21 | import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel; |
22 | |
23 | /** |
24 | * @generated |
25 | */ |
26 | public class PalladioComponentModelTextNonResizableEditPolicy extends |
27 | NonResizableEditPolicyEx { |
28 | |
29 | /** |
30 | * @generated |
31 | */ |
32 | private IFigure selectionFeedbackFigure; |
33 | |
34 | /** |
35 | * @generated |
36 | */ |
37 | private IFigure focusFeedbackFigure; |
38 | |
39 | /** |
40 | * @generated |
41 | */ |
42 | private FigureListener hostPositionListener; |
43 | |
44 | /** |
45 | * @generated |
46 | */ |
47 | protected void showPrimarySelection() { |
48 | if (getHostFigure() instanceof WrappingLabel) { |
49 | ((WrappingLabel) getHostFigure()).setSelected(true); |
50 | ((WrappingLabel) getHostFigure()).setFocus(true); |
51 | } else { |
52 | showSelection(); |
53 | showFocus(); |
54 | } |
55 | } |
56 | |
57 | /** |
58 | * @generated |
59 | */ |
60 | protected void showSelection() { |
61 | if (getHostFigure() instanceof WrappingLabel) { |
62 | ((WrappingLabel) getHostFigure()).setSelected(true); |
63 | ((WrappingLabel) getHostFigure()).setFocus(false); |
64 | } else { |
65 | hideSelection(); |
66 | addFeedback(selectionFeedbackFigure = createSelectionFeedbackFigure()); |
67 | getHostFigure().addFigureListener(getHostPositionListener()); |
68 | refreshSelectionFeedback(); |
69 | hideFocus(); |
70 | } |
71 | } |
72 | |
73 | /** |
74 | * @generated |
75 | */ |
76 | protected void hideSelection() { |
77 | if (getHostFigure() instanceof WrappingLabel) { |
78 | ((WrappingLabel) getHostFigure()).setSelected(false); |
79 | ((WrappingLabel) getHostFigure()).setFocus(false); |
80 | } else { |
81 | if (selectionFeedbackFigure != null) { |
82 | removeFeedback(selectionFeedbackFigure); |
83 | getHostFigure().removeFigureListener(getHostPositionListener()); |
84 | selectionFeedbackFigure = null; |
85 | } |
86 | hideFocus(); |
87 | } |
88 | } |
89 | |
90 | /** |
91 | * @generated |
92 | */ |
93 | protected void showFocus() { |
94 | if (getHostFigure() instanceof WrappingLabel) { |
95 | ((WrappingLabel) getHostFigure()).setFocus(true); |
96 | } else { |
97 | hideFocus(); |
98 | addFeedback(focusFeedbackFigure = createFocusFeedbackFigure()); |
99 | refreshFocusFeedback(); |
100 | } |
101 | } |
102 | |
103 | /** |
104 | * @generated |
105 | */ |
106 | protected void hideFocus() { |
107 | if (getHostFigure() instanceof WrappingLabel) { |
108 | ((WrappingLabel) getHostFigure()).setFocus(false); |
109 | } else { |
110 | if (focusFeedbackFigure != null) { |
111 | removeFeedback(focusFeedbackFigure); |
112 | focusFeedbackFigure = null; |
113 | } |
114 | } |
115 | } |
116 | |
117 | /** |
118 | * @generated |
119 | */ |
120 | protected Rectangle getFeedbackBounds() { |
121 | Rectangle bounds; |
122 | if (getHostFigure() instanceof Label) { |
123 | bounds = ((Label) getHostFigure()).getTextBounds(); |
124 | bounds.intersect(getHostFigure().getBounds()); |
125 | } else { |
126 | bounds = getHostFigure().getBounds().getCopy(); |
127 | } |
128 | getHostFigure().getParent().translateToAbsolute(bounds); |
129 | getFeedbackLayer().translateToRelative(bounds); |
130 | return bounds; |
131 | } |
132 | |
133 | /** |
134 | * @generated |
135 | */ |
136 | protected IFigure createSelectionFeedbackFigure() { |
137 | if (getHostFigure() instanceof Label) { |
138 | Label feedbackFigure = new Label(); |
139 | feedbackFigure.setOpaque(true); |
140 | feedbackFigure |
141 | .setBackgroundColor(ColorConstants.menuBackgroundSelected); |
142 | feedbackFigure |
143 | .setForegroundColor(ColorConstants.menuForegroundSelected); |
144 | return feedbackFigure; |
145 | } else { |
146 | RectangleFigure feedbackFigure = new RectangleFigure(); |
147 | feedbackFigure.setFill(false); |
148 | return feedbackFigure; |
149 | } |
150 | } |
151 | |
152 | /** |
153 | * @generated |
154 | */ |
155 | protected IFigure createFocusFeedbackFigure() { |
156 | return new Figure() { |
157 | |
158 | protected void paintFigure(Graphics graphics) { |
159 | graphics.drawFocus(getBounds().getResized(-1, -1)); |
160 | } |
161 | }; |
162 | } |
163 | |
164 | /** |
165 | * @generated |
166 | */ |
167 | protected void updateLabel(Label target) { |
168 | Label source = (Label) getHostFigure(); |
169 | target.setText(source.getText()); |
170 | target.setTextAlignment(source.getTextAlignment()); |
171 | target.setFont(source.getFont()); |
172 | } |
173 | |
174 | /** |
175 | * @generated |
176 | */ |
177 | protected void refreshSelectionFeedback() { |
178 | if (selectionFeedbackFigure != null) { |
179 | if (selectionFeedbackFigure instanceof Label) { |
180 | updateLabel((Label) selectionFeedbackFigure); |
181 | selectionFeedbackFigure.setBounds(getFeedbackBounds()); |
182 | } else { |
183 | selectionFeedbackFigure.setBounds(getFeedbackBounds().expand(5, |
184 | 5)); |
185 | } |
186 | } |
187 | } |
188 | |
189 | /** |
190 | * @generated |
191 | */ |
192 | protected void refreshFocusFeedback() { |
193 | if (focusFeedbackFigure != null) { |
194 | focusFeedbackFigure.setBounds(getFeedbackBounds()); |
195 | } |
196 | } |
197 | |
198 | /** |
199 | * @generated |
200 | */ |
201 | public void refreshFeedback() { |
202 | refreshSelectionFeedback(); |
203 | refreshFocusFeedback(); |
204 | } |
205 | |
206 | /** |
207 | * @generated |
208 | */ |
209 | private FigureListener getHostPositionListener() { |
210 | if (hostPositionListener == null) { |
211 | hostPositionListener = new FigureListener() { |
212 | public void figureMoved(IFigure source) { |
213 | refreshFeedback(); |
214 | } |
215 | }; |
216 | } |
217 | return hostPositionListener; |
218 | } |
219 | |
220 | /** |
221 | * @generated |
222 | */ |
223 | protected List createSelectionHandles() { |
224 | MoveHandle moveHandle = new MoveHandle((GraphicalEditPart) getHost()); |
225 | moveHandle.setBorder(null); |
226 | moveHandle.setDragTracker(new DragEditPartsTrackerEx(getHost())); |
227 | return Collections.singletonList(moveHandle); |
228 | } |
229 | } |