📄 inputblockdata.java
字号:
package com.sensevision.reader;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
public class InputBlockData extends Dialog {
private Text text_3;
private Text text_1;
private Text text;
private Table table;
private TableViewer tableViewer;
// 表格个的列名称
public static final String[] COLUMN_NAME = { "headName", "block1",
"block2", "block3", "block4" };
protected Object result;
private static List<DataCollection> data;
public static boolean cancel=false;
// 表格中列的索引号
public static final int headName = 0;
public static final int block1 = 1;
public static final int block2 = 2;
public static final int block3 = 3;
public static final int block4 = 4;
protected Shell shell;
/**
* Create the dialog
*
* @param parent
* @param style
*/
public InputBlockData(Shell parent, int style) {
super(parent, style);
}
/**
* Create the dialog
*
* @param parent
*/
public InputBlockData(Shell parent) {
this(parent, SWT.NONE);
}
/**
* Open the dialog
*
* @return the result
*/
public Object open(String tag) {
createContents(tag);
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}
/**
* Create contents of the dialog
*/
protected void createContents(String tag) {
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setSize(428, 410);
shell.setText("Input Block Data Dialog");
tableViewer = new TableViewer(shell, SWT.LEFT);
table = tableViewer.getTable();
table.setLinesVisible(true);
table.setHeaderVisible(false);
table.setBounds(10, 93, 400, 250);
final TableColumn newColumnTableColumn = new TableColumn(table,
SWT.NONE);
newColumnTableColumn.setWidth(80);
newColumnTableColumn.setText("");
final TableColumn newColumnTableColumn_1 = new TableColumn(table,
SWT.NONE);
newColumnTableColumn_1.setWidth(80);
newColumnTableColumn_1.setText("");
final TableColumn newColumnTableColumn_2 = new TableColumn(table,
SWT.NONE);
newColumnTableColumn_2.setWidth(80);
newColumnTableColumn_2.setText("");
final TableColumn newColumnTableColumn_3 = new TableColumn(table,
SWT.NONE);
newColumnTableColumn_3.setWidth(80);
newColumnTableColumn_3.setText("");
final TableColumn newColumnTableColumn_4 = new TableColumn(table,
SWT.NONE);
newColumnTableColumn_4.setWidth(80);
newColumnTableColumn_4.setText("");
text = new Text(shell, SWT.BORDER);
text.setBounds(143, 10, 192, 25);
text.setText(tag);
text.setEnabled(false);
text_1 = new Text(shell, SWT.BORDER);
text_1.setBounds(116, 46, 60, 25);
text_1.setTextLimit(2);
text_3 = new Text(shell, SWT.BORDER);
text_3.addModifyListener(new ModifyListener() {
public void modifyText(final ModifyEvent e) {
int start = 0;
int count = 0;
if (text_1.getText().matches("\\d*")) {
if (text_1.getText() != null
&& !text_1.getText().trim().equals("")) {
start = Integer.parseInt(text_1.getText());
}
}
if (text_3.getText().matches("\\d*")) {
if (text_3.getText() != null
&& !text_3.getText().trim().equals("")) {
count = Integer.parseInt(text_3.getText());
}
}
initData(start, Integer.parseInt(text_3.getText()));
}
});
text_3.setBounds(294, 46, 60, 25);
text_3.setTextLimit(3);
final Label label = new Label(shell, SWT.CENTER);
label.setText("Tag");
label.setBounds(34, 13, 76, 22);
final Label label_1 = new Label(shell, SWT.CENTER);
label_1.setBounds(34, 46, 76, 22);
label_1.setText("Start Block");
final Label label_1_1 = new Label(shell, SWT.CENTER);
label_1_1.setBounds(202, 49, 76, 22);
label_1_1.setText("Block Count");
final Button button = new Button(shell, SWT.NONE);
button.addMouseListener(new MouseAdapter() {
public void mouseDown(final MouseEvent e) {
shell.close();
}
});
button.setText("写入");
button.setBounds(128, 353, 48, 22);
final Button button_1 = new Button(shell, SWT.NONE);
button_1.addMouseListener(new MouseAdapter() {
public void mouseDown(final MouseEvent e) {
cancel=true;
shell.close();
}
});
button_1.setText("取消");
button_1.setBounds(240, 353, 48, 22);
//
}
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell1 = new Shell(display, SWT.SHELL_TRIM);
InputBlockData aa = new InputBlockData(shell1, SWT.LEFT);
aa.open("123");
aa.createContents("123");
// shell1.layout();
// shell1.setSize(450, 450);
// shell1.open();
// shell1.setBounds(201, 91, 64, 64);
// final Combo combo = comboViewer.getCombo();
// combo.setBounds(317, 80, 129, 286);
// shell1.layout();
// while (!shell1.isDisposed()) {
// if (!display.readAndDispatch())
// display.sleep();
// }
}
// 初始化表格数据,通常是从数据库中读出
private void initData(int start, int count) {
data = new ArrayList<DataCollection>();
for (int i = start; i < count+start; i++) {
data.add(new DataCollection("Block" + i, "00", "00", "00",
"00"));
}
// persons.add( new PersonEO("","","李四","女","白色"));
// 设置数据
tableViewer.setContentProvider(new MyContentProvider());
// 设置视图
tableViewer.setLabelProvider(new MyLabelProvider());
// 设置表格数据对象,该方法非常重要,是表格数据入口
tableViewer.setInput(data);
// createContextMenu();
// 设置列属性
tableViewer.setColumnProperties(COLUMN_NAME);
// 设置单元格编辑器对象数组
CellEditor[] editors = new CellEditor[5];
editors[0] = null;
editors[1] = new TextCellEditor(tableViewer.getTable());
editors[2] = new TextCellEditor(tableViewer.getTable());
editors[3] = new TextCellEditor(tableViewer.getTable());
editors[4] = new TextCellEditor(tableViewer.getTable());
// 设置单元格编辑器
tableViewer.setCellEditors(editors);
// 设置单元个修改器
tableViewer.setCellModifier(new ICellModifier() {
// 如果该列为第一列,设置不能修改
public boolean canModify(Object element, String property) {
// if ( property.equals(COLUMN_NAME[0]))
// return false;
return true;
}
// 当处于编辑状态时所显示的值
public Object getValue(Object element, String property) {
DataCollection p = (DataCollection) element;
if (property.equals(COLUMN_NAME[0]))
return p.getHeadName();
if (property.equals(COLUMN_NAME[1]))
return p.getBlock1();
else if (property.equals(COLUMN_NAME[2]))
return p.getBlock2();
else if (property.equals(COLUMN_NAME[3]))
return p.getBlock3();
else if (property.equals(COLUMN_NAME[4]))
return p.getBlock4();
return null;
}
// 当修改后设置更新数据
public void modify(Object element, String property, Object value) {
if (element instanceof Item)
element = ((Item) element).getData();
DataCollection p = (DataCollection) element;
if (property.equals(COLUMN_NAME[0]))
p.setHeadName((String) value);
if (property.equals(COLUMN_NAME[1]))
p.setBlock1((String) value);
else if (property.equals(COLUMN_NAME[2]))
p.setBlock2((String) value);
else if (property.equals(COLUMN_NAME[3]))
p.setBlock3((String) value);
else if (property.equals(COLUMN_NAME[4]))
p.setBlock4((String) value);
// 刷新表格
tableViewer.refresh();
}
});
}
public class MyContentProvider implements IStructuredContentProvider {
// 将初始化数据的入口对象转换成表格使用的数组对象
public Object[] getElements(Object inputElement) {
if (inputElement instanceof List)
return ((List) inputElement).toArray();
return null;
}
// 释放该对象时调用的方法
public void dispose() {
}
// 当表格中的数据改变时调用该方法
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
public class MyLabelProvider implements ITableLabelProvider {
// 设置每个单元格所显示的图标
public Image getColumnImage(Object element, int columnIndex) {
/*
* //如果是性别所在的列 if (columnIndex == GENDER){ //类型转换,element代表表格中的一行
* DataCollection person = (DataCollection)element; //根据不同的值设置不同的图标
* if ( person.getGender().equals("男")) return
* ImageFactory.loadImage(
* Display.getCurrent(),ImageFactory.ICON_BOY); else if (
* person.getGender().equals("女")) return ImageFactory.loadImage(
* Display.getCurrent(),ImageFactory.ICON_GIRL); }
*/
return null;
}
// 设置每个单元格所显示的文字
public String getColumnText(Object element, int columnIndex) {
// 类型转换,element代表表格中的一行
DataCollection dataCollection = (DataCollection) element;
if (columnIndex == headName)// 如果是表头
return dataCollection.getHeadName() + "";
if (columnIndex == block1)// 如果是第一列
return dataCollection.getBlock1() + "";
else if (columnIndex == block2)// 如果是第二列
return dataCollection.getBlock2() + "";
else if (columnIndex == block3)// 如果是第三列
return dataCollection.getBlock3();
else if (columnIndex == block4)// 如果是第四列
return dataCollection.getBlock4() + "";
return "";
}
// 释放对象时释放图像资源
public void dispose() {
// this.dispose();
}
// 以下方法为空实现
public void addListener(ILabelProviderListener listener) {
}
public boolean isLabelProperty(Object element, String property) {
return false;
}
public void removeListener(ILabelProviderListener listener) {
}
}
// public class DelAction extends Action{
// public DelAction(){
// setText("删除");
// }
//
// public void run() {
// //获得当前的所选中的行
// StructuredSelection selection = (StructuredSelection)
// table.getSelection();
// //注意,同时选中的可能是多行,这是返回的是数组对象
// //获得数组对象的一个元素,也就是只有选中一行的情况
// DataCollection p= (DataCollection)selection.getFirstElement();
// //从表中删除该行
// table.remove(p);
// }
//
// }
private void createActions() {
}
public static List getBlockData(){
return data;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -