Skip to content

Commit

Permalink
Further clean-up BREE and composite header handling in Manifest Editor
Browse files Browse the repository at this point in the history
Also remove CompositeManifestHeader.removeManifestElement(), which is
unused since
eclipse-pde#1321
  • Loading branch information
HannesWell committed Jul 6, 2024
1 parent de33737 commit d817686
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
package org.eclipse.pde.internal.core.text.bundle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;

import org.eclipse.osgi.util.ManifestElement;
import org.eclipse.pde.core.IModelChangedEvent;
Expand All @@ -32,7 +34,7 @@ public class CompositeManifestHeader extends ManifestHeader {

private final boolean fSort;

protected ArrayList<Object> fManifestElements;
protected List<PDEManifestElement> fManifestElements;

protected Map<String, PDEManifestElement> fElementMap;

Expand Down Expand Up @@ -105,12 +107,12 @@ protected void addManifestElement(PDEManifestElement element) {
addManifestElement(element, true);
}

protected void addManifestElements(PDEManifestElement[] elements) {
protected void addManifestElements(List<? extends PDEManifestElement> elements) {
for (PDEManifestElement element : elements) {
addManifestElement(element, false);
}
update(false);
fireStructureChanged(elements, IModelChangedEvent.INSERT);
fireStructureChanged(elements.toArray(PDEManifestElement[]::new), IModelChangedEvent.INSERT);
}

protected void addManifestElement(PDEManifestElement element, boolean update) {
Expand Down Expand Up @@ -145,7 +147,7 @@ protected Object removeManifestElement(String name) {
}
} else if (fManifestElements != null) {
for (int i = 0; i < fManifestElements.size(); i++) {
PDEManifestElement element = (PDEManifestElement) fManifestElements.get(i);
PDEManifestElement element = fManifestElements.get(i);
if (name.equals(element.getValue())) {
object = fManifestElements.remove(i);
}
Expand Down Expand Up @@ -183,8 +185,7 @@ public boolean hasElement(String name) {
}

if (fManifestElements != null) {
for (int i = 0; i < fManifestElements.size(); i++) {
PDEManifestElement element = (PDEManifestElement) fManifestElements.get(i);
for (PDEManifestElement element : fManifestElements) {
if (name.equals(element.getValue())) {
return true;
}
Expand All @@ -193,13 +194,9 @@ public boolean hasElement(String name) {
return false;
}

public Vector<String> getElementNames() {
public List<String> getElementNames() {
PDEManifestElement[] elements = getElements();
Vector<String> vector = new Vector<>(elements.length);
for (PDEManifestElement element : elements) {
vector.add(element.getValue());
}
return vector;
return Arrays.stream(elements).map(PDEManifestElement::getValue).toList();
}

public void swap(int index1, int index2) {
Expand All @@ -208,17 +205,14 @@ public void swap(int index1, int index2) {
}
int size = fManifestElements.size();
if (index1 >= 0 && index2 >= 0 && size > Math.max(index1, index2)) {
Object object1 = fManifestElements.get(index1);
Object object2 = fManifestElements.get(index2);
fManifestElements.set(index1, object2);
fManifestElements.set(index2, object1);
Collections.swap(fManifestElements, index1, index2);
update(true);
}
}

protected PDEManifestElement getElementAt(int index) {
if (fManifestElements != null && fManifestElements.size() > index) {
return (PDEManifestElement) fManifestElements.get(index);
return fManifestElements.get(index);
}
return null;
}
Expand All @@ -228,7 +222,7 @@ protected PDEManifestElement getElementAt(int index) {
*/
public PDEManifestElement getPreviousElement(PDEManifestElement targetElement) {
// Ensure we have elements
if (fSort == true) {
if (fSort) {
return null;
} else if (fManifestElements == null) {
return null;
Expand All @@ -247,17 +241,15 @@ public PDEManifestElement getPreviousElement(PDEManifestElement targetElement) {
}
// 1 <= index < size()
// Get the previous element
PDEManifestElement previousElement = (PDEManifestElement) fManifestElements.get(targetIndex - 1);

return previousElement;
return fManifestElements.get(targetIndex - 1);
}

/**
* Method not applicable for headers that are sorted
*/
public PDEManifestElement getNextElement(PDEManifestElement targetElement) {
// Ensure we have elements
if (fSort == true) {
if (fSort) {
return null;
} else if (fManifestElements == null) {
return null;
Expand All @@ -278,9 +270,7 @@ public PDEManifestElement getNextElement(PDEManifestElement targetElement) {
}
// 0 <= index < last element < size()
// Get the next element
PDEManifestElement nextElement = (PDEManifestElement) fManifestElements.get(targetIndex + 1);

return nextElement;
return fManifestElements.get(targetIndex + 1);
}

/**
Expand All @@ -293,7 +283,7 @@ protected void addManifestElement(PDEManifestElement element, int index, boolean
elementCount = fManifestElements.size();
}
// 0 <= index <= size()
if (fSort == true) {
if (fSort) {
return;
} else if (index < 0) {
return;
Expand Down Expand Up @@ -334,29 +324,4 @@ public int indexOf(PDEManifestElement targetElement) {
return fManifestElements.indexOf(targetElement);
}

/**
* Method not applicable for headers that are sorted
*/
protected PDEManifestElement removeManifestElement(PDEManifestElement element, boolean update) {
if (fSort) {
return null;
} else if (fManifestElements == null) {
return null;
} else if (fManifestElements.isEmpty()) {
return null;
}
// Remove the element
boolean removed = fManifestElements.remove(element);
PDEManifestElement removedElement = null;
if (removed) {
removedElement = element;
}
// Fire event
if (update) {
update(false);
fireStructureChanged(removedElement, IModelChangedEvent.REMOVE);
}
return removedElement;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Arrays;
import java.util.List;

import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.osgi.util.ManifestElement;
import org.eclipse.pde.internal.core.ibundle.IBundle;

Expand All @@ -42,36 +41,22 @@ public void addExecutionEnvironment(ExecutionEnvironment environment, int index)
addManifestElement(environment, index, true);
}

public void addExecutionEnvironments(Object[] envs) {
List<ExecutionEnvironment> list = new ArrayList<>(envs.length);
for (Object envObject : envs) {
ExecutionEnvironment env = null;
if (envObject instanceof ExecutionEnvironment ee) {
env = ee;
} else if (envObject instanceof IExecutionEnvironment ee) {
env = new ExecutionEnvironment(this, ee.getId());
}
if (env != null && !hasElement(env.getName())) {
list.add(env);
public void addExecutionEnvironments(List<String> eeIDs) {
List<ExecutionEnvironment> list = new ArrayList<>(eeIDs.size());
for (String eeID : eeIDs) {
if (!hasElement(eeID)) {
list.add(new ExecutionEnvironment(this, eeID));
}
}

if (!list.isEmpty()) {
addManifestElements(list.toArray(new ExecutionEnvironment[list.size()]));
addManifestElements(list);
}
}

public ExecutionEnvironment removeExecutionEnvironment(String eeId) {
return (ExecutionEnvironment) removeManifestElement(eeId);
}

/**
* Remove operation performed using the actual object rather than its value
*/
public ExecutionEnvironment removeExecutionEnvironmentUnique(ExecutionEnvironment environment) {
return (ExecutionEnvironment) removeManifestElement(environment, true);
}

public List<String> getEnvironments() {
PDEManifestElement[] elements = getElements();
return Arrays.stream(elements).map(PDEManifestElement::getValue).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.Objects;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IncrementalProjectBuilder;
Expand Down Expand Up @@ -298,24 +298,22 @@ private void handleAdd() {

@SuppressWarnings("deprecation")
private void addExecutionEnvironments(Object[] result) {
List<String> ees = Arrays.stream(result).map(resultObject -> {
if (resultObject instanceof IExecutionEnvironment ee) {
return ee.getId();
} else if (resultObject instanceof ExecutionEnvironment ee) {
return ee.getName();
}
return null;
}).filter(Objects::nonNull).toList();

IManifestHeader header = getHeader();
if (header == null) {
StringJoiner buffer = new StringJoiner("," + getLineDelimiter() + " "); //$NON-NLS-1$//$NON-NLS-2$
for (Object resultObject : result) {
String id;
if (resultObject instanceof IExecutionEnvironment ee) {
id = ee.getId();
} else if (resultObject instanceof ExecutionEnvironment ee) {
id = ee.getName();
} else {
continue;
}
buffer.add(id);
}
getBundle().setHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, buffer.toString());
String eeList = String.join("," + getLineDelimiter() + " ", ees); //$NON-NLS-1$//$NON-NLS-2$
getBundle().setHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, eeList);
} else {
RequiredExecutionEnvironmentHeader ee = (RequiredExecutionEnvironmentHeader) header;
ee.addExecutionEnvironments(result);
ee.addExecutionEnvironments(ees);
}
}

Expand Down Expand Up @@ -451,8 +449,9 @@ protected boolean canPaste(Object target, Object[] objects) {
@Override
protected void selectionChanged(IStructuredSelection selection) {
getPage().getPDEEditor().setSelection(selection);
if (getPage().getModel().isEditable())
if (getPage().getModel().isEditable()) {
updateButtons();
}
}

@Override
Expand Down

0 comments on commit d817686

Please sign in to comment.