KickJava   Java API By Example, From Geeks To Geeks.

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


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.HierarchyView;
32 import org.eclipse.jface.action.Action;
33 import org.eclipse.ui.IEditorPart;
34 import org.eclipse.ui.IPartListener2;
35 import org.eclipse.ui.IWorkbenchPart;
36 import org.eclipse.ui.IWorkbenchPartReference;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.plugin.AbstractUIPlugin;
39
40
41 /**
42  * This class implements the Link With Editor Action for the Hierarchy View
43  *
44  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
45  * @version $Rev$, $Date$
46  */

47 public class LinkWithEditorHierarchyView extends Action
48 {
49     /** The String for storing the checked state of the action */
50     private static final String JavaDoc LINK_WITH_EDITOR_HIERARCHY_VIEW_DS_KEY = LinkWithEditorHierarchyView.class.getName()
51         + ".dialogsettingkey"; //$NON-NLS-1$
52

53     /** The associated view */
54     private HierarchyView view;
55
56     /** The listener listening on changes on editors */
57     private IPartListener2 editorListener = new IPartListener2()
58     {
59         /* (non-Javadoc)
60          * @see org.eclipse.ui.IPartListener2#partVisible(org.eclipse.ui.IWorkbenchPartReference)
61          */

62         public void partVisible( IWorkbenchPartReference partRef )
63         {
64             IWorkbenchPart part = partRef.getPart( true );
65
66             if ( part instanceof ObjectClassEditor )
67             {
68                 linkViewWithEditor( ( ( ObjectClassEditor ) part ).getOriginalObjectClass() );
69             }
70             else if ( part instanceof AttributeTypeEditor )
71             {
72                 linkViewWithEditor( ( ( AttributeTypeEditor ) part ).getOriginalAttributeType() );
73             }
74         }
75
76
77         /* (non-Javadoc)
78          * @see org.eclipse.ui.IPartListener2#partActivated(org.eclipse.ui.IWorkbenchPartReference)
79          */

80         public void partActivated( IWorkbenchPartReference partRef )
81         {
82         }
83
84
85         /* (non-Javadoc)
86          * @see org.eclipse.ui.IPartListener2#partClosed(org.eclipse.ui.IWorkbenchPartReference)
87          */

88         public void partClosed( IWorkbenchPartReference partRef )
89         {
90         }
91
92
93         /* (non-Javadoc)
94          * @see org.eclipse.ui.IPartListener2#partDeactivated(org.eclipse.ui.IWorkbenchPartReference)
95          */

96         public void partDeactivated( IWorkbenchPartReference partRef )
97         {
98         }
99
100
101         /* (non-Javadoc)
102          * @see org.eclipse.ui.IPartListener2#partHidden(org.eclipse.ui.IWorkbenchPartReference)
103          */

104         public void partHidden( IWorkbenchPartReference partRef )
105         {
106         }
107
108
109         /* (non-Javadoc)
110          * @see org.eclipse.ui.IPartListener2#partInputChanged(org.eclipse.ui.IWorkbenchPartReference)
111          */

112         public void partInputChanged( IWorkbenchPartReference partRef )
113         {
114         }
115
116
117         /* (non-Javadoc)
118          * @see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
119          */

120         public void partOpened( IWorkbenchPartReference partRef )
121         {
122         }
123
124
125         /* (non-Javadoc)
126          * @see org.eclipse.ui.IPartListener2#partBroughtToTop(org.eclipse.ui.IWorkbenchPartReference)
127          */

128         public void partBroughtToTop( IWorkbenchPartReference partRef )
129         {
130         }
131     };
132
133
134     /**
135      * Creates a new instance of LinkWithEditorSchemasView.
136      *
137      * @param view
138      * the associated view
139      */

140     public LinkWithEditorHierarchyView( HierarchyView view )
141     {
142         super( Messages.getString( "LinkWithEditorHierarchyView.Link_with_Editor" ), AS_CHECK_BOX ); //$NON-NLS-1$
143
setToolTipText( getText() );
144         setId( PluginConstants.CMD_LINK_WITH_EDITOR_SCHEMA_VIEW );
145         setImageDescriptor( AbstractUIPlugin.imageDescriptorFromPlugin( Activator.PLUGIN_ID,
146             PluginConstants.IMG_LINK_WITH_EDITOR ) );
147         setEnabled( true );
148         this.view = view;
149
150         // Setting up the default key value (if needed)
151
if ( Activator.getDefault().getDialogSettings().get( LINK_WITH_EDITOR_HIERARCHY_VIEW_DS_KEY ) == null )
152         {
153             Activator.getDefault().getDialogSettings().put( LINK_WITH_EDITOR_HIERARCHY_VIEW_DS_KEY, false );
154         }
155
156         // Setting state from the dialog settings
157
setChecked( Activator.getDefault().getDialogSettings().getBoolean( LINK_WITH_EDITOR_HIERARCHY_VIEW_DS_KEY ) );
158
159         // Enabling the listeners
160
if ( isChecked() )
161         {
162             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener( editorListener );
163         }
164     }
165
166
167     /* (non-Javadoc)
168      * @see org.eclipse.jface.action.Action#run()
169      */

170     public void run()
171     {
172         setChecked( isChecked() );
173         Activator.getDefault().getDialogSettings().put( LINK_WITH_EDITOR_HIERARCHY_VIEW_DS_KEY, isChecked() );
174
175         if ( isChecked() ) // Enabling the listeners
176
{
177             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener( editorListener );
178
179             IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
180                 .getActiveEditor();
181             if ( activeEditor instanceof ObjectClassEditor )
182             {
183                 linkViewWithEditor( ( ( ObjectClassEditor ) activeEditor ).getOriginalObjectClass() );
184             }
185             else if ( activeEditor instanceof AttributeTypeEditor )
186             {
187                 linkViewWithEditor( ( ( AttributeTypeEditor ) activeEditor ).getOriginalAttributeType() );
188             }
189         }
190         else
191         // Disabling the listeners
192
{
193             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().removePartListener( editorListener );
194         }
195     }
196
197
198     /**
199      * Links the view with the right editor
200      *
201      * @param schemaElement
202      * the Schema Element
203      */

204     private void linkViewWithEditor( SchemaElement schemaElement )
205     {
206         if ( schemaElement instanceof AttributeType )
207         {
208             view.setInput( schemaElement );
209         }
210         else if ( schemaElement instanceof ObjectClass )
211         {
212             view.setInput( schemaElement );
213         }
214     }
215 }
216
Popular Tags