try {
IFile file = ClickUtils.getClickConfigFile(project);
IModelManager manager = StructuredModelManager.getModelManager();
model = manager.getModelForEdit(file);
IDOMDocument doc = ((IDOMModel)model).getDocument();
Element root = doc.getDocumentElement();
Element pages = null;
NodeList list = doc.getElementsByTagName(ClickPlugin.TAG_PAGES);
if(list.getLength()==0){
pages = doc.createElement(ClickPlugin.TAG_PAGES);
boolean inserted = false;
NodeList children = root.getChildNodes();
for(int i=0;i<children.getLength();i++){
Node node = children.item(i);
if(node instanceof Element){
root.insertBefore(pages, node);
inserted = true;
break;
}
}
if(!inserted){
root.appendChild(pages);
}
} else {
pages = (Element)list.item(0);
}
NodeList children = pages.getChildNodes();
boolean found = false;
String newClazz = className;
String pagesPackage = pages.getAttribute(ClickPlugin.ATTR_PACKAGE);
if(packageName.length()!=0){
if(pagesPackage==null || !packageName.equals(pagesPackage)){
newClazz = packageName + "." + className;
}
}
for(int i=0;i<children.getLength();i++){
Node node = children.item(i);
if(node instanceof Element && ((Element)node).getNodeName().equals(ClickPlugin.TAG_PAGE)){
Element page = (Element)node;
String path = page.getAttribute(ClickPlugin.ATTR_PATH);
String clazz = page.getAttribute(ClickPlugin.ATTR_CLASSNAME);
if(newPath.equals(path) && newClazz.equals(clazz)){
found = true;
break;
}
}
}
if(!found){
Element page = doc.createElement(ClickPlugin.TAG_PAGE);
page.setAttribute(ClickPlugin.ATTR_PATH, newPath);
page.setAttribute(ClickPlugin.ATTR_CLASSNAME, newClazz);
pages.appendChild(page);
}
model.save();