View Javadoc

1   /*
2    * Created on Jul 24, 2005.
3    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius University, Bratislava.
4    * This file is protected by the Mozilla Public License
5    * version 1.1 (the "License"); you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    * 
8    *      http://euromath2.sourceforge.net/license.html
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package sk.uniba.euromath.editor.actions;
17  
18  import java.util.Iterator;
19  
20  import org.eclipse.gef.ui.actions.ActionRegistry;
21  import org.eclipse.jface.action.IMenuManager;
22  import org.eclipse.jface.action.IStatusLineManager;
23  import org.eclipse.jface.action.IToolBarManager;
24  import org.eclipse.jface.action.Separator;
25  import org.eclipse.ui.IEditorPart;
26  import org.eclipse.ui.IWorkbenchActionConstants;
27  import org.eclipse.ui.actions.ActionFactory;
28  import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
29  
30  import sk.uniba.euromath.editor.EditorSite;
31  import sk.uniba.euromath.editor.IEditor;
32  import sk.uniba.euromath.editor.IFocusListener;
33  import sk.uniba.euromath.editor.xmlEditor.actions.ShowDocumentPropertiesAction;
34  
35  /***
36   * MultiViewActionContributor is contributor for special IEditorPart -
37   * MutliViewXMLEditor. It is common contributor for all opened views.
38   * Contributes to global bars by it's contributions and by contribution of
39   * active IEditor. When focus is changed in EditorSite (active IEditor has
40   * changed) or in workbench page (another EditorSite has ben activated, ...)
41   * contribution are updated and actions are updated so. Action contributions
42   * must implement IMultiViewAction interface, must be held in global registry
43   * and must have unique id.
44   * 
45   * @author Tomáš Studva 19.9.2005
46   */
47  public class MultiViewActionContributor extends
48          MultiPageEditorActionBarContributor implements IFocusListener {
49  
50      /***
51       * Holds EditorSite with focus.
52       */
53      private EditorSite activeEditorSite;
54  
55      /***
56       * Global action registry for registering IEditor's contibutions by their
57       * contributors.
58       */
59      private final ActionRegistry actionRegistry;
60  
61      /***
62       * Local action registry for contribution of this MultiViewActionContributor
63       * contributor. !! Don't use to registering actions in this class.
64       */
65      // private final ActionRegistry localActionRegistry;
66      /***
67       * Constructor.
68       */
69      public MultiViewActionContributor() {
70          super();
71          this.actionRegistry = new ActionRegistry();
72          // this.localActionRegistry = new ActionRegistry();
73      }
74  
75      /*
76       * (non-Javadoc)
77       * 
78       * @see org.eclipse.ui.part.MultiPageEditorActionBarContributor#setActivePage(org.eclipse.ui.IEditorPart)
79       */
80      @Override
81      public void setActivePage(IEditorPart activeEditor) {
82          if (!(activeEditor instanceof EditorSite))
83              throw new IllegalStateException();
84          if (getActiveEditorSite() != null)
85              getActiveEditorSite().removeFocusListener(this);
86          removeContributionByEditors();
87          setActiveEditorSite(((EditorSite) activeEditor));
88          // redirection of actions
89          IMultiViewAction action;
90          for (Iterator i = getActionRegistry().getActions(); i.hasNext();) {
91              action = (IMultiViewAction) i.next();
92              action.setWorkbenchPart(getActiveEditorSite());
93              action.update();
94          }
95  
96          getActiveEditorSite().setActionContributor(this);
97          getActiveEditorSite().addFocusListener(this);
98          contributeByActiveEditor();
99      }
100 
101     /***
102      * Adds contributions of active editor to bars.
103      */
104     private void contributeByActiveEditor() {
105         IMenuManager menuManager = getActionBars().getMenuManager();
106         IToolBarManager toolBarManager = getActionBars().getToolBarManager();
107         IStatusLineManager statusLineManager = getActionBars()
108                 .getStatusLineManager();
109 
110         // call active IEditor contributor
111         if (getActiveEditorSite() != null) {
112             IActionContributor contributor = getActiveEditorSite()
113                     .getActiveEditor().getActionContributor();
114             contributor.contributeToMenu(menuManager);
115             contributor.contributeToStatusLine(statusLineManager);
116             contributor.contributeToToolBar(toolBarManager);
117             menuManager.updateAll(true);
118             toolBarManager.update(true);
119             statusLineManager.update(true);
120         }
121     }
122 
123     /***
124      * Removes contributions of active editor from bars.
125      */
126     private void removeContributionByEditors() {
127         IMenuManager menuManager = getActionBars().getMenuManager();
128         IToolBarManager toolBarManager = getActionBars().getToolBarManager();
129         IStatusLineManager statusLineManager = getActionBars()
130                 .getStatusLineManager();
131         IMultiViewAction action;
132         for (Iterator i = getActionRegistry().getActions(); i.hasNext();) {
133             action = (IMultiViewAction) i.next();
134             menuManager.remove(action.getId());
135             toolBarManager.remove(action.getId());
136             statusLineManager.remove(action.getId());
137         }
138         menuManager.updateAll(true);
139         toolBarManager.update(true);
140         statusLineManager.update(true);
141     }
142 
143     /***
144      * This contributor's action registry.
145      * 
146      * @return action registry used to registry actions used to contribute by
147      *         this contributor
148      */
149     public ActionRegistry getActionRegistry() {
150         return this.actionRegistry;
151     }
152 
153     /*
154      * (non-Javadoc)
155      * 
156      * @see org.eclipse.ui.part.EditorActionBarContributor#contributeToMenu(org.eclipse.jface.action.IMenuManager)
157      */
158     @Override
159     public void contributeToMenu(IMenuManager menuManager) {
160         super.contributeToMenu(menuManager);
161         // get the File menu
162         final IMenuManager file = (IMenuManager) menuManager
163                 .find(IWorkbenchActionConstants.M_FILE);
164         file.insertAfter(ActionFactory.EXPORT.getId(),
165                 new ShowDocumentPropertiesAction(this));
166         file.insertAfter(ActionFactory.EXPORT.getId(), new Separator());
167     }
168 
169     @Override
170     public void contributeToStatusLine(IStatusLineManager statusLineManager) {
171         super.contributeToStatusLine(statusLineManager);
172     }
173 
174     @Override
175     public void contributeToToolBar(IToolBarManager toolBarManager) {
176         super.contributeToToolBar(toolBarManager);
177     }
178     
179     /*
180      * (non-Javadoc)
181      * @see sk.uniba.euromath.editor.IFocusListener#focusGained(sk.uniba.euromath.editor.IEditor)
182      */
183     public void focusGained(IEditor editor) {
184         contributeByActiveEditor();
185     }
186 
187     /*
188      * (non-Javadoc)
189      * @see sk.uniba.euromath.editor.IFocusListener#focusLost(sk.uniba.euromath.editor.IEditor)
190      */
191     public void focusLost(IEditor editor) {
192         removeContributionByEditors();
193     }
194 
195     /***
196      * @return Returns the activeEditorSite.
197      */
198     public EditorSite getActiveEditorSite() {
199         return this.activeEditorSite;
200     }
201 
202     /***
203      * @param activeEditorSite
204      *            The activeEditorSite to set.
205      */
206     public void setActiveEditorSite(EditorSite activeEditorSite) {
207         this.activeEditorSite = activeEditorSite;
208     }
209 }