View Javadoc
1   /**
2    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
3    * use this file except in compliance with the License. You may obtain a copy of
4    * the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
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   * Base class for closing project related tabs actions.
36   *
37   * @author bahlef
38   */
39  abstract class AbstractProjectBaseAction extends AbstractInitialCloseBaseAction {
40  	private static final long serialVersionUID = 5322225046091709258L;
41  
42  	private final String actionNamePrefix;
43  
44  	/**
45  	 * Constructor of abstract class {@link ActionBase}.
46  	 *
47  	 * @param name the name of this action
48  	 * @param initialClose flag indicating to close all tabs with the same ({@code true}) or all tabs with a different ({@code false}) project
49  	 */
50  	public AbstractProjectBaseAction(String name, TopComponent topComponent, boolean initialClose) {
51  		super(name, topComponent, initialClose);
52  
53  		actionNamePrefix = name;
54  	}
55  
56  	/**
57  	 * {@inheritDoc}
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  	 * {@inheritDoc}
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 }