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.editor.textEditor.actions;
13  
14  import java.util.List;
15  import java.util.Map;
16  
17  import javax.xml.namespace.QName;
18  
19  import org.eclipse.gef.GraphicalViewer;
20  import org.eclipse.jface.action.IAction;
21  import org.eclipse.jface.action.IMenuListener;
22  import org.eclipse.jface.action.IMenuManager;
23  import org.eclipse.jface.action.IStatusLineManager;
24  import org.eclipse.jface.action.IToolBarManager;
25  import org.eclipse.jface.action.Separator;
26  
27  import sk.baka.ikslibs.ptr.DomPointer;
28  import sk.uniba.euromath.editor.EditorSite;
29  import sk.uniba.euromath.editor.textEditor.CaretManager;
30  import sk.uniba.euromath.editor.textEditor.actions.lang.Messages;
31  import sk.uniba.euromath.editor.xmlEditor.actions.EMMenuManager;
32  import sk.uniba.euromath.editor.xmlEditor.actions.EmptyAction;
33  import sk.uniba.euromath.editor.xmlEditor.actions.InsertElementAction;
34  import sk.uniba.euromath.editor.xmlEditor.actions.InsertEntityReferenceAction;
35  import sk.uniba.euromath.editor.xmlEditor.actions.InsertPosition;
36  import sk.uniba.euromath.editor.xmlEditor.actions.PasteAction;
37  import sk.uniba.euromath.editor.xmlEditor.actions.XMLActionContributor;
38  
39  /***
40   * @author Tomáš Studva 30.9.2005
41   */
42  public class TextActionContributor extends XMLActionContributor {
43  
44          /***
45           * Insert at caret submenu.
46           */
47          private EMMenuManager insertAtCaretMenu;
48  
49          /***
50           * Entity submenu.
51           */
52          private EMMenuManager entityMenu;
53  
54          /***
55           * Caret provider reference.
56           */
57          private CaretManager caretManager;
58  
59          /***
60           * Constructor.
61           * 
62           * @param site
63           * @param viewer
64           * @param caretProvider
65           */
66          public TextActionContributor(EditorSite site, GraphicalViewer viewer,
67                          CaretManager caretManager) {
68                  super(site, viewer);
69                  this.caretManager = caretManager;
70          }
71  
72          @Override
73          protected void configureFactory() {
74                  super.configureFactory();
75                  //clipboard actions
76                  getActionFactory().registerAction(CopyAsTextAction.id,
77                                  CopyAsTextAction.class);
78                  getActionFactory().registerAction(PasteTextAction.id,
79                                  PasteTextAction.class);
80                  
81                  // delete actions
82                  getActionFactory().registerAction(DeleteTextOnlyAction.id,
83                                  DeleteTextOnlyAction.class);
84          }
85  
86          /***
87           * Creates two more menus.
88           */
89          @Override
90          protected void createContextMenu() {
91                  super.createContextMenu();
92                  this.insertAtCaretMenu = new EMMenuManager(
93                                  Messages
94                                                  .getString("TextActionContributor.InsertAtCaretSubmenuText"), //$NON-NLS-1$
95                                  ActionConsts.INSERT_SUBMENU);
96                  getInsertAtCaretMenu().setRemoveAllWhenShown(true);
97                  getInsertAtCaretMenu().addMenuListener(new IMenuListener() {
98                          public void menuAboutToShow(IMenuManager manager) {
99                                  populateInsertAtCaretMenu(manager);
100                         }
101                 });
102                 this.entityMenu = new EMMenuManager(
103                                 Messages
104                                                 .getString("TextActionContributor.EntitySubmenuText"), //$NON-NLS-1$
105                                 ActionConsts.ENTITY_SUBMENU);
106                 getEntityMenu().setRemoveAllWhenShown(true);
107                 getEntityMenu().addMenuListener(new IMenuListener() {
108                         public void menuAboutToShow(IMenuManager manager) {
109                                 populateEntityMenu(manager);
110                         }
111                 });
112         }
113 
114         @Override
115         public void contributeToToolBar(IToolBarManager manager) {
116                 manager.add(new ActivateStructureTool(getGraphicalViewer()
117                                 .getEditDomain()));
118                 manager.add(new ActivateTextTool(getGraphicalViewer()
119                                 .getEditDomain(), this.caretManager));
120         }
121 
122         @Override
123         protected void contributeToContextMenu(IMenuManager manager) {
124                 super.contributeToContextMenu(manager);
125                 // add copy as text
126                 manager.insertBefore(PasteAction.id,
127                                 checkOrCreateAction(CopyAsTextAction.id));
128 
129                 // change ins pos of paste action
130                 ((PasteAction) checkOrCreateAction(PasteAction.id))
131                                 .setInsertPosition(InsertPosition.AtCaret);
132                 // append paste text action
133                 manager
134                                 .appendToGroup(
135                                                 sk.uniba.euromath.editor.xmlEditor.actions.ActionConsts.CLIPBOARD_GROUP,
136                                                 checkOrCreateAction(PasteTextAction.id));
137                 // Insert - submenu
138                 getInsertAtCaretMenu().add(checkOrCreateAction(EmptyAction.id));
139 
140                 // TODO Studva contribute delete text only action
141                 // manager
142                 // .appendToGroup(
143                 // sk.uniba.euromath.editor.xmlEditor.actions.ActionConsts.SELECTION_GROUP,
144                 // checkOrCreateAction(DeleteTextOnlyAction.id));
145                 manager
146                                 .appendToGroup(
147                                                 sk.uniba.euromath.editor.xmlEditor.actions.ActionConsts.SELECTION_GROUP,
148                                                 new Separator());
149 
150                 manager
151                                 .appendToGroup(
152                                                 sk.uniba.euromath.editor.xmlEditor.actions.ActionConsts.SELECTION_GROUP,
153                                                 this.insertAtCaretMenu);
154         }
155 
156         /***
157          * Fills up insert menu to right content. Prior insert menu must be
158          * empty.
159          * 
160          * @param manager
161          *                of insert menu
162          */
163         protected void populateInsertAtCaretMenu(IMenuManager manager) {
164                 DomPointer pointer = caretManager.getDOMPointer();
165                 //
166                 // Entity - submenu of Insert
167                 manager.add(getEntityMenu());
168                 getEntityMenu().add(checkOrCreateAction(EmptyAction.id));
169                 // Separator
170                 manager
171                                 .add(new Separator(
172                                                 ActionConsts.INSERT_ELEMENT_WIZARD_AT_CARET_GROUP));
173                 // Insert element wizard action
174                 manager.add(new InsertElementAction(InsertPosition.AtCaret,
175                                 null, getWorkbenchPart()));
176                 // Separator
177                 manager
178                                 .add(new Separator(
179                                                 ActionConsts.INSERT_ELEMENT_WITH_NAME_AT_CARET_GROUP));
180                 // Insert actions by element name
181                 if (pointer != null) {
182                         List<QName> qNames = getDocumentModifyHelper()
183                                         .getInsertableElementsQNames(pointer);
184                         for (QName qName : qNames) {
185                                 manager.add(new InsertElementAction(
186                                                 InsertPosition.AtCaret, qName,
187                                                 getWorkbenchPart()));
188                         }
189                 }
190                 manager.updateAll(true);
191         }
192 
193         /***
194          * Fills up entity menu to right content. Prior entity menu must be
195          * empty.
196          * 
197          * @param manager
198          *                of entity menu
199          */
200         protected void populateEntityMenu(IMenuManager manager) {
201                 DomPointer pointer = caretManager.getDOMPointer();
202                 // Insert entity reference wizard action
203                 manager.add(new InsertEntityReferenceAction(
204                                 InsertPosition.AtCaret, null,
205                                 getWorkbenchPart()));
206                 // Separator
207                 manager
208                                 .add(new Separator(
209                                                 ActionConsts.INSERT_ENTITY_WITH_NAME_AT_CARET_GROUP));
210                 // Insert entity actions by names
211                 if (pointer != null) {
212                         List<String> entityNames = getDocumentModifyHelper()
213                                         .getInsertableEntities(null, pointer);
214                         if (entityNames != null) {
215                                 for (String entityName : entityNames) {
216                                         manager
217                                                         .add(new InsertEntityReferenceAction(
218                                                                         InsertPosition.AtCaret,
219                                                                         entityName,
220                                                                         getWorkbenchPart()));
221                                 }
222                         }
223                 }
224                 manager.updateAll(true);
225         }
226 
227         @Override
228         protected void populateInsertPositionMenu(IMenuManager manager,
229                         InsertPosition position) {
230                 manager.add(new PasteTextAction(position, getWorkbenchPart()));
231                 super.populateInsertPositionMenu(manager, position);
232         }
233 
234         @Override
235         public Map<String, IAction> getGlobalActionHandlers() {
236                 Map<String, IAction> result = super.getGlobalActionHandlers();
237                 result.put(PasteAction.id, new PasteAction(
238                                 InsertPosition.AtCaret, getWorkbenchPart()));
239                 return result;
240         }
241 
242         @Override
243         public void contributeToStatusLine(IStatusLineManager manager) {
244                 super.contributeToStatusLine(manager);
245                 // display caret position in line and offset
246                 // manager.add(checkOrCreateAction(DeleteMixedAction.id));
247         }
248 
249         @Override
250         public void contributeToMenu(IMenuManager manager) {
251                 super.contributeToMenu(manager);
252         }
253 
254         /***
255          * @return Returns the entityMenu.
256          */
257         protected EMMenuManager getEntityMenu() {
258                 return this.entityMenu;
259         }
260 
261         /***
262          * @return Returns the insertMenu.
263          */
264         protected EMMenuManager getInsertAtCaretMenu() {
265                 return this.insertAtCaretMenu;
266         }
267 }