From a5fd784f6ff9b57d9fd69cfb96fc9b6b05f2dca8 Mon Sep 17 00:00:00 2001 From: Matthew Horridge Date: Wed, 27 Apr 2016 15:51:57 -0700 Subject: [PATCH] Closes #382 --- ...WLObjectPropertyIndividualPairEditor2.java | 109 +++++++++++++++++- .../owl/ui/usage/UsageByEntityTreeModel.java | 1 - 2 files changed, 106 insertions(+), 4 deletions(-) diff --git a/protege-editor-owl/src/main/java/org/protege/editor/owl/ui/editor/OWLObjectPropertyIndividualPairEditor2.java b/protege-editor-owl/src/main/java/org/protege/editor/owl/ui/editor/OWLObjectPropertyIndividualPairEditor2.java index 9c01f303f..92e03762a 100644 --- a/protege-editor-owl/src/main/java/org/protege/editor/owl/ui/editor/OWLObjectPropertyIndividualPairEditor2.java +++ b/protege-editor-owl/src/main/java/org/protege/editor/owl/ui/editor/OWLObjectPropertyIndividualPairEditor2.java @@ -1,6 +1,8 @@ package org.protege.editor.owl.ui.editor; import org.protege.editor.core.ui.util.AugmentedJTextField; +import org.protege.editor.core.ui.util.InputVerificationStatusChangedListener; +import org.protege.editor.core.ui.util.VerifiedInputEditor; import org.protege.editor.owl.OWLEditorKit; import org.protege.editor.owl.model.OWLModelManager; import org.protege.editor.owl.model.classexpression.OWLExpressionParserException; @@ -11,6 +13,8 @@ import org.semanticweb.owlapi.model.OWLObjectProperty; import javax.swing.*; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; import java.awt.*; import java.util.Collections; @@ -19,21 +23,61 @@ * Stanford Center for Biomedical Informatics Research * 22/09/15 */ -public class OWLObjectPropertyIndividualPairEditor2 extends AbstractOWLObjectEditor { +public class OWLObjectPropertyIndividualPairEditor2 extends AbstractOWLObjectEditor implements VerifiedInputEditor { private JTextField objectPropertyField; private JTextField individualField; + private JPanel panelHolder; + private JPanel panel; private OWLModelManager modelManager; + private JLabel errorLabel = new JLabel(" "); + + private InputVerificationStatusChangedListener verificationStatusChangedListener = newState -> {}; + public OWLObjectPropertyIndividualPairEditor2(OWLEditorKit editorKit) { this.modelManager = editorKit.getOWLModelManager(); + panelHolder = new JPanel(new BorderLayout()); panel = new JPanel(new GridBagLayout()); + panelHolder.add(panel, BorderLayout.NORTH); objectPropertyField = new AugmentedJTextField("", 20, "Enter object property name"); + objectPropertyField.getDocument().addDocumentListener(new DocumentListener() { + @Override + public void insertUpdate(DocumentEvent e) { + validateInput(); + } + + @Override + public void removeUpdate(DocumentEvent e) { + validateInput(); + } + + @Override + public void changedUpdate(DocumentEvent e) { + + } + }); individualField = new AugmentedJTextField("", 20, "Enter individual name"); + individualField.getDocument().addDocumentListener(new DocumentListener() { + @Override + public void insertUpdate(DocumentEvent e) { + validateInput(); + } + + @Override + public void removeUpdate(DocumentEvent e) { + validateInput(); + } + + @Override + public void changedUpdate(DocumentEvent e) { + + } + }); Insets insets = new Insets(2, 2, 2, 2); panel.add(objectPropertyField, new GridBagConstraints( @@ -53,12 +97,25 @@ public OWLObjectPropertyIndividualPairEditor2(OWLEditorKit editorKit) { GridBagConstraints.HORIZONTAL, insets, 0, 0)); + + errorLabel.setForeground(Color.RED); + panel.add(errorLabel, + new GridBagConstraints( + 0, 1, + 2, 1, + 100, 0, + GridBagConstraints.BASELINE_TRAILING, + GridBagConstraints.HORIZONTAL, + insets, + 0, 0 + )); + JLabel tipLabel = new JLabel("(Tip: Use CTRL+Space to auto-complete names)"); tipLabel.setForeground(Color.GRAY); tipLabel.setHorizontalAlignment(SwingConstants.RIGHT); panel.add(tipLabel, new GridBagConstraints( - 0, 1, + 0, 2, 2, 1, 100, 0, GridBagConstraints.BASELINE_TRAILING, @@ -92,6 +149,36 @@ public Object createObject(String text) throws OWLExpressionParserException { return modelManager.getOWLEntityFinder().getOWLIndividual(text); } }); + validateInput(); + } + + private void validateInput() { + objectPropertyField.setToolTipText(null); + individualField.setToolTipText(null); + errorLabel.setText(" "); + + String objectPropertyName = objectPropertyField.getText().trim(); + if(!objectPropertyName.isEmpty() && modelManager.getOWLEntityFinder().getOWLObjectProperty(objectPropertyName) == null) { + objectPropertyField.setToolTipText("Invalid object property"); + errorLabel.setText("Invalid property name"); + verificationStatusChangedListener.verifiedStatusChanged(false); + return; + } + + String individualName = individualField.getText().trim(); + if(!individualName.isEmpty() && modelManager.getOWLEntityFinder().getOWLIndividual(individualName) == null) { + individualField.setToolTipText("Invalid individual name"); + errorLabel.setText("Invalid individual name"); + verificationStatusChangedListener.verifiedStatusChanged(false); + return; + } + if (objectPropertyName.isEmpty() || individualName.isEmpty()) { + verificationStatusChangedListener.verifiedStatusChanged(false); + return; + } + verificationStatusChangedListener.verifiedStatusChanged(true); + + } @Override @@ -106,7 +193,7 @@ public boolean canEdit(Object object) { @Override public JComponent getEditorComponent() { - return panel; + return panelHolder; } @Override @@ -139,4 +226,20 @@ public boolean setEditedObject(OWLObjectPropertyIndividualPair editedObject) { public void dispose() { } + + @Override + public void addStatusChangedListener(InputVerificationStatusChangedListener listener) { + if (listener != null) { + this.verificationStatusChangedListener = listener; + } + else { + verificationStatusChangedListener = (newState) -> {}; + } + validateInput(); + } + + @Override + public void removeStatusChangedListener(InputVerificationStatusChangedListener listener) { + this.verificationStatusChangedListener = (newState) -> {}; + } } diff --git a/protege-editor-owl/src/main/java/org/protege/editor/owl/ui/usage/UsageByEntityTreeModel.java b/protege-editor-owl/src/main/java/org/protege/editor/owl/ui/usage/UsageByEntityTreeModel.java index b60e563eb..1ad9ba720 100644 --- a/protege-editor-owl/src/main/java/org/protege/editor/owl/ui/usage/UsageByEntityTreeModel.java +++ b/protege-editor-owl/src/main/java/org/protege/editor/owl/ui/usage/UsageByEntityTreeModel.java @@ -470,7 +470,6 @@ public void visit(OWLTransitiveObjectPropertyAxiom axiom) { public void visit(SWRLRule rule) { - } }