KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > BrowserText


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.browser;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.io.StringWriter JavaDoc;
15
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.custom.BusyIndicator;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.Rectangle;
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.Label;
29 import org.eclipse.swt.widgets.Link;
30 import org.eclipse.swt.widgets.Text;
31
32 public class BrowserText {
33     private String JavaDoc url;
34
35     private FallbackScrolledComposite scomp;
36
37     private Label title;
38
39     private Label exTitle;
40
41     private Label text;
42
43     private Label sep;
44
45     protected Link link;
46
47     private BrowserViewer viewer;
48
49     private Button button;
50
51     private Text exception;
52
53     private boolean expanded;
54
55     private Throwable JavaDoc ex;
56
57     class ReflowScrolledComposite extends FallbackScrolledComposite {
58         public ReflowScrolledComposite(Composite parent, int style) {
59             super(parent, style);
60         }
61
62         public void reflow(boolean flushCache) {
63             updateWidth(this);
64             super.reflow(flushCache);
65         }
66     }
67
68     public BrowserText(Composite parent, BrowserViewer viewer, Throwable JavaDoc ex) {
69         this.viewer = viewer;
70         this.ex = ex;
71         Color bg = parent.getDisplay()
72                 .getSystemColor(SWT.COLOR_LIST_BACKGROUND);
73         scomp = new ReflowScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
74         Composite client = new Composite(scomp, SWT.NULL);
75         fillContent(client, bg);
76         scomp.setContent(client);
77         scomp.setBackground(bg);
78     }
79
80     private void fillContent(Composite parent, Color bg) {
81         GridLayout layout = new GridLayout();
82         layout.verticalSpacing = 10;
83         parent.setLayout(layout);
84         title = new Label(parent, SWT.WRAP);
85         title.setText(Messages.BrowserText_title);
86         title.setFont(JFaceResources.getHeaderFont());
87         title.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
88         title.setBackground(bg);
89
90         link = new Link(parent, SWT.WRAP);
91         link.setText(Messages.BrowserText_link);
92         link.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
93         link.setToolTipText(Messages.BrowserText_tooltip);
94         link.addSelectionListener(new SelectionAdapter() {
95             public void widgetSelected(SelectionEvent e) {
96                 BusyIndicator.showWhile(link.getDisplay(), new Runnable JavaDoc() {
97                     public void run() {
98                         doOpenExternal();
99                     }
100                 });
101             }
102         });
103         link.setBackground(bg);
104         sep = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
105         sep.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
106         exTitle = new Label(parent, SWT.NULL);
107         exTitle.setBackground(bg);
108         exTitle.setFont(JFaceResources.getBannerFont());
109         exTitle.setText(Messages.BrowserText_dtitle);
110         exTitle.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
111         text = new Label(parent, SWT.WRAP);
112         text.setText(Messages.BrowserText_text);
113         text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
114         text.setBackground(bg);
115         button = new Button(parent, SWT.PUSH);
116         updateButtonText();
117         button.addSelectionListener(new SelectionAdapter() {
118             public void widgetSelected(SelectionEvent e) {
119                 toggleException();
120             }
121         });
122         exception = new Text(parent, SWT.MULTI);
123         loadExceptionText();
124         GridData gd = new GridData(GridData.FILL_BOTH);
125         gd.exclude = true;
126         exception.setLayoutData(gd);
127     }
128
129     private void loadExceptionText() {
130         StringWriter JavaDoc swriter = new StringWriter JavaDoc();
131         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
132         writer.println(ex.getMessage());
133         ex.printStackTrace(writer);
134         writer.close();
135         exception.setText(swriter.toString());
136     }
137
138     protected void toggleException() {
139         expanded = !expanded;
140         updateButtonText();
141         GridData gd = (GridData) exception.getLayoutData();
142         gd.exclude = !expanded;
143         exception.setVisible(expanded);
144         refresh();
145     }
146
147     private void updateButtonText() {
148         if (expanded)
149             button.setText(Messages.BrowserText_button_collapse);
150         else
151             button.setText(Messages.BrowserText_button_expand);
152     }
153
154     protected void updateWidth(Composite parent) {
155         Rectangle area = parent.getClientArea();
156         updateWidth(title, area.width);
157         updateWidth(text, area.width);
158         updateWidth(sep, area.width);
159         updateWidth(link, area.width);
160         updateWidth(exTitle, area.width);
161         updateWidth(exception, area.width);
162     }
163
164     private void updateWidth(Control c, int width) {
165         GridData gd = (GridData) c.getLayoutData();
166         if (gd != null)
167             gd.widthHint = width - 10;
168     }
169
170     protected void doOpenExternal() {
171         IBrowserViewerContainer container = viewer.getContainer();
172         if (container != null)
173             container.openInExternalBrowser(url);
174     }
175
176     public Control getControl() {
177         return scomp;
178     }
179
180     public boolean setUrl(String JavaDoc url) {
181         this.url = url;
182         return true;
183     }
184
185     public void setFocus() {
186         link.setFocus();
187     }
188
189     public String JavaDoc getUrl() {
190         return url;
191     }
192
193     public void refresh() {
194         scomp.reflow(true);
195     }
196 }
197
Popular Tags