001: /*
002: * ConnectionInfo.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.Color;
015: import java.awt.GridLayout;
016: import java.awt.event.ActionEvent;
017: import java.awt.event.ActionListener;
018: import java.beans.PropertyChangeEvent;
019: import java.beans.PropertyChangeListener;
020: import javax.swing.JComponent;
021: import javax.swing.JTextField;
022: import workbench.db.ConnectionProfile;
023: import workbench.db.WbConnection;
024: import workbench.gui.WbSwingUtilities;
025: import workbench.gui.actions.WbAction;
026: import workbench.gui.tools.ConnectionInfoPanel;
027: import workbench.resource.ResourceMgr;
028:
029: /**
030: * @author support@sql-workbench.net
031: */
032: public class ConnectionInfo extends JComponent implements
033: PropertyChangeListener, ActionListener {
034: private JTextField display;
035: private WbConnection sourceConnection;
036: private Color defaultBackground;
037:
038: public ConnectionInfo(Color aBackground) {
039: super ();
040:
041: this .display = new JTextField();
042:
043: this .setLayout(new GridLayout(1, 1, 0, 0));
044: this .add(this .display);
045: super .setBackground(aBackground);
046: this .defaultBackground = aBackground;
047: this .display.setEditable(false);
048: this .display.setBorder(null);
049: TextComponentMouseListener l = new TextComponentMouseListener();
050: WbAction a = new WbAction(this , "show-info");
051: a.setMenuTextByKey("MnuTxtConnInfo");
052: l.addAction(a);
053: this .display.addMouseListener(l);
054: }
055:
056: public void setConnection(WbConnection aConnection) {
057: if (this .sourceConnection != null) {
058: this .sourceConnection.removeChangeListener(this );
059: }
060:
061: this .sourceConnection = aConnection;
062:
063: Color bkg = null;
064:
065: if (this .sourceConnection != null) {
066: this .sourceConnection.addChangeListener(this );
067: ConnectionProfile p = aConnection.getProfile();
068: if (p != null) {
069: bkg = p.getInfoDisplayColor();
070: }
071: }
072:
073: final Color newBackground = bkg;
074:
075: WbSwingUtilities.invoke(new Runnable() {
076: public void run() {
077: setInfoColor(newBackground);
078: updateDisplay();
079: }
080: });
081: }
082:
083: private void setInfoColor(Color c) {
084: if (c == null) {
085: this .setBackground(this .defaultBackground);
086: } else {
087: this .setBackground(c);
088: }
089: }
090:
091: public void setBackground(Color c) {
092: super .setBackground(c);
093: if (this .display != null) {
094: this .display.setBackground(c);
095: }
096: }
097:
098: private void updateDisplay() {
099: if (this .sourceConnection != null) {
100: this .display.setText(" "
101: + this .sourceConnection.getDisplayString());
102: StringBuilder tip = new StringBuilder(30);
103: tip.append("<html>");
104: tip.append(this .sourceConnection.getDatabaseProductName());
105: tip.append(" ");
106: tip.append(this .sourceConnection.getDatabaseVersion());
107: tip.append("<br>");
108: tip.append(ResourceMgr.getFormattedString("TxtDrvVersion",
109: this .sourceConnection.getDriverVersion()));
110: tip.append("</html>");
111: this .display.setToolTipText(tip.toString());
112: } else {
113: this .display.setText(" "
114: + ResourceMgr.getString("TxtNotConnected"));
115: this .display.setToolTipText(null);
116: }
117: }
118:
119: public void propertyChange(PropertyChangeEvent evt) {
120: if (evt.getSource() == this .sourceConnection
121: && (WbConnection.PROP_CATALOG.equals(evt
122: .getPropertyName()) || WbConnection.PROP_SCHEMA
123: .equals(evt.getPropertyName()))) {
124: this .updateDisplay();
125: }
126: }
127:
128: public void actionPerformed(ActionEvent e) {
129: ConnectionInfoPanel.showConnectionInfo(sourceConnection);
130: }
131:
132: }
|