Skip to content

Commit 7efa6a8

Browse files
authored
Made Close button Explicit (#3104)
- Crerated Action for close button. - Added icon. - Made sure that close button closes the active tabbed editor instead of random tool windows.
1 parent f545854 commit 7efa6a8

3 files changed

Lines changed: 159 additions & 10 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
Copyright 2026 Damian Nikodem
3+
4+
This file is part of Universal Gcode Sender (UGS).
5+
6+
UGS is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
UGS is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with UGS. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package com.willwinder.ugs.nbp.core.actions;
20+
21+
import com.willwinder.ugs.nbp.lib.Mode;
22+
import com.willwinder.ugs.nbp.lib.services.LocalizingService;
23+
import org.openide.awt.ActionID;
24+
import org.openide.awt.ActionReference;
25+
import org.openide.awt.ActionReferences;
26+
import org.openide.awt.ActionRegistration;
27+
import org.openide.loaders.DataObject;
28+
import org.openide.util.ImageUtilities;
29+
import org.openide.windows.TopComponent;
30+
import org.openide.windows.WindowManager;
31+
32+
import javax.swing.AbstractAction;
33+
import java.awt.event.ActionEvent;
34+
35+
/**
36+
* Closes the currently open editor/program file.
37+
*
38+
* @author Damian Nikodem
39+
*/
40+
@ActionID(
41+
category = LocalizingService.CATEGORY_FILE,
42+
id = "com.willwinder.ugs.nbp.core.actions.CloseFileAction")
43+
@ActionRegistration(
44+
displayName = "Close",
45+
lazy = false)
46+
@ActionReferences({
47+
@ActionReference(
48+
path = LocalizingService.MENU_FILE,
49+
position = 250)
50+
})
51+
public class CloseFileAction extends AbstractAction {
52+
public static final String ICON_BASE = "resources/icons/close.svg";
53+
private static final String WELCOME_PAGE_CLASS = "com.willwinder.ugp.welcome.WelcomePageTopComponent";
54+
private static final String GCODE_MIME_TYPE = "text/xgcode";
55+
private static final String DESIGN_MIME_TYPE = "application/x-ugs";
56+
57+
public CloseFileAction() {
58+
putValue("iconBase", ICON_BASE);
59+
putValue(SMALL_ICON, ImageUtilities.loadImageIcon(ICON_BASE, false));
60+
putValue(NAME, "Close");
61+
putValue("noIconInMenu", Boolean.FALSE);
62+
}
63+
64+
@Override
65+
public void actionPerformed(ActionEvent e) {
66+
TopComponent activated = TopComponent.getRegistry().getActivated();
67+
if (activated != null && WELCOME_PAGE_CLASS.equals(activated.getClass().getName()) && activated.close()) {
68+
return;
69+
}
70+
71+
TopComponent selectedEditor = getSelectedEditorTopComponent();
72+
if (selectedEditor != null && isCloseableDocument(selectedEditor)) {
73+
selectedEditor.close();
74+
}
75+
}
76+
77+
private boolean isCloseableDocument(TopComponent topComponent) {
78+
if (WELCOME_PAGE_CLASS.equals(topComponent.getClass().getName())) {
79+
return true;
80+
}
81+
82+
DataObject dataObject = topComponent.getLookup().lookup(DataObject.class);
83+
if (dataObject == null) {
84+
return false;
85+
}
86+
87+
String mimeType = dataObject.getPrimaryFile().getMIMEType();
88+
return GCODE_MIME_TYPE.equals(mimeType) || DESIGN_MIME_TYPE.equals(mimeType);
89+
}
90+
91+
private TopComponent getSelectedEditorTopComponent() {
92+
TopComponent primaryEditor = getSelectedTopComponentForMode(Mode.EDITOR_PRIMARY);
93+
if (primaryEditor != null && isCloseableDocument(primaryEditor)) {
94+
return primaryEditor;
95+
}
96+
97+
TopComponent secondaryEditor = getSelectedTopComponentForMode(Mode.EDITOR_SECONDARY);
98+
if (secondaryEditor != null && isCloseableDocument(secondaryEditor)) {
99+
return secondaryEditor;
100+
}
101+
102+
return null;
103+
}
104+
105+
private TopComponent getSelectedTopComponentForMode(String modeName) {
106+
org.openide.windows.Mode mode = WindowManager.getDefault().findMode(modeName);
107+
if (mode == null) {
108+
return null;
109+
}
110+
111+
return mode.getSelectedTopComponent();
112+
}
113+
}

ugs-platform/ugs-platform-ugscore/src/main/resources/com/willwinder/ugs/nbp/core/layer.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@
7474
<file name="org-openide-actions-SaveAsAction.instance_hidden"/>
7575
<file name="org-netbeans-modules-editor-ExportHtmlAction.shadow_hidden"/>
7676

77-
<file name="org-openide-actions-CloseAction.instance">
78-
<attr name="instanceCreate" methodvalue="org.openide.awt.Actions.context"/>
79-
<attr name="delegate" methodvalue="org.openide.awt.Actions.performer"/>
80-
<attr name="selectionType" stringvalue="ANY"/>
81-
<attr name="surviveFocusChange" boolvalue="false"/>
82-
<attr name="displayName" bundlevalue="org/openide/awt/Bundle#Close"/>
83-
<attr name="noIconInMenu" boolvalue="true"/>
84-
<attr name="type" stringvalue="org.netbeans.api.actions.Closable"/>
85-
</file>
86-
8777
<file name="Separator3.instance_hidden"/>
8878
<file name="org-openide-actions-PageSetupAction.shadow_hidden"/>
8979
<file name="org-openide-actions-PrintAction.shadow_hidden"/>
Lines changed: 46 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)