[Markups]
[aprog.git] / Markups / src / net / sourceforge / aprog / markups / MarkupsActions.java
blob9ba1b8b86d33f3df64f269554f046637ea755beb
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.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;
50 /**
52 * @author codistmonk (creation 2010-07-03)
54 public final class MarkupsActions {
56 /**
57 * @throws IllegalInstantiationException To prevent instantiation
59 private MarkupsActions() {
60 throw new IllegalInstantiationException();
63 /**
65 * @param context
66 * <br>Unused
68 public static final void quit(final Context context) {
69 if (confirm(context)) {
70 System.exit(0);
74 /**
75 * Returns {@code true} if the file doesn't need saving;
76 * otherwise, asks the user what to do.
78 * @param context
79 * <br>Not null
80 * <br>Input-output
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:
87 save(context);
88 break;
89 case JOptionPane.NO_OPTION:
90 break;
91 case JOptionPane.CANCEL_OPTION:
92 return false;
93 default:
94 Tools.getLoggerForThisMethod().log(Level.WARNING, "Unhandled option");
95 break;
99 return true;
104 * @param context
105 * <br>Not null
106 * <br>Input-output
108 public static final void newFile(final Context context) {
109 if (confirm(context)) {
110 context.set(FILE, null);
116 * @param context
117 * <br>Not null
118 * <br>Input-output
120 public static final void open(final Context context) {
121 if (!confirm(context)) {
122 return;
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());
135 * @param context
136 * <br>Not null
137 * <br>Input-output
138 * @param file
139 * <br>Not null
140 * <br>Shared
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);
149 * @param context
150 * <br>Not null
151 * <br>Input-output
153 public static final void save(final Context context) {
154 final File file = context.get(FILE);
156 if (file != null) {
157 save(context, file);
158 } else {
159 saveAs(context);
165 * @param context
166 * <br>Not null
167 * <br>Input-output
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());
180 * @param context
181 * <br>Not null
182 * <br>Input-output
183 * @param file
184 * <br>Not null
185 * <br>Shared
187 private static final void save(final Context context, final File file) {
188 try {
189 XMLTools.write(
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);
202 * @param context
203 * <br>Not null
205 public static final void undo(final Context context) {
206 Tools.debugPrint("TODO");
208 SubtitlesAdjusterActions.showTODOMessage(context);
213 * @param context
214 * <br>Not null
216 public static final void redo(final Context context) {
217 Tools.debugPrint("TODO");
219 SubtitlesAdjusterActions.showTODOMessage(context);
224 * @param context
225 * <br>Not null
227 public static final void copy(final Context context) {
228 Tools.debugPrint("TODO");
230 SubtitlesAdjusterActions.showTODOMessage(context);
235 * @param context
236 * <br>Not null
238 public static final void cut(final Context context) {
239 Tools.debugPrint("TODO");
241 SubtitlesAdjusterActions.showTODOMessage(context);
246 * @param context
247 * <br>Not null
249 public static final void paste(final Context context) {
250 Tools.debugPrint("TODO");
252 SubtitlesAdjusterActions.showTODOMessage(context);
257 * @param context
258 * <br>Not null
260 public static final void appendNewNode(final Context context) {
261 Tools.debugPrint("TODO");
263 SubtitlesAdjusterActions.showTODOMessage(context);
268 * @param context
269 * <br>Not null
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);
281 if (index >= 0) {
282 Tools.debugPrint("TODO");
284 return;
289 final List<Node> siblings = MarkupsTools.getNonattributeChildren(parent);
290 final int index = siblings.indexOf(node);
292 if (index < 0) {
293 Tools.debugPrint(siblings);
294 throw new IllegalStateException("Orphan node: " + node);
297 if (index > 0) {
298 parent.insertBefore(node, siblings.get(index - 1));
302 parent.normalize();
304 context.set(SELECTED_NODE, node);
309 * @param context
310 * <br>Not null
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);
320 if (index >= 0) {
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");
328 return;
333 final List<Node> siblings = MarkupsTools.getNonattributeChildren(parent);
334 final int index = siblings.indexOf(node);
336 if (index < 0) {
337 Tools.debugPrint(siblings);
338 throw new IllegalStateException("Orphan node: " + node);
341 if (index == siblings.size() - 2) {
342 parent.appendChild(node);
343 } else {
344 parent.insertBefore(node, siblings.get(index + 2));
348 parent.normalize();
350 context.set(SELECTED_NODE, node);
355 * @param context
356 * <br>Not null
357 * <br>Input-output
359 public static final void evaluateXPathExpression(final Context context) {
360 final Node node = context.get(SELECTED_NODE);
362 try {
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);
373 * @param context
374 * <br>Not null
375 * <br>Input-output
377 public static final void evaluateQuasiXPathExpression(final Context context) {
378 try {
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);