📄 tablecombostrategy.java
字号:
package com.swtplus.widgets.combo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import com.swtplus.internal.Assert;
import com.swtplus.widgets.PCombo;
/**
* TableComboStrategy creates a Table widget inside a PCombo's dropdown.
* The value object returned to the PCombo is a TableItem.
*/
public class TableComboStrategy implements IComboStrategy {
private Table table;
private int tableStyle = SWT.NONE;
private int visibleItemCount = 5;
/**
* Creates a TableComboStrategy with the given strategy style and the
* given SWT Table style.
*
* @param style style bits for this strategy (currently none available)
* @param tableStyle SWT style bits to be given to Table constructor
*/
public TableComboStrategy(int style,int tableStyle) {
this.tableStyle = tableStyle;
}
/* (non-Javadoc)
* @see com.swtplus.widgets.combo.IComboStrategy#getSize(int)
*/
public Point getSize(int comboWidth) {
int x = Math.max(comboWidth,table.computeSize(SWT.DEFAULT,SWT.DEFAULT).x);
int y = table.getHeaderHeight() + (visibleItemCount * table.getItemHeight());
return new Point(x,y);
}
/* (non-Javadoc)
* @see com.swtplus.widgets.combo.IComboStrategy#createContents(org.eclipse.swt.widgets.Composite, com.swtplus.widgets.combo.IComboUpdater)
*/
public Control createContents(Composite parent, final IComboUpdater updater) {
table = new Table(parent,tableStyle);
table.addSelectionListener(new SelectionListener(){
public void widgetSelected(SelectionEvent e) {
TableItem[] items = table.getSelection();
if (items.length < 1)
return;
updater.update(items[0],true);
}
public void widgetDefaultSelected(SelectionEvent e) {
}}
);
return table;
}
/* (non-Javadoc)
* @see com.swtplus.widgets.combo.IComboStrategy#keyUp(com.swtplus.widgets.combo.IComboUpdater)
*/
public void keyUp(IComboUpdater updater) {
int sel = table.getSelectionIndex();
if (sel < 1)
return;
table.setSelection(sel -1);
table.showSelection();
TableItem[] items = table.getSelection();
if (items.length < 1)
return;
updater.update(items[0],false);
}
/* (non-Javadoc)
* @see com.swtplus.widgets.combo.IComboStrategy#keyDown(com.swtplus.widgets.combo.IComboUpdater)
*/
public void keyDown(IComboUpdater updater) {
if (table.getItemCount() == 0)
return;
int sel = table.getSelectionIndex();
if (sel == -1){
table.setSelection(0);
} else if (sel != table.getItemCount() -1){
table.setSelection(sel +1);
}
table.showSelection();
TableItem[] items = table.getSelection();
if (items.length < 1)
return;
updater.update(items[0],false);
}
/* (non-Javadoc)
* @see com.swtplus.widgets.combo.IComboStrategy#selectValue(java.lang.Object)
*/
public void selectValue(Object newValue) {
TableItem[] items = table.getItems();
TableItem selectThis = null;
for (int i = 0; i < items.length; i++) {
TableItem item = items[i];
if (item.getText().equals(newValue)){
selectThis = item;
break;
}
}
if (selectThis != null){
table.setSelection(new TableItem[]{selectThis});
}
}
/* (non-Javadoc)
* @see com.swtplus.widgets.combo.IComboStrategy#canReturnFocusOnMouseDown()
*/
public boolean canReturnFocusOnMouseDown() {
return false;
}
/**
* Returns the number of items that will be visible in the
* popup.
*
* @return Items to be visible
*/
public int getVisibleItemCount() {
return visibleItemCount;
}
/**
* Sets how many items will be visible in the popup.
* @param visibleItemCount Items to be visible
*
*/
public void setVisibleItemCount(int visibleItemCount) {
this.visibleItemCount = visibleItemCount;
}
/**
* Returns the Table widget created on the popup.
*
* @return the underlying Table widget *
*/
public Table getTable() {
return table;
}
/* (non-Javadoc)
* @see com.swtplus.widgets.combo.IComboStrategy#mouseWheel(int)
*/
public void mouseWheel(int linesToScroll) {
linesToScroll = -linesToScroll;
if (table.getItemCount() == 0)
return;
int current = table.getTopIndex();
current += linesToScroll;
if (current < 0){
current = 0;
} else if (current > table.getItemCount() -1){
current = table.getItemCount() -1;
}
table.setTopIndex(current);}
/* (non-Javadoc)
* @see com.swtplus.widgets.combo.IComboStrategy#dispose()
*/
public void dispose() {
}
/* (non-Javadoc)
* @see com.swtplus.widgets.combo.IComboStrategy#createLabelProvider()
*/
public IComboLabelProvider createLabelProvider(){
IComboLabelProvider labelProvider = new IComboLabelProvider(){
public String getText(Object element) {
if (element == null)
return "";
if (element instanceof String)
return (String) element;
TableItem item = (TableItem) element;
return item.getText();
}
public Image getImage(Object element) {
if (element == null)
return null;
if (element instanceof String)
return null;
TableItem item = (TableItem) element;
return item.getImage();
}
public String getToolTip(Object element) {
if (element == null)
return "";
if (element instanceof String)
return (String) element;
TableItem item = (TableItem) element;
return item.getText();
}
public void dispose() {
}
};
return labelProvider;
}
/* (non-Javadoc)
* @see com.swtplus.widgets.combo.IComboStrategy#jumpTo(java.lang.String)
*/
public void jumpTo(String beginning) {
TableItem[] items = table.getItems();
for (int i = 0; i < items.length; i++) {
TableItem item = items[i];
if (item.getText().toLowerCase().startsWith(beginning.toLowerCase())){
table.setTopIndex(i);
return;
}
}
}
/* (non-Javadoc)
* @see com.swtplus.widgets.combo.IComboStrategy#initializeEditor(org.eclipse.swt.widgets.Text)
*/
public void initializeEditor(Text editor) {
}
/* (non-Javadoc)
* @see com.swtplus.widgets.combo.IComboStrategy#initialize(com.swtplus.widgets.combo.PCombo)
*/
public void initialize(PCombo pCombo) {
}
public void checkValue(Object newValue) {
Assert.isTrue(newValue instanceof String || newValue instanceof TableItem,"Value must be instance of String or TableItem");
}
/* (non-Javadoc)
* @see com.swtplus.widgets.combo.IComboStrategy#isLazyCreation()
*/
public boolean isLazyCreation() {
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -