Skip to content

Commit

Permalink
[JBIDE-22592] always fire change for same properties list instance
Browse files Browse the repository at this point in the history
  • Loading branch information
adietish committed Jul 20, 2016
1 parent a677570 commit e8d3ac6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -141,8 +140,11 @@ private void handleSelectedAppSource(PropertyChangeEvent evt) {
if(evt.getNewValue() instanceof IApplicationSource
&& ResourceKind.TEMPLATE.equals(((IApplicationSource) evt.getNewValue()).getKind())){
IApplicationSource source = (IApplicationSource) evt.getNewValue();
this.template = (ITemplate) source.getSource();
updateTemplateParameters(template);
ITemplate newTemplate = (ITemplate) source.getSource();
if (!Objects.equals(newTemplate, this.template)) {
this.template = newTemplate;
updateTemplateParameters(newTemplate);
}
}
}
private void handleEclipseProject(PropertyChangeEvent evt) {
Expand All @@ -165,7 +167,7 @@ private void updateTemplateParameters(ITemplate template) {
if (template == null) {
return;
}
setParameters(new ArrayList<>(template.getParameters().values()));
setParameters(template.getParameters().values());
setItems(template.getItems());
setLabels(template.getObjectLabels());
}
Expand All @@ -181,54 +183,56 @@ public boolean isParameterModified(IParameter param) {
}

@Override
public void setParameters(List<IParameter> parameters) {
public void setParameters(Collection<IParameter> parameters) {
List<IParameter> oldParameters = new ArrayList<>(this.parameters);
if (parameters == null) {
this.parameters.clear();
} else {
this.originalValueMap = toMap(parameters);
List<IParameter> newParameters = new ArrayList<>();
newParameters.addAll(injectProjectParameters(eclipseProject, parameters));
this.parameters.clear();
this.parameters.addAll(newParameters);
}
firePropertyChange(PROPERTY_PARAMETERS, oldParameters, this.parameters);
}

private Map<String, String> toMap(Collection<IParameter> parameters) {
Map<String, String> paramsMap = new HashMap<>();
if (parameters != null) {
parameters.forEach(p -> paramsMap.put(p.getName(), p.getValue()));
}
originalValueMap = paramsMap;
firePropertyChange(PROPERTY_PARAMETERS, this.parameters, this.parameters = injectProjectParameters(this.eclipseProject, parameters));
return paramsMap;
}

private static List<IParameter> injectProjectParameters(org.eclipse.core.resources.IProject project, List<IParameter> originalParameters) {
private Collection<IParameter> injectProjectParameters(org.eclipse.core.resources.IProject project, Collection<IParameter> originalParameters) {
if (originalParameters == null || originalParameters.isEmpty()) {
return originalParameters;
}
Map<String, String> projectParams = getProjectParameters(project);

List<IParameter> newParameters = originalParameters.stream().map(p -> {
IParameter clone = p.clone();
String value = projectParams.get(clone.getName());
originalParameters.forEach(p -> {
String value = projectParams.get(p.getName());
if (value != null) {
clone.setValue(value);
p.setValue(value);
}
return clone;
}).collect(Collectors.toList());
});

return newParameters;
return originalParameters;
}

private static Map<String, String> getProjectParameters(org.eclipse.core.resources.IProject project) {
private Map<String, String> getProjectParameters(org.eclipse.core.resources.IProject project) {
if(project == null) {
return Collections.emptyMap();
}

Map<String,String> projectParams = new HashMap<>();
String gitRepo = null;
try {
gitRepo = StringUtils.defaultString(EGitUtils.getDefaultRemoteRepo(project));
} catch (CoreException e) {
throw new OpenShiftException(e, NLS.bind("Could not determine the default remote Git repository for \"{0}\"", project.getName()));
}
String gitRepo = getGitRepo(project);
if (gitRepo != null) {
projectParams.put(PARAMETER_SOURCE_REPOSITORY_URL, gitRepo);
projectParams.put(PARAMETER_GIT_URI, gitRepo);//legacy key

String branch;
try {
branch = StringUtils.defaultString(EGitUtils.getCurrentBranch(project));
} catch (CoreException e) {
throw new OpenShiftException(e, NLS.bind("Could not determine the default Git branch for \"{0}\"", project.getName()));
}
String branch = getGitBranch(project);
projectParams.put("SOURCE_REPOSITORY_REF", branch);
projectParams.put("GIT_REF", branch);//legacy key

Expand All @@ -242,6 +246,22 @@ private static Map<String, String> getProjectParameters(org.eclipse.core.resourc
return projectParams;
}

private String getGitBranch(org.eclipse.core.resources.IProject project) {
try {
return StringUtils.defaultString(EGitUtils.getCurrentBranch(project));
} catch (CoreException e) {
throw new OpenShiftException(e, NLS.bind("Could not determine the default Git branch for \"{0}\"", project.getName()));
}
}

private String getGitRepo(org.eclipse.core.resources.IProject project) {
try {
return StringUtils.defaultString(EGitUtils.getDefaultRemoteRepo(project));
} catch (CoreException e) {
throw new OpenShiftException(e, NLS.bind("Could not determine the default remote Git repository for \"{0}\"", project.getName()));
}
}

@Override
public IParameter getSelectedParameter() {
return this.selectedParameter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
******************************************************************************/
package org.jboss.tools.openshift.internal.ui.wizard.newapp.fromtemplate;

import java.util.Collection;
import java.util.List;

import com.openshift.restclient.model.template.IParameter;
Expand All @@ -34,7 +35,7 @@ public interface ITemplateParametersPageModel {
* Set the list of template parameters
* @param parameters
*/
void setParameters(List<IParameter> parameters);
void setParameters(Collection<IParameter> parameters);

/**
* Get the selected parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ protected void doCreateControls(Composite container, DataBindingContext dbc) {
this.viewer = createTable(tableContainer, parametersObservable, dbc);
GridDataFactory.fillDefaults()
.span(1, 5).align(SWT.FILL, SWT.FILL).grab(true, true).hint(500, 300).applyTo(tableContainer);
viewer.setInput(parametersObservable);
IObservableValue<IParameter> selectedParameter =
BeanProperties.value(ITemplateParametersPageModel.PROPERTY_SELECTED_PARAMETER).observe(model);
ValueBindingBuilder.bind(ViewerProperties.singleSelection().observe(viewer))
.to(selectedParameter)
.in(dbc);
viewer.setInput(parametersObservable);
viewer.addDoubleClickListener(onDoubleClick());

// edit button
Expand Down

0 comments on commit e8d3ac6

Please sign in to comment.