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.xmlEditor.actions;
13  
14  import org.eclipse.ui.IWorkbenchPart;
15  import org.w3c.dom.Node;
16  
17  import sk.baka.xml.gene.ExportException;
18  
19  /***
20   * Inserts entity reference to document. Entity name is selected in constructor
21   * or by wizard.
22   * 
23   * @author Tomáš Studva 14.6.2005
24   */
25  public class InsertEntityReferenceAction extends InsertAction {
26  
27          /***
28           * Id of action.
29           */
30          public static final String id = InsertEntityReferenceAction.class
31                          .toString();
32  
33          /***
34           * Name of entity to insert.
35           */
36          private final String entityName;
37  
38          /***
39           * Constructor to create action to insert entity reference directly(if
40           * <code>entityName</code> in not null) or by wizard at position.
41           * 
42           * @param position
43           *                place where to insert node
44           * @param entityName
45           *                entity name to insert by reference, or null to use
46           *                wizard
47           * @param part
48           *                workbench part to associate
49           */
50          public InsertEntityReferenceAction(InsertPosition position,
51                          String entityName, IWorkbenchPart part) {
52                  super(position, part);
53                  if (entityName == null) {
54                          setId(id);
55                          setText("Insert entity reference wizard");
56                  } else {
57                          setId(id + entityName);
58                          setText(entityName);
59                  }
60                  this.entityName = entityName;
61          }
62  
63          /***
64           * Constructor to create action to insert entity reference directly(if
65           * <code>entityName</code> in not null) or by wizard at position at
66           * positionNode.
67           * 
68           * @param nodePosition
69           *                insertion will be near this node
70           * @param position
71           *                place where to insert node
72           * @param entityName
73           *                entity name to insert by reference, or null to use
74           *                wizard
75           * @param part
76           *                associated workbench part
77           */
78          public InsertEntityReferenceAction(Node nodePosition,
79                          InsertPosition position, String entityName,
80                          IWorkbenchPart part) {
81                  super(nodePosition, position, part);
82                  if (entityName == null) {
83                          setId(id);
84                          setText("Insert entity wizard");
85                  } else {
86                          setId(id + entityName);
87                          setText(entityName);
88                  }
89                  this.entityName = entityName;
90          }
91  
92          @Override
93          public void runAsCommand() {
94                  try {
95                          if (this.entityName != null) {
96                                  getModifyHelper().insertEntity(this.entityName,
97                                                  getPointer());
98                          } else {
99                                  getModifyHelper().insertEntity(getShell(),
100                                                 getPointer());
101                         }
102                 } catch (ExportException e) {
103                         handleExportException(e);
104                 }
105         }
106 
107         @Override
108         protected boolean calculateEnabled() {
109                 return super.calculateEnabled() && (getPointer() != null);
110         }
111 
112 }