KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinkWithEditorHierarchyView;
30 import org.apache.directory.ldapstudio.schemas.controller.actions.OpenHierarchyViewPreferencesAction;
31 import org.apache.directory.ldapstudio.schemas.controller.actions.ShowSubtypeHierarchyAction;
32 import org.apache.directory.ldapstudio.schemas.controller.actions.ShowSupertypeHierarchyAction;
33 import org.apache.directory.ldapstudio.schemas.model.LDAPModelEvent;
34 import org.apache.directory.ldapstudio.schemas.model.PoolListener;
35 import org.apache.directory.ldapstudio.schemas.model.SchemaPool;
36 import org.apache.directory.ldapstudio.schemas.view.editors.attributeType.AttributeTypeEditor;
37 import org.apache.directory.ldapstudio.schemas.view.editors.attributeType.AttributeTypeEditorInput;
38 import org.apache.directory.ldapstudio.schemas.view.editors.objectClass.ObjectClassEditor;
39 import org.apache.directory.ldapstudio.schemas.view.editors.objectClass.ObjectClassEditorInput;
40 import org.apache.directory.ldapstudio.schemas.view.views.HierarchyView;
41 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper;
42 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper;
43 import org.eclipse.jface.action.Action;
44 import org.eclipse.jface.action.IMenuManager;
45 import org.eclipse.jface.action.IToolBarManager;
46 import org.eclipse.jface.action.Separator;
47 import org.eclipse.jface.util.IPropertyChangeListener;
48 import org.eclipse.jface.util.PropertyChangeEvent;
49 import org.eclipse.jface.viewers.DoubleClickEvent;
50 import org.eclipse.jface.viewers.IDoubleClickListener;
51 import org.eclipse.jface.viewers.StructuredSelection;
52 import org.eclipse.ui.IEditorInput;
53 import org.eclipse.ui.IWorkbenchPage;
54 import org.eclipse.ui.PartInitException;
55 import org.eclipse.ui.PlatformUI;
56
57
58 /**
59  * This class implements the Controller for the Hierarchy View
60  *
61  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
62  * @version $Rev$, $Date$
63  */

