SaveProjectAction.java :  » Database-JDBC-Connection-Pool » octopus » org » webdocwf » util » loader » wizard » Java Open Source

Java Open Source » Database JDBC Connection Pool » octopus 
octopus » org » webdocwf » util » loader » wizard » SaveProjectAction.java
/*
 * SaveProjectAction.java. Created on Apr 23, 2004.
 */
package org.webdocwf.util.loader.wizard;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import org.webdocwf.util.loader.generator.Utils;

/**
 * 
 * 
 * @author Zoran Milakovic
 */
public class SaveProjectAction extends AbstractAction {

  OctopusProjectFrame octopusProjectFrame;

  public SaveProjectAction(OctopusProjectFrame octopusProjectFrame, String label) {
    this.octopusProjectFrame = octopusProjectFrame;
    putValue(NAME, label);
    putValue(
      SMALL_ICON,
      new ImageIcon(
        getClass().getClassLoader().getResource(
          "org/webdocwf/util/loader/" + "wizard/images/Save.gif")));
    putValue(SHORT_DESCRIPTION, "Save TDT project");
    putValue(LONG_DESCRIPTION, "Save Together Data Transformer project");
    putValue(
      ACCELERATOR_KEY,
      KeyStroke.getKeyStroke('S', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    putValue(MNEMONIC_KEY, new Integer('S'));
  }

  /**
   * This method check if the action is performed (event key pressed)
   * @param e is event
   */
  public void actionPerformed(ActionEvent e) {
    try {
      OctopusLoaderData loaderData =
        octopusProjectFrame.getLoaderPanel().getOctopusLoaderInit();
      OctopusGeneratorData generatorData =
        octopusProjectFrame.getGeneratorPanel().getOctopusGeneratorInit();
      
      String confString = "#  Octopus project file. \n\n";
      confString += "ProjectName="+octopusProjectFrame.getProjectName();
      confString += "\n\n";
      confString += "#  Loader section \n";
      confString += loaderData.toConfString();
      confString += "\n\n#  Generator section \n";
      confString += generatorData.toConfString();
      
      confString = Utils.replaceAll(confString,"\\","\\\\");
      
            if( !this.octopusProjectFrame.getPathToOPF().equals("") ) {
                this.saveFile( this.octopusProjectFrame.getPathToOPF(), confString);
                return;
            }
            
      File current = new File(System.getProperty("user.dir"));
      JFileChooser chooser = new JFileChooser(current);
      chooser.setDialogTitle("Save Together Data Transformer project ...");
      chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
      chooser.setFileFilter(
        (javax.swing.filechooser.FileFilter) new OctopusFileFilter(OctopusFileFilter.OPF));
      chooser.setAcceptAllFileFilterUsed(false);

      chooser.setSelectedFile(new File("new_project.opf"));
      chooser.setApproveButtonText("Save");
      int v = chooser.showOpenDialog(octopusProjectFrame);

      switch (v) {
        case JFileChooser.APPROVE_OPTION :
          if (chooser.getSelectedFile() != null) {
                        this.octopusProjectFrame.setPathToOPF(chooser.getSelectedFile().getAbsolutePath());
            this.saveFile(chooser.getSelectedFile().getAbsolutePath(), confString);
          }
          break;
        case JFileChooser.CANCEL_OPTION :
        case JFileChooser.ERROR_OPTION :
          chooser.removeAll();
          chooser = null;
          current = null;

      }

    } catch (Exception ex) {
      String message = "Error while save Together Data Transformer project. Message :" + ex.getMessage();
      JOptionPane.showMessageDialog(octopusProjectFrame, message + "\n", "Error", 0);
    }
  }

  private boolean saveFile(String fileName, String text) throws Exception {
    boolean success = true;
    try {
      File saveAsFile = new File(fileName);
      FileOutputStream fos = new FileOutputStream(saveAsFile);
      PrintStream printStream = new PrintStream(fos, true);
      printStream.print(text);
      fos.close();
      printStream.close();
    } catch (Exception e) {
      success = false;
      throw e;
    }
    return success;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.