001: package org.dbbrowser.ui.panel.dbbrowserwindow;
002:
003: import java.awt.*;
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.awt.event.MouseListener;
009: import java.util.*;
010: import java.util.List;
011: import infrastructure.internationalization.InternationalizationManager;
012: import infrastructure.propertymanager.PropertyManager;
013: import javax.swing.*;
014: import org.dbbrowser.db.engine.exception.DBEngineException;
015: import org.dbbrowser.db.engine.model.DBTable;
016: import org.dbbrowser.ui.helper.DBTableDataTableModel;
017: import org.dbbrowser.ui.UIControllerForQueries;
018: import org.dbbrowser.ui.UIControllerForRawSQL;
019: import org.dbbrowser.ui.panel.ButtonsPanel;
020: import org.dbbrowser.ui.widget.*;
021: import org.dbbrowser.ui.widget.Button;
022: import org.dbbrowser.help.HelpManager;
023:
024: public class RunSQLPanel extends JPanel implements ActionListener {
025: private static final long serialVersionUID = UIControllerForQueries.version;
026:
027: private static final String TITLE = InternationalizationManager
028: .getInstance().getMessage("dbbrowser-ui",
029: "dbbrowser-ui-dbbrowser-window-title-label", null);;
030: private static final String SELECT_PREVIOUSLY_RUN_SQL_LABEL = InternationalizationManager
031: .getInstance()
032: .getMessage(
033: "dbbrowser-ui",
034: "dbbrowser-ui-dbbrowser-sql-tab-select-previously-run-sql",
035: null);
036: private static final String RUN_SQL_BUTTON_LABEL = InternationalizationManager
037: .getInstance()
038: .getMessage(
039: "dbbrowser-ui",
040: "dbbrowser-ui-dbbrowser-sql-tab-run-sql-button-label",
041: null);;
042: private static final String COMMIT_BUTTON_LABEL = InternationalizationManager
043: .getInstance()
044: .getMessage(
045: "dbbrowser-ui",
046: "dbbrowser-ui-dbbrowser-sql-tab-commit-button-label",
047: null);;
048: private static final String ROLLBACK_BUTTON_LABEL = InternationalizationManager
049: .getInstance()
050: .getMessage(
051: "dbbrowser-ui",
052: "dbbrowser-ui-dbbrowser-sql-tab-rollback-button-label",
053: null);;
054:
055: private static final String POP_UP_MENU_COPY_MENU_ITEM_LABEL = InternationalizationManager
056: .getInstance()
057: .getMessage(
058: "dbbrowser-ui",
059: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-popup-menu-copy-menu-item-label",
060: null);;
061: private static final String POP_UP_MENU_EXPORT_MENU_ITEM_LABEL = InternationalizationManager
062: .getInstance()
063: .getMessage(
064: "dbbrowser-ui",
065: "dbbrowser-ui-dbbrowser-window-tools-menu-copy-label",
066: null);;
067: private static final String POP_UP_MENU_WHATS_THIS_MENU_ITEM_LABEL = InternationalizationManager
068: .getInstance()
069: .getMessage(
070: "dbbrowser-ui",
071: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-popup-menu-whats-this-menu-item-label",
072: null);;
073: private static final String POP_UP_MENU_HELP_MENU_ITEM_LABEL = InternationalizationManager
074: .getInstance()
075: .getMessage(
076: "dbbrowser-ui",
077: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-popup-menu-help-menu-item-label",
078: null);;
079:
080: private static final String COMMIT_BUTTON_ICON_FILENAME = PropertyManager
081: .getInstance()
082: .getProperty(
083: "dbbrowser-ui-dbbrowser-window-sql-tab-commit-button-icon");
084: private static final String ROLLBACK_BUTTON_ICON_FILENAME = PropertyManager
085: .getInstance()
086: .getProperty(
087: "dbbrowser-ui-dbbrowser-window-sql-tab-rollback-button-icon");
088:
089: private static final String COPY_POPUP_ITEM_ICON_FILENAME = PropertyManager
090: .getInstance().getProperty(
091: "dbbrowser-popup-menu-copy-icon-filename");
092: private static final String EXPORT_POPUP_ITEM_ICON_FILENAME = PropertyManager
093: .getInstance()
094: .getProperty(
095: "dbbrowser-ui-dbbrowser-window-toolbar-export-icon");
096: private static final String WHATS_THIS_POPUP_ITEM_ICON_FILENAME = PropertyManager
097: .getInstance().getProperty(
098: "dbbrowser-popup-menu-whats-this-icon-filename");
099: private static final String HELP_POPUP_ITEM_ICON_FILENAME = PropertyManager
100: .getInstance().getProperty(
101: "dbbrowser-ui-dbbrowser-window-toolbar-help-icon");
102:
103: private UIControllerForQueries uicontroller = null;
104: private UIControllerForRawSQL uiControllerForRawSQL = null;
105: private String initialContents = null;
106:
107: private JComboBox comboBoxForHistoryOfSQLStatements = new JComboBox();
108: private ButtonsPanel buttonsPanel = null;
109: private SyntaxHighlighterPanel syntaxHighlighterPanel = null;
110: private JPanel resultsPanel = new JPanel();
111: private Table resultsTable = null;
112: private JPanel panelForSplitPane = new JPanel();
113: private JSplitPane splitPane = null;
114: private SQLHistoryComboBoxModel sqlHistoryComboBoxModel = null;
115:
116: public RunSQLPanel(UIControllerForQueries uicontroller,
117: UIControllerForRawSQL uiControllerForRawSQL) {
118: this .uicontroller = uicontroller;
119: this .uiControllerForRawSQL = uiControllerForRawSQL;
120: initialize();
121:
122: //Add key listener for CTRL-Enter
123: this .syntaxHighlighterPanel.getTextPane().addKeyListener(
124: new RunSQLKeyBinding());
125: }
126:
127: public RunSQLPanel(UIControllerForQueries uicontroller,
128: UIControllerForRawSQL uiControllerForRawSQL,
129: String initialContents) {
130: this .uicontroller = uicontroller;
131: this .uiControllerForRawSQL = uiControllerForRawSQL;
132: this .initialContents = initialContents;
133: initialize();
134:
135: //Add key listener for CTRL-Enter
136: this .syntaxHighlighterPanel.getTextPane().addKeyListener(
137: new RunSQLKeyBinding());
138: }
139:
140: private class RunSQLKeyBinding extends KeyAdapter {
141: public void keyPressed(KeyEvent e) {
142: //if CTRL-Enter is pressed, run SQL
143: if (e.getKeyCode() == 10 && e.getModifiers() == 2) {
144: runSQL();
145: }
146: }
147: }
148:
149: private void initialize() {
150: //Set the layout
151: this .setLayout(new BoxLayout(this , BoxLayout.PAGE_AXIS));
152:
153: //Setup the combo box
154: sqlHistoryComboBoxModel = new SQLHistoryComboBoxModel();
155: sqlHistoryComboBoxModel
156: .addElement(SELECT_PREVIOUSLY_RUN_SQL_LABEL);
157: this .comboBoxForHistoryOfSQLStatements
158: .setModel(sqlHistoryComboBoxModel);
159:
160: //Set the size of the drop down box
161: Dimension dd = new Dimension(500,
162: this .comboBoxForHistoryOfSQLStatements
163: .getPreferredSize().height);
164: this .comboBoxForHistoryOfSQLStatements.setPreferredSize(dd);
165:
166: //Add the listener for the combo box
167: this .comboBoxForHistoryOfSQLStatements.addActionListener(this );
168:
169: //Set the action command for the drop down box
170: this .comboBoxForHistoryOfSQLStatements
171: .setActionCommand("ComboBoxForSQLHistory");
172:
173: //Add the drop down box
174: this .add(this .comboBoxForHistoryOfSQLStatements);
175:
176: //Setup the text area
177: this .syntaxHighlighterPanel = new SyntaxHighlighterPanel();
178:
179: //Set the initial contents if it is not null
180: if (this .initialContents != null
181: && (!"".equals(this .initialContents))) {
182: this .syntaxHighlighterPanel.setText(this .initialContents);
183: }
184:
185: Dimension minimumSizeOfTheTextArea = new Dimension(2000, 100);
186: this .syntaxHighlighterPanel
187: .setMinimumSize(minimumSizeOfTheTextArea);
188:
189: //Add the split pane
190: this .splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
191: this .syntaxHighlighterPanel, resultsPanel);
192: this .splitPane.setOneTouchExpandable(true);
193: this .splitPane.setDividerLocation(250);
194: this .panelForSplitPane.setLayout(new BorderLayout());
195: this .panelForSplitPane.add(this .splitPane);
196: this .add(this .panelForSplitPane);
197:
198: //Add the buttons
199: String runSQLIconFilename = PropertyManager
200: .getInstance()
201: .getProperty(
202: "dbbrowser-ui-dbbrowser-window-sql-tab-run-sql-icon");
203: Button runSQLButton = new Button(RUN_SQL_BUTTON_LABEL, this ,
204: RUN_SQL_BUTTON_LABEL,
205: new ImageIcon(runSQLIconFilename), Boolean.FALSE);
206: Button commitButton = new Button(COMMIT_BUTTON_LABEL, this ,
207: COMMIT_BUTTON_LABEL, new ImageIcon(
208: COMMIT_BUTTON_ICON_FILENAME), Boolean.FALSE);
209: Button rollbackButton = new Button(ROLLBACK_BUTTON_LABEL, this ,
210: ROLLBACK_BUTTON_LABEL, new ImageIcon(
211: ROLLBACK_BUTTON_ICON_FILENAME), Boolean.FALSE);
212:
213: List listOFButtons = new ArrayList();
214: listOFButtons.add(runSQLButton);
215: listOFButtons.add(commitButton);
216: listOFButtons.add(rollbackButton);
217:
218: //Setup the buttons panel
219: this .buttonsPanel = new ButtonsPanel(listOFButtons);
220:
221: //If autocommit is on, disable all buttons
222: String autoCommitFlag = PropertyManager.getInstance()
223: .getProperty("dbbrowser-auto-commit");
224: if ("true".equals(autoCommitFlag)) {
225: //Disable the commit and rollback buttons
226: this .buttonsPanel.enableButton(COMMIT_BUTTON_LABEL,
227: Boolean.FALSE);
228: this .buttonsPanel.enableButton(ROLLBACK_BUTTON_LABEL,
229: Boolean.FALSE);
230: }
231:
232: this .add(this .buttonsPanel);
233:
234: //Add the pop up menu
235: JPopupMenu popupMenuForRunSQLTable = new JPopupMenu();
236: JMenuItem copyMenuItem = new JMenuItem(
237: POP_UP_MENU_COPY_MENU_ITEM_LABEL, new ImageIcon(
238: COPY_POPUP_ITEM_ICON_FILENAME));
239: //copyMenuItem.addActionListener(this);
240: popupMenuForRunSQLTable.add(copyMenuItem);
241: popupMenuForRunSQLTable.addSeparator();
242: JMenuItem exportMenuItem = new JMenuItem(
243: POP_UP_MENU_EXPORT_MENU_ITEM_LABEL, new ImageIcon(
244: EXPORT_POPUP_ITEM_ICON_FILENAME));
245: copyMenuItem.addActionListener(this );
246: popupMenuForRunSQLTable.add(exportMenuItem);
247: popupMenuForRunSQLTable.addSeparator();
248: JMenuItem whatsThisMenuItem = new JMenuItem(
249: POP_UP_MENU_WHATS_THIS_MENU_ITEM_LABEL, new ImageIcon(
250: WHATS_THIS_POPUP_ITEM_ICON_FILENAME));
251: popupMenuForRunSQLTable.add(whatsThisMenuItem);
252: JMenuItem helpMenuItem = new JMenuItem(
253: POP_UP_MENU_HELP_MENU_ITEM_LABEL, new ImageIcon(
254: HELP_POPUP_ITEM_ICON_FILENAME));
255: helpMenuItem.addActionListener(HelpManager.getInstance()
256: .getActionListenerForHelpEvents());
257: popupMenuForRunSQLTable.add(helpMenuItem);
258:
259: //Add listener to components that can bring up popup menus.
260: MouseListener popupListenerForResultsTable = new BasicPopupListener(
261: popupMenuForRunSQLTable);
262: this .resultsPanel
263: .addMouseListener(popupListenerForResultsTable);
264:
265: //Setup CSH
266: HelpManager.getInstance().registerCSH(whatsThisMenuItem, this ,
267: "sql_tab");
268: }
269:
270: public void actionPerformed(ActionEvent e) {
271: if (RUN_SQL_BUTTON_LABEL.equals(e.getActionCommand())) {
272: runSQL();
273: }
274:
275: //If the combox box value is selected, append it to the ext area
276: if (COMMIT_BUTTON_LABEL.equals(e.getActionCommand())) {
277: try {
278: //Rollback if autocommit if off
279: String autoCommitFlag = PropertyManager.getInstance()
280: .getProperty("dbbrowser-auto-commit");
281: if ("false".equals(autoCommitFlag)) {
282: this .uiControllerForRawSQL.commit();
283: }
284: } catch (DBEngineException exc) {
285: String errorMessage = InternationalizationManager
286: .getInstance()
287: .getMessage(
288: "dbbrowser-ui",
289: "dbbrowser-ui-dbbrowser-window-sql-failed",
290: null);
291: JOptionPane.showMessageDialog(null, errorMessage
292: + " - " + exc.getMessage(), TITLE,
293: JOptionPane.ERROR_MESSAGE);
294: }
295: }
296:
297: //If the combox box value is selected, append it to the ext area
298: if (ROLLBACK_BUTTON_LABEL.equals(e.getActionCommand())) {
299: try {
300: //Rollback if autocommit if off
301: String autoCommitFlag = PropertyManager.getInstance()
302: .getProperty("dbbrowser-auto-commit");
303: if ("false".equals(autoCommitFlag)) {
304: this .uiControllerForRawSQL.rollback();
305: }
306: } catch (DBEngineException exc) {
307: String errorMessage = InternationalizationManager
308: .getInstance()
309: .getMessage(
310: "dbbrowser-ui",
311: "dbbrowser-ui-dbbrowser-window-sql-failed",
312: null);
313: JOptionPane.showMessageDialog(null, errorMessage
314: + " - " + exc.getMessage(), TITLE,
315: JOptionPane.ERROR_MESSAGE);
316: }
317: }
318:
319: //If the combox box value is selected, append it to the ext area
320: if ("ComboBoxForSQLHistory".equals(e.getActionCommand())) {
321: //append the value if it is not the first value
322: if (this .comboBoxForHistoryOfSQLStatements
323: .getSelectedIndex() != 0) {
324: this .syntaxHighlighterPanel
325: .append(this .comboBoxForHistoryOfSQLStatements
326: .getSelectedItem().toString());
327: }
328: }
329:
330: //If the user wants to copy something
331: if (POP_UP_MENU_EXPORT_MENU_ITEM_LABEL.equals(e
332: .getActionCommand())) {
333:
334: }
335: }
336:
337: private void runSQL() {
338: //Get the text from the text area
339: String sql = this .syntaxHighlighterPanel.getText();
340:
341: //Run the sql and get the results as DBTable
342: try {
343: //Set the auto commit flag
344: String autoCommitFlag = PropertyManager.getInstance()
345: .getProperty("dbbrowser-auto-commit");
346: if ("false".equals(autoCommitFlag)) {
347: this .uiControllerForRawSQL.setAutoCommit(false);
348: }
349:
350: //Run the SQL
351: DBTable dbTable = this .uiControllerForRawSQL.runRawSQL(sql);
352:
353: //if a value is returned
354: if (dbTable != null) {
355: DBTableDataTableModel dbTableDataTableModel = new DBTableDataTableModel(
356: dbTable);
357:
358: //Remove the existing results table from the results panel if table is not null
359: if (this .resultsTable != null) {
360: this .resultsPanel.remove(this .resultsTable);
361: }
362:
363: //Display the results as a table
364: this .resultsTable = new Table(this .uicontroller, null);
365:
366: this .resultsPanel.add(this .resultsTable);
367: this .resultsPanel.setLayout(new BoxLayout(
368: this .resultsPanel, BoxLayout.PAGE_AXIS));
369: Object[] messageParameters = new Object[] { "0",
370: dbTable.getNumberOfRowsInTable(),
371: dbTable.getNumberOfRowsInTable() };
372: String title = InternationalizationManager
373: .getInstance()
374: .getMessage(
375: "dbbrowser-ui",
376: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-panel-title",
377: messageParameters);
378: this .resultsPanel.setBorder(BorderFactory
379: .createTitledBorder(title));
380: this .resultsTable
381: .initializeTable(dbTableDataTableModel);
382:
383: //Add the sql to the combo box if it is not a multiline statement
384: boolean b = doesStringOccurMoreThanOnceInAnotherString(
385: sql, ";");
386: if (b == false) {
387: sqlHistoryComboBoxModel.addElement(sql);
388: }
389: }
390:
391: //Update the UI
392: this .updateUI();
393: } catch (DBEngineException exc) {
394: String errorMessage = InternationalizationManager
395: .getInstance().getMessage("dbbrowser-ui",
396: "dbbrowser-ui-dbbrowser-window-sql-failed",
397: null);
398: JOptionPane.showMessageDialog(null, errorMessage + " - "
399: + exc.getMessage(), TITLE,
400: JOptionPane.ERROR_MESSAGE);
401: }
402:
403: //Update the UI
404: this .updateUI();
405: }
406:
407: private boolean doesStringOccurMoreThanOnceInAnotherString(
408: String containerString, String stringToLookFor) {
409: boolean occursMoreThanOnce = false;
410: int firstLocation = containerString.indexOf(stringToLookFor);
411:
412: if (firstLocation != -1) {
413: int secondLocation = containerString.indexOf(
414: stringToLookFor, firstLocation
415: + stringToLookFor.length());
416: if (secondLocation != -1) {
417: occursMoreThanOnce = true;
418: }
419: }
420: return occursMoreThanOnce;
421: }
422:
423: private class SQLHistoryComboBoxModel extends DefaultComboBoxModel {
424: public void addElement(Object anObject) {
425: //Add the element if it does not already exist
426: if (this .getIndexOf(anObject) == -1) {
427: super.addElement(anObject);
428: }
429: }
430: }
431: }
|