KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > actions > OpenFileAction


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.actions;
22
23
24 import java.io.File JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26
27 import org.apache.directory.ldapstudio.Messages;
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.Path;
30 import org.eclipse.jface.action.Action;
31 import org.eclipse.jface.action.IAction;
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.resource.ImageDescriptor;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.widgets.FileDialog;
37 import org.eclipse.ui.IEditorDescriptor;
38 import org.eclipse.ui.IEditorRegistry;
39 import org.eclipse.ui.IPathEditorInput;
40 import org.eclipse.ui.IWorkbench;
41 import org.eclipse.ui.IWorkbenchPage;
42 import org.eclipse.ui.IWorkbenchWindow;
43 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
44 import org.eclipse.ui.PartInitException;
45
46
47 /**
48  * The Action is used to open files from file system.
49  * It creates IPathEditorInput inputs.
50  *
51  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
52  * @version $Rev$, $Date$
53  */

54 public class OpenFileAction extends Action implements IWorkbenchWindowActionDelegate
55 {
56
57     /** The workbench window */
58     private IWorkbenchWindow workbenchWindow;
59
60
61     /**
62      * Creates a new instance of OpenFileAction.
63      */

64     public OpenFileAction()
65     {
66         setId( "org.apache.directory.ldapstudio.openFile" ); //$NON-NLS-1$
67
setText( Messages.getString( "OpenFileAction.Open_File" ) ); //$NON-NLS-1$
68
setToolTipText( Messages.getString( "OpenFileAction.Open_file_from_filesystem" ) ); //$NON-NLS-1$
69
setEnabled( true );
70     }
71
72
73     /**
74      * Creates a new instance of OpenFileAction.
75      *
76      * @param window the workbench window
77      */

78     public OpenFileAction( IWorkbenchWindow window )
79     {
80         this();
81         init( window );
82     }
83
84
85     /**
86      * {@inheritDoc}
87      */

88     public void dispose()
89     {
90         workbenchWindow = null;
91     }
92
93
94     /**
95      * {@inheritDoc}
96      */

97     public void init( IWorkbenchWindow window )
98     {
99         workbenchWindow = window;
100     }
101
102
103     /**
104      * {@inheritDoc}
105      */

106     public void run( IAction action )
107     {
108         run();
109     }
110
111
112     /**
113      * {@inheritDoc}
114      */

115     public void selectionChanged( IAction action, ISelection selection )
116     {
117     }
118
119
120     /**
121      * {@inheritDoc}
122      */

123     public void run()
124     {
125         // get path
126
FileDialog dialog = new FileDialog( workbenchWindow.getShell(), SWT.OPEN );
127         dialog.setText( Messages.getString( "OpenFileAction.Open_File" ) ); //$NON-NLS-1$
128
String JavaDoc path = dialog.open();
129         if ( path == null || path.length() == 0 )
130         {
131             // canceled
132
return;
133         }
134
135         // check file
136
File JavaDoc file = new File JavaDoc( path );
137         if ( !file.exists() )
138         {
139             String JavaDoc msg = MessageFormat.format(
140                 Messages.getString( "OpenFileAction.File_x_does_not_exist" ), new Object JavaDoc[] //$NON-NLS-1$
141
{ file.getName() } );
142             MessageDialog.openWarning( workbenchWindow.getShell(), Messages
143                 .getString( "OpenFileAction.Warning_message" ), msg ); //$NON-NLS-1$
144
return;
145         }
146         if ( !file.canRead() )
147         {
148             String JavaDoc msg = MessageFormat.format(
149                 Messages.getString( "OpenFileAction.File_x_is_not_readable" ), new Object JavaDoc[] //$NON-NLS-1$
150
{ file.getName() } );
151             MessageDialog.openWarning( workbenchWindow.getShell(), Messages
152                 .getString( "OpenFileAction.Warning_message" ), msg ); //$NON-NLS-1$
153
return;
154         }
155
156         // get editor for this file
157
IWorkbench workbench = workbenchWindow.getWorkbench();
158         IEditorRegistry editorRegistry = workbench.getEditorRegistry();
159         IEditorDescriptor descriptor = editorRegistry.getDefaultEditor( file.getName() );
160
161         if ( descriptor == null )
162         {
163             String JavaDoc msg = MessageFormat.format(
164                 Messages.getString( "OpenFileAction.No_appropriate_editor_found_for_x" ), new Object JavaDoc[] //$NON-NLS-1$
165
{ file.getName() } );
166             MessageDialog.openWarning( workbenchWindow.getShell(), Messages
167                 .getString( "OpenFileAction.Warning_message" ), msg ); //$NON-NLS-1$
168
return;
169         }
170
171         // create IEdiorInput
172
IPath location = new Path( file.getAbsolutePath() );
173         ImageDescriptor imageDescriptor = descriptor.getImageDescriptor();
174         IPathEditorInput input = new PathEditorInput( location, imageDescriptor );
175
176         // activate editor
177
IWorkbenchPage page = workbenchWindow.getActivePage();
178         String JavaDoc editorId = descriptor.getId();
179         try
180         {
181             page.openEditor( input, editorId );
182         }
183         catch ( PartInitException e )
184         {
185             e.printStackTrace();
186         }
187     }
188
189 }
Popular Tags