popupaddvalue.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 595 行 · 第 1/2 页

JAVA
595
字号
				}			}		}		catch (OWLException ex) {			ex.printStackTrace();		}	}		private JPanel createWidget(Object title, JComponent comp, JComponent comp2) {		JPanel widget = new JPanel();		widget.setLayout(new BorderLayout());				if (title!=null) {			if (title instanceof String) {				String titleStr = title.toString();				JLabel lbl = new JLabel(titleStr);				lbl.setFont(tahoma);				widget.add(lbl, "West");			}			else widget.add((Component) title, "West");		}		widget.add(comp, "Center");		if (comp2!=null) widget.add(comp2, "East");		return widget;	}		/**	 * Check for the validity of the property-value pair based on the range of the 	 * property and the data value specified by the user	 * @param dt - XSD DataType (or rdfs:Literal)	 * @param value - string value specified by user	 * @return	 */	private boolean checkValidValue(OWLConcreteDataTypeImpl dt, String value) {		boolean valid = false;		String xsd = XMLSchemaSimpleDatatypeVocabulary.XS;		String errorMsg = "Invalid Value for Specified Datatype - require ";				if (dt.getURI().toString().equals(RDFVocabularyAdapter.RDF+"XMLLiteral"))			this.isRDFXMLLiteral = true;				return DataValueChecker.isValidValue( this, dt, value);	}		private boolean addValueChange() {				try {			isRDFXMLLiteral = false;						if (prop instanceof OWLDataProperty) {				// create datevalue from user-typed text				String val = newValueFld.getText();				OWLDataValue dVal = null;							if (!rangeBox.getModel().getElementAt(0).toString().equals("OWL Data Range")) {					// range is not data value range					OWLConcreteDataTypeImpl dt = (OWLConcreteDataTypeImpl) rangeBox.getSelectedValue();					URI datatypeURI = dt.getURI(); 					boolean valid = checkValidValue(dt, val);								if (valid) {																		if (this.isRDFXMLLiteral) {//							 //TODO AK: need to add parseType="Literal"							 // currently escapeChars implemented below						}												// Evren: Do not use a language identifier becuase language identifiers can						// only be used with plain literals (but here we might have a different						// datatype) and also there is no need to assume that English is the default 						// language. 						dVal = swoopModel.getSelectedOntology().getOWLDataFactory().getOWLConcreteData(datatypeURI, null, val);					}					else {						return false;					}				}				else {					val = valueBox.getSelectedValue().toString();					String dType = val.substring(val.lastIndexOf("(")+1, val.length()-1);					//val = val.substring(0, val.lastIndexOf("(")).trim();					URI datatypeURI = new URI(dType); 					// Evren: Do not use a language identifier becuase language identifiers can					// only be used with plain literals (but here we might have a different					// datatype) and also there is no need to assume that English is the default 					// language. 										dVal = swoopModel.getSelectedOntology().getOWLDataFactory().getOWLConcreteData(datatypeURI, null, val);				}								// create data property change				OWLIndividual dispInst = (OWLIndividual) swoopModel.getSelectedEntity();				AddDataPropertyInstance change = new AddDataPropertyInstance(swoopModel.getSelectedOntology(), dispInst, (OWLDataProperty) prop, dVal, null);				changes.add(change);			}			else {				// prop is an object property				Object[] individuals = null;								if ((newValueFld.getText()!=null) && (!newValueFld.getText().trim().equals(""))) {					// create new instance					String indID = newValueFld.getText();					// check for invalid chars in indID					indID = indID.replaceAll(" ","_");					if (indID.indexOf("#") < 0) {						indID = "#"+indID;					}					URI indURI = null;					if (!isURL(indID)) indURI =swoopModel.getSelectedOntology().getLogicalURI().resolve(indID);					else indURI = new URI(indID);										OWLIndividual ind = swoopModel.getSelectedOntology().getOWLDataFactory().getOWLIndividual(indURI);					individuals = new Object[] {ind};					OWLClass cla = (OWLClass) rangeBox.getSelectedValue();					AddIndividualClass change = new AddIndividualClass(swoopModel.getSelectedOntology(), ind, cla, null);					changes.add(change);				} else {					// get existing instance					individuals = valueBox.getSelectedValues();				}				// create object property instance				OWLIndividual dispInst = (OWLIndividual) swoopModel.getSelectedEntity();				for (int i = 0; i < individuals.length; i++) {					OWLIndividual ind = (OWLIndividual) individuals[i];					AddObjectPropertyInstance change = new AddObjectPropertyInstance(swoopModel.getSelectedOntology(), dispInst, (OWLObjectProperty) prop, ind, null);					changes.add(change);					// **************************************************					// Added for Econnections					// ***************************************************					if (prop instanceof OWLObjectProperty) {						if (((OWLObjectProperty) prop).isLink()) {							RemoveEntity oc2 = new RemoveEntity(swoopModel									.getSelectedOntology(), ind, null);							changes.add(oc2);						}					}					// *********************************				}			}						swoopModel.addUncommittedChanges(changes);			changes = new ArrayList(); // reset it after changes have been added					}		catch (Exception e) {			e.printStackTrace();		}		return true;	}		public void actionPerformed(ActionEvent e) {				if (e.getSource() == actionBar) {			if (AddCloseBar.ADD.equals(e.getActionCommand())) {				addValueChange();			}			if (AddCloseBar.ADDCLOSE.equals(e.getActionCommand())) {				if (addValueChange()) {					dispose();				}			}			if (AddCloseBar.CLOSE.equals(e.getActionCommand())) {				dispose();			}		}				if (e.getSource()==rangeBox) {			fillValueBox();		}			}		private void fillValueBox() {		Object selItem = rangeBox.getSelectedValue();		Set valueList = new TreeSet(EntityComparator.INSTANCE);				if (selItem instanceof OWLClass) {			OWLClass cla = (OWLClass) selItem;			// fill valueBox with existing instances of cla			try {				Set instSet = new HashSet();								if (!cla.getURI().toString().equals(OWLVocabularyAdapter.INSTANCE.getThing())) {					instSet = reasoner.instancesOf(cla);					// also get any enumerations of the class					Set enums = cla.getEnumerations(swoopModel.getSelectedOntology());					for (Iterator it = enums.iterator(); it.hasNext();) {						OWLEnumeration en = (OWLEnumeration) it.next();						instSet.addAll(en.getIndividuals());					}				}				else {					// add all individuals in the ontology for owl:Thing					instSet.addAll(swoopModel.getEntitySet(swoopModel.getSelectedOntology(), swoopModel.TRANSCLOSE_ONT, swoopModel.INDIVIDUALS));				}								Iterator iter = instSet.iterator();				while (iter.hasNext()) {					OWLIndividual ind = (OWLIndividual) iter.next();					valueList.add(ind);				}				valueBox.setFont(new Font(swoopModel.getFontFace(), Font.PLAIN, 11));				valueBox.setListData(valueList.toArray());			}			catch (OWLException ex) {				ex.printStackTrace();			}		}	}		private boolean isURL(String str) {				try {			URL url = new URL(str);			return true;		}		catch (Exception ex) {  			ex.printStackTrace();		}		return false;	}			class RangeRenderer extends JLabel implements ListCellRenderer {		private SwoopModel swoopModel;		RangeRenderer(SwoopModel swoopModel) {			this.swoopModel = swoopModel;		}		public Component getListCellRendererComponent(			JList list,			Object value,			int index,			boolean isSelected,			boolean cellHasFocus) {			try {				URI dispURI = null;				if (value instanceof OWLClass) {					OWLClass cla = (OWLClass) value;					dispURI = cla.getURI();				}				else if (value instanceof OWLConcreteDataTypeImpl) {					OWLConcreteDataTypeImpl dt = (OWLConcreteDataTypeImpl) value;					dispURI = dt.getURI();				}				else if (value instanceof OWLIndividual) {					OWLIndividual ind = (OWLIndividual) value;					dispURI = ind.getURI();				}								if (dispURI!=null) setText(swoopModel.shortForm(dispURI));				else setText(value.toString());							} catch (OWLException e) {				setText("Anonymous");			}			if (isSelected) {				setBackground(list.getSelectionBackground());				setForeground(list.getSelectionForeground());			} else {				setBackground(list.getBackground());				setForeground(list.getForeground());			}			setEnabled(list.isEnabled());			setFont(list.getFont());			setOpaque(true);			return this;		}	}	public void valueChanged(ListSelectionEvent e) {		if (e.getSource()==rangeBox) {			fillValueBox();		}			}	public void keyTyped(KeyEvent arg0) {		}	public void keyPressed(KeyEvent e) {		if (e.getSource()==rangeBox || e.getSource()==valueBox) {			String alpha = Character.toString(e.getKeyChar()).toLowerCase();			PopupCommon.listSelector(swoopModel, (JList) e.getSource(), alpha);		}				if (e.getSource()==newValueFld) {			if (e.getKeyCode()==10) {				addValueChange();			}		}			}	public void keyReleased(KeyEvent arg0) {	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?