64 public class HierarchyViewController implements PoolListener
65 {
66     /** The associated view */
67     private HierarchyView view;
68
69     /** The authorized Preferences keys*/
70     List JavaDoc<String JavaDoc> authorizedPrefs;
71
72     // The Actions
73
private Action showSupertypeHierarchy;
74     private Action showSubtypeHierarchy;
75     private Action linkWithEditor;
76     private Action openPreferencePage;
77
78
79     /**
80      * Creates a new instance of SchemasViewController.
81      *
82      * @param view
83      * the associated view
84      */

85     public HierarchyViewController( HierarchyView view )
86     {
87         this.view = view;
88
89         SchemaPool.getInstance().addListener( this );
90
91         initAuthorizedPrefs();
92         initActions();
93         initToolbar();
94         initMenu();
95         initDoubleClickListener();
96         initPreferencesListener();
97     }
98
99
100     /**
101      * Initializes the values for the authorized preferences.
102      */

103     private void initAuthorizedPrefs()
104     {
105         authorizedPrefs = new ArrayList JavaDoc<String JavaDoc>();
106         authorizedPrefs.add( PluginConstants.PREFS_HIERARCHY_VIEW_LABEL );
107         authorizedPrefs.add( PluginConstants.PREFS_HIERARCHY_VIEW_ABBREVIATE );
108         authorizedPrefs.add( PluginConstants.PREFS_HIERARCHY_VIEW_ABBREVIATE_MAX_LENGTH );
109         authorizedPrefs.add( PluginConstants.PREFS_HIERARCHY_VIEW_SECONDARY_LABEL_DISPLAY );
110         authorizedPrefs.add( PluginConstants.PREFS_HIERARCHY_VIEW_SECONDARY_LABEL );
111         authorizedPrefs.add( PluginConstants.PREFS_HIERARCHY_VIEW_SECONDARY_LABEL_ABBREVIATE );
112         authorizedPrefs.add( PluginConstants.PREFS_HIERARCHY_VIEW_SECONDARY_LABEL_ABBREVIATE_MAX_LENGTH );
113     }
114
115
116     /**
117      * Initializes the Actions.
118      */

119     private void initActions()
120     {
121         // Setting up the default key value (if needed)
122
if ( Activator.getDefault().getDialogSettings().get( PluginConstants.PREFS_HIERARCHY_VIEW_MODE ) == null )
123         {
124             Activator.getDefault().getDialogSettings().put( PluginConstants.PREFS_HIERARCHY_VIEW_MODE,
125                 PluginConstants.PREFS_HIERARCHY_VIEW_MODE_SUPERTYPE );
126         }
127         showSupertypeHierarchy = new ShowSupertypeHierarchyAction( view );
128         showSubtypeHierarchy = new ShowSubtypeHierarchyAction( view );
129         linkWithEditor = new LinkWithEditorHierarchyView( view );
130         openPreferencePage = new OpenHierarchyViewPreferencesAction();
131     }
132
133
134     /**
135      * Initializes the Toolbar.
136      */

137     private void initToolbar()
138     {
139         IToolBarManager toolbar = view.getViewSite().getActionBars().getToolBarManager();
140         toolbar.add( showSupertypeHierarchy );
141         toolbar.add( showSubtypeHierarchy );
142     }
143
144
145     /**
146      * Initializes the Menu.
147      */

148     private void initMenu()
149     {
150         IMenuManager menu = view.getViewSite().getActionBars().getMenuManager();
151         menu.add( showSupertypeHierarchy );
152         menu.add( showSubtypeHierarchy );
153         menu.add( new Separator() );
154         menu.add( linkWithEditor );
155         menu.add( new Separator() );
156         menu.add( openPreferencePage );
157     }
158
159
160     /**
161      * Initializes the DoubleClickListener.
162      */

163     private void initDoubleClickListener()
164     {
165         view.getViewer().addDoubleClickListener( new IDoubleClickListener()
166         {
167             public void doubleClick( DoubleClickEvent event )
168             {
169                 // What we get from the treeViewer is a StructuredSelection
170
StructuredSelection selection = ( StructuredSelection ) event.getSelection();
171
172                 // Here's the real object (an AttributeTypeWrapper, ObjectClassWrapper or IntermediateNode)
173
Object JavaDoc objectSelection = selection.getFirstElement();
174                 IEditorInput input = null;
175                 String JavaDoc editorId = null;
176
177                 // Selecting the right editor and input
178
if ( objectSelection instanceof AttributeTypeWrapper )
179                 {
180                     input = new AttributeTypeEditorInput( ( ( AttributeTypeWrapper ) objectSelection )
181                         .getMyAttributeType() );
182                     editorId = AttributeTypeEditor.ID;
183                 }
184                 else if ( objectSelection instanceof ObjectClassWrapper )
185                 {
186                     input = new ObjectClassEditorInput( ( ( ObjectClassWrapper ) objectSelection ).getMyObjectClass() );
187                     editorId = ObjectClassEditor.ID;
188                 }
189
190                 // Let's open the editor
191
if ( input != null )
192                 {
193                     IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
194                     try
195                     {
196                         page.openEditor( input, editorId );
197                     }
198                     catch ( PartInitException e )
199                     {
200                         // TODO ADD A LOGGER
201
}
202                 }
203             }
204         } );
205     }
206
207
208     /**
209      * Initializes the listener on the preferences store.
210      */

211     private void initPreferencesListener()
212     {
213         Activator.getDefault().getPreferenceStore().addPropertyChangeListener( new IPropertyChangeListener()
214         {
215             /* (non-Javadoc)
216              * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
217              */

218             public void propertyChange( PropertyChangeEvent event )
219             {
220                 if ( authorizedPrefs.contains( event.getProperty() ) )
221                 {
222                     view.refresh();
223                 }
224             }
225         } );
226     }
227
228
229     /* (non-Javadoc)
230      * @see org.apache.directory.ldapstudio.schemas.model.PoolListener#poolChanged(org.apache.directory.ldapstudio.schemas.model.SchemaPool, org.apache.directory.ldapstudio.schemas.model.LDAPModelEvent)
231      */

232     public void poolChanged( SchemaPool p, LDAPModelEvent e )
233     {
234         view.refresh();
235     }
236 }
237
Popular Tags