KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > rcp > action > NewFileAction


1 /**
2  * <p> Project: com.nightlabs.base </p>
3  * <p> Copyright: Copyright (c) 2005 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 05.07.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.rcp.action;
9
10 import java.io.File JavaDoc;
11 import java.io.IOException JavaDoc;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.ui.PartInitException;
15
16 import com.nightlabs.base.NLBasePlugin;
17 import com.nightlabs.rcp.io.IOUtil;
18
19 public class NewFileAction
20 extends Action
21 implements INewFileAction
22 {
23     public final String JavaDoc ID = NewFileAction.class.getName() + " " + hashCode();
24     
25     public NewFileAction()
26     {
27         super();
28         init();
29     }
30
31     protected void init()
32     {
33         setId(ID);
34     }
35         
36     protected String JavaDoc fileExtension;
37     public String JavaDoc getFileExtension() {
38         return fileExtension;
39     }
40     public void setFileExtension(String JavaDoc fileExtension) {
41         this.fileExtension = fileExtension;
42     }
43     
44     protected int fileCount = 0;
45     protected int nextFileCount() {
46         return ++fileCount;
47     }
48     
49     protected String JavaDoc defaultFileName = NLBasePlugin.getResourceString("action.new.unnamed");
50     protected String JavaDoc defaultPath = "";
51                 
52     protected String JavaDoc getDefaultPath()
53     {
54     if (defaultPath.equals("")) {
55         String JavaDoc tmpDir = System.getProperty("java.io.tmpdir");
56         defaultPath = tmpDir;
57     }
58     return defaultPath;
59     }
60     
61     public void run()
62     {
63         nextFileCount();
64         File JavaDoc file = createFile(fileExtension);
65         try {
66             file.createNewFile();
67             IOUtil.openFile(file, false);
68         } catch (PartInitException e) {
69             e.printStackTrace();
70         } catch (IOException JavaDoc e) {
71             e.printStackTrace();
72         }
73     }
74     
75 // public void run()
76
// {
77
// nextFileCount();
78
// File file = createFile(fileExtension);
79
// try {
80
// IOUtil.openFile(file);
81
// } catch (PartInitException e) {
82
// e.printStackTrace();
83
// }
84
// }
85

86     protected File JavaDoc createFile(String JavaDoc fileExtension)
87     {
88         String JavaDoc fileName = defaultFileName + fileCount + "." + fileExtension;
89         return new File JavaDoc(getDefaultPath(), fileName);
90     }
91 }
92
Popular Tags