1 11 12 package org.eclipse.pde.internal.ui.editor.contentassist.display; 13 14 import java.io.IOException ; 15 import java.io.StringReader ; 16 import java.util.Iterator ; 17 18 import org.eclipse.core.runtime.ListenerList; 19 20 import org.eclipse.pde.internal.ui.PDEPlugin; 21 import org.eclipse.pde.internal.ui.editor.text.HTMLPrinter; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.SWTError; 24 import org.eclipse.swt.browser.Browser; 25 import org.eclipse.swt.browser.LocationAdapter; 26 import org.eclipse.swt.browser.LocationEvent; 27 import org.eclipse.swt.custom.StyleRange; 28 import org.eclipse.swt.events.DisposeEvent; 29 import org.eclipse.swt.events.DisposeListener; 30 import org.eclipse.swt.events.FocusEvent; 31 import org.eclipse.swt.events.FocusListener; 32 import org.eclipse.swt.events.KeyEvent; 33 import org.eclipse.swt.events.KeyListener; 34 import org.eclipse.swt.graphics.Color; 35 import org.eclipse.swt.graphics.Font; 36 import org.eclipse.swt.graphics.FontData; 37 import org.eclipse.swt.graphics.Point; 38 import org.eclipse.swt.graphics.Rectangle; 39 import org.eclipse.swt.graphics.TextLayout; 40 import org.eclipse.swt.graphics.TextStyle; 41 import org.eclipse.swt.layout.GridData; 42 import org.eclipse.swt.layout.GridLayout; 43 import org.eclipse.swt.widgets.Composite; 44 import org.eclipse.swt.widgets.Display; 45 import org.eclipse.swt.widgets.Event; 46 import org.eclipse.swt.widgets.Label; 47 import org.eclipse.swt.widgets.Listener; 48 import org.eclipse.swt.widgets.Menu; 49 import org.eclipse.swt.widgets.Shell; 50 51 import org.eclipse.jface.text.IInformationControl; 52 import org.eclipse.jface.text.IInformationControlExtension; 53 import org.eclipse.jface.text.IInformationControlExtension3; 54 import org.eclipse.jface.text.TextPresentation; 55 56 57 72 public class BrowserInformationControl implements IInformationControl, IInformationControlExtension, IInformationControlExtension3, DisposeListener { 73 74 75 82 public static boolean isAvailable(Composite parent) { 83 if (!fgAvailabilityChecked) { 84 try { 85 if (parent == null) 86 parent= PDEPlugin.getActiveWorkbenchShell(); 87 if (parent == null) 88 return false; 90 Browser browser= new Browser(parent, SWT.NONE); 91 browser.dispose(); 92 fgIsAvailable= true; 93 } catch (SWTError er) { 94 fgIsAvailable= false; 95 } finally { 96 fgAvailabilityChecked= true; 97 } 98 } 99 100 return fgIsAvailable; 101 } 102 103 104 105 private static final int BORDER= 1; 106 107 111 private static final int MIN_WIDTH= 80; 112 private static final int MIN_HEIGHT= 80; 113 114 115 118 private static boolean fgIsAvailable= false; 119 private static boolean fgAvailabilityChecked= false; 120 121 122 private Shell fShell; 123 124 private Browser fBrowser; 125 126 private boolean fBrowserHasContent; 127 128 private int fMaxWidth= -1; 129 130 private int fMaxHeight= -1; 131 private Font fStatusTextFont; 132 private Label fStatusTextField; 133 private String fStatusFieldText; 134 private boolean fHideScrollBars; 135 private Listener fDeactivateListener; 136 private ListenerList fFocusListeners= new ListenerList(); 137 private Label fSeparator; 138 private String fInputText; 139 private TextLayout fTextLayout; 140 141 private TextStyle fBoldStyle; 142 143 152 public BrowserInformationControl(Shell parent, int shellStyle, int style) { 153 this(parent, shellStyle, style, null); 154 } 155 156 167 public BrowserInformationControl(Shell parent, int shellStyle, int style, String statusFieldText) { 168 fStatusFieldText= statusFieldText; 169 170 fShell= new Shell(parent, SWT.NO_FOCUS | SWT.ON_TOP | shellStyle); 171 Display display= fShell.getDisplay(); 172 fShell.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); 173 fTextLayout= new TextLayout(display); 174 175 Composite composite= fShell; 176 GridLayout layout= new GridLayout(1, false); 177 int border= ((shellStyle & SWT.NO_TRIM) == 0) ? 0 : BORDER; 178 layout.marginHeight= border; 179 layout.marginWidth= border; 180 composite.setLayout(layout); 181 182 if (statusFieldText != null) { 183 composite= new Composite(composite, SWT.NONE); 184 layout= new GridLayout(1, false); 185 layout.marginHeight= 0; 186 layout.marginWidth= 0; 187 layout.verticalSpacing= 1; 188 layout.horizontalSpacing= 1; 189 composite.setLayout(layout); 190 191 GridData gd= new GridData(GridData.FILL_BOTH); 192 composite.setLayoutData(gd); 193 194 composite.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND)); 195 composite.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); 196 } 197 198 fBrowser= new Browser(composite, SWT.NONE); 200 fHideScrollBars= (style & SWT.V_SCROLL) == 0 && (style & SWT.H_SCROLL) == 0; 201 202 GridData gd= new GridData(GridData.BEGINNING | GridData.FILL_BOTH); 203 fBrowser.setLayoutData(gd); 204 205 fBrowser.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND)); 206 fBrowser.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); 207 fBrowser.addKeyListener(new KeyListener() { 208 209 public void keyPressed(KeyEvent e) { 210 if (e.character == 0x1B) fShell.dispose(); 212 } 213 214 public void keyReleased(KeyEvent e) {} 215 }); 216 222 fBrowser.addLocationListener(new LocationAdapter() { 223 226 public void changing(LocationEvent event) { 227 String location= event.location; 228 234 if (!"about:blank".equals(location) && !("carbon".equals(SWT.getPlatform()) && location.startsWith("applewebdata:"))) event.doit= false; 236 } 237 }); 238 239 fBrowser.setMenu(new Menu(fShell, SWT.NONE)); 241 242 if (statusFieldText != null) { 244 245 fSeparator= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.LINE_DOT); 246 fSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 247 248 fStatusTextField= new Label(composite, SWT.RIGHT); 250 fStatusTextField.setText(statusFieldText); 251 Font font= fStatusTextField.getFont(); 252 FontData[] fontDatas= font.getFontData(); 253 for (int i= 0; i < fontDatas.length; i++) 254 fontDatas[i].setHeight(fontDatas[i].getHeight() * 9 / 10); 255 fStatusTextFont= new Font(fStatusTextField.getDisplay(), fontDatas); 256 fStatusTextField.setFont(fStatusTextFont); 257 gd= new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING); 258 fStatusTextField.setLayoutData(gd); 259 260 fStatusTextField.setForeground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW)); 261 262 fStatusTextField.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); 263 } 264 265 addDisposeListener(this); 266 createTextLayout(); 267 } 268 269 277 public BrowserInformationControl(Shell parent,int style) { 278 this(parent, SWT.TOOL | SWT.NO_TRIM, style); 279 } 280 281 288 public BrowserInformationControl(Shell parent) { 289 this(parent, SWT.NONE); 290 } 291 292 293 296 public void setInformation(String content) { 297 fBrowserHasContent= content != null && content.length() > 0; 298 299 if (!fBrowserHasContent) 300 content= "<html><body ></html>"; 302 fInputText= content; 303 304 int shellStyle= fShell.getStyle(); 305 boolean RTL= (shellStyle & SWT.RIGHT_TO_LEFT) != 0; 306 307 String [] styles= null; 308 if (RTL && !fHideScrollBars) 309 styles= new String [] { "direction:rtl;", "word-wrap:break-word;" }; else if (RTL && fHideScrollBars) 311 styles= new String [] { "direction:rtl;", "overflow:hidden;", "word-wrap:break-word;" }; else if (fHideScrollBars && true) 313 styles= new String [] { "overflow:hidden;", "word-wrap: break-word;" }; 315 if (styles != null) { 316 StringBuffer buffer= new StringBuffer (content); 317 HTMLPrinter.insertStyles(buffer, styles); 318 content= buffer.toString(); 319 } 320 321 fBrowser.setText(content); 322 323 } 324 325 329 public void setStatusText(String statusFieldText) { 330 fStatusFieldText= statusFieldText; 331 } 332 333 336 public void setVisible(boolean visible) { 337 if (fShell.isVisible() == visible) 338 return; 339 340 if (visible) { 341 if (fStatusTextField != null) { 342 boolean state= fStatusFieldText != null; 343 if (state) 344 fStatusTextField.setText(fStatusFieldText); 345 fStatusTextField.setVisible(state); 346 fSeparator.setVisible(state); 347 } 348 } 349 350 fShell.setVisible(visible); 351 if (!visible) 352 setInformation(""); } 354 355 361 private void createTextLayout() { 362 fTextLayout= new TextLayout(fBrowser.getDisplay()); 363 364 Font font= fBrowser.getFont(); 366 fTextLayout.setFont(font); 367 fTextLayout.setWidth(-1); 368 FontData[] fontData= font.getFontData(); 369 for (int i= 0; i < fontData.length; i++) 370 fontData[i].setStyle(SWT.BOLD); 371 font= new Font(fShell.getDisplay(), fontData); 372 fBoldStyle= new TextStyle(font, null, null); 373 374 fTextLayout.setText(" "); int tabWidth = fTextLayout.getBounds().width; 377 fTextLayout.setTabs(new int[] {tabWidth}); 378 379 fTextLayout.setText(""); } 381 382 385 public void dispose() { 386 fTextLayout.dispose(); 387 fTextLayout= null; 388 fBoldStyle.font.dispose(); 389 fBoldStyle= null; 390 if (fShell != null && !fShell.isDisposed()) 391 fShell.dispose(); 392 else 393 widgetDisposed(null); 394 } 395 396 399 public void widgetDisposed(DisposeEvent event) { 400 if (fStatusTextFont != null && !fStatusTextFont.isDisposed()) 401 fStatusTextFont.dispose(); 402 403 fShell= null; 404 fBrowser= null; 405 fStatusTextFont= null; 406 } 407 408 411 public void setSize(int width, int height) { 412 fShell.setSize(Math.min(width, fMaxWidth), Math.min(height, fMaxHeight)); 413 } 414 415 418 public void setLocation(Point location) { 419 fShell.setLocation(location); 420 } 421 422 425 public void setSizeConstraints(int maxWidth, int maxHeight) { 426 fMaxWidth= maxWidth; 427 fMaxHeight= maxHeight; 428 } 429 430 433 public Point computeSizeHint() { 434 TextPresentation presentation= new TextPresentation(); 435 HTML2TextReader reader= new HTML2TextReader(new StringReader (fInputText), presentation); 436 String text; 437 try { 438 text= reader.getString(); 439 } catch (IOException e) { 440 text= ""; } 442 443 fTextLayout.setText(text); 444 Iterator iter= presentation.getAllStyleRangeIterator(); 445 while (iter.hasNext()) { 446 StyleRange sr= (StyleRange)iter.next(); 447 if (sr.fontStyle == SWT.BOLD) 448 fTextLayout.setStyle(fBoldStyle, sr.start, sr.start + sr.length - 1); 449 } 450 Rectangle bounds= fTextLayout.getBounds(); 451 int width= bounds.width; 452 int height= bounds.height; 453 454 width += 15; 455 height += 25; 456 457 if (fStatusFieldText != null && fSeparator != null) { 458 fTextLayout.setText(fStatusFieldText); 459 Rectangle statusBounds= fTextLayout.getBounds(); 460 Rectangle separatorBounds= fSeparator.getBounds(); 461 width= Math.max(width, statusBounds.width); 462 height= height + statusBounds.height + separatorBounds.height; 463 } 464 465 if (fMaxWidth != SWT.DEFAULT) 467 width= Math.min(fMaxWidth, width); 468 if (fMaxHeight != SWT.DEFAULT) 469 height= Math.min(fMaxHeight, height); 470 471 width= Math.max(MIN_WIDTH, width); 473 height= Math.max(MIN_HEIGHT, height); 474 475 return new Point(width, height); 476 } 477 478 481 public Rectangle computeTrim() { 482 return fShell.computeTrim(0, 0, 0, 0); 483 } 484 485 488 public Rectangle getBounds() { 489 return fShell.getBounds(); 490 } 491 492 495 public boolean restoresLocation() { 496 return false; 497 } 498 499 502 public boolean restoresSize() { 503 return false; 504 } 505 506 509 public void addDisposeListener(DisposeListener listener) { 510 fShell.addDisposeListener(listener); 511 } 512 513 516 public void removeDisposeListener(DisposeListener listener) { 517 fShell.removeDisposeListener(listener); 518 } 519 520 523 public void setForegroundColor(Color foreground) { 524 fBrowser.setForeground(foreground); 525 } 526 527 530 public void setBackgroundColor(Color background) { 531 fBrowser.setBackground(background); 532 } 533 534 537 public boolean isFocusControl() { 538 return fBrowser.isFocusControl(); 539 } 540 541 544 public void setFocus() { 545 fShell.forceFocus(); 546 fBrowser.setFocus(); 547 } 548 549 552 public void addFocusListener(final FocusListener listener) { 553 fBrowser.addFocusListener(listener); 554 555 559 if (fFocusListeners.isEmpty()) { 560 fDeactivateListener= new Listener() { 561 public void handleEvent(Event event) { 562 Object [] listeners= fFocusListeners.getListeners(); 563 for (int i = 0; i < listeners.length; i++) 564 ((FocusListener)listeners[i]).focusLost(new FocusEvent(event)); 565 } 566 }; 567 fBrowser.getShell().addListener(SWT.Deactivate, fDeactivateListener); 568 } 569 fFocusListeners.add(listener); 570 } 571 572 575 public void removeFocusListener(FocusListener listener) { 576 fBrowser.removeFocusListener(listener); 577 578 582 fFocusListeners.remove(listener); 583 if (fFocusListeners.isEmpty()) { 584 fBrowser.getShell().removeListener(SWT.Deactivate, fDeactivateListener); 585 fDeactivateListener= null; 586 } 587 } 588 589 592 public boolean hasContents() { 593 return fBrowserHasContent; 594 } 595 } 596 597 | Popular Tags |