1 11 package org.eclipse.jdt.internal.ui.text.java.hover; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.custom.StyledText; 15 import org.eclipse.swt.events.DisposeEvent; 16 import org.eclipse.swt.events.DisposeListener; 17 import org.eclipse.swt.events.FocusListener; 18 import org.eclipse.swt.events.KeyEvent; 19 import org.eclipse.swt.events.KeyListener; 20 import org.eclipse.swt.graphics.Color; 21 import org.eclipse.swt.graphics.Font; 22 import org.eclipse.swt.graphics.FontData; 23 import org.eclipse.swt.graphics.Point; 24 import org.eclipse.swt.graphics.RGB; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.layout.GridLayout; 27 import org.eclipse.swt.widgets.Composite; 28 import org.eclipse.swt.widgets.Display; 29 import org.eclipse.swt.widgets.Label; 30 import org.eclipse.swt.widgets.Shell; 31 32 import org.eclipse.jface.preference.IPreferenceStore; 33 import org.eclipse.jface.preference.PreferenceConverter; 34 import org.eclipse.jface.resource.JFaceResources; 35 36 import org.eclipse.jface.text.Document; 37 import org.eclipse.jface.text.IDocument; 38 import org.eclipse.jface.text.IInformationControl; 39 import org.eclipse.jface.text.IInformationControlExtension; 40 import org.eclipse.jface.text.source.ISourceViewer; 41 import org.eclipse.jface.text.source.SourceViewer; 42 43 import org.eclipse.jdt.ui.PreferenceConstants; 44 import org.eclipse.jdt.ui.text.IJavaPartitions; 45 46 import org.eclipse.jdt.internal.ui.JavaPlugin; 47 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer; 48 import org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration; 49 50 56 public class SourceViewerInformationControl implements IInformationControl, IInformationControlExtension, DisposeListener { 57 58 59 private static final int BORDER= 1; 60 61 private Shell fShell; 62 63 private StyledText fText; 64 65 private SourceViewer fViewer; 66 71 private Label fStatusField; 72 77 private Label fSeparator; 78 83 private Font fStatusTextFont; 84 88 private int fMaxWidth= SWT.DEFAULT; 89 93 private int fMaxHeight= SWT.DEFAULT; 94 95 private Color fBackgroundColor; 96 private boolean fIsSystemBackgroundColor= true; 97 98 99 108 public SourceViewerInformationControl(Shell parent, int shellStyle, int style) { 109 this(parent, shellStyle, style, null); 110 } 111 112 124 public SourceViewerInformationControl(Shell parent, int shellStyle, int style, String statusFieldText) { 125 GridLayout layout; 126 GridData gd; 127 128 fShell= new Shell(parent, SWT.NO_FOCUS | SWT.ON_TOP | shellStyle); 129 Display display= fShell.getDisplay(); 130 fShell.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); 131 132 initializeColors(); 133 134 Composite composite= fShell; 135 layout= new GridLayout(1, false); 136 int border= ((shellStyle & SWT.NO_TRIM) == 0) ? 0 : BORDER; 137 layout.marginHeight= border; 138 layout.marginWidth= border; 139 composite.setLayout(layout); 140 gd= new GridData(GridData.FILL_HORIZONTAL); 141 composite.setLayoutData(gd); 142 143 if (statusFieldText != null) { 144 composite= new Composite(composite, SWT.NONE); 145 layout= new GridLayout(1, false); 146 layout.marginHeight= 0; 147 layout.marginWidth= 0; 148 composite.setLayout(layout); 149 gd= new GridData(GridData.FILL_BOTH); 150 composite.setLayoutData(gd); 151 composite.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND)); 152 composite.setBackground(fBackgroundColor); 153 } 154 155 IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore(); 157 fViewer= new JavaSourceViewer(composite, null, null, false, style, store); 158 fViewer.configure(new SimpleJavaSourceViewerConfiguration(JavaPlugin.getDefault().getJavaTextTools().getColorManager(), store, null, IJavaPartitions.JAVA_PARTITIONING, false)); 159 fViewer.setEditable(false); 160 161 fText= fViewer.getTextWidget(); 162 gd= new GridData(GridData.BEGINNING | GridData.FILL_BOTH); 163 fText.setLayoutData(gd); 164 fText.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND)); 165 fText.setBackground(fBackgroundColor); 166 167 initializeFont(); 168 169 fText.addKeyListener(new KeyListener() { 170 171 public void keyPressed(KeyEvent e) { 172 if (e.character == 0x1B) fShell.dispose(); 174 } 175 176 public void keyReleased(KeyEvent e) {} 177 }); 178 179 if (statusFieldText != null) { 181 182 fSeparator= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.LINE_DOT); 184 fSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 185 186 fStatusField= new Label(composite, SWT.RIGHT); 188 fStatusField.setText(statusFieldText); 189 Font font= fStatusField.getFont(); 190 FontData[] fontDatas= font.getFontData(); 191 for (int i= 0; i < fontDatas.length; i++) 192 fontDatas[i].setHeight(fontDatas[i].getHeight() * 9 / 10); 193 fStatusTextFont= new Font(fStatusField.getDisplay(), fontDatas); 194 fStatusField.setFont(fStatusTextFont); 195 GridData gd2= new GridData(GridData.FILL_VERTICAL | GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING); 196 fStatusField.setLayoutData(gd2); 197 198 fStatusField.setForeground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW)); 200 fStatusField.setBackground(fBackgroundColor); 201 } 202 203 addDisposeListener(this); 204 } 205 206 private void initializeColors() { 207 RGB bgRGB= getHoverBackgroundColorRGB(); 208 if (bgRGB != null) { 209 fBackgroundColor= new Color(fShell.getDisplay(), bgRGB); 210 fIsSystemBackgroundColor= false; 211 } else { 212 fBackgroundColor= fShell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND); 213 fIsSystemBackgroundColor= true; 214 } 215 } 216 217 private RGB getHoverBackgroundColorRGB() { 218 IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); 219 return store.getBoolean(PreferenceConstants.EDITOR_SOURCE_HOVER_BACKGROUND_COLOR_SYSTEM_DEFAULT) 220 ? null 221 : PreferenceConverter.getColor(store, PreferenceConstants.EDITOR_SOURCE_HOVER_BACKGROUND_COLOR); 222 } 223 224 232 public SourceViewerInformationControl(Shell parent,int style) { 233 this(parent, SWT.NO_TRIM | SWT.TOOL, style); 234 } 235 236 247 public SourceViewerInformationControl(Shell parent,int style, String statusFieldText) { 248 this(parent, SWT.NO_TRIM | SWT.TOOL, style, statusFieldText); 249 } 250 251 258 public SourceViewerInformationControl(Shell parent) { 259 this(parent, SWT.NONE); 260 } 261 262 272 public SourceViewerInformationControl(Shell parent, String statusFieldText) { 273 this(parent, SWT.NONE, statusFieldText); 274 } 275 276 281 private void initializeFont() { 282 Font font= JFaceResources.getFont("org.eclipse.jdt.ui.editors.textfont"); StyledText styledText= getViewer().getTextWidget(); 284 styledText.setFont(font); 285 } 286 287 290 public void setInput(Object input) { 291 if (input instanceof String ) 292 setInformation((String )input); 293 else 294 setInformation(null); 295 } 296 297 300 public void setInformation(String content) { 301 if (content == null) { 302 fViewer.setInput(null); 303 return; 304 } 305 306 IDocument doc= new Document(content); 307 JavaPlugin.getDefault().getJavaTextTools().setupJavaDocumentPartitioner(doc, IJavaPartitions.JAVA_PARTITIONING); 308 fViewer.setInput(doc); 309 } 310 311 314 public void setVisible(boolean visible) { 315 fShell.setVisible(visible); 316 } 317 318 322 public void widgetDisposed(DisposeEvent event) { 323 if (fStatusTextFont != null && !fStatusTextFont.isDisposed()) 324 fStatusTextFont.dispose(); 325 326 fStatusTextFont= null; 327 fShell= null; 328 fText= null; 329 } 330 331 334 public final void dispose() { 335 if (!fIsSystemBackgroundColor) 336 fBackgroundColor.dispose(); 337 if (fShell != null && !fShell.isDisposed()) 338 fShell.dispose(); 339 else 340 widgetDisposed(null); 341 } 342 343 346 public void setSize(int width, int height) { 347 348 if (fStatusField != null) { 349 GridData gd= (GridData)fViewer.getTextWidget().getLayoutData(); 350 Point statusSize= fStatusField.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 351 Point separatorSize= fSeparator.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 352 gd.heightHint= height - statusSize.y - separatorSize.y; 353 } 354 fShell.setSize(width, height); 355 356 if (fStatusField != null) 357 fShell.pack(true); 358 } 359 360 363 public void setLocation(Point location) { 364 fShell.setLocation(location); 365 } 366 367 370 public void setSizeConstraints(int maxWidth, int maxHeight) { 371 fMaxWidth= maxWidth; 372 fMaxHeight= maxHeight; 373 } 374 375 378 public Point computeSizeHint() { 379 int x= SWT.DEFAULT; 381 int y= SWT.DEFAULT; 382 Point size= fShell.computeSize(x, y); 383 if (size.x > fMaxWidth) 384 x= fMaxWidth; 385 if (size.y > fMaxHeight) 386 y= fMaxHeight; 387 388 if (x != SWT.DEFAULT || y != SWT.DEFAULT) 390 size= fShell.computeSize(x, y, false); 391 392 return size; 393 } 394 395 398 public void addDisposeListener(DisposeListener listener) { 399 fShell.addDisposeListener(listener); 400 } 401 402 405 public void removeDisposeListener(DisposeListener listener) { 406 fShell.removeDisposeListener(listener); 407 } 408 409 412 public void setForegroundColor(Color foreground) { 413 fText.setForeground(foreground); 414 } 415 416 419 public void setBackgroundColor(Color background) { 420 fText.setBackground(background); 421 } 422 423 426 public boolean isFocusControl() { 427 return fText.isFocusControl(); 428 } 429 430 433 public void setFocus() { 434 fShell.forceFocus(); 435 fText.setFocus(); 436 } 437 438 441 public void addFocusListener(FocusListener listener) { 442 fText.addFocusListener(listener); 443 } 444 445 448 public void removeFocusListener(FocusListener listener) { 449 fText.removeFocusListener(listener); 450 } 451 452 455 public boolean hasContents() { 456 return fText.getCharCount() > 0; 457 } 458 459 protected ISourceViewer getViewer() { 460 return fViewer; 461 } 462 } 463 | Popular Tags |