001: package org.dbbrowser.ui.panel.dbbrowserwindow;
002:
003: import java.awt.*;
004: import java.util.Collections;
005: import java.util.List;
006: import infrastructure.internationalization.InternationalizationManager;
007: import infrastructure.logging.Log;
008: import infrastructure.propertymanager.PropertyManager;
009: import javax.swing.*;
010: import javax.swing.event.TreeSelectionEvent;
011: import javax.swing.event.TreeSelectionListener;
012: import javax.swing.tree.DefaultMutableTreeNode;
013: import org.dbbrowser.db.engine.exception.DBEngineException;
014: import org.dbbrowser.db.engine.model.DBTable;
015: import org.dbbrowser.db.engine.model.View;
016: import org.dbbrowser.ui.UIControllerForQueries;
017: import org.dbbrowser.ui.UIControllerForUpdates;
018: import org.dbbrowser.ui.UIControllerForRawSQL;
019:
020: public class BrowserPanel extends JPanel implements
021: TreeSelectionListener {
022: private static final long serialVersionUID = UIControllerForQueries.version;
023:
024: private static final String TITLE = InternationalizationManager
025: .getInstance().getMessage("dbbrowser-ui",
026: "dbbrowser-ui-dbbrowser-window-title-label", null);;
027: private String tableDataTitle = InternationalizationManager
028: .getInstance()
029: .getMessage(
030: "dbbrowser-ui",
031: "dbbrowser-ui-dbbrowser-browser-tab-tabledata-tab-title",
032: null);
033: private String columnDetailsTitle = InternationalizationManager
034: .getInstance()
035: .getMessage(
036: "dbbrowser-ui",
037: "dbbrowser-ui-dbbrowser-browser-tab-columndetails-tab-title",
038: null);
039: private String dbTableSequencesTitle = InternationalizationManager
040: .getInstance()
041: .getMessage(
042: "dbbrowser-ui",
043: "dbbrowser-ui-dbbrowser-browser-tab-db-table-sequences-tab-title",
044: null);
045: private String dividerLocation = PropertyManager
046: .getInstance()
047: .getProperty(
048: "dbbrowser-ui-dbbrowser-window-browser-panel-splitpane-divider-location");
049:
050: private TableDataPanel tableDataPanel = null;
051: private ColumnDetailsPanel columnDetailsPanel = null;
052: private JSplitPane splitPane = null;
053: private JTabbedPane tabbedPane = null;
054: private UIControllerForQueries uiControllerForQueries = null;
055: private UIControllerForUpdates uiControllerForUpdates = null;
056: private UIControllerForRawSQL uiControllerForRawSQL = null;
057: private TableTreePanel tableTreePanel = null;
058:
059: private Integer pagingSize = null;
060:
061: public BrowserPanel(UIControllerForQueries uiControllerForQueries,
062: UIControllerForUpdates uiControllerForUpdates,
063: UIControllerForRawSQL uiControllerForRawSQL) {
064: this .uiControllerForQueries = uiControllerForQueries;
065: this .uiControllerForUpdates = uiControllerForUpdates;
066: this .uiControllerForRawSQL = uiControllerForRawSQL;
067:
068: pagingSize = new Integer(Integer.parseInt(PropertyManager
069: .getInstance().getProperty(
070: "dbbrowser-ui-browser-window-paging-size")));
071: initialize();
072: }
073:
074: private void initialize() {
075: this .setLayout(new BorderLayout());
076: //Dimension minimumSize = new Dimension(200, 100);
077: tableTreePanel = new TableTreePanel(
078: this .uiControllerForQueries,
079: this .uiControllerForRawSQL, this );
080: //JScrollPane scrollPane1 = new JScrollPane( tableTreePanel );
081: //scrollPane1.setMinimumSize( minimumSize );
082:
083: tabbedPane = new JTabbedPane();
084: this .tableDataPanel = new TableDataPanel(
085: this .uiControllerForQueries,
086: this .uiControllerForUpdates);
087: tabbedPane.addTab(tableDataTitle, null, this .tableDataPanel,
088: tableDataTitle);
089:
090: this .columnDetailsPanel = new ColumnDetailsPanel(
091: this .uiControllerForQueries,
092: this .uiControllerForUpdates);
093: tabbedPane.addTab(columnDetailsTitle, null,
094: this .columnDetailsPanel, columnDetailsTitle);
095:
096: this .splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
097: tableTreePanel, tabbedPane);
098: this .splitPane.setOneTouchExpandable(true);
099:
100: this .splitPane.setDividerLocation(Integer
101: .parseInt(dividerLocation));
102: this .add(this .splitPane);
103: }
104:
105: public void refresh() {
106: DefaultMutableTreeNode node = this .tableTreePanel
107: .getLastSelectedPathComponent();
108:
109: //if user has selected something, then refresh it
110: if (node != null) {
111: Object selectedUserObject = node.getUserObject();
112: TreeNode treeNode = (TreeNode) selectedUserObject;
113:
114: //Set the right component to be the tabel tree panel
115: this .splitPane.setRightComponent(this .tabbedPane);
116:
117: if (TreeNode.ROOT_VIEW_TYPE.equals(treeNode.getType())) //This is the 'root' - it has been already built
118: {
119: //Dont do anything
120: } else if (TreeNode.SCHEMAS_TYPE.equals(treeNode.getType())) //User has selected 'schemas', dont do anything as it is already built
121: {
122: //Dont do anything
123:
124: try {
125: //Get the list of schemas
126: List listOfSchemas = this .uiControllerForQueries
127: .listSchemas();
128:
129: //Sort the list of tablespaces
130: Collections.sort(listOfSchemas);
131:
132: //Clear existing list of schemas
133: this .tableTreePanel.clear(node);
134:
135: //Add a sub tree for every schema user has access to
136: for (int i = 0; i < listOfSchemas.size(); i++) {
137: String schemaName = (String) listOfSchemas
138: .get(i);
139: TreeNode tableTreeNode = new TreeNode(
140: schemaName, TreeNode.SCHEMA_TYPE);
141: //DefaultMutableTreeNode schema = new DefaultMutableTreeNode(tn);
142: //node.add(schema);
143: this .tableTreePanel.add(node, tableTreeNode);
144: }
145: } catch (DBEngineException exc) {
146: Log.getInstance().fatalMessage(exc.getMessage(),
147: BrowserPanel.class.getName());
148: String sqlFailedLabel = InternationalizationManager
149: .getInstance()
150: .getMessage(
151: "dbbrowser-ui",
152: "dbbrowser-ui-dbbrowser-window-sql-failed",
153: null);
154: JOptionPane.showMessageDialog(null, sqlFailedLabel
155: + " - " + exc.getMessage(), TITLE,
156: JOptionPane.ERROR_MESSAGE);
157: }
158: } else if (TreeNode.SCHEMA_TYPE.equals(treeNode.getType())) //User has selected a schema, show the tables in that table
159: {
160: String schemaName = treeNode.getName();
161:
162: //Get the tables in the tablespace
163: try {
164: //Get the tables in the tablespace
165: List listOfTablesInSchema = this .uiControllerForQueries
166: .listTablesInSchema(schemaName);
167:
168: //Sort the list of tables
169: Collections.sort(listOfTablesInSchema);
170:
171: //Clear the existing data in node
172: this .tableTreePanel.clear(node);
173:
174: //Add a sub tree for every table
175: for (int i = 0; i < listOfTablesInSchema.size(); i++) {
176: String tableName = (String) listOfTablesInSchema
177: .get(i);
178: TreeNode tableTreeNode = new TreeNode(
179: tableName, TreeNode.TABLE_TYPE);
180: this .tableTreePanel.add(node, tableTreeNode);
181: }
182: } catch (DBEngineException exc) {
183: Log.getInstance().fatalMessage(exc.getMessage(),
184: BrowserPanel.class.getName());
185: String sqlFailedLabel = InternationalizationManager
186: .getInstance()
187: .getMessage(
188: "dbbrowser-ui",
189: "dbbrowser-ui-dbbrowser-window-sql-failed",
190: null);
191: JOptionPane.showMessageDialog(null, sqlFailedLabel
192: + " - " + exc.getMessage(), TITLE,
193: JOptionPane.ERROR_MESSAGE);
194: }
195: } else if (TreeNode.TABLE_TYPE.equals(treeNode.getType())) //User has selected a table
196: {
197: //Get the schema and table name
198: TreeNode schemaTreeNode = (TreeNode) ((DefaultMutableTreeNode) node
199: .getParent()).getUserObject();
200: String schemaName = schemaTreeNode.getName();
201: String tableName = treeNode.getName();
202: ;
203:
204: //Show the column details and data in the table
205: showColumnDetailsAndData(schemaName, tableName);
206: } else if (TreeNode.VIEWS_TYPE.equals(treeNode.getType())) {
207: try {
208: //Get the list of views
209: List listOfViews = this .uiControllerForQueries
210: .listViews();
211:
212: //Clear the existing data in node
213: this .tableTreePanel.clear(node);
214:
215: //Add a sub tree for every view
216: for (int i = 0; i < listOfViews.size(); i++) {
217: View view = (View) listOfViews.get(i);
218: String viewName = view.getViewName();
219: TreeNode tn = new TreeNode(viewName,
220: TreeNode.VIEW_TYPE, view);
221: DefaultMutableTreeNode nodeForView = new DefaultMutableTreeNode(
222: tn);
223: node.add(nodeForView);
224: }
225: } catch (DBEngineException exc) {
226: Log.getInstance().fatalMessage(
227: exc.getClass().getName() + " - "
228: + exc.getMessage(),
229: BrowserPanel.class.getName());
230: String sqlFailedLabel = InternationalizationManager
231: .getInstance()
232: .getMessage(
233: "dbbrowser-ui",
234: "dbbrowser-ui-dbbrowser-window-sql-failed",
235: null);
236: JOptionPane.showMessageDialog(null, sqlFailedLabel
237: + " - " + exc.getMessage(), TITLE,
238: JOptionPane.ERROR_MESSAGE);
239: }
240: } else if (TreeNode.VIEW_TYPE.equals(treeNode.getType())) //Show details about a view
241: {
242: //Get the details for a view
243: View view = (View) treeNode.getUserObject();
244:
245: //Get the SQL for the view
246: try {
247: String viewDefinition = this .uiControllerForQueries
248: .getSQLForView(view);
249:
250: //Run the sql for view and get the results
251: DBTable dbTable = this .uiControllerForRawSQL
252: .runRawSQL(viewDefinition);
253:
254: //Show the results of the view if the results are not null
255: if (dbTable != null) {
256: View newView = new View(view.getSchemaName(),
257: view.getViewName(), viewDefinition);
258: ViewPanel vp = new ViewPanel(newView, dbTable,
259: this .uiControllerForUpdates);
260: vp.updateUI();
261: this .splitPane.setRightComponent(vp);
262: vp.initialize();
263: }
264: } catch (DBEngineException exc) {
265: Log.getInstance().fatalMessage(
266: exc.getClass().getName() + " - "
267: + exc.getMessage(),
268: BrowserPanel.class.getName());
269: String sqlFailedLabel = InternationalizationManager
270: .getInstance()
271: .getMessage(
272: "dbbrowser-ui",
273: "dbbrowser-ui-dbbrowser-window-sql-failed",
274: null);
275: JOptionPane.showMessageDialog(null, sqlFailedLabel
276: + " - " + exc.getMessage(), TITLE,
277: JOptionPane.ERROR_MESSAGE);
278: }
279: }
280:
281: //Set the right component to be the table if views are not selected
282: if (!TreeNode.VIEW_TYPE.equals(treeNode.getType())) {
283: this .splitPane.setRightComponent(this .tabbedPane);
284: }
285:
286: this .splitPane.setDividerLocation(Integer
287: .parseInt(dividerLocation));
288:
289: //Update the UI
290: this .tableTreePanel.refresh(node);
291: this .updateUI();
292: }
293: }
294:
295: public void valueChanged(TreeSelectionEvent e) {
296: //Store the cursor type
297: Cursor defaultCursor = Cursor.getDefaultCursor();
298:
299: Cursor waitCursor = Cursor
300: .getPredefinedCursor(Cursor.WAIT_CURSOR);
301: JFrame rootFrame = (JFrame) SwingUtilities
302: .getWindowAncestor(this );
303:
304: //Change the cursor to hourglass
305: try {
306: rootFrame.setCursor(waitCursor);
307: refresh();
308: } finally {
309: //Change the cursor to normal cursor
310: rootFrame.setCursor(defaultCursor);
311: }
312: }
313:
314: private void showColumnDetailsAndData(String schemaName,
315: String tableName) {
316: //Get the data in the table
317: try {
318: DBTable dbTable = null;
319:
320: //if filter has been set, use it, else get all records
321: if (this .tableDataPanel.getFilter(tableName) != null) {
322: //Filter has been set, use it
323: dbTable = this .uiControllerForQueries
324: .getFilteredDataInATable(schemaName, tableName,
325: this .tableDataPanel
326: .getFilter(tableName));
327: } else {
328: //Get the results from the database and show the data in the table
329: dbTable = this .uiControllerForQueries
330: .getAllDataInATable(schemaName, tableName,
331: new Integer(0), this .pagingSize);
332: }
333:
334: //Show the results
335: this .tableDataPanel.initializeTable(dbTable);
336:
337: //Show the details of the columns
338: this .columnDetailsPanel.initializeTable(dbTable);
339: } catch (DBEngineException exc) {
340: Log.getInstance()
341: .fatalMessage(
342: exc.getClass().getName() + " - "
343: + exc.getMessage(),
344: BrowserPanel.class.getName());
345: String sqlFailedLabel = InternationalizationManager
346: .getInstance().getMessage("dbbrowser-ui",
347: "dbbrowser-ui-dbbrowser-window-sql-failed",
348: null);
349: JOptionPane.showMessageDialog(null, sqlFailedLabel + " - "
350: + exc.getMessage(), TITLE,
351: JOptionPane.ERROR_MESSAGE);
352: }
353: }
354: }
|