KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > controller > actions > LinkWithEditorSchemasView


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.controller.actions;
21
22
23 import org.apache.directory.ldapstudio.schemas.Activator;
24 import org.apache.directory.ldapstudio.schemas.Messages;
25 import org.apache.directory.ldapstudio.schemas.PluginConstants;
26 import org.apache.directory.ldapstudio.schemas.model.AttributeType;
27 import org.apache.directory.ldapstudio.schemas.model.ObjectClass;
28 import org.apache.directory.ldapstudio.schemas.model.Schema;
29 import org.apache.directory.ldapstudio.schemas.view.editors.attributeType.AttributeTypeEditor;
30 import org.apache.directory.ldapstudio.schemas.view.editors.objectClass.ObjectClassEditor;
31 import org.apache.directory.ldapstudio.schemas.view.editors.schema.SchemaEditor;
32 import org.apache.directory.ldapstudio.schemas.view.views.SchemasView;
33 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper;
34 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ITreeNode;
35 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.IntermediateNode;
36 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper;
37 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.SchemaWrapper;
38 import org.eclipse.jface.action.Action;
39 import org.eclipse.jface.viewers.ISelection;
40 import org.eclipse.jface.viewers.ITreeSelection;
41 import org.eclipse.jface.viewers.StructuredSelection;
42 import org.eclipse.ui.IEditorPart;
43 import org.eclipse.ui.IEditorReference;
44 import org.eclipse.ui.IPartListener2;
45 import org.eclipse.ui.ISelectionListener;
46 import org.eclipse.ui.IWorkbenchPart;
47 import org.eclipse.ui.IWorkbenchPartReference;
48 import org.eclipse.ui.PlatformUI;
49 import org.eclipse.ui.plugin.AbstractUIPlugin;
50
51
52 /**
53  * This class implements the Link With Editor Action for the Schemas View
54  *
55  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
56  * @version $Rev$, $Date$
57  */

