KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > wizard > SaveProjectAction


1 /*
2  * SaveProjectAction.java. Created on Apr 23, 2004.
3  */

4 package org.webdocwf.util.loader.wizard;
5
6 import java.awt.Toolkit JavaDoc;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.io.File JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.PrintStream JavaDoc;
11
12 import javax.swing.AbstractAction JavaDoc;
13 import javax.swing.ImageIcon JavaDoc;
14 import javax.swing.JFileChooser JavaDoc;
15 import javax.swing.JOptionPane JavaDoc;
16 import javax.swing.KeyStroke JavaDoc;
17
18 import org.webdocwf.util.loader.generator.Utils;
19
20 /**
21  *
22  *
23  * @author Zoran Milakovic
24  */

25 public class SaveProjectAction extends AbstractAction JavaDoc {
26
27     OctopusProjectFrame octopusProjectFrame;
28
29     public SaveProjectAction(OctopusProjectFrame octopusProjectFrame, String JavaDoc label) {
30         this.octopusProjectFrame = octopusProjectFrame;
31         putValue(NAME, label);
32         putValue(
33             SMALL_ICON,
34             new ImageIcon JavaDoc(
35                 getClass().getClassLoader().getResource(
36                     "org/webdocwf/util/loader/" + "wizard/images/Save.gif")));
37         putValue(SHORT_DESCRIPTION, "Save TDT project");
38         putValue(LONG_DESCRIPTION, "Save Together Data Transformer project");
39         putValue(
40             ACCELERATOR_KEY,
41             KeyStroke.getKeyStroke('S', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
42         putValue(MNEMONIC_KEY, new Integer JavaDoc('S'));
43     }
44
45     /**
46      * This method check if the action is performed (event key pressed)
47      * @param e is event
48      */

49     public void actionPerformed(ActionEvent JavaDoc e) {
50         try {
51             OctopusLoaderData loaderData =
52                 octopusProjectFrame.getLoaderPanel().getOctopusLoaderInit();
53             OctopusGeneratorData generatorData =
54                 octopusProjectFrame.getGeneratorPanel().getOctopusGeneratorInit();
55             
56             String JavaDoc confString = "# Octopus project file. \n\n";
57             confString += "ProjectName="+octopusProjectFrame.getProjectName();
58             confString += "\n\n";
59             confString += "# Loader section \n";
60             confString += loaderData.toConfString();
61             confString += "\n\n# Generator section \n";
62             confString += generatorData.toConfString();
63             
64             confString = Utils.replaceAll(confString,"\\","\\\\");
65             
66             if( !this.octopusProjectFrame.getPathToOPF().equals("") ) {
67                 this.saveFile( this.octopusProjectFrame.getPathToOPF(), confString);
68                 return;
69             }
70             
71             File JavaDoc current = new File JavaDoc(System.getProperty("user.dir"));
72             JFileChooser JavaDoc chooser = new JFileChooser JavaDoc(current);
73             chooser.setDialogTitle("Save Together Data Transformer project ...");
74             chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
75             chooser.setFileFilter(
76                 (javax.swing.filechooser.FileFilter JavaDoc) new OctopusFileFilter(OctopusFileFilter.OPF));
77             chooser.setAcceptAllFileFilterUsed(false);
78
79             chooser.setSelectedFile(new File JavaDoc("new_project.opf"));
80             chooser.setApproveButtonText("Save");
81             int v = chooser.showOpenDialog(octopusProjectFrame);
82
83             switch (v) {
84                 case JFileChooser.APPROVE_OPTION :
85                     if (chooser.getSelectedFile() != null) {
86                         this.octopusProjectFrame.setPathToOPF(chooser.getSelectedFile().getAbsolutePath());
87                         this.saveFile(chooser.getSelectedFile().getAbsolutePath(), confString);
88                     }
89                     break;
90                 case JFileChooser.CANCEL_OPTION :
91                 case JFileChooser.ERROR_OPTION :
92                     chooser.removeAll();
93                     chooser = null;
94                     current = null;
95
96             }
97
98         } catch (Exception JavaDoc ex) {
99             String JavaDoc message = "Error while save Together Data Transformer project. Message :" + ex.getMessage();
100             JOptionPane.showMessageDialog(octopusProjectFrame, message + "\n", "Error", 0);
101         }
102     }
103
104     private boolean saveFile(String JavaDoc fileName, String JavaDoc text) throws Exception JavaDoc {
105         boolean success = true;
106         try {
107             File JavaDoc saveAsFile = new File JavaDoc(fileName);
108             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(saveAsFile);
109             PrintStream JavaDoc printStream = new PrintStream JavaDoc(fos, true);
110             printStream.print(text);
111             fos.close();
112             printStream.close();
113         } catch (Exception JavaDoc e) {
114             success = false;
115             throw e;
116         }
117         return success;
118     }
119 }
120
Popular Tags