001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Niraj Aswani, 09/March/07
011 *
012 * $Id: InstanceAction.html,v 1.0 2007/03/09 16:13:01 niraj Exp $
013 */
014 package gate.gui.ontology;
015
016 import gate.creole.ontology.*;
017 import gate.gui.MainFrame;
018
019 import java.awt.*;
020 import java.awt.event.ActionEvent;
021 import java.util.*;
022 import javax.swing.*;
023 import javax.swing.tree.DefaultMutableTreeNode;
024
025 /**
026 * Action to create a new Instance in the ontology
027 */
028 public class InstanceAction extends AbstractAction implements
029 TreeNodeSelectionListener {
030 private static final long serialVersionUID = 3257844402729529651L;
031
032 public InstanceAction(String caption, Icon icon) {
033 super(caption, icon);
034 mainPanel = new JPanel(new GridBagLayout());
035 GridBagConstraints gbc = new GridBagConstraints();
036 gbc.insets = new Insets(3, 3, 3, 3);
037 gbc.anchor = GridBagConstraints.WEST;
038
039 mainPanel.add(new JLabel("Name Space:"), gbc);
040 mainPanel.add(nameSpace = new JTextField(30), gbc);
041
042 gbc.gridy = 1;
043 mainPanel.add(new JLabel("Instance Name:"), gbc);
044 mainPanel.add(instanceName = new JTextField(30), gbc);
045 }
046
047 public void actionPerformed(ActionEvent actionevent) {
048 OResource selectedNode = ((OResourceNode)selectedNodes.get(0)
049 .getUserObject()).getResource();
050 String ns = selectedNode.getURI().getNameSpace();
051 if(gate.creole.ontology.Utils.hasSystemNameSpace(
052 selectedNode.getURI().toString())) {
053 ns = ontology.getDefaultNameSpace();
054 }
055 nameSpace.setText(ns);
056
057 nameSpace.setText(ontology.getDefaultNameSpace() == null ?
058 "http://gate.ac.uk/example#" : ontology.getDefaultNameSpace());
059 JOptionPane pane = new JOptionPane(mainPanel, JOptionPane.QUESTION_MESSAGE,
060 JOptionPane.OK_CANCEL_OPTION, MainFrame.getIcon("ontology-instance")) {
061 public void selectInitialValue() {
062 instanceName.requestFocusInWindow();
063 instanceName.selectAll();
064 }
065 };
066 pane.createDialog(MainFrame.getInstance(),"New Instance").setVisible(true);
067 Object selectedValue = pane.getValue();
068 if (selectedValue != null
069 && selectedValue instanceof Integer
070 && (Integer) selectedValue == JOptionPane.OK_OPTION) {
071 String s = nameSpace.getText();
072 if (!Utils.isValidNameSpace(s)) {
073 JOptionPane.showMessageDialog(MainFrame.getInstance(),
074 "Invalid Name Space: " + s + "\nExample: http://gate.ac.uk/example#");
075 return;
076 }
077 if(!Utils.isValidOntologyResourceName(instanceName.getText())) {
078 JOptionPane.showMessageDialog(MainFrame.getInstance(),
079 "Invalid Instance: " + instanceName.getText());
080 return;
081 }
082 if(ontology.getOResourceFromMap(s + instanceName.getText()) != null) {
083 JOptionPane.showMessageDialog(MainFrame.getInstance(),"<html>" +
084 "Resource <b>" + s+instanceName.getText() + "</b> already exists.");
085 return;
086 }
087
088 for(int i = 0; i < selectedNodes.size(); i++) {
089 Object obj = ((OResourceNode)selectedNodes.get(i)
090 .getUserObject()).getResource();
091 if(obj instanceof OClass) {
092 ontology.addOInstance(new URI(nameSpace.getText()
093 + instanceName.getText(), false), (OClass)obj);
094 }
095 }
096 }
097 }
098
099 public Ontology getOntology() {
100 return ontology;
101 }
102
103 public void setOntology(Ontology ontology) {
104 this.ontology = ontology;
105 }
106
107 public void selectionChanged(ArrayList<DefaultMutableTreeNode> arraylist) {
108 selectedNodes = arraylist;
109 }
110
111 JTextField nameSpace;
112 JTextField instanceName;
113 JPanel mainPanel;
114 Ontology ontology;
115 ArrayList<DefaultMutableTreeNode> selectedNodes;
116 }
|