1
2
3
4
5
6
7
8
9
10
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
48
49
50
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
75 createActions();
76 hookGlobalActions();
77 getViewSite().getActionBars().getMenuManager().addMenuListener(
78 new IMenuListener() {
79 public void menuAboutToShow(
80 IMenuManager manager) {
81
82 getInsertTextAction().update();
83 }
84 });
85
86 setEnabled(false);
87
88
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
105
106 }
107
108 private void fillMenu(IMenuManager mgr) {
109 mgr.removeAll();
110
111 mgr.add(getInsertTextAction());
112 }
113
114 private void createActions() {
115
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
159 fillMenu(getViewSite().getActionBars().getMenuManager());
160 fillToolbar(getViewSite().getActionBars().getToolBarManager());
161 fillContextMenu();
162
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 }