Skip to content

Commit

Permalink
Packages/bundles that are already imported/added are shown
Browse files Browse the repository at this point in the history
  • Loading branch information
lathapatil committed Oct 22, 2024
1 parent b29e97a commit 204c26f
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaCore;
Expand All @@ -41,6 +42,7 @@
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
Expand Down Expand Up @@ -402,10 +404,10 @@ private void handleAdd() {
IPluginModelBase model = (IPluginModelBase) getPage().getModel();
final IProject project = model.getUnderlyingResource().getProject();
if (PluginProject.isJavaProject(project)) {
ILabelProvider labelProvider = new JavaElementLabelProvider();
ILabelProvider labelProvider = new ExportPackgeLabelProvider();
final ConditionalListSelectionDialog dialog = new ConditionalListSelectionDialog(
PDEPlugin.getActiveWorkbenchShell(), labelProvider,
PDEUIMessages.ExportPackageSection_dialogButtonLabel);
PDEUIMessages.ExportPackageSection_dialogButtonLabel,fHeader);
final Collection<String> pckgs = fHeader == null ? Collections.emptySet() : fHeader.getPackageNames();
final boolean allowJava = "true".equals(getBundle().getHeader(ICoreConstants.ECLIPSE_JREBUNDLE)); //$NON-NLS-1$
Runnable runnable = () -> {
Expand Down Expand Up @@ -437,7 +439,9 @@ private void handleAdd() {
if (fHeader != null) {
for (Object selectedObject : selected) {
IPackageFragment candidate = (IPackageFragment) selectedObject;
if (!fHeader.getPackageNames().contains(candidate.getElementName())){
fHeader.addPackage(new ExportPackageObject(fHeader, candidate, getVersionAttribute()));
}
}
} else {
getBundle().setHeader(getExportedPackageHeader(), getValue(selected));
Expand Down Expand Up @@ -620,4 +624,22 @@ private void calculateUses() {
Action action = new CalculateUsesAction(proj, (IBundlePluginModelBase) getPage().getModel());
action.run();
}

class ExportPackgeLabelProvider extends LabelProvider {
JavaElementLabelProvider javaElementLabel = new JavaElementLabelProvider();
@Override
public String getText(Object element) {

if (element instanceof IJavaElement javaElement) {
String text = javaElementLabel.getText(javaElement);
final Collection<String> pckgs = fHeader == null ? Collections.emptySet() : fHeader.getPackageNames();
if (!pckgs.isEmpty() && pckgs.contains(text)) {
return text + " " + PDEUIMessages.PluginModelManager_alreadyExported; //$NON-NLS-1$
}

}
return javaElementLabel.getText(element);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,21 @@ public class ImportPackageSection extends TableSection {

private ImportPackageHeader fHeader;

static class ImportItemWrapper {
public static class ImportItemWrapper {
Object fUnderlying;
private boolean isImported = false;

public boolean isAlreadyImported() {
return isImported;
}

public ImportItemWrapper(Object underlying) {
fUnderlying = underlying;
}

public void setAlreadyImported() {
this.isImported = true;
}
@Override
public String toString() {
return getName();
Expand Down Expand Up @@ -180,7 +188,7 @@ public Object[] getElements(Object parent) {
}
}

static class ImportPackageDialogLabelProvider extends LabelProvider {
class ImportPackageDialogLabelProvider extends LabelProvider {
@Override
public Image getImage(Object element) {
return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_PACKAGE);
Expand All @@ -201,6 +209,10 @@ public String getText(Object element) {
buffer.append(' ');
buffer.append(PDELabelProvider.formatVersion(version.toString()));
}
if (p.isAlreadyImported() && fHeader != null && fHeader.hasPackage(p.getName())) {
buffer.append(' ');
buffer.append(PDEUIMessages.PluginModelManager_alreadyImported);
}
return buffer.toString();
}
}
Expand Down Expand Up @@ -464,7 +476,8 @@ private void handleAdd() {
Set<String> names = new HashSet<>(); // set of String names, do not allow the same package to be added twice
for (int i = 0; i < selected.length; i++) {
ImportPackageObject impObject = null;
if (selected[i] instanceof ImportItemWrapper)
if (selected[i] instanceof ImportItemWrapper
&& !((ImportItemWrapper) selected[i]).isAlreadyImported())
selected[i] = ((ImportItemWrapper) selected[i]).fUnderlying;

if (selected[i] instanceof ExportPackageDescription)
Expand Down Expand Up @@ -530,8 +543,14 @@ private void setElements(ConditionalListSelectionDialog dialog) {
NameVersionDescriptor nameVersion = new NameVersionDescriptor(exportedPackage.getName(), exportedPackage.getVersion().toString(), NameVersionDescriptor.TYPE_PACKAGE);
if (("java".equals(name) || name.startsWith("java.")) && !allowJava) //$NON-NLS-1$ //$NON-NLS-2$
continue;
if (nameVersions.add(nameVersion) && (fHeader == null || !fHeader.hasPackage(name)))
elements.add(new ImportItemWrapper(exportedPackage));
if (nameVersions.add(nameVersion)) {
ImportItemWrapper importItemWrapper = new ImportItemWrapper(exportedPackage);
elements.add(importItemWrapper);
if (fHeader != null && fHeader.hasPackage(name)) {
importItemWrapper.setAlreadyImported();
}
}

}
IPluginModelBase model = (IPluginModelBase) getPage().getPDEEditor().getAggregateModel();
if (model instanceof IBundlePluginModelBase) {
Expand All @@ -544,8 +563,13 @@ private void setElements(ConditionalListSelectionDialog dialog) {
String name = pkg.getName();
String version = pkg.getVersion();
NameVersionDescriptor nameVersion = new NameVersionDescriptor(name, version, NameVersionDescriptor.TYPE_PACKAGE);
if (nameVersions.add(nameVersion) && (fHeader == null || !fHeader.hasPackage(name)))
elements.add(new ImportItemWrapper(pkg));
if (nameVersions.add(nameVersion)) {
ImportItemWrapper importItemWrapper = new ImportItemWrapper(pkg);
elements.add(importItemWrapper);
if (fHeader != null && fHeader.hasPackage(name)) {
importItemWrapper.setAlreadyImported();
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -288,7 +290,7 @@ public boolean doGlobalAction(String actionId) {

@Override
protected boolean canPaste(Object targetObject, Object[] sourceObjects) {
Set<String> existingImportsSet = null;
Map<String, Boolean> existingImportsMap = null;
// Only import objects that are not already existing imports can be
// pasted
for (Object sourceObject : sourceObjects) {
Expand All @@ -298,12 +300,12 @@ protected boolean canPaste(Object targetObject, Object[] sourceObjects) {
}
// Get the current import objects and store them for searching
// purposes
if (existingImportsSet == null) {
existingImportsSet = PluginSelectionDialog.getExistingImports(getModel(), false);
if (existingImportsMap == null) {
existingImportsMap = PluginSelectionDialog.getExistingImports(getModel(), false);
}
// Only import object that do not exist are allowed
ImportObject importObject = (ImportObject) sourceObject;
if (existingImportsSet.contains(importObject.getImport().getId())) {
if (existingImportsMap.keySet().contains(importObject.getImport().getId())) {
return false;
}
}
Expand Down Expand Up @@ -427,21 +429,28 @@ else if (base instanceof PluginBaseNode)

private void handleAdd() {
IPluginModelBase model = (IPluginModelBase) getPage().getModel();
PluginSelectionDialog dialog = new PluginSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), getAvailablePlugins(model), true);
PluginSelectionDialog dialog = new PluginSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), getAvailablePlugins(model), true, model);
dialog.create();
if (dialog.open() == Window.OK) {
Object[] models = dialog.getResult();
IPluginImport[] imports = new IPluginImport[models.length];
List<IPluginImport> imports = new ArrayList<>();
HashMap<String, Boolean> existingImports = PluginSelectionDialog.getExistingImports(model, false);
try {
for (int i = 0; i < models.length; i++) {
if (!existingImports.keySet().contains(((IPluginModel) models[i]).getPluginBase().getId())) {
IPluginModel candidate = (IPluginModel) models[i];
String pluginId = candidate.getPlugin().getId();
IPluginImport importNode = createImport(model.getPluginFactory(), pluginId);
String version = VersionUtil.computeInitialPluginVersion(candidate.getPlugin().getVersion());
importNode.setVersion(version);
imports[i] = importNode;
imports.add(importNode);
}
}
if (imports.size() > 0) {
IPluginImport[] pluginImports = new IPluginImport[imports.size()];
pluginImports = imports.toArray(pluginImports);
addImports(model.getPluginBase(), pluginImports);
}
addImports(model.getPluginBase(), imports);
} catch (CoreException e) {
}
}
Expand Down Expand Up @@ -497,15 +506,14 @@ public void swap(int index1, int index2) {

private IPluginModelBase[] getAvailablePlugins(IPluginModelBase model) {
IPluginModelBase[] plugins = PluginRegistry.getActiveModels(false);
Set<String> existingImports = PluginSelectionDialog.getExistingImports(model, false);
HashMap<String, Boolean> existingImports = PluginSelectionDialog.getExistingImports(model, false);
ArrayList<IPluginModelBase> result = new ArrayList<>();
for (int i = 0; i < plugins.length; i++) {
if (!existingImports.contains(plugins[i].getPluginBase().getId())) {
if (!getModel().getPluginBase().getId().equals(plugins[i].getPluginBase().getId()))
result.add(plugins[i]);
}
}

if (!existingImports.contains("system.bundle")) //$NON-NLS-1$
if (!existingImports.keySet().contains("system.bundle")) //$NON-NLS-1$
addSystemBundle(result);
return result.toArray(new IPluginModelBase[result.size()]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.text.StringMatcher;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.pde.internal.core.text.bundle.ExportPackageHeader;
import org.eclipse.pde.internal.ui.PDEUIMessages;
import org.eclipse.pde.internal.ui.editor.plugin.ImportPackageSection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
Expand All @@ -34,12 +38,19 @@ public class ConditionalListSelectionDialog extends ElementListSelectionDialog {
private final String fButtonText;
private Object[] fElements;
private Object[] fConditionalElements;
private ExportPackageHeader exportPackageHeader;

public ConditionalListSelectionDialog(Shell parent, ILabelProvider renderer, String buttonText) {
super(parent, renderer);
fButtonText = buttonText;
}

public ConditionalListSelectionDialog(Shell activeWorkbenchShell, ILabelProvider labelProvider,
String exportPackageSection_dialogButtonLabel, ExportPackageHeader fHeader) {
this(activeWorkbenchShell, labelProvider, exportPackageSection_dialogButtonLabel);
exportPackageHeader = fHeader;
}

@Override
protected Control createDialogArea(Composite parent) {
Composite comp = (Composite) super.createDialogArea(parent);
Expand All @@ -62,6 +73,7 @@ protected Control createDialogArea(Composite parent) {
else
setListElements(fElements);
}));
button.setSelection(true);
return comp;
}

Expand Down Expand Up @@ -111,4 +123,32 @@ protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
}

@Override
protected void updateButtonsEnableState(IStatus status) {
super.updateButtonsEnableState(status);
Button okButton = getOkButton();
Object[] currentSelection = fFilteredList.getSelection();
if(currentSelection.length>0)
okButton.setEnabled(false);
for (Object selection : currentSelection) {
if (selection instanceof ImportPackageSection.ImportItemWrapper) {
if (!((ImportPackageSection.ImportItemWrapper) selection).isAlreadyImported()) {
okButton.setEnabled(true);
break;
}
}
else if (selection instanceof IPackageFragment) {
if (exportPackageHeader == null || exportPackageHeader.getPackageNames().isEmpty()) {
okButton.setEnabled(true);
break;
}
else if (!exportPackageHeader.getPackageNames()
.contains(((IPackageFragment) selection).getElementName())) {
okButton.setEnabled(true);
break;
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import java.util.Set;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

Expand Down Expand Up @@ -122,7 +122,7 @@ public class PointSelectionPage extends BaseWizardSelectionPage {
private final IPluginModelBase fModel;
private Button fFilterCheck;
private IPluginExtensionPoint fCurrentPoint;
private final Set<String> fAvailableImports;
private final Map<String, Boolean> fAvailableImports;
private final IProject fProject;
private Label fTemplateLabel;
private ExtensionTreeSelectionPage fWizardsPage;
Expand All @@ -147,7 +147,7 @@ public boolean select(Viewer viewer, Object parentElement, Object element) {
if (point instanceof PluginExtensionPointNode)
return true;

return fAvailableImports.contains(point.getPluginBase().getId());
return fAvailableImports.keySet().contains(point.getPluginBase().getId());
}
}

Expand Down Expand Up @@ -511,7 +511,7 @@ public boolean finish() {
}

String pluginID = fCurrentPoint.getPluginBase().getId();
if (!(fCurrentPoint instanceof PluginExtensionPointNode) && !fAvailableImports.contains(pluginID) && !(fCurrentPoint.getPluginBase() instanceof IFragment)) {
if (!(fCurrentPoint instanceof PluginExtensionPointNode) && !fAvailableImports.keySet().contains(pluginID) && !(fCurrentPoint.getPluginBase() instanceof IFragment)) {
if (MessageDialog.openQuestion(getShell(), PDEUIMessages.NewExtensionWizard_PointSelectionPage_dependencyTitle, NLS.bind(PDEUIMessages.NewExtensionWizard_PointSelectionPage_dependencyMessage, new String[] {pluginID, fCurrentPoint.getId()}))) {
IPluginImport importNode = fModel.getPluginFactory().createImport();
importNode.setId(pluginID);
Expand Down

0 comments on commit 204c26f

Please sign in to comment.