From 551ec668e8359f6838fb0d9dad19371c6b3c9e93 Mon Sep 17 00:00:00 2001 From: Joacim Breiler Date: Wed, 10 Aug 2022 06:51:38 +0200 Subject: [PATCH] Added feature for file rename (#1955) * Added feature for renaming files on the controller * Attempt to fix settings component in the designer so that its settings can be persisted --- .../utils/ThreadHelper.java | 5 +- .../designer/gui/SelectionSettingsPanel.java | 11 ++- .../platform/SettingsTopComponent.java | 16 +++-- .../nbp/filebrowser/FileBrowserDialog.java | 70 ++++++++++--------- .../ugs/nbp/filebrowser/FileTableModel.java | 8 ++- 5 files changed, 66 insertions(+), 44 deletions(-) diff --git a/ugs-core/src/com/willwinder/universalgcodesender/utils/ThreadHelper.java b/ugs-core/src/com/willwinder/universalgcodesender/utils/ThreadHelper.java index 2d8ee96d71..fe4d974639 100644 --- a/ugs-core/src/com/willwinder/universalgcodesender/utils/ThreadHelper.java +++ b/ugs-core/src/com/willwinder/universalgcodesender/utils/ThreadHelper.java @@ -18,7 +18,6 @@ This file is part of Universal Gcode Sender (UGS). */ package com.willwinder.universalgcodesender.utils; - import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -69,7 +68,7 @@ static public ScheduledFuture scheduleAtFixedRate(Runnable command, long inte * // Will wait ten seconds and then throw a TimeoutException as true will never be equal to false * ThreadHelper.waitUntil(() -> { return true == false}, 10, TimeUnit.SECONDS); * - * // Will return almost immediatly + * // Will return almost immediately * ThreadHelper.waitUntil(() -> { return true == true}, 10, TimeUnit.SECONDS); * } * @@ -88,7 +87,7 @@ static public void waitUntil(final BooleanSupplier waitUntilSupplier, int timeou } }).get(timeout, units); } catch (InterruptedException | ExecutionException e) { - logger.log(Level.WARNING, "An error occured while waiting for the thread to finnish", e); + logger.log(Level.WARNING, "An error occurred while waiting for the thread to finnish", e); } } diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/SelectionSettingsPanel.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/SelectionSettingsPanel.java index 5f03949602..665c7e0e31 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/SelectionSettingsPanel.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/gui/SelectionSettingsPanel.java @@ -80,6 +80,7 @@ public SelectionSettingsPanel(Controller controller) { addTextSettingFields(); addPositionSettings(); addCutSettings(controller); + registerControllerListeners(controller); } private void addPositionSettings() { @@ -180,13 +181,16 @@ public Component getListCellRendererComponent(JList list, Object value, int i add(targetDepthSpinner, "grow, wrap"); setEnabled(false); + targetDepthSpinner.setModel(new SpinnerNumberModel(controller.getSettings().getStockThickness(), 0d, controller.getSettings().getStockThickness(), 0.1d)); + } + + private void registerControllerListeners(Controller controller) { if (this.controller != null) { this.controller.getSelectionManager().removeSelectionListener(this); } this.controller = controller; this.controller.getSelectionManager().addSelectionListener(this); this.controller.getSelectionManager().addListener(this); - targetDepthSpinner.setModel(new SpinnerNumberModel(controller.getSettings().getStockThickness(), 0d, controller.getSettings().getStockThickness(), 0.1d)); } private void addTextSettingFields() { @@ -419,4 +423,9 @@ public void onAnchorChanged(Anchor anchor) { this.anchor = anchor; onEvent(new EntityEvent(controller.getSelectionManager(), EventType.MOVED)); } + + public void release() { + this.controller.getSelectionManager().removeSelectionListener(this); + this.controller.getSelectionManager().removeListener(this); + } } diff --git a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/platform/SettingsTopComponent.java b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/platform/SettingsTopComponent.java index 9670c93820..b912c5fdda 100644 --- a/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/platform/SettingsTopComponent.java +++ b/ugs-platform/ugs-platform-plugin-designer/src/main/java/com/willwinder/ugs/nbp/designer/platform/SettingsTopComponent.java @@ -23,7 +23,6 @@ This file is part of Universal Gcode Sender (UGS). import com.willwinder.ugs.nbp.designer.logic.ControllerEventType; import com.willwinder.ugs.nbp.designer.logic.ControllerListener; import com.willwinder.ugs.nbp.lib.lookup.CentralLookup; -import org.netbeans.api.settings.ConvertAsProperties; import org.openide.windows.TopComponent; /** @@ -37,24 +36,33 @@ This file is part of Universal Gcode Sender (UGS). public class SettingsTopComponent extends TopComponent implements ControllerListener { private static final long serialVersionUID = 324234398723987873L; - private transient final SelectionSettingsPanel selectionSettingsPanel; + private transient SelectionSettingsPanel selectionSettingsPanel; public SettingsTopComponent() { setMinimumSize(new java.awt.Dimension(50, 50)); setPreferredSize(new java.awt.Dimension(200, 200)); setLayout(new java.awt.BorderLayout()); setDisplayName("Cut settings"); + } + + @Override + protected void componentClosed() { + super.componentClosed(); + selectionSettingsPanel.release(); + selectionSettingsPanel = null; Controller controller = CentralLookup.getDefault().lookup(Controller.class); - selectionSettingsPanel = new SelectionSettingsPanel(controller); - controller.addListener(this); + controller.removeListener(this); } @Override protected void componentOpened() { super.componentOpened(); removeAll(); + Controller controller = CentralLookup.getDefault().lookup(Controller.class); + selectionSettingsPanel = new SelectionSettingsPanel(controller); add(selectionSettingsPanel); + controller.addListener(this); } @Override diff --git a/ugs-platform/ugs-platform-plugin-filebrowser/src/main/java/com/willwinder/ugs/nbp/filebrowser/FileBrowserDialog.java b/ugs-platform/ugs-platform-plugin-filebrowser/src/main/java/com/willwinder/ugs/nbp/filebrowser/FileBrowserDialog.java index 4cbe7cb296..9e9cc1b236 100644 --- a/ugs-platform/ugs-platform-plugin-filebrowser/src/main/java/com/willwinder/ugs/nbp/filebrowser/FileBrowserDialog.java +++ b/ugs-platform/ugs-platform-plugin-filebrowser/src/main/java/com/willwinder/ugs/nbp/filebrowser/FileBrowserDialog.java @@ -20,11 +20,13 @@ This file is part of Universal Gcode Sender (UGS). import com.willwinder.universalgcodesender.File; import com.willwinder.universalgcodesender.IFileService; +import com.willwinder.universalgcodesender.uielements.components.TableCellListener; import com.willwinder.universalgcodesender.uielements.helpers.LoaderDialogHelper; import com.willwinder.universalgcodesender.utils.ThreadHelper; import org.apache.commons.io.IOUtils; import org.openide.util.ImageUtilities; +import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFileChooser; @@ -39,14 +41,13 @@ This file is part of Universal Gcode Sender (UGS). import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; +import java.awt.event.ActionEvent; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.logging.Logger; -public class FileBrowserDialog extends JDialog implements MouseListener, ListSelectionListener { +public class FileBrowserDialog extends JDialog implements ListSelectionListener { private final Logger LOGGER = Logger.getLogger(FileBrowserDialog.class.getSimpleName()); private final FileTableModel tableModel; @@ -63,13 +64,22 @@ public FileBrowserDialog(IFileService fileService) { setTitle("File browser"); setPreferredSize(new Dimension(300, 400)); setMinimumSize(new Dimension(200, 300)); - setLayout(new BorderLayout()); tableModel = new FileTableModel(); fileTable = new FileTable(tableModel); fileTable.getSelectionModel().addListSelectionListener(this); - fileTable.addMouseListener(this); + new TableCellListener(fileTable, new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() instanceof TableCellListener) { + TableCellListener tableCellListener = (TableCellListener) e.getSource(); + String currentFilename = (String) tableCellListener.getOldValue(); + String newFileName = (String) tableCellListener.getNewValue(); + handleFileRename(currentFilename, newFileName); + } + } + }); add(new JScrollPane(fileTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER); @@ -94,6 +104,28 @@ public FileBrowserDialog(IFileService fileService) { pack(); } + private void handleFileRename(String currentFilename, String newFilename) { + setEnabled(false); + LoaderDialogHelper.showDialog("Renaming file", 1500, this); + LOGGER.info("Renaming file " + currentFilename + " to " + newFilename); + ThreadHelper.invokeLater(() -> { + try { + File currentFile = new File(currentFilename, currentFilename, 0); + byte[] bytes = fileService.downloadFile(currentFile); + Thread.sleep(2000); + fileService.uploadFile(newFilename, bytes); + Thread.sleep(2000); + fileService.deleteFile(currentFile); + } catch (IOException | InterruptedException ex) { + ex.printStackTrace(); + } finally { + LoaderDialogHelper.closeDialog(); + setEnabled(true); + refreshFileList(); + } + }); + } + private void refreshFileList() { try { tableModel.replace(fileService.getFiles()); @@ -132,33 +164,6 @@ private void handleFileUpload() { } } - @Override - public void mouseClicked(MouseEvent e) { - if (e.getClickCount() > 1) { - SwingUtilities.invokeLater(this::handleFileDownload); - } - } - - @Override - public void mousePressed(MouseEvent e) { - - } - - @Override - public void mouseReleased(MouseEvent e) { - - } - - @Override - public void mouseEntered(MouseEvent e) { - - } - - @Override - public void mouseExited(MouseEvent e) { - - } - @Override public void setEnabled(boolean enabled) { super.setEnabled(enabled); @@ -193,7 +198,6 @@ private void handleFileDownload() { } } - private void handleFileDelete() { setEnabled(false); LoaderDialogHelper.showDialog("Deleting file", 1500, this); diff --git a/ugs-platform/ugs-platform-plugin-filebrowser/src/main/java/com/willwinder/ugs/nbp/filebrowser/FileTableModel.java b/ugs-platform/ugs-platform-plugin-filebrowser/src/main/java/com/willwinder/ugs/nbp/filebrowser/FileTableModel.java index 36b997d6de..0f79f42206 100644 --- a/ugs-platform/ugs-platform-plugin-filebrowser/src/main/java/com/willwinder/ugs/nbp/filebrowser/FileTableModel.java +++ b/ugs-platform/ugs-platform-plugin-filebrowser/src/main/java/com/willwinder/ugs/nbp/filebrowser/FileTableModel.java @@ -71,7 +71,7 @@ public Class getColumnClass(int columnIndex) { @Override public boolean isCellEditable(int rowIndex, int columnIndex) { - return false; + return columnIndex == 0; } @Override @@ -89,11 +89,13 @@ public Object getValueAt(int rowIndex, int columnIndex) { @Override public void setValueAt(Object aValue, int rowIndex, int columnIndex) { - + File file = fileList.get(rowIndex); + if (columnIndex == 0) { + file.setAbsolutePath((String) aValue); + } } public File get(int index) { return fileList.get(index); - } }