Skip to content

Commit

Permalink
Closes #348
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhorridge committed Mar 5, 2016
1 parent 7fe8877 commit d9a985b
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ public JMenuBar buildMenu() {
return menuBar;
}

public JPopupMenu buildPopupMenu(final PopupMenuId menuId) {
JPopupMenu popupMenu = new JPopupMenu();
parentChildMap.clear();
Map<String, MenuActionPlugin> idPluginMap = getPlugins();
for (MenuActionPlugin plugin : idPluginMap.values()) {
MenuActionPlugin parent = idPluginMap.get(plugin.getParentId());
getChildren(parent).add(plugin);
}
// Should now have a hierarchy of plugins
Collection<MenuActionPlugin> popupPlugins = getSortedList(getChildren(null));
String lastGroup = "";
for(MenuActionPlugin plugin : popupPlugins) {
if(PopupMenuId.isPopupMenuId(plugin.getParentId())) {
PopupMenuId popupMenuId = new PopupMenuId(plugin.getParentId());
if (popupMenuId.equals(menuId)) {
if (!lastGroup.isEmpty() && !lastGroup.equals(plugin.getGroup())) {
popupMenu.add(new JSeparator());
}
lastGroup = plugin.getGroup();
addMenu(plugin, popupMenu);
}
}
}
return popupMenu;
}

private void addMenu(MenuActionPlugin plugin, JComponent menuContainer) {
List<MenuActionPlugin> children = getSortedList(getChildren(plugin));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.protege.editor.core.ui.menu;

import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
* Matthew Horridge
* Stanford Center for Biomedical Informatics Research
* 05/03/16
*/
public class PopupMenuId {

private final String id;

public PopupMenuId(String id) {
this.id = checkNotNull(id);
checkArgument(id.startsWith("["), "PopupMenuIds must start with '['");
checkArgument(id.endsWith("]"), "PopupMenuIds must end with ']'");
}

/**
* Tests whether an id is a popup menu Id. Popup menu ids start with '[' and end with ']'
* @param id The id to test. Not {@code null}.
* @return true if the id is a popup menu Id, otherwise false.
*/
public static boolean isPopupMenuId(String id) {
return id.startsWith("[") && id.endsWith("]");
}

public String getId() {
return id;
}

@Override
public int hashCode() {
return Objects.hashCode(id);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof PopupMenuId)) {
return false;
}
PopupMenuId other = (PopupMenuId) obj;
return this.id.equals(other.id);
}


