View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.plugin.views.inputBox;
13  
14  import org.eclipse.jface.action.IMenuListener;
15  import org.eclipse.jface.action.IMenuManager;
16  import org.eclipse.jface.action.IToolBarManager;
17  import org.eclipse.jface.viewers.IInputProvider;
18  import org.eclipse.jface.viewers.ISelectionProvider;
19  import org.eclipse.swt.SWT;
20  import org.eclipse.swt.widgets.Composite;
21  import org.eclipse.swt.widgets.Text;
22  import org.eclipse.ui.IPartListener;
23  import org.eclipse.ui.IViewSite;
24  import org.eclipse.ui.IWorkbenchPart;
25  import org.eclipse.ui.PartInitException;
26  import org.eclipse.ui.part.ViewPart;
27  
28  import sk.uniba.euromath.document.XMLAccess;
29  import sk.uniba.euromath.editor.EditorSite;
30  import sk.uniba.euromath.editor.IEditor;
31  import sk.uniba.euromath.editor.IFocusListener;
32  import sk.uniba.euromath.editor.MultiViewXMLEditor;
33  import sk.uniba.euromath.editor.selections.IDOMSelectionProvider;
34  import sk.uniba.euromath.editor.textEditor.ICaretProvider;
35  import sk.uniba.euromath.plugin.views.inputBox.actions.InsertTextAction;
36  
37  /***
38   * Box to fast input of text.
39   * 
40   * @author Tomáš Studva 20.7.2005
41   */
42  public class InputBoxView extends ViewPart implements IInputProvider,
43          IPartListener, IFocusListener {
44  
45      private Text textWidget;
46  
47      // TODO Studva Presunul som InsertTEXAction do mathml pluginu, ak sa ma
48      // napajat do
49      // tejto triedy, treba to vyriesit cez extension point. mato.
50      // private InsertTEXAction insertTEXAction;
51  
52      private InsertTextAction insertTextAction;
53  
54      private EditorSite activeEditorSite;
55  
56      private boolean enabled;
57  
58      public static String ID = "sk.uniba.euromath.plugin.views.inputBox.InputBoxView";
59  
60      public InputBoxView() {
61          super();
62      }
63  
64      @Override
65      public void init(IViewSite site) throws PartInitException {
66          super.init(site);
67      }
68  
69      @Override
70      public void createPartControl(Composite parent) {
71          getSite().getPage().addPartListener(this);
72          setTextWidget(new Text(parent, SWT.MULTI));
73  
74          // actions
75          createActions();
76          hookGlobalActions();
77          getViewSite().getActionBars().getMenuManager().addMenuListener(
78                  new IMenuListener() {
79                      public void menuAboutToShow(IMenuManager manager) {
80                          // insertTEXAction.update();
81                          getInsertTextAction().update();
82                      }
83                  });
84  
85          setEnabled(false);
86  
87          // Restore state from the previous session.
88          restoreState();
89      }
90  
91      private void restoreState() {
92  
93      }
94  
95      private void hookGlobalActions() {
96      }
97  
98      private void fillContextMenu() {
99      }
100 
101     private void fillToolbar(IToolBarManager mgr) {
102         /*
103          * mgr.removeAll(); if (enabled) { mgr.add(insertTextAction); }
104          */
105     }
106 
107     private void fillMenu(IMenuManager mgr) {
108         mgr.removeAll();
109         // mgr.add(insertTEXAction);
110         mgr.add(getInsertTextAction());
111     }
112 
113     private void createActions() {
114         // insertTEXAction = new InsertTEXAction(this);
115         setInsertTextAction(new InsertTextAction(this));
116     }
117 
118     @Override
119     public void setFocus() {
120         getTextWidget().setFocus();
121     }
122 
123     @Override
124     public Object getAdapter(Class adapter) {
125         if (adapter.equals(IInputProvider.class))
126             return this;
127 
128         if (getActiveEditorSite() != null) {
129             if (adapter.equals(XMLAccess.class))
130                 return getActiveEditorSite().getXMLAccess();
131 
132             if (adapter.equals(ICaretProvider.class)
133                     && (getActiveEditorSite().getActiveEditor() instanceof ICaretProvider))
134                 return (ICaretProvider) getActiveEditorSite().getActiveEditor();
135 
136             if (adapter.equals(ISelectionProvider.class)
137                     && (getActiveEditorSite().getActiveEditor() instanceof ISelectionProvider))
138                 return (ISelectionProvider) getActiveEditorSite()
139                         .getActiveEditor();
140 
141             if (adapter.equals(IDOMSelectionProvider.class)
142                     && (getActiveEditorSite().getActiveEditor() instanceof IDOMSelectionProvider))
143                 return getActiveEditorSite().getActiveEditor();
144         }
145 
146         return super.getAdapter(adapter);
147     }
148 
149     public Object getInput() {
150         return getTextWidget().getText();
151     }
152 
153     private void setEnabled(boolean enabled) {
154         this.enabled = enabled;
155         // textWidget.setEnabled(enabled);
156         fillMenu(getViewSite().getActionBars().getMenuManager());
157         fillToolbar(getViewSite().getActionBars().getToolBarManager());
158         fillContextMenu();
159         // fillEditorContextMenu();
160         getViewSite().getActionBars().updateActionBars();
161     }
162 
163     private void calculateEnabled() {
164         setEnabled((getActiveEditorSite() != null)
165                 && (getActiveEditorSite().getActiveEditor() instanceof ICaretProvider));
166 
167     }
168 
169     private void refresh(IWorkbenchPart part) {
170         if (part instanceof MultiViewXMLEditor) {
171             MultiViewXMLEditor editor = (MultiViewXMLEditor) part;
172             setActiveEditorSite(editor.getActiveView());
173         } else {
174             setActiveEditorSite(null);
175         }
176         calculateEnabled();
177     }
178 
179     public void partActivated(IWorkbenchPart part) {
180         refresh(part);
181     }
182 
183     public void partBroughtToTop(IWorkbenchPart part) {
184     }
185 
186     public void partClosed(IWorkbenchPart part) {
187         if (getActiveEditorSite() != null)
188             getActiveEditorSite().removeFocusListener(this);
189         refresh(part);
190     }
191 
192     public void partDeactivated(IWorkbenchPart part) {
193     }
194 
195     public void partOpened(IWorkbenchPart part) {
196         refresh(part);
197         if (getActiveEditorSite() != null)
198             getActiveEditorSite().addFocusListener(this);
199     }
200 
201     public void focusGained(IEditor editor) {
202         calculateEnabled();
203     }
204 
205     public void focusLost(IEditor editor) {
206         calculateEnabled();
207     }
208 
209     /***
210      * @return Returns the insertTextAction.
211      */
212     public InsertTextAction getInsertTextAction() {
213         return this.insertTextAction;
214     }
215 
216     /***
217      * @return Returns the activeEditorSite.
218      */
219     protected EditorSite getActiveEditorSite() {
220         return this.activeEditorSite;
221     }
222 
223     /***
224      * @param activeEditorSite
225      *            The activeEditorSite to set.
226      */
227     protected void setActiveEditorSite(EditorSite activeEditorSite) {
228         this.activeEditorSite = activeEditorSite;
229     }
230 
231     /***
232      * @return Returns the textWidget.
233      */
234     protected Text getTextWidget() {
235         return this.textWidget;
236     }
237 
238     /***
239      * @param textWidget
240      *            The textWidget to set.
241      */
242     protected void setTextWidget(Text textWidget) {
243         this.textWidget = textWidget;
244     }
245 
246     /***
247      * @return Returns the enabled.
248      */
249     protected boolean isEnabled() {
250         return this.enabled;
251     }
252 
253     /***
254      * @param insertTextAction
255      *            The insertTextAction to set.
256      */
257     protected void setInsertTextAction(InsertTextAction insertTextAction) {
258         this.insertTextAction = insertTextAction;
259     }
260 
261 }