KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > PDEFormEditorContributor


1 /*******************************************************************************
2  * Copyright (c) 2003, 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;
12
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.action.ICoolBarManager;
18 import org.eclipse.jface.action.IMenuManager;
19 import org.eclipse.jface.action.IStatusLineManager;
20 import org.eclipse.jface.action.IToolBarManager;
21 import org.eclipse.jface.action.Separator;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.pde.core.IBaseModel;
24 import org.eclipse.pde.core.IEditable;
25 import org.eclipse.pde.internal.ui.PDEPlugin;
26 import org.eclipse.pde.internal.ui.PDEUIMessages;
27 import org.eclipse.ui.IActionBars;
28 import org.eclipse.ui.IEditorActionBarContributor;
29 import org.eclipse.ui.IEditorPart;
30 import org.eclipse.ui.actions.ActionFactory;
31 import org.eclipse.ui.forms.editor.IFormPage;
32 import org.eclipse.ui.ide.IDEActionFactory;
33 import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
34 import org.eclipse.ui.texteditor.IUpdate;
35
36 public class PDEFormEditorContributor extends MultiPageEditorActionBarContributor {
37
38     protected PDEFormEditor fEditor;
39
40     protected IFormPage fPage;
41
42     private SaveAction fSaveAction;
43
44     protected RevertAction fRevertAction;
45
46     private ClipboardAction fCutAction;
47
48     private ClipboardAction fCopyAction;
49
50     private ClipboardAction fPasteAction;
51
52     private Hashtable JavaDoc fGlobalActions = new Hashtable JavaDoc();
53
54     class GlobalAction extends Action implements IUpdate {
55         private String JavaDoc id;
56
57         public GlobalAction(String JavaDoc id) {
58             this.id = id;
59         }
60
61         public void run() {
62             fEditor.performGlobalAction(id);
63             updateSelectableActions(fEditor.getSelection());
64         }
65
66         public void update() {
67             getActionBars().updateActionBars();
68         }
69     }
70
71     class ClipboardAction extends GlobalAction {
72         public ClipboardAction(String JavaDoc id) {
73             super(id);
74             setEnabled(false);
75         }
76
77         public void selectionChanged(ISelection selection) {
78         }
79
80         public boolean isEditable() {
81             if (fEditor == null)
82                 return false;
83             IBaseModel model = fEditor.getAggregateModel();
84             return model instanceof IEditable ? ((IEditable) model).isEditable() : false;
85         }
86     }
87
88     class CutAction extends ClipboardAction {
89         public CutAction() {
90             super(ActionFactory.CUT.getId());
91             setText(PDEUIMessages.EditorActions_cut);
92         }
93
94         public void selectionChanged(ISelection selection) {
95             setEnabled(isEditable() && fEditor.canCopy(selection));
96         }
97     }
98
99     class CopyAction extends ClipboardAction {
100         public CopyAction() {
101             super(ActionFactory.COPY.getId());
102             setText(PDEUIMessages.EditorActions_copy);
103         }
104
105         public void selectionChanged(ISelection selection) {
106             setEnabled(fEditor.canCopy(selection));
107         }
108     }
109
110     class PasteAction extends ClipboardAction {
111         public PasteAction() {
112             super(ActionFactory.PASTE.getId());
113             setText(PDEUIMessages.EditorActions_paste);
114         }
115
116         public void selectionChanged(ISelection selection) {
117             setEnabled(isEditable() && fEditor.canPasteFromClipboard());
118         }
119     }
120
121     class SaveAction extends Action implements IUpdate {
122         public SaveAction() {
123         }
124
125         public void run() {
126             if (fEditor != null)
127                 PDEPlugin.getActivePage().saveEditor(fEditor, false);
128         }
129
130         public void update() {
131             setEnabled(fEditor != null ? fEditor.isDirty() : false);
132         }
133     }
134
135     class RevertAction extends Action implements IUpdate {
136         public RevertAction() {
137         }
138
139         public void run() {
140             if (fEditor != null)
141                 fEditor.doRevert();
142         }
143
144         public void update() {
145             setEnabled(fEditor != null ? fEditor.isDirty() : false);
146         }
147     }
148
149     public PDEFormEditorContributor(String JavaDoc menuName) {}
150     
151     private void addGlobalAction(String JavaDoc id) {
152         GlobalAction action = new GlobalAction(id);
153         addGlobalAction(id, action);
154     }
155
156     private void addGlobalAction(String JavaDoc id, Action action) {
157         fGlobalActions.put(id, action);
158         getActionBars().setGlobalActionHandler(id, action);
159     }
160
161     public void addClipboardActions(IMenuManager mng) {
162         mng.add(fCutAction);
163         mng.add(fCopyAction);
164         mng.add(fPasteAction);
165         mng.add(new Separator());
166         mng.add(fRevertAction);
167     }
168
169     public void contextMenuAboutToShow(IMenuManager mng) {
170         contextMenuAboutToShow(mng, true);
171     }
172
173     public void contextMenuAboutToShow(IMenuManager mng, boolean addClipboard) {
174         if (fEditor != null)
175             updateSelectableActions(fEditor.getSelection());
176         if (addClipboard)
177             addClipboardActions(mng);
178         mng.add(fSaveAction);
179     }
180
181     public void contributeToMenu(IMenuManager mm) {
182     }
183
184     public void contributeToStatusLine(IStatusLineManager slm) {
185     }
186
187     public void contributeToToolBar(IToolBarManager tbm) {
188     }
189
190     public void contributeToCoolBar(ICoolBarManager cbm) {
191     }
192
193     public PDEFormEditor getEditor() {
194         return fEditor;
195     }
196
197     public IAction getGlobalAction(String JavaDoc id) {
198         return (IAction) fGlobalActions.get(id);
199     }
200
201     public IAction getSaveAction() {
202         return fSaveAction;
203     }
204
205     public IAction getRevertAction() {
206         return fRevertAction;
207     }
208
209     public IStatusLineManager getStatusLineManager() {
210         return getActionBars().getStatusLineManager();
211     }
212
213     protected void makeActions() {
214         // clipboard actions
215
fCutAction = new CutAction();
216         fCopyAction = new CopyAction();
217         fPasteAction = new PasteAction();
218         addGlobalAction(ActionFactory.CUT.getId(), fCutAction);
219         addGlobalAction(ActionFactory.COPY.getId(), fCopyAction);
220         addGlobalAction(ActionFactory.PASTE.getId(), fPasteAction);
221         addGlobalAction(ActionFactory.DELETE.getId());
222         // undo/redo
223
addGlobalAction(ActionFactory.UNDO.getId());
224         addGlobalAction(ActionFactory.REDO.getId());
225         // select/find
226
addGlobalAction(ActionFactory.SELECT_ALL.getId());
227         addGlobalAction(ActionFactory.FIND.getId());
228         // bookmark
229
addGlobalAction(IDEActionFactory.BOOKMARK.getId());
230         // save/revert
231
fSaveAction = new SaveAction();
232         fSaveAction.setText(PDEUIMessages.EditorActions_save);
233         fRevertAction = new RevertAction();
234         fRevertAction.setText(PDEUIMessages.EditorActions_revert);
235         addGlobalAction(ActionFactory.REVERT.getId(), fRevertAction);
236     }
237
238     public void setActiveEditor(IEditorPart targetEditor) {
239         if (targetEditor instanceof PDESourcePage) {
240             // Fixing the 'goto line' problem -
241
// the action is thinking that source page
242
// is a standalone editor and tries to activate it
243
// #19361
244
PDESourcePage page = (PDESourcePage) targetEditor;
245             PDEPlugin.getActivePage().activate(page.getEditor());
246             return;
247         }
248         if (!(targetEditor instanceof PDEFormEditor))
249             return;
250         
251         fEditor = (PDEFormEditor) targetEditor;
252         fEditor.updateUndo(getGlobalAction(ActionFactory.UNDO.getId()),
253                            getGlobalAction(ActionFactory.REDO.getId()));
254         setActivePage(fEditor.getActiveEditor());
255         updateSelectableActions(fEditor.getSelection());
256     }
257
258     public void setActivePage(IEditorPart newEditor) {
259         if (fEditor == null)
260             return;
261         IFormPage oldPage = fPage;
262         fPage = fEditor.getActivePageInstance();
263         if (fPage != null) {
264             updateActions();
265             if (oldPage != null && !oldPage.isEditor() && !fPage.isEditor()) {
266                 getActionBars().updateActionBars();
267             }
268         }
269     }
270
271     public void updateActions() {
272         fSaveAction.update();
273         fRevertAction.update();
274     }
275
276     public void updateSelectableActions(ISelection selection) {
277         if (fEditor != null) {
278             fCutAction.selectionChanged(selection);
279             fCopyAction.selectionChanged(selection);
280             fPasteAction.selectionChanged(selection);
281         }
282     }
283     
284     public IEditorActionBarContributor getSourceContributor() {
285         return null;
286     }
287     
288     public void init(IActionBars bars) {
289         super.init(bars);
290         makeActions();
291     }
292
293 }
294
Popular Tags