001: package org.dbbrowser.ui;
002:
003: import java.awt.GridLayout;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006: import java.util.ArrayList;
007: import java.util.List;
008: import javax.swing.BoxLayout;
009: import javax.swing.JCheckBox;
010: import javax.swing.JComboBox;
011: import javax.swing.JDialog;
012: import javax.swing.JLabel;
013: import javax.swing.JOptionPane;
014: import javax.swing.JPanel;
015: import javax.swing.JTextField;
016: import org.dbbrowser.db.engine.exception.DBEngineException;
017: import org.dbbrowser.db.engine.model.ColumnInfo;
018: import org.dbbrowser.db.engine.model.DBTable;
019: import org.dbbrowser.ui.panel.ButtonsPanel;
020: import org.dbbrowser.ui.widget.Button;
021: import infrastructure.internationalization.InternationalizationManager;
022:
023: public class AddNewColumnWindow implements ActionListener {
024: private static final String TITLE = InternationalizationManager
025: .getInstance().getMessage("dbbrowser-ui",
026: "dbbrowser-ui-dbbrowser-window-title-label", null);;
027:
028: private String ADD_NEW_COLUMN_LABEL = InternationalizationManager
029: .getInstance().getMessage("dbbrowser-ui",
030: "dbbrowser-ui-add-new-record-window-add-label",
031: null);
032: private UIControllerForUpdates uiControllerForUpdates = null;
033: private DBTable dbTable = null;
034:
035: private JPanel mainPanel = new JPanel();
036: private JDialog dialog = null;
037: private ButtonsPanel buttonsPanel = null;
038:
039: private JPanel panelForData = new JPanel();
040: private JTextField fieldForColumnName = new JTextField();
041: private JTextField fieldForColumnSize = new JTextField();
042: private JComboBox comboBoxForDataType = null;
043: private JCheckBox checkBoxForNullableNature = new JCheckBox();
044:
045: public AddNewColumnWindow(
046: UIControllerForUpdates uiControllerForUpdates,
047: DBTable dbTable) {
048: this .uiControllerForUpdates = uiControllerForUpdates;
049: this .dbTable = dbTable;
050:
051: initialize();
052: }
053:
054: public void actionPerformed(ActionEvent e) {
055: if (ADD_NEW_COLUMN_LABEL.equals(e.getActionCommand())) {
056: //Get the column type name
057: String columnTypeName = this .comboBoxForDataType
058: .getSelectedItem().toString();
059:
060: //Validate the values entered by the user
061: String columnName = this .fieldForColumnName.getText();
062: if (columnName == null || columnName.length() == 0) {
063: String COLUMN_NAME_INCORRECT_MESSAGE = InternationalizationManager
064: .getInstance()
065: .getMessage(
066: "dbbrowser-ui",
067: "dbbrowser-ui-add-new-record-window-wrong-column-name-error-message-label",
068: null);
069: JOptionPane.showMessageDialog(null,
070: COLUMN_NAME_INCORRECT_MESSAGE, TITLE,
071: JOptionPane.ERROR_MESSAGE);
072: } else {
073: String columnDisplaySize = this .fieldForColumnSize
074: .getText();
075: if (columnDisplaySize == null
076: || columnDisplaySize.length() == 0) {
077: String COLUMN_SIZE_INCORRECT_MESSAGE = InternationalizationManager
078: .getInstance()
079: .getMessage(
080: "dbbrowser-ui",
081: "dbbrowser-ui-add-new-record-window-wrong-column-size-error-message-label",
082: null);
083: JOptionPane.showMessageDialog(null,
084: COLUMN_SIZE_INCORRECT_MESSAGE, TITLE,
085: JOptionPane.ERROR_MESSAGE);
086: } else {
087: try {
088: int size = Integer.parseInt(columnDisplaySize);
089:
090: //Validation complete
091:
092: //Get nullable nature
093: boolean nullable = this .checkBoxForNullableNature
094: .isSelected();
095: String nullableString = ColumnInfo.COLUMN_NOT_NULLABLE;
096: if (nullable) {
097: nullableString = ColumnInfo.COLUMN_NULLABLE;
098: }
099:
100: //Build the column info
101: ColumnInfo ci = new ColumnInfo(columnName,
102: columnTypeName, null,
103: new Integer(size), nullableString,
104: Boolean.FALSE, Boolean.FALSE,
105: Boolean.FALSE, null);
106:
107: //Add the column
108: try {
109: this .uiControllerForUpdates.addNewColumn(
110: this .dbTable.getSchemaName(),
111: this .dbTable.getTableName(), ci);
112: this .dialog.pack();
113: this .dialog.dispose();
114: this .dialog = null;
115: } catch (DBEngineException exc) {
116: String errorMessage = InternationalizationManager
117: .getInstance()
118: .getMessage(
119: "dbbrowser-ui",
120: "dbbrowser-ui-dbbrowser-window-sql-failed",
121: null);
122: JOptionPane.showMessageDialog(null,
123: errorMessage + " - "
124: + exc.getMessage(), TITLE,
125: JOptionPane.ERROR_MESSAGE);
126: }
127: } catch (NumberFormatException exc) {
128: String COLUMN_SIZE_INCORRECT_MESSAGE = InternationalizationManager
129: .getInstance()
130: .getMessage(
131: "dbbrowser-ui",
132: "dbbrowser-ui-add-new-record-window-wrong-column-size-error-message-label",
133: null);
134: JOptionPane.showMessageDialog(null,
135: COLUMN_SIZE_INCORRECT_MESSAGE, TITLE,
136: JOptionPane.ERROR_MESSAGE);
137: }
138: }
139: }
140: }
141: }
142:
143: public void show() {
144: this .dialog.setVisible(true);
145: }
146:
147: private void initialize() {
148: //Setup the dialog
149: this .dialog = new JDialog();
150: this .dialog.setTitle(TITLE);
151: this .dialog.setModal(true);
152:
153: //Setup the frame
154: this .dialog.setSize(500, 200);
155: this .dialog.setLocationRelativeTo(null);
156: this .dialog.getContentPane().add(this .mainPanel);
157:
158: //Build the list of buttons
159: List listOfButtons = new ArrayList();
160: Button addNewColumnButton = new Button(ADD_NEW_COLUMN_LABEL,
161: this , ADD_NEW_COLUMN_LABEL, null, Boolean.FALSE);
162: listOfButtons.add(addNewColumnButton);
163:
164: //Setup the navigation panel
165: buttonsPanel = new ButtonsPanel(listOfButtons);
166:
167: //Set up the textfields and labels
168: this .panelForData.setLayout(new GridLayout(4, 2));
169:
170: String COLUMN_NAME_LABEL = InternationalizationManager
171: .getInstance()
172: .getMessage(
173: "dbbrowser-ui",
174: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-column-name",
175: null);
176: String COLUMN_SIZE_LABEL = InternationalizationManager
177: .getInstance()
178: .getMessage(
179: "dbbrowser-ui",
180: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-size",
181: null);
182: String COLUMN_TYPE_LABEL = InternationalizationManager
183: .getInstance()
184: .getMessage(
185: "dbbrowser-ui",
186: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-data-type",
187: null);
188: String COLUMN__NULLABLE_LABEL = InternationalizationManager
189: .getInstance()
190: .getMessage(
191: "dbbrowser-ui",
192: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-nullable",
193: null);
194:
195: JLabel labelForColumnName = new JLabel(COLUMN_NAME_LABEL);
196: JLabel labelForColumnSize = new JLabel(COLUMN_SIZE_LABEL);
197: JLabel labelForColumnType = new JLabel(COLUMN_TYPE_LABEL);
198: JLabel labelForNullableNature = new JLabel(
199: COLUMN__NULLABLE_LABEL);
200:
201: //Build the list of column types
202: String[] dataTypes = new String[] {
203: ColumnInfo.COLUMN_TYPE_VARCHAR,
204: ColumnInfo.COLUMN_TYPE_DATE,
205: ColumnInfo.COLUMN_TYPE_DATE_TIME,
206: ColumnInfo.COLUMN_TYPE_NUMBER };
207: this .comboBoxForDataType = new JComboBox(dataTypes);
208: this .comboBoxForDataType.setEditable(false);
209:
210: this .panelForData.add(labelForColumnName);
211: this .panelForData.add(fieldForColumnName);
212: this .panelForData.add(labelForColumnSize);
213: this .panelForData.add(fieldForColumnSize);
214: this .panelForData.add(labelForColumnType);
215: this .panelForData.add(comboBoxForDataType);
216: this .panelForData.add(labelForNullableNature);
217: this .panelForData.add(checkBoxForNullableNature);
218:
219: //Add the buttons panel
220: this .mainPanel.setLayout(new BoxLayout(this.mainPanel,
221: BoxLayout.PAGE_AXIS));
222: this.mainPanel.add(this.panelForData);
223: this.mainPanel.add(this.buttonsPanel);
224: }
225: }
|