DeleteOntologyResourceAction.java
01 /*
02  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
03  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
04  *
05  *  This file is part of GATE (see http://gate.ac.uk/), and is free
06  *  software, licenced under the GNU Library General Public License,
07  *  Version 2, June 1991 (in the distribution as file licence.html,
08  *  and also available at http://gate.ac.uk/gate/licence.html).
09  *
10  *  Niraj Aswani, 09/March/07
11  *
12  *  $Id: DeleteOntologyResourceAction.html,v 1.0 2007/03/09 16:13:01 niraj Exp $
13  */
14 package gate.gui.ontology;
15 
16 import gate.creole.ontology.*;
17 import gate.gui.MainFrame;
18 
19 import java.awt.event.ActionEvent;
20 import java.util.ArrayList;
21 import javax.swing.*;
22 import javax.swing.tree.DefaultMutableTreeNode;
23 
24 /**
25  * Action to delete a resource from ontology.
26  */
27 public class DeleteOntologyResourceAction extends AbstractAction implements
28                                                                 TreeNodeSelectionListener {
29   private static final long serialVersionUID = 3257289136439439920L;
30 
31   public DeleteOntologyResourceAction(String caption, Icon icon) {
32     super(caption, icon);
33   }
34 
35   public void actionPerformed(ActionEvent actionevent) {
36     String[] resourcesToDelete = new String[selectedNodes.size()];
37     int i = 0;
38     for (DefaultMutableTreeNode node : selectedNodes) {
39       Object object = ((OResourceNodenode.getUserObject()).getResource();
40       resourcesToDelete[i++((OResourceobject).getONodeID().toString();
41     }
42     JList list = new JList(resourcesToDelete);
43     int choice = JOptionPane.showOptionDialog(MainFrame.getInstance(),
44       new Object[]{"Are you sure you want to delete the following resources?",
45       "\n\n"new JScrollPane(list)'\n'}"Delete resources",
46       JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
47       new String[]{"Delete resources""Cancel"}"Cancel");
48     if (choice == JOptionPane.CLOSED_OPTION || choice == 1)  { return}
49     for (DefaultMutableTreeNode node : selectedNodes) {
50       Object object = ((OResourceNodenode.getUserObject()).getResource();
51       try {
52         if (object instanceof OClass) {
53           if (ontology.containsOClass(((OClassobject).getURI()))
54             ontology.removeOClass((OClassobject);
55           continue;
56         }
57         if (object instanceof OInstance) {
58           if (ontology.getOInstance(((OInstanceobject).getURI()) != null)
59             ontology.removeOInstance((OInstanceobject);
60           continue;
61         }
62         if ((object instanceof RDFProperty)
63           && ontology.getOResourceFromMap(
64             ((RDFPropertyobject).getURI().toString()) != null)
65           ontology.removeProperty((RDFPropertyobject);
66       }
67       catch (Exception re) {
68         re.printStackTrace();
69         JOptionPane.showMessageDialog(MainFrame.getInstance(), re.getMessage() +
70           "\nPlease see tab messages for more information!");
71       }
72     }
73   }
74 
75   public Ontology getOntology() {
76     return ontology;
77   }
78 
79   public void setOntology(Ontology ontology) {
80     this.ontology = ontology;
81   }
82 
83   public void selectionChanged(ArrayList<DefaultMutableTreeNode> arraylist) {
84     selectedNodes = arraylist;
85   }
86 
87   protected Ontology ontology;
88   protected ArrayList<DefaultMutableTreeNode> selectedNodes;
89 }