58 public class LinkWithEditorSchemasView extends Action
59 {
60     /** The String for storing the checked state of the action */
61     private static final String JavaDoc LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY = LinkWithEditorSchemasView.class.getName()
62         + ".dialogsettingkey"; //$NON-NLS-1$
63

64     /** The associated view */
65     private SchemasView schemasView;
66
67     /** The listener listening on changes on editors */
68     private IPartListener2 editorListener = new IPartListener2()
69     {
70         /* (non-Javadoc)
71          * @see org.eclipse.ui.IPartListener2#partVisible(org.eclipse.ui.IWorkbenchPartReference)
72          */

73         public void partVisible( IWorkbenchPartReference partRef )
74         {
75             IWorkbenchPart part = partRef.getPart( true );
76
77             if ( part instanceof ObjectClassEditor )
78             {
79                 schemasView.getSite().getPage().removePostSelectionListener( viewListener );
80                 linkViewWithEditor( ( ( ObjectClassEditor ) part ).getOriginalObjectClass() );
81                 schemasView.getSite().getPage().addPostSelectionListener( viewListener );
82             }
83             else if ( part instanceof AttributeTypeEditor )
84             {
85                 schemasView.getSite().getPage().removePostSelectionListener( viewListener );
86                 linkViewWithEditor( ( ( AttributeTypeEditor ) part ).getOriginalAttributeType() );
87                 schemasView.getSite().getPage().addPostSelectionListener( viewListener );
88             }
89             else if ( part instanceof SchemaEditor )
90             {
91                 schemasView.getSite().getPage().removePostSelectionListener( viewListener );
92                 linkViewWithEditor( ( ( SchemaEditor ) part ).getSchema() );
93                 schemasView.getSite().getPage().addPostSelectionListener( viewListener );
94             }
95         }
96
97
98         /* (non-Javadoc)
99          * @see org.eclipse.ui.IPartListener2#partActivated(org.eclipse.ui.IWorkbenchPartReference)
100          */

101         public void partActivated( IWorkbenchPartReference partRef )
102         {
103         }
104
105
106         /* (non-Javadoc)
107          * @see org.eclipse.ui.IPartListener2#partClosed(org.eclipse.ui.IWorkbenchPartReference)
108          */

109         public void partClosed( IWorkbenchPartReference partRef )
110         {
111         }
112
113
114         /* (non-Javadoc)
115          * @see org.eclipse.ui.IPartListener2#partDeactivated(org.eclipse.ui.IWorkbenchPartReference)
116          */

117         public void partDeactivated( IWorkbenchPartReference partRef )
118         {
119         }
120
121
122         /* (non-Javadoc)
123          * @see org.eclipse.ui.IPartListener2#partHidden(org.eclipse.ui.IWorkbenchPartReference)
124          */

125         public void partHidden( IWorkbenchPartReference partRef )
126         {
127         }
128
129
130         /* (non-Javadoc)
131          * @see org.eclipse.ui.IPartListener2#partInputChanged(org.eclipse.ui.IWorkbenchPartReference)
132          */

133         public void partInputChanged( IWorkbenchPartReference partRef )
134         {
135         }
136
137
138         /* (non-Javadoc)
139          * @see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
140          */

141         public void partOpened( IWorkbenchPartReference partRef )
142         {
143         }
144
145
146         /* (non-Javadoc)
147          * @see org.eclipse.ui.IPartListener2#partBroughtToTop(org.eclipse.ui.IWorkbenchPartReference)
148          */

149         public void partBroughtToTop( IWorkbenchPartReference partRef )
150         {
151         }
152     };
153
154     /** The listener listening on changes on the view */
155     private ISelectionListener viewListener = new ISelectionListener()
156     {
157         /* (non-Javadoc)
158          * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
159          */

160         public void selectionChanged( IWorkbenchPart part, ISelection selection )
161         {
162             ITreeSelection iSelection = ( ITreeSelection ) selection;
163
164             Object JavaDoc selectedObject = iSelection.getFirstElement();
165
166             if ( ( selectedObject instanceof SchemaWrapper ) || ( selectedObject instanceof ObjectClassWrapper )
167                 || ( selectedObject instanceof AttributeTypeWrapper ) )
168             {
169                 linkEditorWithView( ( ITreeNode ) selectedObject );
170             }
171         }
172     };
173
174
175     /**
176      * Creates a new instance of LinkWithEditorSchemasView.
177      *
178      * @param view
179      * the associated view
180      */

181     public LinkWithEditorSchemasView( SchemasView view )
182     {
183         super( Messages.getString( "LinkWithEditorSchemasView.Link_with_Editor" ), AS_CHECK_BOX ); //$NON-NLS-1$
184
setToolTipText( getText() );
185         setId( PluginConstants.CMD_LINK_WITH_EDITOR_SCHEMA_VIEW );
186         setImageDescriptor( AbstractUIPlugin.imageDescriptorFromPlugin( Activator.PLUGIN_ID,
187             PluginConstants.IMG_LINK_WITH_EDITOR ) );
188         setEnabled( true );
189         schemasView = view;
190
191         // Setting up the default key value (if needed)
192
if ( Activator.getDefault().getDialogSettings().get( LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY ) == null )
193         {
194             Activator.getDefault().getDialogSettings().put( LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY, false );
195         }
196
197         // Setting state from the dialog settings
198
setChecked( Activator.getDefault().getDialogSettings().getBoolean( LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY ) );
199
200         // Enabling the listeners
201
if ( isChecked() )
202         {
203             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener( editorListener );
204             schemasView.getSite().getPage().addPostSelectionListener( viewListener );
205         }
206     }
207
208
209     /* (non-Javadoc)
210      * @see org.eclipse.jface.action.Action#run()
211      */

212     public void run()
213     {
214         setChecked( isChecked() );
215         Activator.getDefault().getDialogSettings().put( LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY, isChecked() );
216
217         if ( isChecked() ) // Enabling the listeners
218
{
219             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener( editorListener );
220
221             IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
222                 .getActiveEditor();
223             if ( activeEditor instanceof ObjectClassEditor )
224             {
225                 schemasView.getSite().getPage().removePostSelectionListener( viewListener );
226                 linkViewWithEditor( ( ( ObjectClassEditor ) activeEditor ).getOriginalObjectClass() );
227                 schemasView.getSite().getPage().addPostSelectionListener( viewListener );
228             }
229             else if ( activeEditor instanceof AttributeTypeEditor )
230             {
231                 schemasView.getSite().getPage().removePostSelectionListener( viewListener );
232                 linkViewWithEditor( ( ( AttributeTypeEditor ) activeEditor ).getOriginalAttributeType() );
233                 schemasView.getSite().getPage().addPostSelectionListener( viewListener );
234             }
235             else if ( activeEditor instanceof SchemaEditor )
236             {
237                 schemasView.getSite().getPage().removePostSelectionListener( viewListener );
238                 linkViewWithEditor( ( ( SchemaEditor ) activeEditor ).getSchema() );
239                 schemasView.getSite().getPage().addPostSelectionListener( viewListener );
240             }
241
242             schemasView.getSite().getPage().addPostSelectionListener( viewListener );
243         }
244         else
245         // Disabling the listeners
246
{
247             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().removePartListener( editorListener );
248             schemasView.getSite().getPage().removePostSelectionListener( viewListener );
249         }
250     }
251
252
253     /**
254      * Links the view with the right editor
255      *
256      * @param o
257      * the object
258      */

259     private void linkViewWithEditor( Object JavaDoc o )
260     {
261         StructuredSelection structuredSelection = null;
262         ITreeNode wrapper = null;
263
264         if ( o instanceof AttributeType )
265         {
266             wrapper = new AttributeTypeWrapper( ( AttributeType ) o, null );
267             structuredSelection = new StructuredSelection( wrapper );
268         }
269         else if ( o instanceof ObjectClass )
270         {
271             wrapper = new ObjectClassWrapper( ( ObjectClass ) o, null );
272             structuredSelection = new StructuredSelection( wrapper );
273         }
274         else if ( o instanceof Schema )
275         {
276             wrapper = new SchemaWrapper( ( Schema ) o, null );
277             structuredSelection = new StructuredSelection( wrapper );
278         }
279         else
280         {
281             // If the editor isn't an attribute type editor or object class editor, we return
282
return;
283         }
284
285         Object JavaDoc foundItem = schemasView.getViewer().testFindItem( wrapper );
286         if ( foundItem != null ) // The node we are looking for is already loaded in the TreeViewer
287
{
288             schemasView.getViewer().setSelection( structuredSelection, true );
289         }
290         else
291         // The node we are looking for is not yet loaded in the TreeViewer, we have to find and load it.
292
{
293             ITreeNode foundElement = schemasView.findElementInTree( wrapper );
294
295             if ( foundElement != null )
296             {
297                 expandFromTopToBottom( foundElement );
298                 schemasView.getViewer().setSelection( structuredSelection );
299             }
300         }
301     }
302
303
304     /**
305      * Expands from top to bottom the element and its successive parent (if needed)
306      *
307      * @param element
308      * the bottom element
309      */

310     private void expandFromTopToBottom( Object JavaDoc element )
311     {
312         if ( element instanceof SchemaWrapper )
313         {
314             SchemaWrapper schemaWrapper = ( SchemaWrapper ) element;
315             if ( !schemasView.getViewer().getExpandedState( schemaWrapper ) )
316             {
317                 schemasView.getViewer().setExpandedState( schemaWrapper, true );
318             }
319         }
320         else if ( element instanceof ObjectClassWrapper )
321         {
322             ObjectClassWrapper objectClassWrapper = ( ObjectClassWrapper ) element;
323             expandFromTopToBottom( objectClassWrapper.getParent() );
324             schemasView.getViewer().setExpandedState( objectClassWrapper, true );
325         }
326         else if ( element instanceof AttributeTypeWrapper )
327         {
328             AttributeTypeWrapper attributeTypeWrapper = ( AttributeTypeWrapper ) element;
329             expandFromTopToBottom( attributeTypeWrapper.getParent() );
330             schemasView.getViewer().setExpandedState( attributeTypeWrapper, true );
331         }
332         else if ( element instanceof IntermediateNode )
333         {
334             IntermediateNode intermediateNode = ( IntermediateNode ) element;
335             expandFromTopToBottom( intermediateNode.getParent() );
336             schemasView.getViewer().setExpandedState( intermediateNode, true );
337         }
338     }
339
340
341     /**
342      * Links the editor to the view
343      *
344      * @param wrapper
345      * the selected element in the view
346      */

347     private void linkEditorWithView( ITreeNode wrapper )
348     {
349         IEditorReference[] references = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
350             .getEditorReferences();
351
352         for ( IEditorReference reference : references )
353         {
354             IWorkbenchPart workbenchPart = reference.getPart( true );
355
356             if ( ( ( workbenchPart instanceof ObjectClassEditor ) && ( wrapper instanceof ObjectClassWrapper ) && ( reference
357                 .getPartName().equals( ( ( ObjectClassWrapper ) wrapper ).getMyObjectClass().getNames()[0] ) ) )
358                 || ( ( workbenchPart instanceof AttributeTypeEditor ) && ( wrapper instanceof AttributeTypeWrapper ) && ( reference
359                     .getPartName().equals( ( ( AttributeTypeWrapper ) wrapper ).getMyAttributeType().getNames()[0] ) ) )
360                 || ( ( workbenchPart instanceof SchemaEditor ) && ( wrapper instanceof SchemaWrapper ) && ( reference
361                     .getPartName().equals( ( ( SchemaWrapper ) wrapper ).getMySchema().getName() ) ) ) )
362             {
363                 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().bringToTop( workbenchPart );
364                 return;
365             }
366         }
367     }
368 }
369
Popular Tags