Skip to content

Commit

Permalink
Added feature for file rename (#1955)
Browse files Browse the repository at this point in the history
* Added feature for renaming files on the controller
* Attempt to fix settings component in the designer so that its settings can be persisted
  • Loading branch information
breiler authored Aug 10, 2022
1 parent b5348cd commit 551ec66
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
* }</pre>
*
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public SelectionSettingsPanel(Controller controller) {
addTextSettingFields();
addPositionSettings();
addCutSettings(controller);
registerControllerListeners(controller);
}

private void addPositionSettings() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -193,7 +198,6 @@ private void handleFileDownload() {
}
}


private void handleFileDelete() {
setEnabled(false);
LoaderDialogHelper.showDialog("Deleting file", 1500, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Class<?> getColumnClass(int columnIndex) {

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
return columnIndex == 0;
}

@Override
Expand All @@ -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);

}
}

0 comments on commit 551ec66

Please sign in to comment.