KickJava   Java API By Example, From Geeks To Geeks.

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


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.SchemaElement;
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.views.SchemaElementsView;
32 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper;
33 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ITreeNode;
34 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.IntermediateNode;
35 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper;
36 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.SchemaWrapper;
37 import org.eclipse.jface.action.Action;
38 import org.eclipse.jface.viewers.ISelection;
39 import org.eclipse.jface.viewers.ITreeSelection;
40 import org.eclipse.jface.viewers.StructuredSelection;
41 import org.eclipse.ui.IEditorPart;
42 import org.eclipse.ui.IEditorReference;
43 import org.eclipse.ui.IPartListener2;
44 import org.eclipse.ui.ISelectionListener;
45 import org.eclipse.ui.IWorkbenchPart;
46 import org.eclipse.ui.IWorkbenchPartReference;
47 import org.eclipse.ui.PlatformUI;
48 import org.eclipse.ui.plugin.AbstractUIPlugin;
49
50
51 /**
52  * This class implements the Link With Editor Action for the Schemas View
53  *
54  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
55  * @version $Rev$, $Date$
56  */

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

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

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

94         public void partActivated( IWorkbenchPartReference partRef )
95         {
96         }
97
98
99         /* (non-Javadoc)
100          * @see org.eclipse.ui.IPartListener2#partClosed(org.eclipse.ui.IWorkbenchPartReference)
101          */

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

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

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

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

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

142         public void partBroughtToTop( IWorkbenchPartReference partRef )
143         {
144         }
145     };
146
147     /** The listener listening on changes on the view */
148     private ISelectionListener viewListener = new ISelectionListener()
149     {
150         /* (non-Javadoc)
151          * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
152          */

153         public void selectionChanged( IWorkbenchPart part, ISelection selection )
154         {
155             ITreeSelection iSelection = ( ITreeSelection ) selection;
156
157             Object JavaDoc selectedObject = iSelection.getFirstElement();
158
159             if ( ( selectedObject instanceof SchemaWrapper ) || ( selectedObject instanceof ObjectClassWrapper )
160                 || ( selectedObject instanceof AttributeTypeWrapper ) )
161             {
162                 linkEditorWithView( ( ITreeNode ) selectedObject );
163             }
164         }
165     };
166
167
168     /**
169      * Creates a new instance of LinkWithEditorSchemaElementsView.
170      *
171      * @param view
172      * the associated view
173      */

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

205     public void run()
206     {
207         setChecked( isChecked() );
208         Activator.getDefault().getDialogSettings().put( LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY, isChecked() );
209
210         if ( isChecked() ) // Enabling the listeners
211
{
212             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener( editorListener );
213
214             IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
215                 .getActiveEditor();
216             if ( activeEditor instanceof ObjectClassEditor )
217             {
218                 linkViewWithEditor( ( ( ObjectClassEditor ) activeEditor ).getOriginalObjectClass() );
219             }
220             else if ( activeEditor instanceof AttributeTypeEditor )
221             {
222                 linkViewWithEditor( ( ( AttributeTypeEditor ) activeEditor ).getOriginalAttributeType() );
223             }
224
225             view.getSite().getPage().addPostSelectionListener( viewListener );
226         }
227         else
228         // Disabling the listeners
229
{
230             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().removePartListener( editorListener );
231             view.getSite().getPage().removePostSelectionListener( viewListener );
232         }
233     }
234
235
236     /**
237      * Links the view with the right editor
238      *
239      * @param schemaElement
240      * the Schema Element
241      */

