Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1203a80fb1051fd2a3beac2c84d38fa9203ba533 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
 * Copyright (C) 2011, Greg Amerson <gregory.amerson@liferay.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.egit.ui.internal.synchronize.action;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.egit.ui.UIText;
import org.eclipse.egit.ui.internal.synchronize.model.GitModelObject;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.util.OpenStrategy;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.SelectionListenerAction;
import org.eclipse.ui.ide.IDE;

/**
 * Action for opening an editor on the currently selected git working file.
 *
 * <p>
 * This class may be instantiated; it is not intended to be subclassed.
 * </p>
 *
 * @noextend This class is not intended to be subclassed by clients.
 */
public class OpenWorkingFileAction extends SelectionListenerAction {

	/**
	 * The workbench page to open the editor in.
	 */
	private IWorkbenchPage workbenchPage;

	/**
	 * Creates a new action that will open editors on the selected working file
	 * resources.
	 *
	 * @param page
	 *            the workbench page in which to open the editor
	 */
	public OpenWorkingFileAction(IWorkbenchPage page) {
		super(null);
		setText(UIText.OpenWorkingFileAction_text);
		setToolTipText(UIText.OpenWorkingFileAction_tooltip);
		this.workbenchPage = page;
	}

	/**
	 * Return the workbench page to open the editor in.
	 *
	 * @return the workbench page to open the editor in
	 */
	protected IWorkbenchPage getWorkbenchPage() {
		return workbenchPage;
	}

	/**
	 * Opens a editor on the given file resource.
	 *
	 * @param file
	 *            the workspace file
	 */
	protected void openWorkspaceFile(IFile file) {
		try {
			boolean activate = OpenStrategy.activateOnOpen();
			IDE.openEditor(getWorkbenchPage(), file, activate);
		} catch (PartInitException e) {
			ErrorDialog.openError(getWorkbenchPage().getWorkbenchWindow()
					.getShell(),
					UIText.OpenWorkingFileAction_openWorkingFileShellTitle, e
							.getMessage(), e.getStatus());
		}
	}

	/**
	 * Opens external file, the editor that is used is based on best guess from
	 * file name.
	 *
	 * @param file
	 *            the external file
	 */
	protected void openExternalFile(File file) {
		try {
			boolean activate = OpenStrategy.activateOnOpen();
			IEditorDescriptor desc = IDE.getEditorDescriptor(file.getName());
			IDE.openEditor(getWorkbenchPage(), file.toURI(), desc.getId(),
					activate);
		} catch (PartInitException e) {
			ErrorDialog.openError(getWorkbenchPage().getWorkbenchWindow()
					.getShell(),
					UIText.OpenWorkingFileAction_openWorkingFileShellTitle, e
							.getMessage(), e.getStatus());
		}
	}

	/*
	 * (non-Javadoc) Method declared on IAction.
	 */
	public void run() {
		IStructuredSelection selection = getStructuredSelection();

		IResource resource = getExistingResource(selection);

		if (resource instanceof IFile)
			openWorkspaceFile((IFile) resource);
		else {
			Object element = selection.getFirstElement();

			if (element instanceof GitModelObject) {
				IPath location = ((GitModelObject) element).getLocation();

				if (location != null && location.toFile().exists())
					openExternalFile(location.toFile());
			}
		}
	}

	private IResource getExistingResource(IStructuredSelection selection) {
		Object element = selection.getFirstElement();

		if (element instanceof IAdaptable) {
			IResource resource = (IResource) ((IAdaptable) element)
					.getAdapter(IResource.class);

			if (resource != null && resource.exists())
				return resource;
		}

		return null;
	}

	/**
	 * Enable the action only if the selection contains IFiles
	 */
	protected boolean updateSelection(IStructuredSelection selection) {
		return super.updateSelection(selection)
				&& selectionIsOfType(IResource.FILE);
	}
}

Back to the top