📄 connectionstreepanel.java
字号:
/* * ConnectionsTreePanel.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */package org.executequery.gui.browser;import java.awt.BorderLayout;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.SQLException;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Vector;import javax.swing.JButton;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.SwingUtilities;import javax.swing.event.TreeExpansionEvent;import javax.swing.event.TreeExpansionListener;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.TreeNode;import javax.swing.tree.TreePath;import org.executequery.ConnectionProperties;import org.executequery.EventMediator;import org.executequery.GUIUtilities;import org.executequery.SystemUtilities;import org.underworldlabs.swing.tree.DynamicTree;import org.underworldlabs.swing.toolbar.PanelToolBar;import org.executequery.event.ConnectionEvent;import org.executequery.event.ConnectionListener;import org.executequery.databasemediators.DatabaseConnection;import org.executequery.databasemediators.MetaDataValues;import org.executequery.gui.AbstractDockedTabActionPanel;import org.executequery.components.table.BrowserTreeCellRenderer;import org.executequery.gui.BaseDialog;import org.executequery.gui.importexport.ImportExportDelimitedPanel;import org.executequery.gui.importexport.ImportExportExcelPanel;import org.executequery.gui.importexport.ImportExportProcess;import org.executequery.gui.importexport.ImportExportXMLPanel;import org.executequery.util.Log;import org.underworldlabs.swing.GUIUtils;import org.underworldlabs.swing.actions.ReflectiveAction;import org.underworldlabs.swing.util.IconUtilities;import org.underworldlabs.util.MiscUtils;import org.underworldlabs.swing.util.SwingWorker;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * * @author Takis Diakoumis * @version $Revision: 1.27 $ * @date $Date: 2006/09/21 14:36:14 $ */public class ConnectionsTreePanel extends AbstractDockedTabActionPanel implements ConnectionListener, TreeExpansionListener, TreeSelectionListener { /** the title for this panel */ public static final String TITLE = "Connections"; /** the tree object */ private DynamicTree tree; /** saved connections */ private Vector<DatabaseConnection> connections; /** Worker for meta data retrieval */ private SwingWorker worker; /** the browser's control object */ private BrowserController controller; /** the old selected browser node */ private TreePath oldSelectionPath; /** the tree popup menu */ private PopMenu popupMenu; /** indicates whether to reselect the connection * properties node following a disconnection event. */ private boolean rootSelectOnDisconnect; // ------------------------------------- // tool bar buttons /** move connection up button */ private JButton upButton; /** move connection down button */ private JButton downButton; /** the reload node button */ private JButton reloadButton; /** new connection button */ private JButton newConnectionButton; /** delete connection button */ private JButton deleteConnectionButton; // ------------------------------------- /** Creates a new instance of ConnectionsPanel */ public ConnectionsTreePanel() { super(new BorderLayout()); init(); } private void init() { // default to not select the root node after a disconnection rootSelectOnDisconnect = false; // initialise the controller controller = new BrowserController(this); // setup the root node - simple label only DatabaseObject rootObject = new DatabaseObject(BrowserConstants.ROOT_NODE, "Database Connections"); DefaultMutableTreeNode root = new DefaultMutableTreeNode(rootObject); // loop through available connections and add as leaf objects connections = ConnectionProperties.getConnectionsVector(); for (int i = 0, k = connections.size(); i < k; i++) { DatabaseConnection dc = connections.elementAt(i); ConnectionObject object = new ConnectionObject(dc); BrowserTreeNode node = new BrowserTreeNode(object, true); root.add(node); } // load the tree and panel icons Map icons = new HashMap(); for (int i = 0; i < BrowserConstants.NODE_ICONS.length; i++) { icons.put(BrowserConstants.NODE_ICONS[i], GUIUtilities.loadIcon(BrowserConstants.NODE_ICONS[i], true)); } icons.put(BrowserConstants.DATABASE_OBJECT_IMAGE, GUIUtilities.loadIcon(BrowserConstants.DATABASE_OBJECT_IMAGE, true)); tree = new DynamicTree(root); tree.setCellRenderer(new BrowserTreeCellRenderer(icons)); tree.addTreeSelectionListener(this); tree.addTreeExpansionListener(this); tree.addMouseListener(new MouseHandler()); // --------------------------------------- // the tool bar PanelToolBar tools = new PanelToolBar(); newConnectionButton = tools.addButton( this, "newConnection", GUIUtilities.getAbsoluteIconPath("NewConnection16.gif"), "New connection"); deleteConnectionButton = tools.addButton( this, "deleteConnection", GUIUtilities.getAbsoluteIconPath("Delete16.gif"), "Remove connection"); upButton = tools.addButton( this, "moveConnectionUp", GUIUtilities.getAbsoluteIconPath("Up16.gif"), "Move up"); downButton = tools.addButton( this, "moveConnectionDown", GUIUtilities.getAbsoluteIconPath("Down16.gif"), "Move down"); reloadButton = tools.addButton( this, "reloadSelection", GUIUtilities.getAbsoluteIconPath("Reload16.gif"), "Reload the currently selected node"); // add the tools and tree to the panel add(tools, BorderLayout.NORTH); add(new JScrollPane(tree), BorderLayout.CENTER); // register with the event listener EventMediator.registerListener(EventMediator.CONNECTION_EVENT, this); enableButtons(false); tree.setSelectionRow(0); } private void enableButtons(final boolean enable) { SwingUtilities.invokeLater(new Runnable() { public void run() { upButton.setEnabled(enable); downButton.setEnabled(enable); reloadButton.setEnabled(!enable); deleteConnectionButton.setEnabled(enable); } }); } /** * Moves the selected connection (host node) up in the list. */ public void moveConnectionUp() { tree.moveSelectionUp(); // adjust the position of the connection Object object = tree.getLastPathComponent(); moveNode((BrowserTreeNode)object, DynamicTree.MOVE_UP); } /** * Sets the selected connection tree node to the * specified database connection. * * @param dc - the database connection to select * @param reload - if true, ensures the view panel is set to * the connection properties */ public void setSelectedConnection(DatabaseConnection dc, boolean reload) { try { reloadView = true; setSelectedConnection(dc); } finally { reloadView = false; } } /** * Sets the selected connection tree node to the * specified database connection. * * @param dc - the database connection to select */ public void setSelectedConnection(DatabaseConnection dc) { DefaultMutableTreeNode node = null; // retrieve the root node and loop through DefaultMutableTreeNode root = tree.getRootNode(); for (Enumeration i = root.children(); i.hasMoreElements();) { DefaultMutableTreeNode _node = (DefaultMutableTreeNode)i.nextElement(); Object userObject = _node.getUserObject(); // make sure its a connection object if (userObject instanceof ConnectionObject) { ConnectionObject object = (ConnectionObject)userObject; if (object.getDatabaseConnection() == dc) { node = _node; break; } } } // select the node path if (node != null) { TreePath path = new TreePath(node.getPath()); tree.scrollPathToVisible(path); tree.setSelectionPath(path); if (reloadView) { Object object = tree.getLastPathComponent(); if (object instanceof BrowserTreeNode) { controller.valueChanged( getSelectedMetaObject(), (BrowserTreeNode)object, false); } } } } /** * Deletes the selected connection (host node) from the list. */ public void deleteConnection() { deleteConnection(null); } /** * Deletes the specified connection (host node) from the list. * * @param the node representing the connection to be removed */ public void deleteConnection(BrowserTreeNode node) { boolean isSelectedNode = false; if (node == null) { Object object = tree.getLastPathComponent(); node = (BrowserTreeNode)object; isSelectedNode = true; } else { if (tree.getLastPathComponent() == node) { isSelectedNode = true; } } int yesNo = GUIUtilities.displayConfirmCancelDialog( "Are you sure you want to delete the connection " + node + " ?"); if (yesNo != JOptionPane.YES_OPTION) { return; } ConnectionObject metaObject = (ConnectionObject)node.getDatabaseUserObject(); DatabaseConnection dc = metaObject.getDatabaseConnection(); // the next selection index will be the index of // the one being removed - (index - 1) int index = connections.indexOf(dc); if (index == -1) { return; } // remove from the connections connections.remove(index); if (index > connections.size() - 1) { index = connections.size() - 1; } if (isSelectedNode) { String prefix = connections.get(index).getName(); tree.removeNode(node, prefix); } else { tree.removeNode(node); } // save the connections controller.saveConnections(); } /** * Returns the database connection at the specified point. * * @return the connection properties object */ protected DatabaseConnection getConnectionAt(Point point) { TreePath path = tree.getPathForLocation(point.x, point.y); return getConnectionAt(tree.getPathForLocation(point.x, point.y)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -