001: package org.dbbrowser.ui;
002:
003: import infrastructure.internationalization.InternationalizationManager;
004: import infrastructure.logging.Log;
005: import infrastructure.propertymanager.PropertyManager;
006: import java.awt.*;
007: import java.awt.event.ActionEvent;
008: import java.awt.event.ActionListener;
009: import java.util.*;
010: import java.util.List;
011: import javax.swing.*;
012: import org.dbbrowser.db.engine.exception.DBEngineException;
013: import org.dbbrowser.db.engine.model.ColumnInfo;
014: import org.dbbrowser.db.engine.model.DBRow;
015: import org.dbbrowser.db.engine.model.DBTable;
016: import org.dbbrowser.db.engine.model.DBTableCell;
017: import org.dbbrowser.ui.panel.ButtonsPanel;
018: import org.dbbrowser.ui.widget.Button;
019: import org.dbbrowser.ui.helper.ColumnInfoComparator;
020:
021: public class AddNewRecordWindow implements ActionListener {
022: private static final long serialVersionUID = UIControllerForQueries.version;
023:
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_RECORD_BUTTON_LABEL = InternationalizationManager
029: .getInstance()
030: .getMessage(
031: "dbbrowser-ui",
032: "dbbrowser-ui-dbbrowser-browser-add-new-record-window-add-record-button-label",
033: null);
034: private static final String COMMIT_BUTTON_LABEL = InternationalizationManager
035: .getInstance()
036: .getMessage(
037: "dbbrowser-ui",
038: "dbbrowser-ui-dbbrowser-sql-tab-commit-button-label",
039: null);
040: private static final String ROLLBACK_BUTTON_LABEL = InternationalizationManager
041: .getInstance()
042: .getMessage(
043: "dbbrowser-ui",
044: "dbbrowser-ui-dbbrowser-sql-tab-rollback-button-label",
045: null);
046:
047: private static final String ADD_NEW_RECORD_ICON_FILENAME = PropertyManager
048: .getInstance()
049: .getProperty(
050: "dbbrowser-ui-dbbrowser-window-add-new-record-icon");
051: private static final String COMMIT_BUTTON_ICON_FILENAME = PropertyManager
052: .getInstance()
053: .getProperty(
054: "dbbrowser-ui-dbbrowser-window-sql-tab-commit-button-icon");
055: private static final String ROLLBACK_BUTTON_ICON_FILENAME = PropertyManager
056: .getInstance()
057: .getProperty(
058: "dbbrowser-ui-dbbrowser-window-sql-tab-rollback-button-icon");
059:
060: private JPanel mainPanel = new JPanel();
061: private JDialog dialog = null;
062: private UIControllerForUpdates uiControllerForUpdates = null;
063: private DBTable dbTable = null;
064: private JPanel panelForData = new JPanel();
065: private JPanel panelForScrollPane = new JPanel();
066: private Map mapOfTextFieldToColumnInfos = new HashMap();
067:
068: public AddNewRecordWindow(
069: UIControllerForUpdates uiControllerForUpdates,
070: DBTable dbTable) {
071: this .uiControllerForUpdates = uiControllerForUpdates;
072: this .dbTable = dbTable;
073: initialize();
074: }
075:
076: private void initialize() {
077: //Setup the dialog
078: this .dialog = new JDialog();
079: this .dialog.setTitle(TITLE);
080: this .dialog.setModal(true);
081:
082: //Setup the main panel
083: this .panelForScrollPane.setLayout(new BoxLayout(
084: this .panelForScrollPane, BoxLayout.PAGE_AXIS));
085: this .panelForScrollPane.setBorder(BorderFactory
086: .createTitledBorder(this .dbTable.getTableName()));
087: this .mainPanel.setLayout(new BoxLayout(this .mainPanel,
088: BoxLayout.PAGE_AXIS));
089: JScrollPane pane = new JScrollPane(this .panelForData);
090: this .panelForScrollPane.add(pane);
091: this .mainPanel.add(this .panelForScrollPane);
092:
093: //Setup the frame
094: this .dialog.setSize(700, 500);
095: this .dialog.setLocationRelativeTo(null);
096: this .dialog.getContentPane().add(this .mainPanel);
097:
098: //Get the list of column infos
099: List listOfColumnInfos = this .dbTable.getListOfColumnInfos();
100:
101: //Sort the list of DBTable Cells
102: Collections.sort(listOfColumnInfos, new ColumnInfoComparator());
103:
104: //Iterate through the list of table cells
105: Iterator i = listOfColumnInfos.iterator();
106: while (i.hasNext()) {
107: ColumnInfo columnInfo = (ColumnInfo) i.next();
108: String labelText = columnInfo.getColumnName();
109: JTextField textFieldForData = new JTextField(20);
110:
111: //if it is not editable, disable it
112: if (!columnInfo.isEditable().booleanValue()) {
113: textFieldForData.setEnabled(false);
114: }
115:
116: //if it is primary key, mark it as such
117: if (columnInfo.isPrimaryKeyColumn().booleanValue()) {
118: labelText = labelText + "(PK)";
119: }
120:
121: JLabel labelForData = new JLabel(labelText);
122: JLabel labelForDataType = null;
123:
124: if (ColumnInfo.COLUMN_TYPE_DATE.equals(columnInfo
125: .getColumnTypeName())
126: || ColumnInfo.COLUMN_TYPE_DATE_TIME
127: .equals(columnInfo.getColumnTypeName())) {
128: labelForDataType = new JLabel(columnInfo
129: .getColumnTypeName()
130: + " - "
131: + columnInfo.getNullableNature()
132: + " - " + DBTableCell.DATE_FORMAT_STRING);
133: } else {
134: labelForDataType = new JLabel(columnInfo
135: .getColumnTypeName()
136: + " - " + columnInfo.getNullableNature());
137: }
138:
139: //Add to map
140: this .mapOfTextFieldToColumnInfos.put(textFieldForData,
141: columnInfo);
142:
143: //Add the label to the panel for labels
144: this .panelForData.add(labelForData);
145: this .panelForData.add(labelForDataType);
146: this .panelForData.add(textFieldForData);
147: }
148:
149: //Set the layout on the panel
150: this .panelForData.setLayout(new GridLayout(listOfColumnInfos
151: .size(), 3));
152:
153: //Setup the panel for the add new record button
154: List listOfButtonsInAddNewRecordPanel = new ArrayList();
155: Button addNewRecordButton = new Button(
156: ADD_NEW_RECORD_BUTTON_LABEL, this ,
157: ADD_NEW_RECORD_BUTTON_LABEL, new ImageIcon(
158: ADD_NEW_RECORD_ICON_FILENAME), Boolean.TRUE);
159: listOfButtonsInAddNewRecordPanel.add(addNewRecordButton);
160: Button commitButton = new Button(COMMIT_BUTTON_LABEL, this ,
161: COMMIT_BUTTON_LABEL, new ImageIcon(
162: COMMIT_BUTTON_ICON_FILENAME), Boolean.TRUE);
163: listOfButtonsInAddNewRecordPanel.add(commitButton);
164: Button rollbackButton = new Button(ROLLBACK_BUTTON_LABEL, this ,
165: ROLLBACK_BUTTON_LABEL, new ImageIcon(
166: ROLLBACK_BUTTON_ICON_FILENAME), Boolean.TRUE);
167: listOfButtonsInAddNewRecordPanel.add(rollbackButton);
168:
169: ButtonsPanel addNewRecordButtonPanel = new ButtonsPanel(
170: listOfButtonsInAddNewRecordPanel);
171:
172: //If autocommit is on, disable all buttons
173: String autoCommitFlag = PropertyManager.getInstance()
174: .getProperty("dbbrowser-auto-commit");
175: if ("true".equals(autoCommitFlag)) {
176: //Disable the commit and rollback buttons
177: addNewRecordButtonPanel.enableButton(COMMIT_BUTTON_LABEL,
178: Boolean.FALSE);
179: addNewRecordButtonPanel.enableButton(ROLLBACK_BUTTON_LABEL,
180: Boolean.FALSE);
181: }
182:
183: //Add the panel to the main panel
184: this .mainPanel.add(addNewRecordButtonPanel);
185: }
186:
187: public void show() {
188: this .dialog.setVisible(true);
189: }
190:
191: public void actionPerformed(ActionEvent e) {
192: if (ADD_NEW_RECORD_BUTTON_LABEL.equals(e.getActionCommand())) {
193: //Get all the text fields
194: Iterator i = this .mapOfTextFieldToColumnInfos.keySet()
195: .iterator();
196: List listOfNewValues = new ArrayList();
197:
198: while (i.hasNext()) {
199: JTextField textField = (JTextField) i.next();
200:
201: //Get the new value typed by the user
202: String valueToAdd = textField.getText();
203:
204: //If the user has typed a value, then build a table cell
205: if ((valueToAdd != null) && (!valueToAdd.equals(""))) {
206: //Get the column info from the map
207: ColumnInfo ci = (ColumnInfo) this .mapOfTextFieldToColumnInfos
208: .get(textField);
209: //Set the new value
210: DBTableCell newTableCell = new DBTableCell(ci,
211: valueToAdd, Boolean.TRUE);
212: listOfNewValues.add(newTableCell);
213: }
214: }
215:
216: //Build the dbrow which needs to be updated
217: DBRow updatedDBRow = new DBRow(listOfNewValues);
218:
219: //Add the DBRow
220: try {
221: if (this .uiControllerForUpdates != null) {
222: //Set the auto commit flag
223: String autoCommitFlag = PropertyManager
224: .getInstance().getProperty(
225: "dbbrowser-auto-commit");
226: if ("false".equals(autoCommitFlag)) {
227: this .uiControllerForUpdates
228: .setAutoCommit(false);
229: }
230:
231: //Add the row
232: this .uiControllerForUpdates.addNewRow(this .dbTable
233: .getSchemaName(), this .dbTable
234: .getTableName(), updatedDBRow);
235: }
236: } catch (DBEngineException exc) {
237: Log.getInstance().fatalMessage(exc.getMessage(),
238: AddNewRecordWindow.class.getName());
239: String sqlFailedLabel = InternationalizationManager
240: .getInstance()
241: .getMessage(
242: "dbbrowser-ui",
243: "dbbrowser-ui-dbbrowser-window-sql-failed",
244: null);
245: JOptionPane.showMessageDialog(null, sqlFailedLabel
246: + " - " + exc.getMessage(), TITLE,
247: JOptionPane.ERROR_MESSAGE);
248: }
249: }
250:
251: if (COMMIT_BUTTON_LABEL.equals(e.getActionCommand())) {
252: try {
253: this .uiControllerForUpdates.commit();
254: this .dialog.pack();
255: this .dialog.dispose();
256: this .dialog = null;
257: } catch (DBEngineException exc) {
258: Log.getInstance().fatalMessage(exc.getMessage(),
259: AddNewRecordWindow.class.getName());
260: String sqlFailedLabel = InternationalizationManager
261: .getInstance()
262: .getMessage(
263: "dbbrowser-ui",
264: "dbbrowser-ui-dbbrowser-window-sql-failed",
265: null);
266: JOptionPane.showMessageDialog(null, sqlFailedLabel
267: + " - " + exc.getMessage(), TITLE,
268: JOptionPane.ERROR_MESSAGE);
269: }
270: }
271:
272: if (ROLLBACK_BUTTON_LABEL.equals(e.getActionCommand())) {
273: try {
274: this .uiControllerForUpdates.rollback();
275: this .dialog.pack();
276: this .dialog.dispose();
277: this .dialog = null;
278: } catch (DBEngineException exc) {
279: Log.getInstance().fatalMessage(exc.getMessage(),
280: AddNewRecordWindow.class.getName());
281: String sqlFailedLabel = InternationalizationManager
282: .getInstance()
283: .getMessage(
284: "dbbrowser-ui",
285: "dbbrowser-ui-dbbrowser-window-sql-failed",
286: null);
287: JOptionPane.showMessageDialog(null, sqlFailedLabel
288: + " - " + exc.getMessage(), TITLE,
289: JOptionPane.ERROR_MESSAGE);
290: }
291: }
292:
293: //Update the main panel
294: SwingUtilities.updateComponentTreeUI(this.mainPanel);
295: }
296: }
|