1 11 12 package org.eclipse.jface.text; 13 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.custom.StyledText; 16 import org.eclipse.swt.events.DisposeEvent; 17 import org.eclipse.swt.events.DisposeListener; 18 import org.eclipse.swt.events.FocusListener; 19 import org.eclipse.swt.events.KeyEvent; 20 import org.eclipse.swt.events.KeyListener; 21 import org.eclipse.swt.graphics.Color; 22 import org.eclipse.swt.graphics.Drawable; 23 import org.eclipse.swt.graphics.Point; 24 import org.eclipse.swt.graphics.Rectangle; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.Control; 28 import org.eclipse.swt.widgets.Display; 29 import org.eclipse.swt.widgets.Shell; 30 31 import org.eclipse.jface.dialogs.PopupDialog; 32 33 34 43 public class DefaultInformationControl implements IInformationControl, IInformationControlExtension, IInformationControlExtension3, DisposeListener { 44 45 50 public interface IInformationPresenter { 51 52 68 String updatePresentation(Display display, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight); 69 } 70 71 72 79 public interface IInformationPresenterExtension { 80 81 100 String updatePresentation(Drawable drawable, String hoverInfo, TextPresentation presentation, int maxWidth, int maxHeight); 101 } 102 103 104 108 private static final int INNER_BORDER= 1; 109 110 114 private PopupDialog fPopupDialog; 115 116 private StyledText fText; 117 118 private IInformationPresenter fPresenter; 119 120 private TextPresentation fPresentation= new TextPresentation(); 121 122 private int fMaxWidth= -1; 123 124 private int fMaxHeight= -1; 125 126 127 137 public DefaultInformationControl(Shell parent, int shellStyle, int style, IInformationPresenter presenter) { 138 this(parent, shellStyle, style, presenter, null); 139 } 140 141 154 public DefaultInformationControl(Shell parentShell, int shellStyle, final int style, IInformationPresenter presenter, String statusFieldText) { 155 shellStyle= shellStyle | SWT.NO_FOCUS | SWT.ON_TOP; 156 fPopupDialog= new PopupDialog(parentShell, shellStyle, false, false, false, false, null, statusFieldText) { 157 protected Control createDialogArea(Composite parent) { 158 fText= new StyledText(parent, SWT.MULTI | SWT.READ_ONLY | style); 160 GridData gd= new GridData(GridData.BEGINNING | GridData.FILL_BOTH); 161 gd.horizontalIndent= INNER_BORDER; 162 gd.verticalIndent= INNER_BORDER; 163 fText.setLayoutData(gd); 164 fText.addKeyListener(new KeyListener() { 165 166 public void keyPressed(KeyEvent e) { 167 if (e.character == 0x1B) close(); 169 } 170 171 public void keyReleased(KeyEvent e) {} 172 }); 173 return fText; 174 } 175 }; 176 177 fPresenter= presenter; 178 179 fPopupDialog.create(); 181 } 182 183 192 public DefaultInformationControl(Shell parent,int style, IInformationPresenter presenter) { 193 this(parent, SWT.TOOL | SWT.NO_TRIM, style, presenter); 194 } 195 196 208 public DefaultInformationControl(Shell parent,int style, IInformationPresenter presenter, String statusFieldText) { 209 this(parent, SWT.TOOL | SWT.NO_TRIM, style, presenter, statusFieldText); 210 } 211 212 219 public DefaultInformationControl(Shell parent) { 220 this(parent, SWT.NONE, null); 221 } 222 223 231 public DefaultInformationControl(Shell parent, IInformationPresenter presenter) { 232 this(parent, SWT.NONE, presenter); 233 } 234 235 238 public void setInformation(String content) { 239 if (fPresenter == null) { 240 fText.setText(content); 241 } else { 242 fPresentation.clear(); 243 if (fPresenter instanceof IInformationPresenterExtension) 244 content= ((IInformationPresenterExtension)fPresenter).updatePresentation(fPopupDialog.getShell(), content, fPresentation, fMaxWidth, fMaxHeight); 245 else 246 content= fPresenter.updatePresentation(fPopupDialog.getShell().getDisplay(), content, fPresentation, fMaxWidth, fMaxHeight); 247 if (content != null) { 248 fText.setText(content); 249 TextPresentation.applyTextPresentation(fPresentation, fText); 250 } else { 251 fText.setText(""); } 253 } 254 } 255 256 259 public void setVisible(boolean visible) { 260 if (visible) { 261 if (fText.getWordWrap()) { 262 Point currentSize= fPopupDialog.getShell().getSize(); 263 fPopupDialog.getShell().pack(true); 264 Point newSize= fPopupDialog.getShell().getSize(); 265 if (newSize.x > currentSize.x || newSize.y > currentSize.y) 266 setSize(currentSize.x, currentSize.y); } 268 fPopupDialog.open(); 269 } else 270 fPopupDialog.getShell().setVisible(false); 271 } 272 273 276 public void dispose() { 277 fPopupDialog.close(); 278 fPopupDialog= null; 279 } 280 281 284 public void setSize(int width, int height) { 285 fPopupDialog.getShell().setSize(width, height); 286 } 287 288 291 public void setLocation(Point location) { 292 fPopupDialog.getShell().setLocation(location); 293 } 294 295 298 public void setSizeConstraints(int maxWidth, int maxHeight) { 299 fMaxWidth= maxWidth; 300 fMaxHeight= maxHeight; 301 } 302 303 306 public Point computeSizeHint() { 307 int widthHint= SWT.DEFAULT; 309 if (fMaxWidth > -1 && fText.getWordWrap()) 310 widthHint= fMaxWidth; 311 312 return fPopupDialog.getShell().computeSize(widthHint, SWT.DEFAULT, true); 313 } 314 315 319 public Rectangle computeTrim() { 320 return fPopupDialog.getShell().computeTrim(0, 0, 0, 0); 321 } 322 323 327 public Rectangle getBounds() { 328 return fPopupDialog.getShell().getBounds(); 329 } 330 331 335 public boolean restoresLocation() { 336 return false; 337 } 338 339 343 public boolean restoresSize() { 344 return false; 345 } 346 347 350 public void addDisposeListener(DisposeListener listener) { 351 fPopupDialog.getShell().addDisposeListener(listener); 352 } 353 354 357 public void removeDisposeListener(DisposeListener listener) { 358 fPopupDialog.getShell().removeDisposeListener(listener); 359 } 360 361 364 public void setForegroundColor(Color foreground) { 365 fText.setForeground(foreground); 366 } 367 368 371 public void setBackgroundColor(Color background) { 372 fText.setBackground(background); 373 } 374 375 378 public boolean isFocusControl() { 379 return fText.isFocusControl(); 380 } 381 382 385 public void setFocus() { 386 fPopupDialog.getShell().forceFocus(); 387 fText.setFocus(); 388 } 389 390 393 public void addFocusListener(FocusListener listener) { 394 fText.addFocusListener(listener); 395 } 396 397 400 public void removeFocusListener(FocusListener listener) { 401 fText.removeFocusListener(listener); 402 } 403 404 407 public boolean hasContents() { 408 return fText.getCharCount() > 0; 409 } 410 411 416 public void widgetDisposed(DisposeEvent event) { 417 } 418 } 419 420 | Popular Tags |