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
.*;
29 import java
.awt
.Component
;
30 import java
.io
.FileNotFoundException
;
31 import java
.util
.logging
.Level
;
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
;
52 * @author codistmonk (creation 2010-07-03)
54 public final class MarkupsActions
{
57 * @throws IllegalInstantiationException To prevent instantiation
59 private MarkupsActions() {
60 throw new IllegalInstantiationException();
68 public static final void quit(final Context context
) {
69 if (confirm(context
)) {
75 * Returns {@code true} if the file doesn't need saving;
76 * otherwise, asks the user what to do.
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
:
89 case JOptionPane
.NO_OPTION
:
91 case JOptionPane
.CANCEL_OPTION
:
94 Tools
.getLoggerForThisMethod().log(Level
.WARNING
, "Unhandled option");
108 public static final void newFile(final Context context
) {
109 if (confirm(context
)) {
110 context
.set(FILE
, null);
120 public static final void open(final Context context
) {
121 if (!confirm(context
)) {
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
);
142 public static final void save(final Context context
) {
143 final File file
= context
.get(FILE
);
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());
176 private static final void save(final Context context
, final File file
) {
178 final TreeModel treeModel
= (TreeModel
) context
.get(TREE_MODEL
);
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
);
196 public static final void undo(final Context context
) {
197 Tools
.debugPrint("TODO");
199 SubtitlesAdjusterActions
.showTODOMessage(context
);
207 public static final void redo(final Context context
) {
208 Tools
.debugPrint("TODO");
210 SubtitlesAdjusterActions
.showTODOMessage(context
);
218 public static final void copy(final Context context
) {
219 Tools
.debugPrint("TODO");
221 SubtitlesAdjusterActions
.showTODOMessage(context
);
229 public static final void cut(final Context context
) {
230 Tools
.debugPrint("TODO");
232 SubtitlesAdjusterActions
.showTODOMessage(context
);
240 public static final void paste(final Context context
) {
241 Tools
.debugPrint("TODO");
243 SubtitlesAdjusterActions
.showTODOMessage(context
);
251 public static final void appendNewNode(final Context context
) {
252 Tools
.debugPrint("TODO");
254 SubtitlesAdjusterActions
.showTODOMessage(context
);
262 public static final void moveUp(final Context context
) {
263 Tools
.debugPrint("TODO");
265 SubtitlesAdjusterActions
.showTODOMessage(context
);
273 public static final void moveDown(final Context context
) {
274 Tools
.debugPrint("TODO");
276 SubtitlesAdjusterActions
.showTODOMessage(context
);