KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > forms > MessageManager


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.internal.forms;
13
14 import java.io.PrintWriter JavaDoc;
15 import java.io.StringWriter JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.Hashtable JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.eclipse.jface.dialogs.IMessageProvider;
22 import org.eclipse.jface.fieldassist.ControlDecoration;
23 import org.eclipse.jface.fieldassist.FieldDecoration;
24 import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.custom.CLabel;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.ui.forms.IMessage;
31 import org.eclipse.ui.forms.IMessageManager;
32 import org.eclipse.ui.forms.IMessagePrefixProvider;
33 import org.eclipse.ui.forms.widgets.Hyperlink;
34 import org.eclipse.ui.forms.widgets.ScrolledForm;
35
36 /**
37  * @see IMessageManager
38  */

39
40 public class MessageManager implements IMessageManager {
41     private static final DefaultPrefixProvider DEFAULT_PREFIX_PROVIDER = new DefaultPrefixProvider();
42     private ArrayList JavaDoc messages = new ArrayList JavaDoc();
43     private Hashtable JavaDoc decorators = new Hashtable JavaDoc();
44     private boolean autoUpdate = true;
45     private ScrolledForm scrolledForm;
46     private IMessagePrefixProvider prefixProvider = DEFAULT_PREFIX_PROVIDER;
47     private int decorationPosition = SWT.LEFT | SWT.BOTTOM;
48     private static FieldDecoration standardError = FieldDecorationRegistry
49             .getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_ERROR);
50     private static FieldDecoration standardWarning = FieldDecorationRegistry
51             .getDefault().getFieldDecoration(
52                     FieldDecorationRegistry.DEC_WARNING);
53
54     private static final String JavaDoc[] SINGLE_MESSAGE_SUMMARY_KEYS = {
55             Messages.MessageManager_sMessageSummary,
56             Messages.MessageManager_sMessageSummary,
57             Messages.MessageManager_sWarningSummary,
58             Messages.MessageManager_sErrorSummary };
59
60     private static final String JavaDoc[] MULTIPLE_MESSAGE_SUMMARY_KEYS = {
61             Messages.MessageManager_pMessageSummary,
62             Messages.MessageManager_pMessageSummary,
63             Messages.MessageManager_pWarningSummary,
64             Messages.MessageManager_pErrorSummary };
65
66     static class Message implements IMessage {
67         Control control;
68         Object JavaDoc data;
69         Object JavaDoc key;
70         String JavaDoc message;
71         int type;
72         String JavaDoc prefix;
73
74         Message(Object JavaDoc key, String JavaDoc message, int type, Object JavaDoc data) {
75             this.key = key;
76             this.message = message;
77             this.type = type;
78             this.data = data;
79         }
80
81         /*
82          * (non-Javadoc)
83          *
84          * @see org.eclipse.jface.dialogs.IMessage#getKey()
85          */

86         public Object JavaDoc getKey() {
87             return key;
88         }
89
90         /*
91          * (non-Javadoc)
92          *
93          * @see org.eclipse.jface.dialogs.IMessageProvider#getMessage()
94          */

95         public String JavaDoc getMessage() {
96             return message;
97         }
98
99         /*
100          * (non-Javadoc)
101          *
102          * @see org.eclipse.jface.dialogs.IMessageProvider#getMessageType()
103          */

104         public int getMessageType() {
105             return type;
106         }
107
108         /*
109          * (non-Javadoc)
110          *
111          * @see org.eclipse.ui.forms.messages.IMessage#getControl()
112          */

113         public Control getControl() {
114             return control;
115         }
116
117         /*
118          * (non-Javadoc)
119          *
120          * @see org.eclipse.ui.forms.messages.IMessage#getData()
121          */

122         public Object JavaDoc getData() {
123             return data;
124         }
125
126         /*
127          * (non-Javadoc)
128          *
129          * @see org.eclipse.ui.forms.messages.IMessage#getPrefix()
130          */

131         public String JavaDoc getPrefix() {
132             return prefix;
133         }
134     }
135
136     static class DefaultPrefixProvider implements IMessagePrefixProvider {
137
138         public String JavaDoc getPrefix(Control c) {
139             Composite parent = c.getParent();
140             Control[] siblings = parent.getChildren();
141             for (int i = 0; i < siblings.length; i++) {
142                 if (siblings[i] == c) {
143                     // this is us - go backward until you hit
144
// a label-like widget
145
for (int j = i - 1; j >= 0; j--) {
146                         Control label = siblings[j];
147                         String JavaDoc ltext = null;
148                         if (label instanceof Label) {
149                             ltext = ((Label) label).getText();
150                         } else if (label instanceof Hyperlink) {
151                             ltext = ((Hyperlink) label).getText();
152                         } else if (label instanceof CLabel) {
153                             ltext = ((CLabel) label).getText();
154                         }
155                         if (ltext != null) {
156                             if (!ltext.endsWith(":")) //$NON-NLS-1$
157
return ltext + ": "; //$NON-NLS-1$
158
return ltext + " "; //$NON-NLS-1$
159
}
160                     }
161                     break;
162                 }
163             }
164             return null;
165         }
166     }
167
168     class ControlDecorator {
169         private ControlDecoration decoration;
170         private ArrayList JavaDoc controlMessages = new ArrayList JavaDoc();
171         private String JavaDoc prefix;
172
173         ControlDecorator(Control control) {
174             this.decoration = new ControlDecoration(control, decorationPosition, scrolledForm.getBody());
175         }
176
177         public boolean isDisposed() {
178             return decoration.getControl() == null;
179         }
180
181         void updatePrefix() {
182             prefix = null;
183         }
184
185         void updatePosition() {
186             Control control = decoration.getControl();
187             decoration.dispose();
188             this.decoration = new ControlDecoration(control, decorationPosition, scrolledForm.getBody());
189             update();
190         }
191
192         String JavaDoc getPrefix() {
193             if (prefix == null)
194                 createPrefix();
195             return prefix;
196         }
197
198         private void createPrefix() {
199             if (prefixProvider == null) {
200                 prefix = ""; //$NON-NLS-1$
201
return;
202             }
203             prefix = prefixProvider.getPrefix(decoration.getControl());
204             if (prefix == null)
205                 // make a prefix anyway
206
prefix = ""; //$NON-NLS-1$
207
}
208
209         void addAll(ArrayList JavaDoc target) {
210             target.addAll(controlMessages);
211         }
212
213         void addMessage(Object JavaDoc key, String JavaDoc text, Object JavaDoc data, int type) {
214             Message message = MessageManager.this.addMessage(getPrefix(), key,
215                     text, data, type, controlMessages);
216             message.control = decoration.getControl();
217             if (isAutoUpdate())
218                 update();
219         }
220
221         boolean removeMessage(Object JavaDoc key) {
222             Message message = findMessage(key, controlMessages);
223             if (message != null) {
224                 controlMessages.remove(message);
225                 if (isAutoUpdate())
226                     update();
227             }
228             return message != null;
229         }
230
231         boolean removeMessages() {
232             if (controlMessages.isEmpty())
233                 return false;
234             controlMessages.clear();
235             if (isAutoUpdate())
236                 update();
237             return true;
238         }
239
240         public void update() {
241             if (controlMessages.isEmpty()) {
242                 decoration.setDescriptionText(null);
243                 decoration.hide();
244             } else {
245                 ArrayList JavaDoc peers = createPeers(controlMessages);
246                 int type = ((IMessage) peers.get(0)).getMessageType();
247                 String JavaDoc description = createDetails(createPeers(peers), true);
248                 if (type == IMessageProvider.ERROR)
249                     decoration.setImage(standardError.getImage());
250                 else if (type == IMessageProvider.WARNING)
251                     decoration.setImage(standardWarning.getImage());
252                 decoration.setDescriptionText(description);
253                 decoration.show();
254             }
255         }
256     }
257
258     /**
259      * Creates a new instance of the message manager that will work with the
260      * provided form.
261      *
262      * @param scrolledForm
263      * the form to control
264      */

