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.CaretManager;
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(
80                                                          IMenuManager manager) {
81                                                  // insertTEXAction.update();
82                                                  getInsertTextAction().update();
83                                          }
84                                  });
85  
86                  setEnabled(false);
87  
88                  // Restore state from the previous session.
89                  restoreState();
90          }
91  
92          private void restoreState() {
93  
94          }
95  
96          private void hookGlobalActions() {
97          }
98  
99          private void fillContextMenu() {
100         }
101 
102         private void fillToolbar(IToolBarManager mgr) {
103                 /*
104                  * mgr.removeAll(); if (enabled) { mgr.add(insertTextAction); }
105                  */
106         }
107 
108         private void fillMenu(IMenuManager mgr) {
109                 mgr.removeAll();
110                 // mgr.add(insertTEXAction);
111                 mgr.add(getInsertTextAction());
112         }
113 
114         private void createActions() {
115                 // insertTEXAction = new InsertTEXAction(this);
116                 setInsertTextAction(new InsertTextAction(this));
117         }
118 
119         @Override
120         public void setFocus() {
121                 getTextWidget().setFocus();
122         }
123 
124         @Override
125         public Object getAdapter(Class adapter) {
126                 if (adapter.equals(IInputProvider.class))
127                         return this;
128 
129                 if (getActiveEditorSite() != null) {
130                         if (adapter.equals(XMLAccess.class))
131                                 return getActiveEditorSite().getXMLAccess();
132 
133                         if (adapter.equals(CaretManager.class))
134                                 return getActiveEditorSite().getAdapter(
135                                                 CaretManager.class);
136 
137                         if (adapter.equals(ISelectionProvider.class)
138                                         && (getActiveEditorSite()
139                                                         .getActiveEditor() instanceof ISelectionProvider))
140                                 return (ISelectionProvider) getActiveEditorSite()
141                                                 .getActiveEditor();
142 
143                         if (adapter.equals(IDOMSelectionProvider.class)
144                                         && (getActiveEditorSite()
145                                                         .getActiveEditor() instanceof IDOMSelectionProvider))
146                                 return getActiveEditorSite().getActiveEditor();
147                 }
148 
149                 return super.getAdapter(adapter);
150         }
151 
152         public Object getInput() {
153                 return getTextWidget().getText();
154         }
155 
156         private void setEnabled(boolean enabled) {
157                 this.enabled = enabled;
158                 // textWidget.setEnabled(enabled);
159                 fillMenu(getViewSite().getActionBars().getMenuManager());
160                 fillToolbar(getViewSite().getActionBars().getToolBarManager());
161                 fillContextMenu();
162                 // fillEditorContextMenu();
163                 getViewSite().getActionBars().updateActionBars();
164         }
165 
166         private void calculateEnabled() {
167                 setEnabled((getActiveEditorSite() != null)
168                                 && (getAdapter(CaretManager.class) != null));
169 
170         }
171 
172         private void refresh(IWorkbenchPart part) {
173                 if (part instanceof MultiViewXMLEditor) {
174                         MultiViewXMLEditor editor = (MultiViewXMLEditor) part;
175                         setActiveEditorSite(editor.getActiveView());
176                 } else {
177                         setActiveEditorSite(null);
178                 }
179                 calculateEnabled();
180         }
181 
182         public void partActivated(IWorkbenchPart part) {
183                 refresh(part);
184         }
185 
186         public void partBroughtToTop(IWorkbenchPart part) {
187         }
188 
189         public void partClosed(IWorkbenchPart part) {
190                 if (getActiveEditorSite() != null)
191                         getActiveEditorSite().removeFocusListener(this);
192                 refresh(part);
193         }
194 
195         public void partDeactivated(IWorkbenchPart part) {
196         }
197 
198         public void partOpened(IWorkbenchPart part) {
199                 refresh(part);
200                 if (getActiveEditorSite() != null)
201                         getActiveEditorSite().addFocusListener(this);
202         }
203 
204         public void focusGained(IEditor editor) {
205                 calculateEnabled();
206         }
207 
208         public void focusLost(IEditor editor) {
209                 calculateEnabled();
210         }
211 
212         /***
213          * @return Returns the insertTextAction.
214          */
215         public InsertTextAction getInsertTextAction() {
216                 return this.insertTextAction;
217         }
218 
219         /***
220          * @return Returns the activeEditorSite.
221          */
222         protected EditorSite getActiveEditorSite() {
223                 return this.activeEditorSite;
224         }
225 
226         /***
227          * @param activeEditorSite
228          *                The activeEditorSite to set.
229          */
230         protected void setActiveEditorSite(EditorSite activeEditorSite) {
231                 this.activeEditorSite = activeEditorSite;
232         }
233 
234         /***
235          * @return Returns the textWidget.
236          */
237         protected Text getTextWidget() {
238                 return this.textWidget;
239         }
240 
241         /***
242          * @param textWidget
243          *                The textWidget to set.
244          */
245         protected void setTextWidget(Text textWidget) {
246                 this.textWidget = textWidget;
247         }
248 
249         /***
250          * @return Returns the enabled.
251          */
252         protected boolean isEnabled() {
253                 return this.enabled;
254         }
255 
256         /***
257          * @param insertTextAction
258          *                The insertTextAction to set.
259          */
260         protected void setInsertTextAction(InsertTextAction insertTextAction) {
261                 this.insertTextAction = insertTextAction;
262         }
263 
264 }