[Markups]
[aprog.git] / Markups / src / net / sourceforge / aprog / markups / MarkupsActions.java
blob2c695d2c03d9f8c493c4b631b93d892054440e92
1 /*
2 * The MIT License
3 *
4 * Copyright 2010 Codist Monk.
5 *
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
22 * THE SOFTWARE.
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;
34 import java.io.File;
35 import java.io.FileOutputStream;
36 import java.util.LinkedHashSet;
37 import java.util.List;
38 import java.util.Set;
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;
52 /**
54 * @author codistmonk (creation 2010-07-03)
56 public final class MarkupsActions {
58 /**
59 * @throws IllegalInstantiationException To prevent instantiation
61 private MarkupsActions() {
62 throw new IllegalInstantiationException();
65 /**
67 * @param context
68 * <br>Unused
70 public static final void quit(final Context context) {
71 if (confirm(context)) {
72 System.exit(0);
76 /**
77 * Returns {@code true} if the file doesn't need saving;
78 * otherwise, asks the user what to do.
80 * @param context
81 * <br>Not null
82 * <br>Input-output
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:
89 save(context);
90 break;
91 case JOptionPane.NO_OPTION:
92 break;
93 case JOptionPane.CANCEL_OPTION:
94 return false;
95 default:
96 Tools.getLoggerForThisMethod().log(Level.WARNING, "Unhandled option");
97 break;
101 return true;
106 * @param context
107 * <br>Not null
108 * <br>Input-output
110 public static final void newFile(final Context context) {
111 if (confirm(context)) {
112 context.set(FILE, null);
118 * @param context
119 * <br>Not null
120 * <br>Input-output
122 public static final void open(final Context context) {
123 if (!confirm(context)) {
124 return;
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());
137 * @param context
138 * <br>Not null
139 * <br>Input-output
140 * @param file
141 * <br>Not null
142 * <br>Shared
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);
151 * @param context
152 * <br>Not null
153 * <br>Input-output
155 public static final void save(final Context context) {
156 final File file = context.get(FILE);
158 if (file != null) {
159 save(context, file);
160 } else {
161 saveAs(context);
167 * @param context
168 * <br>Not null
169 * <br>Input-output
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());
182 * @param context
183 * <br>Not null
184 * <br>Input-output
185 * @param file
186 * <br>Not null
187 * <br>Shared
189 private static final void save(final Context context, final File file) {
190 try {
191 XMLTools.write(
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);
204 * @param context
205 * <br>Not null
207 public static final void undo(final Context context) {
208 Tools.debugPrint("TODO");
210 SubtitlesAdjusterActions.showTODOMessage(context);
215 * @param context
216 * <br>Not null
218 public static final void redo(final Context context) {
219 Tools.debugPrint("TODO");
221 SubtitlesAdjusterActions.showTODOMessage(context);
226 * @param context
227 * <br>Not null
229 public static final void copy(final Context context) {
230 Tools.debugPrint("TODO");
232 SubtitlesAdjusterActions.showTODOMessage(context);
237 * @param context
238 * <br>Not null
240 public static final void cut(final Context context) {
241 Tools.debugPrint("TODO");
243 SubtitlesAdjusterActions.showTODOMessage(context);
248 * @param context
249 * <br>Not null
251 public static final void paste(final Context context) {
252 Tools.debugPrint("TODO");
254 SubtitlesAdjusterActions.showTODOMessage(context);
259 * @param context
260 * <br>Not null
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");
269 break;
270 case Node.ELEMENT_NODE:
271 options.add("Element");
272 options.add("Attribute");
273 options.add("Text");
274 options.add("Comment");
275 options.add("CDATA");
276 break;
277 default:
278 return;
281 final String nodeType = (String) JOptionPane.showInputDialog(
282 null,
283 "Node type",
284 "Add node",
285 JOptionPane.PLAIN_MESSAGE,
286 null,
287 options.toArray(),
288 "Element");
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(""));
306 * @param context
307 * <br>Not null
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);
319 if (index >= 0) {
320 Tools.debugPrint("TODO");
322 return;
327 final List<Node> siblings = MarkupsTools.getNonattributeChildren(parent);
328 final int index = siblings.indexOf(node);
330 if (index < 0) {
331 Tools.debugPrint(siblings);
332 throw new IllegalStateException("Orphan node: " + node);
335 if (index > 0) {
336 parent.insertBefore(node, siblings.get(index - 1));
340 parent.normalize();
342 context.set(SELECTED_NODE, node);
347 * @param context
348 * <br>Not null
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);
358 if (index >= 0) {
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");
366 return;
371 final List<Node> siblings = MarkupsTools.getNonattributeChildren(parent);
372 final int index = siblings.indexOf(node);
374 if (index < 0) {
375 Tools.debugPrint(siblings);
376 throw new IllegalStateException("Orphan node: " + node);
379 if (index == siblings.size() - 2) {
380 parent.appendChild(node);
381 } else {
382 parent.insertBefore(node, siblings.get(index + 2));
386 parent.normalize();
388 context.set(SELECTED_NODE, node);
393 * @param context
394 * <br>Not null
395 * <br>Input-output
397 public static final void evaluateXPathExpression(final Context context) {
398 final Node node = context.get(SELECTED_NODE);
400 try {
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);
411 * @param context
412 * <br>Not null
413 * <br>Input-output
415 public static final void evaluateQuasiXPathExpression(final Context context) {
416 try {
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);