[Markups]
[aprog.git] / Markups / src / net / sourceforge / aprog / markups / MarkupsActions.java
blob4ea1db9bc91bd54eb44227933513dcc4b2244e8a
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.*;
29 import java.awt.Component;
30 import java.io.FileNotFoundException;
31 import java.util.logging.Level;
33 import java.io.File;
34 import java.io.FileOutputStream;
36 import javax.swing.JFileChooser;
37 import javax.swing.JOptionPane;
38 import javax.swing.tree.DefaultMutableTreeNode;
39 import javax.swing.tree.TreeModel;
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 final File file = fileChooser.getSelectedFile();
131 context.set(DOM, XMLTools.parse(new InputSource(file.getAbsolutePath())));
132 context.set(FILE, file);
138 * @param context
139 * <br>Not null
140 * <br>Input-output
142 public static final void save(final Context context) {
143 final File file = context.get(FILE);
145 if (file != null) {
146 save(context, file);
147 } else {
148 saveAs(context);
154 * @param context
155 * <br>Not null
156 * <br>Input-output
158 public static final void saveAs(final Context context) {
159 final JFileChooser fileChooser = new JFileChooser();
161 if (JFileChooser.APPROVE_OPTION == fileChooser.showSaveDialog((Component) context.get(MAIN_FRAME)) &&
162 fileChooser.getSelectedFile() != null) {
163 save(context, fileChooser.getSelectedFile());
169 * @param context
170 * <br>Not null
171 * <br>Input-output
172 * @param file
173 * <br>Not null
174 * <br>Shared
176 private static final void save(final Context context, final File file) {
177 try {
178 final TreeModel treeModel = (TreeModel) context.get(TREE_MODEL);
180 XMLTools.write(
181 (Node) ((DefaultMutableTreeNode) treeModel.getRoot()).getUserObject(),
182 new FileOutputStream(file),
184 context.set(FILE, file);
185 context.set(FILE_MODIFIED, false);
186 } catch (final FileNotFoundException exception) {
187 SubtitlesAdjusterActions.showErrorMessage(context, exception);
193 * @param context
194 * <br>Not null
196 public static final void undo(final Context context) {
197 Tools.debugPrint("TODO");
199 SubtitlesAdjusterActions.showTODOMessage(context);
204 * @param context
205 * <br>Not null
207 public static final void redo(final Context context) {
208 Tools.debugPrint("TODO");
210 SubtitlesAdjusterActions.showTODOMessage(context);
215 * @param context
216 * <br>Not null
218 public static final void copy(final Context context) {
219 Tools.debugPrint("TODO");
221 SubtitlesAdjusterActions.showTODOMessage(context);
226 * @param context
227 * <br>Not null
229 public static final void cut(final Context context) {
230 Tools.debugPrint("TODO");
232 SubtitlesAdjusterActions.showTODOMessage(context);
237 * @param context
238 * <br>Not null
240 public static final void paste(final Context context) {
241 Tools.debugPrint("TODO");
243 SubtitlesAdjusterActions.showTODOMessage(context);
248 * @param context
249 * <br>Not null
251 public static final void appendNewNode(final Context context) {
252 Tools.debugPrint("TODO");
254 SubtitlesAdjusterActions.showTODOMessage(context);
259 * @param context
260 * <br>Not null
262 public static final void moveUp(final Context context) {
263 Tools.debugPrint("TODO");
265 SubtitlesAdjusterActions.showTODOMessage(context);
270 * @param context
271 * <br>Not null
273 public static final void moveDown(final Context context) {
274 Tools.debugPrint("TODO");
276 SubtitlesAdjusterActions.showTODOMessage(context);