001: package org.dbbrowser.ui.panel.dbbrowserwindow;
002:
003: import java.awt.BorderLayout;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006: import java.awt.event.KeyAdapter;
007: import java.awt.event.KeyEvent;
008: import java.util.ArrayList;
009: import java.util.List;
010: import infrastructure.internationalization.InternationalizationManager;
011: import infrastructure.logging.Log;
012: import infrastructure.propertymanager.PropertyManager;
013: import javax.swing.BorderFactory;
014: import javax.swing.BoxLayout;
015: import javax.swing.ImageIcon;
016: import javax.swing.JOptionPane;
017: import javax.swing.JPanel;
018: import javax.swing.JTabbedPane;
019: import org.dbbrowser.db.engine.exception.DBEngineException;
020: import org.dbbrowser.db.engine.model.DBTable;
021: import org.dbbrowser.db.engine.model.View;
022: import org.dbbrowser.ui.UIControllerForQueries;
023: import org.dbbrowser.ui.UIControllerForUpdates;
024: import org.dbbrowser.ui.helper.DBTableDataTableModel;
025: import org.dbbrowser.ui.panel.ButtonsPanel;
026: import org.dbbrowser.ui.widget.Button;
027: import org.dbbrowser.ui.widget.SyntaxHighlighterPanel;
028: import org.dbbrowser.ui.widget.Table;
029:
030: public class ViewPanel extends JPanel implements ActionListener {
031: private static final long serialVersionUID = UIControllerForQueries.version;
032:
033: private static final String TITLE = InternationalizationManager
034: .getInstance().getMessage("dbbrowser-ui",
035: "dbbrowser-ui-dbbrowser-window-title-label", null);;
036: private String VIEW_DEFINITON_TAB_LABEL = InternationalizationManager
037: .getInstance()
038: .getMessage(
039: "dbbrowser-ui",
040: "dbbrowser-ui-dbbrowser-db-views-view-definiton-tab-label",
041: null);
042: private String VIEW_DATA_TAB_LABEL = InternationalizationManager
043: .getInstance()
044: .getMessage(
045: "dbbrowser-ui",
046: "dbbrowser-ui-dbbrowser-db-views-view-data-tab-label",
047: null);
048: private String UPDATE_VIEW_BUTTON_LABEL = InternationalizationManager
049: .getInstance().getMessage("dbbrowser-ui",
050: "dbbrowser-ui-dbbrowser-update-view-button-label",
051: null);
052: private String UPDATE_VIEW_DEFINITON_BUTTON_ICON = PropertyManager
053: .getInstance()
054: .getProperty(
055: "dbbrowser-ui-view-record-window-update-record-icon");
056:
057: private DBTable dbTable = null;
058: private Table resultsTable = null;
059: private JTabbedPane pane = new JTabbedPane();
060: private View view = null;
061: private SyntaxHighlighterPanel viewDefinitionPanel = null;
062: private UIControllerForUpdates uiControllerForUpdates = null;
063:
064: public ViewPanel(View view, DBTable dbTable,
065: UIControllerForUpdates uiControllerForUpdates) {
066: this .view = view;
067: this .dbTable = dbTable;
068: this .uiControllerForUpdates = uiControllerForUpdates;
069: resultsTable = new Table(null, null);
070: }
071:
072: public void initialize() {
073: this .setLayout(new BorderLayout());
074: DBTableDataTableModel dbTableDataTableModel = new DBTableDataTableModel(
075: this .dbTable);
076:
077: //Display the view definiton
078: JPanel viewPanel = new JPanel();
079: viewPanel.setLayout(new BoxLayout(viewPanel, BoxLayout.Y_AXIS));
080: viewPanel.setBorder(BorderFactory.createTitledBorder(this .view
081: .getViewName()));
082: this .viewDefinitionPanel = new SyntaxHighlighterPanel();
083: this .viewDefinitionPanel.setText(this .view.getViewDefinition());
084: viewPanel.add(this .viewDefinitionPanel);
085:
086: //Add the button to update the view
087: Button buttonToUpdateView = new Button(
088: UPDATE_VIEW_BUTTON_LABEL, this ,
089: UPDATE_VIEW_BUTTON_LABEL, new ImageIcon(
090: UPDATE_VIEW_DEFINITON_BUTTON_ICON),
091: Boolean.TRUE);
092: List listOfButtons = new ArrayList();
093: listOfButtons.add(buttonToUpdateView);
094: ButtonsPanel buttonsPanel = new ButtonsPanel(listOfButtons);
095: viewPanel.add(buttonsPanel);
096: pane.addTab(VIEW_DEFINITON_TAB_LABEL, viewPanel);
097:
098: //Display the results as a table
099: Object[] messageParameters = new Object[] { "0",
100: this .dbTable.getNumberOfRowsInTable(),
101: this .dbTable.getNumberOfRowsInTable() };
102: String title = InternationalizationManager
103: .getInstance()
104: .getMessage(
105: "dbbrowser-ui",
106: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-panel-title",
107: messageParameters);
108: resultsTable.setBorder(BorderFactory
109: .createTitledBorder(this .view.getViewName() + " - "
110: + title));
111: pane.addTab(VIEW_DATA_TAB_LABEL, this .resultsTable);
112: this .add(pane);
113: resultsTable.initializeTable(dbTableDataTableModel);
114:
115: //Add the key listener for syntax highlighter panel
116: this .viewDefinitionPanel.getTextPane().addKeyListener(
117: new RunSQLKeyBinding());
118: }
119:
120: public void actionPerformed(ActionEvent e) {
121: updateView();
122: }
123:
124: private void updateView() {
125: //Update the view
126: try {
127: View updatedView = new View(this .view.getSchemaName(),
128: this .view.getViewName(), this .viewDefinitionPanel
129: .getText());
130: this .uiControllerForUpdates
131: .updateViewDefinition(updatedView);
132: } catch (DBEngineException exc) {
133: Log.getInstance()
134: .fatalMessage(
135: exc.getClass().getName() + " - "
136: + exc.getMessage(),
137: BrowserPanel.class.getName());
138: String sqlFailedLabel = InternationalizationManager
139: .getInstance().getMessage("dbbrowser-ui",
140: "dbbrowser-ui-dbbrowser-window-sql-failed",
141: null);
142: JOptionPane.showMessageDialog(null, sqlFailedLabel + " - "
143: + exc.getMessage(), TITLE,
144: JOptionPane.ERROR_MESSAGE);
145: }
146: }
147:
148: private class RunSQLKeyBinding extends KeyAdapter {
149: public void keyPressed(KeyEvent e) {
150: //if CTRL-Enter is pressed, run SQL
151: if (e.getKeyCode() == 10 && e.getModifiers() == 2) {
152: updateView();
153: }
154: }
155: }
156: }
|