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