KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > statushandlers > StatusNotificationManager


1 /*******************************************************************************
2  * Copyright (c) 2004, 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 package org.eclipse.ui.internal.statushandlers;
12
13 import java.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.HashSet JavaDoc;
17
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.jface.dialogs.ErrorDialog;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.swt.SWTException;
23 import org.eclipse.swt.events.DisposeListener;
24 //import org.eclipse.swt.widgets.Display;
25
import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.internal.WorkbenchPlugin;
27 import org.eclipse.ui.internal.progress.ProgressManagerUtil;
28 import org.eclipse.ui.internal.progress.ProgressMessages;
29 import org.eclipse.ui.progress.IProgressConstants;
30 import org.eclipse.ui.statushandlers.StatusAdapter;
31
32 import com.ibm.icu.text.DateFormat;
33
34 /**
35  * The StatusNotificationManager is the class that manages the display of status
36  * information.
37  */

38 public class StatusNotificationManager {
39
40     private Collection JavaDoc errors = Collections.synchronizedSet(new HashSet JavaDoc());
41
42     private StatusDialog dialog;
43
44     private static StatusNotificationManager sharedInstance;
45
46     private DisposeListener disposeListener = new DisposeListener() {
47         /*
48          * (non-Javadoc)
49          *
50          * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
51          */

52         public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) {
53             dialog = null;
54             errors.clear();
55         }
56     };
57
58     /**
59      * Returns the shared instance.
60      *
61      * @return the shared instance
62      */

63     public static StatusNotificationManager getInstance() {
64         if (sharedInstance == null) {
65             sharedInstance = new StatusNotificationManager();
66         }
67         return sharedInstance;
68     }
69
70     /**
71      * Create a new instance of the manager.
72      */

73     private StatusNotificationManager() {
74     }
75
76     /**
77      * Add a new error to the list.
78      *
79      * @param statusInfo
80      * the error to be displayed
81      */

82     public void addError(StatusAdapter statusAdapter, final boolean modal) {
83
84         if (ErrorDialog.AUTOMATED_MODE == true) {
85             return;
86         }
87
88         final StatusInfo statusInfo = new StatusInfo(statusAdapter);
89
90         if (!PlatformUI.isWorkbenchRunning()) {
91             // we are shutting down, so just log
92
WorkbenchPlugin.log(statusInfo.getStatus().getStatus());
93             return;
94         }
95
96         // Add the error in the UI thread to ensure thread safety in the
97
// dialog
98
if (dialog == null || dialog.getShell().isDisposed()) {
99
100             errors.add(statusInfo);
101             // Delay prompting if the status adapter property is set
102
Object JavaDoc noPromptProperty = statusInfo.getStatus().getProperty(
103                     IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY);
104
105             boolean prompt = true;
106             if (noPromptProperty instanceof Boolean JavaDoc) {
107                 prompt = !((Boolean JavaDoc) noPromptProperty).booleanValue();
108             }
109
110             if (prompt && !PlatformUI.getWorkbench().isClosing()) {
111                 try{
112                     PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable JavaDoc() {
113                         public void run() {
114                             
115                             //Protect against a shutdown after the async was posted
116

117                             if(PlatformUI.getWorkbench().isClosing())
118                                 return;
119                         
120                             if (dialog == null) {
121                                 dialog = new StatusDialog(ProgressManagerUtil
122                                         .getDefaultParent(), statusInfo,
123                                         IStatus.INFO | IStatus.WARNING
124                                         | IStatus.ERROR, modal);
125                                 dialog.open();
126                                 dialog.getShell().addDisposeListener(
127                                     disposeListener);
128                             }
129                         }
130                     });
131                 }
132                 catch(SWTException exception){
133                     //Do nothing here as we will just not display if there are SWT issue
134
//This protects an asyncExec on a disposed display
135
}
136
137             }
138         } else {
139
140             if (statusInfo.getStatus().getProperty(
141                     IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY) != null) {
142                 statusInfo.getStatus().setProperty(
143                         IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY,
144                         Boolean.FALSE);
145             }
146
147             if (!PlatformUI.getWorkbench().isClosing()) {
148                 try{
149                     PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable JavaDoc() {
150                         public void run() {
151                             if(PlatformUI.getWorkbench().isClosing())
152                                 return;
153                             openStatusDialog(modal,statusInfo);
154                         }
155                     });
156                 }
157                 catch (SWTException exception){
158                     //Do nothing here as we will just not display if there are SWT issue
159
//This protects an syncExec on a disposed display
160
}
161             }
162         }
163     }
164
165     /**
166      * Get the currently registered errors in the receiver.
167      *
168      * @return Collection of ErrorInfo
169      */

