View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the License); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
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()) + "." //$NON-NLS-1$
130                 + attr.getNodeName() + "=" + attr.getValue(); //$NON-NLS-1$
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         // gather data:
169         String nodeID;
170         Node node;
171         // TODO !!
172         /*
173          * for (Iterator i = getSelection().getIDs().iterator(); i.hasNext();) {
174          * nodeID = (String) i.next(); try { node =
175          * xmlAccess.getIdManager().getNode(nodeID).item(0); switch
176          * (node.getNodeType()) { case Node.ATTRIBUTE_NODE: attrs++; break; case
177          * Node.CDATA_SECTION_NODE: cdats++; break; case Node.COMMENT_NODE:
178          * comms++; break; case Node.ELEMENT_NODE: elems++; break; case
179          * Node.PROCESSING_INSTRUCTION_NODE: proci++; break; case
180          * Node.TEXT_NODE: texts++; break; default: unknw++; break; } } catch
181          * (DocumentException de) { } }
182          */
183         // build result string:
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         // TODO !!
270         /*
271          * try { short nodeType = getXmlAccess().getIdManager().getNodeType(
272          * getSelection().getIDs().get(0)); result = getSelection().size() + " ";
273          * switch (nodeType) { case Node.ATTRIBUTE_NODE: result += "attributes";
274          * setStatusImage(findImage(OutlineConsts.ATTR_KEY)); break; case
275          * Node.CDATA_SECTION_NODE: result += "cdata sections";
276          * setStatusImage(findImage(OutlineConsts.CDAT_KEY)); break; case
277          * Node.COMMENT_NODE: result += "comments";
278          * setStatusImage(findImage(OutlineConsts.COMM_KEY)); break; case
279          * Node.ELEMENT_NODE: result += "elements";
280          * setStatusImage(findImage(OutlineConsts.ELEM_KEY)); break; case
281          * Node.ENTITY_REFERENCE_NODE: result += "entities";
282          * setStatusImage(findImage(OutlineConsts.ENTI_KEY)); break; case
283          * Node.PROCESSING_INSTRUCTION_NODE: result += "processing
284          * instructions"; setStatusImage(findImage(OutlineConsts.INST_KEY));
285          * break; case Node.TEXT_NODE: result += "text nodes";
286          * setStatusImage(findImage(OutlineConsts.TEXT_KEY)); break; default:
287          * break; } result += " selected"; } catch (DocumentException de) { }
288          */
289         return result;
290     }
291 
292     /***
293      * @return
294      */
295     protected String createMsgForSingle() {
296         String result = ""; //$NON-NLS-1$
297         /*
298          * try { Node node = getXmlAccess().getIdManager().getNode(
299          * getSelection().getIDs().get(0)).item(0); switch (node.getNodeType()) {
300          * case Node.ATTRIBUTE_NODE: result = buildAttributePath((Attr) node);
301          * setStatusImage( findImage(OutlineConsts.ATTR_KEY)); break; case
302          * Node.CDATA_SECTION_NODE: result = node.toString(); setStatusImage(
303          * findImage(OutlineConsts.CDAT_KEY)); break; case Node.COMMENT_NODE:
304          * result = node.toString(); setStatusImage(
305          * findImage(OutlineConsts.COMM_KEY)); break; case Node.ELEMENT_NODE:
306          * result = buildElementPath((Element) node); setStatusImage(
307          * findImage(OutlineConsts.ELEM_KEY)); break; case
308          * Node.ENTITY_REFERENCE_NODE: result = node.toString(); setStatusImage(
309          * findImage(OutlineConsts.ENTI_KEY)); break; case
310          * Node.PROCESSING_INSTRUCTION_NODE: result = node.toString();
311          * setStatusImage( findImage(OutlineConsts.INST_KEY)); break; case
312          * Node.TEXT_NODE: result = node.toString(); setStatusImage(
313          * findImage(OutlineConsts.TEXT_KEY)); break; default: result =
314          * node.toString(); break; } } catch (DocumentException de) { }
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          * setSelection(XMLSelection.copyWithoutTexts(selection)); // invalid
343          * case: if (!getSelection().isValid(getXmlAccess().getIdManager())) {
344          * showInvalidSelectionMsg(); return; } // process valid selection:
345          * String message = ""; //$NON-NLS-1$ if (getSelection().size() == 0) {
346          * message = NO_SELECTION; } else if (getSelection().size() == 1) {
347          * message = createMsgForSingle(); } else { if
348          * (getSelection().isMixed(getXmlAccess().getIdManager())) { message =
349          * createMsgForMixed(); } else { message = createMsgForMulti(); } }
350          * showMessage(message);
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 }