1 11 package org.eclipse.jface.text.source.projection; 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.layout.GridData; 25 import org.eclipse.swt.layout.GridLayout; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.Display; 28 import org.eclipse.swt.widgets.Label; 29 import org.eclipse.swt.widgets.Shell; 30 31 import org.eclipse.jface.text.Document; 32 import org.eclipse.jface.text.IDocument; 33 import org.eclipse.jface.text.IInformationControl; 34 import org.eclipse.jface.text.IInformationControlExtension; 35 import org.eclipse.jface.text.source.SourceViewer; 36 import org.eclipse.jface.text.source.SourceViewerConfiguration; 37 38 import org.eclipse.jface.resource.JFaceResources; 39 40 46 class SourceViewerInformationControl implements IInformationControl, IInformationControlExtension, DisposeListener { 47 48 49 private static final int BORDER= 1; 50 51 52 53 private Shell fShell; 54 55 private StyledText fText; 56 57 private SourceViewer fViewer; 58 59 private Label fStatusField; 60 61 private Label fSeparator; 62 63 private Font fStatusTextFont; 64 65 private int fMaxWidth; 66 67 private int fMaxHeight; 68 69 70 77 public SourceViewerInformationControl(Shell parent, String symbolicFontName) { 78 this(parent, SWT.NO_TRIM | SWT.TOOL, SWT.NONE, symbolicFontName, null); 79 } 80 81 95 public SourceViewerInformationControl(Shell parent, int shellStyle, int style, String symbolicFontName, String statusFieldText) { 96 GridLayout layout; 97 GridData gd; 98 99 fShell= new Shell(parent, SWT.NO_FOCUS | SWT.ON_TOP | shellStyle); 100 Display display= fShell.getDisplay(); 101 fShell.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); 102 103 Composite composite= fShell; 104 layout= new GridLayout(1, false); 105 int border= ((shellStyle & SWT.NO_TRIM) == 0) ? 0 : BORDER; 106 layout.marginHeight= border; 107 layout.marginWidth= border; 108 composite.setLayout(layout); 109 gd= new GridData(GridData.FILL_HORIZONTAL); 110 composite.setLayoutData(gd); 111 112 if (statusFieldText != null) { 113 composite= new Composite(composite, SWT.NONE); 114 layout= new GridLayout(1, false); 115 layout.marginHeight= 0; 116 layout.marginWidth= 0; 117 composite.setLayout(layout); 118 gd= new GridData(GridData.FILL_BOTH); 119 composite.setLayoutData(gd); 120 composite.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND)); 121 composite.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); 122 } 123 124 fViewer= new SourceViewer(composite, null, style); 126 fViewer.configure(new SourceViewerConfiguration()); 127 fViewer.setEditable(false); 128 129 fText= fViewer.getTextWidget(); 130 gd= new GridData(GridData.BEGINNING | GridData.FILL_BOTH); 131 fText.setLayoutData(gd); 132 fText.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND)); 133 fText.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND)); 134 fText.setFont(JFaceResources.getFont(symbolicFontName)); 135 136 fText.addKeyListener(new KeyListener() { 137 138 public void keyPressed(KeyEvent e) { 139 if (e.character == 0x1B) fShell.dispose(); 141 } 142 143 public void keyReleased(KeyEvent e) {} 144 }); 145 146 if (statusFieldText != null) { 148 149 fSeparator= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.LINE_DOT); 151 fSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 152 153 fStatusField= new Label(composite, SWT.RIGHT); 155 fStatusField.setText(statusFieldText); 156 Font font= fStatusField.getFont(); 157 FontData[] fontDatas= font.getFontData(); 158 for (int i= 0; i < fontDatas.length; i++) 159 fontDatas[i].setHeight(fontDatas[i].getHeight() * 9 / 10); 160 fStatusTextFont= new Font(fStatusField.getDisplay(), fontDatas); 161 fStatusField.setFont(fStatusTextFont); 162 GridData gd2= new GridData(GridData.FILL_VERTICAL | GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING); 163 fStatusField.setLayoutData(gd2); 164 165 fStatusField.setForeground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW)); 167 168 fStatusField.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); 169 } 170 171 addDisposeListener(this); 172 } 173 174 178 public void setInput(Object input) { 179 if (input instanceof String ) 180 setInformation((String )input); 181 else 182 setInformation(null); 183 } 184 185 188 public void setInformation(String content) { 189 if (content == null) { 190 fViewer.setInput(null); 191 return; 192 } 193 194 IDocument doc= new Document(content); 195 fViewer.setInput(doc); 196 } 197 198 201 public void setVisible(boolean visible) { 202 fShell.setVisible(visible); 203 } 204 205 208 public void widgetDisposed(DisposeEvent event) { 209 if (fStatusTextFont != null && !fStatusTextFont.isDisposed()) 210 fStatusTextFont.dispose(); 211 212 fStatusTextFont= null; 213 fShell= null; 214 fText= null; 215 } 216 217 220 public final void dispose() { 221 if (fShell != null && !fShell.isDisposed()) 222 fShell.dispose(); 223 else 224 widgetDisposed(null); 225 } 226 227 230 public void setSize(int width, int height) { 231 232 if (fStatusField != null) { 233 GridData gd= (GridData)fViewer.getTextWidget().getLayoutData(); 234 Point statusSize= fStatusField.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 235 Point separatorSize= fSeparator.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 236 gd.heightHint= height - statusSize.y - separatorSize.y; 237 } 238 fShell.setSize(width, height); 239 240 if (fStatusField != null) 241 fShell.pack(true); 242 } 243 244 247 public void setLocation(Point location) { 248 fShell.setLocation(location); 249 } 250 251 254 public void setSizeConstraints(int maxWidth, int maxHeight) { 255 fMaxWidth= maxWidth; 256 fMaxHeight= maxHeight; 257 } 258 259 262 public Point computeSizeHint() { 263 int x= SWT.DEFAULT; 265 int y= SWT.DEFAULT; 266 Point size= fShell.computeSize(x, y); 267 if (size.x > fMaxWidth) 268 x= fMaxWidth; 269 if (size.y > fMaxHeight) 270 y= fMaxHeight; 271 272 if (x != SWT.DEFAULT || y != SWT.DEFAULT) 274 size= fShell.computeSize(x, y, false); 275 276 return size; 277 } 278 279 282 public void addDisposeListener(DisposeListener listener) { 283 fShell.addDisposeListener(listener); 284 } 285 286 289 public void removeDisposeListener(DisposeListener listener) { 290 fShell.removeDisposeListener(listener); 291 } 292 293 296 public void setForegroundColor(Color foreground) { 297 fText.setForeground(foreground); 298 } 299 300 303 public void setBackgroundColor(Color background) { 304 fText.setBackground(background); 305 } 306 307 310 public boolean isFocusControl() { 311 return fText.isFocusControl(); 312 } 313 314 317 public void setFocus() { 318 fShell.forceFocus(); 319 fText.setFocus(); 320 } 321 322 325 public void addFocusListener(FocusListener listener) { 326 fText.addFocusListener(listener); 327 } 328 329 332 public void removeFocusListener(FocusListener listener) { 333 fText.removeFocusListener(listener); 334 } 335 336 339 public boolean hasContents() { 340 return fText.getCharCount() > 0; 341 } 342 } 343 | Popular Tags |