annotearenderer.java

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

JAVA
1,645
字号
		if (Annotea.INSTANCE==null) {			Annotea.initializeAnnotea();		}				// iterate through all servers in Retrieve List		Iterator getIter = annotPrefs.serverRList.iterator();		while (getIter.hasNext()) {			// retrieve annotations from server			URL serverURL;			try {				serverURL = new URL(getIter.next().toString());				AnnoteaClient client = new AnnoteaClient(serverURL, swoopModel);								// call findAnnotations passing it OWL object URI				// and get a set of Description instances				URI currentURI = swoopModel.selectedOWLObject.getURI();								Set descSet = client.findAnnotations(currentURI);				// modify annotates of each description 				if (descSet.size()>0) {					this.normalizeAnnotates(descSet);					swoopModel.addAnnotatedObjectURI(currentURI);				}								OWLOntology ont = swoopModel.getSelectedOntology();				if (currentURI.equals(ont.getURI())) {					// for an ontology,					// annotations also contain annotations on all entities in ontology					// hence need to run through descriptions to see which entity they annotate and add to cache										// first clear all annotation caches of entities in ontology					// since we're using HashSet and duplicate annotations can appear when added below					this.clearOntAnnotationCache(ont); //*** crucial										for (Iterator iter = descSet.iterator(); iter.hasNext();) {						Description desc = (Description) iter.next();						URI annotURI = this.getAnnotationURI(desc.annotates);						this.addAnnotationCache(annotURI, desc);						swoopModel.addAnnotatedObjectURI(annotURI);					}										// refresh display to show "A" superscript					swoopHandler.termDisplay.updateTreeDisplay();					swoopHandler.termDisplay.updateListDisplay();					swoopHandler.ontDisplay.simplySelectOntology(ont);				}				else {					// for an entity, directly store updated result in cache					annotationCache.putAnnotationSet(swoopModel.selectedOWLObject.getURI(), descSet);				}				this.displayAnnotations();								//TESTING: swoopModel.setSelectedOntology(client.findAnnotationsOnt(aboutURI));								statusLbl.setText("Status: Update Completed for " + swoopModel.shortForm(currentURI));			}			catch (Exception e) {				statusLbl.setText("Status: ERROR retrieving annotations - see console log");				e.printStackTrace();			}		}				this.setCursor(currentCursor);	}		private void addAnnotationCache(URI key, Description value) {		Set annotSet = new HashSet();		if (annotationCache.getAnnotationSet(key)!=null) annotSet = annotationCache.getAnnotationSet(key);		annotSet.add(value);		annotationCache.putAnnotationSet(key, annotSet);	}		/**	 * Add an annotation (Description object) to its appropriate	 * location in the node tree (also consider reply)	 *	 */	private void addAnnotationNodeToTree(Description newAnnot) {				// TODO add annotation at its appropriate location in node tree	}		/**	 * Create the annotation node tree again by passing it	 * a new Set of Description objects	 * @param descSet	 */	private void setAnnotationNodeTree(Set descriptionSet, Set extraSet) {				// get rootnode from annotation treetable		JTree annotTree = annotTreeTable.getTree();		DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) annotTree.getModel().getRoot();		rootNode.removeAllChildren();		htmlCore.setText("");		annotAttachBtn.setEnabled(false);				// sort descriptions based on "created" value		SortedSet descSet = new TreeSet(DescriptionComparator.INSTANCE);		descSet.addAll(descriptionSet);				// populate node tree		for (Iterator descIter = descSet.iterator(); descIter.hasNext();) {			Description desc = (Description) descIter.next();			//TESTING: System.out.println(desc.getAuthor()+" "+desc.getCreated()+" "+desc.getBody());			DefaultMutableTreeNode annotNode = new DefaultMutableTreeNode(desc);			rootNode.add(annotNode);		}				// sort extra based on "created" value		if (extraSet.size()>0) {						// add separator description			rootNode.add(getSeparatorNode());						SortedSet extraAnnotSet = new TreeSet(DescriptionComparator.INSTANCE);			extraAnnotSet.addAll(extraSet);						// add extra descriptions to node tree			for (Iterator descIter = extraAnnotSet.iterator(); descIter.hasNext();) {				Description desc = (Description) descIter.next();				//TESTING: System.out.println(desc.getAuthor()+" "+desc.getCreated()+" "+desc.getBody());				DefaultMutableTreeNode annotNode = new DefaultMutableTreeNode(desc);				rootNode.add(annotNode);			}		}				// expand all tree nodes		for (int row=0; row<descSet.size(); row++)			annotTree.expandRow(row);		annotTree.updateUI();		refreshUI();	}		/** Creates description object for the annotation based on 	 *  values provided for author (dc:creator), date (dc:date),	 *  annotates (element being annotated), type (comment/explanation..)	 *  and body given as an html block	 *  	 *  Opens HTTP URL connections to all servers in AnnoteaServerPrefs.serverPList	 *  and POSTs descriptions	 *  (authentication popup appears where reqd) 	 */ 	private void postAnnotation() {				// INITIALIZATION OF ANNOTEA DURING FIRST POST/UPDATE		if (Annotea.INSTANCE==null) {			Annotea.initializeAnnotea();		}						// create description that wraps annotation		Description description = new Description();		URI[] annotates = new URI[2];		try {			// get selected OWLObject to annotate			  			annotates[0] = swoopModel.selectedOWLObject.getURI();						annotates[1] = swoopModel.getSelectedOntology().getURI();			description.setAnnotates(annotates);		} 		catch (Exception e) {			//e.printStackTrace();			statusLbl.setText("Status: Post ERROR - need to select OWL ontology / entity");			return;		}		String htmlBody = ekitCore.getTextPane().getText();				// insert summary in <head> of htmlBody		String summary = subjectFld.getText();		if (!summary.equals("")) {			htmlBody = htmlBody.substring(0, htmlBody.indexOf("<head>")+6) + summary + htmlBody.substring(htmlBody.indexOf("</head>"), htmlBody.length()); 		}		//		htmlBody = stripHTML(htmlBody); // Aditya 09/03/05: Dont need this because its done in the CorrectedRDFRenderer while generating RDF/XML		description.setBody(htmlBody);		description.setBodyType("text/html");		description.setAuthor(authorFld.getText());		description.setCreated(dateFld.getText());		description.setAnnotationType((OWLClass) typeBox.getSelectedItem());				// set ontology change set, if any		if (annotatedChangeSet!=null && annotatedChangeSet.size()>0) {			description.setOntologyChangeSet(annotatedChangeSet);		}				// set current annotated entity definition (rendered as String in Swoop)		// similar to addEntityToComparator in TermsDisplay		String timeStamp = swoopModel.getTimeStamp();		String renderText = "<font face=\"Verdana\" size=2><b>Time:</b>&nbsp;</i>"+timeStamp+"</i><br></font";		URI ontURI;		try {			ontURI = swoopModel.getSelectedOntology().getURI();			renderText += "<font face=\"Verdana\" size=2><b>Ontology:</b>&nbsp;" + swoopModel.shortForm(ontURI) + "</font><br>";		} catch (OWLException e) {						e.printStackTrace();		}		if (swoopModel.selectedOWLObject instanceof OWLOntology) {			int rendererIndex = swoopHandler.ontDisplay.ontDescTab.getSelectedIndex();			JEditorPane renderer = (JEditorPane) swoopHandler.ontDisplay.editors.get(rendererIndex);			renderText += renderer.getText();		}		else {			int rendererIndex = swoopHandler.termDisplay.termDisplayPane.getSelectedIndex();			JEditorPane renderer = (JEditorPane) swoopHandler.termDisplay.editors.get(rendererIndex);			renderText += renderer.getText();		}		description.setAnnotatedEntityDefinition(renderText);				// build a new ontology that stores description instance		OWLOntology annotationOntology =  null;		try {						annotationOntology = description.buildOntology();		} catch (AnnoteaException e3) {			e3.printStackTrace();			return;		}				// TESTING://		StringWriter st = new StringWriter();//		CorrectedRDFRenderer rdfRenderer = new CorrectedRDFRenderer(annotationOntology);//		try {//			rdfRenderer.renderOntology(annotationOntology, st);//		} catch (RendererException ex) {//			ex.printStackTrace();//		}//		try {//			FileWriter temp = new FileWriter("temp.xml");//			temp.write(st.toString());//			temp.close();//		}//		catch (IOException exe) {//			exe.printStackTrace();//		}//		System.out.println(st.toString());		// END OF TESTING				// connect to servers and post ontology//	    Authenticator.setDefault (new MyAuthenticator());		Iterator postIter = annotPrefs.serverPList.iterator();		while (postIter.hasNext()) {			// post message to server			URL serverURL;			try {				serverURL = new URL(postIter.next().toString());				AnnoteaClient client = new AnnoteaClient(serverURL, swoopModel);				URL location = client.post(annotationOntology);				description.setLocation(location);				statusLbl.setText("Status: Annotation successfully posted to server");			} catch (Exception e1) {				e1.printStackTrace();				statusLbl.setText("Status: Annotation post failed");				return;			}					}		// hide popupNew and update		popupNew.hide();		clearBody();//		 retrieveAnnotations(); // NO NEED		// just add new annotation to existing annotation set		try {						Set annotSet = annotationCache.getAnnotationSet(annotates[0]);			if (annotSet==null) annotSet = new HashSet();			annotSet.add(description);			annotationCache.putAnnotationSet(annotates[0], annotSet);			this.displayAnnotations();			swoopModel.addAnnotatedObjectURI(annotates[0]);		} 		catch (Exception e1) {			e1.printStackTrace();		}			}		class MyAuthenticator extends Authenticator {	    protected PasswordAuthentication getPasswordAuthentication() {	      final JDialog jd = new JDialog ();	      jd.setTitle("Authentication");	      jd.setModal(true);	      jd.getContentPane().setLayout (new GridLayout (0, 1));	      JLabel jl = new JLabel (getRequestingPrompt());	      jd.getContentPane().add(jl);	      JTextField username = new JTextField();	      username.setFont(tahoma);	      //username.setBackground (Color.lightGray);	      jd.getContentPane().add(createBox("User: ", username));	      JPasswordField password = new JPasswordField();	      password.setFont(tahoma);	      //password.setBackground(Color.lightGray);	      jd.getContentPane().add(createBox("Pwd: ", password));	      JButton jb = new JButton ("OK");	      JButton jc = new JButton("Cancel");	      JPanel btnPanel = new JPanel();	      btnPanel.setLayout(new GridLayout(1,2));	      btnPanel.add(jb);	      btnPanel.add(jc);	      jd.getContentPane().add(btnPanel);	      jb.addActionListener (new ActionListener() {	        public void actionPerformed (ActionEvent e) {	          jd.dispose();	        }	      });	      jc.addActionListener (new ActionListener() {	        public void actionPerformed (ActionEvent e) {	        	showAuthenticator = false;	        	jd.hide();	        }	      });	      	      jd.pack();	      if (showAuthenticator) jd.setVisible(true);	      return new PasswordAuthentication (username.getText(), password.getText().toCharArray());	    }	  }	/**	 * Whenever the annotation node tree selection changes	 * render annotation body in html pane below	 */	public void valueChanged(TreeSelectionEvent e) {				if (e.getSource()==annotTreeTable.getTree()) {			// get selected tree node annotation object			// and render body in htmlCore			if (annotTreeTable.getTree().getSelectionPath()==null) return;			DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) annotTreeTable.getTree().getSelectionPath().getLastPathComponent();			Description selAnnot = (Description) selNode.getUserObject();						// check if its a blank/separator node i.e. no Annotation type			if (selAnnot.getAnnotationType()==null) return;						String html = "";			String header = "<FONT FACE=\"Verdana\" SIZE=3>";						URI annotURI = this.getAnnotationURI(selAnnot.getAnnotates());			header += "<i>Annotation on: <a href=\""+annotURI+"\">"+swoopModel.shortForm(annotURI)+"</a><br>";						// add saved entity definition string, if any			if (selAnnot.getAnnotatedEntityDefinition()!=null) {				String defn = selAnnot.getAnnotatedEntityDefinition();				this.savedDefinitions.put(String.valueOf(defn.hashCode()), defn);				header += "<br><a href=\"<SAVED^" + annotURI + "^"+ defn.hashCode() + "\">See Original Definition</a> (in Resource Holder) when Annotation was made";			}						header += "</i><hr><br>";			header += "<b>Author: </b>" + selAnnot.getAuthor() + "<br>";			try {				String type = Annotea.getEntityName(selAnnot.getAnnotationType().getURI());				header += "<b>Annotation Type: </b>" + type.substring(0, 2).toUpperCase() + "-" + type + "<br>";			} catch (OWLException e1) {				e1.printStackTrace();			}			header += "<b>Date Created: </b>" + selAnnot.getCreated() + "<br><br>";			header += "</FONT>";			html += header;									String subject = this.getSubject(selAnnot);			html += "<head><b>Subject:</b>"+subject+"</head>";			String body = selAnnot.getBody();			if (body!=null) {				body = body.substring(body.indexOf("<body>"), body.length());			}			html += "<br><br>"+body;						// add form processing			// javascript handling			jsHandler.clear();			htmlCore.setText(html);			htmlCore.setCaretPosition(0);						if (selAnnot.getOntologyChangeSet()!=null && selAnnot.getOntologyChangeSet().size()>0) {				annotAttachBtn.setEnabled(true);				annotAttachBtn.setText("Changes");//				annotAttachBtn.setText("["+selAnnot.getOntologyChangeSet().size()+"]");			}			else annotAttachBtn.setEnabled(false);		}	}	public void SwoopSelectionChanged() {				// if context fixed is selected, return		if (fixContextChk.isSelected()) return;				// save last selected annotation, and select it again at the end if present in treetable		Description lastSelAnnot = null;		if (annotTreeTable.getTree().getSelectionPath()!=null) {			DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) annotTreeTable.getTree().getSelectionPath().getLastPathComponent();			lastSelAnnot = (Description) selNode.getUserObject();			}				this.clearAnnotationNodeTree();		htmlCore.setText("");		annotAttachBtn.setEnabled(false);		String status = "Status: Need to Update";		if (swoopModel.selectedOWLObject!=null) {			try {				status += " for " + swoopModel.shortForm(swoopModel.selectedOWLObject.getURI());			} catch (OWLException e) {				e.printStackTrace();			}		}		statusLbl.setText(status);				// check annotation set cache		if (swoopModel.selectedOWLObject!=null) {			this.displayAnnotations();									}				// if auto-retrieve on selection change is checked		// and annotations not in cache		if (swoopModel.getEnableAutoRetrieve()) {			this.retrieveAnnotations();		}		

⌨️ 快捷键说明

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