mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
1.修正了jconsole多个数据源显示的时候,只显示第一个数据源表格的bug。
2.修改了jconsole界面显示,将界面分为两部分。数据区和版权区。 3.给jconsole的sql语句显示区增加了分类表头。
This commit is contained in:
@@ -1,71 +1,112 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.BorderLayout;
|
||||
import java.awt.GridLayout;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.border.TitledBorder;
|
||||
|
||||
import com.alibaba.druid.support.jconsole.model.DruidTableCellRenderer;
|
||||
import com.alibaba.druid.support.jconsole.model.DruidTableModel;
|
||||
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";
|
||||
private JPanel contentPanel;
|
||||
|
||||
public DruidDataSourcePanel() {
|
||||
super();
|
||||
this.url = REQUEST_URL;
|
||||
|
||||
}
|
||||
|
||||
private void addTable(ColumnData columnData) {
|
||||
ArrayList<ArrayList<LinkedHashMap<String, Object>>> data
|
||||
= columnData.getTableDatas();
|
||||
|
||||
int i = 0;
|
||||
ArrayList<String> ids = columnData.getNames();
|
||||
|
||||
for (ArrayList<LinkedHashMap<String, Object>> listNow : data) {
|
||||
|
||||
tableModel = new DruidTableModel(listNow);
|
||||
table.setModel(tableModel);
|
||||
|
||||
String id = ids.get(i);
|
||||
JPanel panelNow = new JPanel(new BorderLayout());
|
||||
panelNow.setBorder((TitledBorder) BorderFactory
|
||||
.createTitledBorder(KEY_WORD_IDENTITY + ":" + id));
|
||||
contentPanel.add(panelNow);
|
||||
|
||||
panelNow.add(table.getTableHeader(),BorderLayout.NORTH);
|
||||
panelNow.add(table);
|
||||
table.getColumnModel().getColumn(0).setCellRenderer(new DruidTableCellRenderer());
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tableDataProcess(
|
||||
ArrayList<LinkedHashMap<String, Object>> data) {
|
||||
ColumnData columnData = TableDataProcessor.mutilRow2col(data,
|
||||
KEY_WORD_IDENTITY);
|
||||
contentPanel = new JPanel(new GridLayout(0,1));
|
||||
addTable(columnData);
|
||||
|
||||
scrollPane.setViewportView(contentPanel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,51 +1,58 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 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.DruidTableCellRenderer;
|
||||
import com.alibaba.druid.support.jconsole.model.DruidTableModel;
|
||||
import com.alibaba.druid.support.jconsole.util.TableDataProcessor;
|
||||
import com.alibaba.druid.support.jconsole.util.TableDataProcessor.ColumnData;
|
||||
|
||||
/**
|
||||
* 请求地址:/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) {
|
||||
ColumnData columnData = TableDataProcessor.row2col(data);
|
||||
tableModel = new DruidTableModel(columnData.getDatas());
|
||||
table.setModel(tableModel);
|
||||
table.getColumnModel().getColumn(0).setCellRenderer(new DruidTableCellRenderer());
|
||||
|
||||
scrollPane.setViewportView(table);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,164 +1,192 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.event.ComponentEvent;
|
||||
import java.awt.event.ComponentListener;
|
||||
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.BorderFactory;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.border.TitledBorder;
|
||||
|
||||
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;
|
||||
private static final String COPYRIGHT_STRING = "<html>powered by <a href=\"http://blog.csdn.net/yunnysunny\">yunnysunny</a></html>";
|
||||
|
||||
protected JScrollPane scrollPane;
|
||||
protected DruidTableModel tableModel;
|
||||
protected JTable table;
|
||||
protected JPanel copyrightPanel;
|
||||
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();
|
||||
scrollPane.setAutoscrolls(true);
|
||||
scrollPane.setBorder((TitledBorder)BorderFactory.createTitledBorder("数据区"));
|
||||
|
||||
setLayout(null);
|
||||
scrollPane.setBounds(10, 10, getWidth()-20, getHeight()-80);
|
||||
|
||||
this.add(scrollPane);
|
||||
|
||||
copyrightPanel = new JPanel();
|
||||
copyrightPanel.setBorder((TitledBorder)BorderFactory.createTitledBorder("版权区"));
|
||||
JLabel authorInfo = new JLabel(COPYRIGHT_STRING);
|
||||
copyrightPanel.add(authorInfo);
|
||||
|
||||
this.add(copyrightPanel);
|
||||
copyrightPanel.setBounds(10, getHeight()-60, getWidth()-20, 60);
|
||||
|
||||
needRefresh = true;
|
||||
lastRefreshTime = timeNow;
|
||||
this.addComponentListener(new ComponentListener() {
|
||||
|
||||
@Override
|
||||
public void componentShown(ComponentEvent arg0) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentResized(ComponentEvent arg0) {
|
||||
scrollPane.setBounds(10, 10, getWidth()-20,
|
||||
getHeight()-80);
|
||||
copyrightPanel.setBounds(10, getHeight()-60, getWidth()-20, 60);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentMoved(ComponentEvent arg0) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentHidden(ComponentEvent arg0) {
|
||||
|
||||
}
|
||||
});
|
||||
} 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,109 +1,295 @@
|
||||
/*
|
||||
* 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");
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
RowHeaderTable header = new RowHeaderTable(table,20);
|
||||
scrollPane.setRowHeaderView(header);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.border.BevelBorder;
|
||||
import javax.swing.table.JTableHeader;
|
||||
import javax.swing.table.TableColumnModel;
|
||||
|
||||
import com.alibaba.druid.support.jconsole.model.ColumnGroup;
|
||||
import com.alibaba.druid.support.jconsole.model.DruidTableModel;
|
||||
import com.alibaba.druid.support.jconsole.model.GroupableTableHeader;
|
||||
import com.alibaba.druid.support.jconsole.model.GroupableTableHeaderUI;
|
||||
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 static final ArrayList<String> REAL_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 static final String HISOGRAM = "Histogram";
|
||||
private static final String Effected_RowCount_HISOGRAM = "EffectedRowCountHistogram";
|
||||
private static final String ExecuteAndResult_Hold_HISOGRAM = "ExecuteAndResultHoldTimeHistogram";
|
||||
private static final String FetchRowCount_HISOGRAM = "FetchRowCountHistogram";
|
||||
private static final ArrayList<String> ARRAY_DATA_MAP = new ArrayList<String>() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
{
|
||||
add(HISOGRAM);
|
||||
add(Effected_RowCount_HISOGRAM);
|
||||
add(ExecuteAndResult_Hold_HISOGRAM);
|
||||
add(FetchRowCount_HISOGRAM);
|
||||
}
|
||||
};
|
||||
private static final int FIST_LIST_OFFSET = 9;
|
||||
|
||||
private ColumnGroup groupHistogram;
|
||||
private ColumnGroup groupEffectedRowCountHistogram;
|
||||
private ColumnGroup groupExecuteAndResultHoldTimeHistogram;
|
||||
private ColumnGroup groupFetchRowCountHistogram;
|
||||
private ArrayList<Integer> listHistogram;
|
||||
private ArrayList<Integer> listEffectedRowCountHistogram;
|
||||
private ArrayList<Integer> listExecuteAndResultHoldTimeHistogram;
|
||||
private ArrayList<Integer> listFetchRowCountHistogram;
|
||||
|
||||
|
||||
public DruidSQLPanel() {
|
||||
super();
|
||||
this.url = REQUEST_URL;
|
||||
}
|
||||
|
||||
private void addGroupData(String keyNow, int index) {
|
||||
// System.out.println("keyNow:"+keyNow+"index:"+index);
|
||||
if (HISOGRAM.equals(keyNow)) {
|
||||
listHistogram.add(index);
|
||||
} else if (Effected_RowCount_HISOGRAM.equals(keyNow)) {
|
||||
listEffectedRowCountHistogram.add(index);
|
||||
} else if (ExecuteAndResult_Hold_HISOGRAM.equals(keyNow)) {
|
||||
listExecuteAndResultHoldTimeHistogram.add(index);
|
||||
} else if (FetchRowCount_HISOGRAM.equals(keyNow)) {
|
||||
listFetchRowCountHistogram.add(index);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据预处理,将没有用到的数据删除掉,将牵扯到时间分布的数据拆分出来。
|
||||
*
|
||||
* @param data 要处理的数据
|
||||
* @return 最终的处理结果
|
||||
*/
|
||||
private ArrayList<LinkedHashMap<String, Object>> preProcess(
|
||||
ArrayList<LinkedHashMap<String, Object>> data) {
|
||||
groupHistogram = new ColumnGroup(HISOGRAM);
|
||||
groupEffectedRowCountHistogram = new ColumnGroup(Effected_RowCount_HISOGRAM);
|
||||
groupExecuteAndResultHoldTimeHistogram = new ColumnGroup(ExecuteAndResult_Hold_HISOGRAM);
|
||||
groupFetchRowCountHistogram = new ColumnGroup(FetchRowCount_HISOGRAM);
|
||||
listHistogram = new ArrayList<Integer>();
|
||||
listEffectedRowCountHistogram = new ArrayList<Integer>();
|
||||
listExecuteAndResultHoldTimeHistogram = new ArrayList<Integer>();
|
||||
listFetchRowCountHistogram = new ArrayList<Integer>();
|
||||
|
||||
ArrayList<LinkedHashMap<String, Object>> newData
|
||||
= new ArrayList<LinkedHashMap<String, Object>>();
|
||||
int dataIndex = 0;
|
||||
for (LinkedHashMap<String, Object> dataNow : data) {
|
||||
//先把不需要显示的内容删除掉
|
||||
for (Iterator<Entry<String, Object>> it = dataNow.entrySet().iterator(); it.hasNext();) {
|
||||
Entry<String, Object> entry = it.next();
|
||||
if (!SHOW_LIST.contains(entry.getKey())) {
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
// if (dataIndex==0)
|
||||
// System.out.println(dataNow.toString());
|
||||
int offset = FIST_LIST_OFFSET;
|
||||
|
||||
for (String arrayKey:ARRAY_DATA_MAP) {//把数组数据显示为单个数据
|
||||
Object arrayData = dataNow.get(arrayKey);
|
||||
|
||||
if (arrayData instanceof ArrayList<?>) {
|
||||
|
||||
dataNow.remove(arrayKey);
|
||||
if (dataIndex == 0) {
|
||||
REAL_SHOW_LIST.remove(arrayKey);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
ArrayList<Integer> arrayDataList = (ArrayList<Integer>)arrayData;
|
||||
for (int j=0,len=arrayDataList.size();j<len;j++) {
|
||||
int a = (j-1 >= 0)?
|
||||
(int)Math.pow(10,j-1)
|
||||
: 0;
|
||||
int b = (int)Math.pow(10,j);
|
||||
String newKey = arrayKey + "-" +a + "~" + b+"ms";
|
||||
|
||||
dataNow.put(newKey, arrayDataList.get(j));
|
||||
if (dataIndex == 0) {
|
||||
REAL_SHOW_LIST.add(newKey);
|
||||
int index = offset+j;
|
||||
addGroupData(arrayKey,index);
|
||||
if (j == len -1) {
|
||||
offset = index+1;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
}//end of foreach ARRAY_DATA_MAP
|
||||
dataIndex ++;
|
||||
newData.add(dataNow);
|
||||
}
|
||||
|
||||
return newData;
|
||||
}
|
||||
|
||||
private void addTableGroup() {
|
||||
|
||||
TableColumnModel cm = table.getColumnModel();
|
||||
// System.out.println(SHOW_LIST.size());
|
||||
for(int i : listHistogram) {
|
||||
groupHistogram.add(cm.getColumn(i));
|
||||
}
|
||||
for (int j: listEffectedRowCountHistogram) {
|
||||
groupEffectedRowCountHistogram.add(cm.getColumn(j));
|
||||
}
|
||||
for (int x : listExecuteAndResultHoldTimeHistogram) {
|
||||
groupExecuteAndResultHoldTimeHistogram.add(cm.getColumn(x));
|
||||
}
|
||||
for (int y : listFetchRowCountHistogram) {
|
||||
groupFetchRowCountHistogram.add(cm.getColumn(y));
|
||||
}
|
||||
|
||||
GroupableTableHeader header = (GroupableTableHeader) table. getTableHeader();
|
||||
header.addColumnGroup(groupHistogram);
|
||||
header.addColumnGroup(groupEffectedRowCountHistogram);
|
||||
header.addColumnGroup(groupExecuteAndResultHoldTimeHistogram);
|
||||
header.addColumnGroup(groupFetchRowCountHistogram);
|
||||
header.setUI(new GroupableTableHeaderUI());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tableDataProcess(
|
||||
ArrayList<LinkedHashMap<String, Object>> data) {
|
||||
table = new JTable() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected JTableHeader createDefaultTableHeader() {
|
||||
|
||||
return new GroupableTableHeader(columnModel);
|
||||
}
|
||||
};
|
||||
data = preProcess(data);
|
||||
// System.out.println(SHOW_LIST);
|
||||
tableModel = new DruidTableModel(data,REAL_SHOW_LIST);
|
||||
// System.out.println(data.toString());
|
||||
table.setModel(tableModel);
|
||||
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
|
||||
addTableGroup();
|
||||
|
||||
RowHeaderTable header = new RowHeaderTable(table,20);
|
||||
scrollPane.setRowHeaderView(header);
|
||||
scrollPane.setViewportView(table);
|
||||
JLabel jb = new JLabel("N",SwingConstants.CENTER);
|
||||
jb.setBorder(new BevelBorder(BevelBorder.RAISED,null,null,null,null));
|
||||
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER,jb);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
package com.alibaba.druid.support.jconsole.model;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.Dimension;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.table.DefaultTableCellRenderer;
|
||||
import javax.swing.table.JTableHeader;
|
||||
import javax.swing.table.TableCellRenderer;
|
||||
import javax.swing.table.TableColumn;
|
||||
|
||||
public class ColumnGroup {
|
||||
/**合并的JTableHeader的Renderer*/
|
||||
protected TableCellRenderer renderer=null;
|
||||
/**合并的单元格的各个实际的最小单元格存储结构*/
|
||||
protected Vector<Object> vector=null;
|
||||
/**合并后单元格显示的文本信息*/
|
||||
protected String text=null;
|
||||
/**合并的单元格内部两个最小JTableHeader的间隙,其实就是去掉线后那个Border*/
|
||||
private int margin = 0;
|
||||
|
||||
public ColumnGroup(String text) {
|
||||
this(null, text);
|
||||
}
|
||||
|
||||
public ColumnGroup(TableCellRenderer renderer, String text) {
|
||||
if (renderer == null) {
|
||||
this.renderer = new DefaultTableCellRenderer() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Component getTableCellRendererComponent(JTable table,
|
||||
Object value, boolean isSelected, boolean hasFocus,
|
||||
int row, int column) {
|
||||
JTableHeader header = table.getTableHeader();
|
||||
if (header != null) {
|
||||
setForeground(header.getForeground());
|
||||
setBackground(header.getBackground());
|
||||
setFont(header.getFont());
|
||||
}
|
||||
setHorizontalAlignment(JLabel.CENTER);
|
||||
this.setText((value == null) ? "" : value.toString());
|
||||
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
|
||||
return this;
|
||||
}
|
||||
};
|
||||
} else {
|
||||
this.renderer = renderer;
|
||||
}
|
||||
this.text = text;
|
||||
vector = new Vector<Object>();
|
||||
}
|
||||
/**
|
||||
* 增加单元格
|
||||
* @param obj
|
||||
*/
|
||||
public void add(Object obj) {
|
||||
if (obj == null)
|
||||
return;
|
||||
vector.addElement(obj);
|
||||
}
|
||||
/**
|
||||
* 根据JTable的某一列取得它的所有的包含列,
|
||||
* @param column
|
||||
* @param group
|
||||
* @return
|
||||
*/
|
||||
public Vector<ColumnGroup> getColumnGroups(TableColumn column, Vector<ColumnGroup> group) {
|
||||
//通过递归判断列到底属于那个ColumnGroup
|
||||
group.addElement(this);
|
||||
if (vector.contains(column))
|
||||
return group;
|
||||
Enumeration<Object> enumeration = vector.elements();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
Object obj = enumeration.nextElement();
|
||||
if (obj instanceof ColumnGroup) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Vector<ColumnGroup> groups = ((ColumnGroup) obj).getColumnGroups(column,
|
||||
(Vector<ColumnGroup>) group.clone());
|
||||
if (groups != null) {
|
||||
return groups;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public TableCellRenderer getHeaderRenderer() {
|
||||
return renderer;
|
||||
}
|
||||
|
||||
public Object getHeaderValue() {
|
||||
return text;
|
||||
}
|
||||
/**
|
||||
* 取得合并后的单元格的大小
|
||||
* @return
|
||||
*/
|
||||
public int getSize() {
|
||||
return vector == null ? 0 : vector.size();
|
||||
}
|
||||
/**
|
||||
* 取得合并后的单元格的大小,这个方法需要计算,首先
|
||||
* 是取得一个没有合并的最小单元格的JTableHeader
|
||||
* 的大小,通过Renderer取得组件
|
||||
* @return
|
||||
*/
|
||||
public Dimension getSize(JTable table) {
|
||||
Component comp = renderer.getTableCellRendererComponent(table,
|
||||
getHeaderValue(), false, false, -1, -1);
|
||||
int height = comp.getPreferredSize().height;
|
||||
int width = 0;
|
||||
//宽度需要计算合并的还要加上间隙
|
||||
Enumeration<Object> enumeration = vector.elements();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
Object obj = enumeration.nextElement();
|
||||
if (obj instanceof TableColumn) {
|
||||
TableColumn aColumn = (TableColumn) obj;
|
||||
width += aColumn.getWidth();
|
||||
width += margin;
|
||||
} else {
|
||||
width += ((ColumnGroup) obj).getSize(table).width;
|
||||
}
|
||||
}
|
||||
return new Dimension(width, height);
|
||||
}
|
||||
|
||||
public java.lang.String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public boolean removeColumn(ColumnGroup ptg, TableColumn tc) {
|
||||
boolean retFlag = false;
|
||||
if (tc != null) {
|
||||
for (int i = 0; i < ptg.vector.size(); i++) {
|
||||
Object tmpObj = ptg.vector.get(i);
|
||||
if (tmpObj instanceof ColumnGroup) {
|
||||
retFlag = removeColumn((ColumnGroup) tmpObj, tc);
|
||||
if (retFlag) {
|
||||
break;
|
||||
}
|
||||
} else if (tmpObj instanceof TableColumn) {
|
||||
if (tmpObj == tc) {
|
||||
ptg.vector.remove(i);
|
||||
retFlag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return retFlag;
|
||||
}
|
||||
|
||||
public boolean removeColumnGrp(ColumnGroup ptg, ColumnGroup tg) {
|
||||
boolean retFlag = false;
|
||||
if (tg != null) {
|
||||
for (int i = 0; i < ptg.vector.size(); i++) {
|
||||
Object tmpObj = ptg.vector.get(i);
|
||||
if (tmpObj instanceof ColumnGroup) {
|
||||
if (tmpObj == tg) {
|
||||
ptg.vector.remove(i);
|
||||
retFlag = true;
|
||||
break;
|
||||
} else {
|
||||
retFlag = removeColumnGrp((ColumnGroup) tmpObj, tg);
|
||||
if (retFlag) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
} else if (tmpObj instanceof TableColumn) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return retFlag;
|
||||
}
|
||||
|
||||
public void setColumnMargin(int margin) {
|
||||
this.margin = margin;
|
||||
Enumeration<Object> enumeration = vector.elements();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
Object obj = enumeration.nextElement();
|
||||
if (obj instanceof ColumnGroup) {
|
||||
((ColumnGroup) obj).setColumnMargin(margin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setHeaderRenderer(TableCellRenderer renderer) {
|
||||
if (renderer != null) {
|
||||
this.renderer = renderer;
|
||||
}
|
||||
}
|
||||
|
||||
public void setText(java.lang.String newText) {
|
||||
text = newText;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.alibaba.druid.support.jconsole.model;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Component;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.table.TableCellRenderer;
|
||||
|
||||
public class DruidTableCellRenderer extends JLabel implements TableCellRenderer
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// This method is called each time a cell in a column
|
||||
// using this renderer needs to be rendered.
|
||||
public Component getTableCellRendererComponent(JTable table, Object value,
|
||||
boolean isSelected, boolean hasFocus, int row, int column)
|
||||
{
|
||||
|
||||
if ((column == 0))
|
||||
{
|
||||
setBackground(Color.lightGray);
|
||||
}
|
||||
this.setOpaque(true);
|
||||
|
||||
if (value != null) {
|
||||
setText(value.toString());
|
||||
} else {
|
||||
setText((row + 1)+"");
|
||||
}
|
||||
|
||||
// Since the renderer is a component, return itself
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -1,116 +1,121 @@
|
||||
/*
|
||||
* 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.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) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* 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.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) {
|
||||
String keyNow = showKeys.get(columnIndex);
|
||||
if (keyNow != null) {
|
||||
return keyNow.substring(keyNow.indexOf('-')+1, keyNow.length());
|
||||
}
|
||||
}
|
||||
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();
|
||||
if (columnIndex < values.length) {
|
||||
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,77 @@
|
||||
package com.alibaba.druid.support.jconsole.model;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.swing.table.JTableHeader;
|
||||
import javax.swing.table.TableColumn;
|
||||
import javax.swing.table.TableColumnModel;
|
||||
|
||||
public class GroupableTableHeader extends JTableHeader {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
// private static final String uiClassID = "GroupableTableHeaderUI";
|
||||
protected Vector<ColumnGroup> columnGroups = null;
|
||||
|
||||
public GroupableTableHeader(TableColumnModel model) {
|
||||
super(model);
|
||||
setUI(new GroupableTableHeaderUI());
|
||||
setReorderingAllowed(false);
|
||||
setRequestFocusEnabled(false);
|
||||
}
|
||||
|
||||
public void addColumnGroup(ColumnGroup g) {
|
||||
if (columnGroups == null) {
|
||||
columnGroups = new Vector<ColumnGroup>();
|
||||
}
|
||||
columnGroups.addElement(g);
|
||||
}
|
||||
|
||||
public void clearColumnGroups() {
|
||||
columnGroups = null;
|
||||
}
|
||||
|
||||
public ColumnGroup[] getColumnGroups() {
|
||||
ColumnGroup[] retg = null;
|
||||
if (columnGroups.size() > 0) {
|
||||
retg = new ColumnGroup[columnGroups.size()];
|
||||
columnGroups.copyInto(retg);
|
||||
}
|
||||
return retg;
|
||||
}
|
||||
|
||||
public Enumeration<ColumnGroup> getColumnGroups(TableColumn col) {
|
||||
if (columnGroups == null) {
|
||||
return null;
|
||||
}
|
||||
Enumeration<ColumnGroup> enum1 = columnGroups.elements();
|
||||
while (enum1.hasMoreElements()) {
|
||||
ColumnGroup cGroup = (ColumnGroup) enum1.nextElement();
|
||||
Vector<ColumnGroup> v_ret = cGroup.getColumnGroups(col, new Vector<ColumnGroup>());
|
||||
if (v_ret != null) {
|
||||
return v_ret.elements();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isFocusTraversable() {
|
||||
return super.isFocusTraversable() && isRequestFocusEnabled();
|
||||
}
|
||||
|
||||
public void setColumnMargin() {
|
||||
if (columnGroups == null) {
|
||||
return;
|
||||
}
|
||||
int columnMargin = getColumnModel().getColumnMargin();
|
||||
Enumeration<ColumnGroup> enum1 = columnGroups.elements();
|
||||
while (enum1.hasMoreElements()) {
|
||||
ColumnGroup cGroup = (ColumnGroup) enum1.nextElement();
|
||||
cGroup.setColumnMargin(columnMargin);
|
||||
}
|
||||
}
|
||||
|
||||
public void setReorderingAllowed(boolean b) {
|
||||
reorderingAllowed = b;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
package com.alibaba.druid.support.jconsole.model;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Rectangle;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.plaf.basic.BasicTableHeaderUI;
|
||||
import javax.swing.table.DefaultTableCellRenderer;
|
||||
import javax.swing.table.JTableHeader;
|
||||
import javax.swing.table.TableCellRenderer;
|
||||
import javax.swing.table.TableColumn;
|
||||
import javax.swing.table.TableColumnModel;
|
||||
|
||||
public class GroupableTableHeaderUI extends BasicTableHeaderUI {
|
||||
private int m_height;
|
||||
|
||||
private Dimension createHeaderSize(long width) {
|
||||
TableColumnModel columnModel = header.getColumnModel();
|
||||
width += columnModel.getColumnMargin() * columnModel.getColumnCount();
|
||||
if (width > Integer.MAX_VALUE) {
|
||||
width = Integer.MAX_VALUE;
|
||||
}
|
||||
return new Dimension((int) width, getHeaderHeight());
|
||||
}
|
||||
|
||||
private int getColCountUnderColGroup(ColumnGroup cg, int iCount) {
|
||||
Vector<Object> v = cg.vector;
|
||||
for (int i = 0; i < v.size(); i++) {
|
||||
Object obj = v.elementAt(i);
|
||||
if (obj instanceof ColumnGroup) {
|
||||
iCount = getColCountUnderColGroup((ColumnGroup) obj, iCount);
|
||||
} else {
|
||||
iCount++;
|
||||
}
|
||||
}
|
||||
return iCount;
|
||||
}
|
||||
|
||||
public int getHeaderHeight() {
|
||||
int height = 0;
|
||||
TableColumnModel columnModel = header.getColumnModel();
|
||||
for (int column = 0; column < columnModel.getColumnCount(); column++) {
|
||||
TableColumn aColumn = columnModel.getColumn(column);
|
||||
TableCellRenderer renderer = aColumn.getHeaderRenderer();
|
||||
if (renderer == null) {
|
||||
renderer = new DefaultTableCellRenderer() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Component getTableCellRendererComponent(
|
||||
JTable table, Object value, boolean isSelected,
|
||||
boolean hasFocus, int row, int column) {
|
||||
JTableHeader header = table.getTableHeader();
|
||||
if (header != null) {
|
||||
setForeground(header.getForeground());
|
||||
setBackground(header.getBackground());
|
||||
setFont(header.getFont());
|
||||
}
|
||||
setHorizontalAlignment(JLabel.CENTER);
|
||||
setText((value == null) ? "" : value.toString());
|
||||
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
Component comp = renderer.getTableCellRendererComponent(header
|
||||
.getTable(), aColumn.getHeaderValue(), false, false, -1,
|
||||
column);
|
||||
int cHeight = comp.getPreferredSize().height;
|
||||
Enumeration<ColumnGroup> enumeration = ((GroupableTableHeader) header)
|
||||
.getColumnGroups(aColumn);
|
||||
if (enumeration != null) {
|
||||
while (enumeration.hasMoreElements()) {
|
||||
ColumnGroup cGroup = (ColumnGroup) enumeration
|
||||
.nextElement();
|
||||
cHeight += cGroup.getSize(header.getTable()).height;
|
||||
}
|
||||
}
|
||||
height = Math.max(height, cHeight);
|
||||
}
|
||||
height = Math.max(height, m_height);
|
||||
return height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getPreferredSize(JComponent c) {
|
||||
long width = 0;
|
||||
Enumeration<TableColumn> enumeration = header.getColumnModel().getColumns();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
TableColumn aColumn = enumeration.nextElement();
|
||||
width = width + aColumn.getWidth();
|
||||
}
|
||||
|
||||
return createHeaderSize(width);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g, JComponent c) {
|
||||
Rectangle clipBounds = g.getClipBounds();
|
||||
if (header.getColumnModel() == null) {
|
||||
return;
|
||||
}
|
||||
((GroupableTableHeader) header).setColumnMargin();
|
||||
int column = 0;
|
||||
Dimension size = header.getSize();
|
||||
Rectangle cellRect = new Rectangle(0, 0, size.width, size.height);
|
||||
Hashtable<ColumnGroup, Rectangle> h = new Hashtable<ColumnGroup, Rectangle>();
|
||||
int columnMargin = header.getColumnModel().getColumnMargin();
|
||||
Enumeration<TableColumn> enumeration = header.getColumnModel().getColumns();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
cellRect.height = size.height;
|
||||
cellRect.y = 0;
|
||||
TableColumn aColumn = enumeration.nextElement();
|
||||
Enumeration<ColumnGroup> cGroups = ((GroupableTableHeader) header)
|
||||
.getColumnGroups(aColumn);
|
||||
if (cGroups != null) {
|
||||
int groupHeight = 0;
|
||||
while (cGroups.hasMoreElements()) {
|
||||
ColumnGroup cGroup = cGroups.nextElement();
|
||||
Rectangle groupRect = (Rectangle) h.get(cGroup);
|
||||
if (groupRect == null) {
|
||||
groupRect = new Rectangle(cellRect);
|
||||
Dimension d = cGroup.getSize(header.getTable());
|
||||
if (!System.getProperty("java.vm.version").startsWith(
|
||||
"1.2")) {
|
||||
int iColCount = getColCountUnderColGroup(cGroup, 0);
|
||||
groupRect.width = d.width - iColCount
|
||||
* columnMargin;
|
||||
} else {
|
||||
groupRect.width = d.width;
|
||||
}
|
||||
groupRect.height = d.height;
|
||||
h.put(cGroup, groupRect);
|
||||
}
|
||||
paintCell(g, groupRect, cGroup);
|
||||
groupHeight += groupRect.height;
|
||||
cellRect.height = size.height - groupHeight;
|
||||
cellRect.y = groupHeight;
|
||||
}
|
||||
}
|
||||
if (!System.getProperty("java.vm.version").startsWith("1.2")) {
|
||||
cellRect.width = aColumn.getWidth();
|
||||
} else {
|
||||
cellRect.width = aColumn.getWidth() + columnMargin;
|
||||
}
|
||||
|
||||
if (cellRect.intersects(clipBounds)) {
|
||||
paintCell(g, cellRect, column);
|
||||
}
|
||||
cellRect.x += cellRect.width;
|
||||
column++;
|
||||
}
|
||||
}
|
||||
|
||||
private void paintCell(Graphics g, Rectangle cellRect, int columnIndex) {
|
||||
TableColumn aColumn = header.getColumnModel().getColumn(columnIndex);
|
||||
TableCellRenderer renderer = aColumn.getHeaderRenderer();
|
||||
//
|
||||
if (renderer == null) {
|
||||
renderer = new DefaultTableCellRenderer() {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Component getTableCellRendererComponent(JTable table,
|
||||
Object value, boolean isSelected, boolean hasFocus,
|
||||
int row, int column) {
|
||||
JTableHeader header = table.getTableHeader();
|
||||
if (header != null) {
|
||||
setForeground(header.getForeground());
|
||||
setBackground(header.getBackground());
|
||||
setFont(header.getFont());
|
||||
}
|
||||
setHorizontalAlignment(JLabel.CENTER);
|
||||
setText((value == null) ? "" : value.toString());
|
||||
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
String headerValue = aColumn.getHeaderValue().toString();
|
||||
Component component = renderer.getTableCellRendererComponent(header
|
||||
.getTable(), headerValue, false, false, -1, columnIndex);
|
||||
|
||||
rendererPane.add(component);
|
||||
rendererPane.paintComponent(g, component, header, cellRect.x,
|
||||
cellRect.y, cellRect.width, cellRect.height, true);
|
||||
}
|
||||
|
||||
private void paintCell(Graphics g, Rectangle cellRect, ColumnGroup cGroup) {
|
||||
TableCellRenderer renderer = cGroup.getHeaderRenderer();
|
||||
//
|
||||
if (renderer == null) {
|
||||
renderer = new DefaultTableCellRenderer() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Component getTableCellRendererComponent(JTable table,
|
||||
Object value, boolean isSelected, boolean hasFocus,
|
||||
int row, int column) {
|
||||
JTableHeader header = table.getTableHeader();
|
||||
if (header != null) {
|
||||
setForeground(header.getForeground());
|
||||
setBackground(header.getBackground());
|
||||
setFont(header.getFont());
|
||||
}
|
||||
setHorizontalAlignment(JLabel.CENTER);
|
||||
setText((value == null) ? "" : value.toString());
|
||||
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
String headerValue = cGroup.getHeaderValue().toString();
|
||||
Component component = renderer.getTableCellRendererComponent(header
|
||||
.getTable(), headerValue, false, false, -1, -1);
|
||||
|
||||
rendererPane.add(component);
|
||||
rendererPane.paintComponent(g, component, header, cellRect.x,
|
||||
cellRect.y, cellRect.width, cellRect.height, true);
|
||||
}
|
||||
|
||||
public void setHeaderHeight(int iHeight) {
|
||||
m_height = iHeight;
|
||||
}
|
||||
}
|
||||
@@ -1,157 +1,157 @@
|
||||
/*
|
||||
* 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.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;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,87 +1,139 @@
|
||||
/*
|
||||
* 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.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class TableDataProcessor {
|
||||
private static final String COLUMN_KEY_NAME = "名称";
|
||||
private static final String COLUMN_VALUE_NAME = "值";
|
||||
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) {
|
||||
if (keyword != null) {
|
||||
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 ColumnData mutilRow2col(
|
||||
ArrayList<LinkedHashMap<String, Object>> rowDatas, String keyword) {
|
||||
ColumnData datas = new ColumnData();
|
||||
ArrayList<ArrayList<LinkedHashMap<String, Object>>> tableDatas
|
||||
= new ArrayList<ArrayList<LinkedHashMap<String, Object>>>();
|
||||
|
||||
ArrayList<String> colNames = new ArrayList<String>();
|
||||
int rowCount = 0;
|
||||
|
||||
for(LinkedHashMap<String, Object> row: rowDatas) {
|
||||
if (keyword != null) {
|
||||
String keyNow = row.remove(keyword).toString();
|
||||
colNames.add(keyNow);
|
||||
}
|
||||
rowCount ++;
|
||||
|
||||
ArrayList<LinkedHashMap<String, Object>> coldatas
|
||||
= new ArrayList<LinkedHashMap<String, Object>>();
|
||||
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);
|
||||
|
||||
}
|
||||
tableDatas.add(coldatas);
|
||||
}
|
||||
datas.setCount(rowCount);
|
||||
datas.setTableDatas(tableDatas);
|
||||
datas.setNames(colNames);
|
||||
return datas;
|
||||
}
|
||||
|
||||
public static ColumnData row2col(
|
||||
ArrayList<LinkedHashMap<String, Object>> rowDatas) {
|
||||
return row2col(rowDatas,null);
|
||||
}
|
||||
|
||||
public static class ColumnData {
|
||||
private ArrayList<String> names;
|
||||
private ArrayList<LinkedHashMap<String, Object>> datas;
|
||||
private ArrayList<ArrayList<LinkedHashMap<String, Object>>> tableDatas;
|
||||
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;
|
||||
}
|
||||
|
||||
public ArrayList<ArrayList<LinkedHashMap<String, Object>>> getTableDatas() {
|
||||
return tableDatas;
|
||||
}
|
||||
|
||||
public void setTableDatas(
|
||||
ArrayList<ArrayList<LinkedHashMap<String, Object>>> tableDatas) {
|
||||
this.tableDatas = tableDatas;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user