KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > view > views > HierarchyView


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.directory.ldapstudio.schemas.view.views;
21
22
23 import org.apache.directory.ldapstudio.schemas.Activator;
24 import org.apache.directory.ldapstudio.schemas.controller.HierarchyViewController;
25 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ITreeNode;
26 import org.eclipse.jface.viewers.DecoratingLabelProvider;
27 import org.eclipse.jface.viewers.TreeViewer;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.ui.part.ViewPart;
31
32
33 /**
34  * This class implements the Hierarchy View.
35  *
36  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
37  * @version $Rev$, $Date$
38  */

39 public class HierarchyView extends ViewPart
40 {
41     /** The view's ID */
42     public static final String JavaDoc ID = Activator.PLUGIN_ID + ".view.HierarchyView"; //$NON-NLS-1$
43

44     /** The tree viewer */
45     private TreeViewer viewer;
46
47     /** The content provider */
48     private HierarchyViewContentProvider contentProvider;
49
50
51     /* (non-Javadoc)
52      * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
53      */

54     public void createPartControl( Composite parent )
55     {
56         initViewer( parent );
57
58         new HierarchyViewController( this );
59     }
60
61
62     /**
63      * Initializes the Viewer
64      *
65      * @param parent
66      * the parent Composite
67      */

68     private void initViewer( Composite parent )
69     {
70         viewer = new TreeViewer( parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER );
71         contentProvider = new HierarchyViewContentProvider( viewer );
72         viewer.setContentProvider( contentProvider );
73         viewer.setLabelProvider( new DecoratingLabelProvider( new HierarchyViewLabelProvider(), Activator.getDefault()
74             .getWorkbench().getDecoratorManager().getLabelDecorator() ) );
75     }
76
77
78     /* (non-Javadoc)
79      * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
80      */

81     public void setFocus()
82     {
83         viewer.getControl().setFocus();
84     }
85
86
87     /**
88      * Gets the TreeViewer
89      *
90      * @return
91      * the TreeViewer
92      */

93     public TreeViewer getViewer()
94     {
95         return viewer;
96     }
97
98
99     /**
100      * Refreshes the viewer.
101      */

102     public void refresh()
103     {
104         viewer.refresh();
105         viewer.expandAll();
106     }
107
108
109     public void setInput( Object JavaDoc input )
110     {
111         viewer.setInput( input );
112         viewer.expandAll();
113     }
114
115
116     /**
117      * Search for the given element in the Tree and returns it if it has been found.
118      *
119      * @param element
120      * the element to find
121      * @return
122      * the element if it has been found, null if has not been found
123      */

124     public ITreeNode findElementInTree( ITreeNode element )
125     {
126         if ( element == null )
127         {
128             return null;
129         }
130
131         ITreeNode input = ( ITreeNode ) getViewer().getInput();
132
133         return findElementInTree( element, input );
134     }
135
136
137     /**
138      * Search for the given element in the Tree and returns it if it has been found.
139      *
140      * @param element
141      * the element to find
142      * @param node
143      * the current element
144      * @return
145      */

146     public ITreeNode findElementInTree( ITreeNode element, ITreeNode node )
147     {
148         if ( element.equals( node ) )
149         {
150             return node;
151         }
152         else
153         {
154             Object JavaDoc[] children = contentProvider.getChildren( node );
155             for ( Object JavaDoc child : children )
156             {
157                 ITreeNode foundElement = findElementInTree( element, ( ITreeNode ) child );
158                 if ( foundElement != null )
159                 {
160                     return foundElement;
161                 }
162             }
163         }
164         return null;
165     }
166 }
167
Popular Tags