001: package org.dbbrowser.ui.panel.dbbrowserwindow;
002:
003: import infrastructure.internationalization.InternationalizationManager;
004: import infrastructure.propertymanager.PropertyManager;
005: import java.awt.event.ActionEvent;
006: import java.awt.event.ActionListener;
007: import java.util.ArrayList;
008: import java.util.List;
009: import javax.swing.*;
010: import javax.swing.table.AbstractTableModel;
011: import org.dbbrowser.db.engine.exception.DBEngineException;
012: import org.dbbrowser.db.engine.model.ColumnInfo;
013: import org.dbbrowser.db.engine.model.DBTable;
014: import org.dbbrowser.ui.AddNewColumnWindow;
015: import org.dbbrowser.ui.UIControllerForQueries;
016: import org.dbbrowser.ui.UIControllerForUpdates;
017: import org.dbbrowser.ui.panel.ButtonsPanel;
018: import org.dbbrowser.ui.widget.Button;
019: import org.dbbrowser.ui.widget.Table;
020:
021: public class ColumnDetailsPanel extends JPanel implements
022: ActionListener {
023: private static final long serialVersionUID = UIControllerForQueries.version;
024:
025: private String addNewRecordIconFileName = PropertyManager
026: .getInstance()
027: .getProperty(
028: "dbbrowser-ui-dbbrowser-window-add-new-record-icon");
029: private String deleteRecordIconFileName = PropertyManager
030: .getInstance().getProperty(
031: "dbbrowser-ui-dbbrowser-window-delete-record-icon");
032:
033: private Icon iconForAddNewRecordButton = new ImageIcon(
034: addNewRecordIconFileName);
035: private Icon iconForDeleteRecordButton = new ImageIcon(
036: deleteRecordIconFileName);
037:
038: private static final String TITLE = InternationalizationManager
039: .getInstance().getMessage("dbbrowser-ui",
040: "dbbrowser-ui-dbbrowser-window-title-label", null);;
041: private static final String ADD_NEW_COLUMN_BUTTON_LABEL = InternationalizationManager
042: .getInstance()
043: .getMessage(
044: "dbbrowser-ui",
045: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-add-new-column-button-label",
046: null);;
047: private static final String REMOVE_COLUMN_BUTTON_LABEL = InternationalizationManager
048: .getInstance()
049: .getMessage(
050: "dbbrowser-ui",
051: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-remove-column-button-label",
052: null);;
053:
054: private UIControllerForQueries uiControllerForQueries = null;
055: private UIControllerForUpdates uiControllerForUpdates = null;
056:
057: private List listOfColumnInfos = null;
058: private Table tableForColumnInfos = null;
059: private ButtonsPanel buttonsPanel = null;
060:
061: private DBTable dbTableOfColumnDetails = null;
062:
063: public ColumnDetailsPanel(
064: UIControllerForQueries uiControllerForQueries,
065: UIControllerForUpdates uiControllerForUpdates) {
066: this .uiControllerForQueries = uiControllerForQueries;
067: this .uiControllerForUpdates = uiControllerForUpdates;
068: }
069:
070: private void initialize() {
071: //Set the table for data
072: this .setLayout(new BoxLayout(this , BoxLayout.PAGE_AXIS));
073:
074: //Add the table
075: this .tableForColumnInfos = new Table(
076: this .uiControllerForQueries,
077: this .uiControllerForUpdates);
078: this .add(tableForColumnInfos);
079:
080: //Set the buttons panel
081: Button addNewColumnButton = new Button(
082: ADD_NEW_COLUMN_BUTTON_LABEL, this ,
083: ADD_NEW_COLUMN_BUTTON_LABEL, iconForAddNewRecordButton,
084: Boolean.TRUE);
085: Button removeColumnButton = new Button(
086: REMOVE_COLUMN_BUTTON_LABEL, this ,
087: REMOVE_COLUMN_BUTTON_LABEL, iconForDeleteRecordButton,
088: Boolean.FALSE);
089: List listOfButtons = new ArrayList();
090: listOfButtons.add(addNewColumnButton);
091: listOfButtons.add(removeColumnButton);
092: buttonsPanel = new ButtonsPanel(listOfButtons);
093:
094: this .add(buttonsPanel);
095: }
096:
097: public void initializeTable(DBTable dbTableOfColumnDetails) {
098: //If the buttons panel has not been set, set it up
099: if (this .tableForColumnInfos == null) {
100: initialize();
101: }
102:
103: //Initialize table
104: this .dbTableOfColumnDetails = dbTableOfColumnDetails;
105:
106: //Set the title of the panel
107: this .tableForColumnInfos.setBorder(BorderFactory
108: .createTitledBorder(dbTableOfColumnDetails
109: .getTableName()));
110:
111: //Build the table
112: this .listOfColumnInfos = dbTableOfColumnDetails
113: .getListOfColumnInfos();
114: this .tableForColumnInfos
115: .initializeTable(new ColumnInfoTableModel());
116: }
117:
118: public void actionPerformed(ActionEvent e) {
119: //Add a new column
120: if (ADD_NEW_COLUMN_BUTTON_LABEL.equals(e.getActionCommand())) {
121: AddNewColumnWindow addNewColumnWindow = new AddNewColumnWindow(
122: this .uiControllerForUpdates,
123: this .dbTableOfColumnDetails);
124: addNewColumnWindow.show();
125: }
126:
127: //Remove the selected column
128: if (REMOVE_COLUMN_BUTTON_LABEL.equals(e.getActionCommand())) {
129: if ((this .tableForColumnInfos.getSelectedRow() != null)
130: && (this .tableForColumnInfos.getSelectedRow()
131: .intValue() != -1)) {
132: String removeColumnConfirmationMessage = InternationalizationManager
133: .getInstance()
134: .getMessage(
135: "dbbrowser-ui",
136: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-remove-column-confirmation-message",
137: null);
138: ;
139: int ans = JOptionPane.showConfirmDialog(null,
140: removeColumnConfirmationMessage, TITLE,
141: JOptionPane.OK_CANCEL_OPTION,
142: JOptionPane.QUESTION_MESSAGE);
143:
144: if (ans == 0) {
145: try {
146: //Get the selected column
147: int selectedRow = this .tableForColumnInfos
148: .getSelectedRow().intValue();
149:
150: if (selectedRow != -1) {
151: //Drop the column
152: ColumnInfo ci = (ColumnInfo) this .listOfColumnInfos
153: .get(selectedRow);
154: this .uiControllerForUpdates.dropColumn(
155: dbTableOfColumnDetails
156: .getSchemaName(),
157: dbTableOfColumnDetails
158: .getTableName(), ci);
159:
160: //Update the UI
161: this .listOfColumnInfos
162: .remove(this .tableForColumnInfos
163: .getSelectedRow());
164: this .tableForColumnInfos.update();
165: }
166: } catch (DBEngineException exc) {
167: String errorMessage = InternationalizationManager
168: .getInstance()
169: .getMessage(
170: "dbbrowser-ui",
171: "dbbrowser-ui-dbbrowser-window-sql-failed",
172: null);
173: JOptionPane
174: .showMessageDialog(null, errorMessage
175: + " - " + exc.getMessage(),
176: TITLE,
177: JOptionPane.ERROR_MESSAGE);
178: }
179: }
180: } else {
181: //No column has been selected
182: String errorMessage = InternationalizationManager
183: .getInstance()
184: .getMessage(
185: "dbbrowser-ui",
186: "dbbrowser-ui-dbbrowser-browser-tab-column-details-delete-column-no-column-selected-message",
187: null);
188: JOptionPane.showMessageDialog(null, errorMessage,
189: TITLE, JOptionPane.ERROR_MESSAGE);
190: }
191: }
192: }
193:
194: /**
195: * Private class representing the model for columns in the table
196: * @author amangat
197: */
198: private class ColumnInfoTableModel extends AbstractTableModel {
199: private static final long serialVersionUID = 1l;
200:
201: private String COLUMN_NULLABLE_LABEL = InternationalizationManager
202: .getInstance()
203: .getMessage(
204: "dbbrowser-ui",
205: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-nullable-true",
206: null);;
207: private String COLUMN_NOT_NULLABLE_LABEL = InternationalizationManager
208: .getInstance()
209: .getMessage(
210: "dbbrowser-ui",
211: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-nullable-false",
212: null);;
213: private String COLUMN_NULLABLE_NATURE_UNKNOWN = InternationalizationManager
214: .getInstance()
215: .getMessage(
216: "dbbrowser-ui",
217: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-nullable-unknown",
218: null);;
219:
220: private String COLUMN_NAME_STRING_LABEL = InternationalizationManager
221: .getInstance()
222: .getMessage(
223: "dbbrowser-ui",
224: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-column-name",
225: null);;
226: private String DATA_TYPE_STRING_LABEL = InternationalizationManager
227: .getInstance()
228: .getMessage(
229: "dbbrowser-ui",
230: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-data-type",
231: null);;
232: private String COLUMN_SIZE_STRING_LABEL = InternationalizationManager
233: .getInstance()
234: .getMessage(
235: "dbbrowser-ui",
236: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-size",
237: null);;
238: private String NULLABLE_STRING_LABEL = InternationalizationManager
239: .getInstance()
240: .getMessage(
241: "dbbrowser-ui",
242: "dbbrowser-ui-dbbrowser-browser-tab-column-details-panel-nullable",
243: null);;
244:
245: private String[] listOfColumnInfoHeaders = new String[] {
246: " ", COLUMN_NAME_STRING_LABEL,
247: DATA_TYPE_STRING_LABEL, COLUMN_SIZE_STRING_LABEL,
248: NULLABLE_STRING_LABEL };
249:
250: public String getColumnName(int col) {
251: return listOfColumnInfoHeaders[col];
252: }
253:
254: public int getRowCount() {
255: return listOfColumnInfos.size();
256: }
257:
258: public int getColumnCount() {
259: return listOfColumnInfoHeaders.length;
260: }
261:
262: public Object getValueAt(int row, int col) {
263: String cellValue = null;
264:
265: if (col == 0) {
266: return "" + (row + 1);
267: } else {
268: ColumnInfo columnInfo = (ColumnInfo) listOfColumnInfos
269: .get(row);
270:
271: switch (col) {
272: case 1: {
273: cellValue = columnInfo.getColumnName();
274: break;
275: }
276: case 2: {
277: cellValue = columnInfo.getColumnTypeName();
278: break;
279: }
280: case 3: {
281: cellValue = columnInfo.getColumnDisplaySize()
282: .intValue()
283: + "";
284: break;
285: }
286: case 4: {
287: String nullableNature = columnInfo
288: .getNullableNature();
289: if (ColumnInfo.COLUMN_NULLABLE
290: .equals(nullableNature)) {
291: cellValue = COLUMN_NULLABLE_LABEL;
292: }
293: if (ColumnInfo.COLUMN_NOT_NULLABLE
294: .equals(nullableNature)) {
295: cellValue = COLUMN_NOT_NULLABLE_LABEL;
296: }
297: if (ColumnInfo.COLUMN_NULLABLE_NATURE_UNKNOWN
298: .equals(nullableNature)) {
299: cellValue = COLUMN_NULLABLE_NATURE_UNKNOWN;
300: }
301:
302: break;
303: }
304: }
305: }
306:
307: return cellValue;
308: }
309: }
310:
311: }
|