10
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.util.SafeRunnableDialog;
|
|
14
|
|
15 pragma(msg,"FIXME dwtx.jface.util.SafeRunnableDialog" );
|
|
16 import dwt.widgets.Shell;
|
|
17 import dwtx.core.runtime.IStatus;
|
|
18 class SafeRunnableDialog{
|
|
19 this(IStatus status) ;
|
|
20 Shell getShell();
|
|
21 void create();
|
|
22 void open();
|
|
23 void addStatus(IStatus);
|
|
24 void refresh();
|
|
25 }
|
|
26
|
|
27 /++
|
|
28
|
|
29
|
|
30 // import java.util.ArrayList;
|
|
31 // import java.util.Collection;
|
|
32
|
|
33 import dwt.DWT;
|
|
34 import dwt.graphics.Point;
|
|
35 import dwt.layout.GridData;
|
|
36 import dwt.widgets.Button;
|
|
37 import dwt.widgets.Composite;
|
|
38 import dwt.widgets.Control;
|
|
39 import dwtx.core.runtime.IStatus;
|
|
40 import dwtx.jface.dialogs.ErrorDialog;
|
|
41 import dwtx.jface.dialogs.IDialogConstants;
|
|
42 import dwtx.jface.resource.JFaceResources;
|
|
43 import dwtx.jface.viewers.CellLabelProvider;
|
|
44 import dwtx.jface.viewers.ISelection;
|
|
45 import dwtx.jface.viewers.ISelectionChangedListener;
|
|
46 import dwtx.jface.viewers.IStructuredContentProvider;
|
|
47 import dwtx.jface.viewers.IStructuredSelection;
|
|
48 import dwtx.jface.viewers.SelectionChangedEvent;
|
|
49 import dwtx.jface.viewers.TableViewer;
|
|
50 import dwtx.jface.viewers.Viewer;
|
|
51 import dwtx.jface.viewers.ViewerCell;
|
|
52 import dwtx.jface.viewers.ViewerComparator;
|
|
53
|
|
54 import dwt.dwthelper.utils;
|
|
55
|
|
56 /**
|
|
57 * SafeRunnableDialog is a dialog that can show the results of multiple safe
|
|
58 * runnable errors
|
|
59 *
|
|
60 */
|
|
61 class SafeRunnableDialog : ErrorDialog {
|
|
62
|
|
63 private TableViewer statusListViewer;
|
|
64
|
|
65 private Collection statuses = new ArrayList();
|
|
66
|
|
67 /**
|
|
68 * Create a new instance of the receiver on a status.
|
|
69 *
|
|
70 * @param status
|
|
71 * The status to display.
|
|
72 */
|
|
73 this(IStatus status) {
|
|
74
|
|
75 super(null, JFaceResources.getString("error"), status.getMessage(), //$NON-NLS-1$
|
|
76 status, IStatus.ERROR);
|
|
77
|
|
78 setShellStyle(DWT.DIALOG_TRIM | DWT.MODELESS | DWT.RESIZE | DWT.MIN
|
|
79 | getDefaultOrientation());
|
|
80
|
|
81 setStatus(status);
|
|
82 statuses.add(status);
|
|
83
|
|
84 setBlockOnOpen(false);
|
|
85
|
|
86 String reason = JFaceResources
|
|
87 .getString("SafeRunnableDialog_checkDetailsMessage"); //$NON-NLS-1$
|
|
88 if (status.getException() !is null) {
|
|
89 reason = status.getException().getMessage() is null ? status
|
|
90 .getException().toString() : status.getException()
|
|
91 .getMessage();
|
|
92 }
|
|
93 this.message = JFaceResources.format(JFaceResources
|
|
94 .getString("SafeRunnableDialog_reason"), new Object[] { //$NON-NLS-1$
|
|
95 status.getMessage(), reason });
|
|
96 }
|
|
97
|
|
98 /**
|
|
99 * Method which should be invoked when new errors become available for
|
|
100 * display
|
|
101 */
|
|
102 void refresh() {
|
|
103
|
|
104 if (AUTOMATED_MODE)
|
|
105 return;
|
|
106
|
|
107 createStatusList((Composite) dialogArea);
|
|
108 updateEnablements();
|
|
109 }
|
|
110
|
|
111 /*
|
|
112 * (non-Javadoc)
|
|
113 *
|
|
114 * @see dwtx.jface.dialogs.ErrorDialog#createDialogArea(dwt.widgets.Composite)
|
|
115 */
|
|
116 protected Control createDialogArea(Composite parent) {
|
|
117 Control area = super.createDialogArea(parent);
|
|
118 createStatusList((Composite) area);
|
|
119 return area;
|
|
120 }
|
|
121
|
|
122 /**
|
|
123 * Create the status list if required.
|
|
124 *
|
|
125 * @param parent
|
|
126 * the Control to create it in.
|
|
127 */
|
|
128 private void createStatusList(Composite parent) {
|
|
129 if (isMultipleStatusDialog()) {
|
|
130 if (statusListViewer is null) {
|
|
131 // The job list doesn't exist so create it.
|
|
132 setMessage(JFaceResources
|
|
133 .getString("SafeRunnableDialog_MultipleErrorsMessage")); //$NON-NLS-1$
|
|
134 getShell()
|
|
135 .setText(
|
|
136 JFaceResources
|
|
137 .getString("SafeRunnableDialog_MultipleErrorsTitle")); //$NON-NLS-1$
|
|
138 createStatusListArea(parent);
|
|
139 showDetailsArea();
|
|
140 }
|
|
141 refreshStatusList();
|
|
142 }
|
|
143 }
|
|
144
|
|
145 /*
|
|
146 * Update the button enablements
|
|
147 */
|
|
148 private void updateEnablements() {
|
|
149 Button details = getButton(IDialogConstants.DETAILS_ID);
|
|
150 if (details !is null) {
|
|
151 details.setEnabled(true);
|
|
152 }
|
|
153 }
|
|
154
|
|
155 /**
|
|
156 * This method sets the message in the message label.
|
|
157 *
|
|
158 * @param messageString -
|
|
159 * the String for the message area
|
|
160 */
|
|
161 private void setMessage(String messageString) {
|
|
162 // must not set null text in a label
|
|
163 message = messageString is null ? "" : messageString; //$NON-NLS-1$
|
|
164 if (messageLabel is null || messageLabel.isDisposed()) {
|
|
165 return;
|
|
166 }
|
|
167 messageLabel.setText(message);
|
|
168 }
|
|
169
|
|
170 /**
|
|
171 * Create an area that allow the user to select one of multiple jobs that
|
|
172 * have reported errors
|
|
173 *
|
|
174 * @param parent -
|
|
175 * the parent of the area
|
|
176 */
|
|
177 private void createStatusListArea(Composite parent) {
|
|
178 // Display a list of jobs that have reported errors
|
|
179 statusListViewer = new TableViewer(parent, DWT.SINGLE | DWT.H_SCROLL
|
|
180 | DWT.V_SCROLL | DWT.BORDER);
|
|
181 statusListViewer.setComparator(getViewerComparator());
|
|
182 Control control = statusListViewer.getControl();
|
|
183 GridData data = new GridData(GridData.FILL_BOTH
|
|
184 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL);
|
|
185 data.heightHint = convertHeightInCharsToPixels(10);
|
|
186 control.setLayoutData(data);
|
|
187 statusListViewer.setContentProvider(getStatusContentProvider());
|
|
188 statusListViewer.setLabelProvider(getStatusListLabelProvider());
|
|
189 statusListViewer
|
|
190 .addSelectionChangedListener(new ISelectionChangedListener() {
|
|
191 public void selectionChanged(SelectionChangedEvent event) {
|
|
192 handleSelectionChange();
|
|
193 }
|
|
194 });
|
|
195 applyDialogFont(parent);
|
|
196 statusListViewer.setInput(this);
|
|
197 }
|
|
198
|
|
199 /**
|
|
200 * Return the label provider for the status list.
|
|
201 *
|
|
202 * @return CellLabelProvider
|
|
203 */
|
|
204 private CellLabelProvider getStatusListLabelProvider() {
|
|
205 return new CellLabelProvider() {
|
|
206 /*
|
|
207 * (non-Javadoc)
|
|
208 *
|
|
209 * @see dwtx.jface.viewers.CellLabelProvider#update(dwtx.jface.viewers.ViewerCell)
|
|
210 */
|
|
211 public void update(ViewerCell cell) {
|
|
212 cell.setText(((IStatus) cell.getElement()).getMessage());
|
|
213
|
|
214 }
|
|
215 };
|
|
216 }
|
|
217
|
|
218 /**
|
|
219 * Return the content provider for the statuses.
|
|
220 *
|
|
221 * @return IStructuredContentProvider
|
|
222 */
|
|
223 private IStructuredContentProvider getStatusContentProvider() {
|
|
224 return new IStructuredContentProvider() {
|
|
225 /*
|
|
226 * (non-Javadoc)
|
|
227 *
|
|
228 * @see dwtx.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
|
|
229 */
|
|
230 public Object[] getElements(Object inputElement) {
|
|
231 return statuses.toArray();
|
|
232 }
|
|
233
|
|
234 /*
|
|
235 * (non-Javadoc)
|
|
236 *
|
|
237 * @see dwtx.jface.viewers.IContentProvider#dispose()
|
|
238 */
|
|
239 public void dispose() {
|
|
240
|
|
241 }
|
|
242
|
|
243 /*
|
|
244 * (non-Javadoc)
|
|
245 *
|
|
246 * @see dwtx.jface.viewers.IContentProvider#inputChanged(dwtx.jface.viewers.Viewer,
|
|
247 * java.lang.Object, java.lang.Object)
|
|
248 */
|
|
249 public void inputChanged(Viewer viewer, Object oldInput,
|
|
250 Object newInput) {
|
|
251
|
|
252 }
|
|
253 };
|
|
254 }
|
|
255
|
|
256 /*
|
|
257 * Return whether there are multiple errors to be displayed
|
|
258 */
|
|
259 private bool isMultipleStatusDialog() {
|
|
260 return statuses.size() > 1;
|
|
261 }
|
|
262
|
|
263 /**
|
|
264 * Return a viewer sorter for looking at the jobs.
|
|
265 *
|
|
266 * @return ViewerSorter
|
|
267 */
|
|
268 private ViewerComparator getViewerComparator() {
|
|
269 return new ViewerComparator() {
|
|
270 /*
|
|
271 * (non-Javadoc)
|
|
272 *
|
|
273 * @see dwtx.jface.viewers.ViewerComparator#compare(dwtx.jface.viewers.Viewer,
|
|
274 * java.lang.Object, java.lang.Object)
|
|
275 */
|
|
276 public int compare(Viewer testViewer, Object e1, Object e2) {
|
|
277 String message1 = ((IStatus) e1).getMessage();
|
|
278 String message2 = ((IStatus) e2).getMessage();
|
|
279 if (message1 is null)
|
|
280 return 1;
|
|
281 if (message2 is null)
|
|
282 return -1;
|
|
283
|
|
284 return message1.compareTo(message2);
|
|
285 }
|
|
286 };
|
|
287 }
|
|
288
|
|
289 /**
|
|
290 * Refresh the contents of the viewer.
|
|
291 */
|
|
292 void refreshStatusList() {
|
|
293 if (statusListViewer !is null
|
|
294 && !statusListViewer.getControl().isDisposed()) {
|
|
295 statusListViewer.refresh();
|
|
296 Point newSize = getShell().computeSize(DWT.DEFAULT, DWT.DEFAULT);
|
|
297 getShell().setSize(newSize);
|
|
298 }
|
|
299 }
|
|
300
|
|
301 /**
|
|
302 * Get the single selection. Return null if the selection is not just one
|
|
303 * element.
|
|
304 *
|
|
305 * @return IStatus or <code>null</code>.
|
|
306 */
|
|
307 private IStatus getSingleSelection() {
|
|
308 ISelection rawSelection = statusListViewer.getSelection();
|
|
309 if (rawSelection !is null
|
|
310 && rawSelection instanceof IStructuredSelection) {
|
|
311 IStructuredSelection selection = (IStructuredSelection) rawSelection;
|
|
312 if (selection.size() is 1) {
|
|
313 return (IStatus) selection.getFirstElement();
|
|
314 }
|
|
315 }
|
|
316 return null;
|
|
317 }
|
|
318
|
|
319 /**
|
|
320 * The selection in the multiple job list has changed. Update widget
|
|
321 * enablements and repopulate the list.
|
|
322 */
|
|
323 void handleSelectionChange() {
|
|
324 IStatus newSelection = getSingleSelection();
|
|
325 setStatus(newSelection);
|
|
326 updateEnablements();
|
|
327 showDetailsArea();
|
|
328 }
|
|
329
|
|
330 /*
|
|
331 * (non-Javadoc)
|
|
332 *
|
|
333 * @see dwtx.jface.dialogs.ErrorDialog#shouldShowDetailsButton()
|
|
334 */
|
|
335 protected bool shouldShowDetailsButton() {
|
|
336 return true;
|
|
337 }
|
|
338
|
|
339 /**
|
|
340 * Add the status to the receiver.
|
|
341 * @param status
|
|
342 */
|
|
343 public void addStatus(IStatus status) {
|
|
344 statuses.add(status);
|
|
345 refresh();
|
|
346
|
|
347 }
|
|
348
|
|
349
|
|
350 }
|
|
351 ++/ |