ontologydisplay.java

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

JAVA
1,199
字号
			}			if (swoopModel.selectedOntology != null)				swoopHandler.updateOntologyViews();		} else if (event.getType() == ModelChangeEvent.ONTOLOGY_CHANGED) {			this.disableUIListeners();			ontList.repaint();			ontHideBox.repaint();			this.enableUIListeners();			// changes may fix inconsistencies or may introduce new ones			// so update the onotlogy dusplay			displayOntology();		} else if (event.getType() == ModelChangeEvent.EDITABLE_CHANGED) {			System.out.println("editable changed");			refreshEditorMode();		} else if (event.getType() == ModelChangeEvent.ADDED_CHANGE) {			refreshEditorMode();		} else if (event.getType() == ModelChangeEvent.DEBUGGING_CHANGED) {			this.renderOntology();		}	}	/**	 * Refresh the ontology display whenever "editable" changes in SwoopModel	 */	public void refreshEditorMode() {		editorEnabled = swoopModel.getEditorEnabled();		displayOntology();	}	public void actionPerformed(ActionEvent e) {		if (e.getSource() == ontEditableChk) {			refreshEditorMode();		}		if (e.getSource() == hideOntBtn) {			if (hideOntBtn.getToolTipText().indexOf("Expand") >= 0) {				swoopHandler.expandOntPanel();				SwoopIcons swoopIcons = new SwoopIcons();				if (swoopIcons.upIcon != null)					hideOntBtn.setIcon((ImageIcon) swoopIcons.upIcon);				else					hideOntBtn.setText("Collapse");				hideOntBtn.setToolTipText("Collapse Ontology List");			} else {				swoopHandler.collapseOntPanel();				SwoopIcons swoopIcons = new SwoopIcons();				if (swoopIcons.downIcon != null)					hideOntBtn.setIcon((ImageIcon) swoopIcons.downIcon);				else					hideOntBtn.setText("Expand");				hideOntBtn.setToolTipText("Expand Ontology List");			}		}		if (e.getSource() == ontHideBox				&& e.getActionCommand().equals("comboBoxChanged")) {			// select corresponding ontology			OWLOntology ont = (OWLOntology) ontHideBox.getSelectedItem();			selectOntology(ont);		}	}	/**	 * Reload an OWL Ontology from its physical location (URL). Remove the	 * existing entry from SwoopModel and reload/add the new one	 * 	 * @param ont	 */	public void reloadOntologyFromPhysical(OWLOntology ont) {		try {			String msg = "Reloading Ontology from its physical URL - all changes will be lost! Continue?";			int result = JOptionPane.showConfirmDialog(this, msg,					"Reload Ontology", JOptionPane.YES_NO_OPTION);			if (result == JOptionPane.YES_OPTION) {				// reload ontology ** from its physical location **				URI physicalURI = ont.getPhysicalURI();				swoopModel.removeOntology(ont.getURI());				// also remove ontology from cache (tree/list)				swoopHandler.termDisplay.removeFromCache(ont);				// add new ontology and select it				swoopModel.addOntology(physicalURI);				OWLOntology onto = swoopModel.getOntology(ont.getURI());				swoopModel.setSelectedOntology(onto);				// remove changes associated with old ontology				swoopModel.removeChanges(onto);				System.out.println("Ontology reloaded from " + physicalURI);			}		} catch (OWLException ex) {			ex.printStackTrace();		}	}	/**	 * Save Ontology to a Remote Location (Server) that has WebDAV enabled	 * Ontology is stored at location as an RDF/XML file	 */	public void saveRemoteOntology() {		try {			// get current ontology to save			OWLOntology ontology = swoopModel.getSelectedOntology();			URI uri = ontology.getPhysicalURI();			if (!uri.getScheme().matches("https?")) {				uri = ontology.getLogicalURI();			}			if (!uri.getScheme().matches("https?")) {				uri = URI.create(webDavURI);			}			uri = uri.normalize();			uri = new URI(uri.getScheme(), uri.getSchemeSpecificPart(), null);			final String uriFinal = uri.toString();			JTextField urlField = new JTextField(uriFinal);			/* Add browse Web Dav button *///			JButton browse = new JButton("Browse..");//			browse.addActionListener(new ActionListener() {//				public void actionPerformed(ActionEvent e) {//					BrowserControl.displayURL(uriFinal);//				}//			});			JPanel urlPanel = new JPanel();			urlPanel.setLayout(new BorderLayout());			urlPanel.add(urlField, "Center");			//urlPanel.add(browse, "East");			JTextField userField = new JTextField(webDavLogin);			JTextField passwordField = new JPasswordField(webDavPwd);			JPanel panel = new JPanel(new SpringLayout());			panel.add(new JLabel("URL"));			panel.add(urlPanel);			panel.add(new JLabel("User"));			panel.add(userField);			panel.add(new JLabel("Password"));			panel.add(passwordField);			SpringUtilities.makeCompactGrid(panel, 3, 2, // rows, cols					6, 6, // initX, initY					6, 6); // xPad, yPad			int option = JOptionPane.showConfirmDialog(null, new Object[] {					"Server must have accept HTTP PUT or have WebDAV enabled.", panel },					"Save to WebDAV Store", JOptionPane.OK_CANCEL_OPTION,					JOptionPane.PLAIN_MESSAGE);			if (option != JOptionPane.OK_OPTION)				return;			String url = urlField.getText().trim();			if (url == null)				return;			if (url.indexOf("/") >= 0)				webDavURI = url.substring(0, url.lastIndexOf("/") + 1);			webDavLogin = userField.getText().trim();			webDavPwd = passwordField.getText().trim();			// write rdf or html depending on extension			String output = "";			if (url.endsWith(".html")) {				output = swoopHandler.getObjectHTML(swoopModel.selectedOWLObject);			} else {				StringWriter rdfBuffer = new StringWriter();				CorrectedRDFRenderer rdfRenderer = new CorrectedRDFRenderer();				rdfRenderer.renderOntology(ontology, rdfBuffer);				output = rdfBuffer.toString();			}									// write output to webDav location			if (DavUtil.saveString(output, url, webDavLogin, webDavPwd)) {				System.out.println("Ontology saved to " + url);				JOptionPane.showMessageDialog(this, "Ontology Saved at " + url,						"Remote Export Success",						JOptionPane.INFORMATION_MESSAGE);			} else {				System.out.println("Error saving ontology to " + url);				JOptionPane.showMessageDialog(this,						"Unable to Save Ontology Remotely at " + url,						"Remote Export Error", JOptionPane.ERROR_MESSAGE);			}		} catch (Exception e) {			e.printStackTrace();		}	}	public void addNewOntology() {		try {			PopupNew popupPanel = new PopupNew(this, swoopModel, "Ontology",					null);			popupPanel.setLocation(200, 200);			popupPanel.show();		} catch (Exception ex) {			ex.printStackTrace();		}	}	/**	 * Remove an ontology and its imports closure (if specified) from Swoop	 * 	 * @param ont	 */	public void removeOntology(OWLOntology ont, boolean warning) {		try {			int result = -1;			JCheckBox impChk = new JCheckBox(					"Also remove all its imported ontologies (if any)?");			if (warning) {				String title = "Remove OWL Ontology";				String ontName = swoopModel.shortForm(ont.getURI());				if (ontName.indexOf(":") >= 0)					ontName = ontName.substring(ontName.indexOf(":") + 1,							ontName.length());				String msg = "Do you want to remove this ontology ("						+ (ontName) + ") from SWOOP?";				JPanel msgPanel = new JPanel();				msgPanel.setLayout(new GridLayout(2, 1));				msgPanel.add(new JLabel(msg));				msgPanel.add(impChk);				int options = JOptionPane.YES_NO_OPTION;				result = JOptionPane.showConfirmDialog(this, msgPanel, title,						options);			}			if (result == JOptionPane.YES_OPTION || !warning) {				Set ontURISet = new HashSet();				if (impChk.isSelected()) {					for (Iterator iter = OntologyHelper.importClosure(ont)							.iterator(); iter.hasNext();) {						swoopModel.removeOntology(((OWLOntology) iter.next())								.getURI());					}				} else					swoopModel.removeOntology(ont.getURI());			}		} catch (Exception ex) {		}	}	/***************************************************************************	 * Doesn't call SwoopModel Ontology_Sel_Changed Event Instead directly calls	 * updateOntologyDisplay	 */	public void valueChanged(ListSelectionEvent event) {		if (event.getValueIsAdjusting())			return; // multiple notification calls		if (event.getSource() == ontList) {			// save settings for current ontology			swoopModel.saveCurrentOntSettings();			// check for ont list selection			if (ontList.getSelectedValue() != null) {				OWLOntology ont = (OWLOntology) ontList.getSelectedValue();				swoopModel.selectedOWLObject = ont;				swoopModel.selectedOntology = ont;				swoopModel.selectedEntity = null;				// add ontology element to history as well				if (ont != null)					swoopHandler.termDisplay.addToHistory(ont);				// whenever ontology selection changes, need to revert				// to user-specific ontology settings				// *** below also reclassifies ontology!				swoopModel.loadOntSettings(ont);				// updateOntologyViews() refreshes ontology (via				// od.displayOntology()->od.renderOntology())				// swoopHandler.updateOntologyViews();				swoopHandler.ontRemoveMItem.setEnabled(true);			} else {				swoopHandler.ontRemoveMItem.setEnabled(false);			}			swoopHandler.changeLog.refreshChangePane();		}	}	public void selectOntology(OWLOntology ont) {		ontList.clearSelection();		ontList.setSelectedValue(ont, true);		this.disableUIListeners();		ontHideBox.setSelectedItem(ont);		this.enableUIListeners();	}	/**	 * Refreshes the Ontology Display Pane whenever the currently selected	 * ontology in SwoopModel is changed Does not add ontology to History, that	 * is only done in valueChanged (when ontList selection changes)	 */	protected void displayOntology() {		System.out.println("Display ontology info");		OWLOntology ont = swoopModel.getSelectedOntology();		if (ont == null) {			System.out.println(" but no ontology selected");			return;		}		swoopHandler.ontRemoveMItem.setEnabled(true);		try {			// now that ontology is loaded we can update the address bar			swoopHandler.disableUIListeners();			swoopHandler.updateAddressBar(ont.getURI().toString());		} catch (OWLException e) {			e.printStackTrace();		} finally {			swoopHandler.enableUIListeners();		}		if ((ontList.getSelectedIndex() != -1)				&& (!((OWLOntology) ontList.getSelectedValue()).equals(ont))				|| (ontHideBox.getSelectedIndex() != -1 && !((OWLOntology) ontHideBox						.getSelectedItem()).equals(ont))) {			// sometimes when traversing hyperlinks, user reaches element in			// another ontology			// element displayed but ontology is not selected in list			// this fragment corrects the mismatch b/w			// swoopModel.selectedOntology and ontList.selection			this.simplySelectOntology(ont);			// System.out.println("mismatch corrected");		}		for (int index = 0; index < renderers.size(); index++) {			SwoopOntologyRenderer renderer = (SwoopOntologyRenderer) renderers					.get(index);			StringWriter sw = new StringWriter();			JEditorPane editorPane = (JEditorPane) editors.get(index);			if (renderer instanceof SwoopOptionalRenderer)				ontDescTab.setEnabledAt(index,						((SwoopOptionalRenderer) renderer)								.isVisible(swoopModel));			else if (renderer instanceof SwoopSpeciesValidationRenderer) {				// special check for validator - always make it enabled				ontDescTab.setEnabledAt(index, true);			} else if (renderer instanceof SwoopEditableRenderer) {				SwoopEditableRenderer editableRenderer = (SwoopEditableRenderer) renderer;				editableRenderer						.setEditorEnabled(swoopModel.getEditorEnabled());			} else if (swoopModel.getEditorEnabled())				ontDescTab.setEnabledAt(index, false);			else				ontDescTab.setEnabledAt(index, true);			// if(ont == null || !ontDescTab.isEnabledAt(index)) continue;		}		renderOntology();	}	/**	 * This method is called by displayOntology while refreshing ontology pane.	 * It renders the currently selected OWL Ontology by calling the render()	 * method on the appropriate SwoopOntologyRenderer	 * 	 */	public void renderOntology() {		if (ontDescTab.getSelectedIndex() == -1 || renderers.size() == 0				|| editors.size() == 0)			return; // during initialization		OWLOntology ont = swoopModel.getSelectedOntology();		SwoopOntologyRenderer renderer = (SwoopOntologyRenderer) renderers				.get(ontDescTab.getSelectedIndex());		JEditorPane editorPane = (JEditorPane) editors.get(ontDescTab				.getSelectedIndex());		if (ont == null) {			// *** below is critical to avoid weird HTML exception ***			editorPane.setText("<html><body><br/></body></html>");			editorPane.setCaretPosition(0);			return;		}		StringWriter sw = new StringWriter();		try {			renderer.render(ont, swoopModel, sw);			String html = sw.getBuffer().toString();			if (html.length() < 20) {				System.out.println("HTML:");				System.out.println(html); // error checking			} else {				System.out.println("HTML Length: " + html.length());			}			editorPane.setText(html);			editorPane.setCaretPosition(0);		} catch (RendererException e) {			editorPane.setText("");			JOptionPane.showMessageDialog(null,					"An error occured during display:\n" + e.getMessage(),					"Error!", JOptionPane.ERROR_MESSAGE);		}

⌨️ 快捷键说明

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