KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.apache.directory.ldapstudio.schemas.view.views;
22
23
24 import org.apache.directory.ldapstudio.schemas.Activator;
25 import org.apache.directory.ldapstudio.schemas.controller.SchemasViewController;
26 import org.apache.directory.ldapstudio.schemas.model.Schema;
27 import org.apache.directory.ldapstudio.schemas.model.SchemaPool;
28 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ITreeNode;
29 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.SchemasViewRoot;
30 import org.apache.log4j.Logger;
31 import org.eclipse.core.runtime.IProgressMonitor;
32 import org.eclipse.jface.viewers.DecoratingLabelProvider;
33 import org.eclipse.jface.viewers.TreeViewer;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.ui.ISaveablePart2;
37 import org.eclipse.ui.part.ViewPart;
38
39
40 /**
41  * This class implements the Schemas View
42  *
43  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
44  * @version $Rev$, $Date$
45  */

46 public class SchemasView extends ViewPart implements ISaveablePart2
47 {
48     /** The ID of the View */
49     public static final String JavaDoc ID = Activator.PLUGIN_ID + ".view.SchemasView"; //$NON-NLS-1$
50

51     /** The logger*/
52     private static Logger logger = Logger.getLogger( SchemasView.class );
53
54     /** The viewer */
55     private TreeViewer viewer;
56
57     /** The content provider of the viewer */
58     private SchemasViewContentProvider contentProvider;
59
60
61     /**
62      * {@inheritDoc}
63      */

64     public void createPartControl( Composite parent )
65     {
66         initViewer( parent );
67
68         // Registering the Viewer, so other views can be notified when the viewer selection changes
69
getSite().setSelectionProvider( viewer );
70
71         // Adding the controller
72
new SchemasViewController( this );
73     }
74
75
76     /**
77      * Initializes the Viewer
78      */

79     private void initViewer( Composite parent )
80     {
81         viewer = new TreeViewer( parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER );
82         contentProvider = new SchemasViewContentProvider( viewer );
83         viewer.setContentProvider( contentProvider );
84         viewer.setLabelProvider( new DecoratingLabelProvider( new SchemasViewLabelProvider(), Activator.getDefault()
85             .getWorkbench().getDecoratorManager().getLabelDecorator() ) );
86         viewer.setInput( new SchemasViewRoot() );
87     }
88
89
90     /**
91      * {@inheritDoc}
92      */

93     public void setFocus()
94     {
95         viewer.getControl().setFocus();
96     }
97
98
99     /**
100      * Refreshes completely the view (reload model and re-display).
101      *
102      * @see refresh() for refreshing only the display.
103      */

104     public void completeRefresh()
105     {
106         Object JavaDoc[] exp = viewer.getExpandedElements();
107
108         // Refresh the tree viewer
109
viewer.setInput( new SchemasViewRoot() );
110
111         // Expand all the previsouly expanded elements
112
for ( Object JavaDoc object : exp )
113         {
114             viewer.setExpandedState( object, true );
115         }
116     }
117
118
119     /**
120      * Refreshes the view (re-display only).
121      *
122      * @see completeRefresh() for a complete refresh.
123      */

124     public void refresh()
125     {
126         viewer.refresh();
127     }
128
129
130     /**
131      * Gets the TreeViewer
132      *
133      * @return
134      * the TreeViewer
135      */

136     public TreeViewer getViewer()
137     {
138         return viewer;
139     }
140
141
142     /**
143      * {@inheritDoc}
144      */

145     public int promptToSaveOnClose()
146     {
147         return ISaveablePart2.YES;
148     }
149
150
151     /**
152      * {@inheritDoc}
153      */

154     public void doSave( IProgressMonitor monitor )
155     {
156         // save schemas on disk
157
try
158         {
159             SchemaPool.getInstance().saveAll( true );
160         }
161         catch ( Exception JavaDoc e )
162         {
163             logger.debug( "error when saving schemas on disk after asking for confirmation" ); //$NON-NLS-1$
164
}
165     }
166
167
168     /**
169      * {@inheritDoc}
170      */

171     public void doSaveAs()
172     {
173     }
174
175
176     /**
177      * {@inheritDoc}
178      */

179     public boolean isDirty()
180     {
181         Schema[] schemas = SchemaPool.getInstance().getSchemas();
182         for ( Schema schema : schemas )
183         {
184             if ( schema.type == Schema.SchemaType.userSchema )
185             {
186                 if ( schema.hasBeenModified() || schema.hasPendingModification() )
187                 {
188                     return true;
189                 }
190             }
191         }
192
193         // Default value
194
return false;
195     }
196
197
198     /**
199      * {@inheritDoc}
200      */

201     public boolean isSaveAsAllowed()
202     {
203         return false;
204     }
205
206
207     /**
208      * {@inheritDoc}
209      */

210     public boolean isSaveOnCloseNeeded()
211     {
212         return true;
213     }
214
215
216     /**
217      * Search for the given element in the Tree and returns it if it has been found.
218      *
219      * @param element
220      * the element to find
221      * @return
222      * the element if it has been found, null if has not been found
223      */

224     public ITreeNode findElementInTree( ITreeNode element )
225     {
226         if ( element == null )
227         {
228             return null;
229         }
230
231         ITreeNode input = ( ITreeNode ) getViewer().getInput();
232
233         return findElementInTree( element, input );
234     }
235
236
237     /**
238      * Search for the given element in the Tree and returns it if it has been found.
239      *
240      * @param element
241      * the element to find
242      * @param node
243      * the current element
244      * @return
245      */

246     public ITreeNode findElementInTree( ITreeNode element, ITreeNode node )
247     {
248         if ( element.equals( node ) )
249         {
250             return node;
251         }
252         else
253         {
254             Object JavaDoc[] children = contentProvider.getChildren( node );
255             for ( Object JavaDoc child : children )
256             {
257                 ITreeNode foundElement = findElementInTree( element, ( ITreeNode ) child );
258                 if ( foundElement != null )
259                 {
260                     return foundElement;
261                 }
262             }
263         }
264         return null;
265     }
266 }
267
Popular Tags