看看是什么问题

liush8421 2013-02-02 01:17:35
RCP完成一个View如图所示:

在此界面中我第一次可以添加保存成功,但第二次却不能成功。不添加,只修改没有问题。添加后进行删除操作后如果在添加,仍然可以成功保存!
代码如下:
public class SPClassView extends ViewPart {
...
public void createPartControl(Composite parent) {
...
tv = new TreeViewer(group, SWT.BORDER);
tree = tv.getTree();
...
tv.setContentProvider(new TreeContentProvider());
tv.setLabelProvider(new TreeLabelProvider());
tv.setInput(TreeData());
tableViewer = new TableViewer(group, style);

table = tableViewer.getTable();
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
table.setLinesVisible(true);
table.setHeaderVisible(true);
TableLayout layout = new TableLayout();
table.setLayout(layout);
layout.addColumnData(new ColumnWeightData(100));
final TableColumn newColumnTableColumn_1 = new TableColumn(table,SWT.CENTER);
newColumnTableColumn_1.setWidth(100);
newColumnTableColumn_1.setText("分类名");
...
tableViewer.setContentProvider(new TableViewerContentProvider());
tableViewer.setLabelProvider(new TableViewerLabelProvider());
SearchSQL = new StringBuilder();
SearchSQL.append("select * from SPClass where SPParent=0");

Tabledata = spf.getSPClass(SearchSQL.toString());
tableViewer.setInput(Tabledata);
tv.addPostSelectionChangedListener(listener);
tableViewer.setColumnProperties(new String[] { "SPClass", "SPCode",
"SPIsUsable" });
CellEditor[] cellEditor = new CellEditor[4];
cellEditor[0] = new TextCellEditor(table);
cellEditor[1] = null;
cellEditor[2] = new ComboBoxCellEditor(table, isusables, SWT.NONE);
tableViewer.setCellEditors(cellEditor);
ICellModifier modifier = new SPClassCellModifier(tableViewer);
tableViewer.setCellModifier(modifier);
createActions();
initializeToolBar();
initializeMenu();
createContextMenu(parent);
}
private Object TreeData() {
SearchSQL = new StringBuilder("select * from SPClass");
SPClassList treelist = new SPClassList();
return treelist.getTreeViewList(SearchSQL.toString());
}
// 删除树中节点
private Object DelNode(String val, Vector data) {
for (int i = 0; i < data.size(); i++) {
SPClassEntiry sp = (SPClassEntiry) data.get(i);
if (sp.getClassName().equals(val))
data.remove(i);
if (sp.getChildren().size() > 0)
DelNode(val, sp.getChildren());
}
return data;
}

// 修改树中节点
private Object ModNode(SPClassEntiry val, Vector data) {
for (int i = 0; i < data.size(); i++) {
SPClassEntiry sp = (SPClassEntiry) data.get(i);
if (sp.getID().equals(val.getID())) {
sp.setClassName(val.getClassName());
sp.setClassCode(val.getClassCode());
break;
}
if (sp.getChildren().size() > 0)
ModNode(val, sp.getChildren());
}
return data;
}
// 添加节点
private Object AddNode(SPClassEntiry val, Vector data) {
for (int i = 0; i < data.size(); i++) {
SPClassEntiry sp = (SPClassEntiry) data.get(i);
if (sp.getID().equals(val.getPID())) {
((SPClassEntiry) data.get(i)).getChildren().add(val);
break;
}
if (sp.getChildren().size() > 0)
AddNode(val, sp.getChildren());
}
return data;
}
private void createActions() {
Add = new Action() {
public void run() {
SPClassEntiry spc = null;
IStructuredSelection iis = (IStructuredSelection) tv
.getSelection();
Object obj = iis.getFirstElement();
if (obj instanceof SPClassEntiry) {
spc = (SPClassEntiry) obj;
PID = spc.getID();
PCode = spc.getClassCode();
}
SPClassEntiry temp = new SPClassEntiry();
TableItem[] tableItem = tableViewer.getTable().getItems();
// 分类编号过程
...
temp.setChildren(new Vector());
temp.setClassCode(SubCode);
temp.setClassName("");
temp.setPID(PID);
temp.setIsUsable(1);
((Vector) Tabledata).add(temp);
tableViewer.refresh(Tabledata); // System.out.println(SubCode);
}
};
Add.setText("添加");
Del = new Action() {
public void run() {
IStructuredSelection iis = (IStructuredSelection) tableViewer
.getSelection();
SPClassEntiry spc = (SPClassEntiry) iis.getFirstElement();
Vector tablespclass = (Vector) Tabledata;
tablespclass.remove(spc);
sql1 = "delete from SPClass where SPID=" + spc.getID();
DBConn.ExecuteUpdate(sql1);
tableViewer.refresh(tablespclass);
tv.refresh(DelNode(spc.getClassName(), (Vector) tv.getInput()));
}
}
};
Del.setText("删除");
Save = new Action() {
public void run() {
String sql = "";
String PID = "";
TableItem[] treevalue = tableViewer.getTable().getItems();
for (int i = 0; i < treevalue.length; i++) {
SPClassEntiry temp = (SPClassEntiry) treevalue[i].getData();
String usable = "1";
PID = temp.getPID();
if (temp.getIsUsable().equals("可用")) {
usable = "1";
} else {
usable = "0";
}
sql = "select * from SPClass where SPCode='"
+ temp.getClassCode() + "'";
if (DBConn.RecordIsExist(sql)) {
sql = "update SPClass set SPClass='";
sql += temp.getClassName() + "',SPIsUsable=" + usable;
sql += " where SPCode='" + temp.getClassCode() + "'";
DBConn.ExecuteUpdate(sql);
tv.refresh(ModNode(temp, (Vector) tv.getInput()));
} else {
sql = "insert into SPClass(SPCode,SPClass,SPIsUsable,SPParent) values";
sql += "('" + temp.getClassCode() + "','"
+ temp.getClassName() + "'," + usable + ","
+ PID + ")";
DBConn.ExecuteUpdate(sql);
tv.refresh(AddNode(temp, (Vector) tv.getInput()));
}
}
MessageDialog.openInformation(null, "提示", "保存成功");
}
};
Save.setText("保存");
}
private void initializeToolBar() {
IToolBarManager toolbarManager = getViewSite().getActionBars()
.getToolBarManager();
toolbarManager.add(PubMethod.ToolShowText(Add));
toolbarManager.add(PubMethod.ToolShowText(Del));
toolbarManager.add(PubMethod.ToolShowText(Save));
}
public class TreeLabelProvider implements ILabelProvider {
public String getText(Object element) {
return ((SPClassEntiry) element).getClassName();
}
public Image getImage(Object element) {
return null;
}
public void addListener(ILabelProviderListener listener) {}
public void dispose() { }
public boolean isLabelProperty(Object element, String property) {
return false;
}
public void removeListener(ILabelProviderListener listener) {}
}
public class TableViewerLabelProvider implements ITableLabelProvider {
public String getColumnText(Object element, int col) {
SPClassEntiry o = (SPClassEntiry) element; // 类型转换
if (col == 0)
return o.getClassName();
if (col == 1)
return o.getClassCode();
if (col == 2)
return o.getIsUsable();
return null;
}
public Image getColumnImage(Object element, int col) {
return null;
}
public void dispose() { }
public boolean isLabelProperty(Object element, String property) {
return false;
}
public void addListener(ILabelProviderListener listener) {}

public void removeListener(ILabelProviderListener listener) {}
}
public class SPClassCellModifier implements ICellModifier {
private TableViewer viewer;
public SPClassCellModifier(TableViewer tv) {
this.viewer = tv;
}
public boolean canModify(Object element, String property) {
return true;
}
public Object getValue(Object element, String property) {
SPClassEntiry p = (SPClassEntiry) element;
if (property.equals("SPClass")) {
return p.getClassName();
} else if (property.equals("SPIsUsable")) {
return new Integer(getNameIndex(p.getIsUsable()));
}
throw new RuntimeException("错误列名 : " + property);
}
private int getNameIndex(String name) {
for (int i = 0; i < isusables.length; i++) {
if (isusables[i].equals(name)) {
return i;
}
}
return -1;
}
public void modify(Object element, String property, Object value) {
TableItem item = (TableItem) element;
SPClassEntiry p = (SPClassEntiry) item.getData();
int usable = 0;
if (property.equals("SPIsUsable")) {
Integer comboIndex = (Integer) value;
if (comboIndex.intValue() == -1) {
return;
}
String newName = isusables[comboIndex.intValue()];
if (newName == "可用")
usable = 1;
else
usable = 0;
p.setIsUsable(usable);
} else if (property.equals("SPClass")) {
String newValue = (String) value;
if (newValue.equals("")) {
return;
}
p.setClassName(newValue);
} else {
throw new RuntimeException("错误列名:" + property);
}
viewer.update(p, null);
}
}
public class TreeListener implements ISelectionChangedListener {
public void selectionChanged(final SelectionChangedEvent event) {
setTable();
}
}
public void setTable(){
IStructuredSelection iis = (IStructuredSelection) tv.getSelection();
SPClassEntiry spc = (SPClassEntiry) iis.getFirstElement();
SearchSQL = new StringBuilder();
SearchSQL.append("select * from SPClass where SPParent="
+ spc.getID());
Tabledata = spf.getSPClass(SearchSQL.toString());
tableViewer.setInput(Tabledata);
}
}
请高手帮忙看看是什么问题,该如何解决??
...全文
187 1 点赞 打赏 收藏 举报
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
liush8421 2013-02-02
哦,解决了! 问题在添加后的的temp这个SPClassEntiry缺少ID属性,在添加成功后,在从数据库中将该条记录调出来后再传给AddNode就可以了!
  • 打赏
  • 举报
回复
相关推荐
发帖
Eclipse
加入

5.8w+

社区成员

Java Eclipse
申请成为版主
帖子事件
创建了帖子
2013-02-02 01:17
社区公告
暂无公告