1 /*
2 * Copyright 2006-2016 The JGUIraffe Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.jguiraffe.examples.tutorial.viewset;
17
18 import java.io.File;
19
20 import net.sf.jguiraffe.examples.tutorial.model.DirectoryData;
21 import net.sf.jguiraffe.gui.app.ApplicationContext;
22 import net.sf.jguiraffe.gui.builder.action.FormAction;
23 import net.sf.jguiraffe.gui.builder.utils.MessageOutput;
24 import net.sf.jguiraffe.gui.cmd.CommandBase;
25
26 /**
27 * <p>
28 * A command class for storing view settings for a given directory.
29 * </p>
30 * <p>
31 * A command of this type is executed when the user clicks <em>Save</em> in the
32 * dialog with view settings. The command class stores the current
33 * {@link ViewSettings} instance and triggers the <em>refresh</em> action.
34 * </p>
35 *
36 * @author Oliver Heger
37 * @version $Id: CreateViewSettingsCommand.java 205 2012-01-29 18:29:57Z oheger $
38 */
39 public class CreateViewSettingsCommand extends CommandBase
40 {
41 /** Constant for the resource ID of the title for the error message. */
42 private static final String RES_ERR_TITLE = "viewset_errmsg_tit";
43
44 /** Constant for the resource ID of the text for the error message. */
45 private static final String RES_ERR_TEXT = "viewset_errmsg_txt";
46
47 /** Stores the directory of the view settings file. */
48 private final File targetDirectory;
49
50 /** The application context. */
51 private final ApplicationContext appContext;
52
53 /** The data object containing the view settings to be saved. */
54 private final ViewSettings viewSettings;
55
56 /** The refresh action. */
57 private final FormAction refreshAction;
58
59 public CreateViewSettingsCommand(ApplicationContext appctx,
60 ViewSettings vs, FormAction actRefresh)
61 {
62 super(true);
63 appContext = appctx;
64 DirectoryData dd = appctx.getTypedProperty(DirectoryData.class);
65 assert dd != null : "No current directory!";
66 targetDirectory = dd.getDirectory();
67 viewSettings = vs;
68 refreshAction = actRefresh;
69 }
70
71 /**
72 * Executes this command. Stores the current {@code ViewSettings} object in
73 * the associated directory.
74 *
75 * @throws Exception if an error occurs
76 */
77 @Override
78 public void execute() throws Exception
79 {
80 viewSettings.save(targetDirectory);
81 }
82
83 /**
84 * Updates the UI after a successful execution of this command. This
85 * implementation invokes the refresh action, so that the new view settings
86 * become active. If an error occurred, a message box is displayed.
87 */
88 @Override
89 protected void performGUIUpdate()
90 {
91 if (getException() != null)
92 {
93 getLog().error("Could not create file", getException());
94 appContext.messageBox(RES_ERR_TEXT, RES_ERR_TITLE,
95 MessageOutput.MESSAGE_ERROR, MessageOutput.BTN_OK);
96 }
97 else
98 {
99 refreshAction.execute(null);
100 }
101 }
102 }