KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > BodyTextSection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.plugin;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.pde.core.IModelChangedEvent;
17 import org.eclipse.pde.core.IModelChangedListener;
18 import org.eclipse.pde.core.plugin.IPluginElement;
19 import org.eclipse.pde.core.plugin.IPluginModelBase;
20 import org.eclipse.pde.internal.ui.PDEPlugin;
21 import org.eclipse.pde.internal.ui.PDEUIMessages;
22 import org.eclipse.pde.internal.ui.editor.PDESection;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.dnd.Clipboard;
25 import org.eclipse.swt.events.FocusEvent;
26 import org.eclipse.swt.events.FocusListener;
27 import org.eclipse.swt.events.ModifyEvent;
28 import org.eclipse.swt.events.ModifyListener;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.ui.actions.ActionFactory;
34 import org.eclipse.ui.forms.IFormPart;
35 import org.eclipse.ui.forms.IPartSelectionListener;
36 import org.eclipse.ui.forms.widgets.ExpandableComposite;
37 import org.eclipse.ui.forms.widgets.FormToolkit;
38 import org.eclipse.ui.forms.widgets.Section;
39
40 public class BodyTextSection extends PDESection implements IModelChangedListener, IPartSelectionListener {
41     
42     private IPluginElement fCurrentElement;
43     private Text fText;
44     private boolean fBlockNotification;
45
46     public BodyTextSection(ExtensionsPage page, Composite parent) {
47         super(page, parent, ExpandableComposite.TWISTIE);
48         getSection().setText(PDEUIMessages.ManifestEditor_BodyTextSection_title);
49         createClient(getSection(), page.getManagedForm().getToolkit());
50     }
51
52     private void updateTitle(boolean hasContents) {
53         String JavaDoc title;
54         if (hasContents)
55             title = PDEUIMessages.ManifestEditor_BodyTextSection_titleFull;
56         else
57             title = PDEUIMessages.ManifestEditor_BodyTextSection_title;
58         if (!getSection().getText().equals(title)) {
59             getSection().setText(title);
60             getSection().layout();
61         }
62     }
63
64     public void createClient(Section section, FormToolkit toolkit) {
65
66         Composite container = toolkit.createComposite(section);
67         GridLayout layout = new GridLayout();
68         layout.marginHeight = 2;
69         layout.marginWidth = 2;
70         container.setLayout(layout);
71
72         fText = toolkit.createText(container, "", SWT.MULTI | SWT.WRAP | SWT.V_SCROLL); //$NON-NLS-1$
73
fText.setEditable(false);
74         fText.setLayoutData(new GridData(GridData.FILL_BOTH));
75         fText.addFocusListener(new FocusListener() {
76             public void focusGained(FocusEvent e) { }
77             public void focusLost(FocusEvent e) {
78                 handleApply();
79             }
80         });
81         fText.addModifyListener(new ModifyListener() {
82             public void modifyText(ModifyEvent e) {
83                 if (!fBlockNotification)
84                     markDirty();
85             }
86         });
87         GridData gd = new GridData(GridData.FILL_BOTH);
88         gd.heightHint = 60;
89         fText.setLayoutData(gd);
90
91
92         if (SWT.getPlatform().equals("motif") == false) //$NON-NLS-1$
93
toolkit.paintBordersFor(container);
94         section.setClient(container);
95         initialize();
96     }
97
98     public void dispose() {
99         IPluginModelBase model = (IPluginModelBase) getPage().getModel();
100         if (model != null)
101             model.removeModelChangedListener(this);
102         super.dispose();
103     }
104
105     public boolean doGlobalAction(String JavaDoc actionId) {
106         if (actionId.equals(ActionFactory.DELETE.getId())) {
107             fText.cut();
108             return true;
109         }
110         if (actionId.equals(ActionFactory.CUT.getId())) {
111             // delete here and let the editor transfer
112
// the selection to the clipboard
113
fText.cut();
114             return false;
115         }
116         if (actionId.equals(ActionFactory.SELECT_ALL.getId())) {
117             fText.selectAll();
118             return true;
119         }
120         if (actionId.equals(ActionFactory.COPY.getId())) {
121             fText.copy();
122             return true;
123         }
124         if (actionId.equals(ActionFactory.PASTE.getId())) {
125             fText.paste();
126             return true;
127         }
128         return false;
129     }
130
131     private void handleApply() {
132         try {
133             if (fCurrentElement != null)
134                 fCurrentElement.setText(fText.getText().length() > 0 ? fText.getText() : ""); //$NON-NLS-1$
135
} catch (CoreException e) {
136             PDEPlugin.logException(e);
137         }
138     }
139
140     public void commit(boolean onSave) {
141         handleApply();
142         super.commit(onSave);
143     }
144
145     public void initialize() {
146         IPluginModelBase model = (IPluginModelBase) getPage().getModel();
147         model.addModelChangedListener(this);
148         fText.setEditable(model.isEditable());
149         updateInput();
150     }
151
152     public void modelChanged(IModelChangedEvent event) {
153         if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED)
154             return;
155     }
156
157     public void selectionChanged(IFormPart part, ISelection selection) {
158         Object JavaDoc changeObject = ((IStructuredSelection)selection).getFirstElement();
159         if (fCurrentElement != null)
160             if (fCurrentElement == changeObject)
161                 return;
162         if (changeObject instanceof IPluginElement)
163             fCurrentElement = (IPluginElement) changeObject;
164         else
165             fCurrentElement = null;
166         updateInput();
167     }
168     private void updateInput() {
169         updateText(fCurrentElement);
170         fText.setEditable(isEditable() && fCurrentElement != null);
171     }
172
173     private void updateText(IPluginElement element) {
174         String JavaDoc bodyText = element != null ? element.getText() : null;
175
176         fBlockNotification = true;
177         fText.setText(bodyText != null && bodyText.length( )> 0 ? bodyText : ""); //$NON-NLS-1$
178

179         updateTitle(bodyText != null && bodyText.length( )> 0);
180         fBlockNotification = false;
181     }
182     
183     public boolean canPaste(Clipboard clipboard) {
184         return true;
185     }
186 }
187
Popular Tags