1
2
3
4
5
6
7
8
9
10
11
12
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
29
30
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
38 protected final boolean initialClose;
39
40
41
42
43
44
45
46
47 protected AbstractInitialCloseBaseAction(String name, TopComponent topComponent, boolean initialClose) {
48 super(name, topComponent);
49
50 this.initialClose = initialClose;
51 }
52
53
54
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 }