1 11 12 package org.eclipse.ui.internal.ide.handlers; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.commands.ExecutionEvent; 19 import org.eclipse.core.commands.ExecutionException; 20 import org.eclipse.core.commands.IHandler; 21 import org.eclipse.core.commands.IHandlerListener; 22 import org.eclipse.core.resources.IContainer; 23 import org.eclipse.core.resources.IFile; 24 import org.eclipse.core.resources.IResource; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.runtime.ListenerList; 27 import org.eclipse.jface.action.Action; 28 import org.eclipse.jface.action.IAction; 29 import org.eclipse.jface.dialogs.IDialogConstants; 30 import org.eclipse.jface.viewers.ISelection; 31 import org.eclipse.swt.widgets.Shell; 32 import org.eclipse.ui.IWorkbenchPage; 33 import org.eclipse.ui.IWorkbenchWindow; 34 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 35 import org.eclipse.ui.PartInitException; 36 import org.eclipse.ui.PlatformUI; 37 import org.eclipse.ui.ide.IDE; 38 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 39 import org.eclipse.ui.internal.ide.dialogs.OpenResourceDialog; 40 41 47 public final class OpenResourceHandler extends Action implements IHandler, 48 IWorkbenchWindowActionDelegate { 49 50 53 private static final String PARAM_ID_FILE_PATH = "filePath"; 55 59 private transient ListenerList listenerList = null; 60 61 64 public OpenResourceHandler() { 65 super(); 66 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 67 IIDEHelpContextIds.OPEN_WORKSPACE_FILE_ACTION); 68 } 69 70 public final void addHandlerListener(final IHandlerListener listener) { 71 if (listenerList == null) { 72 listenerList = new ListenerList(ListenerList.IDENTITY); 73 } 74 75 listenerList.add(listener); 76 } 77 78 public final void dispose() { 79 listenerList = null; 80 } 81 82 public final Object execute(final ExecutionEvent event) 83 throws ExecutionException { 84 final List files = new ArrayList (); 85 86 if (event.getParameter(PARAM_ID_FILE_PATH) == null) { 87 Object [] result = queryFileResource(); 89 90 if (result != null) { 91 for (int i = 0; i < result.length; i++) { 92 if (result[i] instanceof IFile) { 93 files.add(result[i]); 94 } 95 } 96 } 97 98 } else { 99 final IResource resource = (IResource) event 101 .getObjectParameterForExecution(PARAM_ID_FILE_PATH); 102 if (!(resource instanceof IFile)) { 103 throw new ExecutionException( 104 "filePath parameter must identify a file"); } 106 files.add(resource); 107 } 108 109 if (files.size() > 0) { 110 111 final IWorkbenchWindow window = PlatformUI.getWorkbench() 112 .getActiveWorkbenchWindow(); 113 if (window == null) { 114 throw new ExecutionException("no active workbench window"); } 116 117 final IWorkbenchPage page = window.getActivePage(); 118 if (page == null) { 119 throw new ExecutionException("no active workbench page"); } 121 122 try { 123 for (Iterator it = files.iterator(); it.hasNext();) { 124 IDE.openEditor(page, (IFile) it.next(), true); 125 } 126 } catch (final PartInitException e) { 127 throw new ExecutionException("error opening file in editor", e); } 129 } 130 131 return null; 132 } 133 134 public final void init(final IWorkbenchWindow window) { 135 } 137 138 143 private final Object [] queryFileResource() { 144 final IWorkbenchWindow window = PlatformUI.getWorkbench() 145 .getActiveWorkbenchWindow(); 146 if (window == null) { 147 return null; 148 } 149 final Shell parent = window.getShell(); 150 final IContainer input = ResourcesPlugin.getWorkspace().getRoot(); 151 152 final OpenResourceDialog dialog = new OpenResourceDialog(parent, input, 153 IResource.FILE); 154 final int resultCode = dialog.open(); 155 if (resultCode != IDialogConstants.OK_ID) { 156 return null; 157 } 158 159 final Object [] result = dialog.getResult(); 160 161 return result; 162 } 163 164 public final void removeHandlerListener(final IHandlerListener listener) { 165 if (listenerList != null) { 166 listenerList.remove(listener); 167 168 if (listenerList.isEmpty()) { 169 listenerList = null; 170 } 171 } 172 } 173 174 public final void run(final IAction action) { 175 try { 176 execute(new ExecutionEvent()); 177 } catch (final ExecutionException e) { 178 } 180 } 181 182 public final void selectionChanged(final IAction action, 183 final ISelection selection) { 184 } 186 } 187 | Popular Tags |