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.commands;
13  
14  import java.util.List;
15  
16  import org.eclipse.core.runtime.IStatus;
17  import org.eclipse.gef.commands.Command;
18  import org.w3c.dom.Node;
19  
20  import sk.baka.ikslibs.interval.DOMInterval;
21  import sk.baka.ikslibs.interval.DOMIntervalSet;
22  import sk.baka.xml.gene.ExportException;
23  import sk.uniba.euromath.EuroMath;
24  import sk.uniba.euromath.document.XMLAccess;
25  import sk.uniba.euromath.editor.textEditor.CaretManager;
26  import sk.uniba.euromath.editor.textEditor.Direction;
27  
28  /***
29   * Command to delete DOMIntervalSet from document.
30   * 
31   * @author Tomáš Studva 17.8.2006
32   */
33  public class DeleteDOMIntervalSetCommand extends Command {
34          /***
35           * Set of DOM intervals to delete.
36           */
37          protected final DOMIntervalSet toDelete;
38  
39          /***
40           * XMLAccess instance.
41           */
42          protected final XMLAccess xmlAccess;
43  
44          /***
45           * Manager of caret.
46           */
47          protected final CaretManager caretManager;
48  
49          protected int caretMovement;
50  
51          protected final Direction direction;
52  
53          /***
54           * Constructor.
55           * 
56           * @param toDelete
57           *                DOMintervalSet to delete.
58           */
59          public DeleteDOMIntervalSetCommand(DOMIntervalSet toDelete,
60                          XMLAccess xmlAccess, CaretManager caretManager,
61                          Direction direction, int caretMovement) {
62                  this.toDelete = toDelete;
63                  this.xmlAccess = xmlAccess;
64                  this.caretManager = caretManager;
65                  assert (caretMovement > 0);
66                  this.caretMovement = caretMovement;
67                  this.direction = direction;
68          }
69  
70          /***
71           * Constructor.
72           * 
73           * @param toDelete
74           *                DOMintervalSet to delete.
75           */
76          public DeleteDOMIntervalSetCommand(DOMIntervalSet toDelete,
77                          XMLAccess xmlAccess, CaretManager caretManager) {
78                  this.toDelete = toDelete;
79                  this.xmlAccess = xmlAccess;
80                  this.caretManager = caretManager;
81                  this.caretMovement = 0;
82                  this.direction = Direction.Left;
83          }
84  
85          /***
86           * Tests if document schema allows interval to be deleted.
87           */
88          private boolean canDelete(DOMInterval interval) {
89                  List<Node> nodes = interval.getContents();
90                  for (Node node : nodes) {
91                          // TODO MOTO P1
92                  }
93                  return true;
94          }
95  
96          /*
97           * (non-Javadoc)
98           * 
99           * @see org.eclipse.gef.commands.Command#canExecute()
100          */
101         @Override
102         public boolean canExecute() {
103                 boolean result = true;
104                 for (DOMInterval interval : this.toDelete) {
105                         result = result & canDelete(interval);
106                 }
107                 return result;
108         }
109 
110         /*
111          * (non-Javadoc)
112          * 
113          * @see org.eclipse.gef.commands.Command#execute()
114          */
115         @Override
116         public void execute() {
117                 if (this.caretMovement > 0)
118                         this.caretManager.moveCaret(direction,
119                                         this.caretMovement);
120                 this.xmlAccess.getModifier().startModify();
121                 this.toDelete.deleteContents(true);
122                 try {
123                         this.xmlAccess.getModifier().endModify();
124                 } catch (ExportException e) {
125                         EuroMath.log(IStatus.ERROR, 0,
126                                         "Error in deletion in document.", e);
127                 }
128         }
129 
130         @Override
131         public void undo() {
132                 this.xmlAccess.getModifier().startModify();
133                 this.xmlAccess.getUndoManager().undo();
134                 try {
135                         this.xmlAccess.getModifier().endModify();
136                 } catch (ExportException ex) {
137                         EuroMath.log(IStatus.ERROR, 0, "Error when undoing.",
138                                         ex);
139                 }
140                 if (this.caretMovement > 0)
141                         this.caretManager.moveCaret(Direction
142                                         .getOpposite(direction),
143                                         this.caretMovement);
144         }
145 
146         @Override
147         public void redo() {
148                 if (this.caretMovement > 0)
149                         this.caretManager.moveCaret(direction,
150                                         this.caretMovement);
151                 this.xmlAccess.getModifier().startModify();
152                 this.xmlAccess.getUndoManager().redo();
153                 try {
154                         this.xmlAccess.getModifier().endModify();
155                 } catch (ExportException ex) {
156                         EuroMath.log(IStatus.ERROR, 0, "Error when undoing.",
157                                         ex);
158                 }
159         }
160 }