Skip to content

Commit

Permalink
Add menu for opening recent files (#2132)
Browse files Browse the repository at this point in the history
* Add menu for opening recent files
  • Loading branch information
breiler authored Jan 19, 2023
1 parent 4db31d6 commit 517b08b
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 32 deletions.
1 change: 1 addition & 0 deletions ugs-core/src/resources/MessagesBundle_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ toolbar.icon.small = Small Icons
toolbar.icon.large = Large Icons
sender.help.verbose.console = Show additional information in the controller console.
platform.menu.open = Open...
platform.menu.open.recent = Open recent
platform.menu.save = Save
platform.menu.edit = Edit Gcode File...
mainWindow.swing.reset = Reset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ This file is part of Universal Gcode Sender (UGS).
@ActionReferences({
@ActionReference(
path = LocalizingService.OpenWindowPath,
position = 11)
position = 12)
})

public final class CloudStorageOpenAction implements ActionListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.ugs.nbp.lib.lookup.CentralLookup;
import com.willwinder.ugs.nbp.lib.services.LocalizingService;
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.utils.GUIHelpers;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
Expand Down Expand Up @@ -61,8 +62,8 @@ This file is part of Universal Gcode Sender (UGS).
public final class OpenAction extends AbstractAction {

public static final String ICON_BASE = "resources/icons/open.svg";
private transient final FileFilterService fileFilterService;
private transient final BackendAPI backend;
private final transient FileFilterService fileFilterService;
private final transient BackendAPI backend;
private final JFileChooser fileChooser;

public OpenAction() {
Expand Down Expand Up @@ -93,31 +94,24 @@ public void actionPerformed(ActionEvent e) {

int returnVal = fileChooser.showOpenDialog(new JFrame());
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
File selectedFile = fileChooser.getSelectedFile();
openFile(selectedFile);
} catch (DataObjectNotFoundException ex) {
ex.printStackTrace();
}
File selectedFile = fileChooser.getSelectedFile();
openFile(selectedFile);
}
}

public void openFile(File selectedFile) throws DataObjectNotFoundException {
if (EditorUtils.closeOpenEditors()) {
backend.getSettings().setLastOpenedFilename(selectedFile.getAbsolutePath());
OpenCookie c = DataObject.find(FileUtil.toFileObject(selectedFile))
.getLookup()
.lookup(OpenCookie.class);
if (c != null) c.open();
}
public void openFile(File selectedFile) {
OpenFileAction action = new OpenFileAction(selectedFile);
action.actionPerformed(null);
}

private JFileChooser createFileChooser(String directory) {
JFileChooser fileChooser = new JFileChooser(directory);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setFileHidingEnabled(true);
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
fileChooser.setAcceptAllFileFilterUsed(true);
return fileChooser;
JFileChooser chooser = new JFileChooser(directory);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setFileHidingEnabled(true);
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setAcceptAllFileFilterUsed(true);
return chooser;
}


}
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());
}
}
}
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public class LocalizingService {
public final static String OpenActionId = "com.willwinder.ugs.nbp.core.actions.OpenAction";
public final static String OpenCategory = CATEGORY_FILE;

public final static String OpenRecentTitleKey = "platform.menu.open.recent";
public final static String OpenRecentTitle = Localization.getString(OpenRecentTitleKey, lang);

public final static String SaveTitleKey = "platform.menu.save";
public final static String SaveTitle = Localization.getString(SaveTitleKey, lang);
public final static String SaveWindowPath = MENU_FILE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.ugp.welcome.content.AbstractTab;
import com.willwinder.ugp.welcome.content.JLinkButton;
import com.willwinder.ugs.nbp.core.actions.OpenAction;
import com.willwinder.ugs.nbp.core.actions.OpenFileAction;
import com.willwinder.ugs.nbp.lib.lookup.CentralLookup;
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.uielements.helpers.ThemeColors;
import net.miginfocom.swing.MigLayout;
import org.openide.loaders.DataObjectNotFoundException;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
Expand All @@ -44,7 +44,7 @@ This file is part of Universal Gcode Sender (UGS).
*/
public class RecentWorkTab extends AbstractTab {
private static final Logger LOGGER = Logger.getLogger(RecentWorkTab.class.getName());
private final BackendAPI backend;
private final transient BackendAPI backend;

public RecentWorkTab() {
super("Recent Work");
Expand Down Expand Up @@ -120,13 +120,7 @@ private JLinkButton createFileButton(Path p) {
button.setLinkColor(ThemeColors.LIGHT_BLUE_GREY);
}

button.addActionListener(l -> {
try {
new OpenAction().openFile(p.toFile());
} catch (DataObjectNotFoundException e) {
LOGGER.log(Level.SEVERE, "Could not open file " + p, e);
}
});
button.addActionListener(l -> new OpenFileAction(p.toFile()).actionPerformed(null));
return button;
}
}

0 comments on commit 517b08b

Please sign in to comment.