ontologydisplay.java

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

JAVA
1,199
字号
//The MIT License//// Copyright (c) 2004 Mindswap Research Group, University of Maryland, College Park//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions://// The above copyright notice and this permission notice shall be included in// all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS// IN THE SOFTWARE.package org.mindswap.swoop;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.io.StringWriter;import java.net.URI;import java.net.URL;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;import java.util.TreeSet;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JEditorPane;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.JTabbedPane;import javax.swing.JTextField;import javax.swing.JToolBar;import javax.swing.SpringLayout;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.event.HyperlinkEvent;import javax.swing.event.HyperlinkListener;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import org.mindswap.swoop.popup.PopupAddAnnotation;import org.mindswap.swoop.popup.PopupAddOntology;import org.mindswap.swoop.popup.PopupNew;import org.mindswap.swoop.renderer.SwoopEditableRenderer;import org.mindswap.swoop.renderer.SwoopOntologyRenderer;import org.mindswap.swoop.renderer.SwoopOptionalRenderer;import org.mindswap.swoop.renderer.ontology.OntologyListRenderer;import org.mindswap.swoop.renderer.ontology.SwoopOntologyInfo;import org.mindswap.swoop.renderer.ontology.SwoopSpeciesValidationRenderer;import org.mindswap.swoop.utils.DavUtil;import org.mindswap.swoop.utils.PluginLoader;import org.mindswap.swoop.utils.SwoopStatistics;import org.mindswap.swoop.utils.owlapi.CorrectedRDFRenderer;import org.mindswap.swoop.utils.ui.ExceptionDialog;import org.mindswap.swoop.utils.ui.OntologyComparator;import org.mindswap.swoop.utils.ui.SpringUtilities;import org.mindswap.swoop.utils.ui.SwingWorker;import org.mindswap.swoop.utils.ui.SwoopIcons;import org.mindswap.swoop.utils.ui.SwoopProgressDialog;import org.semanticweb.owl.io.RendererException;import org.semanticweb.owl.model.OWLAnnotationInstance;import org.semanticweb.owl.model.OWLDisjointClassesAxiom;import org.semanticweb.owl.model.OWLException;import org.semanticweb.owl.model.OWLOntology;import org.semanticweb.owl.model.change.AddImport;import org.semanticweb.owl.model.change.OntologyChange;import org.semanticweb.owl.model.change.RemoveAnnotationInstance;import org.semanticweb.owl.model.change.RemoveImport;import org.semanticweb.owl.model.helper.OntologyHelper;public class OntologyDisplay extends SwoopDisplayPanel implements		ActionListener, ListSelectionListener, SwoopModelListener,		HyperlinkListener, ChangeListener, MouseListener {	/*	 * Global UI objects	 */	JButton hideOntBtn;	boolean editorEnabled, reasonerMode;	Font tahoma = new Font("Tahoma", Font.PLAIN, 11);	JCheckBox ontEditableChk;	JList ontList;	public JTabbedPane ontDescTab;	JPopupMenu popupMenu;	JComboBox ontHideBox;	JScrollPane ontListPane;	JToolBar ontToolBar;	String webDavURI = "http://www.mindswap.org/dav/ontologies/";	String webDavLogin = "", webDavPwd = "";	/*	 * Important public fields	 */	SwoopModel swoopModel; // model shared by all swoop components	public SwoopFrame swoopHandler; // handler for SwoopFrame instance	SwoopOntologyInfo species; // used to get OWL Species Validation	/*	 * A list of renderers associated with each tab. The list contains	 * SwoopentityRenderer objects and the index of renderers correspond to	 * index of tabs	 */	List renderers = new ArrayList();	/*	 * A list of renderers associated with each tab. The list contains	 * JEditorPane objects and the index of editors correspond to index of tabs	 */	public List editors = new ArrayList();	// constructor	public OntologyDisplay(SwoopModel model, SwoopFrame swf) {		this.swoopModel = model;		this.swoopHandler = swf;		editorEnabled = false;		reasonerMode = false;		species = new SwoopOntologyInfo();		setupUI();	}	public void setupUI() {		// panel for ontology list header		JLabel ontListLbl = new JLabel(" Ontology List");		ontListLbl.setFont(tahoma);		SwoopIcons swoopIcons = new SwoopIcons();		if (swoopIcons.upIcon != null)			hideOntBtn = new JButton((ImageIcon) swoopIcons.upIcon);		else			hideOntBtn = new JButton("Collapse");		hideOntBtn.setToolTipText("Collapse Ontology List");		hideOntBtn.setFont(tahoma);		hideOntBtn.setBorder(null);		hideOntBtn.addActionListener(this);		// create toolbar for ontology list pane		ontToolBar = new JToolBar();		ontToolBar.add(hideOntBtn);		ontToolBar.add(ontListLbl);		// create row panel for ontology list and description pane		JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);		splitpane.setOneTouchExpandable(true);		ontList = new JList();		ontList.setCellRenderer(new OntologyListRenderer(swoopModel));		ontList.setFont(new Font(swoopModel.getFontFace(), Font.PLAIN, 11));		ontListPane = new JScrollPane(ontList);		/*		 * create ontology combo box, which is hidden by default but appears		 * when collapse is clicked		 */		ontHideBox = new JComboBox();		ontHideBox.setFont(new Font(swoopModel.getFontFace(), Font.PLAIN, 11));		ontHideBox.setEditable(false);		ontHideBox.setRenderer(new OntologyListRenderer(swoopModel));		ontDescTab = new JTabbedPane();		ontDescTab.setFont(tahoma);		// *** critical UI stuff: add change listener after adding tabs		if (!Swoop.isWebStart()) {			PluginLoader ph = PluginLoader.getInstance();			// find all the other classes that implements SwoopOntologyRenderer			// interface and try to generate a renderer for them			List list = ph.getClasses(SwoopOntologyRenderer.class);			for (int i = 0; i < list.size(); i++) {				Class cls = (Class) list.get(i);				if (cls.isInterface())					continue;				SwoopOntologyRenderer renderer = null;				// if is abstract class, don't try to instantiate. see JVM specs				// Table 4.1:				// (http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75734)				if ((cls.getModifiers() >> 10) != 1) {					try {						System.out.println("Try creating renderer "								+ cls.getName());						// if						// (cls.getName().equals("org.mindswap.swoop.SwoopSpeciesValidator"))						// continue;						renderer = (SwoopOntologyRenderer) cls.newInstance();						renderers.add(renderer);					} catch (Throwable e) {						e.printStackTrace();						continue;					}				}			}		} else {			// manually load Ontology Renderers for the webstart			renderers.add(new SwoopOntologyInfo());			renderers.add(new SwoopSpeciesValidationRenderer());		}		// sort the renderers such that SwoopOntologyInfo is always first and		// the		// rest is ordered alphabetically		Collections.sort(renderers, new Comparator() {			public int compare(Object o1, Object o2) {				SwoopOntologyRenderer r1 = (SwoopOntologyRenderer) o1;				SwoopOntologyRenderer r2 = (SwoopOntologyRenderer) o2;				if (r1 instanceof SwoopOntologyInfo)					return -1;				if (r2 instanceof SwoopOntologyInfo)					return 1;				return r1.getName().compareTo(r2.getName());			}		});		// add the editor for each renderer		for (int i = 0; i < renderers.size(); i++) {			SwoopOntologyRenderer renderer = (SwoopOntologyRenderer) renderers					.get(i);			Component component = renderer.getDisplayComponent(this);			ontDescTab.addTab(renderer.getName(), new JScrollPane(component));			if (renderers.get(i) instanceof SwoopOptionalRenderer)				ontDescTab.setEnabledAt(i, false);			editors.add(component);		}		JPanel chkPanel = new JPanel();		chkPanel.setLayout(new BorderLayout());		ontEditableChk = new JCheckBox("Editable");		ontEditableChk.setFont(tahoma);		ontEditableChk.addActionListener(this);		JPanel rightOntPanel = new JPanel();		rightOntPanel.setLayout(new BorderLayout());		rightOntPanel.add(ontDescTab, "Center");		splitpane.setRightComponent(rightOntPanel);		// add components to main panel		setLayout(new BorderLayout());		add(splitpane, "Center");		// add listeners at the end of setup!		ontList.addListSelectionListener(this);		ontList.addMouseListener(this);		ontHideBox.addActionListener(this);		ontHideBox.addMouseListener(this);		ontDescTab.addChangeListener(this);		refreshOntList();	}	public void disableUIListeners() {		// for (int safe=0; safe<5; safe++) {		ontList.removeListSelectionListener(this); // change ontList selection		// causes rendering ontology		// info		ontHideBox.removeActionListener(this);		// }	}	public void enableUIListeners() {		// below is a safety mechanism to ensure that multiple copies of		// listener aren't added		this.disableUIListeners();		// turn on listeners		ontList.addListSelectionListener(this);		ontHideBox.addActionListener(this);	}	/**	 * Useful method for selecting an OWL Ontology without activating any of the	 * listeners. Simply refreshes UI of the list and selects ontology	 * (updateUI() throws funky exceptions)	 * 	 * @param ont	 */	public void simplySelectOntology(OWLOntology ont) {		this.disableUIListeners();		refreshOntList();		if (ont != null)			ontList.setSelectedValue(ont, true);		else			ontList.clearSelection();		refreshOntBox();		ontHideBox.setSelectedItem(ont);		this.enableUIListeners();	}	public void refreshOntList() {		// display ontology list based on swoopModel's ontologies		Set sortedOntSet = new TreeSet(OntologyComparator.INSTANCE);		sortedOntSet.addAll(swoopModel.getOntologies());		ontList.setFont(new Font(swoopModel.getFontFace(), Font.PLAIN, 11));		ontList.setListData(sortedOntSet.toArray());		// we don't need the following line, it is causing an error		// ontList.updateUI();	}	/**	 * Refresh ontology combo box each time an ontology is added/removed from	 * SwoopModel	 */	public void refreshOntBox() {		ontHideBox.removeAllItems();		ontHideBox.setFont(new Font(swoopModel.getFontFace(), Font.PLAIN, 11));		for (int i = 0; i < ontList.getModel().getSize(); i++) {			OWLOntology ont = (OWLOntology) ontList.getModel().getElementAt(i);			ontHideBox.addItem(ont);		}	}	public void modelChanged(ModelChangeEvent event) {		if (event.getType() == ModelChangeEvent.ONT_STATS_CHANGED) {			renderOntology();		} else if (event.getType() == ModelChangeEvent.QNAME_VIEW_CHANGED) {			// System.out.println("OntologyDisplay: QName changed to: " +			// swoopModel.showQNames());			displayOntology();		} else if (event.getType() == ModelChangeEvent.ONTOLOGY_LOADED) {			// refresh ontology list/box			this.disableUIListeners();			refreshOntList();			refreshOntBox();			this.enableUIListeners();			// add ontology element to history as well			if (swoopModel.getSelectedOntology() != null)				swoopHandler.termDisplay.addToHistory(swoopModel						.getSelectedOntology());			// finally, revert UI ontology settings (qnames, imports, reasoner)			swoopHandler.termDisplay.revertUIOntologySettings();		} else if (event.getType() == ModelChangeEvent.ONTOLOGY_REMOVED) {			this.simplySelectOntology(null);		} else if (event.getType() == ModelChangeEvent.CLEAR_SELECTIONS) {			// clear the entire ontology display pane			ontList.clearSelection();			ontHideBox.setSelectedIndex(-1);			for (int i = 0; i < ontDescTab.getTabCount(); i++) {				JEditorPane currEditor = (JEditorPane) editors.get(i);				currEditor.setText("");			}		} else if (event.getType() == ModelChangeEvent.ONTOLOGY_SEL_CHANGED				|| event.getType() == ModelChangeEvent.REASONER_SEL_CHANGED) {			// in this case, don't need to loadOntSettings			// because it has already been loaded in			// swoopModel.setSelectedOntology that fires ONT_SEL_CHANGED			if (swoopModel.selectedEntity != null) {				// ?? still need to refresh ontology info pane when entity is				// selected				renderOntology();

⌨️ 快捷键说明

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