comparison org.eclipse.jface/src/org/eclipse/jface/util/SafeRunnableDialog.d @ 12:bc29606a740c

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