001: package org.dbbrowser.ui.panel.dbbrowserwindow;
002:
003: import infrastructure.internationalization.InternationalizationManager;
004: import infrastructure.logging.Log;
005: import infrastructure.propertymanager.PropertyManager;
006:
007: import java.awt.BorderLayout;
008: import java.awt.Component;
009: import java.awt.event.ActionEvent;
010: import java.awt.event.ActionListener;
011: import java.util.ArrayList;
012: import java.util.List;
013: import javax.swing.BoxLayout;
014: import javax.swing.Icon;
015: import javax.swing.ImageIcon;
016: import javax.swing.JLabel;
017: import javax.swing.JOptionPane;
018: import javax.swing.JPanel;
019: import javax.swing.JTabbedPane;
020: import javax.swing.SwingConstants;
021: import javax.swing.event.ChangeEvent;
022: import javax.swing.event.ChangeListener;
023: import org.dbbrowser.db.engine.exception.DBEngineException;
024: import org.dbbrowser.db.engine.model.DBTable;
025: import org.dbbrowser.drivermanager.ConnectionInfo;
026: import org.dbbrowser.ui.UIControllerForQueries;
027: import org.dbbrowser.ui.UIControllerForUpdates;
028: import org.dbbrowser.ui.helper.DBTableDataTableModel;
029: import org.dbbrowser.ui.panel.ButtonsPanel;
030: import org.dbbrowser.ui.widget.Button;
031: import org.dbbrowser.ui.widget.Table;
032:
033: public class DBConstraintsPanel extends JPanel implements
034: ActionListener, ChangeListener {
035: private static final long serialVersionUID = UIControllerForQueries.version;
036:
037: private String addNewRecordIconFileName = PropertyManager
038: .getInstance()
039: .getProperty(
040: "dbbrowser-ui-dbbrowser-window-add-new-record-icon");
041: private String deleteRecordIconFileName = PropertyManager
042: .getInstance().getProperty(
043: "dbbrowser-ui-dbbrowser-window-delete-record-icon");
044:
045: private Icon iconForAddNewRecordButton = new ImageIcon(
046: addNewRecordIconFileName);
047: private Icon iconForDeleteRecordButton = new ImageIcon(
048: deleteRecordIconFileName);
049:
050: private static final String TITLE = InternationalizationManager
051: .getInstance().getMessage("dbbrowser-ui",
052: "dbbrowser-ui-dbbrowser-window-title-label", null);;
053: private static final String ADD_NEW_CONSTRAINT_BUTTON_LABEL = InternationalizationManager
054: .getInstance()
055: .getMessage(
056: "dbbrowser-ui",
057: "dbbrowser-ui-dbbrowser-constraints-tab-add-new-constraint-button-label",
058: null);;
059: private static final String REMOVE_CONSTRAINT_BUTTON_LABEL = InternationalizationManager
060: .getInstance()
061: .getMessage(
062: "dbbrowser-ui",
063: "dbbrowser-ui-dbbrowser-constraints-tab-remove-constraint-button-label",
064: null);;
065:
066: private UIControllerForQueries uiControllerForQueries = null;
067: private UIControllerForUpdates uiControllerForUpdates = null;
068:
069: private Table tableForConstraints = null;
070: private ButtonsPanel buttonsPanel = null;
071:
072: private boolean constraintsSetupCompleted = false;
073:
074: public DBConstraintsPanel(
075: UIControllerForQueries uiControllerForQueries,
076: UIControllerForUpdates uiControllerForUpdates) {
077: this .uiControllerForQueries = uiControllerForQueries;
078: this .uiControllerForUpdates = uiControllerForUpdates;
079: initialize();
080: }
081:
082: public void refresh() {
083: showConstraints();
084: }
085:
086: public void stateChanged(ChangeEvent e) {
087: //Setup indexes only if this panel has been selected
088: Object source = e.getSource();
089: if ((source != null) && (source instanceof JTabbedPane)) {
090: //Get the selected tab
091: JTabbedPane pane = (JTabbedPane) source;
092: Component selectedComponent = pane.getSelectedComponent();
093: if ((selectedComponent != null)
094: && (selectedComponent instanceof DBConstraintsPanel)) {
095: //If indexes have not been retrieved, retrieve them and cache them
096: if (!constraintsSetupCompleted) {
097: showConstraints();
098: }
099: }
100: }
101: }
102:
103: private void showConstraints() {
104: try {
105: //Remove existing panels
106: if (this .tableForConstraints != null) {
107: this .remove(this .tableForConstraints);
108: this .remove(this .buttonsPanel);
109: }
110:
111: DBTable dbTableOfConstraints = this .uiControllerForQueries
112: .listConstraints();
113:
114: //Add the table
115: this .tableForConstraints = new Table(
116: this .uiControllerForQueries,
117: this .uiControllerForUpdates);
118: this .add(tableForConstraints);
119:
120: //Set the buttons panel
121: Button addNewIndexButton = new Button(
122: ADD_NEW_CONSTRAINT_BUTTON_LABEL, this ,
123: ADD_NEW_CONSTRAINT_BUTTON_LABEL,
124: iconForAddNewRecordButton, Boolean.TRUE);
125: Button removeIndexButton = new Button(
126: REMOVE_CONSTRAINT_BUTTON_LABEL, this ,
127: REMOVE_CONSTRAINT_BUTTON_LABEL,
128: iconForDeleteRecordButton, Boolean.FALSE);
129: List listOfButtons = new ArrayList();
130: listOfButtons.add(addNewIndexButton);
131: listOfButtons.add(removeIndexButton);
132: buttonsPanel = new ButtonsPanel(listOfButtons);
133:
134: this .add(buttonsPanel);
135:
136: //Initialize table
137: this .initializeTable(dbTableOfConstraints);
138: this .constraintsSetupCompleted = true;
139: } catch (DBEngineException exc) {
140: Log.getInstance()
141: .fatalMessage(
142: exc.getClass().getName() + " - "
143: + exc.getMessage(),
144: BrowserPanel.class.getName());
145: String sqlFailedLabel = InternationalizationManager
146: .getInstance().getMessage("dbbrowser-ui",
147: "dbbrowser-ui-dbbrowser-window-sql-failed",
148: null);
149: JOptionPane.showMessageDialog(null, sqlFailedLabel + " - "
150: + exc.getMessage(), TITLE,
151: JOptionPane.ERROR_MESSAGE);
152: } catch (UnsupportedOperationException exc) {
153: ConnectionInfo ci = this .uiControllerForQueries
154: .getConnectionInfo();
155: String[] dbmsTypes = new String[] { ci.getDBMSType() };
156: String featureSupportedByDBMSLabel = InternationalizationManager
157: .getInstance()
158: .getMessage(
159: "dbbrowser-ui",
160: "dbbrowser-ui-dbbrowser-feature-not-supported-error-message",
161: dbmsTypes);
162: JLabel label = new JLabel(featureSupportedByDBMSLabel);
163: label.setHorizontalAlignment(SwingConstants.CENTER);
164: this .setLayout(new BorderLayout());
165: this .add(label);
166: }
167: }
168:
169: private void initialize() {
170: //Set the table for data
171: this .setLayout(new BoxLayout(this , BoxLayout.PAGE_AXIS));
172: }
173:
174: public void initializeTable(DBTable dbTableOfSequences) {
175: this .tableForConstraints
176: .initializeTable(new DBTableDataTableModel(
177: dbTableOfSequences));
178: }
179:
180: public void actionPerformed(ActionEvent e) {
181: //Add a new constraint
182: if (ADD_NEW_CONSTRAINT_BUTTON_LABEL
183: .equals(e.getActionCommand())) {
184: String UNIMPLEMENTED_FEATURE_MESSAGE = InternationalizationManager
185: .getInstance()
186: .getMessage(
187: "dbbrowser-ui",
188: "dbbrowser-ui-dbbrowser-unimplemented-feature",
189: null);
190: JOptionPane.showMessageDialog(null,
191: UNIMPLEMENTED_FEATURE_MESSAGE, TITLE,
192: JOptionPane.INFORMATION_MESSAGE);
193: }
194:
195: //Remove the selected constraint
196: if (REMOVE_CONSTRAINT_BUTTON_LABEL.equals(e.getActionCommand())) {
197: String UNIMPLEMENTED_FEATURE_MESSAGE = InternationalizationManager
198: .getInstance()
199: .getMessage(
200: "dbbrowser-ui",
201: "dbbrowser-ui-dbbrowser-unimplemented-feature",
202: null);
203: JOptionPane.showMessageDialog(null,
204: UNIMPLEMENTED_FEATURE_MESSAGE, TITLE,
205: JOptionPane.INFORMATION_MESSAGE);
206: }
207: }
208: }
|