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: ObjectPropertyAction.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.awt.event.ActionListener;
022 import java.util.*;
023 import javax.swing.*;
024 import javax.swing.tree.DefaultMutableTreeNode;
025
026 /**
027 * Action to create a new ObjectProperty in the ontology
028 */
029 public class ObjectPropertyAction extends AbstractAction implements
030 TreeNodeSelectionListener {
031 private static final long serialVersionUID = 3689632475823551285L;
032
033 public ObjectPropertyAction(String s, Icon icon) {
034 super(s, icon);
035
036 mainPanel = new JPanel(new GridBagLayout());
037 GridBagConstraints gbc = new GridBagConstraints();
038 gbc.insets = new Insets(3, 3, 3, 3);
039 gbc.anchor = GridBagConstraints.WEST;
040
041 mainPanel.add(new JLabel("Name Space:"), gbc);
042 mainPanel.add(nameSpace = new JTextField(30), gbc);
043
044 gbc.gridy = 1;
045 mainPanel.add(new JLabel("Property Name:"), gbc);
046 mainPanel.add(propertyName = new JTextField(30), gbc);
047 mainPanel.add(domainButton = new JButton("Domain"), gbc);
048 mainPanel.add(rangeButton = new JButton("Range"), gbc);
049
050 domainAction = new ValuesSelectionAction();
051 domainButton.addActionListener(new ActionListener() {
052 public void actionPerformed(ActionEvent actionevent) {
053 String as[] = new String[ontologyClassesURIs.size()];
054 for(int i = 0; i < as.length; i++)
055 as[i] = ontologyClassesURIs.get(i);
056 ArrayList<String> arraylist = new ArrayList<String>();
057 for(int j = 0; j < selectedNodes.size(); j++) {
058 DefaultMutableTreeNode defaultmutabletreenode = selectedNodes.get(j);
059 if(((OResourceNode)defaultmutabletreenode.getUserObject())
060 .getResource() instanceof OClass)
061 arraylist.add((((OResourceNode)defaultmutabletreenode
062 .getUserObject()).getResource()).getURI().toString());
063 }
064 String as1[] = new String[arraylist.size()];
065 for(int k = 0; k < as1.length; k++)
066 as1[k] = arraylist.get(k);
067 domainAction.showGUI("Domain", as, as1, false,
068 MainFrame.getIcon("ontology-object-property"));
069 }
070 });
071 rangeAction = new ValuesSelectionAction();
072 rangeButton.addActionListener(new ActionListener() {
073 public void actionPerformed(ActionEvent actionevent) {
074 String as[] = new String[ontologyClassesURIs.size()];
075 for(int i = 0; i < as.length; i++)
076 as[i] = ontologyClassesURIs.get(i);
077 rangeAction.showGUI("Range", as, new String[0], false,
078 MainFrame.getIcon("ontology-object-property"));
079 }
080 });
081 }
082
083 public void actionPerformed(ActionEvent actionevent) {
084 nameSpace.setText(ontology.getDefaultNameSpace() == null ?
085 "http://gate.ac.uk/example#" : ontology.getDefaultNameSpace());
086 JOptionPane pane = new JOptionPane(mainPanel, JOptionPane.QUESTION_MESSAGE,
087 JOptionPane.OK_CANCEL_OPTION,
088 MainFrame.getIcon("ontology-object-property")) {
089 public void selectInitialValue() {
090 propertyName.requestFocusInWindow();
091 propertyName.selectAll();
092 }
093 };
094 pane.createDialog(MainFrame.getInstance(),
095 "New Object Property").setVisible(true);
096 Object selectedValue = pane.getValue();
097 if (selectedValue != null
098 && selectedValue instanceof Integer
099 && (Integer) selectedValue == JOptionPane.OK_OPTION) {
100 String s = nameSpace.getText();
101 if(!Utils.isValidNameSpace(s)) {
102 JOptionPane.showMessageDialog(MainFrame.getInstance(),
103 "Invalid Name Space: " + s + "\nExample: http://gate.ac.uk/example#");
104 return;
105 }
106 if(!Utils.isValidOntologyResourceName(propertyName.getText())) {
107 JOptionPane.showMessageDialog(MainFrame.getInstance(),
108 "Invalid Property Name: " + propertyName.getText());
109 return;
110 }
111 if(ontology.getOResourceFromMap(s + propertyName.getText()) != null) {
112 JOptionPane.showMessageDialog(MainFrame.getInstance(),"<html>" +
113 "Resource <b>" + s+propertyName.getText() + "</b> already exists.");
114 return;
115 }
116 String domainSelectedValues[] = domainAction.getSelectedValues();
117 HashSet<OClass> domainSet = new HashSet<OClass>();
118 for (String domainSelectedValue : domainSelectedValues) {
119 OClass oclass = (OClass)
120 ontology.getOResourceFromMap(domainSelectedValue);
121 domainSet.add(oclass);
122 }
123 String rangeSelectedValues[] = rangeAction.getSelectedValues();
124 HashSet<OClass> rangeSet = new HashSet<OClass>();
125 for (String rangeSelectedValue : rangeSelectedValues) {
126 OClass oclass = (OClass)
127 ontology.getOResourceFromMap(rangeSelectedValue);
128 rangeSet.add(oclass);
129 }
130 ontology.addObjectProperty(new URI(nameSpace.getText()
131 + propertyName.getText(), false), domainSet, rangeSet);
132 }
133 }
134
135 public Ontology getOntology() {
136 return ontology;
137 }
138
139 public void setOntology(Ontology ontology) {
140 this.ontology = ontology;
141 }
142
143 public void selectionChanged(ArrayList<DefaultMutableTreeNode> arraylist) {
144 selectedNodes = arraylist;
145 }
146
147 public ArrayList<String> getOntologyClassesURIs() {
148 return ontologyClassesURIs;
149 }
150
151 public void setOntologyClassesURIs(ArrayList<String> arraylist) {
152 ontologyClassesURIs = arraylist;
153 }
154
155 protected JPanel mainPanel;
156 protected JTextField nameSpace;
157 protected JButton domainButton;
158 protected JButton rangeButton;
159 protected JTextField propertyName;
160 protected ValuesSelectionAction domainAction;
161 protected ValuesSelectionAction rangeAction;
162 protected ArrayList<String> ontologyClassesURIs;
163 protected ArrayList<DefaultMutableTreeNode> selectedNodes;
164 protected Ontology ontology;
165 }
|