1
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
36 public class ProtegeWrapper extends AbstractVisualResource {
37
38 private static final boolean DEBUG = false;
39
40
41 private ProtegeProjectName projectFileName = null;
42
43
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 }
63 private void initGuiComponents() {
64 setLayout(new BorderLayout());
65 protegeRootPane = new JRootPane();
66 }
68
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 } } }
82 private void initListeners() {
83 }
85 public void setHandle(Handle handle){
86 myHandle = handle;
87 }
88
89
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 } } }
107 public void setTarget(Object target){
108 if(target == null){
109 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 }
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 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 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 }
164 } }
167
170
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 } }