[Markups]
[aprog.git] / SubtitlesAdjuster / src / net / sourceforge / aprog / subtitlesadjuster / SubtitlesAdjusterActions.java
blob38cdd1f3c5105eea0ba5bed333595f0e360e2ec3
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.*;
31 import java.awt.Component;
32 import java.io.File;
34 import javax.swing.JFileChooser;
35 import javax.swing.JFrame;
36 import javax.swing.JOptionPane;
37 import javax.swing.filechooser.FileFilter;
39 import net.sourceforge.aprog.context.Context;
40 import net.sourceforge.aprog.swing.SwingTools;
41 import net.sourceforge.aprog.tools.Tools;
43 /**
44 * This class defines all the operations that can be executed on the application context object.
46 * @author codistmonk (creation 2010-06-27)
48 public final class SubtitlesAdjusterActions {
50 /**
51 * Private default constructor to prevent instantiation.
53 private SubtitlesAdjusterActions() {
54 // Do nothing
57 /**
59 * @param context
60 * <br>Not null
62 public static final void showAboutDialog(final Context context) {
63 JOptionPane.showMessageDialog(
64 (Component) context.get(MAIN_FRAME),
65 context.get(APPLICATION_NAME) + "\n" +
66 context.get(APPLICATION_VERSION) + "\n" +
67 context.get(APPLICATION_COPYRIGHT),
68 translate("About $0", context.get(APPLICATION_NAME)),
69 JOptionPane.INFORMATION_MESSAGE);
72 /**
74 * @param context
75 * <br>Not null
77 public static final void showPreferencesDialog(final Context context) {
78 newPreferencesDialog(context).setVisible(true);
81 /**
83 * @param context
84 * <br>Not null
86 public static final void quit(final Context context) {
87 System.exit(0);
90 /**
92 * @param context
93 * <br>Not null
94 * <br>Input-output
96 public static final void open(final Context context) {
97 final JFileChooser fileChooser = new JFileChooser();
99 fileChooser.setMultiSelectionEnabled(false);
100 fileChooser.setFileFilter(new FileFilter() {
102 @Override
103 public final boolean accept(final File file) {
104 return file.isDirectory() || file.getName().endsWith(".srt");
107 @Override
108 public final String getDescription() {
109 return translate("Subtitles file $0", "(*.srt)");
114 if (JFileChooser.APPROVE_OPTION == fileChooser.showOpenDialog((Component) context.get(MAIN_FRAME)) &&
115 fileChooser.getSelectedFile() != null) {
116 ((Subtitles) context.get(SUBTITLES)).load(fileChooser.getSelectedFile());
122 * @param context
123 * <br>Not null
124 * <br>Input-output
126 public static final void save(final Context context) {
127 ((Subtitles) context.get(SUBTITLES)).save();
132 * @param context
133 * <br>Not null
135 public static final void showManual(final Context context) {
136 showTODOMessage(context);
141 * @param context
142 * <br>Not null
144 public static final void showTODOMessage(final Context context) {
145 System.out.println(Tools.debug(3, "TODO"));
146 JOptionPane.showMessageDialog(
147 (Component) context.get(MAIN_FRAME),
148 translate("Not implemented"),
149 context.get(APPLICATION_NAME).toString(),
150 JOptionPane.INFORMATION_MESSAGE);
155 * @param context
156 * <br>Not null
157 * @param throwable
158 * <br>Not null
160 public static final void showErrorMessage(final Context context, final Throwable throwable) {
161 if (SwingTools.canInvokeLaterThisMethodInAWT(null, throwable)) {
162 JOptionPane.showMessageDialog(
163 null,
164 newErrorMessagePanel(throwable),
165 context.get(APPLICATION_NAME).toString(),
166 JOptionPane.ERROR_MESSAGE);
172 * @param context
173 * <br>Not null
175 public static final void updateMainFrameTitle(final Context context) {
176 ((JFrame) context.get(MAIN_FRAME)).setTitle(makeMainFrameTitle(context));
181 * @param context
182 * <br>Not null
183 * @return
184 * <br>Not null
186 private static final String makeMainFrameTitle(final Context context) {
187 final File file = context.get(FILE);
188 final Boolean fileModified = context.get(FILE_MODIFIED);
190 return file == null ? context.get(APPLICATION_NAME).toString() : file.getName() + (fileModified ? "*" : "");