[Markups]
[aprog.git] / Markups / src / net / sourceforge / aprog / markups / Actions.java
blobf27f11c4267188564e271b799d8d8fde77c34999
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.Constants.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.tools.IllegalInstantiationException;
43 import net.sourceforge.aprog.tools.Tools;
44 import net.sourceforge.aprog.xml.XMLTools;
46 import org.w3c.dom.Node;
47 import org.xml.sax.InputSource;
49 /**
51 * @author codistmonk (creation 2010-07-03)
53 public final class Actions {
55 /**
56 * @throws IllegalInstantiationException To prevent instantiation
58 private Actions() {
59 throw new IllegalInstantiationException();
62 /**
64 * @param context
65 * <br>Unused
67 public static final void quit(final Context context) {
68 if (confirm(context)) {
69 System.exit(0);
73 /**
74 * Returns {@code true} if the file doesn't need saving;
75 * otherwise, asks the user what to do.
77 * @param context
78 * <br>Not null
79 * <br>Input-output
80 * @return {@code true} if the caller can proceed
82 public static final boolean confirm(final Context context) {
83 if ((Boolean) context.get(FILE_MODIFIED)) {
84 switch (JOptionPane.showConfirmDialog(null, "Save?", null, JOptionPane.YES_NO_CANCEL_OPTION)) {
85 case JOptionPane.YES_OPTION:
86 save(context);
87 break;
88 case JOptionPane.NO_OPTION:
89 break;
90 case JOptionPane.CANCEL_OPTION:
91 return false;
92 default:
93 Tools.getLoggerForThisMethod().log(Level.WARNING, "Unhandled option");
94 break;
98 return true;
103 * @param context
104 * <br>Not null
105 * <br>Input-output
107 public static final void newFile(final Context context) {
108 if (confirm(context)) {
109 context.set(FILE, null);
115 * @param context
116 * <br>Not null
117 * <br>Input-output
119 public static final void open(final Context context) {
120 if (!confirm(context)) {
121 return;
124 final JFileChooser fileChooser = new JFileChooser();
126 if (JFileChooser.APPROVE_OPTION == fileChooser.showOpenDialog((Component) context.get(MAIN_FRAME)) && fileChooser.getSelectedFile() != null) {
127 final File file = fileChooser.getSelectedFile();
129 context.set(DOM, XMLTools.parse(new InputSource(file.getAbsolutePath())));
130 context.set(FILE, file);
136 * @param context
137 * <br>Not null
138 * <br>Input-output
140 public static final void save(final Context context) {
141 final File file = context.get(FILE);
143 if (file != null) {
144 save(context, file);
145 } else {
146 saveAs(context);
152 * @param context
153 * <br>Not null
154 * <br>Input-output
156 public static final void saveAs(final Context context) {
157 final JFileChooser fileChooser = new JFileChooser();
159 if (JFileChooser.APPROVE_OPTION == fileChooser.showSaveDialog((Component) context.get(MAIN_FRAME)) && fileChooser.getSelectedFile() != null) {
160 save(context, fileChooser.getSelectedFile());
166 * @param context
167 * <br>Not null
168 * <br>Input-output
169 * @param file
170 * <br>Not null
171 * <br>Shared
173 private static final void save(final Context context, final File file) {
174 try {
175 XMLTools.write(
176 (Node) ((DefaultMutableTreeNode) ((TreeModel) context.get(TREE_MODEL)).getRoot()).getUserObject(),
177 new FileOutputStream(file),
179 context.set(FILE, file);
180 context.set(FILE_MODIFIED, false);
181 } catch (final FileNotFoundException exception) {
182 net.sourceforge.aprog.subtitlesadjuster.Actions.showErrorMessage(context, exception);
188 * @param context
189 * <br>Not null
191 public static final void undo(final Context context) {
192 Tools.debugPrint("TODO");
194 net.sourceforge.aprog.subtitlesadjuster.Actions.showTODOMessage(context);
199 * @param context
200 * <br>Not null
202 public static final void redo(final Context context) {
203 Tools.debugPrint("TODO");
205 net.sourceforge.aprog.subtitlesadjuster.Actions.showTODOMessage(context);
210 * @param context
211 * <br>Not null
213 public static final void copy(final Context context) {
214 Tools.debugPrint("TODO");
216 net.sourceforge.aprog.subtitlesadjuster.Actions.showTODOMessage(context);
221 * @param context
222 * <br>Not null
224 public static final void cut(final Context context) {
225 Tools.debugPrint("TODO");
227 net.sourceforge.aprog.subtitlesadjuster.Actions.showTODOMessage(context);
232 * @param context
233 * <br>Not null
235 public static final void paste(final Context context) {
236 Tools.debugPrint("TODO");
238 net.sourceforge.aprog.subtitlesadjuster.Actions.showTODOMessage(context);
243 * @param context
244 * <br>Not null
246 public static final void tree(final Context context) {
247 context.set(VIEW_MODE, Constants.VIEW_MODE_TREE);
252 * @param context
253 * <br>Not null
255 public static final void text(final Context context) {
256 context.set(VIEW_MODE, Constants.VIEW_MODE_TEXT);