KickJava   Java API By Example, From Geeks To Geeks.

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


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.view.views.SchemasView;
29 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper;
30 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.IntermediateNode;
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.apache.log4j.Logger;
34 import org.eclipse.core.runtime.IStatus;
35 import org.eclipse.core.runtime.Status;
36 import org.eclipse.jface.action.Action;
37 import org.eclipse.jface.action.IAction;
38 import org.eclipse.jface.dialogs.ErrorDialog;
39 import org.eclipse.jface.viewers.ISelection;
40 import org.eclipse.jface.viewers.TreeSelection;
41 import org.eclipse.ui.IViewActionDelegate;
42 import org.eclipse.ui.IViewPart;
43 import org.eclipse.ui.IWorkbenchWindow;
44 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.plugin.AbstractUIPlugin;
47
48
49 /**
50  * This class implements the Action for saving a schema
51  */

52 public class SaveAction extends Action implements IWorkbenchWindowActionDelegate, IViewActionDelegate
53 {
54     private static Logger logger = Logger.getLogger( SaveAction.class );
55
56
57     /**
58      * Default constructor
59      * @param window
60      * @param label
61      */

62     public SaveAction()
63     {
64         super( Messages.getString( "SaveAction.Save_schema" ) ); //$NON-NLS-1$
65
setToolTipText( getText() );
66         setId( PluginConstants.CMD_SAVE );
67         setImageDescriptor( AbstractUIPlugin.imageDescriptorFromPlugin( Activator.PLUGIN_ID, PluginConstants.IMG_SAVE ) );
68         setEnabled( true );
69     }
70
71
72     /* (non-Javadoc)
73      * @see org.eclipse.jface.action.Action#run()
74      */

75     public void run()
76     {
77         SchemasView view = ( SchemasView ) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
78             .findView( SchemasView.ID ); //$NON-NLS-1$
79
Object JavaDoc selection = ( ( TreeSelection ) view.getViewer().getSelection() ).getFirstElement();
80
81         // We have to check on which node we are to get the schema name
82
if ( selection != null )
83         {
84
85             Schema schema = null;
86
87             if ( selection instanceof SchemaWrapper )
88             {
89                 schema = ( ( SchemaWrapper ) selection ).getMySchema();
90             }
91             else if ( selection instanceof AttributeTypeWrapper )
92             {
93                 schema = ( ( AttributeTypeWrapper ) selection ).getMyAttributeType().getOriginatingSchema();
94             }
95             else if ( selection instanceof ObjectClassWrapper )
96             {
97                 schema = ( ( ObjectClassWrapper ) selection ).getMyObjectClass().getOriginatingSchema();
98             }
99             else if ( selection instanceof IntermediateNode )
100             {
101                 schema = ( ( SchemaWrapper ) ( ( IntermediateNode ) selection ).getParent() ).getMySchema();
102             }
103
104             if ( schema != null )
105             {
106                 try
107                 {
108                     schema.save();
109                 }
110                 catch ( Exception JavaDoc e )
111                 {
112                     ErrorDialog
113                         .openError(
114                             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
115                             Messages.getString( "SaveAction.Error" ), Messages.getString( "SaveAction.An_error_occurred_when_saving_schema" ) + schema.getName(), new Status( IStatus.ERROR, Activator.PLUGIN_ID, 0, "Status Error Message", null ) ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
116
logger.debug( "An error occured when saving schema " + schema.getName() ); //$NON-NLS-1$
117
}
118             }
119         }
120     }
121
122
123     public void dispose()
124     {
125     }
126
127
128     public void init( IWorkbenchWindow window )
129     {
130     }
131
132
133     public void run( IAction action )
134     {
135         this.run();
136     }
137
138
139     public void selectionChanged( IAction action, ISelection selection )
140     {
141     }
142
143
144     public void init( IViewPart view )
145     {
146     }
147 }
148
Popular Tags