129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.source.projection.SourceViewerInformationControl;
|
|
14
|
131
|
15 import dwtx.jface.text.source.projection.ProjectionViewer; // packageimport
|
|
16 import dwtx.jface.text.source.projection.ProjectionSupport; // packageimport
|
|
17 import dwtx.jface.text.source.projection.IProjectionPosition; // packageimport
|
|
18 import dwtx.jface.text.source.projection.AnnotationBag; // packageimport
|
|
19 import dwtx.jface.text.source.projection.ProjectionSummary; // packageimport
|
|
20 import dwtx.jface.text.source.projection.ProjectionAnnotationHover; // packageimport
|
|
21 import dwtx.jface.text.source.projection.ProjectionRulerColumn; // packageimport
|
|
22 import dwtx.jface.text.source.projection.ProjectionAnnotationModel; // packageimport
|
|
23 import dwtx.jface.text.source.projection.IProjectionListener; // packageimport
|
|
24 import dwtx.jface.text.source.projection.ProjectionAnnotation; // packageimport
|
|
25
|
|
26
|
129
|
27 import dwt.dwthelper.utils;
|
|
28
|
|
29
|
|
30
|
|
31
|
|
32 import dwt.DWT;
|
|
33 import dwt.custom.StyledText;
|
|
34 import dwt.events.DisposeEvent;
|
|
35 import dwt.events.DisposeListener;
|
|
36 import dwt.events.FocusListener;
|
|
37 import dwt.events.KeyEvent;
|
|
38 import dwt.events.KeyListener;
|
|
39 import dwt.graphics.Color;
|
|
40 import dwt.graphics.Font;
|
|
41 import dwt.graphics.FontData;
|
|
42 import dwt.graphics.GC;
|
|
43 import dwt.graphics.Point;
|
|
44 import dwt.graphics.Rectangle;
|
|
45 import dwt.layout.GridData;
|
|
46 import dwt.layout.GridLayout;
|
|
47 import dwt.widgets.Composite;
|
|
48 import dwt.widgets.Control;
|
|
49 import dwt.widgets.Display;
|
|
50 import dwt.widgets.Label;
|
|
51 import dwt.widgets.Shell;
|
|
52 import dwtx.jface.resource.JFaceResources;
|
|
53 import dwtx.jface.text.Document;
|
|
54 import dwtx.jface.text.IDocument;
|
|
55 import dwtx.jface.text.IInformationControl;
|
|
56 import dwtx.jface.text.IInformationControlCreator;
|
|
57 import dwtx.jface.text.IInformationControlExtension;
|
|
58 import dwtx.jface.text.IInformationControlExtension3;
|
|
59 import dwtx.jface.text.IInformationControlExtension5;
|
|
60 import dwtx.jface.text.source.SourceViewer;
|
|
61 import dwtx.jface.text.source.SourceViewerConfiguration;
|
|
62
|
|
63 /**
|
|
64 * Source viewer based implementation of {@link dwtx.jface.text.IInformationControl}.
|
|
65 * Displays information in a source viewer.
|
|
66 *
|
|
67 * @since 3.0
|
|
68 */
|
|
69 class SourceViewerInformationControl : IInformationControl, IInformationControlExtension, IInformationControlExtension3, IInformationControlExtension5, DisposeListener {
|
|
70
|
|
71 /** The control's shell */
|
|
72 private Shell fShell;
|
|
73 /** The control's text widget */
|
|
74 private StyledText fText;
|
|
75 /** The symbolic font name of the text font */
|
|
76 private final String fSymbolicFontName;
|
|
77 /** The text font (do not dispose!) */
|
|
78 private Font fTextFont;
|
|
79 /** The control's source viewer */
|
|
80 private SourceViewer fViewer;
|
|
81 /** The optional status field. */
|
|
82 private Label fStatusField;
|
|
83 /** The separator for the optional status field. */
|
|
84 private Label fSeparator;
|
|
85 /** The font of the optional status text label.*/
|
|
86 private Font fStatusTextFont;
|
|
87 /** The maximal widget width. */
|
|
88 private int fMaxWidth;
|
|
89 /** The maximal widget height. */
|
|
90 private int fMaxHeight;
|
|
91
|
|
92
|
|
93 /**
|
|
94 * Creates a source viewer information control with the given shell as parent. The given shell
|
|
95 * styles are applied to the created shell. The given styles are applied to the created styled
|
|
96 * text widget. The text widget will be initialized with the given font. The status field will
|
|
97 * contain the given text or be hidden.
|
|
98 *
|
|
99 * @param parent the parent shell
|
|
100 * @param isResizable <code>true</code> if resizable
|
|
101 * @param symbolicFontName the symbolic font name
|
|
102 * @param statusFieldText the text to be used in the optional status field or <code>null</code>
|
|
103 * if the status field should be hidden
|
|
104 */
|
|
105 public SourceViewerInformationControl(Shell parent, bool isResizable, String symbolicFontName, String statusFieldText) {
|
|
106 GridLayout layout;
|
|
107 GridData gd;
|
|
108
|
|
109 int shellStyle= DWT.TOOL | DWT.ON_TOP | (isResizable ? DWT.RESIZE : 0);
|
|
110 int textStyle= isResizable ? DWT.V_SCROLL | DWT.H_SCROLL : DWT.NONE;
|
|
111
|
|
112 fShell= new Shell(parent, DWT.NO_FOCUS | DWT.ON_TOP | shellStyle);
|
|
113 Display display= fShell.getDisplay();
|
|
114
|
|
115 Composite composite= fShell;
|
|
116 layout= new GridLayout(1, false);
|
|
117 layout.marginHeight= 0;
|
|
118 layout.marginWidth= 0;
|
|
119 composite.setLayout(layout);
|
|
120 gd= new GridData(GridData.FILL_HORIZONTAL);
|
|
121 composite.setLayoutData(gd);
|
|
122
|
|
123 if (statusFieldText !is null) {
|
|
124 composite= new Composite(composite, DWT.NONE);
|
|
125 layout= new GridLayout(1, false);
|
|
126 layout.marginHeight= 0;
|
|
127 layout.marginWidth= 0;
|
|
128 composite.setLayout(layout);
|
|
129 gd= new GridData(GridData.FILL_BOTH);
|
|
130 composite.setLayoutData(gd);
|
|
131 composite.setForeground(display.getSystemColor(DWT.COLOR_INFO_FOREGROUND));
|
|
132 composite.setBackground(display.getSystemColor(DWT.COLOR_INFO_BACKGROUND));
|
|
133 }
|
|
134
|
|
135 // Source viewer
|
|
136 fViewer= new SourceViewer(composite, null, textStyle);
|
|
137 fViewer.configure(new SourceViewerConfiguration());
|
|
138 fViewer.setEditable(false);
|
|
139
|
|
140 fText= fViewer.getTextWidget();
|
|
141 gd= new GridData(GridData.BEGINNING | GridData.FILL_BOTH);
|
|
142 fText.setLayoutData(gd);
|
|
143 fText.setForeground(parent.getDisplay().getSystemColor(DWT.COLOR_INFO_FOREGROUND));
|
|
144 fText.setBackground(parent.getDisplay().getSystemColor(DWT.COLOR_INFO_BACKGROUND));
|
|
145 fSymbolicFontName= symbolicFontName;
|
|
146 fTextFont= JFaceResources.getFont(symbolicFontName);
|
|
147 fText.setFont(fTextFont);
|
|
148
|
|
149 fText.addKeyListener(new KeyListener() {
|
|
150
|
|
151 public void keyPressed(KeyEvent e) {
|
|
152 if (e.character is 0x1B) // ESC
|
|
153 fShell.dispose();
|
|
154 }
|
|
155
|
|
156 public void keyReleased(KeyEvent e) {}
|
|
157 });
|
|
158
|
|
159 // Status field
|
|
160 if (statusFieldText !is null) {
|
|
161
|
|
162 // Horizontal separator line
|
|
163 fSeparator= new Label(composite, DWT.SEPARATOR | DWT.HORIZONTAL | DWT.LINE_DOT);
|
|
164 fSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
|
|
165
|
|
166 // Status field label
|
|
167 fStatusField= new Label(composite, DWT.RIGHT);
|
|
168 fStatusField.setText(statusFieldText);
|
|
169 Font font= fStatusField.getFont();
|
|
170 FontData[] fontDatas= font.getFontData();
|
|
171 for (int i= 0; i < fontDatas.length; i++)
|
|
172 fontDatas[i].setHeight(fontDatas[i].getHeight() * 9 / 10);
|
|
173 fStatusTextFont= new Font(fStatusField.getDisplay(), fontDatas);
|
|
174 fStatusField.setFont(fStatusTextFont);
|
|
175 GridData gd2= new GridData(GridData.FILL_VERTICAL | GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING);
|
|
176 fStatusField.setLayoutData(gd2);
|
|
177
|
|
178 // Regarding the color see bug 41128
|
|
179 fStatusField.setForeground(display.getSystemColor(DWT.COLOR_WIDGET_DARK_SHADOW));
|
|
180
|
|
181 fStatusField.setBackground(display.getSystemColor(DWT.COLOR_INFO_BACKGROUND));
|
|
182 }
|
|
183
|
|
184 addDisposeListener(this);
|
|
185 }
|
|
186
|
|
187 /**
|
|
188 * @see dwtx.jface.text.IInformationControlExtension2#setInput(java.lang.Object)
|
|
189 * @param input the input object
|
|
190 */
|
|
191 public void setInput(Object input) {
|
|
192 if (input instanceof String)
|
|
193 setInformation((String)input);
|
|
194 else
|
|
195 setInformation(null);
|
|
196 }
|
|
197
|
|
198 /*
|
|
199 * @see IInformationControl#setInformation(String)
|
|
200 */
|
|
201 public void setInformation(String content) {
|
|
202 if (content is null) {
|
|
203 fViewer.setInput(null);
|
|
204 return;
|
|
205 }
|
|
206
|
|
207 IDocument doc= new Document(content);
|
|
208 fViewer.setInput(doc);
|
|
209 }
|
|
210
|
|
211 /*
|
|
212 * @see IInformationControl#setVisible(bool)
|
|
213 */
|
|
214 public void setVisible(bool visible) {
|
|
215 fShell.setVisible(visible);
|
|
216 }
|
|
217
|
|
218 /*
|
|
219 * @see dwt.events.DisposeListener#widgetDisposed(dwt.events.DisposeEvent)
|
|
220 */
|
|
221 public void widgetDisposed(DisposeEvent event) {
|
|
222 if (fStatusTextFont !is null && !fStatusTextFont.isDisposed())
|
|
223 fStatusTextFont.dispose();
|
|
224
|
|
225 fStatusTextFont= null;
|
|
226 fTextFont= null;
|
|
227 fShell= null;
|
|
228 fText= null;
|
|
229 }
|
|
230
|
|
231 /*
|
|
232 * @see dwtx.jface.text.IInformationControl#dispose()
|
|
233 */
|
|
234 public final void dispose() {
|
|
235 if (fShell !is null && !fShell.isDisposed())
|
|
236 fShell.dispose();
|
|
237 else
|
|
238 widgetDisposed(null);
|
|
239 }
|
|
240
|
|
241 /*
|
|
242 * @see IInformationControl#setSize(int, int)
|
|
243 */
|
|
244 public void setSize(int width, int height) {
|
|
245
|
|
246 if (fStatusField !is null) {
|
|
247 GridData gd= (GridData)fViewer.getTextWidget().getLayoutData();
|
|
248 Point statusSize= fStatusField.computeSize(DWT.DEFAULT, DWT.DEFAULT, true);
|
|
249 Point separatorSize= fSeparator.computeSize(DWT.DEFAULT, DWT.DEFAULT, true);
|
|
250 gd.heightHint= height - statusSize.y - separatorSize.y;
|
|
251 }
|
|
252 fShell.setSize(width, height);
|
|
253
|
|
254 if (fStatusField !is null)
|
|
255 fShell.pack(true);
|
|
256 }
|
|
257
|
|
258 /*
|
|
259 * @see IInformationControl#setLocation(Point)
|
|
260 */
|
|
261 public void setLocation(Point location) {
|
|
262 fShell.setLocation(location);
|
|
263 }
|
|
264
|
|
265 /*
|
|
266 * @see IInformationControl#setSizeConstraints(int, int)
|
|
267 */
|
|
268 public void setSizeConstraints(int maxWidth, int maxHeight) {
|
|
269 fMaxWidth= maxWidth;
|
|
270 fMaxHeight= maxHeight;
|
|
271 }
|
|
272
|
|
273 /*
|
|
274 * @see IInformationControl#computeSizeHint()
|
|
275 */
|
|
276 public Point computeSizeHint() {
|
|
277 // compute the preferred size
|
|
278 int x= DWT.DEFAULT;
|
|
279 int y= DWT.DEFAULT;
|
|
280 Point size= fShell.computeSize(x, y);
|
|
281 if (size.x > fMaxWidth)
|
|
282 x= fMaxWidth;
|
|
283 if (size.y > fMaxHeight)
|
|
284 y= fMaxHeight;
|
|
285
|
|
286 // recompute using the constraints if the preferred size is larger than the constraints
|
|
287 if (x !is DWT.DEFAULT || y !is DWT.DEFAULT)
|
|
288 size= fShell.computeSize(x, y, false);
|
|
289
|
|
290 return size;
|
|
291 }
|
|
292
|
|
293 /*
|
|
294 * @see IInformationControl#addDisposeListener(DisposeListener)
|
|
295 */
|
|
296 public void addDisposeListener(DisposeListener listener) {
|
|
297 fShell.addDisposeListener(listener);
|
|
298 }
|
|
299
|
|
300 /*
|
|
301 * @see IInformationControl#removeDisposeListener(DisposeListener)
|
|
302 */
|
|
303 public void removeDisposeListener(DisposeListener listener) {
|
|
304 fShell.removeDisposeListener(listener);
|
|
305 }
|
|
306
|
|
307 /*
|
|
308 * @see IInformationControl#setForegroundColor(Color)
|
|
309 */
|
|
310 public void setForegroundColor(Color foreground) {
|
|
311 fText.setForeground(foreground);
|
|
312 }
|
|
313
|
|
314 /*
|
|
315 * @see IInformationControl#setBackgroundColor(Color)
|
|
316 */
|
|
317 public void setBackgroundColor(Color background) {
|
|
318 fText.setBackground(background);
|
|
319 }
|
|
320
|
|
321 /*
|
|
322 * @see IInformationControl#isFocusControl()
|
|
323 */
|
|
324 public bool isFocusControl() {
|
|
325 return fShell.getDisplay().getActiveShell() is fShell;
|
|
326 }
|
|
327
|
|
328 /*
|
|
329 * @see IInformationControl#setFocus()
|
|
330 */
|
|
331 public void setFocus() {
|
|
332 fShell.forceFocus();
|
|
333 fText.setFocus();
|
|
334 }
|
|
335
|
|
336 /*
|
|
337 * @see IInformationControl#addFocusListener(FocusListener)
|
|
338 */
|
|
339 public void addFocusListener(FocusListener listener) {
|
|
340 fText.addFocusListener(listener);
|
|
341 }
|
|
342
|
|
343 /*
|
|
344 * @see IInformationControl#removeFocusListener(FocusListener)
|
|
345 */
|
|
346 public void removeFocusListener(FocusListener listener) {
|
|
347 fText.removeFocusListener(listener);
|
|
348 }
|
|
349
|
|
350 /*
|
|
351 * @see IInformationControlExtension#hasContents()
|
|
352 */
|
|
353 public bool hasContents() {
|
|
354 return fText.getCharCount() > 0;
|
|
355 }
|
|
356
|
|
357 /*
|
|
358 * @see dwtx.jface.text.IInformationControlExtension3#computeTrim()
|
|
359 * @since 3.4
|
|
360 */
|
|
361 public Rectangle computeTrim() {
|
|
362 Rectangle trim= fShell.computeTrim(0, 0, 0, 0);
|
|
363 addInternalTrim(trim);
|
|
364 return trim;
|
|
365 }
|
|
366
|
|
367 /**
|
|
368 * Adds the internal trimmings to the given trim of the shell.
|
|
369 *
|
|
370 * @param trim the shell's trim, will be updated
|
|
371 * @since 3.4
|
|
372 */
|
|
373 private void addInternalTrim(Rectangle trim) {
|
|
374 if (fStatusField !is null) {
|
|
375 trim.height+= fSeparator.computeSize(DWT.DEFAULT, DWT.DEFAULT).y;
|
|
376 trim.height+= fStatusField.computeSize(DWT.DEFAULT, DWT.DEFAULT).y;
|
|
377 }
|
|
378 }
|
|
379
|
|
380 /*
|
|
381 * @see dwtx.jface.text.IInformationControlExtension3#getBounds()
|
|
382 * @since 3.4
|
|
383 */
|
|
384 public Rectangle getBounds() {
|
|
385 return fShell.getBounds();
|
|
386 }
|
|
387
|
|
388 /*
|
|
389 * @see dwtx.jface.text.IInformationControlExtension3#restoresLocation()
|
|
390 * @since 3.4
|
|
391 */
|
|
392 public bool restoresLocation() {
|
|
393 return false;
|
|
394 }
|
|
395
|
|
396 /*
|
|
397 * @see dwtx.jface.text.IInformationControlExtension3#restoresSize()
|
|
398 * @since 3.4
|
|
399 */
|
|
400 public bool restoresSize() {
|
|
401 return false;
|
|
402 }
|
|
403
|
|
404 /*
|
|
405 * @see dwtx.jface.text.IInformationControlExtension5#getInformationPresenterControlCreator()
|
|
406 * @since 3.4
|
|
407 */
|
|
408 public IInformationControlCreator getInformationPresenterControlCreator() {
|
|
409 return new IInformationControlCreator() {
|
|
410 public IInformationControl createInformationControl(Shell parent) {
|
|
411 return new SourceViewerInformationControl(parent, true, fSymbolicFontName, null);
|
|
412 }
|
|
413 };
|
|
414 }
|
|
415
|
|
416 /*
|
|
417 * @see dwtx.jface.text.IInformationControlExtension5#containsControl(dwt.widgets.Control)
|
|
418 * @since 3.4
|
|
419 */
|
|
420 public bool containsControl(Control control) {
|
|
421 do {
|
|
422 if (control is fShell)
|
|
423 return true;
|
|
424 if (control instanceof Shell)
|
|
425 return false;
|
|
426 control= control.getParent();
|
|
427 } while (control !is null);
|
|
428 return false;
|
|
429 }
|
|
430
|
|
431 /*
|
|
432 * @see dwtx.jface.text.IInformationControlExtension5#isVisible()
|
|
433 * @since 3.4
|
|
434 */
|
|
435 public bool isVisible() {
|
|
436 return fShell !is null && !fShell.isDisposed() && fShell.isVisible();
|
|
437 }
|
|
438
|
|
439 /*
|
|
440 * @see dwtx.jface.text.IInformationControlExtension5#computeSizeConstraints(int, int)
|
|
441 */
|
|
442 public Point computeSizeConstraints(int widthInChars, int heightInChars) {
|
|
443 GC gc= new GC(fText);
|
|
444 gc.setFont(fTextFont);
|
|
445 int width= gc.getFontMetrics().getAverageCharWidth();
|
|
446 int height = gc.getFontMetrics().getHeight();
|
|
447 gc.dispose();
|
|
448
|
|
449 return new Point (widthInChars * width, heightInChars * height);
|
|
450 }
|
|
451 }
|