DatatypePropertyAction.java
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: DatatypePropertyAction.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 datatype property.
028  */
029 public class DatatypePropertyAction extends AbstractAction implements
030                                                           TreeNodeSelectionListener {
031   private static final long serialVersionUID = 3257852073457235252L;
032 
033   public DatatypePropertyAction(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(3333);
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("Data Type:"), gbc);
046     mainPanel.add(datatypesComboBox = new JComboBox(), gbc);
047     mainPanel.add(datatypesComboBox, gbc);
048 
049     gbc.gridy = 2;
050     mainPanel.add(new JLabel("Property Name:"), gbc);
051     mainPanel.add(propertyName = new JTextField(30), gbc);
052     mainPanel.add(domainButton = new JButton("Domain"), gbc);
053 
054     datatypesComboBox.setModel(new DefaultComboBoxModel(new String[] {
055         "http://www.w3.org/2001/XMLSchema#boolean",
056         "http://www.w3.org/2001/XMLSchema#byte",
057         "http://www.w3.org/2001/XMLSchema#date",
058         "http://www.w3.org/2001/XMLSchema#decimal",
059         "http://www.w3.org/2001/XMLSchema#double",
060         "http://www.w3.org/2001/XMLSchema#duration",
061         "http://www.w3.org/2001/XMLSchema#float",
062         "http://www.w3.org/2001/XMLSchema#int",
063         "http://www.w3.org/2001/XMLSchema#integer",
064         "http://www.w3.org/2001/XMLSchema#long",
065         "http://www.w3.org/2001/XMLSchema#negativeInteger",
066         "http://www.w3.org/2001/XMLSchema#nonNegativeInteger",
067         "http://www.w3.org/2001/XMLSchema#nonPositiveInteger",
068         "http://www.w3.org/2001/XMLSchema#positiveInteger",
069         "http://www.w3.org/2001/XMLSchema#short",
070         "http://www.w3.org/2001/XMLSchema#string",
071         "http://www.w3.org/2001/XMLSchema#time",
072         "http://www.w3.org/2001/XMLSchema#unsignedByte",
073         "http://www.w3.org/2001/XMLSchema#unsignedInt",
074         "http://www.w3.org/2001/XMLSchema#unsignedLong",
075         "http://www.w3.org/2001/XMLSchema#unsignedShort"}));
076     domainAction = new ValuesSelectionAction();
077     domainButton.addActionListener(new ActionListener() {
078       public void actionPerformed(ActionEvent actionevent) {
079         String as[] new String[ontologyClassesURIs.size()];
080         for(int i = 0; i < as.length; i++)
081           as[i= ontologyClassesURIs.get(i);
082         ArrayList<String> arraylist = new ArrayList<String>();
083         for(int j = 0; j < selectedNodes.size(); j++) {
084           DefaultMutableTreeNode node = selectedNodes.get(j);
085           OResource res = ((OResourceNode)node.getUserObject()).getResource();
086           if(res instanceof OClass) {
087             arraylist.add(res.getURI().toString());
088           }
089         }
090         String as1[] new String[arraylist.size()];
091         for(int k = 0; k < as1.length; k++) {
092           as1[k= arraylist.get(k);
093         }
094         domainAction.showGUI("Domain", as, as1, false,
095           MainFrame.getIcon("ontology-datatype-property"));
096       }
097     });
098   }
099 
100   public void actionPerformed(ActionEvent actionevent) {
101     nameSpace.setText(ontology.getDefaultNameSpace() == null ?
102       "http://gate.ac.uk/example#" : ontology.getDefaultNameSpace());
103     JOptionPane pane = new JOptionPane(mainPanel, JOptionPane.QUESTION_MESSAGE,
104       JOptionPane.OK_CANCEL_OPTION,
105       MainFrame.getIcon("ontology-datatype-property")) {
106       public void selectInitialValue() {
107         propertyName.requestFocusInWindow();
108         propertyName.selectAll();
109       }
110     };
111     pane.createDialog(MainFrame.getInstance(),
112       "New Datatype Property").setVisible(true);
113     Object selectedValue = pane.getValue();
114     if (selectedValue != null
115     && selectedValue instanceof Integer
116     && (IntegerselectedValue == JOptionPane.OK_OPTION) {
117       String s = nameSpace.getText();
118       if(!Utils.isValidNameSpace(s)) {
119         JOptionPane.showMessageDialog(MainFrame.getInstance(),
120           "Invalid Name Space: " + s + "\nExample: http://gate.ac.uk/example#");
121         return;
122       }
123       if(!Utils.isValidOntologyResourceName(propertyName.getText())) {
124         JOptionPane.showMessageDialog(MainFrame.getInstance(),
125           "Invalid Property Name: " + propertyName.getText());
126         return;
127       }
128       if(ontology.getOResourceFromMap(s + propertyName.getText()) != null) {
129         JOptionPane.showMessageDialog(MainFrame.getInstance(),"<html>" +
130           "Resource <b>" + s+propertyName.getText() "</b> already exists.");
131         return;
132       }
133       String as[] = domainAction.getSelectedValues();
134       HashSet<OClass> hashset = new HashSet<OClass>();
135       for (String a : as) {
136         OClass oclass = (OClassontology.getOResourceFromMap(a);
137         hashset.add(oclass);
138       }
139       DataType dt = DataType.getDataType((String)
140         datatypesComboBox.getSelectedItem());
141       ontology.addDatatypeProperty(new URI(nameSpace.getText()
142         + propertyName.getText()false), hashset, dt);
143     }
144   }
145 
146   public Ontology getOntology() {
147     return ontology;
148   }
149 
150   public void setOntology(Ontology ontology) {
151     this.ontology = ontology;
152   }
153 
154   public void selectionChanged(ArrayList<DefaultMutableTreeNode> arraylist) {
155     selectedNodes = arraylist;
156   }
157 
158   public ArrayList getOntologyClassesURIs() {
159     return ontologyClassesURIs;
160   }
161 
162   public void setOntologyClassesURIs(ArrayList<String> arraylist) {
163     ontologyClassesURIs = arraylist;
164   }
165 
166   protected JPanel mainPanel;
167   protected JTextField nameSpace;
168   protected JComboBox datatypesComboBox;
169   protected JButton domainButton;
170   protected JTextField propertyName;
171   protected ValuesSelectionAction domainAction;
172   protected ArrayList<String> ontologyClassesURIs;
173   protected ArrayList<DefaultMutableTreeNode> selectedNodes;
174   protected Ontology ontology;
175 }