001: package org.jsqltool.gui;
002:
003: import java.awt.*;
004: import javax.swing.*;
005: import java.awt.event.*;
006: import org.jsqltool.utils.Options;
007: import java.text.SimpleDateFormat;
008: import java.io.*;
009: import java.util.Properties;
010:
011: import org.jsqltool.utils.Options;
012: import org.jsqltool.utils.ImageLoader;
013:
014: /**
015: * <p>Title: JSqlTool Project</p>
016: * <p>Description: Dialog used to set application properties.
017: * </p>
018: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
019: *
020: * <p> This file is part of JSqlTool project.
021: * This library is free software; you can redistribute it and/or
022: * modify it under the terms of the (LGPL) Lesser General Public
023: * License as published by the Free Software Foundation;
024: *
025: * GNU LESSER GENERAL PUBLIC LICENSE
026: * Version 2.1, February 1999
027: *
028: * This library is distributed in the hope that it will be useful,
029: * but WITHOUT ANY WARRANTY; without even the implied warranty of
030: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
031: * Library General Public License for more details.
032: *
033: * You should have received a copy of the GNU Library General Public
034: * License along with this library; if not, write to the Free
035: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
036: *
037: * The author may be contacted at:
038: * maurocarniel@tin.it</p>
039: *
040: * @author Mauro Carniel
041: * @version 1.0
042: */
043: public class OptionsDialog extends JDialog {
044:
045: JPanel mainPanel = new JPanel();
046: JPanel buttonsPanel = new JPanel();
047: JPanel centerPanel = new JPanel();
048: JButton cancelButton = new JButton();
049: JButton okButton = new JButton();
050: GridBagLayout gridBagLayout1 = new GridBagLayout();
051: JLabel dateFormatLabel = new JLabel();
052: JTextField dateFormatTF = new JTextField();
053: JLabel planTableLabel = new JLabel();
054: JTextField planTableTF = new JTextField();
055: GridBagLayout gridBagLayout2 = new GridBagLayout();
056: MainFrame frame = null;
057: JLabel updNoPkLabel = new JLabel();
058: JCheckBox updNoPK = new JCheckBox();
059: JLabel languageLabel = new JLabel();
060: JTextField languageTF = new JTextField();
061:
062: public OptionsDialog(MainFrame frame) {
063: super (frame, Options.getInstance().getResource("options"), true);
064: this .frame = frame;
065: try {
066: jbInit();
067: pack();
068: init();
069: setSize(500, 220);
070: Dimension screenSize = Toolkit.getDefaultToolkit()
071: .getScreenSize();
072: Dimension frameSize = this .getSize();
073: if (frameSize.height > screenSize.height) {
074: frameSize.height = screenSize.height;
075: }
076: if (frameSize.width > screenSize.width) {
077: frameSize.width = screenSize.width;
078: }
079: this .setLocation((screenSize.width - frameSize.width) / 2,
080: (screenSize.height - frameSize.height) / 2);
081: setVisible(true);
082: } catch (Exception ex) {
083: ex.printStackTrace();
084: }
085: }
086:
087: public void init() {
088: dateFormatTF.setText(Options.getInstance().getDateFormat());
089: planTableTF.setText(Options.getInstance()
090: .getOracleExplainPlanTable());
091: updNoPK.setSelected(Options.getInstance().isUpdateWhenNoPK());
092: languageTF.setText(Options.getInstance().getLanguage());
093: }
094:
095: public OptionsDialog() {
096: this (null);
097: }
098:
099: private void jbInit() throws Exception {
100: mainPanel.setLayout(gridBagLayout1);
101: cancelButton.setMnemonic(Options.getInstance().getResource(
102: "cancelbutton.mnemonic").charAt(0));
103: cancelButton.setText(Options.getInstance().getResource(
104: "cancelbutton.text"));
105: cancelButton
106: .addActionListener(new OptionsDialog_cancelButton_actionAdapter(
107: this ));
108: okButton.setMnemonic(Options.getInstance().getResource(
109: "okbutton.mnemonic").charAt(0));
110: okButton.setText(Options.getInstance().getResource(
111: "okbutton.text"));
112: okButton
113: .addActionListener(new OptionsDialog_okButton_actionAdapter(
114: this ));
115: buttonsPanel.setBorder(BorderFactory.createEtchedBorder());
116: dateFormatLabel.setText(Options.getInstance().getResource(
117: "date format"));
118: dateFormatTF.setText("");
119: dateFormatTF.setColumns(20);
120: planTableLabel.setText(Options.getInstance().getResource(
121: "oracle plan table name"));
122: planTableTF.setText("");
123: planTableTF.setColumns(20);
124: centerPanel.setLayout(gridBagLayout2);
125: centerPanel.setBorder(BorderFactory.createEtchedBorder());
126: updNoPkLabel.setText(Options.getInstance().getResource(
127: "enable update when no pk found"));
128: languageLabel.setText(Options.getInstance().getResource(
129: "language"));
130: getContentPane().add(mainPanel);
131: mainPanel.add(buttonsPanel, new GridBagConstraints(0, 1, 1, 1,
132: 1.0, 0.0, GridBagConstraints.CENTER,
133: GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
134: mainPanel.add(centerPanel, new GridBagConstraints(0, 0, 1, 1,
135: 1.0, 1.0, GridBagConstraints.CENTER,
136: GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
137: centerPanel.add(dateFormatLabel, new GridBagConstraints(0, 0,
138: 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
139: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
140: buttonsPanel.add(okButton, null);
141: buttonsPanel.add(cancelButton, null);
142: centerPanel.add(dateFormatTF, new GridBagConstraints(1, 0, 1,
143: 1, 1.0, 0.0, GridBagConstraints.WEST,
144: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
145: 0, 0));
146: centerPanel.add(planTableTF, new GridBagConstraints(1, 1, 1, 1,
147: 0.0, 0.0, GridBagConstraints.NORTHWEST,
148: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
149: 0, 0));
150: centerPanel.add(planTableLabel, new GridBagConstraints(0, 1, 1,
151: 1, 0.0, 0.0, GridBagConstraints.NORTHWEST,
152: GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
153: centerPanel.add(updNoPkLabel, new GridBagConstraints(0, 2, 1,
154: 1, 0.0, 0.0, GridBagConstraints.NORTHWEST,
155: GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
156: centerPanel.add(updNoPK, new GridBagConstraints(1, 2, 1, 1,
157: 0.0, 0.0, GridBagConstraints.NORTHWEST,
158: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
159: centerPanel.add(languageLabel, new GridBagConstraints(0, 3, 1,
160: 1, 0.0, 1.0, GridBagConstraints.NORTHWEST,
161: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
162: centerPanel.add(languageTF, new GridBagConstraints(1, 3, 1, 1,
163: 0.0, 0.0, GridBagConstraints.NORTHWEST,
164: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
165: 0, 0));
166: }
167:
168: void okButton_actionPerformed(ActionEvent e) {
169: if (dateFormatTF.getText().length() == 0) {
170: JOptionPane.showMessageDialog(frame, Options.getInstance()
171: .getResource("date format not specified."), Options
172: .getInstance().getResource("attention"),
173: JOptionPane.WARNING_MESSAGE);
174: return;
175: }
176: try {
177: SimpleDateFormat sdf = new SimpleDateFormat(dateFormatTF
178: .getText());
179: } catch (Exception ex) {
180: JOptionPane.showMessageDialog(frame, Options.getInstance()
181: .getResource("invalid date format.\nexample")
182: + ": dd-MM-yyyy hh:mm:ss,SSS", Options
183: .getInstance().getResource("attention"),
184: JOptionPane.WARNING_MESSAGE);
185: return;
186: }
187: if (planTableTF.getText().length() == 0) {
188: JOptionPane.showMessageDialog(frame, Options.getInstance()
189: .getResource("explain plan table not specified."),
190: Options.getInstance().getResource("attention"),
191: JOptionPane.WARNING_MESSAGE);
192: return;
193: }
194: Options.getInstance().setDateFormat(dateFormatTF.getText());
195: Options.getInstance().setOracleExplainPlanTable(
196: planTableTF.getText());
197: Options.getInstance().setUpdateWhenNoPK(updNoPK.isSelected());
198: Options.getInstance().setLanguage(languageTF.getText());
199: try {
200: File profileFile = new File("jsqltool.ini");
201: Properties p = new Properties();
202: p.setProperty("DATE_FORMAT", Options.getInstance()
203: .getDateFormat());
204: p.setProperty("ORACLE_EXPLAIN_PLAN_TABLE", Options
205: .getInstance().getOracleExplainPlanTable());
206: p.setProperty("UPDATE_WHEN_NO_PK", String.valueOf(Options
207: .getInstance().isUpdateWhenNoPK()));
208: p.setProperty("LANGUAGE", Options.getInstance()
209: .getLanguage());
210: p.store(new FileOutputStream(profileFile),
211: "JSQLTOOL Properties");
212: } catch (Exception ex) {
213: ex.printStackTrace();
214: JOptionPane.showMessageDialog(frame, Options.getInstance()
215: .getResource("error while saving")
216: + " jsqltool.ini:\n" + ex.getMessage(), Options
217: .getInstance().getResource("error"),
218: JOptionPane.ERROR_MESSAGE);
219: }
220: setVisible(false);
221: dispose();
222: }
223:
224: void cancelButton_actionPerformed(ActionEvent e) {
225: setVisible(false);
226: dispose();
227: }
228:
229: }
230:
231: class OptionsDialog_okButton_actionAdapter implements
232: java.awt.event.ActionListener {
233: OptionsDialog adaptee;
234:
235: OptionsDialog_okButton_actionAdapter(OptionsDialog adaptee) {
236: this .adaptee = adaptee;
237: }
238:
239: public void actionPerformed(ActionEvent e) {
240: adaptee.okButton_actionPerformed(e);
241: }
242: }
243:
244: class OptionsDialog_cancelButton_actionAdapter implements
245: java.awt.event.ActionListener {
246: OptionsDialog adaptee;
247:
248: OptionsDialog_cancelButton_actionAdapter(OptionsDialog adaptee) {
249: this .adaptee = adaptee;
250: }
251:
252: public void actionPerformed(ActionEvent e) {
253: adaptee.cancelButton_actionPerformed(e);
254: }
255: }
|