001: package org.dbbrowser.ui.panel.dbbrowserwindow;
002:
003: import infrastructure.internationalization.InternationalizationManager;
004: import infrastructure.propertymanager.PropertyManager;
005: import java.awt.Dimension;
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.io.BufferedReader;
009: import java.io.File;
010: import java.io.FileNotFoundException;
011: import java.io.FileReader;
012: import java.io.IOException;
013: import javax.swing.*;
014:
015: import org.dbbrowser.ui.UIControllerForQueries;
016: import org.dbbrowser.ui.UIControllerForRawSQL;
017:
018: public class SQLPanel extends JPanel implements ActionListener {
019: private static final long serialVersionUID = UIControllerForQueries.version;
020:
021: private static final String TITLE = InternationalizationManager
022: .getInstance().getMessage("dbbrowser-ui",
023: "dbbrowser-ui-dbbrowser-window-title-label", null);;
024: private static final String OPEN_NEW_SQL_TAB_LABEL = InternationalizationManager
025: .getInstance()
026: .getMessage(
027: "dbbrowser-ui",
028: "dbbrowser-ui-dbbrowser-sql-tab-open-new-tab-button-label",
029: null);
030: private static final String REMOVE_SQL_TAB_LABEL = InternationalizationManager
031: .getInstance()
032: .getMessage(
033: "dbbrowser-ui",
034: "dbbrowser-ui-dbbrowser-sql-tab-remove-tab-button-label",
035: null);
036: private static final String SELECT_NAME_FOR_TAB_LABEL = InternationalizationManager
037: .getInstance()
038: .getMessage(
039: "dbbrowser-ui",
040: "dbbrowser-ui-dbbrowser-sql-tab-select-name-for-tab",
041: null);
042:
043: private static final String OPEN_NEW_SQL_TAB_ICON_FILENAME = PropertyManager
044: .getInstance()
045: .getProperty(
046: "dbbrowser-ui-dbbrowser-window-sql-tab-open-new-sql-tab-button-icon");
047: private static final String REMOVE_SQL_TAB_ICON_FILENAME = PropertyManager
048: .getInstance()
049: .getProperty(
050: "dbbrowser-ui-dbbrowser-window-sql-tab-remove-sql-tab-button-icon");
051:
052: private UIControllerForQueries uicontroller = null;
053: private UIControllerForRawSQL uiControllerForRawSQL = null;
054:
055: private JPanel topRowPanel = new JPanel();
056: private JTabbedPane tabbedPanesForSQLStatements = new JTabbedPane();
057:
058: private RunSQLPanel sqlPanel = null;
059:
060: public SQLPanel(UIControllerForQueries uicontroller,
061: UIControllerForRawSQL uiControllerForRawSQL) {
062: this .uicontroller = uicontroller;
063: this .uiControllerForRawSQL = uiControllerForRawSQL;
064: initialize();
065: }
066:
067: public void runBatchFile(File file) {
068: try {
069: StringBuffer buffer = new StringBuffer();
070: BufferedReader reader = new BufferedReader(new FileReader(
071: file));
072: String line = reader.readLine();
073: while (line != null) {
074: buffer.append(line + "\n");
075: line = reader.readLine();
076: }
077:
078: reader.close();
079:
080: //Create a new tab with the contents
081: sqlPanel = new RunSQLPanel(this .uicontroller,
082: this .uiControllerForRawSQL, buffer.toString());
083: tabbedPanesForSQLStatements.addTab(file.getName(), null,
084: sqlPanel, file.getAbsolutePath());
085: tabbedPanesForSQLStatements.setSelectedComponent(sqlPanel);
086: } catch (FileNotFoundException exc) {
087: String errorMessage = InternationalizationManager
088: .getInstance()
089: .getMessage(
090: "dbbrowser-ui",
091: "dbbrowser-ui-dbbrowser-window-open-batch-file-failed",
092: null);
093: JOptionPane.showMessageDialog(null, errorMessage, TITLE,
094: JOptionPane.ERROR_MESSAGE);
095: } catch (IOException exc) {
096: String errorMessage = InternationalizationManager
097: .getInstance()
098: .getMessage(
099: "dbbrowser-ui",
100: "dbbrowser-ui-dbbrowser-window-open-batch-file-failed",
101: null);
102: JOptionPane.showMessageDialog(null, errorMessage, TITLE,
103: JOptionPane.ERROR_MESSAGE);
104: }
105: }
106:
107: private void initialize() {
108: this .tabbedPanesForSQLStatements
109: .setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
110:
111: //Set the layout
112: this .setLayout(new BoxLayout(this , BoxLayout.PAGE_AXIS));
113:
114: //Setup the button to add new tab
115: JButton buttonToAddNewSQLTab = new JButton(
116: OPEN_NEW_SQL_TAB_LABEL, new ImageIcon(
117: OPEN_NEW_SQL_TAB_ICON_FILENAME));
118: this .topRowPanel.add(buttonToAddNewSQLTab);
119: buttonToAddNewSQLTab.addActionListener(this );
120:
121: //Setup the button to remove current tab
122: JButton buttonToRemoveSQLTab = new JButton(
123: REMOVE_SQL_TAB_LABEL, new ImageIcon(
124: REMOVE_SQL_TAB_ICON_FILENAME));
125: this .topRowPanel.add(buttonToRemoveSQLTab);
126: buttonToRemoveSQLTab.addActionListener(this );
127:
128: //Set the size of the top row panel
129: Dimension d = new Dimension(
130: this .topRowPanel.getMaximumSize().width, 50);
131: this .topRowPanel.setMaximumSize(d);
132:
133: //Add a tab
134: sqlPanel = new RunSQLPanel(this .uicontroller,
135: this .uiControllerForRawSQL, "");
136: tabbedPanesForSQLStatements.addTab("SQL", sqlPanel);
137:
138: //Add the panels
139: this .add(this .topRowPanel);
140: this .add(this .tabbedPanesForSQLStatements);
141: }
142:
143: public void actionPerformed(ActionEvent e) {
144: if (e.getActionCommand().equals(OPEN_NEW_SQL_TAB_LABEL)) {
145: String tabTitle = (String) JOptionPane.showInputDialog(
146: null, SELECT_NAME_FOR_TAB_LABEL, TITLE,
147: JOptionPane.QUESTION_MESSAGE, null, null, "SQL");
148:
149: if (tabTitle != null) {
150: //Add a tab
151: sqlPanel = new RunSQLPanel(this .uicontroller,
152: this .uiControllerForRawSQL);
153: tabbedPanesForSQLStatements.addTab(tabTitle, sqlPanel);
154: tabbedPanesForSQLStatements
155: .setSelectedComponent(sqlPanel);
156: }
157: }
158:
159: if (e.getActionCommand().equals(REMOVE_SQL_TAB_LABEL)) {
160: //Add a tab
161: int selectedTabIndex = tabbedPanesForSQLStatements
162: .getSelectedIndex();
163: if (selectedTabIndex > -1) {
164: tabbedPanesForSQLStatements.remove(selectedTabIndex);
165: }
166: }
167: }
168: }
|