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

问题描述

|| 如何以编程方式写入.cproject文件并进行回读(在Eclipse CDT中)? 实现AbstractCPropertyTab的类具有复选框,其名称和布尔状态应保存到.cproject。     

解决方法

        我解决了自己的问题。也许有人觉得这很有用。 我将介绍两种方法:一种用于在表上保存复选框的选中状态,另一种用于初始化复选框的值。
/**
 * 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);
            }
        }
    }
}