Skip to content

Commit

Permalink
Merge pull request #1067 from winder/start-page
Browse files Browse the repository at this point in the history
Welcome Page
  • Loading branch information
winder committed Jun 26, 2018
2 parents 766ad56 + a9de9bf commit c6c85d5
Show file tree
Hide file tree
Showing 66 changed files with 2,026 additions and 63 deletions.
Expand Up @@ -91,13 +91,13 @@ public void componentOpened() {
public void componentClosed() {
}

void writeProperties(java.util.Properties p) {
public void writeProperties(java.util.Properties p) {
// better to version settings since initial version as advocated at
// http://wiki.apidesign.org/wiki/PropertyFiles
p.setProperty("version", "1.0");
}

void readProperties(java.util.Properties p) {
public void readProperties(java.util.Properties p) {
String version = p.getProperty("version");
}
}
12 changes: 2 additions & 10 deletions ugs-core/src/com/willwinder/universalgcodesender/MainWindow.java
Expand Up @@ -168,10 +168,6 @@ public MainWindow(BackendAPI backend) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
if (fileChooser.getSelectedFile() != null ) {
settings.setLastOpenedFilename(fileChooser.getSelectedFile().getAbsolutePath());
}

settings.setPort(commPortComboBox.getSelectedItem().toString());
settings.setPortRate(baudrateSelectionComboBox.getSelectedItem().toString());
settings.setScrollWindowEnabled(scrollWindowCheckBox.isSelected());
Expand Down Expand Up @@ -292,10 +288,6 @@ public void run() {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
if (mw.fileChooser.getSelectedFile() != null ) {
mw.settings.setLastOpenedFilename(mw.fileChooser.getSelectedFile().getAbsolutePath());
}

mw.settings.setPort(mw.commPortComboBox.getSelectedItem().toString());
mw.settings.setPortRate(mw.baudrateSelectionComboBox.getSelectedItem().toString());
mw.settings.setScrollWindowEnabled(mw.scrollWindowCheckBox.isSelected());
Expand All @@ -315,7 +307,7 @@ public void run() {
for (String arg : args) {
if (open) {
try {
backend.setGcodeFile(new File(arg));
GUIHelpers.openGcodeFile(new File(arg), backend);
open = false;
} catch (Exception ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
Expand Down Expand Up @@ -1267,7 +1259,7 @@ private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
File gcodeFile = fileChooser.getSelectedFile();
backend.setGcodeFile(gcodeFile);
GUIHelpers.openGcodeFile(gcodeFile, backend);
} catch (Exception ex) {
logger.log(Level.SEVERE, "Problem while browsing.", ex);
displayErrorDialog(ex.getMessage());
Expand Down
@@ -1,5 +1,5 @@
/*
Copyright 2016-2017 Will Winder
Copyright 2016-2018 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand All @@ -19,12 +19,14 @@ This file is part of Universal Gcode Sender (UGS).
package com.willwinder.universalgcodesender.utils;

import com.willwinder.universalgcodesender.i18n.Localization;
import com.willwinder.universalgcodesender.model.BackendAPI;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import javax.swing.JOptionPane;
import java.util.logging.Logger;

/**
*
* @author wwinder
Expand Down Expand Up @@ -85,4 +87,18 @@ public static void displayHelpDialog(final String helpMessage) {
// Localization.getString("help"), JOptionPane.INFORMATION_MESSAGE);
});
}

public static void openGcodeFile(File f, BackendAPI backend) {
ThreadHelper.invokeLater(() -> {
try {
backend.setGcodeFile(f);
Settings settings = backend.getSettings();
settings.setLastOpenedFilename(f.getAbsolutePath());
SettingsFactory.saveSettings(settings);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Couldn't set gcode-file" + e.getMessage(), e);
}
});
}

}
@@ -1,5 +1,5 @@
/*
Copyright 2014-2017 Will Winder
Copyright 2014-2018 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand All @@ -24,16 +24,21 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.universalgcodesender.pendantui.PendantConfigBean;
import com.willwinder.universalgcodesender.types.Macro;
import com.willwinder.universalgcodesender.types.WindowSettings;
import java.nio.file.Path;
import java.nio.file.Paths;

import java.util.*;
import org.apache.commons.lang3.StringUtils;

public class Settings {
// Transient, don't serialize or deserialize.
transient private SettingChangeListener listener = null;
transient public static int HISTORY_SIZE = 20;

private String firmwareVersion = "GRBL";
private String fileName = System.getProperty("user.home");
private Deque<String> fileHistory = new ArrayDeque<>();
private Deque<String> dirHistory = new ArrayDeque<>();
private String port = "";
private String portRate = "115200";
private boolean manualModeEnabled = false;
Expand Down Expand Up @@ -125,7 +130,9 @@ public void setSettingChangeListener(SettingChangeListener listener) {
}

private void changed() {
listener.settingChanged();
if (listener != null) {
listener.settingChanged();
}
}

public String getFirmwareVersion() {
Expand All @@ -142,11 +149,37 @@ public String getLastOpenedFilename() {
return fileName;
}

public void setLastOpenedFilename(String fileName) {
this.fileName = fileName;
public void setLastOpenedFilename(String absolutePath) {
Path p = Paths.get(absolutePath).toAbsolutePath();
this.fileName = p.toString();
updateRecentFiles(p.toString());
updateRecentDirectory(p.getParent().toString());
changed();
}

public Collection<String> getRecentFiles() {
return Collections.unmodifiableCollection(fileHistory);
}

public void updateRecentFiles(String absolutePath) {
updateRecent(this.fileHistory, HISTORY_SIZE, absolutePath);
}

public Collection<String> getRecentDirectories() {
return Collections.unmodifiableCollection(dirHistory);
}

public void updateRecentDirectory(String absolutePath) {
updateRecent(this.dirHistory, HISTORY_SIZE, absolutePath);
}

private static void updateRecent(Deque<String> stack, int maxSize, String element) {
stack.remove(element);
stack.push(element);
while( stack.size() > maxSize)
stack.removeLast();
}

public String getPort() {
return port;
}
Expand Down
@@ -1,5 +1,5 @@
/*
Copywrite 2018 Will Winder
Copyright 2018 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand All @@ -18,6 +18,8 @@ This file is part of Universal Gcode Sender (UGS).
*/
package com.willwinder.universalgcodesender.utils;

import static com.willwinder.universalgcodesender.utils.Settings.HISTORY_SIZE;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -78,4 +80,51 @@ public void changingValueShouldNotifyObservers() {
assertTrue(hasNotifiedListener);
}

@Test
public void settingFileShouldUpdateRecents() {
String path = "/some/file";
String file = path + "/file.gcode";
target.setLastOpenedFilename(file);

Assertions.assertThat(target.getRecentFiles())
.hasSize(1)
.containsExactly(file);
Assertions.assertThat(target.getRecentDirectories())
.hasSize(1)
.containsExactly(path);
}

@Test
public void recentsShouldOverflowOldestAndReturnLIFO() {
String path = "/some/file";

// Add up recents to the brim.
for(int i = 0; i < HISTORY_SIZE; i++) {
target.setLastOpenedFilename(path + i + "/file.gcode");
}

// Overflow.
target.setLastOpenedFilename(path + HISTORY_SIZE + "/file.gcode");

Assertions.assertThat(target.getRecentFiles())
.hasSize(HISTORY_SIZE)
.doesNotContain(path + "0/file.gcode");
Assertions.assertThat(target.getRecentDirectories())
.hasSize(HISTORY_SIZE)
.doesNotContain(path + "0");

// Re-add "1" then overflow "2"
target.setLastOpenedFilename(path + "1/file.gcode");
target.setLastOpenedFilename(path + (HISTORY_SIZE + 1) + "/file.gcode");

// Verify that "2" was bumped and that "1" is the most recent.
Assertions.assertThat(target.getRecentFiles())
.hasSize(HISTORY_SIZE)
.doesNotContain(path + "2/file.gcode")
.startsWith(path + "21/file.gcode", path + "1/file.gcode");
Assertions.assertThat(target.getRecentDirectories())
.hasSize(HISTORY_SIZE)
.doesNotContain(path + "2")
.startsWith(path + "21", path + "1");
}
}
Expand Up @@ -280,19 +280,21 @@ private void generateOneTile(double offsetX, double offsetY, PrintWriter output)

@Override
public void componentOpened() {
// Unused for this top component.
}

@Override
public void componentClosed() {
// Unused for this top component.
}

void writeProperties(java.util.Properties p) {
public void writeProperties(java.util.Properties p) {
// better to version settings since initial version as advocated at
// http://wiki.apidesign.org/wiki/PropertyFiles
p.setProperty("version", "1.0");
}

void readProperties(java.util.Properties p) {
String version = p.getProperty("version");
public void readProperties(java.util.Properties p) {
//String version = p.getProperty("version");
}
}
5 changes: 5 additions & 0 deletions ugs-platform/application/pom.xml
Expand Up @@ -96,6 +96,11 @@
<artifactId>ugs-platform-plugin-setup-wizard</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ugs-platform-welcome-page</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
1 change: 1 addition & 0 deletions ugs-platform/pom.xml
Expand Up @@ -169,5 +169,6 @@
<module>ProbeModule</module>
<module>DowelModule</module>
<module>GcodeTools</module>
<module>ugs-platform-welcome-page</module>
</modules>
</project>
1 change: 0 additions & 1 deletion ugs-platform/ugs-platform-gcode-editor/pom.xml
Expand Up @@ -8,7 +8,6 @@
</parent>

<artifactId>ugs-platform-gcode-editor</artifactId>
<version>2.0-SNAPSHOT</version>
<packaging>nbm</packaging>

<build>
Expand Down
Expand Up @@ -474,15 +474,15 @@ private int moveRow(int row, int offset) {
return dest;
}

void writeProperties(java.util.Properties p) {
public void writeProperties(java.util.Properties p) {
// better to version settings since initial version as advocated at
// http://wiki.apidesign.org/wiki/PropertyFiles
p.setProperty("version", "1.0");
// TODO store your settings
}

void readProperties(java.util.Properties p) {
String version = p.getProperty("version");
public void readProperties(java.util.Properties p) {
//String version = p.getProperty("version");
// TODO read your settings according to their version
}
}
Expand Up @@ -690,9 +690,11 @@ public void componentClosed() {
r = null;
}

void writeProperties(java.util.Properties p) {
public void writeProperties(java.util.Properties p) {
// No properties
}

void readProperties(java.util.Properties p) {
public void readProperties(java.util.Properties p) {
// No properties
}
}
@@ -1,5 +1,5 @@
/*
Copywrite 2015-2016 Will Winder
Copywrite 2015-2018 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand All @@ -21,10 +21,8 @@ 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.Settings;
import com.willwinder.universalgcodesender.utils.SettingsFactory;
import com.willwinder.universalgcodesender.utils.GUIHelpers;
import com.willwinder.universalgcodesender.utils.SwingHelpers;
import com.willwinder.universalgcodesender.utils.ThreadHelper;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
Expand All @@ -33,7 +31,6 @@ This file is part of Universal Gcode Sender (UGS).

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.File;

@ActionID(
category = LocalizingService.OpenCategory,
Expand Down Expand Up @@ -72,24 +69,11 @@ public boolean isEnabled() {
return backend != null;
}

private void openGcodeFile(File f) {
ThreadHelper.invokeLater(() -> {
try {
backend.setGcodeFile(f);
Settings settings = backend.getSettings();
settings.setLastOpenedFilename(f.getAbsolutePath());
SettingsFactory.saveSettings(settings);
} catch (Exception e) {
System.err.println("Couldn't set gcode-file" + e.getMessage());
}
});
}

@Override
public void actionPerformed(ActionEvent e) {
String sourceDir = backend.getSettings().getLastOpenedFilename();
SwingHelpers
.openFile(sourceDir)
.ifPresent(this::openGcodeFile);
.ifPresent(f -> GUIHelpers.openGcodeFile(f, backend));
}
}

0 comments on commit c6c85d5

Please sign in to comment.