1   /*
2    *  Copyright (c) 1998-2001, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Angel Kirilov 18/04/2002
10   *
11   *  $Id: ProtegeWrapper.java,v 1.10 2002/07/04 09:12:43 nasso Exp $
12   *
13   */
14  package gate.gui;
15  
16  import java.awt.*;
17  import java.awt.event.*;
18  import java.net.URL;
19  import javax.swing.*;
20  
21  import gate.*;
22  import gate.creole.*;
23  import gate.creole.ontology.Ontology;
24  // Protege import
25  import edu.stanford.smi.protege.ui.*;
26  import edu.stanford.smi.protege.model.*;
27  
28  /**
29   *  This class wrap the Protege application to show it as VR in GATE
30   */
31  public class ProtegeWrapper extends AbstractVisualResource {
32    /** Debug flag */
33    private static final boolean DEBUG = false;
34  
35    /** File name as string will be VR target for now */
36    private ProtegeProjectName projectFileName = null;
37    
38    /** Should have JRootPane to show Protege in it */
39    private JRootPane protegeRootPane = null;
40      
41    protected Handle myHandle;
42  
43    public ProtegeWrapper() {
44      projectFileName = null;
45    }
46  
47    public Resource init(){
48      initLocalData();
49      initGuiComponents();
50      initListeners();
51      return this;
52    }
53  
54    private void initLocalData() {
55      projectFileName = null;
56    } // initLocalData()
57    
58    private void initGuiComponents() {
59      setLayout(new BorderLayout());
60      protegeRootPane = new JRootPane();
61    } // initGuiComponents()
62  
63    /** Find and remove the Protege toolbar */  
64    private void removeToolbar(JRootPane rootPane) {
65      Container pane = rootPane.getContentPane();
66  
67      Component components[] = pane.getComponents();
68      for(int i=0; i<components.length; ++i) {
69        if(components[i] instanceof ProjectToolBar) {
70          pane.remove((ProjectToolBar) components[i]);
71          pane.add(new JLabel(), BorderLayout.SOUTH, i);
72          break;
73        } // if
74      } // for
75    } // removeToolbar(JRootPane rootPane)
76    
77    private void initListeners() {
78    } // initListeners()
79  
80    public void setHandle(Handle handle){
81      myHandle = handle;
82    }
83  
84    /** Refresh OntoEditor if any on LargeView tab pane */
85    public void refreshOntoeditor(Ontology o) {
86      if(myHandle == null || myHandle.getLargeView() == null) return;
87      
88      JComponent comp = myHandle.getLargeView();
89      if(comp instanceof JTabbedPane) {
90        JTabbedPane tabPane = (JTabbedPane) comp;
91        Component aView;
92        
93        for(int i=0; i<tabPane.getTabCount(); ++i) {
94          aView = tabPane.getComponentAt(i);
95          if(aView instanceof com.ontotext.gate.vr.OntologyEditorImpl) {
96            ((com.ontotext.gate.vr.OntologyEditorImpl) aView).setOntology(o);
97          }
98        } // for
99      } // if
100   } // refreshOntoeditor()
101   
102   public void setTarget(Object target){
103     if(target == null){
104       // if projectFileName is null Protege will create a new project
105       projectFileName = null;
106     }
107     else {
108       if(!(target instanceof ProtegeProjectName)){
109         throw new IllegalArgumentException(
110           "The Protege wrapper can only display Protege projects!\n" +
111           "The provided resource is not a Protege project but a: " +
112           target.getClass().toString() + "!");
113       } // if
114 
115       projectFileName = (ProtegeProjectName) target;
116       String fileName = null;
117     
118       if(projectFileName != null) {
119         URL projectURL = projectFileName.getProjectName();
120         if(projectURL != null) {
121           fileName = projectURL.getFile();
122         }
123         if(fileName != null && fileName.trim().length() == 0) {
124           fileName = null;
125         }
126       }
127 
128       JFrame frame = new JFrame();
129       frame.getContentPane().add(protegeRootPane);
130 
131       ProjectManager.getProjectManager().setRootPane(protegeRootPane);
132       if(DEBUG) {
133         System.out.println("Load Protege project: "+fileName);
134       }
135       ProjectManager.getProjectManager().loadProject(fileName);
136 
137       protegeRootPane.setJMenuBar(null);
138       removeToolbar(protegeRootPane);
139       
140       JScrollPane scroll = new JScrollPane();
141       add(scroll, BorderLayout.CENTER);
142       scroll.getViewport().add(protegeRootPane);
143       
144       // set KnowledgeBase object
145       Project prj = null;
146       KnowledgeBase knBase = null;
147       
148       prj = ProjectManager.getProjectManager().getCurrentProject();
149       if(projectFileName != null && prj != null) {
150         knBase = prj.getKnowledgeBase();
151         projectFileName.setKnowledgeBase(knBase);
152         projectFileName.setViewResource(this);
153 // Some debug information about KnowledgeBase instance        
154 System.out.println("KnBase name: "+knBase.getName());
155 System.out.println("KnBase root cls: "+knBase.getRootClses());
156 System.out.println("KnBase cls count: "+knBase.getClsCount());
157       } // if
158       
159     } // if
160   } // setTarget(Object target)
161   
162 //------------------------------------------------------------------------------
163 // Main method for test purposes  
164   
165   /** Test code*/
166   public static void main(String[] args) {
167 
168     try {
169       Gate.setLocalWebServer(false);
170       Gate.setNetConnected(false);
171       Gate.init();
172   
173       JFrame frame = new JFrame("Protege Wrapper Test");
174       frame.setSize(800, 500);
175   
176       frame.addWindowListener(new WindowAdapter(){
177         public void windowClosing(WindowEvent e){
178           System.exit(0);
179         }
180       });
181 
182       FeatureMap params = Factory.newFeatureMap();
183       params.put("projectName",
184         "");
185       ProtegeProjectName prjName = (ProtegeProjectName) Factory.createResource(
186                             "gate.creole.ProtegeProjectName", params);
187   
188       params.clear();
189 
190       ProtegeWrapper protege;
191 
192       protege = (ProtegeWrapper)Factory.createResource(
193                           "gate.gui.ProtegeWrapper", params);
194 
195       frame.getContentPane().add(protege);
196       frame.pack();
197       frame.setVisible(true);
198       protege.setTarget(prjName);
199 
200     } catch (Exception ex) {
201       ex.printStackTrace();
202     }
203     
204   } // public static void main(String[] args)
205 } // class ProtegeWrapper