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
Fixes #146
Files not present in branch but changes are visible in github commit
  • Loading branch information
lathapatil committed Oct 22, 2024
1 parent b29e97a commit 7b670f6
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*******************************************************************************/
package org.eclipse.pde.internal.ui;

import java.util.HashMap;
import java.util.Locale;

import org.eclipse.core.resources.IProject;
Expand Down Expand Up @@ -86,6 +87,7 @@
import org.eclipse.pde.internal.core.text.bundle.ImportPackageObject;
import org.eclipse.pde.internal.core.text.bundle.PackageObject;
import org.eclipse.pde.internal.core.util.VersionUtil;
import org.eclipse.pde.internal.ui.dialogs.PluginSelectionDialog;
import org.eclipse.pde.internal.ui.elements.NamedElement;
import org.eclipse.pde.internal.ui.util.SWTUtil;
import org.eclipse.pde.internal.ui.util.SharedLabelProvider;
Expand All @@ -96,6 +98,11 @@

public class PDELabelProvider extends SharedLabelProvider {
private static final String SYSTEM_BUNDLE = "system.bundle"; //$NON-NLS-1$
private IPluginModelBase currentModel;

public IPluginModelBase getCurrentPluginModel() {
return currentModel;
}

public PDELabelProvider() {
}
Expand Down Expand Up @@ -203,6 +210,13 @@ public String getObjectText(IPluginBase pluginBase) {
}
if (pluginBase.getModel() != null && !pluginBase.getModel().isInSync())
text += " " + PDEUIMessages.PluginModelManager_outOfSync; //$NON-NLS-1$

HashMap<String, Boolean> existingImports = PluginSelectionDialog.getExistingImports(currentModel, false);
if (existingImports.get(pluginBase.getId()) != null && existingImports.get(pluginBase.getId())) {
text += " " + PDEUIMessages.PluginModelManager_alreadyAddedViaReexport; //$NON-NLS-1$
} else if (existingImports.get(pluginBase.getId()) != null && !existingImports.get(pluginBase.getId())) {
text += " " + PDEUIMessages.PluginModelManager_alreadyAdded; //$NON-NLS-1$
}
return text;
}

Expand Down Expand Up @@ -952,4 +966,7 @@ public static String formatVersion(String versionRange) {
return versionRange;
}

public void setCurrentModel(IPluginModelBase currentModel) {
this.currentModel = currentModel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
public class PDEUIMessages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.pde.internal.ui.pderesources";//$NON-NLS-1$

public static String PluginModelManager_alreadyImported;

public static String PluginModelManager_alreadyAdded;

public static String PluginModelManager_alreadyAddedViaReexport;

public static String PluginModelManager_alreadyExported;

public static String AbstractLauncherToolbar_noProblems;

public static String AbstractLauncherToolbar_noSelection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.osgi.service.resolver.ExportPackageDescription;
import org.eclipse.pde.core.plugin.IFragment;
import org.eclipse.pde.core.plugin.IFragmentModel;
Expand All @@ -42,6 +42,7 @@
import org.eclipse.pde.internal.ui.IHelpContextIds;
import org.eclipse.pde.internal.ui.PDEPlugin;
import org.eclipse.pde.internal.ui.PDEUIMessages;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
Expand Down Expand Up @@ -137,6 +138,11 @@ public PluginSelectionDialog(Shell parentShell, IPluginModelBase[] models, boole
setListLabelProvider(PDEPlugin.getDefault().getLabelProvider());
}

public PluginSelectionDialog(Shell activeWorkbenchShell, IPluginModelBase[] availablePlugins,
boolean multipleSelection, IPluginModelBase model) {
this(activeWorkbenchShell, availablePlugins, multipleSelection);
PDEPlugin.getDefault().getLabelProvider().setCurrentModel(model);
}
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
Expand All @@ -153,8 +159,8 @@ private static IPluginModelBase[] getElements(boolean includeFragments) {
return PluginRegistry.getActiveModels(includeFragments);
}

public static HashSet<String> getExistingImports(IPluginModelBase model, boolean includeImportPkg) {
HashSet<String> existingImports = new HashSet<>();
public static HashMap<String, Boolean> getExistingImports(IPluginModelBase model, boolean includeImportPkg) {
HashMap<String, Boolean> existingImports = new HashMap<>();
addSelfAndDirectImports(existingImports, model);
if (model instanceof IFragmentModel) {
IFragment fragment = ((IFragmentModel) model).getFragment();
Expand All @@ -169,33 +175,34 @@ public static HashSet<String> getExistingImports(IPluginModelBase model, boolean
return existingImports;
}

private static void addSelfAndDirectImports(HashSet<String> set, IPluginModelBase model) {
private static void addSelfAndDirectImports(HashMap<String, Boolean> existingImports, IPluginModelBase model) {
if (model == null) {
return;
}
set.add(model.getPluginBase().getId());
existingImports.put(model.getPluginBase().getId(), false);
IPluginImport[] imports = model.getPluginBase().getImports();
for (IPluginImport pImport : imports) {
String id = pImport.getId();
if (set.add(id)) {
addReexportedImport(set, id);
}
existingImports.put(id, false);
addReexportedImport(existingImports, id);

}
}

private static void addReexportedImport(HashSet<String> set, String id) {
private static void addReexportedImport(HashMap<String, Boolean> existingImports, String id) {
IPluginModelBase model = PluginRegistry.findModel(id);
if (model != null) {
IPluginImport[] imports = model.getPluginBase().getImports();
for (IPluginImport pImport : imports) {
if (pImport.isReexported() && set.add(pImport.getId())) {
addReexportedImport(set, pImport.getId());
if (pImport.isReexported()) {
existingImports.put(pImport.getId(), true);
addReexportedImport(existingImports, pImport.getId());
}
}
}
}

private static void addImportedPackages(IBundlePluginModelBase base, HashSet<String> existingImports) {
private static void addImportedPackages(IBundlePluginModelBase base, HashMap<String, Boolean> existingImports) {
HashMap<String, ImportPackageObject> map = getImportPackages(base);
if (map == null) {
return;
Expand All @@ -221,7 +228,7 @@ private static void addImportedPackages(IBundlePluginModelBase base, HashSet<Str
continue;
}
}
existingImports.add(exported[i].getSupplier().getSymbolicName());
existingImports.put(exported[i].getSupplier().getSymbolicName(), false);
}
}
}
Expand Down Expand Up @@ -298,4 +305,22 @@ 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();
StructuredSelection currentSelection = super.getSelectedItems();
HashMap<String, Boolean> existingImports = PluginSelectionDialog
.getExistingImports(PDEPlugin.getDefault().getLabelProvider().getCurrentPluginModel(), false);
if (!currentSelection.isEmpty())
okButton.setEnabled(false);
for (Object selection : currentSelection) {
if (selection instanceof IPluginModelBase
&& !(existingImports.keySet().contains(((IPluginModelBase) selection).getPluginBase().getId()))) {
okButton.setEnabled(true);
break;
}

}
}
}
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
Loading

0 comments on commit 7b670f6

Please sign in to comment.