242     private void linkViewWithEditor( SchemaElement schemaElement )
243     {
244         StructuredSelection structuredSelection = null;
245         ITreeNode wrapper = null;
246
247         if ( schemaElement instanceof AttributeType )
248         {
249             wrapper = new AttributeTypeWrapper( ( AttributeType ) schemaElement, null );
250             structuredSelection = new StructuredSelection( wrapper );
251
252             view.getViewer().setSelection( structuredSelection, true );
253         }
254         else if ( schemaElement instanceof ObjectClass )
255         {
256             wrapper = new ObjectClassWrapper( ( ObjectClass ) schemaElement, null );
257             structuredSelection = new StructuredSelection( wrapper );
258         }
259         else
260         {
261             return;
262         }
263
264         Object JavaDoc foundItem = view.getViewer().testFindItem( wrapper );
265         if ( foundItem != null ) // The node we are looking for is already loaded in the TreeViewer
266
{
267             view.getViewer().setSelection( structuredSelection, true );
268         }
269         else
270         // The node we are looking for is not yet loaded in the TreeViewer, we have to find and load it.
271
{
272             ITreeNode foundElement = view.findElementInTree( wrapper );
273
274             if ( foundElement != null )
275             {
276                 expandFromTopToBottom( foundElement );
277                 view.getViewer().setSelection( structuredSelection );
278             }
279         }
280     }
281
282
283     /**
284      * Expands from top to bottom the element and its successive parent (if needed)
285      *
286      * @param element
287      * the bottom element
288      */

289     private void expandFromTopToBottom( Object JavaDoc element )
290     {
291         if ( element instanceof SchemaWrapper )
292         {
293             SchemaWrapper schemaWrapper = ( SchemaWrapper ) element;
294             if ( !view.getViewer().getExpandedState( schemaWrapper ) )
295             {
296                 view.getViewer().setExpandedState( schemaWrapper, true );
297             }
298         }
299         else if ( element instanceof ObjectClassWrapper )
300         {
301             ObjectClassWrapper objectClassWrapper = ( ObjectClassWrapper ) element;
302             expandFromTopToBottom( objectClassWrapper.getParent() );
303             view.getViewer().setExpandedState( objectClassWrapper, true );
304         }
305         else if ( element instanceof AttributeTypeWrapper )
306         {
307             AttributeTypeWrapper attributeTypeWrapper = ( AttributeTypeWrapper ) element;
308             expandFromTopToBottom( attributeTypeWrapper.getParent() );
309             view.getViewer().setExpandedState( attributeTypeWrapper, true );
310         }
311         else if ( element instanceof IntermediateNode )
312         {
313             IntermediateNode intermediateNode = ( IntermediateNode ) element;
314             expandFromTopToBottom( intermediateNode.getParent() );
315             view.getViewer().setExpandedState( intermediateNode, true );
316         }
317     }
318
319
320     /**
321      * Links the editor to the view
322      *
323      * @param wrapper
324      * the selected element in the view
325      */

326     private void linkEditorWithView( ITreeNode wrapper )
327     {
328         if ( wrapper != null )
329         {
330             IEditorReference[] references = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
331                 .getEditorReferences();
332
333             for ( IEditorReference reference : references )
334             {
335                 IWorkbenchPart workbenchPart = reference.getPart( true );
336
337                 if ( ( workbenchPart instanceof ObjectClassEditor ) && ( wrapper instanceof ObjectClassWrapper ) )
338                 {
339                     if ( ( ( ObjectClassEditor ) workbenchPart ).getOriginalObjectClass().equals(
340                         ( ( ObjectClassWrapper ) wrapper ).getMyObjectClass() ) )
341                     {
342                         PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().bringToTop( workbenchPart );
343                         return;
344                     }
345                 }
346                 else if ( ( workbenchPart instanceof AttributeTypeEditor )
347                     && ( wrapper instanceof AttributeTypeWrapper ) )
348                 {
349                     if ( ( ( AttributeTypeEditor ) workbenchPart ).getOriginalAttributeType().equals(
350                         ( ( AttributeTypeWrapper ) wrapper ).getMyAttributeType() ) )
351                     {
352                         PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().bringToTop( workbenchPart );
353                         return;
354                     }
355                 }
356             }
357         }
358     }
359 }
360
Popular Tags