/*
* Created on 2004/07/15
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.findsame;
import java.util.Vector;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
public abstract class CompositeList extends Composite implements SelectionListener{
protected Vector vector=new Vector();
private int currentPosition=0;
public CompositeList(Composite composite,int style){
super(composite,style |SWT.V_SCROLL);
ScrollBar bar=this.getVerticalBar();
if(bar!=null){
bar.addSelectionListener(this);
bar.setEnabled(false);
}
}
/*
public void add(String label){
String str[]=new String[2];
str[0]=label;
str[1]=label+".txt";
vector.add(str);
redraw();
}*/
public int countItems(){
return vector.size();
}
public void removeAll(){
Control[] controls=this.getChildren();
for(int i=0;i<controls.length;i++){
controls[i].dispose();
}
vector.removeAllElements();
}
public void redraw(){
Control[] controls=this.getChildren();
for(int i=0;i<controls.length;i++){
controls[i].dispose();
}
int size=countItems();
int totalHeight=0;
int showItems=0;
for(int i=currentPosition;i<size;i++){
Composite comp=createItem(i);
comp.pack();
comp.setLocation(0,totalHeight);
totalHeight+=comp.getSize().y;
// System.out.println(this.getSize());
if(totalHeight>this.getSize().y){
break;
}else{
showItems++;
}
}
initScrollBar(showItems);
}
/**
*
*/
private void initScrollBar(int visible) {
ScrollBar bar=this.getVerticalBar();
if(bar!=null){
if(countItems()>visible){
bar.setEnabled(true);
bar.setMaximum(countItems());
}else{
bar.setEnabled(false);
}
}
}
public abstract Composite createItem(int index);
/*
public Composite createItem(int index){
Composite cp=new Composite(this,SWT.NULL);
cp.setLayout(new GridLayout());
Label label=new Label(cp,SWT.NULL);
label.setText("label");
String[] strings=(String[])vector.elementAt(index);
for(int i=0;i<strings.length;i++){
Button bt=new Button(cp,SWT.NULL);
bt.setText(strings[i]);
}
return cp;
}*/
/* (non-Javadoc)
* @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetSelected(SelectionEvent item) {
if(item.getSource() instanceof ScrollBar){
int index=((ScrollBar)item.getSource()).getSelection();
currentPosition=index;
redraw();
}
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
}
|