KickJava   Java API By Example, From Geeks To Geeks.

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


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.Messages;
29 import org.apache.directory.ldapstudio.schemas.PluginConstants;
30 import org.apache.directory.ldapstudio.schemas.controller.actions.CollapseAllAction;
31 import org.apache.directory.ldapstudio.schemas.controller.actions.CreateANewAttributeTypeAction;
32 import org.apache.directory.ldapstudio.schemas.controller.actions.CreateANewObjectClassAction;
33 import org.apache.directory.ldapstudio.schemas.controller.actions.CreateANewSchemaAction;
34 import org.apache.directory.ldapstudio.schemas.controller.actions.DeleteAction;
35 import org.apache.directory.ldapstudio.schemas.controller.actions.ExportSchemaForADSAction;
36 import org.apache.directory.ldapstudio.schemas.controller.actions.LinkWithEditorSchemasView;
37 import org.apache.directory.ldapstudio.schemas.controller.actions.OpenLocalFileAction;
38 import org.apache.directory.ldapstudio.schemas.controller.actions.OpenSchemaSourceCode;
39 import org.apache.directory.ldapstudio.schemas.controller.actions.OpenSchemasViewPreferencesAction;
40 import org.apache.directory.ldapstudio.schemas.controller.actions.OpenSchemasViewSortDialogAction;
41 import org.apache.directory.ldapstudio.schemas.controller.actions.OpenTypeHierarchyAction;
42 import org.apache.directory.ldapstudio.schemas.controller.actions.RemoveSchemaAction;
43 import org.apache.directory.ldapstudio.schemas.controller.actions.SaveAction;
44 import org.apache.directory.ldapstudio.schemas.controller.actions.SaveAsAction;
45 import org.apache.directory.ldapstudio.schemas.model.Schema;
46 import org.apache.directory.ldapstudio.schemas.model.SchemaCreationException;
47 import org.apache.directory.ldapstudio.schemas.model.SchemaPool;
48 import org.apache.directory.ldapstudio.schemas.model.Schema.SchemaType;
49 import org.apache.directory.ldapstudio.schemas.view.editors.attributeType.AttributeTypeEditor;
50 import org.apache.directory.ldapstudio.schemas.view.editors.attributeType.AttributeTypeEditorInput;
51 import org.apache.directory.ldapstudio.schemas.view.editors.objectClass.ObjectClassEditor;
52 import org.apache.directory.ldapstudio.schemas.view.editors.objectClass.ObjectClassEditorInput;
53 import org.apache.directory.ldapstudio.schemas.view.views.SchemasView;
54 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper;
55 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.IntermediateNode;
56 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper;
57 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.SchemaWrapper;
58 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.IntermediateNode.IntermediateNodeType;
59 import org.apache.log4j.Logger;
60 import org.eclipse.jface.action.Action;
61 import org.eclipse.jface.action.IAction;
62 import org.eclipse.jface.action.IMenuListener;
63 import org.eclipse.jface.action.IMenuManager;
64 import org.eclipse.jface.action.IToolBarManager;
65 import org.eclipse.jface.action.MenuManager;
66 import org.eclipse.jface.action.Separator;
67 import org.eclipse.jface.util.IPropertyChangeListener;
68 import org.eclipse.jface.util.PropertyChangeEvent;
69 import org.eclipse.jface.viewers.DoubleClickEvent;
70 import org.eclipse.jface.viewers.IDoubleClickListener;
71 import org.eclipse.jface.viewers.ISelection;
72 import org.eclipse.jface.viewers.StructuredSelection;
73 import org.eclipse.jface.viewers.TreeSelection;
74 import org.eclipse.jface.viewers.TreeViewer;
75 import org.eclipse.swt.dnd.DND;
76 import org.eclipse.swt.dnd.DropTarget;
77 import org.eclipse.swt.dnd.DropTargetAdapter;
78 import org.eclipse.swt.dnd.DropTargetEvent;
79 import org.eclipse.swt.dnd.FileTransfer;
80 import org.eclipse.swt.dnd.Transfer;
81 import org.eclipse.swt.dnd.TransferData;
82 import org.eclipse.ui.IEditorInput;
83 import org.eclipse.ui.ISelectionListener;
84 import org.eclipse.ui.IWorkbenchActionConstants;
85 import org.eclipse.ui.IWorkbenchPage;
86 import org.eclipse.ui.IWorkbenchPart;
87 import org.eclipse.ui.PartInitException;
88 import org.eclipse.ui.PlatformUI;
89
90
91 /**
92  * This class implements the Controller for the Schemas View
93  *
94  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
95  * @version $Rev$, $Date$
96  */

