popupnew.java

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

JAVA
645
字号
			try {								String uriStr = uriFld.getText();								// check for invalid uri				if (uriStr.trim().equals("") || !(isURI(uriStr))) {					JOptionPane.showMessageDialog(null, "A Valid Logical URI needs to be specified for the new OWL Ontology", "Creation Error!", JOptionPane.ERROR_MESSAGE);					return;				}				URI uri = new URI(uriStr);				OWLDataFactory df = null;								if (type.equals("Ontology")) {					// new ontology					OWLBuilder builder = new OWLBuilder();										// check for existing uri					if (swoopModel.getOntologyURIs().contains(uri)) {						JOptionPane.showMessageDialog(null, "An Ontology with that Logical URI already exists in SWOOP", "Creation Error!", JOptionPane.ERROR_MESSAGE);						return;					}					// if no errors, proceed to building new ontology					builder.createOntology(uri, uri);					ontology = builder.getOntology();					df = ontology.getOWLDataFactory();					addAnnotations(df);										/* also add owl:Thing to the ontology */					// otherwise thing appears as an imported class in the tree?!					OWLClass thing = df.getOWLThing();					AddEntity ae = new AddEntity(ontology, thing, null);					ae.accept((ChangeVisitor) ontology);										swoopModel.addOntology(ontology);					swoopModel.setSelectedOntology(ontology);									}									else {					if (type.equals("Class")) {											// new class						df = ontology.getOWLDataFactory();						newEntity = df.getOWLClass(uri);										}					else if (type.equals("Property")) {						df = ontology.getOWLDataFactory();						if (propType.getSelectedIndex()==0) {							// new datatype property							newEntity = df.getOWLDataProperty(uri);						}						else if (propType.getSelectedIndex()==2) {							// new annotation property							newEntity = df.getOWLAnnotationProperty(uri);						}						else {							// new object property							newEntity = df.getOWLObjectProperty(uri);							//**************************************							//Added for Econnections							//***************************************							if (propType.getSelectedIndex()==2) {								PopupAddForeignOntology popup = new PopupAddForeignOntology(swoopModel);								popup.setLocation(200, 200);								popup.show();																((OWLObjectProperty)newEntity).setLinkTarget(popup.ontologyURI);								if (!(swoopModel.getOntologiesMap().containsKey(popup.ontologyURI)))								   swoopModel.addOntology(popup.ontologyURI);							}							//*************************************													}									}					else if (type.equals("Individual")) {						// new individual						df = ontology.getOWLDataFactory();						newEntity = df.getOWLIndividual(uri);										}										// add rdfs:label and comment if any					addAnnotations(df);										createEntity();				}								addBtn.setEnabled(false);				addCloseBtn.setEnabled(false);				if (e.getSource() == addCloseBtn) {					dispose();				}				idFld.setText("");				labelFld.setText("");				commentArea.setText("");			}			catch (Exception ex) {				ex.printStackTrace();			}		}				if (e.getSource()==cancelBtn) {			dispose();		}			}		private void redrawUI() {		hide();		getContentPane().removeAll();		getContentPane().repaint();					try {			setupUI();			show();		}		catch (Exception ex) {			ex.printStackTrace();		}	}		private boolean isURI(String str) {				try {			URI url = new URI(str);			return true;		}		catch (Exception ex) {}		return false;	}		private void createEntity() {		if (newEntity!=null) {			OWLEntity parent = null;			if (parentBox.getSelectedIndex()!=0) parent = (OWLEntity) parentBox.getSelectedItem();			this.lastSelectedParent = parent;			swoopModel.addEntity(ontology, newEntity, parent);		}	}	private void addAnnotations(OWLDataFactory df) {				try {			String lbl = "", comment = "", version = "";			if (labelFld.getText()!=null) lbl = labelFld.getText().trim();			if (commentArea.getText()!=null) comment = commentArea.getText().trim();			if (idFld.getText()!=null) version = idFld.getText().trim();						// get annotation properties			URI lblURI = new URI("http://www.w3.org/2000/01/rdf-schema#label");			OWLAnnotationProperty lblProp = df.getOWLAnnotationProperty(lblURI);						URI commentURI = new URI("http://www.w3.org/2000/01/rdf-schema#comment");			OWLAnnotationProperty commentProp = df.getOWLAnnotationProperty(commentURI);						URI versionInfoURI = new URI("http://www.w3.org/2002/07/owl#versionInfo");			OWLAnnotationProperty versionInfoProp = df.getOWLAnnotationProperty(versionInfoURI);									if (type.equals("Ontology")) {								AddAnnotationInstance annot = null;								// add version info				if (version.length()>0) {					annot = new AddAnnotationInstance(ontology, ontology, versionInfoProp, version, null);					annot.accept((ChangeVisitor) ontology);				}								// add label				if(lbl.length() > 0) {					annot = new AddAnnotationInstance(ontology, ontology, lblProp, lbl, null);					annot.accept((ChangeVisitor) ontology);				}								// add comment				if(comment.length() > 0) {					annot = new AddAnnotationInstance(ontology, ontology, commentProp, comment, null);					annot.accept((ChangeVisitor) ontology);				}			}			else {				AddAnnotationInstance annot;								// add label				if(lbl.length() > 0) {									annot = new AddAnnotationInstance(ontology, newEntity, lblProp, lbl, null);					annot.accept((ChangeVisitor) ontology);				}								// add comment				if(comment.length() > 0) {									annot = new AddAnnotationInstance(ontology, newEntity, commentProp, comment, null);					annot.accept((ChangeVisitor) ontology);				}			}		}		catch (Exception ex) {			ex.printStackTrace();		}	}		public void changedUpdate(DocumentEvent arg0) {		// TODO Auto-generated method stub			}	private void updateURI() {				if ((!type.equals(("Ontology"))) && (idFld.getText()!=null)) {			String uri = "";			if (uriFld.getText()!=null) uri = uriFld.getText();			if (uri.indexOf("#")>=0) uri = uri.substring(0, uri.indexOf("#"));			else if (uri.indexOf("/")>=0) uri = uri.substring(0, uri.lastIndexOf("/"));			else uri = "";			uri+="#"+idFld.getText();			uriFld.setText(uri);		}	}		public void insertUpdate(DocumentEvent arg0) {		updateURI();	}	public void removeUpdate(DocumentEvent arg0) {		updateURI();	}	public void keyPressed(KeyEvent e) {				if ((!type.equals("Ontology")) && 				((e.getSource()==idFld)) || (e.getSource()==uriFld)) {			addBtn.setEnabled(true);					addCloseBtn.setEnabled(true);				}		if ((type.equals("Ontology")) && (e.getSource()==uriFld)) {			addBtn.setEnabled(true);			addBtn.setEnabled(true);				}				if (e.getKeyCode()==10) {			// enter key pressed			if (addBtn.isEnabled()) {				addBtn.doClick();				if (type.equals("Ontology")) uriFld.requestFocus();				else idFld.requestFocus();							}					}	}	public void keyReleased(KeyEvent arg0) {		// TODO Auto-generated method stub			}	public void keyTyped(KeyEvent arg0) {		// TODO Auto-generated method stub			}	public void fillParentBox() {				try {						parentBox.setFont(new Font(swoopModel.getFontFace(), Font.PLAIN, 11));			parentBox.removeActionListener(this);			parentBox.removeAllItems();						// fill values in parentBox			if (type.equals("Class") || (type.equals("Individual"))) {				OWLClass thing = swoopModel.getSelectedOntology().getOWLDataFactory().getOWLClass( URI.create( OWLVocabularyAdapter.INSTANCE.getThing()));				parentBox.addItem(thing);				Set claSet = new TreeSet(EntityComparator.INSTANCE);				claSet.addAll(swoopModel.getReasoner().getClasses());				Iterator claSetIter = claSet.iterator();				while (claSetIter.hasNext()) {					OWLClass cla = (OWLClass) claSetIter.next();					if (!cla.getURI().equals(thing.getURI()))							parentBox.addItem(cla);				}							}			else if (type.equals("Property")) {				parentBox.addItem("None");				Set propSet = new TreeSet(EntityComparator.INSTANCE);				propSet.addAll(swoopModel.getReasoner().getDataProperties());				propSet.addAll(swoopModel.getReasoner().getObjectProperties());				Iterator propSetIter = propSet.iterator();				while (propSetIter.hasNext()) {					OWLProperty prop = (OWLProperty) propSetIter.next();					parentBox.addItem(prop);				}										}		}		catch (OWLException e) {			e.printStackTrace();		}		finally {			try {				if (!type.equals("Ontology")) {					OWLClass thing = null;					if (swoopModel.getSelectedOntology()!=null) thing = swoopModel.getSelectedOntology().getOWLDataFactory().getOWLClass( URI.create( OWLVocabularyAdapter.INSTANCE.getThing()));					parentBox.addActionListener(this);					if (this.lastSelectedParent!=null) {						if (!this.lastSelectedParent.equals(thing)) parentBox.setSelectedItem(this.lastSelectedParent);						else parentBox.setSelectedIndex(0);					}				}			} 			catch (OWLException e1) {				e1.printStackTrace();			}					}	}	public void modelChanged(ModelChangeEvent event) {				if (event.getType()==ModelChangeEvent.ADDED_ENTITY) {			this.redrawUI();		}			}	public void dispose() {		super.dispose();		swoopModel.removeListener(this);	}	}

⌨️ 快捷键说明

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