/*
* 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 CompositePixelList extends Composite implements SelectionListener{
protected Vector vector=new Vector();
private int currentPosition=0;
private int scrollPixel=50;
public int getScrollPixel() {
return scrollPixel;
}
public void setScrollPixel(int scrollPixel) {
this.scrollPixel = scrollPixel;
}
private int totalHeight;
public CompositePixelList(Composite composite,int style){
super(composite,style |SWT.V_SCROLL); //should support H 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();
}
private void removeAllControls(){
Control[] controls=this.getChildren();
for(int i=0;i<controls.length;i++){
controls[i].dispose();
}
}
public CompositeListItem[] getCompositeItems(){
CompositeListItem[] items=(CompositeListItem[])vector.toArray(new CompositeListItem[vector.size()]);
return items;
}
public void initChildren(){
CompositeListItem[] items=getCompositeItems();
for(int i=0;i<items.length;i++){
Control control=items[i].createItem(this);
items[i].setHeight(control.getSize().y);
totalHeight+=control.getSize().y;
control.dispose();
}
initScrollBar();
}
public void redraw(){
int size=countItems();
int itemY=0;
int startY=scrollPixel*currentPosition;
removeAllControls();
CompositeListItem[] items=getCompositeItems();
for(int i=0;i<items.length;i++){
if(itemY+items[i].getHeight()>=startY){
Composite composite=items[i].createItem(this);
composite.setLocation(0,itemY-startY);
}
if(itemY>startY+this.getSize().y){
break;
}
itemY+=items[i].getHeight();
}
}
/**
*
*/
private void initScrollBar() {
ScrollBar bar=this.getVerticalBar();
if(bar!=null){
if(this.getSize().y<totalHeight){
bar.setEnabled(true);
bar.setMaximum((totalHeight-this.getSize().y)/scrollPixel+bar.getThumb()+1);
bar.setIncrement(1);
bar.setMinimum(0);
bar.setSelection(0);
}else{
bar.setEnabled(false);
}
}
}
/*
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
}
}
|