Skip to content

Commit

Permalink
Merge pull request #5917 from chrisrueger/add-double-click-handler-re…
Browse files Browse the repository at this point in the history
…solutionview

Add double click handler resolutionview
  • Loading branch information
pkriens authored Dec 4, 2023
2 parents 2983be5 + 11a4e40 commit c47e589
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.osgi.resource.Capability;
import org.osgi.resource.Requirement;

import aQute.bnd.header.Attrs;
Expand All @@ -18,6 +19,7 @@

public class CapReqBuilderTest {


@Test
public void testSimple() throws Exception {
CapabilityBuilder cb = new CapabilityBuilder("test");
Expand Down Expand Up @@ -173,4 +175,41 @@ public void testNonAliasedRequirementUnchanged() throws Exception {
assertTrue(original == unaliased, "unaliasing a normal requirement should return the original object");
}

@Test
public void testCapabilityToRequirementWithFilter() throws Exception {
CapReqBuilder cr = new CapReqBuilder("osgi.wiring.package");
Attrs attrs = new Attrs();
attrs.putTyped("bundle-symbolic-name", "org.example");
attrs.putTyped("bundle-version", "1.7.23");
attrs.putTyped("osgi.wiring.package", "org.example.foo");
attrs.putTyped("version", "1.7.23");
attrs.putTyped("bnd.hashes", "123, 456, 789");
cr.addAttributes(attrs);

Capability cap = cr.buildSyntheticCapability();

Requirement req = CapReqBuilder.createRequirementFromCapability(cap)
.buildSyntheticRequirement();

assertEquals("osgi.wiring.package", req.getNamespace());
assertEquals(
"(&(bundle-symbolic-name=org.example)(bundle-version>=1.7.23)(osgi.wiring.package=org.example.foo)(version>=1.7.23)(bnd.hashes=123, 456, 789))",
req.getDirectives()
.get("filter"));

Requirement reqFiltered = CapReqBuilder.createRequirementFromCapability(cap, (name) -> {
if (name.equals("bundle-symbolic-name") || name.equals("bundle-version") || name.equals("bnd.hashes")) {
return false;
}

return true;
})
.buildSyntheticRequirement();

assertEquals("osgi.wiring.package", reqFiltered.getNamespace());
assertEquals("(&(osgi.wiring.package=org.example.foo)(version>=1.7.23))",
reqFiltered.getDirectives()
.get("filter"));
}

}
19 changes: 19 additions & 0 deletions biz.aQute.bndlib/src/aQute/bnd/osgi/resource/CapReqBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;

import org.osgi.framework.Constants;
import org.osgi.framework.Version;
Expand Down Expand Up @@ -709,6 +710,18 @@ static Object toVersions(Object value) {
}

public static RequirementBuilder createRequirementFromCapability(Capability capability) {
return createRequirementFromCapability(capability, null);
}

/**
* @param capability the capability to convert
* @param includeAttributesFilter predicate to control from the caller which
* attributes to include. if <code>null</code> all attributes are
* included.
* @return a RequirementBuilder from the capability
*/
public static RequirementBuilder createRequirementFromCapability(Capability capability,
Predicate<String> includeAttributesFilter) {
final String namespace = capability.getNamespace();
RequirementBuilder builder = new RequirementBuilder(namespace);
final String versionAttrName = Optional.ofNullable(ResourceUtils.getVersionAttributeForNamespace(namespace))
Expand All @@ -720,6 +733,12 @@ public static RequirementBuilder createRequirementFromCapability(Capability capa
.append('&');
}
capAttributes.forEach((name, v) -> {

if (includeAttributesFilter != null && !includeAttributesFilter.test(name)) {
// skip this attribute
return;
}

if (v instanceof Version || name.equals(versionAttrName)
|| (namespace.equals(PackageNamespace.PACKAGE_NAMESPACE)
&& name.equals(AbstractWiringNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE))) {
Expand Down
4 changes: 3 additions & 1 deletion bndtools.core/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ Export-Package: org.osgi.service.metatype.annotations
bndtools.utils;version=project;packages='*',\
slf4j.api,\
org.eclipse.ui.ide.application;version='1.3',\
org.eclipse.ui.console
org.eclipse.ui.console,\
org.eclipse.e4.core.services,\
org.osgi.service.event

-testpath: \
slf4j.api,\
Expand Down
30 changes: 30 additions & 0 deletions bndtools.core/src/bndtools/views/ViewEventTopics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package bndtools.views;

/**
* Topics for the EventBroker which is used for communication between different
* views. For example if View1 sends an event to View2 wants to open a dialog in
* the other view. .
*/
public enum ViewEventTopics {

/**
* Event to open the advances search of the repositories view.
*/
REPOSITORIESVIEW_OPEN_ADVANCED_SEARCH("EVENT/RepositoriesView/openAdvancedSearch");

private String eventtype;

ViewEventTopics(String eventtype) {
this.eventtype = eventtype;
}

public String topic() {
return eventtype;
}

@Override
public String toString() {
return eventtype;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPersistable;
import org.eclipse.ui.XMLMemento;
import org.osgi.resource.Requirement;

public class AdvancedSearchDialog extends TitleAreaDialog implements IPersistable {
Expand Down Expand Up @@ -156,4 +157,19 @@ public void restoreState(IMemento memento) {
}
}

/**
* @param req a requirement
* @return a state object for the given requirement to open advancedSearch
* prefilled in the "Other" tab.
*/
public static IMemento toNamespaceSearchPanelMemento(Requirement req) {
XMLMemento memento = XMLMemento.createWriteRoot("search");
memento.putInteger("tabIndex", 2);
IMemento other = memento.createChild("tab", "Other");
other.putString("namespace", req.getNamespace());
other.putString("filter", req.getDirectives()
.get("filter"));
return memento;
}

}
27 changes: 27 additions & 0 deletions bndtools.core/src/bndtools/views/repository/RepositoriesView.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IAction;
Expand Down Expand Up @@ -89,6 +90,7 @@
import org.eclipse.ui.part.ResourceTransfer;
import org.eclipse.ui.part.ViewPart;
import org.osgi.resource.Requirement;
import org.osgi.service.event.Event;
import org.osgi.service.repository.Repository;

import aQute.bnd.build.Workspace;
Expand Down Expand Up @@ -118,6 +120,7 @@
import bndtools.utils.HierarchicalLabel;
import bndtools.utils.HierarchicalMenu;
import bndtools.utils.SelectionDragAdapter;
import bndtools.views.ViewEventTopics;
import bndtools.wizards.workspace.AddFilesToRepositoryWizard;
import bndtools.wizards.workspace.WorkspaceSetupWizard;

Expand All @@ -143,6 +146,9 @@ public class RepositoriesView extends ViewPart implements RepositoriesViewRefres
private Action downloadAction;
private String advancedSearchState;
private Action offlineAction;
private final IEventBroker eventBroker = PlatformUI.getWorkbench()
.getService(IEventBroker.class);


private final BndPreferences prefs = new BndPreferences();

Expand Down Expand Up @@ -467,6 +473,9 @@ public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
IActionBars actionBars = getViewSite().getActionBars();
actionBars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction);

// Event subscription
eventBroker.subscribe(ViewEventTopics.REPOSITORIESVIEW_OPEN_ADVANCED_SEARCH.topic(),
event -> handleOpenAdvancedSearch(event));
}

private void configureOfflineAction() {
Expand Down Expand Up @@ -1221,4 +1230,22 @@ private HierarchicalLabel<Action> createContextMenueBsn(final RepositoryPlugin r
}));
}

private void handleOpenAdvancedSearch(Event event) {

if (event == null) {
return;
}

// Handle the event, open the dialog
if (event.getProperty(IEventBroker.DATA) instanceof Requirement req) {

// fill and open advanced search
advancedSearchState = AdvancedSearchDialog.toNamespaceSearchPanelMemento(req)
.toString();
advancedSearchAction.setChecked(true);
advancedSearchAction.run();

}
}

}
Loading

0 comments on commit c47e589

Please sign in to comment.