View Javadoc

1   /*
2    *  Copyright 2004 University of Hannover
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package olr.toolbar;
18  
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import olr.om.RdfUser;
25  import olr.rdf.Model;
26  import olr.rdf.Resource;
27  import olr.statementpool.OlrStatementPool;
28  import olr.viewer.InstanceFileViewer;
29  import olr.viewer.ModelViewer;
30  import olr.viewer.SchemaViewer;
31  import olr.viewer.SortedModelViewerList;
32  
33  /***
34   * @version $Id: AbstractToolbarSession.java,v 1.6 2004/08/02 18:38:53 roku Exp $
35   */
36  public abstract class AbstractToolbarSession implements ToolbarSession {
37      AbstractToolbarSession(OlrStatementPool statementPool) {
38          this.statementPool = statementPool;
39          this.modelViewers = new SortedModelViewerList();
40          this.nonRemovableViewers = new HashMap();
41      }
42  
43      public int size() {
44          return modelViewers.size();
45      }
46  
47      public ModelViewer getModelViewerAt(int n) {
48          if (n >= 0 && n < size())
49              return (ModelViewer) modelViewers.get(n);
50          else
51              return null;
52      }
53  
54      public boolean addSchema(String URI, String title) throws Exception {
55          return URI.length() > 0 && addSchema(new Model(URI, getStatementPool()), title);
56      }
57  
58      public boolean addSchema(Model model, String title) {
59          return addSchema(model, title, false);
60      }
61  
62      public boolean addSchema(Model model, String title, boolean nonRemovable) {
63          ModelViewer mv = new SchemaViewer(model, title);
64          modelViewers.add(mv);
65          if (nonRemovable)
66              nonRemovableViewers.put(mv, mv);
67          return true;
68      }
69  
70      public boolean addInstanceFile(String URI, String title) throws Exception {
71          return URI.length() > 0 && addInstanceFile(new Model(URI, getStatementPool()), title);
72      }
73  
74      public boolean addInstanceFile(Model model, String title) throws Exception {
75          modelViewers.add(new InstanceFileViewer(model, title));
76          return true;
77      }
78  
79      public String removeModelAt(int n) throws Exception {
80          ModelViewer mViewer = getModelViewerAt(n);
81          if (mViewer != null && !nonRemovableViewers.containsKey(mViewer)) {
82              String uri = mViewer.getModel().getNamespace();
83              mViewer.getModel().delete();
84              modelViewers.remove(n);
85              return uri;
86          } else {
87              return null;
88          }
89      }
90  
91      public boolean isViewerRemovable(int n) {
92          ModelViewer mViewer = getModelViewerAt(n);
93          if (mViewer != null)
94              return !nonRemovableViewers.containsKey(mViewer);
95          else
96              return false;
97      }
98  
99      public boolean containsProperty(String property) {
100         for (Iterator it = modelViewers.iterator(); it.hasNext();) {
101             ModelViewer mViewer = (ModelViewer) it.next();
102             if ((mViewer instanceof SchemaViewer)
103                     && mViewer.getModel().getSubjects().contains(property))
104                 return true;
105         }
106 
107         return false;
108     }
109 
110     public Object getResource4ID(Object res) {
111         if (res instanceof Resource)
112             return res;
113         else
114             return getStatementPool().getResource(res.toString());
115     }
116 
117     public static ToolbarSession newInstance(RdfUser user, OlrStatementPool statementPool)
118             throws Exception {
119         return new DBToolbarSession(user, statementPool);
120     }
121 
122     private OlrStatementPool getStatementPool() {
123         return this.statementPool;
124     }
125 
126     private List modelViewers;
127 
128     private Map nonRemovableViewers;
129 
130     private OlrStatementPool statementPool;
131 }