1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33
34
35
36
37
38
39
40
41
42 public class CreateFileCommand extends CommandBase
43 {
44
45 private static final String RES_ERR_TITLE = "newfile_errmsg_tit";
46
47
48 private static final String RES_ERR_TEXT = "newfile_errmsg_txt";
49
50
51 private final File targetDirectory;
52
53
54 private final ApplicationContext appContext;
55
56
57 private final CreateFileData fileData;
58
59
60 private final FormAction refreshAction;
61
62
63
64
65
66
67
68
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
84
85
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
105
106
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 }