View Javadoc

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.createfile;
17  
18  import java.io.File;
19  import java.io.FileWriter;
20  import java.io.PrintWriter;
21  
22  import net.sf.jguiraffe.examples.tutorial.model.DirectoryData;
23  import net.sf.jguiraffe.gui.app.ApplicationContext;
24  import net.sf.jguiraffe.gui.builder.action.FormAction;
25  import net.sf.jguiraffe.gui.builder.utils.MessageOutput;
26  import net.sf.jguiraffe.gui.cmd.CommandBase;
27  
28  /**
29   * <p>
30   * A command class for creating a new file.
31   * </p>
32   * <p>
33   * This command is associated with the OK button of the <em>create new file</em>
34   * dialog. It actually creates the file. The data of the file to be created is
35   * obtained from the model of the dialog which is injected. After the file was
36   * created the current directory is refreshed.
37   * </p>
38   *
39   * @author Oliver Heger
40   * @version $Id: CreateFileCommand.java 205 2012-01-29 18:29:57Z oheger $
41   */
42  public class CreateFileCommand extends CommandBase
43  {
44      /** Constant for the resource ID of the title for the error message. */
45      private static final String RES_ERR_TITLE = "newfile_errmsg_tit";
46  
47      /** Constant for the resource ID of the text for the error message. */
48      private static final String RES_ERR_TEXT = "newfile_errmsg_txt";
49  
50      /** Stores the directory of the new file. */
51      private final File targetDirectory;
52  
53      /** The application context. */
54      private final ApplicationContext appContext;
55  
56      /** The data object for the new file. */
57      private final CreateFileData fileData;
58  
59      /** The refresh action. */
60      private final FormAction refreshAction;
61  
62      /**
63       * Creates a new instance of {@code CreateFileCommand} and initializes it
64       * with the application context and the data of the file to be created.
65       *
66       * @param ctx the application context
67       * @param cfd the data for the new file
68       * @param actRefresh the refresh action
69       */
70      public CreateFileCommand(ApplicationContext ctx, CreateFileData cfd,
71              FormAction actRefresh)
72      {
73          super(true);
74          appContext = ctx;
75          DirectoryData dd = ctx.getTypedProperty(DirectoryData.class);
76          assert dd != null : "No current directory!";
77          targetDirectory = dd.getDirectory();
78          fileData = cfd;
79          refreshAction = actRefresh;
80      }
81  
82      /**
83       * Executes this command. Writes the new file.
84       *
85       * @throws Exception in case of an error
86       */
87      @Override
88      public void execute() throws Exception
89      {
90          File newFile = new File(targetDirectory, fileData.getFileName());
91          getLog().info("Creating new file " + newFile.getAbsolutePath());
92          PrintWriter out = new PrintWriter(new FileWriter(newFile));
93          try
94          {
95              out.print(fileData.getFileContent());
96          }
97          finally
98          {
99              out.close();
100         }
101     }
102 
103     /**
104      * Updates the UI after a successful execution of this command. This
105      * implementation invokes the refresh action, so that the newly created file
106      * is displayed.
107      */
108     @Override
109     protected void performGUIUpdate()
110     {
111         if (getException() != null)
112         {
113             getLog().error("Could not create file", getException());
114             appContext.messageBox(RES_ERR_TEXT, RES_ERR_TITLE,
115                     MessageOutput.MESSAGE_ERROR, MessageOutput.BTN_OK);
116         }
117         else
118         {
119             refreshAction.execute(null);
120         }
121     }
122 }