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