001: /*
002: * SettingsPanel.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.settings;
013:
014: import java.awt.BorderLayout;
015: import java.awt.EventQueue;
016: import java.awt.GridBagConstraints;
017: import java.awt.GridBagLayout;
018: import java.awt.Insets;
019: import java.awt.event.ActionListener;
020: import java.util.ArrayList;
021: import java.util.List;
022: import javax.swing.AbstractListModel;
023: import javax.swing.JButton;
024:
025: import javax.swing.JDialog;
026: import javax.swing.JFrame;
027: import javax.swing.JList;
028: import javax.swing.JPanel;
029: import javax.swing.JRootPane;
030: import javax.swing.ListModel;
031: import javax.swing.ListSelectionModel;
032: import javax.swing.WindowConstants;
033: import javax.swing.border.CompoundBorder;
034: import javax.swing.border.EmptyBorder;
035: import javax.swing.border.EtchedBorder;
036: import javax.swing.event.ListSelectionEvent;
037: import javax.swing.event.ListSelectionEvent;
038: import javax.swing.event.ListSelectionListener;
039: import javax.swing.event.ListSelectionListener;
040:
041: import workbench.gui.WbSwingUtilities;
042: import workbench.gui.actions.EscAction;
043: import workbench.gui.components.WbButton;
044: import workbench.gui.help.HelpManager;
045: import workbench.resource.ResourceMgr;
046: import workbench.resource.Settings;
047:
048: /**
049: *
050: * @author sql.workbench@freenet.de
051: */
052: public class SettingsPanel extends JPanel implements ActionListener,
053: ListSelectionListener {
054: private JPanel buttonPanel;
055: private JButton cancelButton;
056: private JButton helpButton;
057: private JPanel content;
058: private JList pageList;
059: private JPanel currentPanel;
060:
061: private JButton okButton;
062:
063: private JDialog dialog;
064: private EscAction escAction;
065: private List<OptionPanelPage> pages;
066:
067: public SettingsPanel() {
068: pages = new ArrayList<OptionPanelPage>();
069: pages.add(new OptionPanelPage("GeneralOptionsPanel",
070: "LblSettingsGeneral"));
071: pages.add(new OptionPanelPage("EditorOptionsPanel",
072: "LblSettingsEditor"));
073: pages.add(new OptionPanelPage("DataFormattingOptionsPanel",
074: "LblSettingsDataFormat"));
075: pages.add(new OptionPanelPage("DataEditOptionsPanel",
076: "LblDataEdit"));
077: pages.add(new OptionPanelPage("DbExplorerOptionsPanel",
078: "LblSettingsDbExplorer"));
079: pages.add(new OptionPanelPage("WindowTitleOptionsPanel",
080: "LblSettingsWinTitle"));
081: pages.add(new OptionPanelPage("FormatterOptionsPanel",
082: "LblSqlFormat"));
083: pages.add(new OptionPanelPage("SqlGenerationOptionsPanel",
084: "LblSqlGeneration"));
085: pages.add(new OptionPanelPage("ExternalToolsPanel",
086: "LblExternalTools"));
087: pages.add(new OptionPanelPage("LnFOptionsPanel",
088: "LblLnFOptions"));
089:
090: initComponents();
091: }
092:
093: public void valueChanged(ListSelectionEvent e) {
094: if (e.getValueIsAdjusting())
095: return;
096:
097: int index = this .pageList.getSelectedIndex();
098:
099: try {
100: WbSwingUtilities.showWaitCursor(this );
101: OptionPanelPage option = pages.get(index);
102: JPanel panel = option.getPanel();
103: if (currentPanel != null) {
104: content.remove(currentPanel);
105: }
106: content.add(panel, BorderLayout.CENTER);
107: content.validate();
108: content.repaint();
109: this .currentPanel = panel;
110: } finally {
111: WbSwingUtilities.showDefaultCursor(this );
112: }
113: }
114:
115: private void initComponents() {
116: ListModel model = new AbstractListModel() {
117: public Object getElementAt(int index) {
118: return pages.get(index);
119: }
120:
121: public int getSize() {
122: return pages.size();
123: }
124: };
125:
126: pageList = new JList(model);
127: pageList.setBorder(new CompoundBorder(new EtchedBorder(),
128: new EmptyBorder(2, 2, 2, 2)));
129: pageList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
130: pageList.addListSelectionListener(this );
131:
132: content = new JPanel(new BorderLayout());
133: content.setBorder(new EmptyBorder(2, 2, 2, 2));
134: content.add(pageList, BorderLayout.WEST);
135:
136: okButton = new WbButton(ResourceMgr.getString("LblOK"));
137: cancelButton = new WbButton(ResourceMgr.getString("LblCancel"));
138: helpButton = new JButton(ResourceMgr.getString("LblHelp"));
139:
140: okButton.addActionListener(this );
141: cancelButton.addActionListener(this );
142: helpButton.addActionListener(this );
143:
144: setLayout(new BorderLayout());
145:
146: buttonPanel = new JPanel(new GridBagLayout());
147:
148: GridBagConstraints constraints;
149: constraints = new GridBagConstraints();
150: constraints.gridx = 0;
151: constraints.gridy = 0;
152: constraints.anchor = GridBagConstraints.WEST;
153: constraints.insets = new Insets(0, 5, 0, 0);
154: buttonPanel.add(helpButton, constraints);
155:
156: constraints = new GridBagConstraints();
157: constraints.gridx = 1;
158: constraints.gridy = 0;
159: constraints.anchor = GridBagConstraints.EAST;
160: constraints.weightx = 1.0;
161: constraints.insets = new Insets(7, 0, 7, 10);
162: buttonPanel.add(okButton, constraints);
163:
164: constraints = new GridBagConstraints();
165: constraints.gridx = 2;
166: constraints.gridy = 0;
167: constraints.anchor = GridBagConstraints.EAST;
168: constraints.insets = new Insets(7, 0, 7, 4);
169: buttonPanel.add(cancelButton, constraints);
170:
171: add(content, BorderLayout.CENTER);
172: add(buttonPanel, BorderLayout.SOUTH);
173: }
174:
175: private void saveSettings() {
176: for (OptionPanelPage page : pages) {
177: page.saveSettings();
178: }
179: }
180:
181: public void showSettingsDialog(JFrame aReference) {
182: this .dialog = new JDialog(aReference, true);
183: this .dialog
184: .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
185: this .dialog.setTitle(ResourceMgr
186: .getString("TxtSettingsDialogTitle"));
187: this .dialog.getContentPane().add(this );
188: int width = Settings.getInstance().getWindowWidth(
189: this .getClass().getName());
190: int height = Settings.getInstance().getWindowHeight(
191: this .getClass().getName());
192: if (width > 0 && height > 0) {
193: this .dialog.setSize(width, height);
194: } else {
195: this .dialog.setSize(600, 480);
196: }
197:
198: this .dialog.getRootPane().setDefaultButton(this .okButton);
199:
200: JRootPane root = dialog.getRootPane();
201: escAction = new EscAction(this );
202: escAction.addToInputMap(root);
203:
204: WbSwingUtilities.center(this .dialog, aReference);
205: EventQueue.invokeLater(new Runnable() {
206: public void run() {
207: pageList.setSelectedIndex(0);
208: pageList.requestFocusInWindow();
209: }
210: });
211: this .dialog.setVisible(true);
212: }
213:
214: private void closeWindow() {
215: Settings.getInstance().setWindowSize(this .getClass().getName(),
216: this .dialog.getWidth(), this .dialog.getHeight());
217: this .dialog.setVisible(false);
218: this .dialog.dispose();
219: this .dialog = null;
220: }
221:
222: public void actionPerformed(java.awt.event.ActionEvent e) {
223: if (e.getSource() == escAction || e.getSource() == cancelButton) {
224: this.closeWindow();
225: } else if (e.getSource() == okButton) {
226: this.saveSettings();
227: this.closeWindow();
228: } else if (e.getSource() == helpButton) {
229: HelpManager.showOptionsHelp();
230: }
231: }
232:
233: }
|