View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the License); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language 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.ikslibs.interval.DOMInterval;
18  import sk.baka.ikslibs.interval.DOMIntervalSet;
19  import sk.baka.ikslibs.ptr.DomPointer;
20  import sk.baka.ikslibs.ptr.DomPointerFactory;
21  import sk.uniba.euromath.editor.textEditor.CaretManager;
22  import sk.uniba.euromath.editor.xmlEditor.actions.lang.Messages;
23  
24  /***
25   * Selects parent of selected element or element with caret.
26   * 
27   * @author TV Created on 22.7.2004
28   * 
29   */
30  public class SelectParentAction extends NodeManipulateAction {
31  
32          /***
33           * Default id of action.
34           */
35          public static final String id = SelectParentAction.class.toString();
36  
37          public Node nodeToSelect;
38  
39          /***
40           * Constructor.
41           * 
42           * @param part
43           */
44          public SelectParentAction(IWorkbenchPart part) {
45                  super(part, NodeAcquireType.Single, true);
46                  setId(id);
47                  setText(Messages.getString("SelectParentAction.Text")); //$NON-NLS-1$
48          }
49  
50          /***
51           * Is enabled if is selected one node or caret is active.
52           */
53          @Override
54          protected boolean calculateEnabled() {
55                  if (!super.calculateEnabled())
56                          return false;
57                  if (getNode().getParentNode() != null) {
58                          // case 1: parent of selected node
59                          this.nodeToSelect = getNode().getParentNode();
60                          return true;
61                  }
62  
63                  if ((getCaretManager() != null)
64                                  && (getCaretManager()
65                                                  .getActiveTextPieceKeeper() != null)) {
66                          String nodeId = getCaretManager()
67                                          .getActiveTextPieceKeeper()
68                                          .getTextPieceInfo().getNodeID();
69                          this.nodeToSelect = getXMLAccess().getIDManager()
70                                          .getNodeNull(nodeId).item(0)
71                                          .getParentNode();
72                  }
73                  return this.nodeToSelect != null;
74          }
75  
76          /***
77           * Getter for caret manager.
78           * 
79           * @return caret manager from associated workbench part
80           */
81          protected CaretManager getCaretManager() {
82                  return (CaretManager) (getWorkbenchPart()
83                                  .getAdapter(CaretManager.class));
84          }
85  
86          /***
87           * Selects parent of selected node or parent of node with caret.
88           */
89          @Override
90          public void run() {
91                  DomPointer startPointer = DomPointerFactory
92                                  .create(this.nodeToSelect);
93                  getSelectionProvider()
94                                  .setSelection(
95                                                  new DOMIntervalSet(
96                                                                  new DOMInterval(
97                                                                                  startPointer,
98                                                                                  startPointer
99                                                                                                  .getNextSibling())));
100                 return;
101 
102         }
103 }