📄 errordialog.java
字号:
* to display all such items without filtering. * * @param parent * the parent shell of the dialog, or <code>null</code> if none * @param dialogTitle * the title to use for this dialog, or <code>null</code> to * indicate that the default title should be used * @param message * the message to show in this dialog, or <code>null</code> to * indicate that the error's message should be shown as the * primary message * @param status * the error to show to the user * @return the code of the button that was pressed that resulted in this * dialog closing. This will be <code>Dialog.OK</code> if the OK * button was pressed, or <code>Dialog.CANCEL</code> if this * dialog's close window decoration or the ESC key was used. */ public static int openError(Shell parent, String dialogTitle, String message, IStatus status) { return openError(parent, dialogTitle, message, status, IStatus.OK | IStatus.INFO | IStatus.WARNING | IStatus.ERROR); } /** * Opens an error dialog to display the given error. Use this method if the * error object being displayed contains child items <it>and </it> you wish * to specify a mask which will be used to filter the displaying of these * children. The error dialog will only be displayed if there is at least * one child status matching the mask. * * @param parentShell * the parent shell of the dialog, or <code>null</code> if none * @param title * the title to use for this dialog, or <code>null</code> to * indicate that the default title should be used * @param message * the message to show in this dialog, or <code>null</code> to * indicate that the error's message should be shown as the * primary message * @param status * the error to show to the user * @param displayMask * the mask to use to filter the displaying of child items, as * per <code>IStatus.matches</code> * @return the code of the button that was pressed that resulted in this * dialog closing. This will be <code>Dialog.OK</code> if the OK * button was pressed, or <code>Dialog.CANCEL</code> if this * dialog's close window decoration or the ESC key was used. * @see org.eclipse.core.runtime.IStatus#matches(int) */ public static int openError(Shell parentShell, String title, String message, IStatus status, int displayMask) { ErrorDialog dialog = new ErrorDialog(parentShell, title, message, status, displayMask); return dialog.open(); } /** * Populates the list using this error dialog's status object. This walks * the child static of the status object and displays them in a list. The * format for each entry is status_path : status_message If the status's * path was null then it (and the colon) are omitted. * @param listToPopulate The list to fill. */ private void populateList(List listToPopulate) { populateList(listToPopulate, status, 0, shouldIncludeTopLevelErrorInDetails); } /** * Populate the list with the messages from the given status. Traverse the * children of the status deeply and also traverse CoreExceptions that appear * in the status. * @param listToPopulate the list to populate * @param buildingStatus the status being displayed * @param nesting the nesting level (increases one level for each level of children) * @param includeStatus whether to include the buildingStatus in the display or * just its children */ private void populateList(List listToPopulate, IStatus buildingStatus, int nesting, boolean includeStatus) { if (!buildingStatus.matches(displayMask)) { return; } Throwable t = buildingStatus.getException(); boolean isCoreException= t instanceof CoreException; boolean incrementNesting= false; if (includeStatus) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < nesting; i++) { sb.append(NESTING_INDENT); } String message = buildingStatus.getMessage(); sb.append(message); listToPopulate.add(sb.toString()); incrementNesting= true; } if (!isCoreException && t != null) { // Include low-level exception message StringBuffer sb = new StringBuffer(); for (int i = 0; i < nesting; i++) { sb.append(NESTING_INDENT); } String message = t.getLocalizedMessage(); if (message == null) { message = t.toString(); } sb.append(message); listToPopulate.add(sb.toString()); incrementNesting= true; } if (incrementNesting) { nesting++; } // Look for a nested core exception if (isCoreException) { CoreException ce = (CoreException)t; IStatus eStatus = ce.getStatus(); // Only print the exception message if it is not contained in the parent message if (message == null || message.indexOf(eStatus.getMessage()) == -1) { populateList(listToPopulate, eStatus, nesting, true); } } // Look for child status IStatus[] children = buildingStatus.getChildren(); for (int i = 0; i < children.length; i++) { populateList(listToPopulate, children[i], nesting, true); } } /** * Returns whether the given status object should be displayed. * * @param status * a status object * @param mask * a mask as per <code>IStatus.matches</code> * @return <code>true</code> if the given status should be displayed, and * <code>false</code> otherwise * @see org.eclipse.core.runtime.IStatus#matches(int) */ protected static boolean shouldDisplay(IStatus status, int mask) { IStatus[] children = status.getChildren(); if (children == null || children.length == 0) { return status.matches(mask); } for (int i = 0; i < children.length; i++) { if (children[i].matches(mask)) { return true; } } return false; } /** * Toggles the unfolding of the details area. This is triggered by the user * pressing the details button. */ private void toggleDetailsArea() { Point windowSize = getShell().getSize(); Point oldSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); if (listCreated) { list.dispose(); listCreated = false; detailsButton.setText(IDialogConstants.SHOW_DETAILS_LABEL); } else { list = createDropDownList((Composite) getContents()); detailsButton.setText(IDialogConstants.HIDE_DETAILS_LABEL); } Point newSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); getShell() .setSize( new Point(windowSize.x, windowSize.y + (newSize.y - oldSize.y))); } /** * Put the details of the status of the error onto the stream. * * @param buildingStatus * @param buffer * @param nesting */ private void populateCopyBuffer(IStatus buildingStatus, StringBuffer buffer, int nesting) { if (!buildingStatus.matches(displayMask)) { return; } for (int i = 0; i < nesting; i++) { buffer.append(NESTING_INDENT); } buffer.append(buildingStatus.getMessage()); buffer.append("\n"); //$NON-NLS-1$ // Look for a nested core exception Throwable t = buildingStatus.getException(); if (t instanceof CoreException) { CoreException ce = (CoreException)t; populateCopyBuffer(ce.getStatus(), buffer, nesting + 1); } IStatus[] children = buildingStatus.getChildren(); for (int i = 0; i < children.length; i++) { populateCopyBuffer(children[i], buffer, nesting + 1); } } /** * Copy the contents of the statuses to the clipboard. */ private void copyToClipboard() { if (clipboard != null) { clipboard.dispose(); } StringBuffer statusBuffer = new StringBuffer(); populateCopyBuffer(status, statusBuffer, 0); clipboard = new Clipboard(list.getDisplay()); clipboard.setContents(new Object[] { statusBuffer.toString() }, new Transfer[] { TextTransfer.getInstance() }); } /* * (non-Javadoc) * * @see org.eclipse.jface.window.Window#close() */ public boolean close() { if (clipboard != null) { clipboard.dispose(); } return super.close(); } /** * Show the details portion of the dialog if it is not already visible. * This method will only work when it is invoked after the control of the dialog * has been set. In other words, after the <code>createContents</code> method * has been invoked and has returned the control for the content area of the dialog. * Invoking the method before the content area has been set or after the dialog has been * disposed will have no effect. * @since 3.1 */ protected final void showDetailsArea() { if (!listCreated) { Control control = getContents(); if (control != null && ! control.isDisposed()) { toggleDetailsArea(); } } } /** * Return whether the Details button should be included. * This method is invoked once when the dialog is built. * By default, the Details button is only included if * the status used when creating the dialog was a multi-status * or if the status contains an exception. * Subclasses may override. * @return whether the Details button should be included * @since 3.1 */ protected boolean shouldShowDetailsButton() { return status.isMultiStatus() || status.getException() != null; } /** * Set the status displayed by this error dialog to the given status. * This only affects the status displayed by the Details list. * The message, image and title should be updated by the subclass, * if desired. * @param status the status to be displayed in the details list * @since 3.1 */ protected final void setStatus(IStatus status) { if (this.status != status) { this.status = status; } shouldIncludeTopLevelErrorInDetails = true; if (listCreated) { repopulateList(); } } /** * Repopulate the supplied list widget. */ private void repopulateList() { if (list != null && !list.isDisposed()) { list.removeAll(); populateList(list); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -