📄 jgrouppanel.java
字号:
package flow.graph.gui.tools;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JGroupPanel extends JPanel {
/*用来管理组的三个容器*/
private JPanel pNorth = new JPanel() {
};
private JPanel pCenter = new JPanel();
private JPanel pSouth = new JPanel();
/*当前全部组的集合*/
private ArrayList groupList = new ArrayList();
/*是否已禁止添加组件*/
private boolean forbidFlag = false;
private int activeGroupIndex;
/*当前激活的组*/
private JGroupContainer activeGroup = null;
transient ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
AbstractButton bttTitle = (AbstractButton) e.getSource();
expandGroup( (JGroupContainer) bttTitle.getParent());
//System.out.println(bttTitle.getParent());
}
};
private boolean hasCreateDefaultGroup = false;
public JGroupPanel() {
initComponents();
//createDefaultGroup();
}
private void initComponents(){
this.setLayout(new BorderLayout());
this.add(pNorth, BorderLayout.NORTH);
this.add(pCenter, BorderLayout.CENTER);
this.add(pSouth, BorderLayout.SOUTH);
pNorth.setLayout(new GroupLayout());
pCenter.setLayout(new BorderLayout());
pSouth.setLayout(new GroupLayout());
forbidFlag = true;
}
public void createDefaultGroup(){
//Default Group
/*
Color bg[] = {
Color.black, Color.red, Color.orange, Color.yellow, Color.green,
Color.cyan, Color.blue, Color.white};
for (int i = 1; i <= 2; i++) {
//for (int i = 1; i <= bg.length; i++) {
//insertGroup(i - 1, "Group " + i, bg[i - 1]);
insertGroup(i - 1, "Group " + i);
Color mc = new Color(255 - bg[i - 1].getRed(),
255 - bg[i - 1].getGreen(),
255 - bg[i - 1].getBlue());
for (int j = 1; j <= 5; j++) {
JButton bttMember = new JButton("Member " + j + " of " + i);
addMember(i - 1, bttMember);
bttMember.setPreferredSize(new Dimension(1, 40));
bttMember.setOpaque(false);
bttMember.setForeground(mc);
}
getGroup(i - 1).setMemberGap(20, 5);
getGroup(i - 1).getTitleButton().setForeground(bg[i - 1]);
}*/
insertGroup(0, "业务列表");
JButton sb = new JButton("哈哈");
sb.setPreferredSize(new Dimension(1, 40));
addMember(0, sb);
insertGroup(1, "业务模板");
JButton sb1 = new JButton("哈哈");
sb1.setPreferredSize(new Dimension(1, 40));
addMember(1, sb1);
getGroup(0).setMemberGap(30, 5);
expandGroup(0);
hasCreateDefaultGroup = true;
}
public JGroupContainer getActiveGroup(){
return activeGroup;
}
/**
* @param groupNames String[] 预设组名
*/
public JGroupPanel(String groupNames[]) {
initComponents();
addGroup(groupNames);
}
/**
* 展开组
* @param name String 组名
*/
public void expandGroup(String name) {
for (int i = getGroupCount() - 1; i >= 0; i--) {
if (getGroupName(i).equals(name)) {
expandGroup(i);
}
}
}
public void expandGroup() {
/*
pNorth.removeAll();
pCenter.removeAll();
pSouth.removeAll();
pNorth.doLayout();
pCenter.doLayout();
pSouth.doLayout();
doLayout();
*/
//if(getGroupCount() > 0)
// expandGroup(0);
this.updateUI();
}
/**
* 展开组
* @param index int 组的顺序号
*/
public void expandGroup(int index) {
expandGroup(getGroup(index));
}
/**
* 展开组
* @param group JGroupContainer 组
*/
protected void expandGroup(JGroupContainer group) {
pNorth.removeAll();
pCenter.removeAll();
pSouth.removeAll();
boolean hasAddCenter = false;
for (int i = 0; i < groupList.size(); i++) {
Component c = (Component) groupList.get(i);
if (hasAddCenter) {
pSouth.add(c);
}
else if (c == group) {
pCenter.add(c, BorderLayout.CENTER);
hasAddCenter = true;
}
else {
pNorth.add(c);
}
if(group.equals(groupList.get(i)))
activeGroupIndex = i;
}
if (activeGroup != null) {
activeGroup.collapse();
}
activeGroup = group;
activeGroup.expand();
pNorth.doLayout();
pCenter.doLayout();
pSouth.doLayout();
doLayout();
}
public int getActiveGroupIndex(){
return activeGroupIndex;
}
/**
* 收缩组
* @param name String 组名
*/
public void collapseGroup(String name) {
for (int i = getGroupCount() - 1; i >= 0; i--) {
if (getGroupName(i).equals(name)) {
collapseGroup(i);
}
}
}
/**
* 收缩组
* @param index int 组的顺序号
*/
public void collapseGroup(int index) {
collapseGroup(getGroup(index));
}
/**
* 收缩组
* @param group JGroupContainer 组
*/
protected void collapseGroup(JGroupContainer group) {
if (group == activeGroup) {
activeGroup.collapse();
activeGroup = null;
}
}
/**
* 添加组
* @param name String 组名
*/
public void addGroup(String name) {
this.insertGroup(getGroupCount(), name);
}
/**
* 添加多个组
* @param names String[] 组名
*/
public void addGroup(String names[]) {
for (int i = 0; i < names.length; i++) {
addGroup(names[i]);
}
}
/**
* 插入一个组
* @param index int 顺序号
* @param name String 组名
* @param bg Color 背景色
*/
public void insertGroup(int index, String name, Color bg) {
if (index < 0 || index > groupList.size()) {
throw new ArrayIndexOutOfBoundsException("index:" + index +
" >count:" + groupList.size());
}
if(hasCreateDefaultGroup){
while(getGroupCount()>0){
removeGroup(0);
}
hasCreateDefaultGroup = false;
}
int countNorth = pNorth.getComponentCount();
int countCenter = pCenter.getComponentCount();
int countSouth = pSouth.getComponentCount();
JGroupContainer group;
if (index <= countNorth) {
group = insertGroup(pNorth, index, name, bg);
}
else if (index <= countNorth + countCenter) {
group = insertGroup(pCenter, index - countNorth, name, bg);
}
else if (index <= countNorth + countCenter + countSouth) {
group = insertGroup(pSouth, index - countNorth - countCenter, name,
bg);
}
else {
group = insertGroup(pSouth, countSouth, name, bg);
}
group.getTitleButton().addActionListener(al);
groupList.add(index, group);
}
/**
* 插入一个组
* @param index int 顺序号
* @param name String 组名
*/
public void insertGroup(int index, String name) {
//insertGroup(index, name, UIManager.getColor("Desktop.background"));
insertGroup(index, name, Color.LIGHT_GRAY);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -