001: /*
002: * ConnectionInfoPanel.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.tools;
013:
014: import java.awt.Toolkit;
015: import java.awt.datatransfer.Clipboard;
016: import java.awt.datatransfer.StringSelection;
017: import java.io.StringWriter;
018: import java.sql.DatabaseMetaData;
019: import javax.swing.JEditorPane;
020: import javax.swing.JFrame;
021: import javax.swing.text.Document;
022: import workbench.WbManager;
023: import workbench.db.DbMetadata;
024: import workbench.db.WbConnection;
025: import workbench.gui.WbSwingUtilities;
026: import workbench.gui.components.TextComponentMouseListener;
027: import workbench.gui.components.ValidatingDialog;
028: import workbench.log.LogMgr;
029: import workbench.resource.ResourceMgr;
030: import workbench.resource.Settings;
031: import workbench.util.StringUtil;
032:
033: /**
034: *
035: * @author support@sql-workbench.net
036: */
037: public class ConnectionInfoPanel extends javax.swing.JPanel {
038:
039: public ConnectionInfoPanel(WbConnection conn) {
040: initComponents();
041:
042: try {
043: StringBuilder content = new StringBuilder();
044: content.append("<html>");
045:
046: DatabaseMetaData meta = conn.getSqlConnection()
047: .getMetaData();
048: DbMetadata wbmeta = conn.getMetadata();
049:
050: content.append("<b>"
051: + ResourceMgr.getString("LblDbProductName")
052: + ":</b> " + meta.getDatabaseProductName()
053: + "<br>\r\n");
054: content.append("<b>"
055: + ResourceMgr.getString("LblDbProductInfo")
056: + ":</b> " + meta.getDatabaseProductVersion()
057: + "<br>\r\n");
058: content
059: .append("<b>"
060: + ResourceMgr
061: .getString("LblDbProductVersion")
062: + ":</b> " + conn.getDatabaseVersion()
063: + "<br>\r\n");
064: content.append("<b>"
065: + ResourceMgr.getString("LblDriverInfoName")
066: + ":</b> " + meta.getDriverName() + "<br>\r\n");
067: content.append("<b>"
068: + ResourceMgr.getString("LblDriverInfoVersion")
069: + ":</b> " + meta.getDriverVersion() + "<br>\r\n");
070: content.append("<b>" + ResourceMgr.getString("LblDbURL")
071: + ":</b> " + conn.getUrl() + "<br>\r\n");
072: content.append("<b>" + ResourceMgr.getString("LblUsername")
073: + ":</b> " + conn.getCurrentUser() + "<br>\r\n");
074: content.append("<b>"
075: + StringUtil.capitalize(wbmeta.getSchemaTerm())
076: + ":</b> "
077: + getDisplayValue(conn.getCurrentSchema())
078: + "<br>\r\n");
079: content.append("<b>"
080: + StringUtil.capitalize(wbmeta.getCatalogTerm())
081: + ":</b> "
082: + getDisplayValue(wbmeta.getCurrentCatalog())
083: + "<br>\r\n");
084: content.append("</html>");
085: infotext.setContentType("text/html");
086: infotext.setFont(Settings.getInstance().getEditorFont());
087: infotext.putClientProperty(
088: JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
089: infotext.setText(content.toString());
090: infotext.setCaretPosition(0);
091: new TextComponentMouseListener(infotext);
092: } catch (Exception e) {
093: LogMgr.logError("ConnectionInfoPanel",
094: "Could not read connection properties", e);
095: }
096: }
097:
098: private String getDisplayValue(String value) {
099: if (value == null)
100: return "";
101: return value;
102: }
103:
104: public static void showConnectionInfo(WbConnection con) {
105: ConnectionInfoPanel p = new ConnectionInfoPanel(con);
106: JFrame f = WbManager.getInstance().getCurrentWindow();
107: ValidatingDialog d = new ValidatingDialog(f, ResourceMgr
108: .getString("LblConnInfo"), p, false);
109: d.setSize(450, 350);
110: WbSwingUtilities.center(d, f);
111: d.setVisible(true);
112: d.dispose();
113: }
114:
115: /** This method is called from within the constructor to
116: * initialize the form.
117: * WARNING: Do NOT modify this code. The content of this method is
118: * always regenerated by the Form Editor.
119: */
120: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
121: private void initComponents() {
122: java.awt.GridBagConstraints gridBagConstraints;
123:
124: jScrollPane1 = new javax.swing.JScrollPane();
125: infotext = new InfoEditorPane();
126: copyButton = new javax.swing.JButton();
127:
128: setLayout(new java.awt.GridBagLayout());
129:
130: infotext.setContentType("text/html");
131: infotext.setEditable(false);
132: infotext.setFont(new java.awt.Font("Dialog", 0, 11));
133: jScrollPane1.setViewportView(infotext);
134:
135: gridBagConstraints = new java.awt.GridBagConstraints();
136: gridBagConstraints.gridx = 0;
137: gridBagConstraints.gridy = 0;
138: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
139: gridBagConstraints.weightx = 1.0;
140: gridBagConstraints.weighty = 1.0;
141: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
142: add(jScrollPane1, gridBagConstraints);
143:
144: copyButton.setText(ResourceMgr.getString("LblCopyInfo"));
145: copyButton.setToolTipText(ResourceMgr
146: .getDescription("LblCopyInfo"));
147: copyButton
148: .addActionListener(new java.awt.event.ActionListener() {
149: public void actionPerformed(
150: java.awt.event.ActionEvent evt) {
151: copyButtonActionPerformed(evt);
152: }
153: });
154: gridBagConstraints = new java.awt.GridBagConstraints();
155: gridBagConstraints.gridx = 0;
156: gridBagConstraints.gridy = 1;
157: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
158: add(copyButton, gridBagConstraints);
159: }// </editor-fold>//GEN-END:initComponents
160:
161: private void copyButtonActionPerformed(
162: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyButtonActionPerformed
163: String content = infotext.getText();
164: copyText(content);
165: }//GEN-LAST:event_copyButtonActionPerformed
166:
167: public void copyText(String content) {
168: String clean = content.replaceAll(StringUtil.REGEX_CRLF, " ");
169: clean = clean.replaceAll(" {2,}", " ");
170: clean = clean.replaceAll("\\<br\\>", "\r\n");
171: clean = clean.replaceAll("\\<[/a-z]*\\>", "").trim();
172: Clipboard clp = Toolkit.getDefaultToolkit()
173: .getSystemClipboard();
174: StringSelection sel = new StringSelection(clean);
175: clp.setContents(sel, sel);
176: }
177:
178: // Variables declaration - do not modify//GEN-BEGIN:variables
179: private javax.swing.JButton copyButton;
180: private javax.swing.JEditorPane infotext;
181: private javax.swing.JScrollPane jScrollPane1;
182:
183: // End of variables declaration//GEN-END:variables
184:
185: private class InfoEditorPane extends JEditorPane {
186: public InfoEditorPane() {
187: super ();
188: }
189:
190: public String getSelection() {
191: Document doc = getDocument();
192: int start = Math.min(getCaret().getDot(), getCaret()
193: .getMark());
194: int end = Math.max(getCaret().getDot(), getCaret()
195: .getMark());
196: if (start == end)
197: return null;
198: StringWriter out = new StringWriter();
199: try {
200: int len = getSelectionEnd() - getSelectionStart();
201: getUI().getEditorKit(this ).write(out, doc, start,
202: end - start);
203: } catch (Exception e) {
204: return null;
205: }
206: return out.toString();
207: }
208:
209: public void copy() {
210: String content = getSelection();
211: if (content == null)
212: content = getText();
213: copyText(content);
214: }
215: }
216: }
|