KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > schema > SchemaFormOutlinePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.schema;
12
13 import org.eclipse.jface.viewers.ILabelProvider;
14 import org.eclipse.pde.core.IModelChangedEvent;
15 import org.eclipse.pde.internal.core.ischema.IDocumentSection;
16 import org.eclipse.pde.internal.core.ischema.ISchema;
17 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
18 import org.eclipse.pde.internal.core.ischema.ISchemaComplexType;
19 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
20 import org.eclipse.pde.internal.core.ischema.ISchemaObject;
21 import org.eclipse.pde.internal.core.ischema.ISchemaType;
22 import org.eclipse.pde.internal.ui.PDEUIMessages;
23 import org.eclipse.pde.internal.ui.editor.FormOutlinePage;
24 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
25
26 public class SchemaFormOutlinePage extends FormOutlinePage {
27     private Object JavaDoc[] fTopics;
28
29     public Object JavaDoc[] getChildren(Object JavaDoc parent) {
30         ISchema schema = (ISchema) fEditor.getAggregateModel();
31         if (schema != null && schema.isValid()) {
32             if (parent instanceof SchemaFormPage) {
33                 return getMarkup();
34             }
35             if (parent instanceof ISchemaElement) {
36                 return getAttributes((ISchemaElement) parent);
37             }
38             if (parent instanceof SchemaOverviewPage) {
39                 return getTopics();
40             }
41         }
42         return super.getChildren(parent);
43     }
44     
45     class SchemaLabelProvider extends BasicLabelProvider {
46         public String JavaDoc getText(Object JavaDoc obj) {
47             String JavaDoc label = getObjectLabel(obj);
48             return (label == null) ? super.getText(obj) : label;
49         }
50     }
51
52     public SchemaFormOutlinePage(PDEFormEditor editor) {
53         super(editor);
54     }
55     
56     public ILabelProvider createLabelProvider() {
57         return new SchemaLabelProvider();
58     }
59
60     protected Object JavaDoc[] createTopics() {
61         ISchema schema = (ISchema) fEditor.getAggregateModel();
62         IDocumentSection[] sections = schema.getDocumentSections();
63         Object JavaDoc[] result = new Object JavaDoc[sections.length + 1];
64         result[0] = schema;
65         for (int i = 1; i <= sections.length; i++) {
66             result[i] = sections[i - 1];
67         }
68         return result;
69     }
70
71     private Object JavaDoc[] getAttributes(ISchemaElement element) {
72         ISchemaType type = element.getType();
73         if (type instanceof ISchemaComplexType) {
74             return ((ISchemaComplexType) type).getAttributes();
75         }
76         return new Object JavaDoc[0];
77     }
78
79     private Object JavaDoc[] getMarkup() {
80         ISchema schema = (ISchema) fEditor.getAggregateModel();
81         return schema.getElements();
82     }
83
84     protected String JavaDoc getObjectLabel(Object JavaDoc obj) {
85         if (obj instanceof ISchema) {
86             return PDEUIMessages.SchemaEditor_topic_overview;
87         }
88         if (obj instanceof IDocumentSection) {
89             IDocumentSection section = (IDocumentSection) obj;
90             String JavaDoc sectionId = section.getSectionId();
91             if (sectionId.equals(IDocumentSection.EXAMPLES))
92                 return PDEUIMessages.SchemaEditor_topic_examples;
93             if (sectionId.equals(IDocumentSection.SINCE))
94                 return PDEUIMessages.SchemaEditor_topic_since;
95             if (sectionId.equals(IDocumentSection.IMPLEMENTATION))
96                 return PDEUIMessages.SchemaEditor_topic_implementation;
97             if (sectionId.equals(IDocumentSection.API_INFO))
98                 return PDEUIMessages.SchemaEditor_topic_api;
99             if (sectionId.equals(IDocumentSection.COPYRIGHT))
100                 return PDEUIMessages.SchemaEditor_topic_copyright;
101         }
102         return null;
103     }
104
105     Object JavaDoc[] getTopics() {
106         if (fTopics == null) {
107             fTopics = createTopics();
108         }
109         return fTopics;
110     }
111
112     protected String JavaDoc getParentPageId(Object JavaDoc item) {
113         if (item instanceof ISchemaElement || item instanceof ISchemaAttribute)
114             return SchemaFormPage.PAGE_ID;
115         if (item instanceof IDocumentSection || item instanceof ISchema)
116             return SchemaOverviewPage.PAGE_ID;
117         return super.getParentPageId(item);
118     }
119
120     public void modelChanged(IModelChangedEvent event) {
121         if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
122             fTopics = null;
123             fTreeViewer.refresh();
124             return;
125         }
126         Object JavaDoc object = event.getChangedObjects()[0];
127         if (event.getChangeType() == IModelChangedEvent.CHANGE) {
128             fTreeViewer.update(object, null);
129         } else {
130             // find the parent
131
Object JavaDoc parent = null;
132             if (object instanceof ISchemaObject) {
133                 parent = ((ISchemaObject) object).getParent();
134             }
135             if (parent != null) {
136                 fTreeViewer.refresh(parent);
137                 fTreeViewer.expandToLevel(parent, 2);
138             } else {
139                 fTreeViewer.refresh();
140                 fTreeViewer.expandAll();
141             }
142         }
143     }
144 }
145
Popular Tags