[SubtitlesAdjuster]
[aprog.git] / SubtitlesAdjuster / src / net / sourceforge / aprog / subtitlesadjuster / SubtitlesAdjusterActions.java
blobff0ae36e006de3c8c6ed6aeb9ceb1a24cf87d083
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.subtitlesadjuster;
27 import static net.sourceforge.aprog.i18n.Messages.*;
28 import static net.sourceforge.aprog.subtitlesadjuster.SubtitlesAdjusterComponents.*;
29 import static net.sourceforge.aprog.subtitlesadjuster.SubtitlesAdjusterConstants.Variables.*;
30 import static net.sourceforge.aprog.tools.Tools.ignore;
32 import java.awt.Component;
33 import java.io.File;
35 import javax.swing.JFileChooser;
36 import javax.swing.JFrame;
37 import javax.swing.JOptionPane;
38 import javax.swing.filechooser.FileFilter;
40 import net.sourceforge.aprog.context.Context;
41 import net.sourceforge.aprog.swing.SwingTools;
42 import net.sourceforge.aprog.tools.Tools;
44 /**
45 * This class defines all the operations that can be executed on the application context object.
47 * @author codistmonk (creation 2010-06-27)
49 public final class SubtitlesAdjusterActions {
51 /**
52 * Private default constructor to prevent instantiation.
54 private SubtitlesAdjusterActions() {
55 // Do nothing
58 /**
60 * @param context
61 * <br>Not null
63 public static final void showAboutDialog(final Context context) {
64 JOptionPane.showMessageDialog(
65 (Component) context.get(MAIN_FRAME),
66 context.get(APPLICATION_NAME) + "\n" +
67 context.get(APPLICATION_VERSION) + "\n" +
68 context.get(APPLICATION_COPYRIGHT),
69 translate("About $0", context.get(APPLICATION_NAME)),
70 JOptionPane.INFORMATION_MESSAGE);
73 /**
75 * @param context
76 * <br>Not null
78 public static final void showPreferencesDialog(final Context context) {
79 newPreferencesDialog(context).setVisible(true);
82 /**
84 * @param context
85 * <br>Not null
87 public static final void quit(final Context context) {
88 ignore(context);
90 // TODO confirm
92 System.exit(0);
95 /**
97 * @param context
98 * <br>Not null
99 * <br>Input-output
101 public static final void open(final Context context) {
102 final JFileChooser fileChooser = new JFileChooser();
104 fileChooser.setMultiSelectionEnabled(false);
105 fileChooser.setFileFilter(new FileFilter() {
107 @Override
108 public final boolean accept(final File file) {
109 return file.isDirectory() || file.getName().endsWith(".srt");
112 @Override
113 public final String getDescription() {
114 return translate("Subtitles file $0", "(*.srt)");
119 if (JFileChooser.APPROVE_OPTION == fileChooser.showOpenDialog((Component) context.get(MAIN_FRAME)) &&
120 fileChooser.getSelectedFile() != null) {
121 ((Subtitles) context.get(SUBTITLES)).load(fileChooser.getSelectedFile());
127 * @param context
128 * <br>Not null
129 * <br>Input-output
131 public static final void save(final Context context) {
132 ((Subtitles) context.get(SUBTITLES)).save();
137 * @param context
138 * <br>Not null
140 public static final void showManual(final Context context) {
141 showTODOMessage(context);
146 * @param context
147 * <br>Not null
149 public static final void showTODOMessage(final Context context) {
150 System.out.println(Tools.debug(3, "TODO"));
151 JOptionPane.showMessageDialog(
152 (Component) context.get(MAIN_FRAME),
153 translate("Not implemented"),
154 context.get(APPLICATION_NAME).toString(),
155 JOptionPane.INFORMATION_MESSAGE);
160 * @param context
161 * <br>Not null
162 * @param throwable
163 * <br>Not null
165 public static final void showErrorMessage(final Context context, final Throwable throwable) {
166 if (SwingTools.canInvokeLaterThisMethodInAWT(null, throwable)) {
167 JOptionPane.showMessageDialog(
168 null,
169 newErrorMessagePanel(throwable),
170 context.get(APPLICATION_NAME).toString(),
171 JOptionPane.ERROR_MESSAGE);
177 * @param context
178 * <br>Not null
180 public static final void updateMainFrameTitle(final Context context) {
181 ((JFrame) context.get(MAIN_FRAME)).setTitle(makeMainFrameTitle(context));
186 * @param context
187 * <br>Not null
188 * @return
189 * <br>Not null
191 private static final String makeMainFrameTitle(final Context context) {
192 final File file = context.get(FILE);
193 final Boolean fileModified = context.get(FILE_MODIFIED);
195 return file == null ? context.get(APPLICATION_NAME).toString() : file.getName() + (fileModified ? "*" : "");