SymmetricPropertyAction.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: SymmetricPropertyAction.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 symmetric property in the ontology.
028  */
029 public class SymmetricPropertyAction extends AbstractAction implements
030                                                            TreeNodeSelectionListener {
031   private static final long serialVersionUID = 3257286915891017008L;
032 
033   public SymmetricPropertyAction(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("Property Name:"), gbc);
046     mainPanel.add(propertyName = new JTextField(30), gbc);
047     mainPanel.add(domainRangeButton = new JButton("Domain and Range"), gbc);
048 
049     domainRangeAction = new ValuesSelectionAction();
050     domainRangeButton.addActionListener(new ActionListener() {
051       public void actionPerformed(ActionEvent actionevent) {
052         String as[] new String[ontologyClassesURIs.size()];
053         for(int i = 0; i < as.length; i++)
054           as[i= ontologyClassesURIs.get(i);
055         ArrayList<String> arraylist = new ArrayList<String>();
056         for(int j = 0; j < selectedNodes.size(); j++) {
057           DefaultMutableTreeNode defaultmutabletreenode = selectedNodes.get(j);
058           if(((OResourceNode)defaultmutabletreenode.getUserObject())
059                   .getResource() instanceof OClass)
060             arraylist.add((((OResourceNode)defaultmutabletreenode
061                     .getUserObject()).getResource()).getURI().toString());
062         }
063         String as1[] new String[arraylist.size()];
064         for(int k = 0; k < as1.length; k++)
065           as1[k= arraylist.get(k);
066         domainRangeAction.showGUI("Domain and Range", as, as1, false,
067           MainFrame.getIcon("ontology-symmetric-property"));
068       }
069     });
070   }
071 
072   public void actionPerformed(ActionEvent actionevent) {
073     nameSpace.setText(ontology.getDefaultNameSpace() == null ?
074       "http://gate.ac.uk/example#" : ontology.getDefaultNameSpace());
075     JOptionPane pane = new JOptionPane(mainPanel, JOptionPane.QUESTION_MESSAGE,
076       JOptionPane.OK_CANCEL_OPTION,
077       MainFrame.getIcon("ontology-symmetric-property")) {
078       public void selectInitialValue() {
079         propertyName.requestFocusInWindow();
080         propertyName.selectAll();
081       }
082     };
083     pane.createDialog(MainFrame.getInstance(),
084       "New Symmetric Property").setVisible(true);
085     Object selectedValue = pane.getValue();
086     if (selectedValue != null
087     && selectedValue instanceof Integer
088     && (IntegerselectedValue == JOptionPane.OK_OPTION) {
089       String s = nameSpace.getText();
090       if(!Utils.isValidNameSpace(s)) {
091         JOptionPane.showMessageDialog(MainFrame.getInstance(),
092           "Invalid NameSpace: " + s + "\n Example: http://gate.ac.uk/example#");
093         return;
094       }
095       if(!Utils.isValidOntologyResourceName(propertyName.getText())) {
096         JOptionPane.showMessageDialog(MainFrame.getInstance(),
097           "Invalid Symmetric Property Name: " + propertyName.getText());
098         return;
099       }
100       if(ontology.getOResourceFromMap(nameSpace.getText()
101               + propertyName.getText()) != null) {
102         JOptionPane.showMessageDialog(MainFrame.getInstance(),"<html>" +
103           "Resource <b>" + s+propertyName.getText() "</b> already exists.");
104         return;
105       }
106       String domainSelectedValues[] = domainRangeAction.getSelectedValues();
107       HashSet<OClass> domainSet = new HashSet<OClass>();
108       for(int j = 0; j < domainSelectedValues.length; j++) {
109         OClass oclass = (OClass)
110           ontology.getOResourceFromMap(domainSelectedValues[j]);
111         domainSet.add(oclass);
112       }
113       ontology.addSymmetricProperty(new URI(nameSpace.getText()
114         + propertyName.getText()false), domainSet);
115     }
116   }
117 
118   public Ontology getOntology() {
119     return ontology;
120   }
121 
122   public void setOntology(Ontology ontology) {
123     this.ontology = ontology;
124   }
125 
126   public void selectionChanged(ArrayList<DefaultMutableTreeNode> arraylist) {
127     selectedNodes = arraylist;
128   }
129 
130   public ArrayList<String> getOntologyClassesURIs() {
131     return ontologyClassesURIs;
132   }
133 
134   public void setOntologyClassesURIs(ArrayList<String> arraylist) {
135     ontologyClassesURIs = arraylist;
136   }
137 
138   protected JPanel mainPanel;
139   protected JTextField nameSpace;
140   protected JTextField propertyName;
141   protected JButton domainRangeButton;
142   protected ValuesSelectionAction domainRangeAction;
143   protected ArrayList<String> ontologyClassesURIs;
144   protected ArrayList<DefaultMutableTreeNode> selectedNodes;
145   protected Ontology ontology;
146 }