170     Collection JavaDoc getErrors() {
171         return errors;
172     }
173
174     /**
175      * @param modal
176      * @param statusInfo
177      */

178     void openStatusDialog(final boolean modal, final StatusInfo statusInfo) {
179         errors.add(statusInfo);
180         if (modal && !dialog.isModal()) {
181             dialog.getShell().removeDisposeListener(disposeListener);
182             dialog.close();
183             dialog = new StatusDialog(ProgressManagerUtil.getDefaultParent(),
184                     statusInfo, IStatus.INFO | IStatus.WARNING | IStatus.ERROR,
185                     modal);
186
187             dialog.open();
188             dialog.getShell().addDisposeListener(disposeListener);
189         } else {
190             dialog.refresh();
191         }
192     }
193
194     /**
195      * A wrapper class for statuses displayed in the dialog.
196      *
197      */

198     protected static class StatusInfo implements Comparable JavaDoc {
199
200         public boolean equals(Object JavaDoc obj) {
201             if (obj instanceof StatusInfo) {
202                 return statusAdapter.equals(((StatusInfo) obj).getStatus());
203             }
204             return super.equals(obj);
205         }
206
207         public int hashCode() {
208             return statusAdapter.hashCode();
209         }
210
211         private final StatusAdapter statusAdapter;
212
213         /**
214          * Constructs a simple <code>StatusInfo</code>, without any
215          * extensions.
216          *
217          * @param status
218          * the root status for this status info
219          */

220         public StatusInfo(StatusAdapter statusAdapter) {
221             this.statusAdapter = statusAdapter;
222
223             Object JavaDoc timestampProperty = statusAdapter
224                     .getProperty(StatusAdapter.TIMESTAMP_PROPERTY);
225
226             if (timestampProperty == null
227                     || !(timestampProperty instanceof Long JavaDoc)) {
228                 statusAdapter.setProperty(StatusAdapter.TIMESTAMP_PROPERTY,
229                         new Long JavaDoc(System.currentTimeMillis()));
230             }
231         }
232
233         String JavaDoc getDisplayString() {
234             String JavaDoc text = statusAdapter.getStatus().getMessage();
235
236             Job job = (Job) (statusAdapter.getAdapter(Job.class));
237             if (job != null) {
238                 text = job.getName();
239             }
240
241             return NLS.bind(ProgressMessages.JobInfo_Error,
242                     (new Object JavaDoc[] {
243                             text,
244                             DateFormat.getDateTimeInstance(DateFormat.LONG,
245                                     DateFormat.LONG).format(
246                                     new Date JavaDoc(getTimestamp())) }));
247         }
248
249         /**
250          * Time when this status info was created.
251          *
252          * @return the time
253          */

254         public long getTimestamp() {
255             return ((Long JavaDoc) statusAdapter
256                     .getProperty(StatusAdapter.TIMESTAMP_PROPERTY)).longValue();
257         }
258
259         /*
260          * (non-Javadoc)
261          *
262          * @see java.lang.Comparable#compareTo(T)
263          */

264         public int compareTo(Object JavaDoc arg0) {
265             if (arg0 instanceof StatusInfo) {
266                 // Order ErrorInfo by time received
267
long otherTimestamp = ((StatusInfo) arg0).getTimestamp();
268                 if (getTimestamp() < otherTimestamp) {
269                     return -1;
270                 } else if (getTimestamp() > otherTimestamp) {
271                     return 1;
272                 } else {
273                     return getDisplayString().compareTo(
274                             ((StatusInfo) arg0).getDisplayString());
275                 }
276             }
277             return 0;
278         }
279
280         /**
281          * @return Returns the status.
282          */

283         public StatusAdapter getStatus() {
284             return statusAdapter;
285         }
286     }
287 }
288
Popular Tags