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;
15  
16  import java.awt.event.ActionEvent;
17  import java.util.Objects;
18  import java.util.logging.Level;
19  import java.util.logging.Logger;
20  
21  import org.openide.windows.Mode;
22  import org.openide.windows.TopComponent;
23  import org.openide.windows.WindowManager;
24  
25  import de.funfried.netbeans.plugins.editor.closeleftright.AbstractBaseAction;
26  
27  /**
28   * Base class for closing left or right tabs actions.
29   *
30   * @author bahlef
31   */
32  public abstract class AbstractInitialCloseBaseAction extends AbstractBaseAction {
33  	private static final long serialVersionUID = -8499198129424546354L;
34  
35  	private static final Logger log = Logger.getLogger(AbstractInitialCloseBaseAction.class.getName());
36  
37  	/** Flag indicating to close all left or all right tabs. */
38  	protected final boolean initialClose;
39  
40  	/**
41  	 * Constructor of abstract class {@link AbstractBaseAction}.
42  	 *
43  	 * @param name the name of this action
44  	 * @param topComponent the {@link TopComponent}
45  	 * @param initialClose flag indicating to close all left ({@code true}) or all right ({@code false}) tabs
46  	 */
47  	protected AbstractInitialCloseBaseAction(String name, TopComponent topComponent, boolean initialClose) {
48  		super(name, topComponent);
49  
50  		this.initialClose = initialClose;
51  	}
52  
53  	/**
54  	 * {@inheritDoc}
55  	 */
56  	@Override
57  	public void actionPerformed(ActionEvent event) {
58  		Mode mode = WindowManager.getDefault().findMode(topComponent);
59  		if (mode == null) {
60  			return;
61  		}
62  
63  		boolean close = initialClose;
64  		for (TopComponent tc : mode.getTopComponents()) {
65  			if (Objects.equals(tc, topComponent)) {
66  				close = !close;
67  				continue;
68  			}
69  
70  			if (close && tc.isOpened()) {
71  				tc.close();
72  			}
73  		}
74  	}
75  
76  	@Override
77  	public boolean isEnabled() {
78  		Mode mode = WindowManager.getDefault().findMode(topComponent);
79  		if (mode == null) {
80  			return false;
81  		}
82  
83  		boolean close = initialClose;
84  
85  		try {
86  			for (TopComponent tc : mode.getTopComponents()) {
87  				if (Objects.equals(tc, topComponent)) {
88  					close = !close;
89  					continue;
90  				}
91  
92  				if (close && tc.isOpened()) {
93  					return true;
94  				}
95  			}
96  		} catch (Exception ex) {
97  			log.log(Level.WARNING, NAME, ex);
98  		}
99  
100 		return false;
101 	}
102 }