KickJava   Java API By Example, From Geeks To Geeks.

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


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.AttributeType;
28 import org.apache.directory.ldapstudio.schemas.model.ObjectClass;
29 import org.apache.directory.ldapstudio.schemas.model.Schema;
30 import org.apache.directory.ldapstudio.schemas.view.views.SchemasView;
31 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper;
32 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper;
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 deleting an element (object class or attribute type)
43  */

44 public class DeleteAction extends Action
45 {
46     private enum ItemType
47     {
48         attributeType, objectClass
49     }
50
51
52     /**
53      * Default constructor
54      * @param window
55      * @param label
56      */

57     public DeleteAction()
58     {
59         super( Messages.getString( "DeleteAction.Delete_the_selected_item" ) ); //$NON-NLS-1$
60
setToolTipText( getText() );
61         setId( PluginConstants.CMD_DELETE );
62         setImageDescriptor( AbstractUIPlugin
63             .imageDescriptorFromPlugin( Activator.PLUGIN_ID, PluginConstants.IMG_DELETE ) );
64         setEnabled( true );
65     }
66
67
68     /**
69      * {@inheritDoc}
70      */

71     public void run()
72     {
73         SchemasView view = ( SchemasView ) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
74             .findView( SchemasView.ID ); //$NON-NLS-1$
75
Object JavaDoc selection = ( ( TreeSelection ) view.getViewer().getSelection() ).getFirstElement();
76
77         Schema schema = null;
78         ItemType item = null;
79
80         if ( selection instanceof AttributeTypeWrapper )
81         {
82             // We have to get the parent of the parent ( AttributeTypeWrapper => IntermediateNode => SchemaWrapper )
83
schema = ( ( AttributeTypeWrapper ) selection ).getMyAttributeType().getOriginatingSchema();
84             item = DeleteAction.ItemType.attributeType;
85         }
86         else if ( selection instanceof ObjectClassWrapper )
87         {
88             // We have to get the parent of the parent ( ObjectClassWrapper => IntermediateNode => SchemaWrapper )
89
schema = ( ( ObjectClassWrapper ) selection ).getMyObjectClass().getOriginatingSchema();
90             item = DeleteAction.ItemType.objectClass;
91         }
92         else
93         {
94             MessageBox messageBox = new MessageBox( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
95                 SWT.OK | SWT.ICON_ERROR );
96             messageBox.setMessage( Messages.getString( "DeleteAction.This_item_cant_be_deleted" ) ); //$NON-NLS-1$
97
messageBox.open();
98             return;
99         }
100
101         // Check if the schema isn't a core schema (core schema can't be modified
102
if ( schema.type == Schema.SchemaType.coreSchema )
103         {
104             MessageBox messageBox = new MessageBox( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
105                 SWT.OK | SWT.ICON_ERROR );
106             messageBox
107                 .setMessage( Messages.getString( "DeleteAction.The_schema" ) + schema.getName() + Messages.getString( "DeleteAction.Is_a_core_schema_It_cant_be_modified" ) ); //$NON-NLS-1$ //$NON-NLS-2$
108
messageBox.open();
109         }
110         else
111         {
112             if ( item == DeleteAction.ItemType.attributeType )
113             {
114                 AttributeType attributeType = ( ( AttributeTypeWrapper ) selection ).getMyAttributeType();
115                 MessageBox messageBox = new MessageBox(
116                     PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OK | SWT.CANCEL
117                         | SWT.ICON_QUESTION );
118                 messageBox
119                     .setMessage( Messages.getString( "DeleteAction.Are_you_sure_you_want_to_delete_the_attribute_type" ) + attributeType.getNames()[0] + Messages.getString( "DeleteAction.Interrogation" ) ); //$NON-NLS-1$ //$NON-NLS-2$
120
if ( messageBox.open() == SWT.OK )
121                 {
122                     schema.removeAttributeType( attributeType );
123                 }
124             }
125             else if ( item == DeleteAction.ItemType.objectClass )
126             {
127                 ObjectClass objectClass = ( ( ObjectClassWrapper ) selection ).getMyObjectClass();
128                 MessageBox messageBox = new MessageBox(
129                     PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OK | SWT.CANCEL
130                         | SWT.ICON_QUESTION );
131                 messageBox
132                     .setMessage( Messages.getString( "DeleteAction.Are_you_sure_you_want_to_delete_the_object_class" ) + objectClass.getNames()[0] + Messages.getString( "DeleteAction.Interrogation" ) ); //$NON-NLS-1$ //$NON-NLS-2$
133
if ( messageBox.open() == SWT.OK )
134                 {
135                     schema.removeObjectClass( objectClass );
136                 }
137             }
138         }
139     }
140 }
141
Popular Tags