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.presentation;
18  
19  import java.util.List;
20  
21  import olr.om.RdfSchemaPeer;
22  import olr.toolbar.ToolbarSession;
23  import olr.viewer.InstanceFileViewer;
24  import olr.viewer.ModelViewer;
25  import olr.viewer.ModelViewerRow;
26  import olr.viewer.SchemaViewer;
27  
28  import org.w3c.dom.html.HTMLElement;
29  import org.w3c.dom.html.HTMLImageElement;
30  import org.w3c.dom.html.HTMLTableElement;
31  import org.w3c.dom.html.HTMLTableRowElement;
32  
33  import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
34  import com.lutris.appserver.server.httpPresentation.HttpPresentationRequest;
35  
36  /***
37   * @version $Id: Toolbar.java,v 1.8 2004/08/03 08:33:53 roku Exp $
38   */
39  public class Toolbar extends SecureHttpPresentation {
40      /***
41       * Initializes an instance of this class.
42       */
43      public Toolbar() {
44      }
45  
46      public String handleDefault() throws HttpPresentationException {
47          return showPage();
48      }
49  
50      public String handleAddSchemaMask() throws HttpPresentationException {
51          return showPage(null, true, false);
52      }
53  
54      public String handleAddInstanceFileMask() throws HttpPresentationException {
55          return showPage(null, false, true);
56      }
57  
58      public String handleAddSchema() throws HttpPresentationException {
59          HttpPresentationRequest request = getComms().request;
60          try {
61              String URI = getComms().request.getParameter("addModelURI");
62              String title = getComms().request.getParameter("addModelTitle");
63              if (URI != null && title != null && URI.length() > 0 && title.length() > 0)
64                  getSessionData().getToolbarSession().addSchema(URI, title);
65          } catch (Exception e) {
66              String s = showPage(e.getMessage());
67              return s;
68          }
69          return showPage(true);
70      }
71  
72      public String handleAddInstanceFile() throws HttpPresentationException {
73          HttpPresentationRequest request = getComms().request;
74          try {
75              String URI = getComms().request.getParameter("addInstanceFileURI");
76              String title = getComms().request.getParameter("addInstanceFileTitle");
77              if (URI != null && title != null && URI.length() > 0 && title.length() > 0)
78                  getSessionData().getToolbarSession().addInstanceFile(URI, title);
79          } catch (Exception e) {
80              String s = showPage(e.getMessage());
81              return s;
82          }
83          return showPage(true);
84      }
85  
86      public String handleRemove() throws HttpPresentationException {
87          HttpPresentationRequest request = getComms().request;
88          try {
89              ToolbarSession toolbarSession = getSessionData().getToolbarSession();
90              int modelNr = Integer.parseInt(request.getParameter("model"));
91              if (modelNr < toolbarSession.size())
92                  toolbarSession.removeModelAt(modelNr);
93          } catch (Exception e) {
94              e.printStackTrace();
95          }
96          return showPage(true);
97      }
98  
99      public String handleExpand() throws HttpPresentationException {
100         HttpPresentationRequest request = getComms().request;
101         try {
102             ToolbarSession toolbarSession = getSessionData().getToolbarSession();
103             int modelNr = Integer.parseInt(request.getParameter("model"));
104             int rowNr = Integer.parseInt(request.getParameter("row"));
105             if (modelNr < toolbarSession.size())
106                 if (rowNr == -1)
107                     toolbarSession.getModelViewerAt(modelNr).expand();
108                 else
109                     toolbarSession.getModelViewerAt(modelNr).expandResource(rowNr);
110         } catch (Exception e) {
111             e.printStackTrace();
112         }
113         return showPage();
114     }
115 
116     public String showPage() {
117         return showPage(null, false, false, false);
118     }
119 
120     public String showPage(String errorMsg) {
121         return showPage(errorMsg, false, false, false);
122     }
123 
124     public String showPage(boolean reloadContent) {
125         return showPage(null, false, false, reloadContent);
126     }
127 
128     public String showPage(String errorMsg, boolean addSchemaMode, boolean addInstanceFileMode) {
129         return showPage(errorMsg, addSchemaMode, addInstanceFileMode, false);
130     }
131 
132     public String showPage(String errorMsg, boolean addSchemaMode, boolean addInstanceFileMode,
133             boolean reloadContent) {
134         ToolbarHTML page = new ToolbarHTML();
135         if (errorMsg != null)
136             page.getElementErrorText().getFirstChild().setNodeValue(errorMsg);
137         if (!reloadContent)
138             page.getElementBody().setAttribute("onLoad", "");
139         HTMLTableElement table = page.getElementGenerate();
140         HTMLTableElement stable = page.getElementSTable();
141         HTMLTableElement atable = page.getElementATable();
142         HTMLTableRowElement headerRow1 = page.getElementHeaderRow1();
143         HTMLTableRowElement headerRow2 = page.getElementHeaderRow2();
144         HTMLTableRowElement headerRow3 = page.getElementHeaderRow3();
145         HTMLTableRowElement headerRow4 = page.getElementHeaderRow4();
146         HTMLTableRowElement schemaRow = page.getElementSchemaRow();
147         HTMLTableRowElement schema2Row = page.getElementSchema2Row();
148         HTMLTableRowElement instanceFileRow = page.getElementInstanceFileRow();
149         HTMLTableRowElement SSchemaRow = page.getElementSSchemaRow();
150         HTMLTableRowElement ASchemaRow = page.getElementASchemaRow();
151         HTMLTableRowElement propertyRow = page.getElementPropertyRow();
152         HTMLTableRowElement resourceRow = page.getElementResourceRow();
153         HTMLTableRowElement literalRow = page.getElementLiteralRow();
154         HTMLElement schema = page.getElementSchema();
155         HTMLElement schema2 = page.getElementSchema2();
156         HTMLElement schemaRemove = page.getElementSchemaRemove();
157         HTMLElement schemaInfo = page.getElementSchemaInfo();
158         HTMLImageElement schemaRemoveImage = page.getElementSchemaRemoveImage();
159         HTMLElement instanceFile = page.getElementInstanceFile();
160         HTMLElement instanceFileRemove = page.getElementInstanceFileRemove();
161         HTMLImageElement instanceFileRemoveImage = page.getElementInstanceFileRemoveImage();
162         HTMLElement property = page.getElementProperty();
163         HTMLElement resource = page.getElementResource();
164         HTMLElement literal = page.getElementLiteral();
165         ToolbarSession toolbarSession = getSessionData().getToolbarSession();
166         boolean b = false;
167         label0: for (int modelNr = 0; modelNr < toolbarSession.size(); modelNr++)
168             try {
169                 ModelViewer mViewer = toolbarSession.getModelViewerAt(modelNr);
170                 List modelRows = mViewer.getModelRows();
171                 String id = String.valueOf(String.valueOf((new StringBuffer("model="))
172                         .append(modelNr)));
173                 if (mViewer instanceof SchemaViewer) {
174 
175                     if (mViewer.getTitle().equals("olr3") || mViewer.getTitle().equals("relations")
176                             || RdfSchemaPeer.isCourseSchema(mViewer.getTitle())) {
177                         schema2.getFirstChild().setNodeValue(mViewer.getTitle());
178                         schema2.setAttribute("href", String.valueOf(String
179                                 .valueOf((new StringBuffer("Toolbar.po?event=expand&")).append(id)
180                                         .append("&").append("row").append("=-1"))));
181                         schemaInfo.setAttribute("href", "http://tauschasbuch.mondschafe.de/"
182                                 + mViewer.getTitle() + ".html");
183                         stable.appendChild(schema2Row.cloneNode(true));
184                         b = true;                        
185                     } else {
186                         schema.getFirstChild().setNodeValue(mViewer.getTitle());
187                         schema.setAttribute("href", String.valueOf(String
188                                 .valueOf((new StringBuffer("Toolbar.po?event=expand&")).append(id)
189                                         .append("&").append("row").append("=-1"))));
190 
191                         if (toolbarSession.isViewerRemovable(modelNr)) {
192                             schemaRemove.setAttribute("href", String.valueOf(String
193                                     .valueOf((new StringBuffer("Toolbar.po?event=remove&"))
194                                             .append(id))));
195                             schemaRemoveImage.setSrc("media/remove.gif");
196                         } else {
197                             schemaRemoveImage.setSrc("media/space.gif");
198                         }
199                         atable.appendChild(schemaRow.cloneNode(true));
200                         b = false;
201                     }
202                 } else if (mViewer instanceof InstanceFileViewer) {
203                     instanceFile.getFirstChild().setNodeValue(mViewer.getTitle());
204                     instanceFile.setAttribute("href", String.valueOf(String
205                             .valueOf((new StringBuffer("Toolbar.po?event=expand&")).append(id)
206                                     .append("&").append("row").append("=-1"))));
207                     if (toolbarSession.isViewerRemovable(modelNr)) {
208                         instanceFileRemove.setAttribute("href", String
209                                 .valueOf(String.valueOf((new StringBuffer(
210                                         "Toolbar.po?event=remove&")).append(id))));
211                         instanceFileRemoveImage.setSrc("media/remove.gif");
212                     } else {
213                         instanceFileRemoveImage.setSrc("media/space.gif");
214                     }
215                     table.appendChild(instanceFileRow.cloneNode(true));
216                 }
217                 int rowNr = 0;
218                 do {
219                     if (rowNr >= modelRows.size())
220                         continue label0;
221                     ModelViewerRow row = (ModelViewerRow) modelRows.get(rowNr);
222                     if (mViewer != null)
223                         mViewer.checkSub(rowNr);//neu
224                     ModelViewerRow nextRow = rowNr >= modelRows.size() - 1 ? null
225                             : (ModelViewerRow) modelRows.get(rowNr + 1);
226                     boolean isEndOfSublist = nextRow != null ? nextRow.getIndent() != row
227                             .getIndent() : true;
228                     String subid = String.valueOf(String.valueOf((new StringBuffer(String
229                             .valueOf(String.valueOf(id)))).append("&").append("row").append("=")
230                             .append(rowNr)));
231                     String exp = row.isExpandable() ? row.isExpanded() ? "- " : "+ " : "";
232                     if (row.isSchemaProperty()) {
233                         property.setAttribute("href", String.valueOf(String
234                                 .valueOf((new StringBuffer("Toolbar.po?event=expand&"))
235                                         .append(subid))));
236                         property.getFirstChild().setNodeValue(
237                                 String.valueOf(exp) + String.valueOf(row.getTitle()));
238                         if (b) {
239                             stable.appendChild(propertyRow.cloneNode(true));
240                         } else {
241                             atable.appendChild(propertyRow.cloneNode(true));
242                         }
243                     } else if (row.isExpandable()) {
244                         page.getElementSpace1().setAttribute("width",
245                                 "".concat(String.valueOf(String.valueOf(row.getIndent() * 8))));
246                         resource.setAttribute("href", String.valueOf(String
247                                 .valueOf((new StringBuffer("Toolbar.po?event=expand&"))
248                                         .append(subid))));
249                         resource.getFirstChild().setNodeValue(
250                                 String.valueOf(exp) + String.valueOf(row.getTitle()));
251                         if (b)
252                             stable.appendChild(resourceRow.cloneNode(true));
253                         else
254                             atable.appendChild(resourceRow.cloneNode(true));
255                     } else {
256                         page.getElementSpace2().setAttribute("width",
257                                 "".concat(String.valueOf(String.valueOf(row.getIndent() * 8))));
258                         literal.getChildNodes().item(1).setNodeValue(
259                                 String.valueOf(exp) + String.valueOf(row.getTitle()));
260                         if (b)
261                             stable.appendChild(literalRow.cloneNode(true));
262                         else
263                             atable.appendChild(literalRow.cloneNode(true));
264                     }
265                     rowNr++;
266                 } while (true);
267             } catch (Exception e) {
268                 e.printStackTrace();
269             }
270             
271         if (addSchemaMode)
272             headerRow1.getParentNode().removeChild(headerRow1);
273         else
274             headerRow2.getParentNode().removeChild(headerRow2);
275         
276         if (addInstanceFileMode)
277             headerRow3.getParentNode().removeChild(headerRow3);
278         else
279             headerRow4.getParentNode().removeChild(headerRow4);
280         
281         schemaRow.getParentNode().removeChild(schemaRow);
282         schema2Row.getParentNode().removeChild(schema2Row);
283         instanceFileRow.getParentNode().removeChild(instanceFileRow);
284 
285         propertyRow.getParentNode().removeChild(propertyRow);
286         resourceRow.getParentNode().removeChild(resourceRow);
287         
288         literalRow.getParentNode().removeChild(literalRow);
289         return page.toDocument();
290     }
291 
292     private final String THIS_HTML = "Toolbar.po";
293 
294     private final String REMOVE_GIF = "media/remove.gif";
295 
296     private final String SPACE_GIF = "media/space.gif";
297 
298     private final String MODEL_ID = "model";
299 
300     private final String ROW_ID = "row";
301 
302     private final String ADDMODELURI_ID = "addModelURI";
303 
304     private final String ADDMODELTITLE_ID = "addModelTitle";
305 
306     private final String ADDINSTANCEFILEURI_ID = "addInstanceFileURI";
307 
308     private final String ADDINSTANCEFILETITLE_ID = "addInstanceFileTitle";
309 
310     private final String ONLOAD_ID = "onLoad";
311 
312     private final String EXPAND_EVT = "expand";
313 
314     private final String REMOVE_EVT = "remove";
315 
316     private final String ADDSCHEMA_EVT = "addSchema";
317 
318     private final String ADDSCHEMAMASK_EVT = "addSchemaMask";
319 
320     private final String ADDINSTANCEFILE_EVT = "addInstanceFile";
321 
322     private final String ADDINSTANCEFILEMASK_EVT = "addInstanceFileMask";
323 }