001: /*******************************************************************************
002: * Copyright (c) 2004, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.part;
011:
012: import java.io.PrintWriter;
013: import java.io.StringWriter;
014:
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.jface.dialogs.IDialogConstants;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.events.SelectionAdapter;
019: import org.eclipse.swt.events.SelectionEvent;
020: import org.eclipse.swt.graphics.Image;
021: import org.eclipse.swt.layout.FillLayout;
022: import org.eclipse.swt.layout.GridData;
023: import org.eclipse.swt.layout.GridLayout;
024: import org.eclipse.swt.widgets.Button;
025: import org.eclipse.swt.widgets.Composite;
026: import org.eclipse.swt.widgets.Control;
027: import org.eclipse.swt.widgets.Display;
028: import org.eclipse.swt.widgets.Label;
029: import org.eclipse.swt.widgets.Text;
030:
031: /**
032: * @since 3.1
033: */
034: public class StatusPart {
035:
036: boolean showingDetails = false;
037: private Button detailsButton;
038: private Composite detailsArea;
039: private Control details = null;
040: private IStatus reason;
041:
042: public StatusPart(final Composite parent, IStatus reason_) {
043: this .reason = reason_;
044: GridLayout layout = new GridLayout();
045:
046: layout.numColumns = 3;
047:
048: int spacing = 8;
049: int margins = 8;
050: layout.marginBottom = margins;
051: layout.marginTop = margins;
052: layout.marginLeft = margins;
053: layout.marginRight = margins;
054: layout.horizontalSpacing = spacing;
055: layout.verticalSpacing = spacing;
056: parent.setLayout(layout);
057:
058: Label imageLabel = new Label(parent, SWT.NONE);
059: Image image = getImage();
060: if (image != null) {
061: image.setBackground(imageLabel.getBackground());
062: imageLabel.setImage(image);
063: imageLabel.setLayoutData(new GridData(
064: GridData.HORIZONTAL_ALIGN_CENTER
065: | GridData.VERTICAL_ALIGN_BEGINNING));
066: }
067:
068: Text text = new Text(parent, SWT.MULTI | SWT.READ_ONLY
069: | SWT.WRAP);
070: text.setBackground(text.getDisplay().getSystemColor(
071: SWT.COLOR_WIDGET_BACKGROUND));
072:
073: //text.setForeground(JFaceColors.getErrorText(text.getDisplay()));
074: text.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL
075: | GridData.GRAB_HORIZONTAL
076: | GridData.VERTICAL_ALIGN_BEGINNING));
077: text.setText(reason.getMessage());
078:
079: detailsButton = new Button(parent, SWT.PUSH);
080: detailsButton.addSelectionListener(new SelectionAdapter() {
081: public void widgetSelected(SelectionEvent e) {
082: showDetails(!showingDetails);
083: }
084: });
085:
086: detailsButton.setLayoutData(new GridData(
087: GridData.HORIZONTAL_ALIGN_BEGINNING
088: | GridData.VERTICAL_ALIGN_BEGINNING));
089: detailsButton.setVisible(reason.getException() != null);
090:
091: updateDetailsText();
092:
093: detailsArea = new Composite(parent, SWT.NONE);
094: GridData data = new GridData(GridData.FILL_BOTH);
095: data.horizontalSpan = 3;
096: data.verticalSpan = 1;
097: detailsArea.setLayoutData(data);
098: detailsArea.setLayout(new FillLayout());
099: parent.layout(true);
100: }
101:
102: /**
103: * Return the image for the upper-left corner of this part
104: *
105: * @return
106: */
107: private Image getImage() {
108: Display d = Display.getCurrent();
109:
110: switch (reason.getSeverity()) {
111: case IStatus.ERROR:
112: return d.getSystemImage(SWT.ICON_ERROR);
113: case IStatus.WARNING:
114: return d.getSystemImage(SWT.ICON_WARNING);
115: default:
116: return d.getSystemImage(SWT.ICON_INFORMATION);
117: }
118: }
119:
120: private void showDetails(boolean shouldShow) {
121: if (shouldShow == showingDetails) {
122: return;
123: }
124: this .showingDetails = shouldShow;
125: updateDetailsText();
126: }
127:
128: private void updateDetailsText() {
129: if (details != null) {
130: details.dispose();
131: details = null;
132: }
133:
134: if (showingDetails) {
135: detailsButton.setText(IDialogConstants.HIDE_DETAILS_LABEL);
136: Text detailsText = new Text(detailsArea, SWT.BORDER
137: | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI
138: | SWT.READ_ONLY | SWT.LEFT_TO_RIGHT);
139: detailsText.setText(getDetails(reason));
140: detailsText.setBackground(detailsText.getDisplay()
141: .getSystemColor(SWT.COLOR_LIST_BACKGROUND));
142: details = detailsText;
143: detailsArea.layout(true);
144: } else {
145: detailsButton.setText(IDialogConstants.SHOW_DETAILS_LABEL);
146: }
147: }
148:
149: private String getDetails(IStatus status) {
150: if (status.getException() != null) {
151: return getStackTrace(status.getException());
152: }
153:
154: return ""; //$NON-NLS-1$
155: }
156:
157: private String getStackTrace(Throwable throwable) {
158: StringWriter swriter = new StringWriter();
159: PrintWriter pwriter = new PrintWriter(swriter);
160: throwable.printStackTrace(pwriter);
161: pwriter.flush();
162: pwriter.close();
163: return swriter.toString();
164: }
165: }
|