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