001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.ide.actions;
011:
012: import org.eclipse.osgi.util.NLS;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.widgets.FileDialog;
016:
017: import org.eclipse.core.filesystem.EFS;
018: import org.eclipse.core.filesystem.IFileStore;
019:
020: import org.eclipse.core.runtime.Path;
021:
022: import org.eclipse.jface.action.Action;
023: import org.eclipse.jface.action.IAction;
024: import org.eclipse.jface.dialogs.MessageDialog;
025: import org.eclipse.jface.viewers.ISelection;
026:
027: import org.eclipse.ui.IWorkbenchPage;
028: import org.eclipse.ui.IWorkbenchWindow;
029: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
030: import org.eclipse.ui.PartInitException;
031: import org.eclipse.ui.ide.IDE;
032: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
033: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
034:
035: /**
036: * Standard action for opening an editor on local file(s).
037: * <p>
038: * This class may be instantiated; it is not intended to be subclassed.
039: * </p>
040: */
041: public class OpenLocalFileAction extends Action implements
042: IWorkbenchWindowActionDelegate {
043:
044: private IWorkbenchWindow window;
045: private String filterPath;
046:
047: /**
048: * Creates a new action for opening a local file.
049: */
050: public OpenLocalFileAction() {
051: setEnabled(true);
052: }
053:
054: /* (non-Javadoc)
055: * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
056: */
057: public void dispose() {
058: window = null;
059: filterPath = null;
060: }
061:
062: /* (non-Javadoc)
063: * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
064: */
065: public void init(IWorkbenchWindow window) {
066: this .window = window;
067: filterPath = System.getProperty("user.home"); //$NON-NLS-1$
068: }
069:
070: /* (non-Javadoc)
071: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
072: */
073: public void run(IAction action) {
074: run();
075: }
076:
077: /* (non-Javadoc)
078: * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
079: */
080: public void selectionChanged(IAction action, ISelection selection) {
081: }
082:
083: /* (non-Javadoc)
084: * @see org.eclipse.jface.action.Action#run()
085: */
086: public void run() {
087: FileDialog dialog = new FileDialog(window.getShell(), SWT.OPEN
088: | SWT.MULTI);
089: dialog.setText(IDEWorkbenchMessages.OpenLocalFileAction_title);
090: dialog.setFilterPath(filterPath);
091: dialog.open();
092: String[] names = dialog.getFileNames();
093:
094: if (names != null) {
095: filterPath = dialog.getFilterPath();
096:
097: int numberOfFilesNotFound = 0;
098: StringBuffer notFound = new StringBuffer();
099: for (int i = 0; i < names.length; i++) {
100: IFileStore fileStore = EFS.getLocalFileSystem()
101: .getStore(new Path(filterPath));
102: fileStore = fileStore.getChild(names[i]);
103: if (!fileStore.fetchInfo().isDirectory()
104: && fileStore.fetchInfo().exists()) {
105: IWorkbenchPage page = window.getActivePage();
106: try {
107: IDE.openEditorOnFileStore(page, fileStore);
108: } catch (PartInitException e) {
109: String msg = NLS
110: .bind(
111: IDEWorkbenchMessages.OpenLocalFileAction_message_errorOnOpen,
112: fileStore.getName());
113: IDEWorkbenchPlugin.log(msg, e.getStatus());
114: MessageDialog
115: .openError(
116: window.getShell(),
117: IDEWorkbenchMessages.OpenLocalFileAction_title,
118: msg);
119: }
120: } else {
121: if (++numberOfFilesNotFound > 1)
122: notFound.append('\n');
123: notFound.append(fileStore.getName());
124: }
125: }
126:
127: if (numberOfFilesNotFound > 0) {
128: String msgFmt = numberOfFilesNotFound == 1 ? IDEWorkbenchMessages.OpenLocalFileAction_message_fileNotFound
129: : IDEWorkbenchMessages.OpenLocalFileAction_message_filesNotFound;
130: String msg = NLS.bind(msgFmt, notFound.toString());
131: MessageDialog.openError(window.getShell(),
132: IDEWorkbenchMessages.OpenLocalFileAction_title,
133: msg);
134: }
135: }
136: }
137: }
|