4 * Copyright 2010 Codist Monk.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package net
.sourceforge
.aprog
.markups
;
27 import static net
.sourceforge
.aprog
.markups
.MarkupsConstants
.Variables
.*;
28 import static net
.sourceforge
.aprog
.xml
.XMLTools
.*;
30 import java
.awt
.Component
;
31 import java
.io
.FileNotFoundException
;
32 import java
.util
.logging
.Level
;
35 import java
.io
.FileOutputStream
;
36 import java
.util
.List
;
38 import javax
.swing
.JFileChooser
;
39 import javax
.swing
.JOptionPane
;
41 import net
.sourceforge
.aprog
.context
.Context
;
42 import net
.sourceforge
.aprog
.subtitlesadjuster
.SubtitlesAdjusterActions
;
43 import net
.sourceforge
.aprog
.tools
.IllegalInstantiationException
;
44 import net
.sourceforge
.aprog
.tools
.Tools
;
45 import net
.sourceforge
.aprog
.xml
.XMLTools
;
47 import org
.w3c
.dom
.Node
;
48 import org
.xml
.sax
.InputSource
;
52 * @author codistmonk (creation 2010-07-03)
54 public final class MarkupsActions
{
57 * @throws IllegalInstantiationException To prevent instantiation
59 private MarkupsActions() {
60 throw new IllegalInstantiationException();
68 public static final void quit(final Context context
) {
69 if (confirm(context
)) {
75 * Returns {@code true} if the file doesn't need saving;
76 * otherwise, asks the user what to do.
81 * @return {@code true} if the caller can proceed
83 public static final boolean confirm(final Context context
) {
84 if ((Boolean
) context
.get(FILE_MODIFIED
)) {
85 switch (JOptionPane
.showConfirmDialog(null, "Save?", null, JOptionPane
.YES_NO_CANCEL_OPTION
)) {
86 case JOptionPane
.YES_OPTION
:
89 case JOptionPane
.NO_OPTION
:
91 case JOptionPane
.CANCEL_OPTION
:
94 Tools
.getLoggerForThisMethod().log(Level
.WARNING
, "Unhandled option");
108 public static final void newFile(final Context context
) {
109 if (confirm(context
)) {
110 context
.set(FILE
, null);
120 public static final void open(final Context context
) {
121 if (!confirm(context
)) {
125 final JFileChooser fileChooser
= new JFileChooser();
127 if (JFileChooser
.APPROVE_OPTION
== fileChooser
.showOpenDialog((Component
) context
.get(MAIN_FRAME
)) &&
128 fileChooser
.getSelectedFile() != null) {
129 open(context
, fileChooser
.getSelectedFile());
142 public static final void open(final Context context
, final File file
) {
143 context
.set(DOM
, XMLTools
.parse(new InputSource(file
.getAbsolutePath())));
144 context
.set(FILE
, file
);
153 public static final void save(final Context context
) {
154 final File file
= context
.get(FILE
);
169 public static final void saveAs(final Context context
) {
170 final JFileChooser fileChooser
= new JFileChooser();
172 if (JFileChooser
.APPROVE_OPTION
== fileChooser
.showSaveDialog((Component
) context
.get(MAIN_FRAME
)) &&
173 fileChooser
.getSelectedFile() != null) {
174 save(context
, fileChooser
.getSelectedFile());
187 private static final void save(final Context context
, final File file
) {
190 (Node
) context
.get(DOM
),
191 new FileOutputStream(file
),
193 context
.set(FILE
, file
);
194 context
.set(FILE_MODIFIED
, false);
195 } catch (final FileNotFoundException exception
) {
196 SubtitlesAdjusterActions
.showErrorMessage(context
, exception
);
205 public static final void undo(final Context context
) {
206 Tools
.debugPrint("TODO");
208 SubtitlesAdjusterActions
.showTODOMessage(context
);
216 public static final void redo(final Context context
) {
217 Tools
.debugPrint("TODO");
219 SubtitlesAdjusterActions
.showTODOMessage(context
);
227 public static final void copy(final Context context
) {
228 Tools
.debugPrint("TODO");
230 SubtitlesAdjusterActions
.showTODOMessage(context
);
238 public static final void cut(final Context context
) {
239 Tools
.debugPrint("TODO");
241 SubtitlesAdjusterActions
.showTODOMessage(context
);
249 public static final void paste(final Context context
) {
250 Tools
.debugPrint("TODO");
252 SubtitlesAdjusterActions
.showTODOMessage(context
);
260 public static final void appendNewNode(final Context context
) {
261 Tools
.debugPrint("TODO");
263 SubtitlesAdjusterActions
.showTODOMessage(context
);
271 public static final void moveUp(final Context context
) {
272 final Node node
= context
.get(SELECTED_NODE
);
273 final Node parent
= getNode(node
, "..");
275 context
.set(SELECTED_NODE
, null);
278 final List
<Node
> siblings
= MarkupsTools
.getAttributeChildren(parent
);
279 final int index
= siblings
.indexOf(node
);
282 Tools
.debugPrint("TODO");
289 final List
<Node
> siblings
= MarkupsTools
.getNonattributeChildren(parent
);
290 final int index
= siblings
.indexOf(node
);
293 Tools
.debugPrint(siblings
);
294 throw new IllegalStateException("Orphan node: " + node
);
298 parent
.insertBefore(node
, siblings
.get(index
- 1));
304 context
.set(SELECTED_NODE
, node
);
312 public static final void moveDown(final Context context
) {
313 final Node node
= context
.get(SELECTED_NODE
);
314 final Node parent
= getNode(node
, "..");
317 final List
<Node
> siblings
= MarkupsTools
.getAttributeChildren(parent
);
318 final int index
= siblings
.indexOf(node
);
321 if (index
== siblings
.size() - 2) {
322 parent
.getAttributes().removeNamedItem(node
.getNodeName());
323 parent
.getAttributes().setNamedItem(node
);
324 } else if (index
< siblings
.size() - 2) {
325 Tools
.debugPrint("TODO");
333 final List
<Node
> siblings
= MarkupsTools
.getNonattributeChildren(parent
);
334 final int index
= siblings
.indexOf(node
);
337 Tools
.debugPrint(siblings
);
338 throw new IllegalStateException("Orphan node: " + node
);
341 if (index
== siblings
.size() - 2) {
342 parent
.appendChild(node
);
344 parent
.insertBefore(node
, siblings
.get(index
+ 2));
350 context
.set(SELECTED_NODE
, node
);
359 public static final void evaluateXPathExpression(final Context context
) {
360 final Node node
= context
.get(SELECTED_NODE
);
363 context
.set(XPATH_RESULT
, XMLTools
.getNodes(node
, (String
) context
.get(XPATH_EXPRESSION
)));
364 context
.set(XPATH_ERROR
, null);
365 } catch (final Exception exception
) {
366 context
.set(XPATH_RESULT
, null);
367 context
.set(XPATH_ERROR
, exception
);
377 public static final void evaluateQuasiXPathExpression(final Context context
) {
379 XMLTools
.getOrCreateNode((Node
) context
.get(SELECTED_NODE
), (String
) context
.get(QUASI_XPATH_EXPRESSION
));
381 context
.set(QUASI_XPATH_ERROR
, null);
382 } catch (final Exception exception
) {
383 context
.set(QUASI_XPATH_ERROR
, exception
);