001: package vicazh.hyperpool;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006: import javax.xml.transform.*;
007: import java.beans.*;
008: import java.io.*;
009: import java.util.logging.*;
010:
011: /**
012: * The graphic object writer
013: *
014: * @author Victor Zhigunov
015: * @version 0.4.0
016: */
017: public class IWriter implements ActionListener, PropertyChangeListener,
018: WindowListener {
019: private JDialog dialog;
020:
021: private JOptionPane optionPane;
022:
023: private JFileChooser chooser;
024:
025: private Component parent;
026:
027: private JMenuItem menuSave;
028:
029: private AbstractButton buttonSave;
030:
031: private JDialog dialogReplace;
032:
033: private JOptionPane optionPaneReplace;
034:
035: /**
036: * @param dialog
037: * save dialog
038: * @param chooser
039: * file chooser
040: * @param parent
041: * parent component
042: * @param menuSave
043: * save menu item
044: * @param buttonSave
045: * save button
046: * @param dialogReplace
047: * replace dialog
048: */
049: public IWriter(JDialog dialog, JFileChooser chooser,
050: Component parent, JMenuItem menuSave,
051: AbstractButton buttonSave, JDialog dialogReplace)
052: throws TransformerException {
053: this .dialog = dialog;
054: optionPane = (JOptionPane) dialog.getContentPane();
055: optionPane.addPropertyChangeListener(this );
056: this .chooser = chooser;
057: this .parent = parent;
058: this .menuSave = menuSave;
059: menuSave.addActionListener(this );
060: this .buttonSave = buttonSave;
061: buttonSave.addActionListener(this );
062: this .dialogReplace = dialogReplace;
063: optionPaneReplace = (JOptionPane) dialogReplace
064: .getContentPane();
065: optionPaneReplace.addPropertyChangeListener(this );
066: }
067:
068: private ICom com;
069:
070: /**
071: * @param com
072: * communicator
073: */
074: public void setCom(ICom com) {
075: this .com = com;
076: }
077:
078: byte[] data;
079:
080: private File out;
081:
082: public void actionPerformed(ActionEvent e) {
083: try {
084: Object source = e.getSource();
085: if (source == menuSave || source == buttonSave) {
086: data = com.get();
087: Writer.put(data, out);
088: } else if (e.getActionCommand().equals("saveas")) {
089: if (chooser.showSaveDialog(parent) != JFileChooser.APPROVE_OPTION)
090: return;
091: File f = getSelectedFile();
092: if (f.exists()) {
093: dialogReplace.setLocationRelativeTo(dialogReplace
094: .getOwner());
095: dialogReplace.setVisible(true);
096: Object value = optionPaneReplace.getValue();
097: if (value == JOptionPane.UNINITIALIZED_VALUE)
098: return;
099: optionPaneReplace
100: .setValue(JOptionPane.UNINITIALIZED_VALUE);
101: if (((Integer) value).intValue() != JOptionPane.YES_OPTION)
102: return;
103: }
104: setOut(f);
105: data = com.get();
106: Writer.put(data, out);
107: } else
108: windowClosing(null);
109: } catch (Exception ex) {
110: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
111: }
112: }
113:
114: public void windowClosing(WindowEvent e) {
115: try {
116: if (!com.isConnect() || check() != Status.CANCEL)
117: System.exit(0);
118: } catch (Exception ex) {
119: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
120: }
121: }
122:
123: enum Status {
124: EMPTY, OK, CANCEL
125: };
126:
127: Status check() throws IOException, TransformerException {
128: byte[] d = com.get();
129: if (d == null)
130: return Status.EMPTY;
131: if (data != null && new String(d).equals(new String(data)))
132: return Status.OK;
133: dialog.setLocationRelativeTo(dialog.getOwner());
134: dialog.setVisible(true);
135: Object value = optionPane.getValue();
136: if (value == JOptionPane.UNINITIALIZED_VALUE)
137: return Status.CANCEL;
138: optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
139: switch (((Integer) value).intValue()) {
140: case JOptionPane.NO_OPTION:
141: return Status.OK;
142: case JOptionPane.OK_OPTION:
143: if (out == null) {
144: if (chooser.showSaveDialog(parent) != JFileChooser.APPROVE_OPTION)
145: return Status.CANCEL;
146: out = getSelectedFile();
147: }
148: Writer.put(d, out);
149: return Status.OK;
150: }
151: return Status.CANCEL;
152: }
153:
154: private File getSelectedFile() {
155: File f = chooser.getSelectedFile();
156: String end = '.' + ((IFileFilter) chooser.getFileFilter()).extension;
157: return f.getName().endsWith(end) ? f : new File(f
158: .getAbsolutePath()
159: + end);
160: }
161:
162: public void propertyChange(PropertyChangeEvent e) {
163: Component c = ((JComponent) e.getSource())
164: .getTopLevelAncestor();
165: String prop = e.getPropertyName();
166: if (c.isVisible()
167: && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop
168: .equals(JOptionPane.INPUT_VALUE_PROPERTY)))
169: c.setVisible(false);
170: }
171:
172: public void windowOpened(WindowEvent e) {
173: }
174:
175: public void windowDeactivated(WindowEvent e) {
176: }
177:
178: public void windowActivated(WindowEvent e) {
179: }
180:
181: public void windowDeiconified(WindowEvent e) {
182: }
183:
184: public void windowIconified(WindowEvent e) {
185: }
186:
187: public void windowClosed(WindowEvent e) {
188: }
189:
190: /**
191: * Set the xml result file
192: *
193: * @param out
194: * xml result file
195: */
196: public void setOut(File out) throws TransformerException {
197: this .out = out;
198: if (out != null) {
199: menuSave.setEnabled(true);
200: buttonSave.setEnabled(true);
201: }
202: }
203: }
|