001: /*
002: * SaveProjectAction.java. Created on Apr 23, 2004.
003: */
004: package org.webdocwf.util.loader.wizard;
005:
006: import java.awt.Toolkit;
007: import java.awt.event.ActionEvent;
008: import java.io.File;
009: import java.io.FileOutputStream;
010: import java.io.PrintStream;
011:
012: import javax.swing.AbstractAction;
013: import javax.swing.ImageIcon;
014: import javax.swing.JFileChooser;
015: import javax.swing.JOptionPane;
016: import javax.swing.KeyStroke;
017:
018: import org.webdocwf.util.loader.generator.Utils;
019:
020: /**
021: *
022: *
023: * @author Zoran Milakovic
024: */
025: public class SaveProjectAction extends AbstractAction {
026:
027: OctopusProjectFrame octopusProjectFrame;
028:
029: public SaveProjectAction(OctopusProjectFrame octopusProjectFrame,
030: String label) {
031: this .octopusProjectFrame = octopusProjectFrame;
032: putValue(NAME, label);
033: putValue(SMALL_ICON, new ImageIcon(getClass().getClassLoader()
034: .getResource(
035: "org/webdocwf/util/loader/"
036: + "wizard/images/Save.gif")));
037: putValue(SHORT_DESCRIPTION, "Save TDT project");
038: putValue(LONG_DESCRIPTION,
039: "Save Together Data Transformer project");
040: putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('S', Toolkit
041: .getDefaultToolkit().getMenuShortcutKeyMask()));
042: putValue(MNEMONIC_KEY, new Integer('S'));
043: }
044:
045: /**
046: * This method check if the action is performed (event key pressed)
047: * @param e is event
048: */
049: public void actionPerformed(ActionEvent e) {
050: try {
051: OctopusLoaderData loaderData = octopusProjectFrame
052: .getLoaderPanel().getOctopusLoaderInit();
053: OctopusGeneratorData generatorData = octopusProjectFrame
054: .getGeneratorPanel().getOctopusGeneratorInit();
055:
056: String confString = "# Octopus project file. \n\n";
057: confString += "ProjectName="
058: + octopusProjectFrame.getProjectName();
059: confString += "\n\n";
060: confString += "# Loader section \n";
061: confString += loaderData.toConfString();
062: confString += "\n\n# Generator section \n";
063: confString += generatorData.toConfString();
064:
065: confString = Utils.replaceAll(confString, "\\", "\\\\");
066:
067: if (!this .octopusProjectFrame.getPathToOPF().equals("")) {
068: this .saveFile(this .octopusProjectFrame.getPathToOPF(),
069: confString);
070: return;
071: }
072:
073: File current = new File(System.getProperty("user.dir"));
074: JFileChooser chooser = new JFileChooser(current);
075: chooser
076: .setDialogTitle("Save Together Data Transformer project ...");
077: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
078: chooser
079: .setFileFilter((javax.swing.filechooser.FileFilter) new OctopusFileFilter(
080: OctopusFileFilter.OPF));
081: chooser.setAcceptAllFileFilterUsed(false);
082:
083: chooser.setSelectedFile(new File("new_project.opf"));
084: chooser.setApproveButtonText("Save");
085: int v = chooser.showOpenDialog(octopusProjectFrame);
086:
087: switch (v) {
088: case JFileChooser.APPROVE_OPTION:
089: if (chooser.getSelectedFile() != null) {
090: this .octopusProjectFrame.setPathToOPF(chooser
091: .getSelectedFile().getAbsolutePath());
092: this .saveFile(chooser.getSelectedFile()
093: .getAbsolutePath(), confString);
094: }
095: break;
096: case JFileChooser.CANCEL_OPTION:
097: case JFileChooser.ERROR_OPTION:
098: chooser.removeAll();
099: chooser = null;
100: current = null;
101:
102: }
103:
104: } catch (Exception ex) {
105: String message = "Error while save Together Data Transformer project. Message :"
106: + ex.getMessage();
107: JOptionPane.showMessageDialog(octopusProjectFrame, message
108: + "\n", "Error", 0);
109: }
110: }
111:
112: private boolean saveFile(String fileName, String text)
113: throws Exception {
114: boolean success = true;
115: try {
116: File saveAsFile = new File(fileName);
117: FileOutputStream fos = new FileOutputStream(saveAsFile);
118: PrintStream printStream = new PrintStream(fos, true);
119: printStream.print(text);
120: fos.close();
121: printStream.close();
122: } catch (Exception e) {
123: success = false;
124: throw e;
125: }
126: return success;
127: }
128: }
|