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
.LinkedHashSet
;
37 import java
.util
.List
;
40 import javax
.swing
.JFileChooser
;
41 import javax
.swing
.JOptionPane
;
43 import net
.sourceforge
.aprog
.context
.Context
;
44 import net
.sourceforge
.aprog
.subtitlesadjuster
.SubtitlesAdjusterActions
;
45 import net
.sourceforge
.aprog
.tools
.IllegalInstantiationException
;
46 import net
.sourceforge
.aprog
.tools
.Tools
;
47 import net
.sourceforge
.aprog
.xml
.XMLTools
;
49 import org
.w3c
.dom
.Node
;
50 import org
.xml
.sax
.InputSource
;
54 * @author codistmonk (creation 2010-07-03)
56 public final class MarkupsActions
{
59 * @throws IllegalInstantiationException To prevent instantiation
61 private MarkupsActions() {
62 throw new IllegalInstantiationException();
70 public static final void quit(final Context context
) {
71 if (confirm(context
)) {
77 * Returns {@code true} if the file doesn't need saving;
78 * otherwise, asks the user what to do.
83 * @return {@code true} if the caller can proceed
85 public static final boolean confirm(final Context context
) {
86 if ((Boolean
) context
.get(FILE_MODIFIED
)) {
87 switch (JOptionPane
.showConfirmDialog(null, "Save?", null, JOptionPane
.YES_NO_CANCEL_OPTION
)) {
88 case JOptionPane
.YES_OPTION
:
91 case JOptionPane
.NO_OPTION
:
93 case JOptionPane
.CANCEL_OPTION
:
96 Tools
.getLoggerForThisMethod().log(Level
.WARNING
, "Unhandled option");
110 public static final void newFile(final Context context
) {
111 if (confirm(context
)) {
112 context
.set(FILE
, null);
122 public static final void open(final Context context
) {
123 if (!confirm(context
)) {
127 final JFileChooser fileChooser
= new JFileChooser();
129 if (JFileChooser
.APPROVE_OPTION
== fileChooser
.showOpenDialog((Component
) context
.get(MAIN_FRAME
)) &&
130 fileChooser
.getSelectedFile() != null) {
131 open(context
, fileChooser
.getSelectedFile());
144 public static final void open(final Context context
, final File file
) {
145 context
.set(DOM
, XMLTools
.parse(new InputSource(file
.getAbsolutePath())));
146 context
.set(FILE
, file
);
155 public static final void save(final Context context
) {
156 final File file
= context
.get(FILE
);
171 public static final void saveAs(final Context context
) {
172 final JFileChooser fileChooser
= new JFileChooser();
174 if (JFileChooser
.APPROVE_OPTION
== fileChooser
.showSaveDialog((Component
) context
.get(MAIN_FRAME
)) &&
175 fileChooser
.getSelectedFile() != null) {
176 save(context
, fileChooser
.getSelectedFile());
189 private static final void save(final Context context
, final File file
) {
192 (Node
) context
.get(DOM
),
193 new FileOutputStream(file
),
195 context
.set(FILE
, file
);
196 context
.set(FILE_MODIFIED
, false);
197 } catch (final FileNotFoundException exception
) {
198 SubtitlesAdjusterActions
.showErrorMessage(context
, exception
);
207 public static final void undo(final Context context
) {
208 Tools
.debugPrint("TODO");
210 SubtitlesAdjusterActions
.showTODOMessage(context
);
218 public static final void redo(final Context context
) {
219 Tools
.debugPrint("TODO");
221 SubtitlesAdjusterActions
.showTODOMessage(context
);
229 public static final void copy(final Context context
) {
230 Tools
.debugPrint("TODO");
232 SubtitlesAdjusterActions
.showTODOMessage(context
);
240 public static final void cut(final Context context
) {
241 Tools
.debugPrint("TODO");
243 SubtitlesAdjusterActions
.showTODOMessage(context
);
251 public static final void paste(final Context context
) {
252 Tools
.debugPrint("TODO");
254 SubtitlesAdjusterActions
.showTODOMessage(context
);
262 public static final void appendNewNode(final Context context
) {
263 final Node node
= context
.get(SELECTED_NODE
);
264 final Set
<String
> options
= new LinkedHashSet
<String
>();
266 switch (node
.getNodeType()) {
267 case Node
.DOCUMENT_NODE
:
268 options
.add("Element");
270 case Node
.ELEMENT_NODE
:
271 options
.add("Element");
272 options
.add("Attribute");
274 options
.add("Comment");
275 options
.add("CDATA");
281 final String nodeType
= (String
) JOptionPane
.showInputDialog(
285 JOptionPane
.PLAIN_MESSAGE
,
291 if ("Element".equals(nodeType
)) {
292 getOrCreateNode(node
, "new-element");
293 } else if ("Attribute".equals(nodeType
)) {
294 getOrCreateNode(node
, "@new-attribute");
295 } else if ("Text".equals(nodeType
)) {
296 node
.appendChild(getOwnerDocument(node
).createTextNode(""));
297 } else if ("Comment".equals(nodeType
)) {
298 node
.appendChild(getOwnerDocument(node
).createComment(""));
299 } else if ("CDATA".equals(nodeType
)) {
300 node
.appendChild(getOwnerDocument(node
).createCDATASection(""));
309 public static final void moveUp(final Context context
) {
310 final Node node
= context
.get(SELECTED_NODE
);
311 final Node parent
= getNode(node
, "..");
313 context
.set(SELECTED_NODE
, null);
316 final List
<Node
> siblings
= MarkupsTools
.getAttributeChildren(parent
);
317 final int index
= siblings
.indexOf(node
);
320 Tools
.debugPrint("TODO");
327 final List
<Node
> siblings
= MarkupsTools
.getNonattributeChildren(parent
);
328 final int index
= siblings
.indexOf(node
);
331 Tools
.debugPrint(siblings
);
332 throw new IllegalStateException("Orphan node: " + node
);
336 parent
.insertBefore(node
, siblings
.get(index
- 1));
342 context
.set(SELECTED_NODE
, node
);
350 public static final void moveDown(final Context context
) {
351 final Node node
= context
.get(SELECTED_NODE
);
352 final Node parent
= getNode(node
, "..");
355 final List
<Node
> siblings
= MarkupsTools
.getAttributeChildren(parent
);
356 final int index
= siblings
.indexOf(node
);
359 if (index
== siblings
.size() - 2) {
360 parent
.getAttributes().removeNamedItem(node
.getNodeName());
361 parent
.getAttributes().setNamedItem(node
);
362 } else if (index
< siblings
.size() - 2) {
363 Tools
.debugPrint("TODO");
371 final List
<Node
> siblings
= MarkupsTools
.getNonattributeChildren(parent
);
372 final int index
= siblings
.indexOf(node
);
375 Tools
.debugPrint(siblings
);
376 throw new IllegalStateException("Orphan node: " + node
);
379 if (index
== siblings
.size() - 2) {
380 parent
.appendChild(node
);
382 parent
.insertBefore(node
, siblings
.get(index
+ 2));
388 context
.set(SELECTED_NODE
, node
);
397 public static final void evaluateXPathExpression(final Context context
) {
398 final Node node
= context
.get(SELECTED_NODE
);
401 context
.set(XPATH_RESULT
, XMLTools
.getNodes(node
, (String
) context
.get(XPATH_EXPRESSION
)));
402 context
.set(XPATH_ERROR
, null);
403 } catch (final Exception exception
) {
404 context
.set(XPATH_RESULT
, null);
405 context
.set(XPATH_ERROR
, exception
);
415 public static final void evaluateQuasiXPathExpression(final Context context
) {
417 XMLTools
.getOrCreateNode((Node
) context
.get(SELECTED_NODE
), (String
) context
.get(QUASI_XPATH_EXPRESSION
));
419 context
.set(QUASI_XPATH_ERROR
, null);
420 } catch (final Exception exception
) {
421 context
.set(QUASI_XPATH_ERROR
, exception
);