AnnotationPropertyAction.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: AnnotationPropertyAction.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 javax.swing.*;
022 
023 /**
024  * Action to create a new annotation property.
025  */
026 public class AnnotationPropertyAction extends AbstractAction {
027   private static final long serialVersionUID = 3546358452780544048L;
028 
029   /**
030    * Constructor
031    
032    @param s - Label assigned to the Button
033    @param icon - Icon assigned to the Button
034    */
035   public AnnotationPropertyAction(String s, Icon icon) {
036     super(s, icon);
037     mainPanel = new JPanel(new GridBagLayout());
038     GridBagConstraints gbc = new GridBagConstraints();
039     gbc.insets = new Insets(3333);
040     gbc.anchor = GridBagConstraints.WEST;
041 
042     mainPanel.add(new JLabel("Name Space:"), gbc);
043     mainPanel.add(nameSpace = new JTextField(30), gbc);
044 
045     gbc.gridy = 1;
046     mainPanel.add(new JLabel("Property Name:"), gbc);
047     mainPanel.add(propertyName = new JTextField(30), gbc);
048   }
049 
050   public void actionPerformed(ActionEvent actionevent) {
051     nameSpace.setText(ontology.getDefaultNameSpace() == null ?
052       "http://gate.ac.uk/example#" : ontology.getDefaultNameSpace());
053     JOptionPane pane = new JOptionPane(mainPanel, JOptionPane.QUESTION_MESSAGE,
054       JOptionPane.OK_CANCEL_OPTION,
055       MainFrame.getIcon("ontology-annotation-property")) {
056       public void selectInitialValue() {
057         propertyName.requestFocusInWindow();
058         propertyName.selectAll();
059       }
060     };
061     pane.createDialog(MainFrame.getInstance(),
062       "New Annotation Property").setVisible(true);
063     Object selectedValue = pane.getValue();
064     if (selectedValue != null
065     && selectedValue instanceof Integer
066     && (IntegerselectedValue == JOptionPane.OK_OPTION) {
067       String s = nameSpace.getText();
068       if(!Utils.isValidNameSpace(s)) {
069         JOptionPane.showMessageDialog(MainFrame.getInstance(),
070           "Invalid Name Space: " + s + "\nExample: http://gate.ac.uk/example#");
071         return;
072       }
073       if(!Utils.isValidOntologyResourceName(propertyName.getText())) {
074         JOptionPane.showMessageDialog(MainFrame.getInstance(),
075           "Invalid Property Name: " + propertyName.getText());
076         return;
077       }
078       if(ontology.getOResourceFromMap(s + propertyName.getText()) != null) {
079         JOptionPane.showMessageDialog(MainFrame.getInstance(),"<html>" +
080           "Resource <b>" + s+propertyName.getText() "</b> already exists.");
081         return;
082       }
083       ontology.addAnnotationProperty(new URI(nameSpace.getText()
084               + propertyName.getText()false));
085     }
086   }
087 
088   /**
089    @return the associated ontology
090    */
091   public Ontology getOntology() {
092     return ontology;
093   }
094 
095   /**
096    * Specifies the ontology that should be used to add/remove resource
097    * to/from.
098    */
099   public void setOntology(Ontology ontology) {
100     this.ontology = ontology;
101   }
102 
103   protected JPanel mainPanel;
104   protected JTextField nameSpace;
105   protected JTextField propertyName;
106   protected Ontology ontology;
107 }