@Override
public String toString() {
return toStringHelper("PopupMenuId")
.addValue(id)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

package org.protege.editor.core.ui.menu;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;

@RunWith(MockitoJUnitRunner.class)
public class PopupMenuId_TestCase {

private PopupMenuId popupMenuId;
private String id = "[The id]";

@Before
public void setUp()
{
popupMenuId = new PopupMenuId(id);
}

@Test(expected = java.lang.NullPointerException.class)
public void shouldThrowNullPointerExceptionIf_id_IsNull() {
new PopupMenuId(null);
}

@Test
public void shouldReturnSupplied_id() {
assertThat(popupMenuId.getId(), is(this.id));
}

@Test
public void shouldBeEqualToSelf() {
assertThat(popupMenuId, is(popupMenuId));
}

@Test
public void shouldNotBeEqualToNull() {
assertThat(popupMenuId.equals(null), is(false));
}

@Test
public void shouldBeEqualToOther() {
assertThat(popupMenuId, is(new PopupMenuId(id)));
}

@Test
public void shouldNotBeEqualToOtherThatHasDifferent_id() {
assertThat(popupMenuId, is(not(new PopupMenuId("[b4d7ddab-7759-43c9-a349-e9ee36423b70]"))));
}

@Test
public void shouldBeEqualToOtherHashCode() {
assertThat(popupMenuId.hashCode(), is(new PopupMenuId(id).hashCode()));
}

@Test
public void shouldImplementToString() {
assertThat(popupMenuId.toString(), startsWith("PopupMenuId"));
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionForIllegalStart() {
new PopupMenuId("TheInvalidId]");
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionForIllegalEnd() {
new PopupMenuId("[TheInvalidId");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
* www.cs.man.ac.uk/~horridgm<br><br>
*/
public class OWLModelManagerTree<N extends OWLObject> extends OWLObjectTree<N> implements RefreshableComponent {
private static final long serialVersionUID = 9168555447316716896L;


private OWLModelManagerListener listener;

private OWLEntityRendererListener rendererListener;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.protege.editor.owl.ui.tree;

import org.protege.editor.core.ui.menu.MenuBuilder;
import org.protege.editor.core.ui.menu.PopupMenuId;
import org.protege.editor.owl.OWLEditorKit;
import org.protege.editor.owl.model.OWLModelManager;
import org.protege.editor.owl.model.hierarchy.OWLObjectHierarchyProvider;
Expand Down Expand Up @@ -36,6 +38,8 @@
import java.util.*;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* Author: Matthew Horridge<br>
* The University Of Manchester<br>
Expand Down Expand Up @@ -67,6 +71,8 @@ public class OWLObjectTree<N extends OWLObject> extends JTree implements OWLObje

private Point mouseDownPos;

private Optional<PopupMenuId> popupMenuId = Optional.empty();


public OWLObjectTree(OWLEditorKit eKit, OWLObjectHierarchyProvider<N> provider) {
this(eKit, provider, null);
Expand Down Expand Up @@ -119,6 +125,9 @@ public void mouseReleased(MouseEvent e) {
if (e.getClickCount() == 3 && e.isControlDown() && e.isShiftDown()) {
reload();
}
if(e.isPopupTrigger()) {
showPopupMenu(e);
}
}
});

Expand All @@ -128,6 +137,9 @@ public void mousePressed(MouseEvent e) {
// the key that corresponds to the menu accelerator key (CTRL on Windows, and
// CMD on the Mac).
altDown = e.isAltDown();
if (e.isPopupTrigger()) {
showPopupMenu(e);
}
}
});

Expand All @@ -148,6 +160,35 @@ public void treeCollapsed(TreeExpansionEvent event) {
});
}

/**
* Sets the popupMenuId for this tree.
* @param popupMenuId The id. Not {@code null}.
*/
public void setPopupMenuId(PopupMenuId popupMenuId) {
this.popupMenuId = Optional.of(checkNotNull(popupMenuId));
}

/**
* Clears the popupMenuId for this tree.
*/
public void clearPopupMenuId() {
this.popupMenuId = Optional.empty();
}

private Optional<PopupMenuId> getPopupMenuId() {
return popupMenuId;
}

private void showPopupMenu(MouseEvent e) {
if(!getPopupMenuId().isPresent()) {
return;
}
MenuBuilder menuBuilder = new MenuBuilder(eKit);
PopupMenuId popupMenuId = getPopupMenuId().get();
JPopupMenu popupMenu = menuBuilder.buildPopupMenu(popupMenuId);
popupMenu.show(this, e.getX(), e.getY());
}


public String getToolTipText(MouseEvent event) {
N obj = getOWLObjectAtMousePosition(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ public void setSelectedEntity(E entity) {
getTree().setSelectedOWLObject(entity);
}

public OWLObjectTree<E> getAssertedTree() {
return assertedTree;
}


public void setSelectedEntities(Set<E> entities) {
getTree().setSelectedOWLObjects(entities);
Expand Down Expand Up @@ -223,12 +227,6 @@ public boolean requestFocusInWindow() {
protected OWLObjectTree<E> getTree() {
Optional<ViewMode> viewMode= getView().getViewMode();
return viewModeComponent.getComponentForViewMode(viewMode);
// if(!viewMode.isPresent() || !inferredTree.isPresent() || viewMode.get().equals(ViewMode.ASSERTED)) {
// return assertedTree;
// }
// else {
// return inferredTree.get();
// }
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
*/
public abstract class AbstractOWLClassHierarchyViewComponent extends AbstractOWLEntityHierarchyViewComponent<OWLClass> {

/**
*
*/
private static final long serialVersionUID = -2033744534853698832L;

protected OWLObject updateView() {
return updateView(getOWLWorkspace().getOWLSelectionModel().getLastSelectedClass());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.protege.editor.owl.ui.view.cls;

import org.protege.editor.core.ui.menu.PopupMenuId;
import org.protege.editor.owl.model.OWLModelManager;
import org.protege.editor.owl.model.entity.OWLEntityCreationSet;
import org.protege.editor.owl.model.hierarchy.OWLObjectHierarchyProvider;
Expand Down Expand Up @@ -84,6 +85,7 @@ public void add(OWLClass child, OWLClass parent) {
handleAdd(child, parent);
}
});
getAssertedTree().setPopupMenuId(new PopupMenuId("[AssertedClassHierarchy]"));
}


Expand Down

0 comments on commit d9a985b

Please sign in to comment.