KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > controller > SchemaElementsController


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.controller;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.directory.ldapstudio.schemas.Activator;
28 import org.apache.directory.ldapstudio.schemas.PluginConstants;
29 import org.apache.directory.ldapstudio.schemas.controller.actions.HideAttributeTypesAction;
30 import org.apache.directory.ldapstudio.schemas.controller.actions.HideObjectClassesAction;
31 import org.apache.directory.ldapstudio.schemas.controller.actions.LinkWithEditorSchemaElementsView;
32 import org.apache.directory.ldapstudio.schemas.controller.actions.OpenSchemaElementsViewSortDialogAction;
33 import org.apache.directory.ldapstudio.schemas.controller.actions.OpenTypeHierarchyAction;
34 import org.apache.directory.ldapstudio.schemas.controller.actions.OpenSchemaElementsViewPreferencesAction;
35 import org.apache.directory.ldapstudio.schemas.view.editors.attributeType.AttributeTypeEditor;
36 import org.apache.directory.ldapstudio.schemas.view.editors.attributeType.AttributeTypeEditorInput;
37 import org.apache.directory.ldapstudio.schemas.view.editors.objectClass.ObjectClassEditor;
38 import org.apache.directory.ldapstudio.schemas.view.editors.objectClass.ObjectClassEditorInput;
39 import org.apache.directory.ldapstudio.schemas.view.views.SchemaElementsView;
40 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper;
41 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.IntermediateNode;
42 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper;
43 import org.apache.log4j.Logger;
44 import org.eclipse.jface.action.Action;
45 import org.eclipse.jface.action.IMenuListener;
46 import org.eclipse.jface.action.IMenuManager;
47 import org.eclipse.jface.action.IToolBarManager;
48 import org.eclipse.jface.action.MenuManager;
49 import org.eclipse.jface.action.Separator;
50 import org.eclipse.jface.util.IPropertyChangeListener;
51 import org.eclipse.jface.util.PropertyChangeEvent;
52 import org.eclipse.jface.viewers.DoubleClickEvent;
53 import org.eclipse.jface.viewers.IDoubleClickListener;
54 import org.eclipse.jface.viewers.StructuredSelection;
55 import org.eclipse.jface.viewers.TreeSelection;
56 import org.eclipse.jface.viewers.TreeViewer;
57 import org.eclipse.ui.IEditorInput;
58 import org.eclipse.ui.IWorkbenchActionConstants;
59 import org.eclipse.ui.IWorkbenchPage;
60 import org.eclipse.ui.PartInitException;
61 import org.eclipse.ui.PlatformUI;
62
63
64 /**
65  * This class implements the Controller for the Schema Elements View.
66  *
67  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
68  * @version $Rev$, $Date$
69  */

