1
2
3
4
5
6
7
8
9
10
11
12
13
14 package de.funfried.netbeans.plugins.editor.closeleftright.actions.project;
15
16 import java.awt.event.ActionEvent;
17 import java.util.Objects;
18
19 import javax.swing.Action;
20
21 import org.apache.commons.lang3.ArrayUtils;
22 import org.netbeans.api.project.Project;
23 import org.netbeans.api.project.ProjectInformation;
24 import org.netbeans.api.project.ProjectUtils;
25 import org.netbeans.api.project.ui.OpenProjects;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileUtil;
28 import org.openide.windows.Mode;
29 import org.openide.windows.TopComponent;
30 import org.openide.windows.WindowManager;
31
32 import de.funfried.netbeans.plugins.editor.closeleftright.actions.AbstractInitialCloseBaseAction;
33
34
35
36
37
38
39 abstract class AbstractProjectBaseAction extends AbstractInitialCloseBaseAction {
40 private static final long serialVersionUID = 5322225046091709258L;
41
42 private final String actionNamePrefix;
43
44
45
46
47
48
49
50 public AbstractProjectBaseAction(String name, TopComponent topComponent, boolean initialClose) {
51 super(name, topComponent, initialClose);
52
53 actionNamePrefix = name;
54 }
55
56
57
58
59 @Override
60 public void actionPerformed(ActionEvent event) {
61 Project[] openProjects = OpenProjects.getDefault().getOpenProjects();
62
63 Project project = getProjectByTopComponent(openProjects, this.topComponent);
64 if (project == null) {
65 return;
66 }
67
68 Mode mode = WindowManager.getDefault().findMode(this.topComponent);
69 if (mode == null) {
70 return;
71 }
72
73 for (TopComponent tc : mode.getTopComponents()) {
74 Project otherProject = getProjectByTopComponent(openProjects, tc);
75 if (tc.isOpened() && ((initialClose && Objects.equals(project, otherProject)) || (!initialClose && !Objects.equals(project, otherProject)))) {
76 tc.close();
77 }
78 }
79 }
80
81
82
83
84 @Override
85 public boolean isEnabled() {
86 Project project = getProjectByTopComponent(OpenProjects.getDefault().getOpenProjects(), topComponent);
87 if (project != null) {
88 ProjectInformation projectInfo = ProjectUtils.getInformation(project);
89 if (projectInfo != null) {
90 putValue(Action.NAME, actionNamePrefix + " (" + projectInfo.getDisplayName() + ")");
91 }
92
93 return true;
94 } else {
95 putValue(Action.NAME, actionNamePrefix);
96
97 return false;
98 }
99 }
100
101 private Project getProjectByTopComponent(Project[] openProjects, TopComponent topComponent) {
102 if (ArrayUtils.isEmpty(openProjects) || topComponent == null) {
103 return null;
104 }
105
106 Project ret = null;
107
108 FileObject fileObject = topComponent.getLookup().lookup(FileObject.class);
109 if (fileObject != null) {
110 for (Project project : openProjects) {
111 if (FileUtil.isParentOf(project.getProjectDirectory(), fileObject)
112 && (ret == null || FileUtil.isParentOf(ret.getProjectDirectory(), project.getProjectDirectory()))) {
113 ret = project;
114 }
115 }
116 }
117
118 return ret;
119 }
120 }