97 public class SchemasViewController
98 {
99     /** The logger */
100     private static Logger logger = Logger.getLogger( SchemasViewController.class );
101
102     /** The associated view */
103     private SchemasView view;
104
105     /** The authorized Preferences keys*/
106     List JavaDoc<String JavaDoc> authorizedPrefs;
107
108     /** The Drag'n'Drop FileTransfer Object */
109     private final static FileTransfer fileTransfer = FileTransfer.getInstance();
110
111     /** The Context Menu */
112     private MenuManager contextMenu;
113
114     // The Actions
115
private Action openLocalFile;
116     private Action createANewSchema;
117     private Action removeSchema;
118     private Action createANewObjectClass;
119     private Action createANewAttributeType;
120     private Action deleteAction;
121     private Action collapseAll;
122     private Action linkWithEditor;
123     private Action openSchemaSourceCode;
124     private Action save;
125     private Action saveAs;
126     private Action openSortDialog;
127     private Action openPreferencePage;
128     private Action openTypeHierarchy;
129     private Action exportSchemaForADS;
130
131
132     /**
133      * Creates a new instance of SchemasViewController.
134      *
135      * @param view
136      * the associated view
137      */

138     public SchemasViewController( SchemasView view )
139     {
140         this.view = view;
141
142         initAuthorizedPrefs();
143         initActions();
144         initToolbar();
145         initMenu();
146         initContextMenu();
147         initDragAndDrop();
148         initDoubleClickListener();
149         registerUpdateActions();
150         initPreferencesListener();
151     }
152
153
154     /**
155      * Initializes the values for the authorized preferences.
156      */

157     private void initAuthorizedPrefs()
158     {
159         authorizedPrefs = new ArrayList JavaDoc<String JavaDoc>();
160         authorizedPrefs.add( PluginConstants.PREFS_SCHEMAS_VIEW_LABEL );
161         authorizedPrefs.add( PluginConstants.PREFS_SCHEMAS_VIEW_ABBREVIATE );
162         authorizedPrefs.add( PluginConstants.PREFS_SCHEMAS_VIEW_ABBREVIATE_MAX_LENGTH );
163         authorizedPrefs.add( PluginConstants.PREFS_SCHEMAS_VIEW_GROUPING );
164         authorizedPrefs.add( PluginConstants.PREFS_SCHEMAS_VIEW_SORTING_BY );
165         authorizedPrefs.add( PluginConstants.PREFS_SCHEMAS_VIEW_SORTING_ORDER );
166     }
167
168
169     /**
170      * Initializes the Actions.
171      */

172     private void initActions()
173     {
174         openLocalFile = new OpenLocalFileAction();
175         createANewSchema = new CreateANewSchemaAction();
176         removeSchema = new RemoveSchemaAction();
177         createANewObjectClass = new CreateANewObjectClassAction();
178         createANewAttributeType = new CreateANewAttributeTypeAction();
179         deleteAction = new DeleteAction();
180         collapseAll = new CollapseAllAction( view.getViewer() );
181         linkWithEditor = new LinkWithEditorSchemasView( view );
182         openSchemaSourceCode = new OpenSchemaSourceCode();
183         save = new SaveAction();
184         saveAs = new SaveAsAction();
185         openSortDialog = new OpenSchemasViewSortDialogAction();
186         openPreferencePage = new OpenSchemasViewPreferencesAction();
187         openTypeHierarchy = new OpenTypeHierarchyAction();
188         exportSchemaForADS = new ExportSchemaForADSAction( view );
189     }
190
191
192     /**
193      * Initializes the Toolbar.
194      */

195     private void initToolbar()
196     {
197         IToolBarManager toolbar = view.getViewSite().getActionBars().getToolBarManager();
198         toolbar.add( openLocalFile );
199         toolbar.add( createANewSchema );
200         toolbar.add( removeSchema );
201         toolbar.add( new Separator() );
202         toolbar.add( createANewObjectClass );
203         toolbar.add( createANewAttributeType );
204         toolbar.add( ( IAction ) deleteAction );
205         toolbar.add( new Separator() );
206         toolbar.add( collapseAll );
207         toolbar.add( linkWithEditor );
208     }
209
210
211     /**
212      * Initializes the Menu.
213      */

214     private void initMenu()
215     {
216         IMenuManager menu = view.getViewSite().getActionBars().getMenuManager();
217         menu.add( openSortDialog );
218         menu.add( new Separator() );
219         menu.add( linkWithEditor );
220         menu.add( new Separator() );
221         menu.add( openPreferencePage );
222     }
223
224
225     /**
226      * Initializes the ContextMenu.
227      */

228     private void initContextMenu()
229     {
230         TreeViewer viewer = view.getViewer();
231
232         contextMenu = new MenuManager( "" ); //$NON-NLS-1$
233
contextMenu.setRemoveAllWhenShown( true );
234         contextMenu.addMenuListener( new IMenuListener()
235         {
236             /**
237              * {@inheritDoc}
238              */

239             public void menuAboutToShow( IMenuManager manager )
240             {
241                 Object JavaDoc selection = ( ( TreeSelection ) view.getViewer().getSelection() ).getFirstElement();
242
243                 if ( selection instanceof SchemaWrapper )
244                 {
245                     manager.add( openLocalFile );
246                     manager.add( new Separator() );
247                     Schema schema = ( ( SchemaWrapper ) selection ).getMySchema();
248                     if ( schema.type == SchemaType.coreSchema )
249                     {
250                         manager.add( saveAs );
251                         manager.add( new Separator() );
252                         manager.add( exportSchemaForADS);
253                         manager.add( new Separator() );
254                         manager.add( openSchemaSourceCode );
255                     }
256                     else if ( schema.type == SchemaType.userSchema )
257                     {
258                         manager.add( createANewObjectClass );
259                         manager.add( createANewAttributeType );
260                         manager.add( new Separator() );
261                         manager.add( save );
262                         manager.add( saveAs );
263                         manager.add( removeSchema );
264                         manager.add( new Separator() );
265                         manager.add( exportSchemaForADS);
266                         manager.add( new Separator() );
267                         manager.add( openSchemaSourceCode );
268                     }
269                 }
270                 else if ( selection instanceof IntermediateNode )
271                 {
272                     if ( ( ( IntermediateNode ) selection ).getType() == IntermediateNodeType.ATTRIBUTE_TYPE_FOLDER )
273                     {
274                         manager.add( createANewAttributeType );
275                     }
276                     else if ( ( ( IntermediateNode ) selection ).getType() == IntermediateNodeType.OBJECT_CLASS_FOLDER )
277                     {
278                         manager.add( createANewObjectClass );
279                     }
280                 }
281                 else if ( ( selection instanceof AttributeTypeWrapper ) )
282                 {
283                     manager.add( openTypeHierarchy );
284                     manager.add( deleteAction );
285                     manager.add( new Separator() );
286                     manager.add( createANewAttributeType );
287                 }
288                 else if ( ( selection instanceof ObjectClassWrapper ) )
289                 {
290                     manager.add( openTypeHierarchy );
291                     manager.add( deleteAction );
292                     manager.add( new Separator() );
293                     manager.add( createANewObjectClass );
294                 }
295                 else
296                 {
297                     // Nothing is selected
298
if ( selection == null )
299                     {
300                         manager.add( createANewSchema );
301                     }
302                 }
303
304                 manager.add( new Separator( IWorkbenchActionConstants.MB_ADDITIONS ) );
305             }
306         } );
307
308         // set the context menu to the table viewer
309
viewer.getControl().setMenu( contextMenu.createContextMenu( viewer.getControl() ) );
310
311         // register the context menu to enable extension actions
312
view.getSite().registerContextMenu( contextMenu, viewer );
313     }
314
315
316     /**
317      * Initializes the DragNDrop support.
318      */

319     private void initDragAndDrop()
320     {
321         DropTarget target = new DropTarget( view.getViewer().getControl(), DND.DROP_COPY );
322         //we only support file dropping on the viewer
323
Transfer[] types = new Transfer[]
324             { FileTransfer.getInstance() };
325         target.setTransfer( types );
326         target.addDropListener( new DropTargetAdapter()
327         {
328             /**
329              * {@inheritDoc}
330              */

331             public void dragEnter( DropTargetEvent event )
332             {
333                 if ( ( event.operations & DND.DROP_COPY ) != 0 )
334                 {
335                     event.detail = DND.DROP_COPY;
336                 }
337                 else
338                 {
339                     event.detail = DND.DROP_NONE;
340                 }
341
342                 //we only want files
343
for ( TransferData dataType : event.dataTypes )
344                 {
345                     if ( fileTransfer.isSupportedType( dataType ) )
346                     {
347                         event.currentDataType = dataType;
348                         break;
349                     }
350                 }
351             }
352
353
354             /**
355              * {@inheritDoc}
356              */

357             public void drop( DropTargetEvent event )
358             {
359                 if ( fileTransfer.isSupportedType( event.currentDataType ) )
360                 {
361                     SchemaPool pool = SchemaPool.getInstance();
362                     String JavaDoc[] files = ( String JavaDoc[] ) event.data;
363                     for ( String JavaDoc file : files )
364                     {
365                         try
366                         {
367                             pool.addSchema( Schema.localPathToURL( file ) );
368                         }
369                         catch ( SchemaCreationException e )
370                         {
371                             logger.debug( "error when initializing new schema after drag&drop: " + file ); //$NON-NLS-1$
372
}
373                     }
374                 }
375             }
376         } );
377     }
378
379
380     /**
381      * Initializes the DoubleClickListener.
382      */

383     private void initDoubleClickListener()
384     {
385         view.getViewer().addDoubleClickListener( new IDoubleClickListener()
386         {
387             /**
388              * {@inheritDoc}
389              */

390             public void doubleClick( DoubleClickEvent event )
391             {
392                 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
393
394                 TreeViewer viewer = view.getViewer();
395
396                 // What we get from the treeViewer is a StructuredSelection
397
StructuredSelection selection = ( StructuredSelection ) event.getSelection();
398
399                 // Here's the real object (an AttributeTypeWrapper, ObjectClassWrapper or IntermediateNode)
400
Object JavaDoc objectSelection = selection.getFirstElement();
401                 IEditorInput input = null;
402                 String JavaDoc editorId = null;
403
404                 // Selecting the right editor and input
405
if ( objectSelection instanceof AttributeTypeWrapper )
406                 {
407                     input = new AttributeTypeEditorInput( ( ( AttributeTypeWrapper ) objectSelection )
408                         .getMyAttributeType() );
409                     editorId = AttributeTypeEditor.ID;
410                 }
411                 else if ( objectSelection instanceof ObjectClassWrapper )
412                 {
413                     input = new ObjectClassEditorInput( ( ( ObjectClassWrapper ) objectSelection ).getMyObjectClass() );
414                     editorId = ObjectClassEditor.ID;
415                 }
416                 else if ( ( objectSelection instanceof IntermediateNode )
417                     || ( objectSelection instanceof SchemaWrapper ) )
418                 {
419                     // Here we don't open an editor, we just expand the node.
420
viewer.setExpandedState( objectSelection, !viewer.getExpandedState( objectSelection ) );
421                 }
422
423                 // Let's open the editor
424
if ( input != null )
425                 {
426                     try
427                     {
428                         page.openEditor( input, editorId );
429                     }
430                     catch ( PartInitException e )
431                     {
432                         logger.debug( "error when opening the editor" ); //$NON-NLS-1$
433
}
434                 }
435             }
436         } );
437     }
438
439
440     /**
441      * Registers a Listener on the Schemas View and enable/disable the
442      * Actions according to the selection.
443      */

444     private void registerUpdateActions()
445     {
446         // Handling selection of the Browser View to enable/disable the Actions
447
view.getSite().getPage().addSelectionListener( new ISelectionListener()
448         {
449             /**
450              * {@inheritDoc}
451              */

452             public void selectionChanged( IWorkbenchPart part, ISelection selection )
453             {
454                 if ( ! ( selection instanceof TreeSelection ) )
455                 {
456                     return;
457                 }
458                 
459                 TreeSelection treeSelection = ( TreeSelection ) selection;
460
461                 Object JavaDoc selectedObject = ( ( TreeSelection ) selection ).getFirstElement();
462
463                 if ( treeSelection.size() != 1 || selectedObject == null )
464                 {
465                     removeSchema.setEnabled( false );
466                     createANewObjectClass.setEnabled( false );
467                     createANewAttributeType.setEnabled( false );
468                     deleteAction.setEnabled( false );
469                 }
470                 else if ( selectedObject instanceof SchemaWrapper )
471                 {
472                     SchemaWrapper schemaWrapper = ( SchemaWrapper ) selectedObject;
473
474                     if ( schemaWrapper.getMySchema().type == SchemaType.coreSchema )
475                     {
476                         removeSchema.setEnabled( false );
477                         createANewObjectClass.setEnabled( false );
478                         createANewAttributeType.setEnabled( false );
479                         deleteAction.setEnabled( false );
480                     }
481                     else
482                     {
483                         removeSchema.setEnabled( true );
484                         createANewObjectClass.setEnabled( true );
485                         createANewAttributeType.setEnabled( true );
486                         deleteAction.setEnabled( false );
487                     }
488                 }
489                 else if ( selectedObject instanceof AttributeTypeWrapper )
490                 {
491                     AttributeTypeWrapper attributeTypeWrapper = ( AttributeTypeWrapper ) selectedObject;
492                     deleteAction.setText( Messages.getString( "SchemasViewController.Delete_Attribute_Type" ) + " '" //$NON-NLS-1$ //$NON-NLS-2$
493
+ attributeTypeWrapper.getMyAttributeType().getNames()[0] + "'" ); //$NON-NLS-1$
494

495                     if ( attributeTypeWrapper.getMyAttributeType().getOriginatingSchema().type == SchemaType.coreSchema )
496                     {
497                         createANewObjectClass.setEnabled( false );
498                         createANewAttributeType.setEnabled( false );
499                         deleteAction.setEnabled( false );
500                     }
501                     else
502                     {
503                         createANewObjectClass.setEnabled( false );
504                         createANewAttributeType.setEnabled( true );
505                         deleteAction.setEnabled( true );
506                     }
507                 }
508                 else if ( selectedObject instanceof ObjectClassWrapper )
509                 {
510                     ObjectClassWrapper objectClassWrapper = ( ObjectClassWrapper ) selectedObject;
511                     deleteAction.setText( Messages.getString( "SchemasViewController.Delete_Object_Class" ) + " '" //$NON-NLS-1$ //$NON-NLS-2$
512
+ objectClassWrapper.getMyObjectClass().getNames()[0] + "'" ); //$NON-NLS-1$
513

514                     if ( objectClassWrapper.getMyObjectClass().getOriginatingSchema().type == SchemaType.coreSchema )
515                     {
516                         createANewObjectClass.setEnabled( false );
517                         createANewAttributeType.setEnabled( false );
518                         deleteAction.setEnabled( false );
519                     }
520                     else
521                     {
522                         createANewObjectClass.setEnabled( true );
523                         createANewAttributeType.setEnabled( false );
524                         deleteAction.setEnabled( true );
525                     }
526                 }
527                 else if ( selectedObject instanceof IntermediateNode )
528                 {
529                     IntermediateNode intermediateNode = ( IntermediateNode ) selectedObject;
530                     SchemaWrapper schemaWrapper = ( SchemaWrapper ) intermediateNode.getParent();
531
532                     if ( schemaWrapper.getMySchema().type == SchemaType.coreSchema )
533                     {
534                         removeSchema.setEnabled( false );
535                         createANewObjectClass.setEnabled( false );
536                         createANewAttributeType.setEnabled( false );
537                         deleteAction.setEnabled( false );
538                     }
539                     else
540                     {
541                         if ( intermediateNode.getType() == IntermediateNodeType.OBJECT_CLASS_FOLDER )
542                         {
543                             removeSchema.setEnabled( true );
544                             createANewObjectClass.setEnabled( true );
545                             createANewAttributeType.setEnabled( false );
546                             deleteAction.setEnabled( false );
547                         }
548                         else if ( intermediateNode.getType() == IntermediateNodeType.ATTRIBUTE_TYPE_FOLDER )
549                         {
550                             removeSchema.setEnabled( true );
551                             createANewObjectClass.setEnabled( false );
552                             createANewAttributeType.setEnabled( true );
553                             deleteAction.setEnabled( false );
554                         }
555                         else
556                         {
557                             removeSchema.setEnabled( true );
558                             createANewObjectClass.setEnabled( false );
559                             createANewAttributeType.setEnabled( false );
560                             deleteAction.setEnabled( false );
561                         }
562                     }
563                 }
564             }
565         } );
566     }
567
568
569     /**
570      * Initializes the listener on the preferences store.
571      */

572     private void initPreferencesListener()
573     {
574         Activator.getDefault().getPreferenceStore().addPropertyChangeListener( new IPropertyChangeListener()
575         {
576             /* (non-Javadoc)
577              * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
578              */

579             public void propertyChange( PropertyChangeEvent event )
580             {
581                 if ( authorizedPrefs.contains( event.getProperty() ) )
582                 {
583                     if ( PluginConstants.PREFS_SCHEMAS_VIEW_GROUPING == event.getProperty() )
584                     {
585                         view.completeRefresh();
586                     }
587                     else
588                     {
589                         view.refresh();
590                     }
591                 }
592             }
593         } );
594     }
595 }
596
Popular Tags