001: package org.dbbrowser.ui;
002:
003: import infrastructure.internationalization.InternationalizationManager;
004: import infrastructure.propertymanager.PropertyManagementException;
005: import infrastructure.propertymanager.PropertyManager;
006: import java.awt.GridLayout;
007: import java.awt.Window;
008: import java.awt.event.ActionEvent;
009: import java.awt.event.ActionListener;
010: import java.util.ArrayList;
011: import java.util.List;
012: import javax.swing.BorderFactory;
013: import javax.swing.BoxLayout;
014: import javax.swing.Icon;
015: import javax.swing.ImageIcon;
016: import javax.swing.JCheckBox;
017: import javax.swing.JLabel;
018: import javax.swing.JOptionPane;
019: import javax.swing.JPanel;
020: import javax.swing.JTextField;
021: import javax.swing.SwingUtilities;
022:
023: import org.dbbrowser.ui.panel.ButtonsPanel;
024: import org.dbbrowser.ui.widget.Button;
025:
026: public class PreferencesPanel extends JPanel implements ActionListener {
027: private static final String TITLE = InternationalizationManager
028: .getInstance().getMessage("dbbrowser-ui",
029: "dbbrowser-ui-dbbrowser-window-title-label", null);
030:
031: private static final String RECORDS_PER_PAGE_LABEL = InternationalizationManager
032: .getInstance()
033: .getMessage(
034: "dbbrowser-ui",
035: "dbbrowser-ui-preferences-window-records-per-page-label",
036: null);
037: private static final String AUTO_COMMIT_LABEL = InternationalizationManager
038: .getInstance()
039: .getMessage(
040: "dbbrowser-ui",
041: "dbbrowser-ui-preferences-window-auto-commit-label",
042: null);
043: private static final String SHOW_SQL_LOG_LABEL = InternationalizationManager
044: .getInstance()
045: .getMessage(
046: "dbbrowser-ui",
047: "dbbrowser-ui-preferences-window-show-sql-log-label",
048: null);
049: private static final String SORT_COLUMNS_LABEL = InternationalizationManager
050: .getInstance().getMessage("dbbrowser-ui",
051: "dbbrowser-ui-preferences-window-sort-columns",
052: null);
053: private static final String SAVE_CHANGES_BUTTON_LABEL = InternationalizationManager
054: .getInstance()
055: .getMessage(
056: "dbbrowser-ui",
057: "dbbrowser-ui-new-connections-details-panel-save-changes-button-label",
058: null);
059: private static final String SAVE_CHANGES_ICON_FILENAME = PropertyManager
060: .getInstance()
061: .getProperty(
062: "dbbrowser-ui-view-record-window-update-record-icon");
063: private static final String SQL_STATEMENT_TERMINATOR = InternationalizationManager
064: .getInstance()
065: .getMessage(
066: "dbbrowser-ui",
067: "dbbrowser-ui-preferences-window-sql-statement-terminator-label",
068: null);
069:
070: private Icon iconForSaveChanges = new ImageIcon(
071: SAVE_CHANGES_ICON_FILENAME);
072: private JTextField textFieldForRecordsPerPage = new JTextField("");
073: private JTextField textFieldForSQLStatementTerminator = new JTextField(
074: "");
075: private JCheckBox checkBoxForAutoCommit = new JCheckBox();
076: private JCheckBox checkBoxForShowSQL = new JCheckBox();
077: private JCheckBox checkBoxForSortColumns = new JCheckBox();
078:
079: public PreferencesPanel() {
080: initialize();
081: }
082:
083: public void actionPerformed(ActionEvent e) {
084: String recordsPerPageString = this .textFieldForRecordsPerPage
085: .getText();
086:
087: try {
088: int recordsPerPage = Integer.parseInt(recordsPerPageString);
089: PropertyManager.getInstance().setProperty(
090: "dbbrowser-ui-browser-window-paging-size",
091: "" + recordsPerPage);
092: PropertyManager.getInstance().setProperty(
093: "dbbrowser-sql-statement-terminator",
094: this .textFieldForSQLStatementTerminator.getText());
095:
096: if (checkBoxForAutoCommit.isSelected()) {
097: PropertyManager.getInstance().setProperty(
098: "dbbrowser-auto-commit", "true");
099: } else {
100: PropertyManager.getInstance().setProperty(
101: "dbbrowser-auto-commit", "false");
102: }
103:
104: if (checkBoxForShowSQL.isSelected()) {
105: PropertyManager.getInstance().setProperty(
106: "dbbrowser-ui-dbbrowser-window-show-sql-log",
107: "true");
108: } else {
109: PropertyManager.getInstance().setProperty(
110: "dbbrowser-ui-dbbrowser-window-show-sql-log",
111: "false");
112: }
113:
114: if (checkBoxForSortColumns.isSelected()) {
115: PropertyManager.getInstance().setProperty(
116: "dbbrowser-ui-sort-columns-in-table", "true");
117: } else {
118: PropertyManager.getInstance().setProperty(
119: "dbbrowser-ui-sort-columns-in-table", "false");
120: }
121: } catch (NumberFormatException exc) {
122: String errorMessage = InternationalizationManager
123: .getInstance()
124: .getMessage(
125: "dbbrowser-ui",
126: "dbbrowser-ui-preferences-window-not-a-number-error-message-label",
127: null);
128: JOptionPane.showMessageDialog(null, errorMessage, TITLE,
129: JOptionPane.ERROR_MESSAGE);
130: } catch (PropertyManagementException exc) {
131: String errorMessage = InternationalizationManager
132: .getInstance()
133: .getMessage(
134: "dbbrowser-ui",
135: "dbbrowser-ui-dbbrowser-window-save-property-failed-message",
136: null);
137: JOptionPane.showMessageDialog(null, errorMessage, TITLE,
138: JOptionPane.ERROR_MESSAGE);
139: }
140:
141: //Close this window
142: Window window = SwingUtilities.getWindowAncestor(this );
143: if (window != null) {
144: window.dispose();
145: window.hide();
146: }
147: }
148:
149: private void initialize() {
150: //Setup the dialog
151: this .setLayout(new BoxLayout(this , BoxLayout.PAGE_AXIS));
152:
153: //Build the widgets
154: JLabel labelForRecordsPerPage = new JLabel(
155: RECORDS_PER_PAGE_LABEL);
156: JLabel labelForAutoCommit = new JLabel(AUTO_COMMIT_LABEL);
157: JLabel labelForShowSQLInLog = new JLabel(SHOW_SQL_LOG_LABEL);
158: JLabel labelForSortColumns = new JLabel(SORT_COLUMNS_LABEL);
159: JLabel labelForSQLStatementTerminator = new JLabel(
160: SQL_STATEMENT_TERMINATOR);
161:
162: JPanel panel = new JPanel();
163: String panelTitle = InternationalizationManager
164: .getInstance()
165: .getMessage(
166: "dbbrowser-ui",
167: "dbbrowser-ui-preferences-window-preferences-panel-title",
168: null);
169: panel.setBorder(BorderFactory.createTitledBorder(panelTitle));
170: panel.setLayout(new GridLayout(5, 2));
171: panel.add(labelForRecordsPerPage);
172: panel.add(textFieldForRecordsPerPage);
173: panel.add(labelForAutoCommit);
174: panel.add(checkBoxForAutoCommit);
175: panel.add(labelForShowSQLInLog);
176: panel.add(checkBoxForShowSQL);
177: panel.add(labelForSortColumns);
178: panel.add(checkBoxForSortColumns);
179: panel.add(labelForSQLStatementTerminator);
180: panel.add(textFieldForSQLStatementTerminator);
181:
182: //Set the values from the properties file
183: textFieldForRecordsPerPage.setText(PropertyManager
184: .getInstance().getProperty(
185: "dbbrowser-ui-browser-window-paging-size"));
186: textFieldForSQLStatementTerminator.setText(PropertyManager
187: .getInstance().getProperty(
188: "dbbrowser-sql-statement-terminator"));
189:
190: String autoCommitFlag = PropertyManager.getInstance()
191: .getProperty("dbbrowser-auto-commit");
192: if ("true".equals(autoCommitFlag)) {
193: checkBoxForAutoCommit.setSelected(true);
194: }
195:
196: String showSQLLog = PropertyManager.getInstance().getProperty(
197: "dbbrowser-ui-dbbrowser-window-show-sql-log");
198: if ("true".equals(showSQLLog)) {
199: checkBoxForShowSQL.setSelected(true);
200: }
201:
202: String sortColumns = PropertyManager.getInstance().getProperty(
203: "dbbrowser-ui-sort-columns-in-table");
204: if ("true".equals(sortColumns)) {
205: checkBoxForSortColumns.setSelected(true);
206: }
207:
208: //Add the panel
209: this .add(panel);
210:
211: //Build the list of buttons for the navigation buttons panel
212: List listOfButtons = new ArrayList();
213: Button firstRecordButton = new Button(
214: SAVE_CHANGES_BUTTON_LABEL, this ,
215: SAVE_CHANGES_BUTTON_LABEL, iconForSaveChanges,
216: Boolean.FALSE);
217: listOfButtons.add(firstRecordButton);
218:
219: //Setup the navigation panel
220: ButtonsPanel butonsPanel = new ButtonsPanel(listOfButtons);
221: this.add(butonsPanel);
222: }
223: }
|