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