1 20 21 package org.apache.directory.ldapstudio.actions; 22 23 24 import java.io.File ; 25 import java.text.MessageFormat ; 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 54 public class OpenFileAction extends Action implements IWorkbenchWindowActionDelegate 55 { 56 57 58 private IWorkbenchWindow workbenchWindow; 59 60 61 64 public OpenFileAction() 65 { 66 setId( "org.apache.directory.ldapstudio.openFile" ); setText( Messages.getString( "OpenFileAction.Open_File" ) ); setToolTipText( Messages.getString( "OpenFileAction.Open_file_from_filesystem" ) ); setEnabled( true ); 70 } 71 72 73 78 public OpenFileAction( IWorkbenchWindow window ) 79 { 80 this(); 81 init( window ); 82 } 83 84 85 88 public void dispose() 89 { 90 workbenchWindow = null; 91 } 92 93 94 97 public void init( IWorkbenchWindow window ) 98 { 99 workbenchWindow = window; 100 } 101 102 103 106 public void run( IAction action ) 107 { 108 run(); 109 } 110 111 112 115 public void selectionChanged( IAction action, ISelection selection ) 116 { 117 } 118 119 120 123 public void run() 124 { 125 FileDialog dialog = new FileDialog( workbenchWindow.getShell(), SWT.OPEN ); 127 dialog.setText( Messages.getString( "OpenFileAction.Open_File" ) ); String path = dialog.open(); 129 if ( path == null || path.length() == 0 ) 130 { 131 return; 133 } 134 135 File file = new File ( path ); 137 if ( !file.exists() ) 138 { 139 String msg = MessageFormat.format( 140 Messages.getString( "OpenFileAction.File_x_does_not_exist" ), new Object [] { file.getName() } ); 142 MessageDialog.openWarning( workbenchWindow.getShell(), Messages 143 .getString( "OpenFileAction.Warning_message" ), msg ); return; 145 } 146 if ( !file.canRead() ) 147 { 148 String msg = MessageFormat.format( 149 Messages.getString( "OpenFileAction.File_x_is_not_readable" ), new Object [] { file.getName() } ); 151 MessageDialog.openWarning( workbenchWindow.getShell(), Messages 152 .getString( "OpenFileAction.Warning_message" ), msg ); return; 154 } 155 156 IWorkbench workbench = workbenchWindow.getWorkbench(); 158 IEditorRegistry editorRegistry = workbench.getEditorRegistry(); 159 IEditorDescriptor descriptor = editorRegistry.getDefaultEditor( file.getName() ); 160 161 if ( descriptor == null ) 162 { 163 String msg = MessageFormat.format( 164 Messages.getString( "OpenFileAction.No_appropriate_editor_found_for_x" ), new Object [] { file.getName() } ); 166 MessageDialog.openWarning( workbenchWindow.getShell(), Messages 167 .getString( "OpenFileAction.Warning_message" ), msg ); return; 169 } 170 171 IPath location = new Path( file.getAbsolutePath() ); 173 ImageDescriptor imageDescriptor = descriptor.getImageDescriptor(); 174 IPathEditorInput input = new PathEditorInput( location, imageDescriptor ); 175 176 IWorkbenchPage page = workbenchWindow.getActivePage(); 178 String 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 |