popupnew.java

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

JAVA
645
字号
package org.mindswap.swoop.popup;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.net.URI;import java.net.URL;import java.util.Iterator;import java.util.Set;import java.util.TreeSet;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.SpringLayout;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import org.mindswap.swoop.ModelChangeEvent;import org.mindswap.swoop.OntologyDisplay;import org.mindswap.swoop.SwoopModel;import org.mindswap.swoop.SwoopModelListener;import org.mindswap.swoop.TermsDisplay;import org.mindswap.swoop.renderer.SwoopCellRenderer;import org.mindswap.swoop.utils.ui.EntityComparator;import org.mindswap.swoop.utils.ui.SpringUtilities;import org.semanticweb.owl.io.vocabulary.OWLVocabularyAdapter;import org.semanticweb.owl.model.OWLAnnotationProperty;import org.semanticweb.owl.model.OWLClass;import org.semanticweb.owl.model.OWLDataFactory;import org.semanticweb.owl.model.OWLDataProperty;import org.semanticweb.owl.model.OWLEntity;import org.semanticweb.owl.model.OWLException;import org.semanticweb.owl.model.OWLObjectProperty;import org.semanticweb.owl.model.OWLOntology;import org.semanticweb.owl.model.OWLProperty;import org.semanticweb.owl.model.change.AddAnnotationInstance;import org.semanticweb.owl.model.change.AddEntity;import org.semanticweb.owl.model.change.ChangeVisitor;import org.semanticweb.owl.model.helper.OWLBuilder;/** * @author Aditya * This class pops up whenever the user needs to add a New Ontology, Class, * Property or Individual  */public class PopupNew extends JFrame implements ActionListener, DocumentListener, KeyListener, SwoopModelListener {	Font tahoma = new Font("Tahoma", Font.PLAIN, 11);	String type;	JButton addBtn, cancelBtn;	JTextField idFld, uriFld, labelFld;	JTextArea commentArea;	JComboBox headerBox, propType, parentBox;	String NEWLINE = System.getProperty("line.separator");	OWLOntology ontology;	OWLEntity newEntity;	SwoopModel swoopModel;	JPanel SwoopHandler;	OWLEntity lastSelectedParent;	private JButton addCloseBtn;		public PopupNew(JPanel handler, SwoopModel swoopModel, String type, OWLOntology ontology) {				this.SwoopHandler = handler;		this.swoopModel = swoopModel;		this.type = type;		if (!type.equals("Ontology")) this.ontology = ontology;		this.lastSelectedParent = swoopModel.getSelectedEntity();		setDefaultCloseOperation(DISPOSE_ON_CLOSE);		//setModal(true);		try {			setupUI();					}		catch (Exception ex) {			ex.printStackTrace();		}	}		private void setupUI() throws OWLException {				JPanel mainPanel = new JPanel(new SpringLayout());				JLabel headerLbl = new JLabel();		headerBox = new JComboBox();		headerBox.setFont(tahoma);				headerLbl.setFont(tahoma);		if (type.equals("Ontology")) {			headerLbl.setText("Adding OWL Ontology");			headerBox.addItem("Adding OWL Ontology");		}		else if (type.equals("Class")) {			headerLbl.setText("Adding OWL Class");			headerBox.addItem("Adding OWL Class");			headerBox.addItem("Adding OWL Property");			headerBox.addItem("Adding OWL Individual");		}		else if (type.equals("Property")) {			headerLbl.setText("Adding OWL Property");			headerBox.addItem("Adding OWL Property");			headerBox.addItem("Adding OWL Class");			headerBox.addItem("Adding OWL Individual");		}		else if (type.equals("Individual")) {			headerLbl.setText("Adding OWL Individual");						headerBox.addItem("Adding OWL Individual");			headerBox.addItem("Adding OWL Class");			headerBox.addItem("Adding OWL Property");		}		headerBox.addActionListener(this);				mainPanel.add(new JLabel(""));		mainPanel.add(headerBox);				JLabel propLbl = new JLabel("Property Type:");		propLbl.setFont(tahoma);		propType = new JComboBox();		propType.setFont(tahoma);		propType.addItem("OWL Datatype Property");		propType.addItem("OWL Object Property");		propType.addItem("OWL Annotation Property");				//*****************************************		//Added for Econn		//*****************************************		propType.addItem("OWL Link Property");						//****************************************		propType.addActionListener(this);				propLbl.setLabelFor(propType);		if (type.equals("Property")) {			mainPanel.add(propLbl);			mainPanel.add(propType);			propType.setSelectedIndex(1); // DEFAULT PROPERTY TYPE		}						// add parent box		parentBox = new JComboBox();		parentBox.setFont(tahoma);		parentBox.setRenderer(new SwoopCellRenderer(swoopModel));		JLabel parentLbl = new JLabel();		parentLbl.setFont(tahoma);		if (type.equals("Class")) parentLbl.setText("subClass-of");		else if (type.equals("Property")) parentLbl.setText("subProperty-of");		else parentLbl.setText("Instance-of");		if (!type.equals("Ontology")) {			mainPanel.add(parentLbl);			mainPanel.add(parentBox);		}		fillParentBox();				JLabel idLbl = new JLabel("ID:");		if (type.equals("Ontology")) idLbl.setText("Version-info:");		idLbl.setFont(tahoma);		idFld = new JTextField();		idFld.setFont(tahoma);		idFld.getDocument().addDocumentListener(this);		idFld.addKeyListener(this);		idLbl.setLabelFor(idFld);		mainPanel.add(idLbl);		mainPanel.add(idFld);				JLabel uriLbl = new JLabel("Logical URI:");		uriLbl.setFont(tahoma);		uriFld = new JTextField();		uriFld.setFont(tahoma);		uriFld.addKeyListener(this);		if (!type.equals("Ontology")) uriFld.setText(ontology.getLogicalURI().toString()+"#");		else uriFld.setText("");		uriLbl.setLabelFor(uriFld);		mainPanel.add(uriLbl);		mainPanel.add(uriFld);				JLabel lbl = new JLabel("Label:");		lbl.setFont(tahoma);		labelFld = new JTextField("");		labelFld.setFont(tahoma);		labelFld.addKeyListener(this);		lbl.setLabelFor(labelFld);		mainPanel.add(lbl);		mainPanel.add(labelFld);				JLabel comm = new JLabel("Comment:");		comm.setFont(tahoma);		commentArea = new JTextArea();		commentArea.setFont(tahoma);		commentArea.setText(NEWLINE+NEWLINE+NEWLINE);		commentArea.setCaretPosition(0);		commentArea.addKeyListener(this);		JScrollPane commentPane = new JScrollPane(commentArea);		mainPanel.add(comm);		comm.setLabelFor(commentPane);		mainPanel.add(commentPane);						addBtn = new JButton("Add");		addBtn.setFont(tahoma);		addBtn.setEnabled(false);		addBtn.addActionListener(this);		addCloseBtn = new JButton("Add & Close");		addCloseBtn.setFont(tahoma);		addCloseBtn.setEnabled(false);		addCloseBtn.addActionListener(this);		cancelBtn = new JButton("Cancel");		cancelBtn.setFont(tahoma);		cancelBtn.addActionListener(this);		JPanel btnPanel = new JPanel();		btnPanel.setLayout(new GridLayout(1,2));		btnPanel.add(addBtn);		btnPanel.add(addCloseBtn);		btnPanel.add(cancelBtn);		mainPanel.add(new JLabel(""));		mainPanel.add(btnPanel);				int rows = 6;		if (type.equals("Class") || type.equals("Individual")) rows = 7;		if (type.equals("Property")) rows = 8;				SpringUtilities.makeCompactGrid(mainPanel,                rows, 2, //rows, cols                6, 6,        //initX, initY                6, 6);       //xPad, yPad				Container content = getContentPane();		content.setLayout(new BorderLayout());		content.add(mainPanel, "Center");				if (type.equals("Ontology")) {			setSize(350, 230);			setResizable(false);			setTitle("New OWL Ontology");			headerBox.setVisible(false);		}		else {			if (type.equals("Class")) {				setSize(350, 260);			}			else if (type.equals("Property")){				setSize(350, 290);			}			else if (type.equals("Individual")){				setSize(350, 265);			}			setResizable(true);			setTitle("New Entity");			headerBox.setVisible(true);					}			}		private JPanel createRowPanel(String lblStr, Component comp) {		JPanel panel = new JPanel();		panel.setLayout(new BorderLayout());		JLabel lbl = new JLabel(lblStr);		lbl.setFont(tahoma);		panel.add(lbl, "West");		panel.add(comp, "Center");		return panel;	}		public void actionPerformed(ActionEvent e) {				if (e.getSource()==parentBox && e.getActionCommand().equals("comboBoxChanged")) {						if (parentBox.getSelectedItem() instanceof OWLDataProperty) {				propType.setSelectedIndex(0);							}			else if (parentBox.getSelectedItem() instanceof OWLObjectProperty) {				//******************************************				//Changed for Econnections				//*********************************************				if(((OWLObjectProperty)parentBox.getSelectedItem()).isLink())					propType.setSelectedIndex(3);				else					propType.setSelectedIndex(1);				//************************************************							}					}				if (e.getSource()==propType && e.getActionCommand().equals("comboBoxChanged")) {			if (propType.getSelectedIndex()==2) {				// annotation property selected, disable parentBox				if (parentBox!=null) parentBox.setEnabled(false);			}			else if (parentBox!=null) parentBox.setEnabled(true);		}				if (e.getSource()==headerBox && e.getActionCommand().equals("comboBoxChanged")) {			// if header box selection is not same as current type			if ((!type.equals("Ontology")) && (headerBox.getSelectedItem().toString().indexOf(type)==-1)) {				// switch type 				String newType = headerBox.getSelectedItem().toString();				if (newType.indexOf("Class")>=0) {					type = "Class"; 					((TermsDisplay) SwoopHandler).termTabPane.setSelectedIndex(0);				}				else if (newType.indexOf("Property")>=0){					type = "Property";					((TermsDisplay) SwoopHandler).termTabPane.setSelectedIndex(1);				}				else if (newType.indexOf("Individual")>=0){					type = "Individual";					((TermsDisplay) SwoopHandler).termTabPane.setSelectedIndex(2);				}								this.redrawUI();			}		}				if (e.getSource()==addBtn || e.getSource() == addCloseBtn) {

⌨️ 快捷键说明

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