KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > apacheds > configuration > editor > ServerConfigurationEditor


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.editor;
21
22
23 import org.apache.directory.ldapstudio.apacheds.configuration.Activator;
24 import org.apache.directory.ldapstudio.apacheds.configuration.model.ServerConfiguration;
25 import org.apache.directory.ldapstudio.apacheds.configuration.model.ServerConfigurationWriter;
26 import org.apache.directory.ldapstudio.apacheds.configuration.model.ServerConfigurationWriterException;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.widgets.FileDialog;
31 import org.eclipse.swt.widgets.MessageBox;
32 import org.eclipse.ui.IEditorInput;
33 import org.eclipse.ui.IEditorSite;
34 import org.eclipse.ui.PartInitException;
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.forms.editor.FormEditor;
37
38
39 /**
40  * This class implements the Server Configuration Editor.
41  *
42  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
43  * @version $Rev$, $Date$
44  */

45 public class ServerConfigurationEditor extends FormEditor
46 {
47     /** The Editor ID */
48     public static final String JavaDoc ID = "org.apache.directory.ldapstudio.apacheds.configuration.editor";
49
50     /** The editor input */
51     private IEditorInput input;
52
53     /** The Server Configuration */
54     private ServerConfiguration serverConfiguration;
55
56     /** The dirty flag */
57     private boolean dirty = false;
58
59     // The Pages
60
private GeneralPage generalPage;
61     private PartitionsPage partitionsPage;
62     private InterceptorsPage interceptorsPage;
63     private ExtendedOperationsPage extendedOperationsPage;
64
65
66     /* (non-Javadoc)
67      * @see org.eclipse.ui.forms.editor.FormEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
68      */

69     public void init( IEditorSite site, IEditorInput input ) throws PartInitException
70     {
71         super.init( site, input );
72         this.input = input;
73         setPartName( input.getName() );
74         serverConfiguration = ( ( ServerConfigurationEditorInput ) input ).getServerConfiguration();
75         dirty = serverConfiguration.getPath() == null;
76     }
77
78
79     /* (non-Javadoc)
80      * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
81      */

82     protected void addPages()
83     {
84         try
85         {
86             generalPage = new GeneralPage( this );
87             addPage( generalPage );
88
89             partitionsPage = new PartitionsPage( this );
90             addPage( partitionsPage );
91
92             interceptorsPage = new InterceptorsPage( this );
93             addPage( interceptorsPage );
94
95             extendedOperationsPage = new ExtendedOperationsPage( this );
96             addPage( extendedOperationsPage );
97         }
98         catch ( PartInitException e )
99         {
100             Activator.getDefault().getLog().log(
101                 new Status( Status.ERROR, Activator.PLUGIN_ID, Status.OK, e.getMessage(), e.getCause() ) );
102         }
103     }
104
105
106     /* (non-Javadoc)
107      * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
108      */

109     public void doSave( IProgressMonitor monitor )
110     {
111         monitor.beginTask( "Saving the Server Configuration", 5 );
112         generalPage.save();
113         monitor.worked( 1 );
114         partitionsPage.save();
115         monitor.worked( 1 );
116         interceptorsPage.save();
117         monitor.worked( 1 );
118         extendedOperationsPage.save();
119         monitor.worked( 1 );
120
121         // Checking if the ServerConfiguration is already existing or if it's a new file.
122
if ( serverConfiguration.getPath() == null )
123         {
124             FileDialog fd = new FileDialog( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.SAVE );
125             fd.setText( "Select a file" );
126             fd.setFilterExtensions( new String JavaDoc[]
127                 { "*.xml", "*.*" } );
128             fd.setFilterNames( new String JavaDoc[]
129                 { "XML files", "All files" } );
130             String JavaDoc selectedFile = fd.open();
131             // selected == null if 'cancel' has been pushed
132
if ( selectedFile == null || "".equals( selectedFile ) )
133             {
134                 monitor.setCanceled( true );
135                 return;
136             }
137
138             // TODO Add the overwrite code...
139

140             serverConfiguration.setPath( selectedFile );
141             setTitleToolTip( input.getToolTipText() );
142         }
143
144         // Saving the ServerConfiguration to disk
145
try
146         {
147             ServerConfigurationWriter writer = new ServerConfigurationWriter();
148             writer.write( serverConfiguration );
149         }
150         catch ( ServerConfigurationWriterException e )
151         {
152             MessageBox messageBox = new MessageBox( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
153                 SWT.OK | SWT.ICON_ERROR );
154             messageBox.setText( "Error!" );
155             messageBox.setMessage( "An error occurred when writing the file to disk." + "\n" + e.getMessage() );
156             messageBox.open();
157             setDirty( true );
158             monitor.done();
159             return;
160         }
161
162         monitor.worked( 1 );
163         setDirty( false );
164         monitor.done();
165     }
166
167
168     /* (non-Javadoc)
169      * @see org.eclipse.ui.part.EditorPart#doSaveAs()
170      */

171     public void doSaveAs()
172     {
173     }
174
175
176     /* (non-Javadoc)
177      * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
178      */

179     public boolean isSaveAsAllowed()
180     {
181         return false;
182     }
183
184
185     /* (non-Javadoc)
186      * @see org.eclipse.ui.forms.editor.FormEditor#isDirty()
187      */

188     public boolean isDirty()
189     {
190         return dirty;
191     }
192
193
194     /**
195      * Sets the dirty state of the editor.
196      *
197      * @param dirty
198      * the new dirty
199      */

200     public void setDirty( boolean dirty )
201     {
202         this.dirty = dirty;
203         editorDirtyStateChanged();
204     }
205 }
206
Popular Tags