KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > cheatsheet > CSSourceViewer


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
38  * CSSourceViewerFactory
39  *
40  */

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     /**
56      * @param page
57      */

58     public CSSourceViewer(PDEFormPage page) {
59         // Create the underlying document
60
fDocument = new Document();
61         fPage = page;
62     }
63     
64     /**
65      * @return
66      */

67     public IDocument getDocument() {
68         return fDocument;
69     }
70     
71     /**
72      * @return
73      */

74     public SourceViewer getViewer() {
75         return fViewer;
76     }
77     
78     /**
79      * @return
80      */

81     private XMLConfiguration getConfiguration() {
82         if (fSourceConfiguration == null) {
83             // Get the color manager
84
fColorManager = ColorManager.getDefault();
85             // Create the source configuration
86
fSourceConfiguration = new XMLConfiguration(fColorManager);
87         }
88         return fSourceConfiguration;
89     }
90
91     /**
92      * Utility method for creating a field for syntax highlighting
93      * @param parent
94      * @param heightHint
95      * @param widthHint
96      */

97     public void createUI(Composite parent, int heightHint, int widthHint) {
98         // Create the source viewer
99
int style = SWT.MULTI | SWT.WRAP | SWT.V_SCROLL;
100         fViewer = new SourceViewer(parent, null, style);
101         // Configure the source viewer
102
fViewer.configure(getConfiguration());
103         // Setup the underlying document
104
IDocumentSetupParticipant participant = new XMLDocumentSetupParticpant();
105         participant.setup(fDocument);
106         // Set the document on the source viewer
107
fViewer.setDocument(fDocument);
108         // Configure the underlying styled text widget
109
configureUIStyledText(heightHint, widthHint, fViewer.getTextWidget());
110         // Create style text listeners
111
createUIListenersStyledText(fViewer.getTextWidget());
112     }
113
114     /**
115      *
116      */

117     public void createUIListeners() {
118         // Ensure the viewer was created
119
if (fViewer == null) {
120             return;
121         }
122         // Create source viewer listeners
123
// Create selection listener
124
fViewer.addSelectionChangedListener(new ISelectionChangedListener() {
125             public void selectionChanged(SelectionChangedEvent event) {
126                 fPage.getPDEEditor().setSelection(event.getSelection());
127             }
128         });
129         // Create focus listener
130
fViewer.getTextWidget().addFocusListener(new FocusAdapter() {
131             public void focusGained(FocusEvent e) {
132                 fPage.getPDEEditor().getContributor().updateSelectableActions(null);
133             }
134         });
135     }
136     
137     /**
138      * @param textWidget
139      */

140     private void createUIListenersStyledText(StyledText textWidget) {
141         // Track the number of source viewers created
142
fSourceViewerCount++;
143         // The color manager and source viewer configuration should be disposed
144
// When the last source viewer is diposed, dispose of the color manager
145
// and source viewer configuration
146
textWidget.addDisposeListener(new DisposeListener() {
147             public void widgetDisposed(DisposeEvent e) {
148                 fSourceViewerCount--;
149                 if (fSourceViewerCount == 0) {
150                     dispose();
151                 }
152             }
153         });
154     }
155
156     /**
157      *
158      */

159     private void dispose() {
160         // TODO: MP: CompCS: Profile Sleek when making static to ensure no leaks
161
// Dispose of the color manager
162
if (fColorManager != null) {
163             fColorManager.dispose();
164             fColorManager = null;
165         }
166         // Dispose of the source configuration
167
if (fSourceConfiguration != null) {
168             fSourceConfiguration.dispose();
169             fSourceConfiguration = null;
170         }
171     }
172     
173     /**
174      * @param heightHint
175      * @param widthHint
176      * @param styledText
177      */

178     private void configureUIStyledText(int heightHint,
179             int widthHint, StyledText styledText) {
180         // Configure the underlying styled text widget
181
styledText.setMenu(fPage.getPDEEditor().getContextMenu());
182         // Force borders
183
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     /**
191      * The menu set on the underlying styled text widget of the source viewer
192      * needs to be set to null before being diposed; otherwise, the menu will
193      * be disposed along with the widget.
194      * @param viewer
195      */

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     /**
210      * Utility method used to tie global actions into source viewers.
211      *
212      * @param actionId
213      * @param viewer
214      * @return
215      */

216     public boolean doGlobalAction(String JavaDoc actionId) {
217         // Ensure the viewer was created
218
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     /**
252      * @param viewer
253      * @return
254      */

255     public boolean canPaste() {
256         // Ensure the viewer was created
257
if (fViewer == null) {
258             return false;
259         }
260         return fViewer.canDoOperation(ITextOperationTarget.PASTE);
261     }
262 }
263
Popular Tags