annotearenderer.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,645 行 · 第 1/4 页
JAVA
1,645 行
// re-select lastSelAnnotation if possible if (lastSelAnnot!=null) { // cycle through tree table and select it DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) annotTreeTable.getTree().getModel().getRoot(); DefaultMutableTreeNode tempNode = rootNode; while (tempNode!=null) { Description desc = (Description) tempNode.getUserObject(); if (desc.equals(lastSelAnnot)) { annotTreeTable.getTree().setSelectionPath(new TreePath(tempNode.getPath()));// annotTreeTable.getTree().scrollPathToVisible(new TreePath(tempNode.getPath())); break; } tempNode = tempNode.getNextNode(); } } } public void modelChanged(ModelChangeEvent event) { if (event.getType()==ModelChangeEvent.AUTORETRIEVE_CHANGED) { if (swoopModel.getEnableAutoRetrieve()) autoRetLbl.setText("(Auto-Update : ON, Ctrl-U to toggle setting)"); else autoRetLbl.setText("(Auto-Update : OFF, Ctrl-U to toggle setting)"); this.SwoopSelectionChanged(); } else if (event.getType()==ModelChangeEvent.ONTOLOGY_SEL_CHANGED || event.getType()==ModelChangeEvent.ENTITY_SEL_CHANGED || event.getType()==ModelChangeEvent.ONTOLOGY_LOADED ) { this.SwoopSelectionChanged(); } else if (event.getType()==ModelChangeEvent.ONTOLOGY_REMOVED || event.getType()==ModelChangeEvent.CLEAR_SELECTIONS) { this.clearAnnotationNodeTree(); } else if (event.getType() == ModelChangeEvent.ANNOTATION_CACHE_CHANGED) { // get swoop annotation cache this.annotationCache = swoopModel.getAnnotationCache(); this.SwoopSelectionChanged(); } } /** * Remove annotation from Annotea Server * @param desc - Description object to be deleted */ public void deleteAnnotation(Description desc) { try { statusLbl.setText("Status: Deleting Annotation"); String title = "Delete Annotation"; String msg = "Do you want to delete this annotation from the server?"; int options = JOptionPane.YES_NO_OPTION; int result = JOptionPane.showConfirmDialog(this, msg, title, options); if(result==JOptionPane.YES_OPTION) { Cursor currentCursor = this.getCursor(); this.setCursor(new Cursor(Cursor.WAIT_CURSOR)); URL location = desc.getLocation(); //TODO: currently only deletes from default annotatea server, not generic case URL serverURL = new URL(annotPrefs.serverRList.iterator().next().toString()); AnnoteaClient client = new AnnoteaClient(serverURL, swoopModel); client.delete(location); htmlCore.setText(""); annotAttachBtn.setEnabled(false);// retrieveAnnotations(); // NO NEED // just remove existing annotation to existing annotation set try { URI annotURI = this.getAnnotationURI(desc.getAnnotates()); Set annotSet = annotationCache.getAnnotationSet(annotURI); if (annotSet!=null) annotSet.remove(desc); else annotSet = new HashSet(); annotationCache.putAnnotationSet(annotURI, annotSet); this.displayAnnotations(); if (annotSet.size()==0) { swoopModel.getAnnotatedObjectURIs().remove(annotURI); } } catch (Exception e1) { e1.printStackTrace(); } finally { this.setCursor(currentCursor); } } } catch (Exception ex) { statusLbl.setText("Status: Delete ERROR - see console log"); ex.printStackTrace(); } } /** * Update Popup New Annotation frame with the current date * */ public void updatePopupDate() { Calendar cal = Calendar.getInstance(TimeZone.getDefault()); String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT); sdf.setTimeZone(TimeZone.getDefault()); dateFld.setText(sdf.format(cal.getTime())); } public void hyperlinkUpdate(HyperlinkEvent e) {// if (e.getSource()==ekitCore.getTextPane() || // e.getSource()==htmlCore) { String hLink = e.getDescription(); //DISABLE TOOLTIPS BECAUSE OF DOUBLE CLICK MESSAGE// if (isURI(hLink)) {// if (e.getSource()==ekitCore.getTextPane()) ekitCore.getTextPane().setToolTipText(hLink);// // else htmlCore.setToolTipText(hLink);// }// else if (hLink.startsWith("ESC")) {// // escape Swoop and open link in web browser// htmlCore.setToolTipText(hLink.substring(3, hLink.length()));// } if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { if (isURI(hLink)) { try { URI uri = new URI(hLink); try { // if entity already displayed, return if (uri.equals(swoopModel.selectedOWLObject.getURI())) return; } catch (OWLException e2) { e2.printStackTrace(); } if (swoopModel.getOntologyURIs().contains(uri)) { // select ontology in main swoop UI swoopHandler.ontDisplay.selectOntology(swoopModel.getOntology(uri)); } else { swoopHandler.termDisplay.selectEntity(hLink); } } catch (URISyntaxException e1) { e1.printStackTrace(); } } else if (hLink.startsWith("<ESC")) { // open in a standard web browser? hLink = hLink.substring(3, hLink.length()); BrowserControl.displayURL(hLink); } else if (hLink.startsWith("<SAVED")) { // link points to saved entity defn String annotURI = hLink.substring(hLink.indexOf("^")+1, hLink.lastIndexOf("^")); String hashCode = hLink.substring(hLink.lastIndexOf("^")+1, hLink.length()); String defn = this.savedDefinitions.get(hashCode).toString(); try { // add saved defn to resource holder! swoopHandler.termDisplay.comparator.addEntity("Original Definition of Annotated Entity", new URI(annotURI), defn); } catch (URISyntaxException e1) { e1.printStackTrace(); } } } //} } public boolean isURI(String str) { try { URL uri = new URL(str); } catch (Exception ex) { return false; } return true; } public void mouseClicked(MouseEvent e) { // maximize i.e. popup new window with annotation message // when user double clicks on htmlCore if (e.getSource()==htmlCore) { // System.out.println(e.getButton()+":"+e.getClickCount()); if (e.getButton()==1 && e.getClickCount()==2) { JFrame maxAnnot = new JFrame("Annotation Message"); Container content = maxAnnot.getContentPane(); content.setLayout(new BorderLayout()); //JEditorPane maxPane = new JEditorPane(); maxPane = new HTMLPane(); maxPane.addActionListener(this); // javascript handler JavaScriptHandler maxJSHandler = new JavaScriptHandler(); maxPane.addTag("javascript", maxJSHandler); maxPane.setContentType("text/html"); maxPane.setEditable(false); maxPane.addHyperlinkListener(this); maxPane.setText(htmlCore.getText()); maxPane.setCaretPosition(0); content.add(new JScrollPane(maxPane), "Center"); maxAnnot.setSize(600, 600); maxAnnot.setLocation(100, 100); maxAnnot.show(); } } } public void mousePressed(MouseEvent arg0) { } public void mouseReleased(MouseEvent arg0) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent arg0) { } public void keyTyped(KeyEvent arg0) { } public void keyPressed(KeyEvent e) { if (e.getSource()==ekitCore.getTextPane()) { // user presses CTRL-L in Ekit pane, then // auto insert hyperlink corresponding to last word if (e.getKeyCode()==76 && e.getModifiers()==2) { Document doc = ekitCore.getTextPane().getDocument(); String docText = ""; try { docText = doc.getText(0, doc.getLength()); int caretPos = ekitCore.getTextPane().getCaretPosition(); String txt = docText.substring(0, caretPos); StringTokenizer tokens = new StringTokenizer(txt, " "); String lastWord = ""; while (tokens.hasMoreTokens()) { lastWord = tokens.nextToken(); } //TESTING: // System.out.println(lastWord); // weird hack to remove funny character present in some strings (is inserted when two hyperlinkable words follow each other) int funnyCharIndex = -1; for (int i=0; i<lastWord.length(); i++) { if (lastWord.charAt(i)==160) { funnyCharIndex = i; } } if (funnyCharIndex!=-1) lastWord = lastWord.substring(funnyCharIndex+1, lastWord.length()); //** end of weird hack OWLOntology ont = swoopModel.getSelectedOntology(); String uriLink = ""; Set allURIs = OntologyHelper.allURIs(ont); for (Iterator iter = allURIs.iterator(); iter.hasNext(); ) { String uri = iter.next().toString(); if (uri.endsWith("#"+lastWord) || uri.endsWith("/"+lastWord)) { uriLink = uri; break; } } // System.out.println(uriLink); if (uriLink.equals("")) { // return; // NO MATCHING URI FOUND // check if lastWord a valid URI if (this.isURI(lastWord)) { uriLink = "<ESC"+lastWord; //ESC is used to skip Swoop and open link in standard web browser } else return; } String insertLink = " <a href=\""+uriLink+"\">" + lastWord + "</a> "; String htmlText = ekitCore.getTextPane().getText(); htmlText = htmlText.replaceAll(" ", " "); htmlText = htmlText.replaceAll(" "+ lastWord, insertLink); ekitCore.getTextPane().setText(htmlText); } catch (Exception e1) { e1.printStackTrace(); } } } } /** * Return a set of Annotations (descriptions) on all Annotated OWLEntities * present in the OWLOntology ont passed to it as an argument * @param ont * @return */ private Set getAllEntityAnnotations(OWLOntology ont, OWLEntity skip) { Set entityDescSet = new HashSet(); try { Set annotObjURIs = swoopModel.getAnnotatedObjectURIs(); for (Iterator iter = annotObjURIs.iterator(); iter.hasNext();) { URI uri = (URI) iter.next(); if ((ont.getClass(uri)!=null || ont.getDataProperty(uri)!=null || ont.getObjectProperty(uri)!=null || ont.getIndividual(uri)!=null) && (skip==null || !uri.equals(skip.getURI()))) { entityDescSet.addAll(new HashSet(annotationCache.getAnnotationSet(uri))); } } } catch (OWLException ex) { ex.printStackTrace(); } return entityDescSet; } /** * Refresh the display of the JTreetable containing Annotations * *from the annotation cache*. Take into account whether displayAll * checkbox is selected or not. */ private void displayAnnotations() { try { // get main annotations on current selected object in Swoop OWLNamedObject currObj = swoopModel.selectedOWLObject; Set mainSet = new HashSet(); if (currObj!=null && annotationCache.getAnnotationSet(currObj.getURI())!=null) { mainSet = new HashSet(annotationCache.getAnnotationSet(currObj.getURI())); } // get annotations on ontology Set extraSet = new HashSet(); if (displayAll.isSelected()) { OWLOntology ont = swoopModel.getSelectedOntology(); if (ont!=null) { if (annotationCache.getAnnotationSet(ont.getURI())!=null) extraSet = new HashSet(annotationCache.getAnnotationSet(ont.getURI())); OWLEntity skip = null; if (!currObj.getURI().equals(ont.getURI())) skip = (OWLEntity) currObj; else extraSet = new HashSet(); // don't include ontology annotations in extraSet while viewing ont extraSet.addAll(this.getAllEntityAnnotations(ont, skip)); } } // finally call setAnnotationNodeTree to display tree this.setAnnotationNodeTree(mainSet, extraSet); } catch(OWLException ex) { ex.printStackTrace(); } } public void keyReleased(KeyEvent arg0) { } private DefaultMutableTreeNode getSeparatorNode() { Description separator = new Description(); separator.setAuthor("-----------"); separator.setBody("----------------------------"); separator.setCreated("------"); return new DefaultMutableTreeNode(separator); } private URI getAnnotationURI(URI[] annotates) { return annotates[0]; } /** * Normalized annotates so that actual OWLNamedObject being annotated * is present in annotates[0] and Ontology is in annotates[1] * Also, if ontology *itself* was annotated, annotates[1] = null * * @param descSet - set of Description objects * @return */ private void normalizeAnnotates(Set descSet) { if (descSet!=null) { for (Iterator iter = descSet.iterator(); iter.hasNext();) { try { Description desc = (Description) iter.next(); URI[] annotates = desc.getAnnotates(); if (annotates.length>1 && annotates[1]!=null && annotates[0]!=null) { if (!annotates[0].equals(annotates[1]) && annotates[0].equals(swoopModel.getSelectedOntology().getURI())) { // swap the two URI swap = new URI(annotates[0].toString()); annotates[0] = new URI(annotates[1].toString()); annotates[1] = swap; } } else { // Tricky! Suppose annotation is on ontology // then only one annotates value is returned // System.out.println(desc); } desc.setAnnotates(annotates); } catch (Exception ex) { ex.printStackTrace(); } } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?