This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
/
OpenContextFileAction.java
99 lines (90 loc) · 3.29 KB
/
OpenContextFileAction.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
/*******************************************************************************
* Copyright (c) 2012 VMware, Inc.
* 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:
* VMware, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.eclipse.beans.ui.livegraph.actions;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.actions.BaseSelectionListenerAction;
import org.springframework.ide.eclipse.beans.ui.livegraph.LiveGraphUiPlugin;
import org.springframework.ide.eclipse.beans.ui.livegraph.model.LiveBean;
import org.springsource.ide.eclipse.commons.core.SpringCoreUtils;
import org.springsource.ide.eclipse.commons.core.StatusHandler;
import org.springsource.ide.eclipse.commons.ui.SpringUIUtils;
/**
* @author Leo Dos Santos
*/
public class OpenContextFileAction extends BaseSelectionListenerAction {
public OpenContextFileAction() {
super("Open Context File");
}
@Override
public void run() {
IStructuredSelection selection = getStructuredSelection();
List elements = selection.toList();
final List<String> contexts = new ArrayList<String>();
for (Object obj : elements) {
if (obj instanceof LiveBean) {
LiveBean bean = (LiveBean) obj;
final String appContext = bean.getResource();
if (appContext != null && appContext.trim().length() > 0) {
contexts.add(appContext);
}
}
}
// find the app contexts in the workspace and open them
try {
// need a project mapper in place of hard-coded sample
IProject project = SpringCoreUtils.createProject("org.springframework.samples.petclinic", null,
new NullProgressMonitor());
project.accept(new IResourceVisitor() {
public boolean visit(final IResource resource) throws CoreException {
if (resource instanceof IFile) {
for (String appContext : contexts) {
if (appContext.equals(resource.getName().trim())) {
SpringUIUtils.openInEditor((IFile) resource, 0);
}
}
return false;
}
return true;
}
});
}
catch (CoreException e) {
StatusHandler.log(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
"An error occurred while attempting to open an application context file.", e));
}
}
@Override
protected boolean updateSelection(IStructuredSelection selection) {
if (!selection.isEmpty()) {
List elements = selection.toList();
for (Object obj : elements) {
if (obj instanceof LiveBean) {
LiveBean bean = (LiveBean) obj;
String appContext = bean.getResource();
if (appContext != null && appContext.trim().length() > 0) {
return true;
}
}
}
}
return false;
}
}