Skip to content

Commit

Permalink
Clean-up code in EESection and RequiredEEHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Jun 30, 2024
1 parent 40fe485 commit a6b6051
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2012 IBM Corporation and others.
* Copyright (c) 2005, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -14,6 +14,7 @@
package org.eclipse.pde.internal.core.text.bundle;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.osgi.util.ManifestElement;
Expand All @@ -32,10 +33,6 @@ protected PDEManifestElement createElement(ManifestElement element) {
return new ExecutionEnvironment(this, element.getValue());
}

public boolean hasExecutionEnvironment(IExecutionEnvironment env) {
return hasElement(env.getId());
}

public void addExecutionEnvironment(IExecutionEnvironment env) {
addManifestElement(new ExecutionEnvironment(this, env.getId()));
}
Expand All @@ -45,13 +42,13 @@ public void addExecutionEnvironment(ExecutionEnvironment environment, int index)
}

public void addExecutionEnvironments(Object[] envs) {
ArrayList<ExecutionEnvironment> list = new ArrayList<>(envs.length);
List<ExecutionEnvironment> list = new ArrayList<>(envs.length);
for (Object envObject : envs) {
ExecutionEnvironment env = null;
if (envObject instanceof ExecutionEnvironment) {
env = (ExecutionEnvironment) envObject;
} else if (envObject instanceof IExecutionEnvironment) {
env = new ExecutionEnvironment(this, ((IExecutionEnvironment) envObject).getId());
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);
Expand All @@ -63,10 +60,6 @@ public void addExecutionEnvironments(Object[] envs) {
}
}

public void addExecutionEnvironments(ExecutionEnvironment[] envs) {
addManifestElements(envs);
}

public ExecutionEnvironment removeExecutionEnvironment(ExecutionEnvironment env) {
return (ExecutionEnvironment) removeManifestElement(env);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2017 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -15,8 +15,9 @@
*******************************************************************************/
package org.eclipse.pde.internal.ui.editor.plugin;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IncrementalProjectBuilder;
Expand All @@ -29,6 +30,7 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.Separator;
Expand Down Expand Up @@ -66,11 +68,11 @@
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.events.IHyperlinkListener;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;
Expand All @@ -84,7 +86,7 @@ public class ExecutionEnvironmentSection extends TableSection {
private Action fRemoveAction;
private Action fAddAction;

class EELabelProvider extends LabelProvider {
private static class EELabelProvider extends LabelProvider {

private final Image fImage;

Expand Down Expand Up @@ -112,20 +114,6 @@ public void dispose() {
}
}

static class ContentProvider implements IStructuredContentProvider {
@Override
public Object[] getElements(Object inputElement) {
if (inputElement instanceof IBundleModel model) {
IBundle bundle = model.getBundle();
IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
if (header instanceof RequiredExecutionEnvironmentHeader) {
return ((RequiredExecutionEnvironmentHeader) header).getEnvironments();
}
}
return new Object[0];
}
}

public ExecutionEnvironmentSection(PDEFormPage page, Composite parent) {
super(page, parent, Section.DESCRIPTION, new String[] {PDEUIMessages.RequiredExecutionEnvironmentSection_add, PDEUIMessages.RequiredExecutionEnvironmentSection_remove, PDEUIMessages.RequiredExecutionEnvironmentSection_up, PDEUIMessages.RequiredExecutionEnvironmentSection_down});
createClient(getSection(), page.getEditor().getToolkit());
Expand All @@ -150,24 +138,24 @@ protected void createClient(Section section, FormToolkit toolkit) {

createViewerPartControl(container, SWT.FULL_SELECTION | SWT.MULTI, 2, toolkit);
fEETable = tablePart.getTableViewer();
fEETable.setContentProvider(new ContentProvider());
fEETable.setContentProvider((IStructuredContentProvider) inputElement -> {
if (inputElement instanceof IBundleModel model) {
IBundle bundle = model.getBundle();
@SuppressWarnings("deprecation")
IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
if (header instanceof RequiredExecutionEnvironmentHeader breeHeader) {
return breeHeader.getEnvironments();
}
}
return new Object[0];
});
fEETable.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());

Hyperlink link = toolkit.createHyperlink(container, PDEUIMessages.BuildExecutionEnvironmentSection_configure, SWT.NONE);
link.addHyperlinkListener(new IHyperlinkListener() {
@Override
public void linkEntered(HyperlinkEvent e) {
}

@Override
public void linkExited(HyperlinkEvent e) {
}

@Override
public void linkActivated(HyperlinkEvent e) {
SWTFactory.showPreferencePage(PDEPlugin.getActiveWorkbenchShell(), "org.eclipse.jdt.debug.ui.jreProfiles", null); //$NON-NLS-1$
}
});
link.addHyperlinkListener(IHyperlinkListener.linkActivatedAdapter(e -> {
Shell shell = PDEPlugin.getActiveWorkbenchShell();
SWTFactory.showPreferencePage(shell, "org.eclipse.jdt.debug.ui.jreProfiles", null); //$NON-NLS-1$
}));
GridData gd = new GridData();
gd.horizontalSpan = 2;
link.setLayoutData(gd);
Expand All @@ -176,30 +164,19 @@ public void linkActivated(HyperlinkEvent e) {
try {
if (project != null && project.hasNature(JavaCore.NATURE_ID)) {
link = toolkit.createHyperlink(container, PDEUIMessages.ExecutionEnvironmentSection_updateClasspath, SWT.NONE);
link.addHyperlinkListener(new IHyperlinkListener() {
@Override
public void linkEntered(HyperlinkEvent e) {
}

@Override
public void linkExited(HyperlinkEvent e) {
}

@Override
public void linkActivated(HyperlinkEvent e) {
try {
getPage().getEditor().doSave(null);
IPluginModelBase model = PluginRegistry.findModel(project);
if (model != null) {
ClasspathComputer.setClasspath(project, model);
if (PDEPlugin.getWorkspace().isAutoBuilding()) {
doFullBuild(project);
}
link.addHyperlinkListener(IHyperlinkListener.linkActivatedAdapter(e -> {
try {
getPage().getEditor().doSave(null);
IPluginModelBase model = PluginRegistry.findModel(project);
if (model != null) {
ClasspathComputer.setClasspath(project, model);
if (PDEPlugin.getWorkspace().isAutoBuilding()) {
doFullBuild(project);
}
} catch (CoreException e1) {
}
} catch (CoreException e1) {
}
});
}));
gd = new GridData();
gd.horizontalSpan = 2;
link.setLayoutData(gd);
Expand Down Expand Up @@ -300,9 +277,7 @@ public void swap(int index1, int index2) {
private void handleRemove() {
IStructuredSelection ssel = fEETable.getStructuredSelection();
if (!ssel.isEmpty()) {
Iterator<?> iter = ssel.iterator();
while (iter.hasNext()) {
Object object = iter.next();
for (Object object : ssel) {
if (object instanceof ExecutionEnvironment) {
getHeader().removeExecutionEnvironment((ExecutionEnvironment) object);
}
Expand All @@ -324,24 +299,21 @@ private void handleAdd() {
}
}

@SuppressWarnings("deprecation")
private void addExecutionEnvironments(Object[] result) {
IManifestHeader header = getHeader();
if (header == null) {
StringBuilder buffer = new StringBuilder();
StringJoiner buffer = new StringJoiner("," + getLineDelimiter() + " "); //$NON-NLS-1$//$NON-NLS-2$
for (Object resultObject : result) {
String id = null;
if (resultObject instanceof IExecutionEnvironment)
id = ((IExecutionEnvironment) resultObject).getId();
else if (resultObject instanceof ExecutionEnvironment)
id = ((ExecutionEnvironment) resultObject).getName();
else
String id;
if (resultObject instanceof IExecutionEnvironment ee) {
id = ee.getId();
} else if (resultObject instanceof ExecutionEnvironment ee) {
id = ee.getName();
} else {
continue;
if (buffer.length() > 0) {
buffer.append(","); //$NON-NLS-1$
buffer.append(getLineDelimiter());
buffer.append(" "); //$NON-NLS-1$
}
buffer.append(id);
buffer.add(id);
}
getBundle().setHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, buffer.toString());
} else {
Expand All @@ -358,17 +330,15 @@ private String getLineDelimiter() {
return TextUtil.getDefaultLineDelimiter();
}

private Object[] getEnvironments() {
private IExecutionEnvironment[] getEnvironments() {
RequiredExecutionEnvironmentHeader header = getHeader();
IExecutionEnvironment[] envs = JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments();
if (header == null)
IExecutionEnvironmentsManager eeManager = JavaRuntime.getExecutionEnvironmentsManager();
IExecutionEnvironment[] envs = eeManager.getExecutionEnvironments();
if (header == null) {
return envs;
ArrayList<Object> list = new ArrayList<>();
for (int i = 0; i < envs.length; i++) {
if (!header.hasExecutionEnvironment(envs[i]))
list.add(envs[i]);
}
return list.toArray();
List<IExecutionEnvironment> ees = header.getElementNames().stream().map(eeManager::getEnvironment).toList();
return Arrays.stream(envs).filter(ee -> !ees.contains(ee)).toArray(IExecutionEnvironment[]::new);
}

@Override
Expand Down Expand Up @@ -425,12 +395,12 @@ private IBundleModel getBundleModel() {

protected RequiredExecutionEnvironmentHeader getHeader() {
IBundle bundle = getBundle();
if (bundle == null)
if (bundle == null) {
return null;
}
@SuppressWarnings("deprecation")
IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
if (header instanceof RequiredExecutionEnvironmentHeader)
return (RequiredExecutionEnvironmentHeader) header;
return null;
return header instanceof RequiredExecutionEnvironmentHeader breeHeader ? breeHeader : null;
}

protected boolean isFragment() {
Expand Down Expand Up @@ -471,10 +441,11 @@ public boolean doGlobalAction(String actionId) {
protected boolean canPaste(Object target, Object[] objects) {
RequiredExecutionEnvironmentHeader header = getHeader();
for (Object object : objects) {
if (object instanceof ExecutionEnvironment) {
String env = ((ExecutionEnvironment) object).getName();
if (header == null || !header.hasElement(env))
if (object instanceof ExecutionEnvironment executionEnvironment) {
String env = executionEnvironment.getName();
if (header == null || !header.hasElement(env)) {
return true;
}
}
}
return false;
Expand Down Expand Up @@ -576,12 +547,7 @@ public boolean canDropMove(Object targetObject, Object[] sourceObjects, int targ

private boolean validateDropMoveModel(ExecutionEnvironment sourceEEObject, ExecutionEnvironment targetEEObject) {
// Objects have to be from the same model
IBundleModel sourceModel = sourceEEObject.getModel();
IBundleModel targetModel = targetEEObject.getModel();
if (sourceModel.equals(targetModel)) {
return true;
}
return false;
return sourceEEObject.getModel().equals(targetEEObject.getModel());
}

private boolean validateDropMoveSanity(Object targetObject, Object[] sourceObjects) {
Expand All @@ -590,10 +556,7 @@ private boolean validateDropMoveSanity(Object targetObject, Object[] sourceObjec
return false;
}
// Validate source objects
if (validateDragMoveSanity(sourceObjects) == false) {
return false;
}
return true;
return validateDragMoveSanity(sourceObjects);
}

@Override
Expand Down
Loading

0 comments on commit a6b6051

Please sign in to comment.