popupaddvalue.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 595 行 · 第 1/2 页
JAVA
595 行
package org.mindswap.swoop.popup;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.net.URI;import java.net.URL;import java.util.ArrayList;import java.util.Collections;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;import java.util.TreeSet;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.JTextField;import javax.swing.ListCellRenderer;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import org.mindswap.swoop.SwoopModel;import org.mindswap.swoop.reasoner.SwoopReasoner;import org.mindswap.swoop.renderer.SwoopCellRenderer;import org.mindswap.swoop.utils.DataValueChecker;import org.mindswap.swoop.utils.SetUtils;import org.mindswap.swoop.utils.ui.AddCloseBar;import org.mindswap.swoop.utils.ui.EntityComparator;import org.semanticweb.owl.impl.model.OWLConcreteDataTypeImpl;import org.semanticweb.owl.impl.model.OWLDataEnumerationImpl;import org.semanticweb.owl.io.vocabulary.OWLVocabularyAdapter;import org.semanticweb.owl.io.vocabulary.RDFVocabularyAdapter;import org.semanticweb.owl.io.vocabulary.XMLSchemaSimpleDatatypeVocabulary;import org.semanticweb.owl.model.OWLClass;import org.semanticweb.owl.model.OWLDataProperty;import org.semanticweb.owl.model.OWLDataRange;import org.semanticweb.owl.model.OWLDataValue;import org.semanticweb.owl.model.OWLDescription;import org.semanticweb.owl.model.OWLEnumeration;import org.semanticweb.owl.model.OWLException;import org.semanticweb.owl.model.OWLIndividual;import org.semanticweb.owl.model.OWLObjectProperty;import org.semanticweb.owl.model.OWLOntology;import org.semanticweb.owl.model.OWLProperty;import org.semanticweb.owl.model.change.AddDataPropertyInstance;import org.semanticweb.owl.model.change.AddIndividualClass;import org.semanticweb.owl.model.change.AddObjectPropertyInstance;import org.semanticweb.owl.model.change.RemoveEntity;import org.semanticweb.owl.model.helper.OntologyHelper;/** * @author Aditya * This class pops up whenever the user needs to specify an Property-Value pair * in a Ontology Change on an OWL Individual * */public class PopupAddValue extends JFrame implements ActionListener, ListSelectionListener, KeyListener { SwoopReasoner reasoner; SwoopModel swoopModel; OWLProperty prop; public List changes; Font tahoma = new Font("Tahoma", Font.PLAIN, 11); JList rangeBox, valueBox; JTextField newValueFld; AddCloseBar actionBar; boolean isRDFXMLLiteral = false; public PopupAddValue(SwoopReasoner reasoner, SwoopModel swoopModel, OWLProperty prop) { // setModal(true); this.reasoner = reasoner; this.swoopModel = swoopModel; this.prop = prop; this.changes = new ArrayList(); setupUI(); } private void setupUI() { JLabel propLbl = new JLabel(""); List rangeList = new ArrayList(); rangeBox = new JList(); rangeBox.setFont(tahoma); rangeBox.setCellRenderer(new RangeRenderer(swoopModel)); rangeBox.addKeyListener(this); Set valueList = new TreeSet(EntityComparator.INSTANCE);; valueBox = new JList(); valueBox.setFont(tahoma); valueBox.setCellRenderer(new SwoopCellRenderer(swoopModel)); valueBox.addKeyListener(this); boolean shrink = false; try { String propName = swoopModel.shortForm(prop.getURI()); propLbl = new JLabel("Specify value for Property '"+propName+"'", JLabel.LEFT); propLbl.setFont(tahoma); // fill range box with property range's OWLOntology ontology = swoopModel.getSelectedOntology(); Set ontologies; if (swoopModel.getImportsSetting(ontology)) { ontologies = OntologyHelper.importClosure(ontology); } else { ontologies = Collections.singleton(ontology); } Set rangeSet = prop.getRanges(ontologies); if (rangeSet.size()>0) { Iterator iter = rangeSet.iterator(); while (iter.hasNext()) { if (prop instanceof OWLObjectProperty) { // range of ObjectProperty is a class Object obj = iter.next(); if (obj instanceof OWLClass) { OWLClass desc = (OWLClass) obj; rangeList.add(desc); /* add owl:Thing regardless? */ OWLClass thing = swoopModel.getSelectedOntology().getOWLDataFactory().getOWLClass( URI.create( OWLVocabularyAdapter.INSTANCE.getThing())); rangeList.add(thing); addRangeSubClasses(desc, rangeList); } else if (obj instanceof OWLDescription) { // one-of range of values if (obj instanceof OWLEnumeration) { OWLEnumeration enumElem = (OWLEnumeration) obj; Iterator indIter = enumElem.getIndividuals().iterator(); Set indList = new TreeSet(EntityComparator.INSTANCE); while (indIter.hasNext()) { OWLIndividual ind = (OWLIndividual) indIter.next(); indList.add(ind); } valueList.addAll(indList); } } } else { // range of DatatypeProperty is a OWLDataRange desc = (OWLDataRange) iter.next(); if (desc instanceof OWLConcreteDataTypeImpl) { OWLConcreteDataTypeImpl dt = (OWLConcreteDataTypeImpl) desc; rangeList.add(dt); shrink = true; } else if (desc instanceof OWLDataEnumerationImpl) { OWLDataEnumerationImpl dt = (OWLDataEnumerationImpl) desc; rangeList.add("OWL Data Range"); Iterator deIter = dt.getValues().iterator(); while (deIter.hasNext()) { String val = deIter.next().toString(); String dType = val.substring(val.lastIndexOf("^")+1, val.length()); // dType = "("+dType.substring(dType.indexOf("#")+1, dType.length())+")";// ["+dType+"]"; val = val.substring(0, val.indexOf("^")); valueList.add(val+" "+"("+dType+")"); } } } } } else { // ** no range specified ** // default - String? for Datatype Properties // default - OWLThing for Object Properties if (prop instanceof OWLDataProperty) { URI xsdString = new URI("http://www.w3.org/2001/XMLSchema#string"); OWLConcreteDataTypeImpl dt = (OWLConcreteDataTypeImpl) swoopModel.getSelectedOntology().getOWLDataFactory().getOWLConcreteDataType(xsdString); rangeList.add(dt); shrink = true; } else { OWLClass thing = swoopModel.getSelectedOntology().getOWLDataFactory().getOWLClass( URI.create( OWLVocabularyAdapter.INSTANCE.getThing())); rangeList.add(thing); addRangeSubClasses(thing, rangeList); } } rangeBox.setListData(rangeList.toArray()); valueBox.setListData(valueList.toArray()); } catch (Exception e) { e.printStackTrace(); } rangeBox.addListSelectionListener(this); rangeBox.setSelectedIndex(-1); if (rangeBox.getModel().getSize()>0) rangeBox.setSelectedIndex(0); /* applyBtn = new JButton("Add"); applyBtn.setFont(tahoma); applyBtn.addActionListener(this); addBtn = new JButton("Add & Close"); addBtn.setFont(tahoma); addBtn.addActionListener(this); cancelBtn = new JButton("Cancel"); cancelBtn.setFont(tahoma); cancelBtn.addActionListener(this); JPanel box = new JPanel(); box.setLayout(new GridLayout(1,3)); box.add(applyBtn); box.add(addBtn); box.add(cancelBtn); */ actionBar = new AddCloseBar(); actionBar.addActionListener(this); Container content = getContentPane(); content.setLayout(new BorderLayout()); JPanel tab1W = new JPanel(); tab1W.setLayout(new BorderLayout()); JLabel propRangeLbl = new JLabel("Property Range"); propRangeLbl.setFont(tahoma); tab1W.add(propRangeLbl, "North"); tab1W.add(new JScrollPane(rangeBox), "Center"); JPanel tab1E = new JPanel(); tab1E.setLayout(new BorderLayout()); String valText = " Datatype Value:"; if (prop instanceof OWLObjectProperty) valText = " Existing Instance(s):"; JLabel valueLbl = new JLabel(valText); tab1E.add(valueLbl, "North"); tab1E.add(new JScrollPane(valueBox), "Center"); JSplitPane tab1Split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); tab1Split.setOneTouchExpandable(true); tab1Split.setLeftComponent(tab1W); if (prop instanceof OWLObjectProperty) tab1Split.setRightComponent(tab1E); tab1Split.setDividerLocation(150); newValueFld = new JTextField(); newValueFld.addKeyListener(this); JPanel newValuePanel = null; if (prop instanceof OWLObjectProperty) { newValuePanel = createWidget("OR Specify New Value/URI: ", newValueFld, null); tab1E.add(newValuePanel, "South"); } else { if (rangeList.get(0).toString().indexOf("OWL Data Range")==-1) { newValuePanel = createWidget("Specify Data Value: ", newValueFld, null); } else { newValuePanel = createWidget("Specify Data Value: ", new JScrollPane(valueBox), null); } tab1W.add(newValuePanel, "South"); } JPanel tab1I = new JPanel(); tab1I.setLayout(new BorderLayout()); tab1I.add(tab1Split, "Center"); content.add(tab1I, "Center"); content.add(actionBar, "South"); setTitle("Adding Property Value"); if (shrink) setSize(445,130); else setSize(445, 300); } private void addRangeSubClasses(OWLClass range, List rangeList) { // add all subclasses of desc to range as well //*********************************************** //Changed for Econnections // in order not to get an exception with link prop. //*********************************************** try { if(!((OWLObjectProperty)prop).isLink()){ Set allSubCla = SetUtils.union(reasoner.descendantClassesOf(range)); Iterator allSubIter = allSubCla.iterator(); OWLClass nothing = swoopModel.getSelectedOntology().getOWLDataFactory().getOWLClass( URI.create( OWLVocabularyAdapter.INSTANCE.getNothing())); while (allSubIter.hasNext()) { Object subDesObj = allSubIter.next(); if (!(subDesObj instanceof OWLClass)) continue; OWLClass subCla = (OWLClass) subDesObj; if (!subCla.equals(nothing) && !rangeList.contains(subCla)) rangeList.add(subCla);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?