1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.plugin.views.outline;
13
14 import org.eclipse.jface.action.IStatusLineManager;
15 import org.eclipse.jface.resource.ImageRegistry;
16 import org.eclipse.swt.graphics.Image;
17 import org.w3c.dom.Attr;
18 import org.w3c.dom.Element;
19 import org.w3c.dom.Node;
20
21 import sk.baka.ikslibs.interval.DOMIntervalSet;
22 import sk.uniba.euromath.document.XMLAccess;
23 import sk.uniba.euromath.editor.selections.DOMSelectionChangedEvent;
24 import sk.uniba.euromath.editor.selections.IDOMSelectionChangedListener;
25 import sk.uniba.euromath.editor.selections.IDOMSelectionProvider;
26
27 /***
28 * <code>StatusLineUpdateManager</code> class is responsible for status line
29 * responses to selection changes in outline view.
30 *
31 * @author TV Created on 26.5.2004
32 *
33 */
34
35 public class StatusLineUpdateManager extends Object {
36
37 protected IStatusLineManager statusLine;
38
39 protected XMLAccess xmlAccess;
40
41 private IDOMSelectionChangedListener selectionHandler = new IDOMSelectionChangedListener() {
42 public void selectionChanged(DOMSelectionChangedEvent event) {
43 initialize();
44 handleSelection(event);
45 }
46 };;
47
48 protected DOMIntervalSet selection;
49
50 private ImageRegistry registry = null;
51
52 protected Image statusImage = null;
53
54 /***
55 * @param manager
56 */
57 public StatusLineUpdateManager(IStatusLineManager manager, XMLAccess access) {
58 super();
59 this.statusLine = manager;
60 setXMLAccess(access);
61 }
62
63 /***
64 * @return Returns the registry.
65 */
66 protected ImageRegistry getRegistry() {
67 return this.registry;
68 }
69
70 /***
71 * @param registry
72 * The registry to set.
73 */
74 protected void setRegistry(ImageRegistry registry) {
75 this.registry = registry;
76 }
77
78 /***
79 * @return Returns the statusImage.
80 */
81 protected Image getStatusImage() {
82 return this.statusImage;
83 }
84
85 /***
86 * @param statusImage
87 * The statusImage to set.
88 */
89 protected void setStatusImage(Image statusImage) {
90 this.statusImage = statusImage;
91 }
92
93 /***
94 * @return Returns the statusLine.
95 */
96 protected IStatusLineManager getStatusLine() {
97 return this.statusLine;
98 }
99
100 /***
101 * @param statusLine
102 * The statusLine to set.
103 */
104 protected void setStatusLine(IStatusLineManager statusLine) {
105 this.statusLine = statusLine;
106 }
107
108 /***
109 * @return Returns the xmlAccess.
110 */
111 protected XMLAccess getXmlAccess() {
112 return this.xmlAccess;
113 }
114
115 /***
116 * @param xmlAccess
117 * The xmlAccess to set.
118 */
119 protected void setXmlAccess(XMLAccess xmlAccess) {
120 this.xmlAccess = xmlAccess;
121 }
122
123 /***
124 * @param attr
125 * @return
126 */
127 private String buildAttributePath(Attr attr) {
128
129 return buildElementPath(attr.getOwnerElement()) + "."
130 + attr.getNodeName() + "=" + attr.getValue();
131 }
132
133 /***
134 * @param element
135 * @return
136 */
137 protected String buildElementPath(Element element) {
138 String result = element.getLocalName();
139 while ((element.getParentNode() != null)
140 && (element.getParentNode() instanceof Element)) {
141 element = (Element) element.getParentNode();
142 result = element.getLocalName() + "/" + result;
143 }
144 return result;
145 }
146
147 protected boolean canShow() {
148 return (getStatusLine() != null) && (getXmlAccess() != null);
149 }
150
151 public final void connect(IDOMSelectionProvider provider) {
152 if (provider != null) {
153 provider.addSelectionChangedListener(getSelectionHandler());
154 }
155 }
156
157 protected String createMsgForMixed() {
158 int attrs = 0;
159 int cdats = 0;
160 int comms = 0;
161 int elems = 0;
162 int proci = 0;
163 int texts = 0;
164 int unknw = 0;
165
166 String result = "";
167
168
169 String nodeID;
170 Node node;
171
172
173
174
175
176
177
178
179
180
181
182
183
184 if (attrs > 0) {
185 if (result.length() > 0) {
186 result += ", ";
187 }
188 if (attrs == 1) {
189 result += "1 attribute";
190 } else {
191 result += attrs + " attributes";
192 }
193 }
194
195 if (cdats > 0) {
196 if (result.length() > 0) {
197 result += ", ";
198 }
199 if (cdats == 1) {
200 result += "1 cdata section";
201 } else {
202 result += cdats + " cdata sections";
203 }
204 }
205
206 if (comms > 0) {
207 if (result.length() > 0) {
208 result += ", ";
209 }
210 if (comms == 1) {
211 result += "1 comment";
212 } else {
213 result += comms + " comments";
214 }
215 }
216
217 if (elems > 0) {
218 if (result.length() > 0) {
219 result += ", ";
220 }
221 if (elems == 1) {
222 result += "1 element";
223 } else {
224 result += elems + " elements";
225 }
226 }
227
228 if (proci > 0) {
229 if (result.length() > 0) {
230 result += ", ";
231 }
232 if (proci == 1) {
233 result += "1 processing instruction";
234 } else {
235 result += proci + " processing instructions";
236 }
237 }
238
239 if (texts > 0) {
240 if (result.length() > 0) {
241 result += ", ";
242 }
243 if (texts == 1) {
244 result += "1 text node";
245 } else {
246 result += texts + " text nodes";
247 }
248 }
249
250 if (unknw > 0) {
251 if (result.length() > 0) {
252 result += ", ";
253 }
254 if (unknw == 1) {
255 result += "1 unknown item";
256 } else {
257 result += unknw + " unknown items";
258 }
259 }
260
261 return result;
262 }
263
264 /***
265 * @return
266 */
267 protected String createMsgForMulti() {
268 String result = "";
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289 return result;
290 }
291
292 /***
293 * @return
294 */
295 protected String createMsgForSingle() {
296 String result = "";
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 return result;
317 }
318
319 public final void disconnect(IDOMSelectionProvider provider) {
320 if (provider != null) {
321 provider.removeSelectionChangedListener(getSelectionHandler());
322 }
323 }
324
325 protected Image findImage(String key) {
326 if (getImageRegistry() != null) {
327 return getImageRegistry().get(key);
328 }
329 return null;
330 }
331
332 public ImageRegistry getImageRegistry() {
333 return this.registry;
334 }
335
336 protected IDOMSelectionChangedListener getSelectionHandler() {
337 return this.selectionHandler;
338 }
339
340 protected void handle(DOMIntervalSet selection) {
341
342
343
344
345
346
347
348
349
350
351
352 }
353
354 /***
355 * Getter for selection.
356 *
357 * @return
358 */
359 protected DOMIntervalSet getDOMSelection() {
360 return this.selection;
361 }
362
363 protected void handleSelection(DOMSelectionChangedEvent event) {
364 if (!canShow())
365 return;
366
367 handle( event.getDOMSelection());
368 }
369
370 protected void initialize() {
371 setStatusImage(null);
372 }
373
374 public void setImageRegistry(ImageRegistry registry) {
375 this.registry = registry;
376 }
377
378 /***
379 * @param access
380 */
381 public void setXMLAccess(XMLAccess access) {
382 this.xmlAccess = access;
383 }
384
385 public void show(DOMIntervalSet selection) {
386 if (selection != null) {
387 handle(selection);
388 this.statusLine.update(true);
389 }
390 }
391
392 /***
393 * @param message
394 */
395 protected void showMessage(String message) {
396 getStatusLine().setErrorMessage(null);
397 getStatusLine().setMessage(getStatusImage(), message);
398 }
399
400 }