1 11 package org.eclipse.jface.util; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.jface.dialogs.ErrorDialog; 18 import org.eclipse.jface.dialogs.IDialogConstants; 19 import org.eclipse.jface.resource.JFaceResources; 20 import org.eclipse.jface.viewers.CellLabelProvider; 21 import org.eclipse.jface.viewers.ISelection; 22 import org.eclipse.jface.viewers.ISelectionChangedListener; 23 import org.eclipse.jface.viewers.IStructuredContentProvider; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 import org.eclipse.jface.viewers.SelectionChangedEvent; 26 import org.eclipse.jface.viewers.TableViewer; 27 import org.eclipse.jface.viewers.Viewer; 28 import org.eclipse.jface.viewers.ViewerCell; 29 import org.eclipse.jface.viewers.ViewerComparator; 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.graphics.Point; 32 import org.eclipse.swt.layout.GridData; 33 import org.eclipse.swt.widgets.Button; 34 import org.eclipse.swt.widgets.Composite; 35 import org.eclipse.swt.widgets.Control; 36 37 42 class SafeRunnableDialog extends ErrorDialog { 43 44 private TableViewer statusListViewer; 45 46 private Collection statuses = new ArrayList (); 47 48 54 SafeRunnableDialog(IStatus status) { 55 56 super(null, JFaceResources.getString("error"), status.getMessage(), status, IStatus.ERROR); 58 59 setShellStyle(SWT.DIALOG_TRIM | SWT.MODELESS | SWT.RESIZE | SWT.MIN 60 | getDefaultOrientation()); 61 62 setStatus(status); 63 statuses.add(status); 64 65 setBlockOnOpen(false); 66 67 String reason = JFaceResources 68 .getString("SafeRunnableDialog_checkDetailsMessage"); if (status.getException() != null) { 70 reason = status.getException().getMessage() == null ? status 71 .getException().toString() : status.getException() 72 .getMessage(); 73 } 74 this.message = JFaceResources.format(JFaceResources 75 .getString("SafeRunnableDialog_reason"), new Object [] { status.getMessage(), reason }); 77 } 78 79 83 void refresh() { 84 85 if (AUTOMATED_MODE) 86 return; 87 88 createStatusList((Composite) dialogArea); 89 updateEnablements(); 90 } 91 92 97 protected Control createDialogArea(Composite parent) { 98 Control area = super.createDialogArea(parent); 99 createStatusList((Composite) area); 100 return area; 101 } 102 103 109 private void createStatusList(Composite parent) { 110 if (isMultipleStatusDialog()) { 111 if (statusListViewer == null) { 112 setMessage(JFaceResources 114 .getString("SafeRunnableDialog_MultipleErrorsMessage")); getShell() 116 .setText( 117 JFaceResources 118 .getString("SafeRunnableDialog_MultipleErrorsTitle")); createStatusListArea(parent); 120 showDetailsArea(); 121 } 122 refreshStatusList(); 123 } 124 } 125 126 129 private void updateEnablements() { 130 Button details = getButton(IDialogConstants.DETAILS_ID); 131 if (details != null) { 132 details.setEnabled(true); 133 } 134 } 135 136 142 private void setMessage(String messageString) { 143 message = messageString == null ? "" : messageString; if (messageLabel == null || messageLabel.isDisposed()) { 146 return; 147 } 148 messageLabel.setText(message); 149 } 150 151 158 private void createStatusListArea(Composite parent) { 159 statusListViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL 161 | SWT.V_SCROLL | SWT.BORDER); 162 statusListViewer.setComparator(getViewerComparator()); 163 Control control = statusListViewer.getControl(); 164 GridData data = new GridData(GridData.FILL_BOTH 165 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL); 166 data.heightHint = convertHeightInCharsToPixels(10); 167 control.setLayoutData(data); 168 statusListViewer.setContentProvider(getStatusContentProvider()); 169 statusListViewer.setLabelProvider(getStatusListLabelProvider()); 170 statusListViewer 171 .addSelectionChangedListener(new ISelectionChangedListener() { 172 public void selectionChanged(SelectionChangedEvent event) { 173 handleSelectionChange(); 174 } 175 }); 176 applyDialogFont(parent); 177 statusListViewer.setInput(this); 178 } 179 180 185 private CellLabelProvider getStatusListLabelProvider() { 186 return new CellLabelProvider() { 187 192 public void update(ViewerCell cell) { 193 cell.setText(((IStatus) cell.getElement()).getMessage()); 194 195 } 196 }; 197 } 198 199 204 private IStructuredContentProvider getStatusContentProvider() { 205 return new IStructuredContentProvider() { 206 211 public Object [] getElements(Object inputElement) { 212 return statuses.toArray(); 213 } 214 215 220 public void dispose() { 221 222 } 223 224 230 public void inputChanged(Viewer viewer, Object oldInput, 231 Object newInput) { 232 233 } 234 }; 235 } 236 237 240 private boolean isMultipleStatusDialog() { 241 return statuses.size() > 1; 242 } 243 244 249 private ViewerComparator getViewerComparator() { 250 return new ViewerComparator() { 251 257 public int compare(Viewer testViewer, Object e1, Object e2) { 258 String message1 = ((IStatus) e1).getMessage(); 259 String message2 = ((IStatus) e2).getMessage(); 260 if (message1 == null) 261 return 1; 262 if (message2 == null) 263 return -1; 264 265 return message1.compareTo(message2); 266 } 267 }; 268 } 269 270 273 void refreshStatusList() { 274 if (statusListViewer != null 275 && !statusListViewer.getControl().isDisposed()) { 276 statusListViewer.refresh(); 277 Point newSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); 278 getShell().setSize(newSize); 279 } 280 } 281 282 288 private IStatus getSingleSelection() { 289 ISelection rawSelection = statusListViewer.getSelection(); 290 if (rawSelection != null 291 && rawSelection instanceof IStructuredSelection) { 292 IStructuredSelection selection = (IStructuredSelection) rawSelection; 293 if (selection.size() == 1) { 294 return (IStatus) selection.getFirstElement(); 295 } 296 } 297 return null; 298 } 299 300 304 void handleSelectionChange() { 305 IStatus newSelection = getSingleSelection(); 306 setStatus(newSelection); 307 updateEnablements(); 308 showDetailsArea(); 309 } 310 311 316 protected boolean shouldShowDetailsButton() { 317 return true; 318 } 319 320 324 public void addStatus(IStatus status) { 325 statuses.add(status); 326 refresh(); 327 328 } 329 330 331 } 332 | Popular Tags |