KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > apacheds > configuration > actions > OpenServerConfigurationAction


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 package org.apache.directory.ldapstudio.apacheds.configuration.actions;
21
22
23 import org.apache.directory.ldapstudio.apacheds.configuration.Activator;
24 import org.apache.directory.ldapstudio.apacheds.configuration.editor.ServerConfigurationEditor;
25 import org.apache.directory.ldapstudio.apacheds.configuration.editor.ServerConfigurationEditorInput;
26 import org.apache.directory.ldapstudio.apacheds.configuration.model.ServerConfiguration;
27 import org.apache.directory.ldapstudio.apacheds.configuration.model.ServerConfigurationParser;
28 import org.apache.directory.ldapstudio.apacheds.configuration.model.ServerConfigurationParserException;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.jface.action.Action;
31 import org.eclipse.jface.action.IAction;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.widgets.FileDialog;
35 import org.eclipse.swt.widgets.MessageBox;
36 import org.eclipse.ui.IWorkbenchPage;
37 import org.eclipse.ui.IWorkbenchWindow;
38 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
39 import org.eclipse.ui.PartInitException;
40 import org.eclipse.ui.PlatformUI;
41
42
43 /**
44  * This class implements the Open Editor Action.
45  *
46  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
47  * @version $Rev$, $Date$
48  */

49 public class OpenServerConfigurationAction extends Action implements IWorkbenchWindowActionDelegate
50 {
51     /* (non-Javadoc)
52      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
53      */

54     public void run( IAction action )
55     {
56         FileDialog fd = new FileDialog( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN );
57         fd.setText( "Select a file" );
58         fd.setFilterExtensions( new String JavaDoc[]
59             { "*.xml", "*.*" } );
60         fd.setFilterNames( new String JavaDoc[]
61             { "XML files", "All files" } );
62         String JavaDoc selectedFile = fd.open();
63         // selected == null if 'cancel' has been pushed
64
if ( selectedFile == null || "".equals( selectedFile ) )
65         {
66             return;
67         }
68
69         ServerConfigurationParser parser = new ServerConfigurationParser();
70         ServerConfiguration serverConfiguration = null;
71
72         try
73         {
74             serverConfiguration = parser.parse( selectedFile );
75         }
76         catch ( ServerConfigurationParserException e )
77         {
78             MessageBox messageBox = new MessageBox( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
79                 SWT.OK | SWT.ICON_ERROR );
80             messageBox.setText( "Error!" );
81             messageBox.setMessage( "An error occurred when reading the file." + "\n" + e.getMessage() );
82             messageBox.open();
83             return;
84         }
85
86         IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
87         try
88         {
89             page.openEditor( new ServerConfigurationEditorInput( serverConfiguration ), ServerConfigurationEditor.ID );
90         }
91         catch ( PartInitException e )
92         {
93             Activator.getDefault().getLog().log(
94                 new Status( Status.ERROR, Activator.PLUGIN_ID, Status.OK, e.getMessage(), e.getCause() ) );
95             return;
96         }
97     }
98
99
100     /* (non-Javadoc)
101      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
102      */

103     public void selectionChanged( IAction action, ISelection selection )
104     {
105     }
106
107
108     /* (non-Javadoc)
109      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
110      */

111     public void dispose()
112     {
113     }
114
115
116     /* (non-Javadoc)
117      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
118      */

119     public void init( IWorkbenchWindow window )
120     {
121     }
122 }
123
Popular Tags