KickJava   Java API By Example, From Geeks To Geeks.

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


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.actions;
22
23
24 import org.apache.directory.ldapstudio.schemas.Activator;
25 import org.apache.directory.ldapstudio.schemas.Messages;
26 import org.apache.directory.ldapstudio.schemas.PluginConstants;
27 import org.apache.directory.ldapstudio.schemas.model.Schema;
28 import org.apache.directory.ldapstudio.schemas.model.SchemaPool;
29 import org.apache.directory.ldapstudio.schemas.view.views.SchemasView;
30 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper;
31 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper;
32 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.SchemaWrapper;
33 import org.eclipse.jface.action.Action;
34 import org.eclipse.jface.viewers.TreeSelection;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.widgets.MessageBox;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.plugin.AbstractUIPlugin;
39
40
41 /**
42  * This class implements the Action for removing a schema from the pool
43  */

44 public class RemoveSchemaAction extends Action
45 {
46
47     /**
48      * Default constructor
49      * @param window
50      * @param label
51      */

52     public RemoveSchemaAction()
53     {
54         super( Messages.getString( "RemoveSchemaAction.Remove_the_selected_schema" ) ); //$NON-NLS-1$
55
setToolTipText( getText() );
56         setId( PluginConstants.CMD_REMOVE_SCHEMA );
57         setImageDescriptor( AbstractUIPlugin.imageDescriptorFromPlugin( Activator.PLUGIN_ID,
58             PluginConstants.IMG_REMOVE_SCHEMA ) );
59         setEnabled( true );
60     }
61
62
63     /**
64      * {@inheritDoc}
65      */

66     public void run()
67     {
68         SchemasView view = ( SchemasView ) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
69             .findView( SchemasView.ID ); //$NON-NLS-1$
70
Object JavaDoc selection = ( ( TreeSelection ) view.getViewer().getSelection() ).getFirstElement();
71
72         String JavaDoc schemaName = null;
73
74         // We have to check on which node we are to get the schema name
75
if ( selection instanceof SchemaWrapper )
76         {
77             schemaName = ( ( SchemaWrapper ) selection ).getMySchema().getName();
78         }
79         else if ( selection instanceof AttributeTypeWrapper )
80         {
81             // We have to get the parent of the parent ( AttributeTypeWrapper => IntermediateNode => SchemaWrapper )
82
schemaName = ( ( SchemaWrapper ) ( ( AttributeTypeWrapper ) selection ).getParent().getParent() )
83                 .getMySchema().getName();
84         }
85         else if ( selection instanceof ObjectClassWrapper )
86         {
87             // We have to get the parent of the parent ( ObjectClassWrapper => IntermediateNode => SchemaWrapper )
88
schemaName = ( ( SchemaWrapper ) ( ( ObjectClassWrapper ) selection ).getParent().getParent() )
89                 .getMySchema().getName();
90         }
91
92         if ( schemaName != null )
93         {
94             // Getting the SchemaPool
95
SchemaPool pool = SchemaPool.getInstance();
96             // Getting the right schema
97
Schema schema = pool.getSchema( schemaName );
98
99             if ( schema.type == Schema.SchemaType.coreSchema )
100             {
101                 MessageBox messageBox = new MessageBox(
102                     PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OK | SWT.ICON_ERROR );
103                 messageBox
104                     .setMessage( Messages.getString( "RemoveSchemaAction.The_schema" ) + schemaName + Messages.getString( "RemoveSchemaAction.Is_a_core_schema_It_cant_be_removed_from_the_pool" ) ); //$NON-NLS-1$ //$NON-NLS-2$
105
messageBox.open();
106                 return;
107             }
108             if ( schema.hasBeenModified() || schema.hasPendingModification() )
109             {
110                 MessageBox messageBox = new MessageBox(
111                     PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OK | SWT.CANCEL
112                         | SWT.ICON_QUESTION );
113                 messageBox.setMessage( Messages
114                     .getString( "RemoveSchemaAction.This_schema_has_been_modified_or_has_pending_modifications" ) ); //$NON-NLS-1$
115
if ( messageBox.open() == SWT.OK )
116                 {
117                     pool.removeSchema( schema );
118                 }
119             }
120             else
121             {
122                 pool.removeSchema( schema );
123             }
124         }
125     }
126 }
127
Popular Tags