/*
* Created on 2004/09/05
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.commons.swt.ui;
import java.util.List;
import java.util.Vector;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
/**
*
*
*/
public class GroupCheckTreeContoler implements SelectionListener{
private Color selectedColor;
private Color notSelectedColor;
private String pathSeparater="/";
public Color getNotSelectedColor() {
return notSelectedColor;
}
public void setNotSelectedColor(Color notSelectedColor) {
this.notSelectedColor = notSelectedColor;
}
public String getPathSeparater() {
return pathSeparater;
}
public void setPathSeparater(String pathSeparater) {
this.pathSeparater = pathSeparater;
}
public Color getSelectedColor() {
return selectedColor;
}
public void setSelectedColor(Color selectedColor) {
this.selectedColor = selectedColor;
}
public GroupCheckTreeContoler(Tree tree){
selectedColor=new Color(tree.getDisplay(),0,0,0);
notSelectedColor=new Color(tree.getDisplay(),128,128,128);
tree.addSelectionListener(this);
TreeItem rootItem=tree.getTopItem();
setNotSelectedColor(rootItem);
TreeItem[] selectedLeafs=getSelectedLeafItem(rootItem);
for(int i=0;i<selectedLeafs.length;i++){
updateStatus(selectedLeafs[i]);
}
}
/**
* @param item
*/
private void updateStatus(final TreeItem item) {
TreeItem currentTreeItem=item;
if(currentTreeItem.getItemCount()==0){
if(currentTreeItem.getChecked()){
currentTreeItem.setForeground(selectedColor);
}else{
currentTreeItem.setForeground(notSelectedColor);
}
}
while(currentTreeItem.getParentItem()!=null){
currentTreeItem=currentTreeItem.getParentItem();
int selectionCount=0;
TreeItem[] childrens=currentTreeItem.getItems();
boolean hasItem=false;
for(int i=0;i<childrens.length;i++){
if(childrens[i].getChecked()==true){
selectionCount++;
}
if(childrens[i].getForeground().equals(selectedColor)){
hasItem=true;
}
}
if(selectionCount==currentTreeItem.getItemCount()){
currentTreeItem.setChecked(true);
currentTreeItem.setForeground(selectedColor);
}else if(hasItem){
currentTreeItem.setForeground(selectedColor);
currentTreeItem.setChecked(false);
}else{
currentTreeItem.setForeground(notSelectedColor);
currentTreeItem.setChecked(false);
}
}
}
/**
* @param rootItem
*/
private final void setNotSelectedColor(TreeItem currentItem) {
currentItem.setForeground(notSelectedColor);
if(currentItem.getItemCount()>0){
TreeItem children[]=currentItem.getItems();
for (int i = 0; i < children.length; i++) {
setNotSelectedColor(children[i]);
}
}
}
public TreeItem[] getSelectedLeafItem(TreeItem rootItem){
TreeItem[] leafs=getLeafItem(rootItem);
Vector selecteds=new Vector();
for(int i=0;i<leafs.length;i++){
if(leafs[i].getChecked()){
selecteds.add(leafs[i]);
}
}
return (TreeItem[])selecteds.toArray(new TreeItem[selecteds.size()]);
}
public TreeItem[] getLeafItem(TreeItem rootItem){
Vector leafVector=new Vector();
checkLeaf(leafVector,rootItem);
TreeItem[] items=(TreeItem[])leafVector.toArray(new TreeItem[leafVector.size()]);
return items;
}
public String[] getPaths(TreeItem[] items){
String paths[]=new String[items.length];
for (int i = 0; i < paths.length; i++) {
paths[i]=getPath(items[i]);
}
return paths;
}
/**
* @param leafVector
* @param currentItem
*/
private void checkLeaf(List leafVector, TreeItem currentItem) {
if(currentItem.getItemCount()>0){
TreeItem children[]=currentItem.getItems();
for (int i = 0; i < children.length; i++) {
checkLeaf(leafVector,children[i]);
}
}else{
if(currentItem.getChecked()){
leafVector.add(currentItem);
}
}
}
public String getPath(TreeItem item){
String result=item.getText();
TreeItem currentItem=item;
while(item.getParentItem()!=null){
currentItem=item.getParentItem();
result=currentItem.getText()+pathSeparater+result;
}
return result;
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetSelected(SelectionEvent event) {
if(event.getSource() instanceof Tree){
TreeItem selectTreeItem=(TreeItem)event.item;
if(selectTreeItem.getItemCount()>0){
if(selectTreeItem.getChecked()){
selectAllChildren(selectTreeItem);
}else{
unselectAllChildren(selectTreeItem);
}
}
updateStatus(selectTreeItem);
}
}
/**
* @param selectTreeItem
*/
private void unselectAllChildren(TreeItem currentTreeItem) {
currentTreeItem.setForeground(notSelectedColor);
TreeItem[] childrens=currentTreeItem.getItems();
for(int i=0;i<childrens.length;i++){
childrens[i].setChecked(false);
childrens[i].setForeground(notSelectedColor);
unselectAllChildren(childrens[i]);
}
}
/**
* @param selectTreeItem
*/
private void selectAllChildren(TreeItem currentTreeItem) {
currentTreeItem.setForeground(selectedColor);
TreeItem[] childrens=currentTreeItem.getItems();
for(int i=0;i<childrens.length;i++){
childrens[i].setForeground(selectedColor);
childrens[i].setChecked(true);
selectAllChildren(childrens[i]);
}
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
}
|