Skip to content

Commit

Permalink
FUSETOOLS-1778 - Improve support of Global Elements
Browse files Browse the repository at this point in the history
- Modify interface to have access to CamelFile
- Support selection of Element in the Tree
  • Loading branch information
apupier committed May 4, 2016
1 parent 884c61d commit 512e1f3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.fusesource.ide.camel.model.service.core.internal.CamelModelServiceCoreActivator;
import org.fusesource.ide.camel.model.service.core.io.CamelIOHandler;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
Expand Down Expand Up @@ -129,16 +130,31 @@ public void setGlobalDefinitions(Map<String, Node> globalDefinitions) {
*/
public String addGlobalDefinition(String id, Node def) {
String usedId = id != null ? id : "_def" + UUID.randomUUID().toString();
if (usedId != null && this.globalDefinitions.containsKey(usedId)) return null;
if (id == null && this.globalDefinitions.containsValue(def)) return null;
this.globalDefinitions.put(usedId, def);
if (def.getParentNode() == null || def.getParentNode().isEqualNode(getDocument().getDocumentElement()) == false) {
getDocument().getDocumentElement().insertBefore(def, getDocument().getDocumentElement().getChildNodes().item(0));
if (usedId != null && globalDefinitions.containsKey(usedId) || id == null && globalDefinitions.containsValue(def)) {
return null;
}
globalDefinitions.put(usedId, def);
final Node parentNode = def.getParentNode();
final Element documentElement = getDocument().getDocumentElement();
if (parentNode == null || !parentNode.isEqualNode(documentElement)) {
documentElement.insertBefore(def, documentElement.getChildNodes().item(0));
fireModelChanged();
}
return usedId;
}

public String updateGlobalDefinition(String id, Node def) {
String usedId = id != null ? id : "_def" + UUID.randomUUID().toString();
Node oldDef = globalDefinitions.put(usedId, def);
final Node parentNode = def.getParentNode();
final Element documentElement = getDocument().getDocumentElement();
if (parentNode == null || !parentNode.isEqualNode(documentElement)) {
documentElement.replaceChild(def, oldDef);
fireModelChanged();
}
return usedId;
}

/**
* removes the global definition from context
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.11.1",
org.fusesource.ide.camel.model.service.core,
org.fusesource.ide.camel.model.service.v2_16_1;bundle-version="8.0.0",
org.eclipse.core.databinding.observable;bundle-version="1.5.0"
Export-Package: org.fusesource.ide.camel.model.service.core.tests.integration.core.io
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,13 @@ private void createNewEntry() {
switch (item.getContributor().getGlobalConfigElementType()) {
case GLOBAL_ELEMENT:
String id = ((Element) newXMLNode).getAttribute("id");
cf.addGlobalDefinition(Strings.isBlank(id) ? UUID.randomUUID().toString() : id, newXMLNode);
if (cf.getGlobalDefinitions().containsKey(id)) {
cf.updateGlobalDefinition(Strings.isBlank(id) ? UUID.randomUUID().toString() : id, newXMLNode);
} else {
cf.addGlobalDefinition(Strings.isBlank(id) ? UUID.randomUUID().toString() : id, newXMLNode);
}
reload();
treeViewer.setSelection(new StructuredSelection(newXMLNode), true);
break;
case CONTEXT_DATAFORMAT:
final CamelBasicModelElement newDataFormat = addDataFormat(cf, (Element) newXMLNode);
Expand Down Expand Up @@ -671,7 +677,7 @@ private void modifyEntry() {
Element modElem = o instanceof Element ? (Element)o : o instanceof AbstractCamelModelElement ? (Element)((AbstractCamelModelElement)o).getXmlNode() : null;
ICustomGlobalConfigElementContribution extHandler = getExtensionForElement(modElem);
if (extHandler != null) {
GlobalConfigurationTypeWizard wizard = extHandler.modifyGlobalElement(parentEditor.getDesignEditor().getModel().getDocument());
GlobalConfigurationTypeWizard wizard = extHandler.modifyGlobalElement(parentEditor.getDesignEditor().getModel());
if (wizard == null) {
try {
new ShowPropertiesViewHandler().execute(null);
Expand All @@ -695,7 +701,12 @@ private void modifyEntry() {
case CONTEXT_ENDPOINT: AbstractCamelModelElement cme = (AbstractCamelModelElement)o;
cme.initialize();
break;
case GLOBAL_ELEMENT:
case GLOBAL_ELEMENT:
String id = ((Element) newXMLNode).getAttribute("id");
CamelFile cf = parentEditor.getDesignEditor().getModel();
cf.updateGlobalDefinition(Strings.isBlank(id) ? UUID.randomUUID().toString() : id, newXMLNode);
reload();
treeViewer.setSelection(new StructuredSelection(newXMLNode), true);
default: // nothing to do - handled via node events
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.fusesource.ide.camel.model.service.core.catalog.dataformats.DataFormatModel;
import org.fusesource.ide.camel.model.service.core.model.CamelFile;
import org.fusesource.ide.foundation.core.util.CamelUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

Expand All @@ -45,7 +44,8 @@ public GlobalConfigurationTypeWizard createGlobalElement(CamelFile camelFile) {
* @see org.fusesource.ide.camel.editor.provider.ext.ICustomGlobalConfigElementContribution#modifyGlobalElement(org.w3c.dom.Document)
*/
@Override
public GlobalConfigurationTypeWizard modifyGlobalElement(Document document) {
public GlobalConfigurationTypeWizard modifyGlobalElement(CamelFile camelFile) {
// It is redirected to Properties view
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.fusesource.ide.camel.model.service.core.catalog.components.ComponentModel;
import org.fusesource.ide.camel.model.service.core.model.CamelFile;
import org.fusesource.ide.foundation.core.util.CamelUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
Expand All @@ -51,8 +50,8 @@ public GlobalConfigurationTypeWizard createGlobalElement(CamelFile camelFile) {
* @see org.fusesource.ide.camel.editor.provider.ext.ICustomGlobalConfigElementContribution#modifyGlobalElement(org.w3c.dom.Document)
*/
@Override
public GlobalConfigurationTypeWizard modifyGlobalElement(Document document) {
// TODO Auto-generated method stub
public GlobalConfigurationTypeWizard modifyGlobalElement(CamelFile camelFile) {
// It is redirected to Properties view
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import org.fusesource.ide.camel.model.service.core.catalog.Dependency;
import org.fusesource.ide.camel.model.service.core.model.CamelFile;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
Expand Down Expand Up @@ -54,12 +53,12 @@ public interface ICustomGlobalConfigElementContribution {
* implementors code to change the XML element given as parameter. If no
* wizard provided, the edit action will put focus on Properties view.
*
* @param document
* a reference to the camel context xml document to be used for
* creating dom nodes / elements
* @param camelFile
* a reference to the camel file providing the context on which
* global elements will be created
* @return true if changed by user or false if canceled modifying
*/
GlobalConfigurationTypeWizard modifyGlobalElement(Document document);
GlobalConfigurationTypeWizard modifyGlobalElement(CamelFile camelFile);

/**
* this method is invoked if the user deleted a global element from the
Expand Down

0 comments on commit 512e1f3

Please sign in to comment.