001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.studio.apacheds.actions;
021
022
023 import org.apache.directory.studio.apacheds.ApacheDsPluginConstants;
024 import org.apache.directory.studio.apacheds.ApacheDsPluginUtils;
025 import org.apache.directory.studio.apacheds.configuration.editor.ServerConfigurationEditor;
026 import org.apache.directory.studio.apacheds.model.Server;
027 import org.apache.directory.studio.apacheds.views.ServersView;
028 import org.eclipse.core.runtime.IPath;
029 import org.eclipse.core.runtime.Platform;
030 import org.eclipse.jface.action.Action;
031 import org.eclipse.jface.action.IAction;
032 import org.eclipse.jface.resource.ImageDescriptor;
033 import org.eclipse.jface.viewers.ISelection;
034 import org.eclipse.jface.viewers.StructuredSelection;
035 import org.eclipse.ui.IPathEditorInput;
036 import org.eclipse.ui.IPersistableElement;
037 import org.eclipse.ui.IWorkbenchPage;
038 import org.eclipse.ui.IWorkbenchWindow;
039 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
040 import org.eclipse.ui.PartInitException;
041 import org.eclipse.ui.PlatformUI;
042
043
044 /**
045 * This class implements the open action for a server.
046 *
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 * @version $Rev$, $Date$
049 */
050 public class OpenConfigurationAction extends Action implements IWorkbenchWindowActionDelegate
051 {
052 /** The associated view */
053 private ServersView view;
054
055
056 /**
057 * Creates a new instance of OpenConfigurationAction.
058 */
059 public OpenConfigurationAction()
060 {
061 super( Messages.getString( "OpenConfigurationAction.OpenConfiguration" ) ); //$NON-NLS-1$
062 init();
063 }
064
065
066 /**
067 * Creates a new instance of OpenConfigurationAction.
068 *
069 * @param view
070 * the associated view
071 */
072 public OpenConfigurationAction( ServersView view )
073 {
074 super( Messages.getString( "OpenConfigurationAction.OpenConfiguration" ) ); //$NON-NLS-1$
075 this.view = view;
076 init();
077 }
078
079
080 /**
081 * Initializes the action.
082 */
083 private void init()
084 {
085 setId( ApacheDsPluginConstants.CMD_OPEN_CONFIGURATION );
086 setActionDefinitionId( ApacheDsPluginConstants.CMD_OPEN_CONFIGURATION );
087 setToolTipText( Messages.getString( "OpenConfigurationAction.OpenConfigurationToolTip" ) ); //$NON-NLS-1$
088 }
089
090
091 /* (non-Javadoc)
092 * @see org.eclipse.jface.action.Action#run()
093 */
094 public void run()
095 {
096 if ( view != null )
097 {
098 // What we get from the TableViewer is a StructuredSelection
099 StructuredSelection selection = ( StructuredSelection ) view.getViewer().getSelection();
100
101 // Here's the real object
102 Server server = ( Server ) selection.getFirstElement();
103 if ( server != null )
104 {
105 // Opening the editor
106 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
107 PathEditorInput input = new PathEditorInput( ApacheDsPluginUtils.getApacheDsServersFolder().append(
108 server.getId() ).append( "conf" ).append( "server.xml" ) ); //$NON-NLS-1$ //$NON-NLS-2$
109 try
110 {
111 page.openEditor( input, ServerConfigurationEditor.ID );
112 }
113 catch ( PartInitException e )
114 {
115 ApacheDsPluginUtils.reportError( Messages.getString( "OpenConfigurationAction.ErrorWhenOpening" ) //$NON-NLS-1$
116 + e.getMessage() );
117 }
118 }
119 }
120 }
121
122
123 /* (non-Javadoc)
124 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
125 */
126 public void run( IAction action )
127 {
128 run();
129 }
130
131
132 /* (non-Javadoc)
133 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
134 */
135 public void dispose()
136 {
137 // Nothing to do
138 }
139
140
141 /* (non-Javadoc)
142 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
143 */
144 public void init( IWorkbenchWindow window )
145 {
146 // Nothing to do
147 }
148
149
150 /* (non-Javadoc)
151 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
152 */
153 public void selectionChanged( IAction action, ISelection selection )
154 {
155 // Nothing to do
156 }
157
158 /**
159 * This IEditorInput is used to open files that are located in the local file system.
160 *
161 * Inspired from org.eclipse.ui.internal.editors.text.NonExistingFileEditorInput.java
162 *
163 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
164 * @version $Rev$, $Date$
165 */
166 class PathEditorInput implements IPathEditorInput
167 {
168 /** The absolute path in local file system */
169 private IPath path;
170
171
172 /**
173 *
174 * Creates a new instance of PathEditorInput.
175 *
176 * @param path the absolute path
177 */
178 public PathEditorInput( IPath path )
179 {
180 if ( path == null )
181 {
182 throw new IllegalArgumentException();
183 }
184
185 this.path = path;
186 }
187
188
189 /**
190 * Returns hash code of the path.
191 */
192 public int hashCode()
193 {
194 return path.hashCode();
195 }
196
197
198 /**
199 * This implemention just compares the paths
200 */
201 public boolean equals( Object o )
202 {
203 if ( this == o )
204 {
205 return true;
206 }
207
208 if ( o instanceof PathEditorInput )
209 {
210 PathEditorInput input = ( PathEditorInput ) o;
211 return path.equals( input.path );
212 }
213
214 return false;
215 }
216
217
218 /**
219 * {@inheritDoc}
220 */
221 public boolean exists()
222 {
223 return path.toFile().exists();
224 }
225
226
227 /**
228 * {@inheritDoc}
229 */
230 public ImageDescriptor getImageDescriptor()
231 {
232 return PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor( path.toString() );
233 }
234
235
236 /**
237 * Returns the file name only.
238 */
239 public String getName()
240 {
241 return path.toFile().getName();
242 //return path.toString();
243 }
244
245
246 /**
247 * Returns the complete path.
248 */
249 public String getToolTipText()
250 {
251 return path.makeRelative().toOSString();
252 }
253
254
255 /**
256 * {@inheritDoc}
257 */
258 public IPath getPath()
259 {
260 return path;
261 }
262
263
264 /**
265 * {@inheritDoc}
266 */
267 @SuppressWarnings("unchecked")
268 public Object getAdapter( Class adapter )
269 {
270 return Platform.getAdapterManager().getAdapter( this, adapter );
271 }
272
273
274 /**
275 * {@inheritDoc}
276 */
277 public IPersistableElement getPersistable()
278 {
279 return null;
280 }
281
282
283 /**
284 * Returns the path.
285 */
286 public IPath getErrorMessage( Object element )
287 {
288 if ( element instanceof PathEditorInput )
289 {
290 PathEditorInput input = ( PathEditorInput ) element;
291 return input.getPath();
292 }
293
294 return null;
295 }
296 }
297 }