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
.Constants
.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
.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
;
51 * @author codistmonk (creation 2010-07-03)
53 public final class Actions
{
56 * @throws IllegalInstantiationException To prevent instantiation
59 throw new IllegalInstantiationException();
67 public static final void quit(final Context context
) {
68 if (confirm(context
)) {
74 * Returns {@code true} if the file doesn't need saving;
75 * otherwise, asks the user what to do.
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
:
88 case JOptionPane
.NO_OPTION
:
90 case JOptionPane
.CANCEL_OPTION
:
93 Tools
.getLoggerForThisMethod().log(Level
.WARNING
, "Unhandled option");
107 public static final void newFile(final Context context
) {
108 if (confirm(context
)) {
109 context
.set(FILE
, null);
119 public static final void open(final Context context
) {
120 if (!confirm(context
)) {
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
);
140 public static final void save(final Context context
) {
141 final File file
= context
.get(FILE
);
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());
173 private static final void save(final Context context
, final File file
) {
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
);
191 public static final void undo(final Context context
) {
192 Tools
.debugPrint("TODO");
194 net
.sourceforge
.aprog
.subtitlesadjuster
.Actions
.showTODOMessage(context
);
202 public static final void redo(final Context context
) {
203 Tools
.debugPrint("TODO");
205 net
.sourceforge
.aprog
.subtitlesadjuster
.Actions
.showTODOMessage(context
);
213 public static final void copy(final Context context
) {
214 Tools
.debugPrint("TODO");
216 net
.sourceforge
.aprog
.subtitlesadjuster
.Actions
.showTODOMessage(context
);
224 public static final void cut(final Context context
) {
225 Tools
.debugPrint("TODO");
227 net
.sourceforge
.aprog
.subtitlesadjuster
.Actions
.showTODOMessage(context
);
235 public static final void paste(final Context context
) {
236 Tools
.debugPrint("TODO");
238 net
.sourceforge
.aprog
.subtitlesadjuster
.Actions
.showTODOMessage(context
);
246 public static final void tree(final Context context
) {
247 context
.set(VIEW_MODE
, Constants
.VIEW_MODE_TREE
);
255 public static final void text(final Context context
) {
256 context
.set(VIEW_MODE
, Constants
.VIEW_MODE_TEXT
);