70 public class SchemaElementsController
71 {
72     /** The logger */
73     private static Logger logger = Logger.getLogger( SchemaElementsController.class );
74
75     /** The authorized Preferences keys*/
76     List JavaDoc<String JavaDoc> authorizedPrefs;
77
78     /** The associated view */
79     private SchemaElementsView view;
80
81     /** The Context Menu */
82     private MenuManager contextMenu;
83
84     // The Actions
85
private Action hideObjectClasses;
86     private Action hideAttributeTypes;
87     private Action linkWithEditor;
88     private Action openSortDialog;
89     private Action openPreferencePage;
90     private Action openTypeHierarchy;
91
92
93     /**
94      * Creates a new instance of SchemaElementsController.
95      */

96     public SchemaElementsController( SchemaElementsView view )
97     {
98         this.view = view;
99
100         initAuthorizedPrefs();
101         initActions();
102         initToolbar();
103         initMenu();
104         initContextMenu();
105         initDoubleClickListener();
106         initPreferencesListener();
107     }
108
109
110     private void initAuthorizedPrefs()
111     {
112         authorizedPrefs = new ArrayList JavaDoc<String JavaDoc>();
113         authorizedPrefs.add( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_LABEL );
114         authorizedPrefs.add( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_ABBREVIATE );
115         authorizedPrefs.add( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_ABBREVIATE_MAX_LENGTH );
116         authorizedPrefs.add( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SECONDARY_LABEL_DISPLAY );
117         authorizedPrefs.add( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SECONDARY_LABEL );
118         authorizedPrefs.add( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SECONDARY_LABEL_ABBREVIATE );
119         authorizedPrefs.add( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SECONDARY_LABEL_ABBREVIATE_MAX_LENGTH );
120         authorizedPrefs.add( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_GROUPING );
121         authorizedPrefs.add( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_BY );
122         authorizedPrefs.add( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_SORTING_ORDER );
123     }
124
125
126     /**
127      * Initializes the Actions.
128      */

129     private void initActions()
130     {
131         hideObjectClasses = new HideObjectClassesAction( view.getViewer() );
132         hideAttributeTypes = new HideAttributeTypesAction( view.getViewer() );
133         linkWithEditor = new LinkWithEditorSchemaElementsView( view );
134         openSortDialog = new OpenSchemaElementsViewSortDialogAction();
135         openPreferencePage = new OpenSchemaElementsViewPreferencesAction();
136         openTypeHierarchy = new OpenTypeHierarchyAction();
137     }
138
139
140     /**
141      * Initializes the Toolbar.
142      */

143     private void initToolbar()
144     {
145         IToolBarManager toolbar = view.getViewSite().getActionBars().getToolBarManager();
146         toolbar.add( hideObjectClasses );
147         toolbar.add( hideAttributeTypes );
148         toolbar.add( new Separator() );
149         toolbar.add( linkWithEditor );
150     }
151
152
153     /**
154      * Initializes the Menu.
155      */

156     private void initMenu()
157     {
158         IMenuManager menu = view.getViewSite().getActionBars().getMenuManager();
159         menu.add( openSortDialog );
160         menu.add( new Separator() );
161         menu.add( linkWithEditor );
162         menu.add( new Separator() );
163         menu.add( openPreferencePage );
164     }
165
166
167     /**
168      * Initializes the DoubleClickListener
169      */

170     private void initDoubleClickListener()
171     {
172         view.getViewer().addDoubleClickListener( new IDoubleClickListener()
173         {
174             public void doubleClick( DoubleClickEvent event )
175             {
176                 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
177
178                 SchemaElementsView view = ( SchemaElementsView ) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
179                     .getActivePage().findView( SchemaElementsView.ID );
180                 TreeViewer viewer = view.getViewer();
181
182                 // What we get from the treeViewer is a StructuredSelection
183
StructuredSelection selection = ( StructuredSelection ) event.getSelection();
184
185                 // Here's the real object (an AttributeTypeWrapper, ObjectClassWrapper or IntermediateNode)
186
Object JavaDoc objectSelection = selection.getFirstElement();
187                 IEditorInput input = null;
188                 String JavaDoc editorId = null;
189
190                 // Selecting the right editor and input
191
if ( objectSelection instanceof AttributeTypeWrapper )
192                 {
193                     input = new AttributeTypeEditorInput( ( ( AttributeTypeWrapper ) objectSelection )
194                         .getMyAttributeType() );
195                     editorId = AttributeTypeEditor.ID;
196                 }
197                 else if ( objectSelection instanceof ObjectClassWrapper )
198                 {
199                     input = new ObjectClassEditorInput( ( ( ObjectClassWrapper ) objectSelection ).getMyObjectClass() );
200                     editorId = ObjectClassEditor.ID;
201                 }
202                 else if ( objectSelection instanceof IntermediateNode )
203                 {
204                     // Here we don't open an editor, we just expand the node.
205
viewer.setExpandedState( objectSelection, !viewer.getExpandedState( objectSelection ) );
206                 }
207
208                 // Let's open the editor
209
if ( input != null )
210                 {
211                     try
212                     {
213                         page.openEditor( input, editorId );
214                     }
215                     catch ( PartInitException e )
216                     {
217                         logger.debug( "error when opening the editor" ); //$NON-NLS-1$
218
}
219                 }
220             }
221         } );
222     }
223
224
225     /**
226      * Initializes the listener on the preferences store
227      */

228     private void initPreferencesListener()
229     {
230         Activator.getDefault().getPreferenceStore().addPropertyChangeListener( new IPropertyChangeListener()
231         {
232             /* (non-Javadoc)
233              * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
234              */

235             public void propertyChange( PropertyChangeEvent event )
236             {
237                 if ( authorizedPrefs.contains( event.getProperty() ) )
238                 {
239                     if ( PluginConstants.PREFS_SCHEMA_ELEMENTS_VIEW_GROUPING == event.getProperty() )
240                     {
241                         view.refresh();
242                     }
243                     else
244                     {
245                         view.update();
246                     }
247                 }
248             }
249         } );
250     }
251
252
253     /**
254      * Initializes the ContextMenu.
255      */

256     private void initContextMenu()
257     {
258         TreeViewer viewer = view.getViewer();
259         contextMenu = new MenuManager( "" ); //$NON-NLS-1$
260
contextMenu.setRemoveAllWhenShown( true );
261         contextMenu.addMenuListener( new IMenuListener()
262         {
263             public void menuAboutToShow( IMenuManager manager )
264             {
265                 Object JavaDoc selection = ( ( TreeSelection ) view.getViewer().getSelection() ).getFirstElement();
266
267                 if ( ( selection instanceof AttributeTypeWrapper ) )
268                 {
269                     manager.add( openTypeHierarchy );
270                 }
271                 else if ( ( selection instanceof ObjectClassWrapper ) )
272                 {
273                     manager.add( openTypeHierarchy );
274                 }
275
276                 manager.add( new Separator( IWorkbenchActionConstants.MB_ADDITIONS ) );
277             }
278         } );
279
280         // set the context menu to the table viewer
281
viewer.getControl().setMenu( contextMenu.createContextMenu( viewer.getControl() ) );
282
283         // register the context menu to enable extension actions
284
view.getSite().registerContextMenu( contextMenu, viewer );
285     }
286 }
287
Popular Tags