//package psn.nicholas.swing;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JPanel;
public class JGroupPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel northPanel;
private JPanel centerPanel;
private JPanel southPanel;
private ArrayList groupList = new ArrayList();
private boolean forbidFlag = false;
private boolean hasCreateDefaultGroup = false;
private MemberGroup activeGroup = null;
private GroupHandler al;
public JGroupPanel() {
super(new BorderLayout());
al = new GroupHandler();
northPanel = new JPanel(new GridLayout(0,1,0,0));
centerPanel = new JPanel(new BorderLayout());
southPanel = new JPanel(new GridLayout(0,1,0,0));
add(northPanel, BorderLayout.NORTH);
add(centerPanel, BorderLayout.CENTER);
add(southPanel, BorderLayout.SOUTH);
// createDefaultGroup();
}
/*
private void createDefaultGroup() {
forbidFlag = true;
for(int i=0;i<3;i++) {
addGroup("Group"+i, i);
for(int j=0;j<5;j++) {
MemberCell cell = new MemberCell(j,"Member"+i+j);
addMember(i,cell);
}
}
expandGroup(0);
hasCreateDefaultGroup = true;
}
*/
//...
protected void addImpl(Component comp, Object constraints, int index) {
if (forbidFlag) {
if (! (comp instanceof MemberGroup)) {
throw new UnsupportedOperationException(
"JGroupPane can't add component!");
}
}
else {
super.addImpl(comp, constraints, index);
}
}
//add group
public void addGroup(String name) {
this.addGroup(name, getGroupCount());
}
public void addGroup(String names[]) {
for (int i = 0; i < names.length; i++) {
addGroup(names[i]);
}
}
//key method
public void addGroup(String name, int index) {
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 = northPanel.getComponentCount();
int countCenter = centerPanel.getComponentCount();
int countSouth = southPanel.getComponentCount();
MemberGroup group;
if (index <= countNorth) {
group = insertGroup(northPanel, name);
}
else if (index <= countNorth + countCenter) {
group = insertGroup(centerPanel, name);
}
else if (index <= countNorth + countCenter + countSouth) {
group = insertGroup(southPanel, name);
}
else {
group = insertGroup(southPanel, name);
}
group.getTitleButton().addActionListener(al);
groupList.add(index, group);
}
private MemberGroup insertGroup(JPanel p, String name) {
MemberGroup group = new MemberGroup(name);
p.add(group);
return group;
}
//remove
public void removeGroup(String name) {
for (int i = getGroupCount() - 1; i >= 0; i--) {
if (getGroupName(i).equals(name)) {
this.removeGroup(i);
}
}
}
public void removeGroup(int index) {
MemberGroup c = (MemberGroup) groupList.get(index);
c.getParent().remove(c);
c.getTitleButton().removeActionListener(al);
}
//expand
public void expandGroup(String name) {
for (int i = getGroupCount() - 1; i >= 0; i--) {
if (getGroupName(i).equals(name)) {
expandGroup(i);
}
}
}
public void expandGroup(int index) {
expandGroup(getGroup(index));
}
//key method
private void expandGroup(MemberGroup group) {
northPanel.removeAll();
centerPanel.removeAll();
southPanel.removeAll();
boolean hasAddCenter = false;
for (int i = 0; i < groupList.size(); i++) {
Component c = (Component) groupList.get(i);
if (hasAddCenter) {
southPanel.add(c);
}
else if (c == group) {
centerPanel.add(c, BorderLayout.CENTER);
hasAddCenter = true;
}
else {
northPanel.add(c);
}
}
if (activeGroup != null) {
activeGroup.collapse();
}
activeGroup = group;
activeGroup.expand();
northPanel.doLayout();
centerPanel.doLayout();
southPanel.doLayout();
doLayout();
}
//collapse
public void collapseGroup(String name) {
for (int i = getGroupCount() - 1; i >= 0; i--) {
if (getGroupName(i).equals(name)) {
collapseGroup(i);
}
}
}
public void collapseGroup(int index) {
collapseGroup(getGroup(index));
}
//key method
private void collapseGroup(MemberGroup group) {
if (group == activeGroup) {
activeGroup.collapse();
activeGroup = null;
}
}
//set name
public void setGroupName(int index, String name) {
this.getGroup(index).setName(name);
}
//get name
public String getGroupName(int groupIndex) {
return getGroup(groupIndex).getName();
}
public String[] getGroupNames() {
String sResult[] = new String[getGroupCount()];
for (int i = 0; i < getGroupCount(); i++) {
sResult[i] = getGroupName(i);
}
return sResult;
}
//get count
public int getGroupCount() {
return groupList.size();
}
//member group
//add
public void addMember(int groupIndex, Component member) {
getGroup(groupIndex).addMember(member,getGroup(groupIndex).getMemberCount());
}
public void addMember(int groupIndex, int memberIndex, Component member) {
getGroup(groupIndex).addMember(member, memberIndex);
}
//remove
public void removeMember(int groupIndex, int memberIndex) {
getGroup(groupIndex).removeMember(memberIndex);
}
//get
public Component getMember(int groupIndex, int memberIndex) {
return getGroup(groupIndex).getMember(memberIndex);
}
public Component[] getMembers(int groupIndex) {
return getGroup(groupIndex).getMembers();
}
//get count
public int getMemberCount(int groupIndex) {
return getGroup(groupIndex).getMemberCount();
}
//get group
protected MemberGroup getGroup(int index) {
return (MemberGroup) groupList.get(index);
}
private class GroupHandler implements ActionListener {
public GroupHandler() {}
public void actionPerformed(ActionEvent ae) {
JButton button = (JButton)ae.getSource();
expandGroup((MemberGroup)button.getParent());
}
}
}