265     public MessageManager(ScrolledForm scrolledForm) {
266         this.scrolledForm = scrolledForm;
267     }
268
269     /*
270      * (non-Javadoc)
271      *
272      * @see org.eclipse.ui.forms.IMessageManager#addMessage(java.lang.Object,
273      * java.lang.String, int)
274      */

275     public void addMessage(Object JavaDoc key, String JavaDoc messageText, Object JavaDoc data, int type) {
276         addMessage(null, key, messageText, data, type, messages);
277         if (isAutoUpdate())
278             updateForm();
279     }
280
281     /*
282      * (non-Javadoc)
283      *
284      * @see org.eclipse.ui.forms.IMessageManager#addMessage(java.lang.Object,
285      * java.lang.String, int, org.eclipse.swt.widgets.Control)
286      */

287     public void addMessage(Object JavaDoc key, String JavaDoc messageText, Object JavaDoc data,
288             int type, Control control) {
289         ControlDecorator dec = (ControlDecorator) decorators.get(control);
290
291         if (dec == null) {
292             dec = new ControlDecorator(control);
293             decorators.put(control, dec);
294         }
295         dec.addMessage(key, messageText, data, type);
296         if (isAutoUpdate())
297             updateForm();
298     }
299
300     /*
301      * (non-Javadoc)
302      *
303      * @see org.eclipse.ui.forms.IMessageManager#removeMessage(java.lang.Object)
304      */

305     public void removeMessage(Object JavaDoc key) {
306         Message message = findMessage(key, messages);
307         if (message != null) {
308             messages.remove(message);
309             if (isAutoUpdate())
310                 updateForm();
311         }
312     }
313
314     /*
315      * (non-Javadoc)
316      *
317      * @see org.eclipse.ui.forms.IMessageManager#removeMessages()
318      */

319     public void removeMessages() {
320         if (!messages.isEmpty()) {
321             messages.clear();
322             if (isAutoUpdate())
323                 updateForm();
324         }
325     }
326
327     /*
328      * (non-Javadoc)
329      *
330      * @see org.eclipse.ui.forms.IMessageManager#removeMessage(java.lang.Object,
331      * org.eclipse.swt.widgets.Control)
332      */

333     public void removeMessage(Object JavaDoc key, Control control) {
334         ControlDecorator dec = (ControlDecorator) decorators.get(control);
335         if (dec == null)
336             return;
337         if (dec.removeMessage(key))
338             if (isAutoUpdate())
339                 updateForm();
340     }
341
342     /*
343      * (non-Javadoc)
344      *
345      * @see org.eclipse.ui.forms.IMessageManager#removeMessages(org.eclipse.swt.widgets.Control)
346      */

347     public void removeMessages(Control control) {
348         ControlDecorator dec = (ControlDecorator) decorators.get(control);
349         if (dec != null) {
350             if (dec.removeMessages()) {
351                 if (isAutoUpdate())
352                     updateForm();
353             }
354         }
355     }
356
357     /*
358      * (non-Javadoc)
359      *
360      * @see org.eclipse.ui.forms.IMessageManager#removeAllMessages()
361      */

362     public void removeAllMessages() {
363         boolean needsUpdate = false;
364         for (Enumeration JavaDoc enm = decorators.elements(); enm.hasMoreElements();) {
365             ControlDecorator control = (ControlDecorator) enm.nextElement();
366             if (control.removeMessages())
367                 needsUpdate = true;
368         }
369         if (!messages.isEmpty()) {
370             messages.clear();
371             needsUpdate = true;
372         }
373         if (needsUpdate && isAutoUpdate())
374             updateForm();
375     }
376
377     /*
378      * Adds the message if it does not already exist in the provided list.
379      */

