Eclipse CDT:如何写入.cproject文件并回读

时间:2011-05-31 10:26:04

标签: java eclipse plugins eclipse-cdt

如何以编程方式编写.cproject文件并回读(在Eclipse CDT中)?

实现AbstractCPropertyTab的类具有复选框,其名称和布尔状态应保存到.cproject。

1 个答案:

答案 0 :(得分:2)

我解决了自己的问题。也许有人觉得这很有用。

我将介绍两种方法:一种用于保存表格上复选框的已检查状态,另一种用于初始化复选框值。

/**
 * Saves checked state of the packages.
 */
private void saveChecked() { 
    ICConfigurationDescription desc = getResDesc().getConfiguration();
    ICStorageElement strgElem = null;
    try {
        strgElem = desc.getStorage(PACKAGES, true);
    } catch (CoreException e) {
        e.printStackTrace();
    }

    TableItem[] items = pkgCfgViewer.getTable().getItems();
    for(TableItem item : items) {
        if(item != null) {
            String chkd;
            if(item.getChecked()) {
                chkd = "true";
            } else {
                chkd = "false";
            }
            try {  
                String pkgName = item.getText();
                strgElem.setAttribute(pkgName, chkd);
            } catch (Exception e) {
                /*
                 * INVALID_CHARACTER_ERR: An invalid or
                 * illegal XML character is specified. 
                 */
            }
        }
    }
}

/**
 * Initializes the check state of the packages from the storage.
 */
private void initializePackageStates() {
    ICConfigurationDescription desc = getResDesc().getConfiguration();
    ICStorageElement strgElem = null;
    try {
        strgElem = desc.getStorage(PACKAGES, true);
    } catch (CoreException e) {
        e.printStackTrace();
    }
    TableItem[] items = pkgCfgViewer.getTable().getItems();
    for(TableItem item : items) {
        String value = strgElem.getAttribute(item.getText());
        if(value!=null) {
            if(value.equals("true")) {
                item.setChecked(true);
            }
        }
    }
}