-
-
Notifications
You must be signed in to change notification settings - Fork 766
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add menu for opening recent files (#2132)
* Add menu for opening recent files
- Loading branch information
Showing
7 changed files
with
170 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...gs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/actions/OpenFileAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright 2023 Will Winder | ||
This file is part of Universal Gcode Sender (UGS). | ||
UGS is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
UGS is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with UGS. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.willwinder.ugs.nbp.core.actions; | ||
|
||
import com.willwinder.ugs.nbp.lib.EditorUtils; | ||
import com.willwinder.ugs.nbp.lib.lookup.CentralLookup; | ||
import com.willwinder.universalgcodesender.model.BackendAPI; | ||
import com.willwinder.universalgcodesender.utils.GUIHelpers; | ||
import org.openide.cookies.OpenCookie; | ||
import org.openide.filesystems.FileUtil; | ||
import org.openide.loaders.DataObject; | ||
import org.openide.loaders.DataObjectNotFoundException; | ||
|
||
import javax.swing.AbstractAction; | ||
import javax.swing.Action; | ||
import java.awt.event.ActionEvent; | ||
import java.io.File; | ||
|
||
/** | ||
* A generic action for opening a file | ||
* | ||
* @author Joacim Breiler | ||
*/ | ||
public class OpenFileAction extends AbstractAction { | ||
private final File selectedFile; | ||
|
||
public OpenFileAction(File selectedFile) { | ||
this.selectedFile = selectedFile; | ||
putValue(NAME, selectedFile.getName()); | ||
putValue(Action.SHORT_DESCRIPTION, selectedFile.getAbsolutePath()); | ||
} | ||
|
||
@Override | ||
public void actionPerformed(ActionEvent event) { | ||
if (!EditorUtils.closeOpenEditors()) { | ||
return; | ||
} | ||
|
||
try { | ||
BackendAPI backend = CentralLookup.getDefault().lookup(BackendAPI.class); | ||
backend.getSettings().setLastOpenedFilename(selectedFile.getAbsolutePath()); | ||
OpenCookie c = DataObject.find(FileUtil.toFileObject(selectedFile)) | ||
.getLookup() | ||
.lookup(OpenCookie.class); | ||
if (c != null) c.open(); | ||
} catch (DataObjectNotFoundException e) { | ||
GUIHelpers.displayErrorDialog("Could not open file " + selectedFile.getAbsolutePath()); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/actions/OpenRecentAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2023 Will Winder | ||
This file is part of Universal Gcode Sender (UGS). | ||
UGS is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
UGS is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with UGS. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.willwinder.ugs.nbp.core.actions; | ||
|
||
import com.willwinder.ugs.nbp.lib.lookup.CentralLookup; | ||
import com.willwinder.ugs.nbp.lib.services.LocalizingService; | ||
import com.willwinder.universalgcodesender.model.BackendAPI; | ||
import org.openide.awt.ActionID; | ||
import org.openide.awt.ActionReference; | ||
import org.openide.awt.ActionReferences; | ||
import org.openide.awt.ActionRegistration; | ||
import org.openide.awt.DynamicMenuContent; | ||
|
||
import javax.swing.AbstractAction; | ||
import javax.swing.JComponent; | ||
import javax.swing.JMenu; | ||
import javax.swing.JMenuItem; | ||
import java.awt.event.ActionEvent; | ||
import java.io.File; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@ActionID( | ||
id = "com.willwinder.ugs.nbp.core.actions.OpenRecentAction", | ||
category = LocalizingService.OpenCategory) | ||
@ActionRegistration( | ||
iconBase = OpenAction.ICON_BASE, | ||
lazy = false, | ||
displayName = "Open recent...") | ||
@ActionReferences({ | ||
@ActionReference( | ||
path = LocalizingService.MENU_FILE, | ||
position = 11), | ||
}) | ||
public class OpenRecentAction extends AbstractAction implements DynamicMenuContent { | ||
|
||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
// does nothing, this is a popup menu | ||
} | ||
|
||
@Override | ||
public JComponent[] getMenuPresenters() { | ||
JMenu submenu = new JMenu(LocalizingService.OpenRecentTitle); | ||
createMenu().forEach(submenu::add); | ||
return new JComponent[]{submenu}; | ||
} | ||
|
||
@Override | ||
public JComponent[] synchMenuPresenters(JComponent[] items) { | ||
return getMenuPresenters(); | ||
} | ||
|
||
private List<JComponent> createMenu() { | ||
BackendAPI backend = CentralLookup.getDefault().lookup(BackendAPI.class); | ||
return backend.getSettings().getRecentFiles().stream() | ||
.map(file -> Paths.get(file).toFile()) | ||
.filter(File::exists) | ||
.filter(File::isFile) | ||
.map(file -> new JMenuItem(new OpenFileAction(file))) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters