KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > schema > DescriptionSection


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.pde.internal.ui.editor.schema;
12
13 import org.eclipse.jface.action.IMenuManager;
14 import org.eclipse.jface.resource.JFaceResources;
15 import org.eclipse.jface.text.Document;
16 import org.eclipse.jface.text.DocumentEvent;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IDocumentListener;
19 import org.eclipse.jface.text.ITextOperationTarget;
20 import org.eclipse.jface.text.source.SourceViewer;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.ISelectionChangedListener;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.viewers.SelectionChangedEvent;
25 import org.eclipse.pde.internal.core.ischema.ISchemaObject;
26 import org.eclipse.pde.internal.core.ischema.ISchemaObjectReference;
27 import org.eclipse.pde.internal.core.schema.SchemaObject;
28 import org.eclipse.pde.internal.ui.PDEUIMessages;
29 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
30 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
31 import org.eclipse.pde.internal.ui.editor.PDESection;
32 import org.eclipse.pde.internal.ui.editor.context.XMLDocumentSetupParticpant;
33 import org.eclipse.pde.internal.ui.editor.text.IColorManager;
34 import org.eclipse.pde.internal.ui.editor.text.XMLConfiguration;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.dnd.Clipboard;
37 import org.eclipse.swt.events.FocusAdapter;
38 import org.eclipse.swt.events.FocusEvent;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.ui.actions.ActionFactory;
43 import org.eclipse.ui.forms.IFormPart;
44 import org.eclipse.ui.forms.IPartSelectionListener;
45 import org.eclipse.ui.forms.widgets.FormToolkit;
46 import org.eclipse.ui.forms.widgets.Section;
47
48 public class DescriptionSection extends PDESection implements IPartSelectionListener {
49     private SourceViewer fSourceViewer;
50     private IDocument fDocument;
51     private ISchemaObject fElement;
52     private boolean fIgnoreChange;
53     private XMLConfiguration fSourceConfiguration;
54
55     public DescriptionSection(PDEFormPage page, Composite parent, IColorManager colorManager) {
56         super(page, parent, Section.DESCRIPTION);
57         fSourceConfiguration = new XMLConfiguration(colorManager);
58         getSection().setText(PDEUIMessages.SchemaEditor_DescriptionSection_title);
59         getSection().setDescription(PDEUIMessages.SchemaEditor_DescriptionSection_desc);
60         createClient(getSection(), page.getManagedForm().getToolkit());
61     }
62     
63     public void commit(boolean onSave) {
64         updateDescription();
65         super.commit(onSave);
66     }
67     
68     public void createClient(
69         Section section,
70         FormToolkit toolkit) {
71         
72         Composite container = toolkit.createComposite(section);
73         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
74         GridData data = new GridData(GridData.FILL_HORIZONTAL);
75         section.setLayoutData(data);
76         
77         GridData gd;
78         fSourceViewer = new SourceViewer(container, null, SWT.MULTI|SWT.WRAP|SWT.V_SCROLL| SWT.H_SCROLL);
79         fSourceViewer.configure(fSourceConfiguration);
80         fDocument = new Document();
81         new XMLDocumentSetupParticpant().setup(fDocument);
82         fSourceViewer.setDocument(fDocument);
83         fSourceViewer.setEditable(isEditable());
84         fSourceViewer.addSelectionChangedListener(new ISelectionChangedListener() {
85             public void selectionChanged(SelectionChangedEvent event) {
86                 updateSelection(event.getSelection());
87             }
88         });
89         
90         Control styledText = fSourceViewer.getTextWidget();
91         styledText.setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
92         if (SWT.getPlatform().equals("motif") == false) //$NON-NLS-1$
93
toolkit.paintBordersFor(container);
94         styledText.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
95         Control[] children = container.getChildren();
96         Control control = children[children.length - 1];
97         gd = new GridData(GridData.FILL_BOTH);
98         gd.widthHint = 200;
99         gd.heightHint = 120;
100         control.setLayoutData(gd);
101         styledText.setMenu(getPage().getPDEEditor().getContextMenu());
102         styledText.addFocusListener(new FocusAdapter() {
103             public void focusGained(FocusEvent e) {
104                 updateSelection(fSourceViewer.getSelection());
105             }
106         });
107         section.setClient(container);
108         initialize();
109     }
110
111     private void updateSelection(ISelection selection) {
112         getPage().getPDEEditor().setSelection(selection);
113     }
114
115     public boolean doGlobalAction(String JavaDoc actionId) {
116         if (actionId.equals(ActionFactory.CUT.getId())) {
117             fSourceViewer.doOperation(ITextOperationTarget.CUT);
118             return true;
119         } else if (
120             actionId.equals(ActionFactory.COPY.getId())) {
121             fSourceViewer.doOperation(ITextOperationTarget.COPY);
122             return true;
123         } else if (
124             actionId.equals(ActionFactory.PASTE.getId())) {
125             fSourceViewer.doOperation(ITextOperationTarget.PASTE);
126             return true;
127         } else if (
128             actionId.equals(ActionFactory.SELECT_ALL.getId())) {
129             fSourceViewer.doOperation(ITextOperationTarget.SELECT_ALL);
130             return true;
131         } else if (
132             actionId.equals(ActionFactory.DELETE.getId())) {
133             fSourceViewer.doOperation(ITextOperationTarget.DELETE);
134             return true;
135         } else if (
136             actionId.equals(ActionFactory.UNDO.getId())) {
137             fSourceViewer.doOperation(ITextOperationTarget.UNDO);
138             return true;
139         } else if (
140             actionId.equals(ActionFactory.REDO.getId())) {
141             fSourceViewer.doOperation(ITextOperationTarget.REDO);
142             return true;
143         }
144         return false;
145     }
146     
147     protected void fillContextMenu(IMenuManager manager) {
148         getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager);
149     }
150     
151     private void updateDescription() {
152         if (fElement instanceof SchemaObject) {
153             ((SchemaObject)fElement).setDescription(fDocument.get());
154         }
155     }
156     
157     public void initialize() {
158         updateDocument();
159         fDocument.addDocumentListener(new IDocumentListener() {
160             public void documentChanged(DocumentEvent e) {
161                 if (!fIgnoreChange && getPage().getPDEEditor().getAggregateModel().isEditable()) {
162                     markDirty();
163                 }
164             }
165             public void documentAboutToBeChanged(DocumentEvent e) {
166             }
167         });
168     }
169
170     public void selectionChanged(IFormPart part, ISelection selection) {
171         if (!(part instanceof ElementSection))
172             return;
173         Object JavaDoc changeObject = ((IStructuredSelection)selection).getFirstElement();
174         if (changeObject != fElement && isDirty())
175             updateDescription();
176         fElement = (ISchemaObject) changeObject;
177         if (fElement instanceof ISchemaObjectReference)
178             fElement = ((ISchemaObjectReference)fElement).getReferencedObject();
179         updateDocument();
180     }
181     
182     public void setFocus() {
183         fSourceViewer.getTextWidget().setFocus();
184     }
185
186     public void updateDocument() {
187         if (fElement != null) {
188             fIgnoreChange = true;
189             String JavaDoc text = fElement.getDescription();
190             fDocument.set(text == null ? "" : text); //$NON-NLS-1$
191
fIgnoreChange = false;
192         }
193     }
194
195     public boolean canPaste(Clipboard clipboard) {
196         return fSourceViewer.canDoOperation(ITextOperationTarget.PASTE);
197     }
198     
199     /* (non-Javadoc)
200      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
201      */

202     public void dispose() {
203         // Dispose of the source configuration
204
if (fSourceConfiguration != null) {
205             fSourceConfiguration.dispose();
206         }
207         super.dispose();
208     }
209     
210 }
211
Popular Tags