From 8a852409aa5b38c631b09d0a661bcc90ec29d59d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E4=B8=80=E6=A2=93?= <白一梓> Date: Sun, 26 Aug 2012 16:28:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86jconsole?= =?UTF-8?q?=E7=9B=91=E6=8E=A7=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jconsole/DruidDataSourcePanel.java | 93 ++++++-- .../support/jconsole/DruidDriverPanel.java | 73 +++++-- .../druid/support/jconsole/DruidPanel.java | 200 ++++++++++++++---- .../druid/support/jconsole/DruidSQLPanel.java | 145 +++++++++---- .../jconsole/model/DruidTableModel.java | 101 +++++++++ .../jconsole/model/RowHeaderTable.java | 142 +++++++++++++ .../jconsole/util/TableDataProcessor.java | 72 +++++++ 7 files changed, 710 insertions(+), 116 deletions(-) create mode 100644 src/main/java/com/alibaba/druid/support/jconsole/model/DruidTableModel.java create mode 100644 src/main/java/com/alibaba/druid/support/jconsole/model/RowHeaderTable.java create mode 100644 src/main/java/com/alibaba/druid/support/jconsole/util/TableDataProcessor.java diff --git a/src/main/java/com/alibaba/druid/support/jconsole/DruidDataSourcePanel.java b/src/main/java/com/alibaba/druid/support/jconsole/DruidDataSourcePanel.java index 8caac6e11c..8dee034f71 100644 --- a/src/main/java/com/alibaba/druid/support/jconsole/DruidDataSourcePanel.java +++ b/src/main/java/com/alibaba/druid/support/jconsole/DruidDataSourcePanel.java @@ -1,22 +1,71 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.support.jconsole; - -public class DruidDataSourcePanel extends DruidPanel { - - private static final long serialVersionUID = 1L; - -} +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.support.jconsole; + +import java.util.ArrayList; +import java.util.LinkedHashMap; + +import com.alibaba.druid.support.jconsole.model.DruidTableModel; +import com.alibaba.druid.support.jconsole.model.RowHeaderTable; +import com.alibaba.druid.support.jconsole.util.TableDataProcessor; +import com.alibaba.druid.support.jconsole.util.TableDataProcessor.ColumnData; + +/** + * 请求地址:/datasource.json + * + * 请求返回的json格式: + * {"ResultCode":1, + * "Content": + * [{"Identity":31375837,"Name":"DataSource-31375837","DbType":"mysql", + * "DriverClassName":"com.mysql.jdbc.Driver", + * "URL":"jdbc:mysql://localhost:3306/dragoon_v25_masterdb","UserName":"root", + * "FilterClassNames":["com.alibaba.druid.filter.stat.StatFilter"], + * "WaitThreadCount":0,"NotEmptyWaitCount":0,"NotEmptyWaitMillis":0,"PoolingCount":1, + * "PoolingPeak":0,"ActiveCount":0,"ActivePeak":1,"ActivePeakTime":"Tue Aug 21 21:22:33 CST 2012", + * "InitialSize":1,"MinIdle":1,"MaxActive":20,"QueryTimeout":0,"TransactionQueryTimeout":0, + * "LoginTimeout":0,"ValidConnectionCheckerClassName":"com.alibaba.druid.pool.vendor.MySqlValidConnectionChecker", + * "ExceptionSorterClassName":"com.alibaba.druid.pool.vendor.MySqlExceptionSorter", + * "TestOnBorrow":false,"TestOnReturn":false,"TestWhileIdle":true,"DefaultAutoCommit":true, + * "DefaultReadOnly":true,"LogicConnectCount":93,"LogicCloseCount":93,"LogicConnectErrorCount":0, + * "PhysicalConnectCount":1,"PhysicalCloseCount":0,"PhysicalConnectErrorCount":0, + * "ExecuteCount":93,"ErrorCount":0,"CommitCount":0,"RollbackCount":0,"PSCacheAccessCount":31, + * "PSCacheHitCount":30,"PSCacheMissCount":1,"StartTransactionCount":0, + * "TransactionHistogram":[0,0,0,0,0,0],"ConnectionHoldTimeHistogram":[0,0,48,45,0,0,0,0], + * "RemoveAbandoned":false + * }] + * } + * */ +public class DruidDataSourcePanel extends DruidPanel { + + private static final long serialVersionUID = 1L; + private static final String REQUEST_URL = "/datasource.json"; + private static final String KEY_WORD_IDENTITY = "Identity"; + + public DruidDataSourcePanel() { + super(); + this.url = REQUEST_URL; + } + @Override + protected void tableDataProcess( + ArrayList> data) { + ColumnData columnData = TableDataProcessor.row2col(data, KEY_WORD_IDENTITY); + tableModel = new DruidTableModel(columnData.getDatas()); + table.setModel(tableModel); + RowHeaderTable header = new RowHeaderTable( + columnData.getNames(),table,50,columnData.getCount()); + scrollPane.setRowHeaderView(header); + } +} diff --git a/src/main/java/com/alibaba/druid/support/jconsole/DruidDriverPanel.java b/src/main/java/com/alibaba/druid/support/jconsole/DruidDriverPanel.java index 1fb1443edd..21f3fb613e 100644 --- a/src/main/java/com/alibaba/druid/support/jconsole/DruidDriverPanel.java +++ b/src/main/java/com/alibaba/druid/support/jconsole/DruidDriverPanel.java @@ -1,22 +1,51 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.support.jconsole; - -public class DruidDriverPanel extends DruidPanel { - - private static final long serialVersionUID = 1L; - -} +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.support.jconsole; + +import java.util.ArrayList; +import java.util.LinkedHashMap; + +import com.alibaba.druid.support.jconsole.model.DruidTableModel; + +/** + * 请求地址:/basic.json + * + * 请求返回的json格式: + * {"ResultCode":1,"Content": + * {"Version":"0.2.6","Drivers": + * ["sun.jdbc.odbc.JdbcOdbcDriver","com.alibaba.druid.mock.MockDriver", + * "com.mysql.jdbc.Driver","com.alibaba.druid.proxy.DruidDriver"] + * } + * } + * */ +public class DruidDriverPanel extends DruidPanel { + + private static final long serialVersionUID = 1L; + private static final String REQUEST_URL = "/basic.json"; + + public DruidDriverPanel() { + super(); + url = REQUEST_URL; + } + + @Override + protected void tableDataProcess( + ArrayList> data) { + tableModel = new DruidTableModel(data); + table.setModel(tableModel); + } +} + diff --git a/src/main/java/com/alibaba/druid/support/jconsole/DruidPanel.java b/src/main/java/com/alibaba/druid/support/jconsole/DruidPanel.java index f525873b5b..a7882a9ddf 100644 --- a/src/main/java/com/alibaba/druid/support/jconsole/DruidPanel.java +++ b/src/main/java/com/alibaba/druid/support/jconsole/DruidPanel.java @@ -1,36 +1,164 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.support.jconsole; - -import javax.management.MBeanServerConnection; -import javax.swing.JPanel; - -import com.sun.tools.jconsole.JConsoleContext; - -public class DruidPanel extends JPanel { - - private static final long serialVersionUID = 1L; - - protected Object doInBackground(JConsoleContext context) throws Exception { - doInBackground(context.getMBeanServerConnection()); - - return null; - } - - protected void doInBackground(MBeanServerConnection conn) throws Exception { - System.out.println("doInBackground"); - } -} +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.support.jconsole; + +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.util.ArrayList; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import javax.management.MBeanServerConnection; +import javax.management.ObjectName; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; + +import com.alibaba.druid.stat.DruidStatService; +import com.alibaba.druid.support.jconsole.model.DruidTableModel; +import com.alibaba.druid.support.json.JSONUtils; +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; +import com.sun.tools.jconsole.JConsoleContext; + +public abstract class DruidPanel extends JPanel { + + private static final long serialVersionUID = 1L; + protected static final int RESP_SUCCESS_RESULT = 1; + protected static final String RESP_JSON_RESULT_KEY = "ResultCode"; + protected static final String RESP_JSON_CONTENT_KEY = "Content"; + protected static final long DEFAULT_ACTIVE_TIME = 5*60*1000; + + protected static final int DRUID_TABLE_WIDTH = 600; + protected static final int DRUID_TABLE_HEIGHT = 500; + + protected JScrollPane scrollPane; + protected DruidTableModel tableModel; + protected JTable table; + protected String url; + + /** 界面刷新的间隔时间,单位为毫秒. */ + protected long activeTime; + protected long lastRefreshTime; + private final static Log LOG = LogFactory.getLog(DruidPanel.class); + + protected DruidPanel(long activeTime) { + this.activeTime = activeTime; + } + + protected DruidPanel() { + activeTime = DEFAULT_ACTIVE_TIME; + } + + @SuppressWarnings("unchecked") + protected ArrayList> parseData(Object respData) { + ArrayList> data = null; + if (respData instanceof Map) { + LinkedHashMap map = (LinkedHashMap)respData; + int rv = (Integer)map.get(RESP_JSON_RESULT_KEY); + + if (rv == RESP_SUCCESS_RESULT) { + Object content = map.get(RESP_JSON_CONTENT_KEY); + if (content instanceof List) { + data = (ArrayList>)content; + } else if (content instanceof Map) { + LinkedHashMap contentEle + = (LinkedHashMap)content; + data = new ArrayList>(); + data.add(contentEle); + } + } + } + return data; + } + + protected Object getData(String url,MBeanServerConnection conn) throws Exception { + Object o = null; + ObjectName name = new ObjectName(DruidStatService.MBEAN_NAME); + + String result = (String) conn.invoke(name, "service", + new String[] { url }, new String[] { String.class.getName() }); + o = JSONUtils.parse(result); + if (LOG.isDebugEnabled()) { + LOG.debug(o.toString()); + } + return o; + } + + protected abstract void tableDataProcess(ArrayList> data); + + protected void addOrRefreshTable(String url,MBeanServerConnection conn) throws Exception { + if (url != null) { + boolean needRefresh = false; + long timeNow = new Date().getTime(); + if (scrollPane == null) { + table = new JTable(); + + scrollPane = new JScrollPane(table); + scrollPane.setAutoscrolls(true); + GridBagLayout gridbag = new GridBagLayout(); + GridBagConstraints c = new GridBagConstraints(); + setLayout(gridbag); + + c.fill = GridBagConstraints.BOTH; + c.gridx = 0; + c.gridy = 0; + c.weightx = 9; + c.weighty = 9; + + gridbag.setConstraints(scrollPane, c); + + this.add(scrollPane); + needRefresh = true; + lastRefreshTime = timeNow; + } else { + if (lastRefreshTime + activeTime < timeNow) { + needRefresh = true; + lastRefreshTime = timeNow; + } + } + if (needRefresh) { + LOG.debug("refresh" + timeNow); + ArrayList> data = parseData(getData( + url, conn)); + if (data != null) { + tableDataProcess(data); + } + } + + } else { + // url不存在 + LOG.warn("url不存在"); + } + } + + protected Object doInBackground(JConsoleContext context) throws Exception { + doInBackground(context.getMBeanServerConnection()); + + return null; + } + + protected void doInBackground(MBeanServerConnection conn) { + + try { + addOrRefreshTable(url,conn); + } catch (Exception e) { + LOG.warn("", e); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/alibaba/druid/support/jconsole/DruidSQLPanel.java b/src/main/java/com/alibaba/druid/support/jconsole/DruidSQLPanel.java index 133ea50f05..b2500ff33c 100644 --- a/src/main/java/com/alibaba/druid/support/jconsole/DruidSQLPanel.java +++ b/src/main/java/com/alibaba/druid/support/jconsole/DruidSQLPanel.java @@ -1,36 +1,109 @@ -/* - * Copyright 1999-2011 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.support.jconsole; - -import javax.management.MBeanServerConnection; -import javax.management.ObjectName; - -import com.alibaba.druid.stat.DruidStatService; -import com.alibaba.druid.support.json.JSONUtils; - -public class DruidSQLPanel extends DruidPanel { - - private static final long serialVersionUID = 1L; - - protected void doInBackground(MBeanServerConnection conn) throws Exception { - ObjectName name = new ObjectName(DruidStatService.MBEAN_NAME); - String url = "/sql.json"; - String result = (String) conn.invoke(name, "service", new String[] { url }, new String[] { String.class.getName() }); - Object o = JSONUtils.parse(result); - - System.out.println(o); - } -} +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.support.jconsole; + +import java.util.ArrayList; +import java.util.LinkedHashMap; + +import com.alibaba.druid.support.jconsole.model.DruidTableModel; +import com.alibaba.druid.support.jconsole.model.RowHeaderTable; +/** + * 请求地址 /sql.json + * + * 返回数据格式: + *
+ * {
+    "ResultCode": 1,
+    "Content": [
+        {
+            "BatchSizeMax": 0,
+            "BatchSizeTotal": 0,
+            "ConcurrentMax": 1,
+            "DbType": "h2",
+            "EffectedRowCount": 18,
+            "EffectedRowCountHistogram": [
+                0,6,0,0,0,0
+            ],
+            "EffectedRowCountMax": 4,
+            "ErrorCount": 0,
+            "ExecuteAndResultHoldTimeHistogram": [
+                5,0,1,0,0,0,0,0
+            ],
+            "ExecuteAndResultSetHoldTime": 0,
+            "ExecuteCount": 6,
+            "FetchRowCount": 0,
+            "FetchRowCountHistogram": [
+                6,0,0,0,0,0
+            ],
+            "FetchRowCountMax": 0,
+            "Histogram": [
+                5,0,1,0,0,0,0,0
+            ],
+            "ID": 9,
+            "InTransactionCount": 6,
+            "LastSlowParameters": "[4]",
+            "LastTime": "2012-08-22 02:57:56",
+            "MaxTimespan": 18,
+            "MaxTimespanOccurTime": "2012-08-22 12:38:56",
+            "ResultSetHoldTime": 0,
+            "RunningCount": 0,
+            "SQL": "delete from acct_group_permission where group_id=?",
+            "TotalTime": 21
+        },
+     ]
+  }
+  
+ * */ +public class DruidSQLPanel extends DruidPanel { + + private static final long serialVersionUID = 1L; + private static final String REQUEST_URL = "/sql.json"; + private static final ArrayList SHOW_LIST = new ArrayList() { + + private static final long serialVersionUID = 1L; + + { + add("SQL"); + add("ExecuteCount"); + add("MaxTimespan"); + add("InTransactionCount"); + add("ErrorCount"); + add("EffectedRowCount"); + add("FetchRowCount"); + add("RunningCount"); + add("ConcurrentMax"); + add("EffectedRowCountHistogram"); + add("ExecuteAndResultHoldTimeHistogram"); + add("FetchRowCountHistogram"); + } + }; + private RowHeaderTable header; + + public DruidSQLPanel() { + super(); + this.url = REQUEST_URL; + } + + @Override + protected void tableDataProcess( + ArrayList> data) { + tableModel = new DruidTableModel(data,SHOW_LIST); + table.setModel(tableModel); + + header = new RowHeaderTable(table,20); + scrollPane.setRowHeaderView(header); + } +} diff --git a/src/main/java/com/alibaba/druid/support/jconsole/model/DruidTableModel.java b/src/main/java/com/alibaba/druid/support/jconsole/model/DruidTableModel.java new file mode 100644 index 0000000000..77f5cfbe67 --- /dev/null +++ b/src/main/java/com/alibaba/druid/support/jconsole/model/DruidTableModel.java @@ -0,0 +1,101 @@ +package com.alibaba.druid.support.jconsole.model; + +import java.util.ArrayList; +import java.util.LinkedHashMap; + +import javax.swing.event.TableModelListener; +import javax.swing.table.TableModel; + +public class DruidTableModel implements TableModel { + private ArrayList> list; + private ArrayList showKeys; + + public DruidTableModel(ArrayList> list) { + super(); + this.list = list; + showKeys = null; + } + + public DruidTableModel(ArrayList> list, + ArrayList showKeys) { + super(); + this.list = list; + this.showKeys = showKeys; + } + + + @Override + public Class getColumnClass(int columnIndex) { + return String.class; + } + + @Override + public int getColumnCount() { + int colCount = 0; + if (showKeys != null) { + colCount = showKeys.size(); + } else if (list != null) { + int listLen = list.size(); + if (listLen > 0) { + colCount = list.get(0).size(); + } + } + return colCount; + } + + @Override + public String getColumnName(int columnIndex) { + if (showKeys != null && showKeys.size() > 0) { + return showKeys.get(columnIndex); + } + if (list != null && list.size() > 0) { + LinkedHashMap firstElement = list.get(0); + Object[] keys = firstElement.keySet().toArray(); + return keys[columnIndex].toString(); + } + return null; + } + + @Override + public int getRowCount() { + return list.size(); + } + + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + if (list != null && rowIndex < list.size()) { + LinkedHashMap dataNow = list.get(rowIndex); + if (showKeys != null) { + int titleLen = showKeys.size(); + if (titleLen > 0 && columnIndex < titleLen) { + return dataNow.get(showKeys.get(columnIndex)); + } + } else { + Object[] values = dataNow.values().toArray(); + return values[columnIndex]; + } + } + return null; + } + + @Override + public boolean isCellEditable(int rowIndex, int columnIndex) { + return false; + } + + @Override + public void addTableModelListener(TableModelListener l) { + + } + + @Override + public void removeTableModelListener(TableModelListener l) { + + } + + @Override + public void setValueAt(Object aValue, int rowIndex, int columnIndex) { + + } + +} diff --git a/src/main/java/com/alibaba/druid/support/jconsole/model/RowHeaderTable.java b/src/main/java/com/alibaba/druid/support/jconsole/model/RowHeaderTable.java new file mode 100644 index 0000000000..561a1a5551 --- /dev/null +++ b/src/main/java/com/alibaba/druid/support/jconsole/model/RowHeaderTable.java @@ -0,0 +1,142 @@ +package com.alibaba.druid.support.jconsole.model; + +import java.awt.Color; +import java.awt.Component; +import java.awt.Dimension; +import java.util.ArrayList; + +import javax.swing.JLabel; +import javax.swing.JTable; +import javax.swing.ListSelectionModel; +import javax.swing.UIManager; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import javax.swing.table.DefaultTableModel; +import javax.swing.table.JTableHeader; +import javax.swing.table.TableCellRenderer; + +/** + * 用于显示RowHeader的JTable,只需要将其加入JScrollPane的RowHeaderView即可为JTable生成行标题 + */ +public class RowHeaderTable extends JTable +{ + private static final long serialVersionUID = 1L; + + /** + * 为JTable添加RowHeader, + * @param refTable 需要添加rowHeader的JTable + * @param columnWideth rowHeader的宽度 + */ + public RowHeaderTable(JTable refTable,int columnWidth){ + this(null,refTable,columnWidth,1); + } + + public RowHeaderTable(ArrayList title,JTable refTable,int columnWidth) { + this(title,refTable,columnWidth,1); + } + + public RowHeaderTable(ArrayList title,JTable refTable,int columnWidth,int rowSpan) { + super(new DefaultTableModel(refTable.getRowCount()/rowSpan,1)); + this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);//不可以调整列宽 + this.getColumnModel().getColumn(0).setPreferredWidth(columnWidth); + this.setDefaultRenderer(Object.class,new RowHeaderRenderer(title,refTable,this,rowSpan));//设置渲染器 + this.setPreferredScrollableViewportSize (new Dimension(columnWidth,0)); + } +} +/** + * 用于显示RowHeader的JTable的渲染器,当选择某行时,该行颜色会发生变化 + */ +final class RowHeaderRenderer extends JLabel implements TableCellRenderer,ListSelectionListener +{ + private static final long serialVersionUID = 1L; + private JTable reftable;//需要添加rowHeader的JTable + private JTable tableShow;//用于显示rowHeader的JTable + private ArrayList headerList; + private int rowHeightNow; + private int rowSpan; + + public RowHeaderRenderer(JTable reftable,JTable tableShow) { + this(null,reftable,tableShow,0); + } + + public RowHeaderRenderer(ArrayList headerList, + JTable reftable,JTable tableShow,int rowSpan) + { + this.headerList = headerList; + this.reftable = reftable; + this.tableShow=tableShow; + //增加监听器,实现当在reftable中选择行时,RowHeader会发生颜色变化 + ListSelectionModel listModel=reftable.getSelectionModel(); + listModel.addListSelectionListener(this); + rowHeightNow = reftable.getRowCount()*reftable.getRowHeight(); + this.rowSpan = rowSpan; + if (rowSpan > 1) { + rowHeightNow = rowSpan*reftable.getRowHeight(); + } + } + public Component getTableCellRendererComponent(JTable table,Object obj, + boolean isSelected,boolean hasFocus,int row, int col) + { + int rowCountNow = reftable.getRowCount()/rowSpan; + ((DefaultTableModel)table.getModel()).setRowCount(rowCountNow); + JTableHeader header = reftable.getTableHeader(); + this.setOpaque(true); + setBorder(UIManager.getBorder("TableHeader.cellBorder"));//设置为TableHeader的边框类型 + setHorizontalAlignment(CENTER);//让text居中显示 + setBackground(header.getBackground());//设置背景色为TableHeader的背景色 + if ( isSelect(row) ) //当选取单元格时,在row header上设置成选取颜色 + { + setForeground(Color.white); + setBackground(Color.lightGray); + } + else + { + setForeground(header.getForeground()); + } + setFont(header.getFont()); + if (row <= rowCountNow){ + showCol(row); + } + return this; + } + + private void showCol(int row) { + + String text = null; + if (headerList != null && row < headerList.size()) { + text = headerList.get(row); + } else { + text = String.valueOf(row+1); + } + if (rowSpan > 1) { + setText(text); + this.tableShow.setRowHeight(row, rowHeightNow); + } else { + setText(text); + } + + } + + public void valueChanged(ListSelectionEvent e){ + this.tableShow.repaint(); + } + private boolean isSelect(int row) + { + int[] sel = reftable.getSelectedRows(); + if (rowSpan <= 1) { + for ( int i=0,len = sel.length; i> rowDatas, String keyword) { + ColumnData datas = new ColumnData(); + ArrayList> coldatas + = new ArrayList>(); + ArrayList colNames = new ArrayList(); + int rowCount = 0; + int colCount = 0; + for(LinkedHashMap row: rowDatas) { + String keyNow = row.remove(keyword).toString(); + colNames.add(keyNow); + rowCount ++; + + for(Map.Entry element : row.entrySet()) { + LinkedHashMap colData + = new LinkedHashMap(); + colData.put(COLUMN_KEY_NAME, element.getKey()); + colData.put(COLUMN_VALUE_NAME, element.getValue()); + coldatas.add(colData); + if (rowCount == 1) { + colCount++; + } + } + } + datas.setCount(colCount); + datas.setDatas(coldatas); + datas.setNames(colNames); + return datas; + } + + public static class ColumnData { + private ArrayList names; + private ArrayList> datas; + private int count; + + public ColumnData() { + super(); + } + + public ArrayList getNames() { + return names; + } + public void setNames(ArrayList names) { + this.names = names; + } + public ArrayList> getDatas() { + return datas; + } + public void setDatas(ArrayList> datas) { + this.datas = datas; + } + public int getCount() { + return count; + } + public void setCount(int count) { + this.count = count; + } + } +} From 49a3ba4b11858b92fec61166a6f8d06a57012214 Mon Sep 17 00:00:00 2001 From: yunnysunny Date: Sun, 26 Aug 2012 17:33:24 +0800 Subject: [PATCH 2/2] modify the column names of druid sql panel. --- .../java/com/alibaba/druid/support/jconsole/DruidSQLPanel.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/alibaba/druid/support/jconsole/DruidSQLPanel.java b/src/main/java/com/alibaba/druid/support/jconsole/DruidSQLPanel.java index b2500ff33c..60e3063e26 100644 --- a/src/main/java/com/alibaba/druid/support/jconsole/DruidSQLPanel.java +++ b/src/main/java/com/alibaba/druid/support/jconsole/DruidSQLPanel.java @@ -78,13 +78,14 @@ public class DruidSQLPanel extends DruidPanel { { add("SQL"); add("ExecuteCount"); - add("MaxTimespan"); + add("TotalTime"); add("InTransactionCount"); add("ErrorCount"); add("EffectedRowCount"); add("FetchRowCount"); add("RunningCount"); add("ConcurrentMax"); + add("Histogram"); add("EffectedRowCountHistogram"); add("ExecuteAndResultHoldTimeHistogram"); add("FetchRowCountHistogram");