1 12 package org.eclipse.team.internal.ccvs.ui.operations; 13 14 import java.util.Collections ; 15 import java.util.Map ; 16 17 import org.eclipse.jface.resource.JFaceResources; 18 import org.eclipse.jface.text.*; 19 import org.eclipse.jface.text.source.ISourceViewer; 20 import org.eclipse.jface.text.source.SourceViewer; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.custom.StyleRange; 23 import org.eclipse.swt.custom.StyledText; 24 import org.eclipse.swt.events.*; 25 import org.eclipse.swt.graphics.*; 26 import org.eclipse.swt.layout.GridData; 27 import org.eclipse.swt.layout.GridLayout; 28 import org.eclipse.swt.widgets.*; 29 import org.eclipse.ui.editors.text.EditorsUI; 30 import org.eclipse.ui.editors.text.TextSourceViewerConfiguration; 31 32 41 class SourceViewerInformationControl implements IInformationControl, IInformationControlExtension, DisposeListener { 42 43 44 private static final int BORDER= 1; 45 46 47 48 private Shell fShell; 49 50 private StyledText fText; 51 52 private SourceViewer fViewer; 53 54 private Label fStatusField; 55 56 private Label fSeparator; 57 58 private Font fStatusTextFont; 59 60 private int fMaxWidth; 61 62 private int fMaxHeight; 63 64 65 72 public SourceViewerInformationControl(Shell parent, String symbolicFontName) { 73 this(parent, SWT.NO_TRIM | SWT.TOOL, SWT.NONE, symbolicFontName, null); 74 } 75 76 90 public SourceViewerInformationControl(Shell parent, int shellStyle, int style, String symbolicFontName, String statusFieldText) { 91 GridLayout layout; 92 GridData gd; 93 94 fShell= new Shell(parent, SWT.NO_FOCUS | SWT.ON_TOP | shellStyle); 95 Display display= fShell.getDisplay(); 96 fShell.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); 97 98 Composite composite= fShell; 99 layout= new GridLayout(1, false); 100 int border= ((shellStyle & SWT.NO_TRIM) == 0) ? 0 : BORDER; 101 layout.marginHeight= border; 102 layout.marginWidth= border; 103 composite.setLayout(layout); 104 gd= new GridData(GridData.FILL_HORIZONTAL); 105 composite.setLayoutData(gd); 106 107 if (statusFieldText != null) { 108 composite= new Composite(composite, SWT.NONE); 109 layout= new GridLayout(1, false); 110 layout.marginHeight= 0; 111 layout.marginWidth= 0; 112 composite.setLayout(layout); 113 gd= new GridData(GridData.FILL_BOTH); 114 composite.setLayoutData(gd); 115 composite.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND)); 116 composite.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); 117 } 118 119 fViewer= new SourceViewer(composite, null, style); 121 fViewer.setEditable(false); 122 123 fViewer.configure(new TextSourceViewerConfiguration(EditorsUI.getPreferenceStore()) { 126 protected Map getHyperlinkDetectorTargets(ISourceViewer sourceViewer) { 127 return Collections.singletonMap("org.eclipse.ui.DefaultTextEditor", null); 129 } 136 }); 137 138 fText= fViewer.getTextWidget(); 139 gd= new GridData(GridData.BEGINNING | GridData.FILL_BOTH); 140 fText.setLayoutData(gd); 141 fText.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND)); 142 fText.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND)); 143 fText.setFont(JFaceResources.getFont(symbolicFontName)); 144 145 fText.addKeyListener(new KeyListener() { 146 147 public void keyPressed(KeyEvent e) { 148 if (e.character == 0x1B) fShell.dispose(); 150 } 151 152 public void keyReleased(KeyEvent e) {} 153 }); 154 155 if (statusFieldText != null) { 157 158 fSeparator= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.LINE_DOT); 160 fSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 161 162 fStatusField= new Label(composite, SWT.RIGHT); 164 fStatusField.setText(statusFieldText); 165 Font font= fStatusField.getFont(); 166 FontData[] fontDatas= font.getFontData(); 167 for (int i= 0; i < fontDatas.length; i++) 168 fontDatas[i].setHeight(fontDatas[i].getHeight() * 9 / 10); 169 fStatusTextFont= new Font(fStatusField.getDisplay(), fontDatas); 170 fStatusField.setFont(fStatusTextFont); 171 GridData gd2= new GridData(GridData.FILL_VERTICAL | GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING); 172 fStatusField.setLayoutData(gd2); 173 174 fStatusField.setForeground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW)); 176 177 fStatusField.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); 178 } 179 180 addDisposeListener(this); 181 } 182 183 187 public void setInput(Object input) { 188 if (input instanceof String ) 189 setInformation((String )input); 190 else 191 setInformation(null); 192 } 193 194 197 public void setInformation(String content) { 198 if (content == null) { 199 fViewer.setInput(null); 200 return; 201 } 202 203 IDocument doc= new Document(content); 204 fViewer.setInput(doc); 205 206 StyleRange styleRange = new StyleRange(); 208 styleRange.start = 0; 209 styleRange.length = content.indexOf('\n'); 210 styleRange.fontStyle = SWT.BOLD; 211 fViewer.getTextWidget().setStyleRange(styleRange); 212 } 213 214 217 public void setVisible(boolean visible) { 218 fShell.setVisible(visible); 219 } 220 221 224 public void widgetDisposed(DisposeEvent event) { 225 if (fStatusTextFont != null && !fStatusTextFont.isDisposed()) 226 fStatusTextFont.dispose(); 227 228 fStatusTextFont= null; 229 fShell= null; 230 fText= null; 231 } 232 233 236 public final void dispose() { 237 if (fShell != null && !fShell.isDisposed()) 238 fShell.dispose(); 239 else 240 widgetDisposed(null); 241 } 242 243 246 public void setSize(int width, int height) { 247 248 if (fStatusField != null) { 249 GridData gd= (GridData)fViewer.getTextWidget().getLayoutData(); 250 Point statusSize= fStatusField.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 251 Point separatorSize= fSeparator.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 252 gd.heightHint= height - statusSize.y - separatorSize.y; 253 } 254 fShell.setSize(width, height); 255 256 if (fStatusField != null) 257 fShell.pack(true); 258 } 259 260 263 public void setLocation(Point location) { 264 fShell.setLocation(location); 265 } 266 267 270 public void setSizeConstraints(int maxWidth, int maxHeight) { 271 fMaxWidth= maxWidth; 272 fMaxHeight= maxHeight; 273 } 274 275 278 public Point computeSizeHint() { 279 int x= SWT.DEFAULT; 281 int y= SWT.DEFAULT; 282 Point size= fShell.computeSize(x, y); 283 if (size.x > fMaxWidth) 284 x= fMaxWidth; 285 if (size.y > fMaxHeight) 286 y= fMaxHeight; 287 288 if (x != SWT.DEFAULT || y != SWT.DEFAULT) 290 size= fShell.computeSize(x, y, false); 291 292 return size; 293 } 294 295 298 public void addDisposeListener(DisposeListener listener) { 299 fShell.addDisposeListener(listener); 300 } 301 302 305 public void removeDisposeListener(DisposeListener listener) { 306 fShell.removeDisposeListener(listener); 307 } 308 309 312 public void setForegroundColor(Color foreground) { 313 fText.setForeground(foreground); 314 } 315 316 319 public void setBackgroundColor(Color background) { 320 fText.setBackground(background); 321 } 322 323 326 public boolean isFocusControl() { 327 return fText.isFocusControl(); 328 } 329 330 333 public void setFocus() { 334 fShell.forceFocus(); 335 fText.setFocus(); 336 } 337 338 341 public void addFocusListener(FocusListener listener) { 342 fText.addFocusListener(listener); 343 } 344 345 348 public void removeFocusListener(FocusListener listener) { 349 fText.removeFocusListener(listener); 350 } 351 352 355 public boolean hasContents() { 356 return fText.getCharCount() > 0; 357 } 358 } | Popular Tags |