1 11 12 package org.eclipse.pde.internal.ui.editor.cheatsheet; 13 14 import org.eclipse.core.filebuffers.IDocumentSetupParticipant; 15 import org.eclipse.jface.text.Document; 16 import org.eclipse.jface.text.IDocument; 17 import org.eclipse.jface.text.ITextOperationTarget; 18 import org.eclipse.jface.text.source.SourceViewer; 19 import org.eclipse.jface.viewers.ISelectionChangedListener; 20 import org.eclipse.jface.viewers.SelectionChangedEvent; 21 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 22 import org.eclipse.pde.internal.ui.editor.context.XMLDocumentSetupParticpant; 23 import org.eclipse.pde.internal.ui.editor.text.ColorManager; 24 import org.eclipse.pde.internal.ui.editor.text.IColorManager; 25 import org.eclipse.pde.internal.ui.editor.text.XMLConfiguration; 26 import org.eclipse.swt.SWT; 27 import org.eclipse.swt.custom.StyledText; 28 import org.eclipse.swt.events.DisposeEvent; 29 import org.eclipse.swt.events.DisposeListener; 30 import org.eclipse.swt.events.FocusAdapter; 31 import org.eclipse.swt.events.FocusEvent; 32 import org.eclipse.swt.layout.GridData; 33 import org.eclipse.swt.widgets.Composite; 34 import org.eclipse.ui.actions.ActionFactory; 35 import org.eclipse.ui.forms.widgets.FormToolkit; 36 37 41 public class CSSourceViewer { 42 43 private static XMLConfiguration fSourceConfiguration = null; 44 45 private static IColorManager fColorManager = null; 46 47 private static int fSourceViewerCount = 0; 48 49 private SourceViewer fViewer; 50 51 private PDEFormPage fPage; 52 53 private IDocument fDocument; 54 55 58 public CSSourceViewer(PDEFormPage page) { 59 fDocument = new Document(); 61 fPage = page; 62 } 63 64 67 public IDocument getDocument() { 68 return fDocument; 69 } 70 71 74 public SourceViewer getViewer() { 75 return fViewer; 76 } 77 78 81 private XMLConfiguration getConfiguration() { 82 if (fSourceConfiguration == null) { 83 fColorManager = ColorManager.getDefault(); 85 fSourceConfiguration = new XMLConfiguration(fColorManager); 87 } 88 return fSourceConfiguration; 89 } 90 91 97 public void createUI(Composite parent, int heightHint, int widthHint) { 98 int style = SWT.MULTI | SWT.WRAP | SWT.V_SCROLL; 100 fViewer = new SourceViewer(parent, null, style); 101 fViewer.configure(getConfiguration()); 103 IDocumentSetupParticipant participant = new XMLDocumentSetupParticpant(); 105 participant.setup(fDocument); 106 fViewer.setDocument(fDocument); 108 configureUIStyledText(heightHint, widthHint, fViewer.getTextWidget()); 110 createUIListenersStyledText(fViewer.getTextWidget()); 112 } 113 114 117 public void createUIListeners() { 118 if (fViewer == null) { 120 return; 121 } 122 fViewer.addSelectionChangedListener(new ISelectionChangedListener() { 125 public void selectionChanged(SelectionChangedEvent event) { 126 fPage.getPDEEditor().setSelection(event.getSelection()); 127 } 128 }); 129 fViewer.getTextWidget().addFocusListener(new FocusAdapter() { 131 public void focusGained(FocusEvent e) { 132 fPage.getPDEEditor().getContributor().updateSelectableActions(null); 133 } 134 }); 135 } 136 137 140 private void createUIListenersStyledText(StyledText textWidget) { 141 fSourceViewerCount++; 143 textWidget.addDisposeListener(new DisposeListener() { 147 public void widgetDisposed(DisposeEvent e) { 148 fSourceViewerCount--; 149 if (fSourceViewerCount == 0) { 150 dispose(); 151 } 152 } 153 }); 154 } 155 156 159 private void dispose() { 160 if (fColorManager != null) { 163 fColorManager.dispose(); 164 fColorManager = null; 165 } 166 if (fSourceConfiguration != null) { 168 fSourceConfiguration.dispose(); 169 fSourceConfiguration = null; 170 } 171 } 172 173 178 private void configureUIStyledText(int heightHint, 179 int widthHint, StyledText styledText) { 180 styledText.setMenu(fPage.getPDEEditor().getContextMenu()); 182 styledText.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER); 184 GridData data = new GridData(GridData.FILL_HORIZONTAL); 185 data.heightHint = heightHint; 186 data.widthHint = widthHint; 187 styledText.setLayoutData(data); 188 } 189 190 196 public void unsetMenu() { 197 if (fViewer == null) { 198 return; 199 } 200 StyledText styledText = fViewer.getTextWidget(); 201 if (styledText == null) { 202 return; 203 } else if (styledText.isDisposed()) { 204 return; 205 } 206 styledText.setMenu(null); 207 } 208 209 216 public boolean doGlobalAction(String actionId) { 217 if (fViewer == null) { 219 return false; 220 } else if (actionId.equals(ActionFactory.CUT.getId())) { 221 fViewer.doOperation(ITextOperationTarget.CUT); 222 return true; 223 } else if ( 224 actionId.equals(ActionFactory.COPY.getId())) { 225 fViewer.doOperation(ITextOperationTarget.COPY); 226 return true; 227 } else if ( 228 actionId.equals(ActionFactory.PASTE.getId())) { 229 fViewer.doOperation(ITextOperationTarget.PASTE); 230 return true; 231 } else if ( 232 actionId.equals(ActionFactory.SELECT_ALL.getId())) { 233 fViewer.doOperation(ITextOperationTarget.SELECT_ALL); 234 return true; 235 } else if ( 236 actionId.equals(ActionFactory.DELETE.getId())) { 237 fViewer.doOperation(ITextOperationTarget.DELETE); 238 return true; 239 } else if ( 240 actionId.equals(ActionFactory.UNDO.getId())) { 241 fViewer.doOperation(ITextOperationTarget.UNDO); 242 return true; 243 } else if ( 244 actionId.equals(ActionFactory.REDO.getId())) { 245 fViewer.doOperation(ITextOperationTarget.REDO); 246 return true; 247 } 248 return false; 249 } 250 251 255 public boolean canPaste() { 256 if (fViewer == null) { 258 return false; 259 } 260 return fViewer.canDoOperation(ITextOperationTarget.PASTE); 261 } 262 } 263 | Popular Tags |