380
381     private Message addMessage(String JavaDoc prefix, Object JavaDoc key, String JavaDoc messageText,
382             Object JavaDoc data, int type, ArrayList JavaDoc list) {
383         Message message = findMessage(key, list);
384         if (message == null) {
385             message = new Message(key, messageText, type, data);
386             message.prefix = prefix;
387             list.add(message);
388         } else {
389             message.message = messageText;
390             message.type = type;
391             message.data = data;
392         }
393         return message;
394     }
395
396     /*
397      * Finds the message with the provided key in the provided list.
398      */

399
400     private Message findMessage(Object JavaDoc key, ArrayList JavaDoc list) {
401         for (int i = 0; i < list.size(); i++) {
402             Message message = (Message) list.get(i);
403             if (message.getKey().equals(key))
404                 return message;
405         }
406         return null;
407     }
408
409     /*
410      * (non-Javadoc)
411      *
412      * @see org.eclipse.ui.forms.IMessageManager#update()
413      */

414     public void update() {
415         // Update decorations
416
for (Iterator JavaDoc iter = decorators.values().iterator(); iter.hasNext();) {
417             ControlDecorator dec = (ControlDecorator) iter.next();
418             dec.update();
419         }
420         // Update the form
421
updateForm();
422     }
423
424     /*
425      * Updates the container by rolling the messages up from the controls.
426      */

427
428     private void updateForm() {
429         ArrayList JavaDoc mergedList = new ArrayList JavaDoc();
430         mergedList.addAll(messages);
431         for (Enumeration JavaDoc enm = decorators.elements(); enm.hasMoreElements();) {
432             ControlDecorator dec = (ControlDecorator) enm.nextElement();
433             dec.addAll(mergedList);
434         }
435         update(mergedList);
436     }
437
438     private void update(ArrayList JavaDoc mergedList) {
439         pruneControlDecorators();
440         if (mergedList.isEmpty() || mergedList == null) {
441             scrolledForm.setMessage(null, IMessageProvider.NONE);
442             return;
443         }
444         ArrayList JavaDoc peers = createPeers(mergedList);
445         int maxType = ((IMessage) peers.get(0)).getMessageType();
446         String JavaDoc messageText;
447         IMessage[] array = (IMessage[]) peers
448                 .toArray(new IMessage[peers.size()]);
449         if (peers.size() == 1 && ((Message) peers.get(0)).prefix == null) {
450             // a single message
451
IMessage message = (IMessage) peers.get(0);
452             messageText = message.getMessage();
453             scrolledForm.setMessage(messageText, maxType, array);
454         } else {
455             // show a summary message for the message
456
// and list of errors for the details
457
if (peers.size() > 1)
458                 messageText = Messages.bind(
459                         MULTIPLE_MESSAGE_SUMMARY_KEYS[maxType],
460                         new String JavaDoc[] { peers.size() + "" }); //$NON-NLS-1$
461
else
462                 messageText = SINGLE_MESSAGE_SUMMARY_KEYS[maxType];
463             scrolledForm.setMessage(messageText, maxType, array);
464         }
465     }
466
467     private static String JavaDoc getFullMessage(IMessage message) {
468         if (message.getPrefix() == null)
469             return message.getMessage();
470         return message.getPrefix() + message.getMessage();
471     }
472
473     private ArrayList JavaDoc createPeers(ArrayList JavaDoc messages) {
474         ArrayList JavaDoc peers = new ArrayList JavaDoc();
475         int maxType = 0;
476         for (int i = 0; i < messages.size(); i++) {
477             Message message = (Message) messages.get(i);
478             if (message.type > maxType) {
479                 peers.clear();
480                 maxType = message.type;
481             }
482             if (message.type == maxType)
483                 peers.add(message);
484         }
485         return peers;
486     }
487
488     private String JavaDoc createDetails(ArrayList JavaDoc messages, boolean excludePrefix) {
489         StringWriter JavaDoc sw = new StringWriter JavaDoc();
490         PrintWriter JavaDoc out = new PrintWriter JavaDoc(sw);
491
492         for (int i = 0; i < messages.size(); i++) {
493             if (i > 0)
494                 out.println();
495             IMessage m = (IMessage) messages.get(i);
496             out.print(excludePrefix ? m.getMessage() : getFullMessage(m));
497         }
498         out.flush();
499         return sw.toString();
500     }
501
502     public static String JavaDoc createDetails(IMessage[] messages) {
503         if (messages == null || messages.length == 0)
504             return null;
505         StringWriter JavaDoc sw = new StringWriter JavaDoc();
506         PrintWriter JavaDoc out = new PrintWriter JavaDoc(sw);
507
508         for (int i = 0; i < messages.length; i++) {
509             if (i > 0)
510                 out.println();
511             out.print(getFullMessage(messages[i]));
512         }
513         out.flush();
514         return sw.toString();
515     }
516
517     /*
518      * (non-Javadoc)
519      *
520      * @see org.eclipse.ui.forms.IMessageManager#createSummary(org.eclipse.ui.forms.IMessage[])
521      */

