mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #71 from yunnysunny/master
add druid jconsole interface
This commit is contained in:
@@ -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<LinkedHashMap<String, Object>> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<LinkedHashMap<String, Object>> data) {
|
||||
tableModel = new DruidTableModel(data);
|
||||
table.setModel(tableModel);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<LinkedHashMap<String,Object>> parseData(Object respData) {
|
||||
ArrayList<LinkedHashMap<String,Object>> data = null;
|
||||
if (respData instanceof Map) {
|
||||
LinkedHashMap<String,Object> map = (LinkedHashMap<String, Object>)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<LinkedHashMap<String, Object>>)content;
|
||||
} else if (content instanceof Map) {
|
||||
LinkedHashMap<String, Object> contentEle
|
||||
= (LinkedHashMap<String, Object>)content;
|
||||
data = new ArrayList<LinkedHashMap<String,Object>>();
|
||||
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<LinkedHashMap<String,Object>> 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<LinkedHashMap<String, Object>> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,110 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* 返回数据格式:
|
||||
* <pre>
|
||||
* {
|
||||
"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
|
||||
},
|
||||
]
|
||||
}
|
||||
</pre>
|
||||
* */
|
||||
public class DruidSQLPanel extends DruidPanel {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final String REQUEST_URL = "/sql.json";
|
||||
private static final ArrayList<String> SHOW_LIST = new ArrayList<String>() {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
{
|
||||
add("SQL");
|
||||
add("ExecuteCount");
|
||||
add("TotalTime");
|
||||
add("InTransactionCount");
|
||||
add("ErrorCount");
|
||||
add("EffectedRowCount");
|
||||
add("FetchRowCount");
|
||||
add("RunningCount");
|
||||
add("ConcurrentMax");
|
||||
add("Histogram");
|
||||
add("EffectedRowCountHistogram");
|
||||
add("ExecuteAndResultHoldTimeHistogram");
|
||||
add("FetchRowCountHistogram");
|
||||
}
|
||||
};
|
||||
private RowHeaderTable header;
|
||||
|
||||
public DruidSQLPanel() {
|
||||
super();
|
||||
this.url = REQUEST_URL;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tableDataProcess(
|
||||
ArrayList<LinkedHashMap<String, Object>> data) {
|
||||
tableModel = new DruidTableModel(data,SHOW_LIST);
|
||||
table.setModel(tableModel);
|
||||
|
||||
header = new RowHeaderTable(table,20);
|
||||
scrollPane.setRowHeaderView(header);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<LinkedHashMap<String,Object>> list;
|
||||
private ArrayList<String> showKeys;
|
||||
|
||||
public DruidTableModel(ArrayList<LinkedHashMap<String, Object>> list) {
|
||||
super();
|
||||
this.list = list;
|
||||
showKeys = null;
|
||||
}
|
||||
|
||||
public DruidTableModel(ArrayList<LinkedHashMap<String, Object>> list,
|
||||
ArrayList<String> 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<String,Object> 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<String,Object> 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) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> title,JTable refTable,int columnWidth) {
|
||||
this(title,refTable,columnWidth,1);
|
||||
}
|
||||
|
||||
public RowHeaderTable(ArrayList<String> 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<String> headerList;
|
||||
private int rowHeightNow;
|
||||
private int rowSpan;
|
||||
|
||||
public RowHeaderRenderer(JTable reftable,JTable tableShow) {
|
||||
this(null,reftable,tableShow,0);
|
||||
}
|
||||
|
||||
public RowHeaderRenderer(ArrayList<String> 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<len; i++ ) {
|
||||
if (sel[i] == row ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for ( int i=0,len = sel.length; i<len; i++ ) {
|
||||
if (sel[i] / rowSpan == row) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.alibaba.druid.support.jconsole.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class TableDataProcessor {
|
||||
private static final String COLUMN_KEY_NAME = "key";
|
||||
private static final String COLUMN_VALUE_NAME = "value";
|
||||
private TableDataProcessor() {
|
||||
|
||||
}
|
||||
|
||||
public static ColumnData row2col(
|
||||
ArrayList<LinkedHashMap<String, Object>> rowDatas, String keyword) {
|
||||
ColumnData datas = new ColumnData();
|
||||
ArrayList<LinkedHashMap<String, Object>> coldatas
|
||||
= new ArrayList<LinkedHashMap<String, Object>>();
|
||||
ArrayList<String> colNames = new ArrayList<String>();
|
||||
int rowCount = 0;
|
||||
int colCount = 0;
|
||||
for(LinkedHashMap<String, Object> row: rowDatas) {
|
||||
String keyNow = row.remove(keyword).toString();
|
||||
colNames.add(keyNow);
|
||||
rowCount ++;
|
||||
|
||||
for(Map.Entry <String, Object> element : row.entrySet()) {
|
||||
LinkedHashMap<String, Object> colData
|
||||
= new LinkedHashMap<String, Object>();
|
||||
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<String> names;
|
||||
private ArrayList<LinkedHashMap<String, Object>> datas;
|
||||
private int count;
|
||||
|
||||
public ColumnData() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ArrayList<String> getNames() {
|
||||
return names;
|
||||
}
|
||||
public void setNames(ArrayList<String> names) {
|
||||
this.names = names;
|
||||
}
|
||||
public ArrayList<LinkedHashMap<String, Object>> getDatas() {
|
||||
return datas;
|
||||
}
|
||||
public void setDatas(ArrayList<LinkedHashMap<String, Object>> datas) {
|
||||
this.datas = datas;
|
||||
}
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
public void setCount(int count) {
|
||||
this.count = count;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user