All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.coode.dlquery.ResultsList Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
package org.coode.dlquery;

import java.awt.Frame;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.*;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.protege.editor.core.ui.list.MList;
import org.protege.editor.core.ui.list.MListButton;
import org.protege.editor.owl.OWLEditorKit;
import org.protege.editor.owl.ui.OWLClassExpressionComparator;
import org.protege.editor.owl.ui.explanation.ExplanationManager;
import org.protege.editor.owl.ui.framelist.ExplainButton;
import org.protege.editor.owl.ui.renderer.LinkedObjectComponent;
import org.protege.editor.owl.ui.renderer.LinkedObjectComponentMediator;
import org.protege.editor.owl.ui.view.Copyable;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObject;
import org.semanticweb.owlapi.reasoner.Node;
import org.semanticweb.owlapi.reasoner.OWLReasoner;

import static org.coode.dlquery.ResultsSection.*;

/**
 * Author: Matthew Horridge
* The University Of Manchester
* Bio-Health Informatics Group
* Date: 27-Feb-2007

*/ public class ResultsList extends MList implements LinkedObjectComponent, Copyable { private final OWLEditorKit owlEditorKit; private final Set visibleResultsSections = EnumSet.of(SUB_CLASSES); private final LinkedObjectComponentMediator mediator; private final List copyListeners = new ArrayList<>(); public ResultsList(OWLEditorKit owlEditorKit) { this.owlEditorKit = owlEditorKit; setCellRenderer(new DLQueryListCellRenderer(owlEditorKit)); mediator = new LinkedObjectComponentMediator(owlEditorKit, this); getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent event) { if (!event.getValueIsAdjusting()) { ChangeEvent ev = new ChangeEvent(ResultsList.this); for (ChangeListener l : new ArrayList<>(copyListeners)) { l.stateChanged(ev); } } } }); } public boolean isResultsSectionVisible(ResultsSection section) { return visibleResultsSections.contains(section); } public void setResultsSectionVisible(ResultsSection section, boolean b) { if (b) { visibleResultsSections.add(section); } else { visibleResultsSections.remove(section); } } private List toSortedList(Set clses) { OWLClassExpressionComparator descriptionComparator = new OWLClassExpressionComparator(owlEditorKit.getModelManager()); List list = new ArrayList<>(clses); Collections.sort(list, descriptionComparator); return list; } public void setOWLClassExpression(OWLClassExpression description) { List data = new ArrayList<>(); OWLDataFactory factory = owlEditorKit.getOWLModelManager().getOWLDataFactory(); OWLReasoner reasoner = owlEditorKit.getModelManager().getReasoner(); if (isResultsSectionVisible(EQUIVALENT_CLASSES)) { final List results = toSortedList(reasoner.getEquivalentClasses(description).getEntities()); data.add(new DLQueryResultsSection(EQUIVALENT_CLASSES.getDisplayName() + " (" + results.size() + ")")); for (OWLClass cls : results) { data.add(new DLQueryResultsSectionItem(cls, factory.getOWLEquivalentClassesAxiom(description, cls))); } } if (isResultsSectionVisible(SUPER_CLASSES)) { final List results = toSortedList(reasoner.getSuperClasses(description, false).getFlattened()); data.add(new DLQueryResultsSection(SUPER_CLASSES.getDisplayName() + " (" + results.size() + ")")); for (OWLClass superClass : results) { data.add(new DLQueryResultsSectionItem(superClass, factory.getOWLSubClassOfAxiom(description, superClass))); } } if (isResultsSectionVisible(DIRECT_SUPER_CLASSES)) { final List results = toSortedList(reasoner.getSuperClasses(description, true).getFlattened()); data.add(new DLQueryResultsSection(DIRECT_SUPER_CLASSES.getDisplayName() + " (" + results.size() + ")")); for (OWLClass superClass : results) { data.add(new DLQueryResultsSectionItem(superClass, factory.getOWLSubClassOfAxiom(description, superClass))); } } if (isResultsSectionVisible(DIRECT_SUB_CLASSES)) { final Set resultSet = new HashSet<>(); for (Node clsSet : reasoner.getSubClasses(description, true)) { resultSet.addAll(clsSet.getEntities()); } final List results = toSortedList(resultSet); data.add(new DLQueryResultsSection(DIRECT_SUB_CLASSES.getDisplayName() + " (" + results.size() + ")")); for (OWLClass subClass : results) { data.add(new DLQueryResultsSectionItem(subClass, factory.getOWLSubClassOfAxiom(subClass, description))); } } if (isResultsSectionVisible(SUB_CLASSES)) { final Set resultSet = new HashSet<>(); for (Node clsSet : reasoner.getSubClasses(description, false)) { resultSet.addAll(clsSet.getEntities()); } final List results = toSortedList(resultSet); data.add(new DLQueryResultsSection(SUB_CLASSES.getDisplayName() + " (" + results.size() + ")")); for (OWLClass cls : results) { data.add(new DLQueryResultsSectionItem(cls, factory.getOWLSubClassOfAxiom(cls, description))); } } if (isResultsSectionVisible(INSTANCES)) { final Set results = reasoner.getInstances(description, false).getFlattened(); data.add(new DLQueryResultsSection(INSTANCES.getDisplayName() + " (" + results.size() + ")")); for (OWLIndividual ind : results) { data.add(new DLQueryResultsSectionItem(ind, factory.getOWLClassAssertionAxiom(description, ind))); } } setListData(data.toArray()); } protected List getButtons(Object value) { if (!(value instanceof DLQueryResultsSectionItem)) { return Collections.emptyList(); } final OWLAxiom axiom = ((DLQueryResultsSectionItem) value).getAxiom(); ExplanationManager explanationManager = owlEditorKit.getOWLModelManager().getExplanationManager(); if (!explanationManager.hasExplanation(axiom)) { return Collections.emptyList(); } return Arrays.asList( new ExplainButton(e -> { Frame parent = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, ResultsList.this); explanationManager.handleExplain(parent, axiom); }) ); } public JComponent getComponent() { return this; } public OWLObject getLinkedObject() { return mediator.getLinkedObject(); } public Point getMouseCellLocation() { Rectangle r = getMouseCellRect(); if (r == null) { return null; } Point mousePos = getMousePosition(); if (mousePos == null) { return null; } return new Point(mousePos.x - r.x, mousePos.y - r.y); } public Rectangle getMouseCellRect() { Point mousePos = getMousePosition(); if (mousePos == null) { return null; } int sel = locationToIndex(mousePos); if (sel == -1) { return null; } return getCellBounds(sel, sel); } public void setLinkedObject(OWLObject object) { mediator.setLinkedObject(object); } public boolean canCopy() { return getSelectedIndices().length > 0; } public List getObjectsToCopy() { List copyObjects = new ArrayList<>(); for (Object sel : getSelectedValuesList()) { if (sel instanceof DLQueryResultsSectionItem) { copyObjects.add(((DLQueryResultsSectionItem) sel).getOWLObject()); } } return copyObjects; } public void addChangeListener(ChangeListener changeListener) { copyListeners.add(changeListener); } public void removeChangeListener(ChangeListener changeListener) { copyListeners.remove(changeListener); } }