-
Notifications
You must be signed in to change notification settings - Fork 7
/
OpenSnapshotAction.java
210 lines (184 loc) · 6.95 KB
/
OpenSnapshotAction.java
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*******************************************************************************
* Copyright (c) 2008, 2020 SAP AG and IBM Corporation.
* 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
*
* Contributors:
* SAP AG - initial API and implementation
* Andrew Johnson - remove deprecated call
*******************************************************************************/
package org.eclipse.mat.ui.snapshot.actions;
import java.util.ArrayList;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.mat.ui.MemoryAnalyserPlugin;
import org.eclipse.mat.ui.Messages;
import org.eclipse.mat.ui.editor.PathEditorInput;
import org.eclipse.mat.ui.snapshot.OpenSnapshot;
import org.eclipse.mat.util.MessageUtil;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipse.ui.part.FileEditorInput;
public class OpenSnapshotAction extends Action implements IWorkbenchWindowActionDelegate
{
private IWorkbenchWindow fWindow;
public OpenSnapshotAction()
{}
public void run(IAction action)
{
run();
}
public void run()
{
boolean successful = new OpenSnapshot.Visitor()
{
@Override
public void visit(IFileStore fileStore)
{
try
{
IEditorInput input = createEditorInput(fileStore);
IWorkbenchPage page = fWindow.getActivePage();
page.openEditor(input, MemoryAnalyserPlugin.EDITOR_ID);
}
catch (OperationCanceledException e)
{}
catch (PartInitException e)
{
MemoryAnalyserPlugin.log(e.getStatus());
String msg = MessageUtil.format(Messages.OpenSnapshotAction_ErrorOpeningFile, fileStore.getName());
MessageDialog.openError(fWindow.getShell(), Messages.ErrorHelper_InternalError, msg);
}
}
}.go(getWindow().getShell());
if (successful && PlatformUI.getWorkbench().getIntroManager().getIntro() != null)
{
// if this action was called with open welcome page - set it to
// standby mode.
PlatformUI.getWorkbench().getIntroManager().setIntroStandby(
PlatformUI.getWorkbench().getIntroManager().getIntro(), true);
}
}
public static class Handler extends AbstractHandler
{
public Handler()
{}
public Object execute(ExecutionEvent executionEvent)
{
new OpenSnapshotAction().run();
return null;
}
}
static class FileLabelProvider extends LabelProvider
{
/*
* @see
* org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
*/
public String getText(Object element)
{
if (element instanceof IFile)
{
IPath path = ((IFile) element).getFullPath();
return path != null ? path.toString() : ""; //$NON-NLS-1$
}
return super.getText(element);
}
}
public void dispose()
{
fWindow = null;
}
public void init(IWorkbenchWindow window)
{
fWindow = window;
}
public void selectionChanged(IAction action, ISelection selection)
{}
// //////////////////////////////////////////////////////////////
// internal helper methods
// //////////////////////////////////////////////////////////////
private IWorkbenchWindow getWindow()
{
IWorkbench workbench = PlatformUI.getWorkbench();
fWindow = workbench.getActiveWorkbenchWindow();
return fWindow;
}
// //////////////////////////////////////////////////////////////
// helpers to actually correctly open the editor window
// //////////////////////////////////////////////////////////////
private IEditorInput createEditorInput(IFileStore fileStore)
{
try
{
IFile workspaceFile = getWorkspaceFile(fileStore);
if (workspaceFile != null)
return new FileEditorInput(workspaceFile);
return new PathEditorInput(new Path(fileStore.toLocalFile(EFS.NONE, new NullProgressMonitor())
.getAbsolutePath()));
}
catch (CoreException e)
{
throw new RuntimeException(e);
}
}
private IFile getWorkspaceFile(IFileStore fileStore)
{
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IFile[] files = workspace.getRoot().findFilesForLocationURI(fileStore.toURI());
files = filterNonExistentFiles(files);
if (files == null || files.length == 0)
return null;
if (files.length == 1)
return files[0];
return selectWorkspaceFile(files);
}
private IFile[] filterNonExistentFiles(IFile[] files)
{
if (files == null)
return null;
int length = files.length;
ArrayList<IFile> existentFiles = new ArrayList<IFile>(length);
for (int i = 0; i < length; i++)
{
if (files[i].exists())
existentFiles.add(files[i]);
}
return existentFiles.toArray(new IFile[existentFiles.size()]);
}
private IFile selectWorkspaceFile(IFile[] files)
{
ElementListSelectionDialog dialog = new ElementListSelectionDialog(fWindow.getShell(), new FileLabelProvider());
dialog.setElements(files);
dialog.setTitle(Messages.OpenSnapshotAction_SelectWorkspace);
dialog.setMessage(Messages.OpenSnapshotAction_Message);
if (dialog.open() == Window.OK)
return (IFile) dialog.getFirstResult();
else
throw new OperationCanceledException();
}
}