IWriter.java :  » Net » hyperpool-0.4.0 » vicazh » hyperpool » Java Open Source

Java Open Source » Net » hyperpool 0.4.0 
hyperpool 0.4.0 » vicazh » hyperpool » IWriter.java
package vicazh.hyperpool;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.xml.transform.*;
import java.beans.*;
import java.io.*;
import java.util.logging.*;

/**
 * The graphic object writer
 * 
 * @author Victor Zhigunov
 * @version 0.4.0
 */
public class IWriter implements ActionListener, PropertyChangeListener,
    WindowListener {
  private JDialog dialog;

  private JOptionPane optionPane;

  private JFileChooser chooser;

  private Component parent;

  private JMenuItem menuSave;

  private AbstractButton buttonSave;

  private JDialog dialogReplace;

  private JOptionPane optionPaneReplace;

  /**
   * @param dialog
   *            save dialog
   * @param chooser
   *            file chooser
   * @param parent
   *            parent component
   * @param menuSave
   *            save menu item
   * @param buttonSave
   *            save button
   * @param dialogReplace
   *            replace dialog
   */
  public IWriter(JDialog dialog, JFileChooser chooser, Component parent,
      JMenuItem menuSave, AbstractButton buttonSave, JDialog dialogReplace)
      throws TransformerException {
    this.dialog = dialog;
    optionPane = (JOptionPane) dialog.getContentPane();
    optionPane.addPropertyChangeListener(this);
    this.chooser = chooser;
    this.parent = parent;
    this.menuSave = menuSave;
    menuSave.addActionListener(this);
    this.buttonSave = buttonSave;
    buttonSave.addActionListener(this);
    this.dialogReplace = dialogReplace;
    optionPaneReplace = (JOptionPane) dialogReplace.getContentPane();
    optionPaneReplace.addPropertyChangeListener(this);
  }

  private ICom com;

  /**
   * @param com
   *            communicator
   */
  public void setCom(ICom com) {
    this.com = com;
  }

  byte[] data;

  private File out;

  public void actionPerformed(ActionEvent e) {
    try {
      Object source = e.getSource();
      if (source == menuSave || source == buttonSave) {
        data = com.get();
        Writer.put(data, out);
      } else if (e.getActionCommand().equals("saveas")) {
        if (chooser.showSaveDialog(parent) != JFileChooser.APPROVE_OPTION)
          return;
        File f = getSelectedFile();
        if (f.exists()) {
          dialogReplace.setLocationRelativeTo(dialogReplace
              .getOwner());
          dialogReplace.setVisible(true);
          Object value = optionPaneReplace.getValue();
          if (value == JOptionPane.UNINITIALIZED_VALUE)
            return;
          optionPaneReplace.setValue(JOptionPane.UNINITIALIZED_VALUE);
          if (((Integer) value).intValue() != JOptionPane.YES_OPTION)
            return;
        }
        setOut(f);
        data = com.get();
        Writer.put(data, out);
      } else
        windowClosing(null);
    } catch (Exception ex) {
      Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
    }
  }

  public void windowClosing(WindowEvent e) {
    try {
      if (!com.isConnect() || check() != Status.CANCEL)
        System.exit(0);
    } catch (Exception ex) {
      Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
    }
  }

  enum Status {
    EMPTY, OK, CANCEL
  };

  Status check() throws IOException, TransformerException {
    byte[] d = com.get();
    if (d == null)
      return Status.EMPTY;
    if (data != null && new String(d).equals(new String(data)))
      return Status.OK;
    dialog.setLocationRelativeTo(dialog.getOwner());
    dialog.setVisible(true);
    Object value = optionPane.getValue();
    if (value == JOptionPane.UNINITIALIZED_VALUE)
      return Status.CANCEL;
    optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
    switch (((Integer) value).intValue()) {
    case JOptionPane.NO_OPTION:
      return Status.OK;
    case JOptionPane.OK_OPTION:
      if (out == null) {
        if (chooser.showSaveDialog(parent) != JFileChooser.APPROVE_OPTION)
          return Status.CANCEL;
        out = getSelectedFile();
      }
      Writer.put(d, out);
      return Status.OK;
    }
    return Status.CANCEL;
  }

  private File getSelectedFile() {
    File f = chooser.getSelectedFile();
    String end = '.' + ((IFileFilter) chooser.getFileFilter()).extension;
    return f.getName().endsWith(end) ? f : new File(f.getAbsolutePath()
        + end);
  }

  public void propertyChange(PropertyChangeEvent e) {
    Component c = ((JComponent) e.getSource()).getTopLevelAncestor();
    String prop = e.getPropertyName();
    if (c.isVisible()
        && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop
            .equals(JOptionPane.INPUT_VALUE_PROPERTY)))
      c.setVisible(false);
  }

  public void windowOpened(WindowEvent e) {
  }

  public void windowDeactivated(WindowEvent e) {
  }

  public void windowActivated(WindowEvent e) {
  }

  public void windowDeiconified(WindowEvent e) {
  }

  public void windowIconified(WindowEvent e) {
  }

  public void windowClosed(WindowEvent e) {
  }

  /**
   * Set the xml result file
   * 
   * @param out
   *            xml result file
   */
  public void setOut(File out) throws TransformerException {
    this.out = out;
    if (out != null) {
      menuSave.setEnabled(true);
      buttonSave.setEnabled(true);
    }
  }
}
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.