我是RCP框架的初学者。我在eclipse RCP3应用程序名称中使用了一个编辑器,如“食物编辑器”。在这个编辑器中,我使用 SWT.Check 表项创建了一个SWT表。 我有关于复选框选择的问题。
这个编辑器的当前场景是,当Food编辑器打开然后我选中复选框然后我打开新的编辑器名称,如Employee_Editor。 在Employee_Editor打开之前,我选中了带有选中复选框的食物编辑器。然后我再次打开Food编辑器然后前一个“Selected”复选框显示为“Unselected”为什么会发生这种情况?
我试图解决这个问题我在互联网上搜索我发现解决方案使用数据库“选中复选框”保存为数据类型布尔值。 但我不想这样做,因为当很多时候SQL查询出现很多数据。
因此,我尝试通过在createPartControl中定义一个方法“saveSelectedCheckBox”,这是将选中的复选框表项保存到“selectedTableItemList”中。 但是当我关闭食物编辑器时我也遇到问题然后我再次打开它我的“selectedTableItemList”问题与空值有关。 我已经分享了下面给出的代码。
public class FoodDetailsEditor extends EditorPart {
public static final String ID = "rcp_demo.Editor.FoodDetailsEditor"; //$NON-NLS-1$Food_Details
public static final String BID = "Food_Details";
private static final String STORE_SELECTION = "rcp_demo.Editor.FoodDetailsEditor";
private Table table;
ArrayList<Integer> selectedTableItemList= null;
@Override
public void createPartControl(Composite parent) {
ScrolledComposite scrolledComposite_2 = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
scrolledComposite_2.setExpandHorizontal(true);
scrolledComposite_2.setExpandVertical(true);
Composite composite = new Composite(scrolledComposite_2, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION|SWT.CHECK);
GridData gd_table = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
gd_table.heightHint = 156;
gd_table.widthHint = 565;
table.setLayoutData(gd_table);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn tblclmnCheckbox = new TableColumn(table, SWT.NONE);
tblclmnCheckbox.setWidth(100);
tblclmnCheckbox.setText("Checkbox");
TableColumn tblclmnTiming = new TableColumn(table, SWT.NONE);
tblclmnTiming.setWidth(100);
tblclmnTiming.setText("Timing");
TableColumn tblclmnMonday = new TableColumn(table, SWT.NONE);
tblclmnMonday.setWidth(100);
tblclmnMonday.setText("Monday");
TableColumn tblclmnTuesday = new TableColumn(table, SWT.NONE);
tblclmnTuesday.setWidth(100);
tblclmnTuesday.setText("Tuesday");
TableColumn tblclmnWednesday = new TableColumn(table, SWT.NONE);
tblclmnWednesday.setWidth(100);
tblclmnWednesday.setText("Wednesday");
TableColumn tblclmnThursday = new TableColumn(table, SWT.NONE);
tblclmnThursday.setWidth(100);
tblclmnThursday.setText("Thursday");
TableColumn tblclmnFriday = new TableColumn(table, SWT.NONE);
tblclmnFriday.setWidth(100);
tblclmnFriday.setText("Friday");
TableColumn tblclmnSaturday = new TableColumn(table, SWT.NONE);
tblclmnSaturday.setWidth(100);
tblclmnSaturday.setText("Saturday");
scrolledComposite_2.setContent(composite);
scrolledComposite_2.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
TableItem item1 = new TableItem(table, SWT.NONE);
item1.setText(new String[]{"","10:00 to 10:30","Food1","Food2","Food3","Food4","Food5"});
TableItem item2 = new TableItem(table, SWT.NONE);
item2.setText(new String[]{"","11:00 to 11:30","Food1","Food2","Food3","Food4","Food5"});
TableItem item3 = new TableItem(table, SWT.NONE);
item3.setText(new String[]{"","12:00 to 12:30","Food1","Food2","Food3","Food4","Food5"});
TableItem item4 = new TableItem(table, SWT.NONE);
item4.setText(new String[]{"","13:00 to 13:30","Food1","Food2","Food3","Food4","Food5"});
table.addSelectionListener(new SelectionAdapter()
{
@Override
public void widgetSelected(SelectionEvent event)
{
if( event.detail == SWT.CHECK )
{
if( table.indexOf( ( TableItem )event.item ) == table.getSelectionIndex() )
{
TableItem ti = ( TableItem )event.item;
ti.setChecked( !ti.getChecked() );
System.out.println("event.index:-"+event.item);
}
}
saveSelectedCheckBox(table);
}
private void saveSelectedCheckBox(Table table) {
// TODO Auto-generated method stub
TableItem[] items = table.getItems();
selectedTableItemList=new ArrayList<Integer>();//Creating arraylist
for (int ro = 0; ro < table.getItemCount();ro++)
{ if(items[ro].getChecked()== true)
{
System.out.println("items[ro]:=="+items[ro]);
selectedTableItemList.add(ro);
}
}
for(int row = 0; row < selectedTableItemList.size();row++)
{
System.out.println("selectedTableItemList:--"+selectedTableItemList);
}
}
});
}
@Override
public void setFocus() {
// Set the focus
}
@Override
public void doSave(IProgressMonitor monitor) {
// Do the Save operation
}
@Override
public void doSaveAs() {
// Do the Save As operation
}
@Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
System.out.println("init called");
System.out.println("selectedTableItemList:--"+selectedTableItemList);
// Initialize the editor part
if (!(input instanceof FoodDetailsEditorInput)) {
throw new PartInitException("Invalid Input: Must be " + FoodDetailsEditorInput.class.getName());
}
setSite(site);
setInput(input);
}
@Override
public boolean isDirty()
{
return false;
}
@Override
public boolean isSaveAsAllowed() {
System.out.println("isSaveAsAllowed called");
return true;
}
}
我想当Food编辑器打开然后我选中复选框然后我打开新的编辑器名称,如Employee_Editor。 然后我再次打开Food编辑器“Selected”复选框,显示为“Selected”。怎么做到这一点?请帮帮我。
我的系统配置:
Windows 64Bit OS.
Eclipse Kepler 32 bit.
jdk 1.8
答案 0 :(得分:3)
当您关闭编辑器时,您将失去其状态,并在打开编辑器期间再次执行createPartControl()
。所以你让你的桌子处于初始状态。您应该在创建表格后执行TableItem.setChecked()
并将值设置为上一个用户选择的值。最好的方法 - 将表格行中的数据转换为对象,然后使用TableViewer创建表格。
private TableViewer viewer;
private List<WeeklyMenu> menu = Menu.MENU.getWeeklyMenu();
public void createPartControl(Composite parent) {
Table table = createTable(parent);
viewer.setInput(menu);
for (TableItem item : table.getItems()) {
WeeklyMenu weeklyMenu = (WeeklyMenu)item.getData();
item.setChecked(weeklyMenu.isChecked());
}
viewer.refresh();
table.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if( e.detail == SWT.CHECK )
{
TableItem item = (TableItem)e.item;
WeeklyMenu weeklyMenu = (WeeklyMenu)item.getData();
weeklyMenu.setChecked(item.getChecked());
}
}
});
}
private Table createTable(Composite parent) {
viewer = new TableViewer(parent, SWT.BORDER | SWT.FULL_SELECTION | SWT.CHECK);
Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
viewer.setContentProvider(new IStructuredContentProvider() {
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public Object[] getElements(Object inputElement) {
return menu.toArray();
}
});
createColumns(table);
return table;
}
private void createColumns(Table table) {
TableLayout layout = new TableLayout();
layout.addColumnData(new ColumnWeightData(10, true));
layout.addColumnData(new ColumnWeightData(20, true));
layout.addColumnData(new ColumnWeightData(20, true));
layout.addColumnData(new ColumnWeightData(20, true));
layout.addColumnData(new ColumnWeightData(20, true));
layout.addColumnData(new ColumnWeightData(20, true));
layout.addColumnData(new ColumnWeightData(20, true));
layout.addColumnData(new ColumnWeightData(20, true));
layout.addColumnData(new ColumnWeightData(20, true));
table.setLayout(layout);
TableViewerColumn column = createTableViewerColumn("Checkbox");
column.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element) {
return "";
}
});
column = createTableViewerColumn("Timing");
column.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element) {
return ((WeeklyMenu)element).getTiming();
}
});
column = createTableViewerColumn("Monday");
column.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element) {
return ((WeeklyMenu)element).getMonFood();
}
});
column = createTableViewerColumn("Tuesday");
column.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element) {
return ((WeeklyMenu)element).getTueFood();
}
});
column = createTableViewerColumn("Wednesday");
column.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element) {
return ((WeeklyMenu)element).getWedFood();
}
});
column = createTableViewerColumn("Thursday");
column.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element) {
return ((WeeklyMenu)element).getThuFood();
}
});
column = createTableViewerColumn("Friday");
column.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element) {
return ((WeeklyMenu)element).getFriFood();
}
});
column = createTableViewerColumn("Saturday");
column.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element) {
return ((WeeklyMenu)element).getSatFood();
}
});
column = createTableViewerColumn("Sunday");
column.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element) {
return ((WeeklyMenu)element).getSunFood();
}
});
}
private TableViewerColumn createTableViewerColumn(String name) {
TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.CENTER);
TableColumn column = viewerColumn.getColumn();
column.setText(name);
column.setMoveable(true);
return viewerColumn;
}
班级WeeklyMenu
:
public class WeeklyMenu {
private boolean isChecked;
private String timing;
private String monFood;
private String tueFood;
private String wedFood;
private String thuFood;
private String friFood;
private String satFood;
private String sunFood;
public WeeklyMenu(boolean isChecked, String timing, String monFood, String tueFood, String wedFood, String thuFood, String friFood,
String sutFood, String sunFood) {
super();
this.isChecked = isChecked;
this.timing = timing;
this.monFood = monFood;
this.tueFood = tueFood;
this.wedFood = wedFood;
this.thuFood = thuFood;
this.friFood = friFood;
this.satFood = sutFood;
this.sunFood = sunFood;
}
public String getTiming() {
return timing;
}
public void setTiming(String timing) {
this.timing = timing;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public String getMonFood() {
return monFood;
}
public void setMonFood(String monFood) {
this.monFood = monFood;
}
public String getTueFood() {
return tueFood;
}
public void setTueFood(String tueFood) {
this.tueFood = tueFood;
}
public String getWedFood() {
return wedFood;
}
public void setWedFood(String wedFood) {
this.wedFood = wedFood;
}
public String getThuFood() {
return thuFood;
}
public void setThuFood(String thuFood) {
this.thuFood = thuFood;
}
public String getFriFood() {
return friFood;
}
public void setFriFood(String friFood) {
this.friFood = friFood;
}
public String getSatFood() {
return satFood;
}
public void setSatFood(String sutFood) {
this.satFood = sutFood;
}
public String getSunFood() {
return sunFood;
}
public void setSunFood(String sunFood) {
this.sunFood = sunFood;
}
}
班级Menu
:
public class Menu {
public final static Menu MENU = new Menu();
private List<WeeklyMenu> weeklyMenu = new ArrayList<WeeklyMenu>();
private Menu(){
weeklyMenu.add(new WeeklyMenu(false, "10:00 to 10:30", "Food1", "Food2", "Food3", "Food4", "Food5", "Food6", "Food7"));
weeklyMenu.add(new WeeklyMenu(false, "10:30 to 11:00", "Food1", "Food2", "Food3", "Food4", "Food5", "Food6", "Food7"));
weeklyMenu.add(new WeeklyMenu(false, "11:00 to 11:30", "Food1", "Food2", "Food3", "Food4", "Food5", "Food6", "Food7"));
weeklyMenu.add(new WeeklyMenu(false, "11:30 to 12:00", "Food1", "Food2", "Food3", "Food4", "Food5", "Food6", "Food7"));
}
public List<WeeklyMenu> getWeeklyMenu() {
return weeklyMenu;
}
}
将此类添加到您的项目中,并将createPartControl()
替换为引用的代码段。
答案 1 :(得分:0)
table.addSelectionListener(new SelectionAdapter()
{
@Override
public void widgetSelected(SelectionEvent event)
{
if( event.detail == SWT.CHECK )
{
if( table.indexOf( ( TableItem )event.item ) == table.getSelectionIndex() )
{
TableItem ti = ( TableItem )event.item;
ti.setChecked( !ti.getChecked() );
}
saveSelectedCheckBox(table);
}
}
private void saveSelectedCheckBox(Table table)
{
for (int ro = 0; ro < table.getItemCount();ro++)//#32
{ if(items[ro].getChecked()== true)//#32
{//#32
System.out.println("items[ro]:=="+items[ro]);//#32
selectedTableItemList.add(ro);//#32
}//#32
} //#32
for(int row = 0; row < selectedTableItemList.size();row++)//#32
{//#32
//*For FileSaving try
FileDialog dialog = new FileDialog(table.getShell(), SWT.SAVE);
TableItem[] items =table.getItems();
dialog.setText("Save");
String absolutePath = dialog.open();
if (absolutePath == null)
return;
File fl = new File(dialog.getFilterPath() + File.separator + dialog.getFileName());
FileWriter flwr;
int cls = table.getColumnCount();
try {
flwr = new FileWriter(fl);
for (int i = 0; i < items.length; i++) {
for (int j = 0; j <= cls; j++) {
flwr.write(items[i].getText(j) + "\t");
}
flwr.write("\n");
}
flwr.flush();
flwr.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});