KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > part > StatusPart


1 /*******************************************************************************
2  * Copyright (c) 2004, 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  *******************************************************************************/

11 package org.eclipse.ui.internal.part;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.io.StringWriter JavaDoc;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.layout.FillLayout;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Text;
31
32 /**
33  * @since 3.1
34  */

35 public class StatusPart {
36     
37     boolean showingDetails = false;
38     private Button detailsButton;
39     private Composite detailsArea;
40     private Control details = null;
41     private IStatus reason;
42     
43     public StatusPart(final Composite parent, IStatus reason_) {
44         this.reason = reason_;
45         GridLayout layout = new GridLayout();
46         
47         layout.numColumns = 3;
48         
49         int spacing = 8;
50         int margins = 8;
51         layout.marginBottom = margins;
52         layout.marginTop = margins;
53         layout.marginLeft = margins;
54         layout.marginRight = margins;
55         layout.horizontalSpacing = spacing;
56         layout.verticalSpacing = spacing;
57         parent.setLayout(layout);
58         
59         Label imageLabel = new Label(parent, SWT.NONE);
60         Image image = getImage();
61         if (image != null) {
62             image.setBackground(imageLabel.getBackground());
63             imageLabel.setImage(image);
64             imageLabel.setLayoutData(new GridData(
65                     GridData.HORIZONTAL_ALIGN_CENTER
66                             | GridData.VERTICAL_ALIGN_BEGINNING));
67         }
68         
69         Text text = new Text(parent, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
70         text.setBackground(text.getDisplay().getSystemColor(
71                 SWT.COLOR_WIDGET_BACKGROUND));
72
73         //text.setForeground(JFaceColors.getErrorText(text.getDisplay()));
74
text.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
75         text.setText(reason.getMessage());
76         
77         detailsButton = new Button(parent, SWT.PUSH);
78         detailsButton.addSelectionListener(new SelectionAdapter() {
79             public void widgetSelected(SelectionEvent e) {
80                 showDetails(!showingDetails);
81             }
82         });
83         
84         detailsButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING));
85         detailsButton.setVisible(reason.getException() != null);
86         
87         updateDetailsText();
88         
89         detailsArea = new Composite(parent, SWT.NONE);
90         GridData data = new GridData(GridData.FILL_BOTH);
91         data.horizontalSpan = 3;
92         data.verticalSpan = 1;
93         detailsArea.setLayoutData(data);
94         detailsArea.setLayout(new FillLayout());
95         parent.layout(true);
96     }
97     
98     /**
99      * Return the image for the upper-left corner of this part
100      *
101      * @return
102      */

103     private Image getImage() {
104         Display d = Display.getCurrent();
105         
106         switch(reason.getSeverity()) {
107         case IStatus.ERROR:
108             return d.getSystemImage(SWT.ICON_ERROR);
109         case IStatus.WARNING:
110             return d.getSystemImage(SWT.ICON_WARNING);
111         default:
112             return d.getSystemImage(SWT.ICON_INFORMATION);
113         }
114     }
115     
116     private void showDetails(boolean shouldShow) {
117         if (shouldShow == showingDetails) {
118             return;
119         }
120         this.showingDetails = shouldShow;
121         updateDetailsText();
122     }
123     
124     private void updateDetailsText() {
125         if (details != null) {
126             details.dispose();
127             details = null;
128         }
129         
130         if (showingDetails) {
131             detailsButton.setText(IDialogConstants.HIDE_DETAILS_LABEL);
132             Text detailsText = new Text(detailsArea, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL
133                     | SWT.MULTI | SWT.READ_ONLY | SWT.LEFT_TO_RIGHT);
134             detailsText.setText(getDetails(reason));
135             detailsText.setBackground(detailsText.getDisplay().getSystemColor(
136                     SWT.COLOR_LIST_BACKGROUND));
137             details = detailsText;
138             detailsArea.layout(true);
139         } else {
140             detailsButton.setText(IDialogConstants.SHOW_DETAILS_LABEL);
141         }
142     }
143
144     
145     private String JavaDoc getDetails(IStatus status) {
146         if (status.getException() != null) {
147             return getStackTrace(status.getException());
148         }
149         
150         return ""; //$NON-NLS-1$
151
}
152
153     private String JavaDoc getStackTrace(Throwable JavaDoc throwable) {
154         StringWriter JavaDoc swriter = new StringWriter JavaDoc();
155         PrintWriter JavaDoc pwriter = new PrintWriter JavaDoc(swriter);
156         throwable.printStackTrace(pwriter);
157         pwriter.flush();
158         pwriter.close();
159         return swriter.toString();
160     }
161 }
162
Popular Tags