Eclipse CDT: How to write to .cproject file and read back

烂漫一生 提交于 2020-01-15 11:09:07

问题


How to write programmatically to .cproject file and read back (in Eclipse CDT)?

A class implementing AbstractCPropertyTab has checkboxes and name and boolean state of these should be saved to .cproject.


回答1:


I solved my own question. Maybe someone finds this useful.

I will introduce two methods: one for saving checked state of the checkboxes on the table and one to initialize the checkbox values.

/**
 * 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);
            }
        }
    }
}


来源:https://stackoverflow.com/questions/6186185/eclipse-cdt-how-to-write-to-cproject-file-and-read-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!