001 /** 002 * OntoBride library. 003 * GAIA - Group for Artifical Intelligence Applications 004 * Departamento de Ingeniería del Software e Inteligencia Artificial 005 * Universidad Complutense de Madrid 006 * 007 * Licensed under the terms of the GNU Library or Lesser General Public License (LGPL) 008 * 009 * @author Juan A. Recio García 010 * @version 1.0 beta 011 * 012 * This software is a subproject of the jCOLIBRI framework 013 * http://sourceforge.net/projects/jcolibri-cbr/ 014 * http://gaia.fdi.ucm.es/projects/jcolibri/ 015 * 016 * File: PnlConceptsAndInstancesTree.java 017 * 26/02/2007 018 */ 019 020 package es.ucm.fdi.gaia.ontobridge.test.gui; 021 022 import java.awt.event.ActionEvent; 023 import java.awt.event.ActionListener; 024 025 026 import javax.swing.BorderFactory; 027 import javax.swing.Icon; 028 import javax.swing.JButton; 029 import javax.swing.JCheckBox; 030 import javax.swing.JPanel; 031 import javax.swing.JScrollPane; 032 import javax.swing.JTree; 033 import javax.swing.border.Border; 034 import javax.swing.filechooser.FileFilter; 035 import javax.swing.tree.DefaultMutableTreeNode; 036 import javax.swing.tree.DefaultTreeCellRenderer; 037 import javax.swing.tree.DefaultTreeModel; 038 039 040 import java.awt.*; 041 import java.io.File; 042 043 import es.ucm.fdi.gaia.ontobridge.OntoBridge; 044 045 import java.util.*; 046 047 048 /** 049 * Shows concepts, defined and inferred instances and saves the changes into a file 050 * @author Juan Ant. Recio García 051 */ 052 public class PnlConceptsAndInstancesTree extends JPanel { 053 054 private static final long serialVersionUID = 1L; 055 056 private JTree ontologyTree; 057 private DefaultMutableTreeNode root; 058 private JCheckBox inferredInstancesCB; 059 private JButton update; 060 private JButton save; 061 private java.util.ArrayList<String> _instances = new java.util.ArrayList<String>(); 062 private OntoBridge ob; 063 064 private static int maxdepth = 20; //Constant to avoid cycles; 065 private static Icon CONCEPT = new javax.swing.ImageIcon(PnlConceptsAndInstancesTree.class.getResource("/es/ucm/fdi/gaia/ontobridge/test/gui/class-orange.gif")); 066 private static Icon INSTANCE = new javax.swing.ImageIcon(PnlConceptsAndInstancesTree.class.getResource("/es/ucm/fdi/gaia/ontobridge/test/gui/instance.gif")); 067 068 /** 069 * Constructor 070 */ 071 public PnlConceptsAndInstancesTree(OntoBridge ob) { 072 super(); 073 createComponents(); 074 this.ob = ob; 075 readOntology(); 076 } 077 078 /** 079 * Constructor 080 */ 081 public PnlConceptsAndInstancesTree(OntoBridge ob, boolean inferenceEnabled) { 082 super(); 083 createComponents(); 084 this.inferredInstancesCB.setSelected(inferenceEnabled); 085 this.ob = ob; 086 readOntology(); 087 } 088 089 protected void createComponents(){ 090 JScrollPane scrPnl; 091 Border lineBorder, titleBorder, emptyBorder, compoundBorder; 092 093 //set border and layout 094 emptyBorder = BorderFactory.createEmptyBorder(0, 5, 0, 5); 095 lineBorder = BorderFactory.createLineBorder(Color.BLACK); 096 titleBorder = BorderFactory.createTitledBorder(lineBorder, "Ontology Structure"); 097 compoundBorder = BorderFactory.createCompoundBorder(titleBorder, 098 emptyBorder); 099 setBorder(compoundBorder); 100 101 //set Ontology 102 root= new DefaultMutableTreeNode("Thing"); 103 104 ontologyTree = new JTree(root); 105 ontologyTree.setCellRenderer(new MyRenderer()); 106 ontologyTree.setSelectionModel(null); 107 108 109 scrPnl = new JScrollPane(ontologyTree); 110 scrPnl.setViewportView(ontologyTree); 111 112 setLayout(new BorderLayout()); 113 add(scrPnl,BorderLayout.CENTER); 114 115 JPanel options = new JPanel(); 116 inferredInstancesCB = new JCheckBox("Inferred Instances"); 117 update = new JButton("Update"); 118 save = new JButton("Save"); 119 options.setLayout(new BorderLayout()); 120 options.add(inferredInstancesCB, BorderLayout.NORTH); 121 options.add(update, BorderLayout.CENTER); 122 options.add(save, BorderLayout.SOUTH); 123 add(options, BorderLayout.SOUTH); 124 125 126 update.addActionListener(new ActionListener(){ 127 public void actionPerformed(ActionEvent e) { 128 readOntology(); 129 } 130 }); 131 132 save.addActionListener(new ActionListener(){ 133 public void actionPerformed(ActionEvent e) { 134 save(); 135 } 136 }); 137 } 138 139 140 protected void save() 141 { 142 javax.swing.JFileChooser jfc = new javax.swing.JFileChooser(); 143 jfc.setFileFilter(new FileFilter(){ 144 public boolean accept(File f) { 145 return f.getAbsolutePath().endsWith("owl"); 146 } 147 public String getDescription() { 148 return "OWL ontology RDF/XML"; 149 } 150 }); 151 int returnVal = jfc.showSaveDialog(this); 152 if(returnVal == javax.swing.JFileChooser.APPROVE_OPTION) { 153 String filename = jfc.getSelectedFile().getName(); 154 ob.save(filename); 155 } 156 } 157 158 /** 159 * Read the ontology classes. 160 * 161 */ 162 protected void readOntology() { 163 try 164 { 165 root.removeAllChildren(); 166 Iterator<String> rc = ob.listRootClasses(); 167 while(rc.hasNext()) 168 { 169 DefaultMutableTreeNode node = createNode(rc.next(), ob, 0); 170 root.add(node); 171 } 172 ontologyTree.setModel(new DefaultTreeModel(root)); 173 ontologyTree.expandRow(0); 174 175 } catch (Exception e) { 176 org.apache.commons.logging.LogFactory.getLog(this.getClass()).error(e); 177 } 178 } 179 180 181 private DefaultMutableTreeNode createNode(String nodeName, OntoBridge ob, int depth) 182 { 183 DefaultMutableTreeNode node = new DefaultMutableTreeNode(ob.getShortName(nodeName)); 184 if(depth > maxdepth) 185 return node; 186 187 Iterator<String> subClasses = ob.listSubClasses(nodeName, true); 188 while(subClasses.hasNext()) 189 { 190 String subClassName = ob.getShortName(subClasses.next()); 191 if(!subClassName.equals("owl:Nothing")) 192 node.add(createNode(subClassName, ob, depth+1)); 193 } 194 Iterator<String> instances; 195 if(this.inferredInstancesCB.isSelected()) 196 instances = ob.listInstances(nodeName); 197 else 198 instances = ob.listDeclaredInstances(nodeName); 199 200 while(instances.hasNext()) 201 { 202 String instanceName = ob.getShortName(instances.next()); 203 node.add(new DefaultMutableTreeNode(instanceName)); 204 _instances.add(instanceName); 205 } 206 return node; 207 } 208 209 210 class MyRenderer extends DefaultTreeCellRenderer { 211 private static final long serialVersionUID = 1L; 212 213 public MyRenderer() { 214 } 215 216 public Component getTreeCellRendererComponent(JTree tree, Object value, 217 boolean sel, boolean expanded, boolean leaf, int row, 218 boolean hasFocus) { 219 220 super.getTreeCellRendererComponent(tree, value, sel, expanded, 221 leaf, row, hasFocus); 222 223 try { 224 DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode)value; 225 if(_instances.contains( dmtn.getUserObject() )) 226 setIcon(INSTANCE); 227 else 228 setIcon(CONCEPT); 229 } catch (Exception e) { 230 org.apache.commons.logging.LogFactory.getLog(this.getClass()).error(e); 231 } 232 233 return this; 234 } 235 } 236 } 237 238 239