522     public String JavaDoc createSummary(IMessage[] messages) {
523         return createDetails(messages);
524     }
525
526     private void pruneControlDecorators() {
527         for (Iterator JavaDoc iter = decorators.values().iterator(); iter.hasNext();) {
528             ControlDecorator dec = (ControlDecorator) iter.next();
529             if (dec.isDisposed())
530                 iter.remove();
531         }
532     }
533
534     /*
535      * (non-Javadoc)
536      *
537      * @see org.eclipse.ui.forms.IMessageManager#getMessagePrefixProvider()
538      */

539     public IMessagePrefixProvider getMessagePrefixProvider() {
540         return prefixProvider;
541     }
542
543     /*
544      * (non-Javadoc)
545      *
546      * @see org.eclipse.ui.forms.IMessageManager#setMessagePrefixProvider(org.eclipse.ui.forms.IMessagePrefixProvider)
547      */

548     public void setMessagePrefixProvider(IMessagePrefixProvider provider) {
549         this.prefixProvider = provider;
550         for (Iterator JavaDoc iter = decorators.values().iterator(); iter.hasNext();) {
551             ControlDecorator dec = (ControlDecorator) iter.next();
552             dec.updatePrefix();
553         }
554     }
555
556     /*
557      * (non-Javadoc)
558      *
559      * @see org.eclipse.ui.forms.IMessageManager#getDecorationPosition()
560      */

561     public int getDecorationPosition() {
562         return decorationPosition;
563     }
564
565     /*
566      * (non-Javadoc)
567      *
568      * @see org.eclipse.ui.forms.IMessageManager#setDecorationPosition(int)
569      */

570     public void setDecorationPosition(int position) {
571         this.decorationPosition = position;
572         for (Iterator JavaDoc iter = decorators.values().iterator(); iter.hasNext();) {
573             ControlDecorator dec = (ControlDecorator) iter.next();
574             dec.updatePosition();
575         }
576     }
577
578     /*
579      * (non-Javadoc)
580      *
581      * @see org.eclipse.ui.forms.IMessageManager#isAutoUpdate()
582      */

583     public boolean isAutoUpdate() {
584         return autoUpdate;
585     }
586
587     /*
588      * (non-Javadoc)
589      *
590      * @see org.eclipse.ui.forms.IMessageManager#setAutoUpdate(boolean)
591      */

592     public void setAutoUpdate(boolean autoUpdate) {
593         boolean needsUpdate = !this.autoUpdate && autoUpdate;
594         this.autoUpdate = autoUpdate;
595         if (needsUpdate)
596             update();
597     }
598 }
Popular Tags