1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.plugin.views.outline;
13
14 import java.util.List;
15
16 import org.eclipse.jface.action.IStatusLineManager;
17 import org.eclipse.jface.resource.ImageRegistry;
18 import org.eclipse.swt.graphics.Image;
19 import org.w3c.dom.Attr;
20 import org.w3c.dom.Element;
21 import org.w3c.dom.Node;
22
23 import sk.uniba.euromath.document.XMLAccess;
24 import sk.uniba.euromath.editor.selections.DOMSelectionChangedEvent;
25 import sk.uniba.euromath.editor.selections.IDOMSelectionChangedListener;
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 implements IDOMSelectionChangedListener {
36
37 private final IStatusLineManager statusLine;
38
39 private ImageRegistry registry = null;
40
41 private Image statusImage = null;
42
43 /***
44 * @param manager
45 */
46 public StatusLineUpdateManager(IStatusLineManager manager,
47 XMLAccess access, XMLOutlinePage outlinePage) {
48 super();
49 this.statusLine = manager;
50 this.registry = outlinePage.getLabelProvider()
51 .getImageRegistry();
52 outlinePage.addSelectionChangedListener(this);
53 }
54
55 /***
56 * @param attr
57 * @return
58 */
59 private String buildAttributePath(Attr attr) {
60 return buildElementPath(attr.getOwnerElement()) + "."
61 + attr.getNodeName() + "=" + attr.getValue();
62 }
63
64 /***
65 * @param element
66 * @return
67 */
68 protected String buildElementPath(Element element) {
69 String result = element.getLocalName();
70 while ((element.getParentNode() != null)
71 && (element.getParentNode() instanceof Element)) {
72 element = (Element) element.getParentNode();
73 result = element.getLocalName() + "/" + result;
74 }
75 return result;
76 }
77
78 protected String createMsgForMulti(List<Node> nodes) {
79 int attrs = 0;
80 int cdats = 0;
81 int comms = 0;
82 int elems = 0;
83 int proci = 0;
84 int texts = 0;
85 int unknw = 0;
86
87 StringBuilder result = new StringBuilder("");
88
89
90 for (Node node : nodes) {
91 switch (node.getNodeType()) {
92 case Node.ATTRIBUTE_NODE:
93 attrs++;
94 break;
95 case Node.CDATA_SECTION_NODE:
96 cdats++;
97 break;
98 case Node.COMMENT_NODE:
99 comms++;
100 break;
101 case Node.ELEMENT_NODE:
102 elems++;
103 break;
104 case Node.PROCESSING_INSTRUCTION_NODE:
105 proci++;
106 break;
107 case Node.TEXT_NODE:
108 texts++;
109 break;
110 default:
111 unknw++;
112 break;
113 }
114 }
115
116
117 if (attrs > 0) {
118 if (result.length() > 0) {
119 result.append(", ");
120 }
121 result.append(attrs + " attribute");
122 if (attrs > 1)
123 result.append("s");
124 }
125
126 if (cdats > 0) {
127 if (result.length() > 0) {
128 result.append(", ");
129 }
130 result.append(cdats + " cdata section");
131 if (cdats > 1)
132 result.append("s");
133 }
134
135 if (comms > 0) {
136 if (result.length() > 0) {
137 result.append(", ");
138 }
139 result.append(comms + " comment");
140 if (comms > 1)
141 result.append("s");
142 }
143
144 if (elems > 0) {
145 if (result.length() > 0) {
146 result.append(", ");
147 }
148 result.append(elems + " element");
149 if (elems > 1)
150 result.append("s");
151 }
152
153 if (proci > 0) {
154 if (result.length() > 0) {
155 result.append(", ");
156 }
157 result.append(proci + " processing instruction");
158 if (proci > 1)
159 result.append("s");
160 }
161
162 if (texts > 0) {
163 if (result.length() > 0) {
164 result.append(", ");
165 }
166 result.append(texts + " text node");
167 if (texts > 1)
168 result.append("s");
169 }
170
171 if (unknw > 0) {
172 if (result.length() > 0) {
173 result.append(", ");
174 }
175 result.append(unknw + " text node");
176 if (unknw > 1)
177 result.append("s");
178 }
179
180 return result.toString();
181 }
182
183 /***
184 * @return
185 */
186 protected String createMsgForSingle(Node node) {
187 String result = "";
188 switch (node.getNodeType()) {
189 case Node.ATTRIBUTE_NODE:
190 result = buildAttributePath((Attr) node);
191 this.statusImage = findImage(OutlineConsts.ATTR_KEY);
192 break;
193 case Node.CDATA_SECTION_NODE:
194 result = node.toString();
195 this.statusImage = findImage(OutlineConsts.CDAT_KEY);
196 break;
197 case Node.COMMENT_NODE:
198 result = node.toString();
199 this.statusImage = findImage(OutlineConsts.COMM_KEY);
200 break;
201 case Node.ELEMENT_NODE:
202 result = buildElementPath((Element) node);
203 this.statusImage = findImage(OutlineConsts.ELEM_KEY);
204 break;
205 case Node.ENTITY_REFERENCE_NODE:
206 result = node.toString();
207 this.statusImage = findImage(OutlineConsts.ENTI_KEY);
208 break;
209 case Node.PROCESSING_INSTRUCTION_NODE:
210 result = node.toString();
211 this.statusImage = findImage(OutlineConsts.INST_KEY);
212 break;
213 case Node.TEXT_NODE:
214 result = node.toString();
215 this.statusImage = findImage(OutlineConsts.TEXT_KEY);
216 break;
217 default:
218 result = node.toString();
219 break;
220 }
221
222 return result;
223 }
224
225 protected Image findImage(String key) {
226 return this.registry.get(key);
227 }
228
229 /***
230 * Displays mesage in status bar.
231 *
232 * @param message
233 * to display
234 */
235 protected void showMessage(String message) {
236 this.statusLine.setErrorMessage(null);
237 this.statusLine.setMessage(this.statusImage, message);
238 }
239
240 /***
241 * Updates status bar according to selection.
242 */
243 public void selectionChanged(DOMSelectionChangedEvent event) {
244 this.statusImage = null;
245
246 List<Node> nodes = event.getDOMSelection().getContents();
247 switch (nodes.size()) {
248 case 0:
249 showMessage("Nothing selected.");
250 break;
251 case 1:
252 showMessage(createMsgForSingle(nodes.get(0)));
253 break;
254 default:
255 showMessage(createMsgForMulti(nodes));
256 break;
257 }
258
259 }
260 }