1 11 12 package org.eclipse.ant.internal.ui.editor; 13 14 import org.eclipse.ant.internal.ui.AntSourceViewerConfiguration; 15 import org.eclipse.ant.internal.ui.editor.text.AntDocumentSetupParticipant; 16 import org.eclipse.jface.resource.JFaceResources; 17 import org.eclipse.jface.text.Document; 18 import org.eclipse.jface.text.IDocument; 19 import org.eclipse.jface.text.IInformationControl; 20 import org.eclipse.jface.text.IInformationControlExtension; 21 import org.eclipse.jface.text.source.SourceViewer; 22 import org.eclipse.jface.text.source.SourceViewerConfiguration; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.custom.StyledText; 25 import org.eclipse.swt.events.DisposeEvent; 26 import org.eclipse.swt.events.DisposeListener; 27 import org.eclipse.swt.events.FocusListener; 28 import org.eclipse.swt.events.KeyEvent; 29 import org.eclipse.swt.events.KeyListener; 30 import org.eclipse.swt.graphics.Color; 31 import org.eclipse.swt.graphics.Font; 32 import org.eclipse.swt.graphics.Point; 33 import org.eclipse.swt.graphics.Rectangle; 34 import org.eclipse.swt.layout.GridData; 35 import org.eclipse.swt.layout.GridLayout; 36 import org.eclipse.swt.widgets.Composite; 37 import org.eclipse.swt.widgets.Display; 38 import org.eclipse.swt.widgets.Shell; 39 40 public class AntSourceViewerInformationControl implements IInformationControl, IInformationControlExtension, DisposeListener { 41 42 private Shell fShell; 43 44 45 private static final int BORDER= 1; 46 47 48 private SourceViewer fViewer; 49 50 51 private StyledText fText; 52 53 public AntSourceViewerInformationControl(Shell parent) { 54 GridLayout layout; 55 GridData gd; 56 57 fShell= new Shell(parent, SWT.NO_FOCUS | SWT.ON_TOP | SWT.NO_TRIM); 58 Display display= fShell.getDisplay(); 59 fShell.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); 60 61 Composite composite= fShell; 62 layout= new GridLayout(1, false); 63 int border= ((SWT.NO_TRIM & SWT.NO_TRIM) == 0) ? 0 : BORDER; 64 layout.marginHeight= border; 65 layout.marginWidth= border; 66 composite.setLayout(layout); 67 gd= new GridData(GridData.FILL_HORIZONTAL); 68 composite.setLayoutData(gd); 69 fViewer= createViewer(composite); 70 71 fText= fViewer.getTextWidget(); 72 gd= new GridData(GridData.BEGINNING | GridData.FILL_BOTH); 73 fText.setLayoutData(gd); 74 fText.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND)); 75 fText.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND)); 76 77 fText.addKeyListener(new KeyListener() { 78 79 public void keyPressed(KeyEvent e) { 80 if (e.character == 0x1B) fShell.dispose(); 82 } 83 84 public void keyReleased(KeyEvent e) {} 85 }); 86 } 87 88 89 private SourceViewer createViewer(Composite parent) { 90 SourceViewer viewer = new SourceViewer(parent, null, SWT.NONE); 91 92 SourceViewerConfiguration configuration = new AntSourceViewerConfiguration(); 93 viewer.configure(configuration); 94 viewer.setEditable(false); 95 Font font= JFaceResources.getFont(JFaceResources.TEXT_FONT); 96 viewer.getTextWidget().setFont(font); 97 98 return viewer; 99 } 100 101 104 public void setInformation(String content) { 105 if (content == null) { 106 fViewer.setInput(null); 107 return; 108 } 109 IDocument document = new Document(content); 110 new AntDocumentSetupParticipant().setup(document); 111 fViewer.setDocument(document); 112 } 113 114 117 public void setSizeConstraints(int maxWidth, int maxHeight) { 118 } 119 120 123 public Point computeSizeHint() { 124 return fShell.computeSize(SWT.DEFAULT, SWT.DEFAULT); 125 } 126 127 130 public void setVisible(boolean visible) { 131 fShell.setVisible(visible); 132 } 133 134 137 public void setSize(int width, int height) { 138 fShell.setSize(width, height); 139 } 140 141 144 public void setLocation(Point location) { 145 Rectangle trim= fShell.computeTrim(0, 0, 0, 0); 146 Point textLocation= fText.getLocation(); 147 location.x += trim.x - textLocation.x; 148 location.y += trim.y - textLocation.y; 149 fShell.setLocation(location); 150 } 151 152 155 public void dispose() { 156 if (fShell != null && !fShell.isDisposed()) { 157 fShell.dispose(); 158 } else { 159 widgetDisposed(null); 160 } 161 162 } 163 164 167 public void addDisposeListener(DisposeListener listener) { 168 fShell.addDisposeListener(listener); 169 170 } 171 172 175 public void removeDisposeListener(DisposeListener listener) { 176 fShell.removeDisposeListener(listener); 177 } 178 179 182 public void setForegroundColor(Color foreground) { 183 fText.setForeground(foreground); 184 } 185 186 189 public void setBackgroundColor(Color background) { 190 fText.setBackground(background); 191 } 192 193 196 public boolean isFocusControl() { 197 return fText.isFocusControl(); 198 } 199 200 203 public void setFocus() { 204 fShell.forceFocus(); 205 fText.setFocus(); 206 207 } 208 209 212 public void addFocusListener(FocusListener listener) { 213 fText.addFocusListener(listener); 214 } 215 216 219 public void removeFocusListener(FocusListener listener) { 220 fText.removeFocusListener(listener); 221 222 } 223 224 227 public boolean hasContents() { 228 return fText.getCharCount() > 0; 229 } 230 231 234 public void widgetDisposed(DisposeEvent e) { 235 fShell= null; 236 fText= null; 237 } 238 } 239 | Popular Tags |