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.MouseAdapter;
007: import java.awt.event.MouseEvent;
008: import infrastructure.internationalization.InternationalizationManager;
009: import infrastructure.logging.Log;
010: import infrastructure.propertymanager.PropertyManager;
011: import javax.swing.*;
012: import javax.swing.event.TreeSelectionEvent;
013: import javax.swing.event.TreeSelectionListener;
014: import javax.swing.tree.DefaultMutableTreeNode;
015: import javax.swing.tree.DefaultTreeModel;
016: import javax.swing.tree.TreePath;
017: import org.dbbrowser.db.engine.exception.DBEngineException;
018: import org.dbbrowser.db.engine.model.View;
019: import org.dbbrowser.ui.UIControllerForQueries;
020: import org.dbbrowser.ui.UIControllerForRawSQL;
021:
022: public class TableTreePanel extends JPanel {
023: private static final long serialVersionUID = UIControllerForQueries.version;
024: private static final String TITLE = InternationalizationManager
025: .getInstance().getMessage("dbbrowser-ui",
026: "dbbrowser-ui-dbbrowser-window-title-label", null);;
027: private String DROP_TABLE_ICON_FILE_NAME = PropertyManager
028: .getInstance().getProperty(
029: "dbbrowser-ui-dbbrowser-window-drop-table-icon");
030: private String DELETE_ALL_RECORDS_IN_TABLE_ICON_FILE_NAME = PropertyManager
031: .getInstance()
032: .getProperty(
033: "dbbrowser-ui-dbbrowser-window-delete-all-rows-in-table-icon");
034: private String REFRESH_TABLE_ICON_FILE_NAME = PropertyManager
035: .getInstance().getProperty(
036: "dbbrowser-ui-dbbrowser-window-refresh-table-icon");
037: private Icon iconForDropTable = new ImageIcon(
038: DROP_TABLE_ICON_FILE_NAME);
039: private Icon iconForDeleteAllRecordsInTable = new ImageIcon(
040: DELETE_ALL_RECORDS_IN_TABLE_ICON_FILE_NAME);
041: private Icon iconForRefreshTable = new ImageIcon(
042: REFRESH_TABLE_ICON_FILE_NAME);
043:
044: //Lables for file menu
045: private UIControllerForQueries uicontroller = null;
046: private UIControllerForRawSQL uiControllerForRawSQL = null;
047: private JTree tree = null;
048: private DefaultTreeModel treeModel = null;
049: private TreeSelectionListener treeSelectionListener = null;
050:
051: public TableTreePanel(UIControllerForQueries uicontroller,
052: UIControllerForRawSQL uiControllerForRawSQL,
053: TreeSelectionListener treeSelectionListener) {
054: this .uicontroller = uicontroller;
055: this .uiControllerForRawSQL = uiControllerForRawSQL;
056: this .treeSelectionListener = treeSelectionListener;
057: initialize();
058: }
059:
060: public DefaultMutableTreeNode getLastSelectedPathComponent() {
061: return (DefaultMutableTreeNode) this .tree
062: .getLastSelectedPathComponent();
063: }
064:
065: public void add(DefaultMutableTreeNode parent, TreeNode child) {
066: DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(
067: child);
068: treeModel.insertNodeInto(childNode, parent, parent
069: .getChildCount());
070: }
071:
072: private class PopupDecorator extends MouseAdapter {
073: public void mouseReleased(MouseEvent me) {
074: if (me.isPopupTrigger()) {
075: JPopupMenu jPopupMenu = new JPopupMenu();
076:
077: DefaultMutableTreeNode node = getLastSelectedPathComponent();
078: TreePath path = tree.getPathForLocation(me.getX(), me
079: .getY());
080:
081: //If a user object has been set(it wont be for root node)
082: if (node != null) {
083: Object selectedUserObject = node.getUserObject();
084: TreeNode treeNode = (TreeNode) selectedUserObject;
085:
086: //if the popup is on the tree and a table has been selected
087: if (path != null
088: && TreeNode.TABLE_TYPE.equals(treeNode
089: .getType())) {
090: Object[] params = new Object[] { treeNode
091: .getName() };
092:
093: //Add menu item to refresh table
094: String REFRESH_TABLE_MENU_ITEM = InternationalizationManager
095: .getInstance()
096: .getMessage(
097: "dbbrowser-ui",
098: "dbbrowser-ui-dbbrowser-window-browser-panel-refresh-table-menu-item-label",
099: params);
100: JMenuItem refreshTableMenuItem = new JMenuItem(
101: REFRESH_TABLE_MENU_ITEM,
102: iconForRefreshTable);
103: refreshTableMenuItem
104: .setActionCommand("Refresh");
105: refreshTableMenuItem
106: .addActionListener(new MouseActionListener());
107: jPopupMenu.add(refreshTableMenuItem);
108:
109: //Add menu item to delete all records in table
110: String deleteAllRecordsInTableMenuItemLabel = InternationalizationManager
111: .getInstance()
112: .getMessage(
113: "dbbrowser-ui",
114: "dbbrowser-ui-dbbrowser-window-browser-panel-delete-all-records-in-table-menu-item-label",
115: params);
116: JMenuItem deleteAllRecordsInTableMenuItem = new JMenuItem(
117: deleteAllRecordsInTableMenuItemLabel,
118: iconForDeleteAllRecordsInTable);
119: deleteAllRecordsInTableMenuItem
120: .setActionCommand("Delete all records in table");
121: deleteAllRecordsInTableMenuItem
122: .addActionListener(new MouseActionListener());
123: jPopupMenu.add(deleteAllRecordsInTableMenuItem);
124:
125: //Add menu item for drop table
126: String dropTableMenuItemLabel = InternationalizationManager
127: .getInstance()
128: .getMessage(
129: "dbbrowser-ui",
130: "dbbrowser-ui-dbbrowser-window-browser-panel-drop-table-menu-item-label",
131: params);
132: JMenuItem dropTableMenuItem = new JMenuItem(
133: dropTableMenuItemLabel,
134: iconForDropTable);
135: dropTableMenuItem
136: .setActionCommand("Drop table");
137: dropTableMenuItem
138: .addActionListener(new MouseActionListener());
139: jPopupMenu.add(dropTableMenuItem);
140:
141: jPopupMenu.show(tree, me.getX(), me.getY());
142: } else if (path != null
143: && TreeNode.SCHEMA_TYPE.equals(treeNode
144: .getType())) {
145: //Refreshing all tables in the schema
146: Object[] params = new Object[] { treeNode
147: .getName() };
148:
149: //Add menu item to refresh schema
150: String REFRESH_SCHEMA_MENU_ITEM = InternationalizationManager
151: .getInstance()
152: .getMessage(
153: "dbbrowser-ui",
154: "dbbrowser-ui-dbbrowser-window-browser-panel-refresh-table-menu-item-label",
155: params);
156: JMenuItem refreshSchemaMenuItem = new JMenuItem(
157: REFRESH_SCHEMA_MENU_ITEM,
158: iconForRefreshTable);
159: refreshSchemaMenuItem
160: .setActionCommand("Refresh");
161: refreshSchemaMenuItem
162: .addActionListener(new MouseActionListener());
163: jPopupMenu.add(refreshSchemaMenuItem);
164:
165: jPopupMenu.show(tree, me.getX(), me.getY());
166: } else if (path != null
167: && TreeNode.VIEWS_TYPE.equals(treeNode
168: .getType())) {
169: //Refreshing all tables in the schema
170: Object[] params = new Object[] { treeNode
171: .getName() };
172:
173: //Add menu item to refresh schema
174: String REFRESH_SCHEMA_MENU_ITEM = InternationalizationManager
175: .getInstance()
176: .getMessage(
177: "dbbrowser-ui",
178: "dbbrowser-ui-dbbrowser-window-browser-panel-refresh-table-menu-item-label",
179: params);
180: JMenuItem refreshSchemaMenuItem = new JMenuItem(
181: REFRESH_SCHEMA_MENU_ITEM,
182: iconForRefreshTable);
183: refreshSchemaMenuItem
184: .setActionCommand("Refresh");
185: refreshSchemaMenuItem
186: .addActionListener(new MouseActionListener());
187: jPopupMenu.add(refreshSchemaMenuItem);
188:
189: jPopupMenu.show(tree, me.getX(), me.getY());
190: } else if (path != null
191: && TreeNode.VIEW_TYPE.equals(treeNode
192: .getType())) {
193: Object[] params = new Object[] { treeNode
194: .getName() };
195:
196: //Add menu item to refresh table
197: String REFRESH_TABLE_MENU_ITEM = InternationalizationManager
198: .getInstance()
199: .getMessage(
200: "dbbrowser-ui",
201: "dbbrowser-ui-dbbrowser-window-browser-panel-refresh-table-menu-item-label",
202: params);
203: JMenuItem refreshTableMenuItem = new JMenuItem(
204: REFRESH_TABLE_MENU_ITEM,
205: iconForRefreshTable);
206: refreshTableMenuItem
207: .setActionCommand("Refresh");
208: refreshTableMenuItem
209: .addActionListener(new MouseActionListener());
210: jPopupMenu.add(refreshTableMenuItem);
211:
212: //Add menu item for drop table
213: String dropViewMenuItemLabel = InternationalizationManager
214: .getInstance()
215: .getMessage(
216: "dbbrowser-ui",
217: "dbbrowser-ui-dbbrowser-window-browser-panel-drop-view-menu-item-label",
218: params);
219: JMenuItem dropTableMenuItem = new JMenuItem(
220: dropViewMenuItemLabel, iconForDropTable);
221: dropTableMenuItem.setActionCommand("Drop view");
222: dropTableMenuItem
223: .addActionListener(new MouseActionListener());
224: jPopupMenu.add(dropTableMenuItem);
225:
226: jPopupMenu.show(tree, me.getX(), me.getY());
227: }
228: }
229: }
230: }
231: }
232:
233: private class MouseActionListener implements ActionListener {
234: public void actionPerformed(ActionEvent e) {
235: if ("Refresh".equals(e.getActionCommand())) {
236: TreeSelectionEvent event = new TreeSelectionEvent(tree,
237: tree.getSelectionPath(), false, null, null);
238: treeSelectionListener.valueChanged(event);
239: } else if ("Drop table".equals(e.getActionCommand())) {
240: //Get last selected table name
241: DefaultMutableTreeNode node = getLastSelectedPathComponent();
242: Object selectedUserObject = node.getUserObject();
243: TreeNode treeNode = (TreeNode) selectedUserObject;
244:
245: //Confirm drop table
246: Object[] params = new Object[] { treeNode.getName() };
247: String DROP_TABLE_CONFIRMATION_MESSAGE = InternationalizationManager
248: .getInstance()
249: .getMessage(
250: "dbbrowser-ui",
251: "dbbrowser-ui-dbbrowser-window-drop-table-confirmation-message",
252: params);
253:
254: int ans = JOptionPane.showConfirmDialog(null,
255: DROP_TABLE_CONFIRMATION_MESSAGE, TITLE,
256: JOptionPane.YES_NO_OPTION,
257: JOptionPane.QUESTION_MESSAGE);
258: if (JOptionPane.YES_OPTION == ans) {
259: try {
260: uiControllerForRawSQL.runRawSQL("drop table "
261: + treeNode.getName());
262:
263: //Update the UI
264: DefaultMutableTreeNode parent = ((DefaultMutableTreeNode) node
265: .getParent());
266: parent.remove(node);
267: //Refresh the parent schema node
268: refresh(parent);
269:
270: TreeSelectionEvent event = new TreeSelectionEvent(
271: tree, tree.getSelectionPath(), false,
272: null, null);
273: treeSelectionListener.valueChanged(event);
274: } catch (DBEngineException exc) {
275: Log.getInstance().fatalMessage(
276: exc.getMessage(),
277: TableTreePanel.class.getName());
278: String sqlFailedLabel = InternationalizationManager
279: .getInstance()
280: .getMessage(
281: "dbbrowser-ui",
282: "dbbrowser-ui-dbbrowser-window-sql-failed",
283: null);
284: JOptionPane.showMessageDialog(null,
285: sqlFailedLabel + " - "
286: + exc.getMessage(), TITLE,
287: JOptionPane.ERROR_MESSAGE);
288: }
289: }
290: } else if ("Drop view".equals(e.getActionCommand())) {
291: //Get last selected table name
292: DefaultMutableTreeNode node = getLastSelectedPathComponent();
293: Object selectedUserObject = node.getUserObject();
294: TreeNode treeNode = (TreeNode) selectedUserObject;
295:
296: //Confirm drop table
297: Object[] params = new Object[] { treeNode.getName() };
298: String DROP_TABLE_CONFIRMATION_MESSAGE = InternationalizationManager
299: .getInstance()
300: .getMessage(
301: "dbbrowser-ui",
302: "dbbrowser-ui-dbbrowser-window-drop-table-confirmation-message",
303: params);
304:
305: int ans = JOptionPane.showConfirmDialog(null,
306: DROP_TABLE_CONFIRMATION_MESSAGE, TITLE,
307: JOptionPane.YES_NO_OPTION,
308: JOptionPane.QUESTION_MESSAGE);
309: if (JOptionPane.YES_OPTION == ans) {
310: try {
311: View view = (View) treeNode.getUserObject();
312: if (view != null
313: && view.getSchemaName() != null) {
314: uiControllerForRawSQL
315: .runRawSQL("drop view "
316: + view.getSchemaName()
317: + "." + treeNode.getName());
318: } else {
319: uiControllerForRawSQL
320: .runRawSQL("drop view "
321: + treeNode.getName());
322: }
323:
324: //Update the UI
325: DefaultMutableTreeNode parent = ((DefaultMutableTreeNode) node
326: .getParent());
327: parent.remove(node);
328: //Refresh the parent schema node
329: refresh(parent);
330:
331: TreeSelectionEvent event = new TreeSelectionEvent(
332: tree, tree.getSelectionPath(), false,
333: null, null);
334: treeSelectionListener.valueChanged(event);
335: } catch (DBEngineException exc) {
336: Log.getInstance().fatalMessage(
337: exc.getMessage(),
338: TableTreePanel.class.getName());
339: String sqlFailedLabel = InternationalizationManager
340: .getInstance()
341: .getMessage(
342: "dbbrowser-ui",
343: "dbbrowser-ui-dbbrowser-window-sql-failed",
344: null);
345: JOptionPane.showMessageDialog(null,
346: sqlFailedLabel + " - "
347: + exc.getMessage(), TITLE,
348: JOptionPane.ERROR_MESSAGE);
349: }
350: }
351: } else if ("Delete all records in table".equals(e
352: .getActionCommand())) {
353: //Get last selected table name
354: DefaultMutableTreeNode node = getLastSelectedPathComponent();
355: Object selectedUserObject = node.getUserObject();
356: TreeNode treeNode = (TreeNode) selectedUserObject;
357:
358: //Confirm drop table
359: Object[] params = new Object[] { treeNode.getName() };
360: String DROP_TABLE_CONFIRMATION_MESSAGE = InternationalizationManager
361: .getInstance()
362: .getMessage(
363: "dbbrowser-ui",
364: "dbbrowser-ui-dbbrowser-window-browser-panel-delete-all-records-in-table-menu-item-label",
365: params);
366:
367: int ans = JOptionPane.showConfirmDialog(null,
368: DROP_TABLE_CONFIRMATION_MESSAGE, TITLE,
369: JOptionPane.YES_NO_OPTION,
370: JOptionPane.QUESTION_MESSAGE);
371: if (JOptionPane.YES_OPTION == ans) {
372: try {
373: uiControllerForRawSQL.runRawSQL("delete from "
374: + treeNode.getName());
375:
376: //Update the UI
377: refresh(node);
378:
379: TreeSelectionEvent event = new TreeSelectionEvent(
380: tree, tree.getSelectionPath(), false,
381: null, null);
382: treeSelectionListener.valueChanged(event);
383: } catch (DBEngineException exc) {
384: Log.getInstance().fatalMessage(
385: exc.getMessage(),
386: TableTreePanel.class.getName());
387: String sqlFailedLabel = InternationalizationManager
388: .getInstance()
389: .getMessage(
390: "dbbrowser-ui",
391: "dbbrowser-ui-dbbrowser-window-sql-failed",
392: null);
393: JOptionPane.showMessageDialog(null,
394: sqlFailedLabel + " - "
395: + exc.getMessage(), TITLE,
396: JOptionPane.ERROR_MESSAGE);
397: }
398: }
399: }
400:
401: }
402: }
403:
404: public void refresh(DefaultMutableTreeNode parent) {
405: this .treeModel.reload(parent);
406: this .tree.expandPath(new TreePath(parent));
407: }
408:
409: public void clear(DefaultMutableTreeNode parent) {
410: parent.removeAllChildren();
411: }
412:
413: private void initialize() {
414: //Set up the tree for the list of tablespaces
415: this .setLayout(new BorderLayout());
416:
417: try {
418: //Setup the tree
419: this .populateTree();
420: } catch (DBEngineException exc) {
421: Log.getInstance().fatalMessage(exc.getMessage(),
422: TableTreePanel.class.getName());
423: String sqlFailedLabel = InternationalizationManager
424: .getInstance().getMessage("dbbrowser-ui",
425: "dbbrowser-ui-dbbrowser-window-sql-failed",
426: null);
427: JOptionPane.showMessageDialog(null, sqlFailedLabel + " - "
428: + exc.getMessage(), TITLE,
429: JOptionPane.ERROR_MESSAGE);
430: }
431: }
432:
433: private void populateTree() throws DBEngineException {
434: //Setup the JTree
435: TreeNode rootTreeNode = new TreeNode(this .uicontroller
436: .getConnectionInfo().getName(), TreeNode.ROOT_VIEW_TYPE);
437: DefaultMutableTreeNode database = new DefaultMutableTreeNode(
438: rootTreeNode);
439: this .treeModel = new DefaultTreeModel(database);
440: this .tree = new JTree(treeModel);
441: this .tree.setShowsRootHandles(true);
442: this .tree.addMouseListener(new PopupDecorator());
443: this .tree.addTreeSelectionListener(this .treeSelectionListener);
444:
445: //Set the tree cell renderer
446: JScrollPane pane = new JScrollPane(this .tree);
447: Dimension minimumSize = new Dimension(200, 100);
448: pane.setMinimumSize(minimumSize);
449: this .add(pane);
450:
451: //Setup tables and schemas
452: TreeNode schemasNode = new TreeNode("Schemas",
453: TreeNode.SCHEMAS_TYPE);
454: DefaultMutableTreeNode schemasTreeNode = new DefaultMutableTreeNode(
455: schemasNode);
456: database.add(schemasTreeNode);
457:
458: //Setup views
459: TreeNode viewsNode = new TreeNode("Views", TreeNode.VIEWS_TYPE);
460: DefaultMutableTreeNode viewsTreeNode = new DefaultMutableTreeNode(
461: viewsNode);
462: database.add(viewsTreeNode);
463: }
464: }
|