Add ability to specify default values for the wizard from system property. GitHub issue #32

This commit is contained in:
Zlatin Balevsky
2020-06-01 12:09:58 +01:00
parent 59353a6718
commit b9c34cb944
10 changed files with 80 additions and 29 deletions

View File

@ -12,6 +12,7 @@ import com.muwire.core.MuWireSettings
import com.muwire.core.UILoadedEvent
import com.muwire.core.files.FileSharedEvent
import com.muwire.core.util.DataUtil
import com.muwire.gui.wizard.WizardDefaults
import javax.annotation.Nonnull
import javax.inject.Inject
@ -54,11 +55,21 @@ class Ready extends AbstractLifecycleHandler {
props = new MuWireSettings()
boolean embeddedRouterAvailable = Boolean.parseBoolean(System.getProperties().getProperty("embeddedRouter"))
def defaults
if (System.getProperties().containsKey("wizard.defaults")) {
File defaultsFile = new File(System.getProperty("wizard.defaults"))
Properties defaultsProps = new Properties()
defaultsFile.withInputStream { defaultsProps.load(it) }
defaults = new WizardDefaults(defaultsProps)
} else
defaults = new WizardDefaults()
def parent = application.windowManager.findWindow("event-list")
Properties i2pProps = new Properties()
def params = [:]
params['parent'] = parent
params['defaults'] = defaults
params['embeddedRouterAvailable'] = embeddedRouterAvailable
params['muSettings'] = props
params['i2pProps'] = i2pProps

View File

@ -11,6 +11,7 @@ import griffon.metadata.ArtifactProviderFor
@ArtifactProviderFor(GriffonModel)
class WizardModel {
Component parent
WizardDefaults defaults
boolean embeddedRouterAvailable
MuWireSettings muSettings
Properties i2pProps
@ -26,12 +27,12 @@ class WizardModel {
void mvcGroupInit(Map<String,String> args) {
steps << new NicknameStep()
steps << new DirectoriesStep()
steps << new DirectoriesStep(defaults)
if (embeddedRouterAvailable)
steps << new EmbeddedRouterStep()
steps << new EmbeddedRouterStep(defaults)
else
steps << new ExternalRouterStep()
steps << new TunnelStep()
steps << new ExternalRouterStep(defaults)
steps << new TunnelStep(defaults)
steps << new LastStep(embeddedRouterAvailable)
currentStep = 0

View File

@ -13,14 +13,12 @@ class DirectoriesStep extends WizardStep {
def downloadLocationButton
def incompleteLocationButton
public DirectoriesStep(String constraint) {
super("directories")
public DirectoriesStep(WizardDefaults defaults) {
super("directories", defaults)
}
@Override
protected void buildUI(FactoryBuilderSupport builder) {
File defaultDownloadLocation = new File(System.getProperty("user.home"), "MuWire Downloads")
File defaultIncompleteLocation = new File(System.getProperty("user.home"), "MuWire Incompletes")
builder.panel(constraints : getConstraint()) {
gridBagLayout()
@ -30,11 +28,11 @@ class DirectoriesStep extends WizardStep {
constraints : gbc(gridx:0, gridy: 1, gridwidth: 2, insets: [0,0,10,0]))
label(text : "Directory for saving downloaded files", constraints : gbc(gridx:0, gridy: 2))
downloadLocationField = textField(text : defaultDownloadLocation.getAbsolutePath(),
downloadLocationField = textField(text : defaults.downloadLocation,
constraints : gbc(gridx : 0, gridy : 3, fill : GridBagConstraints.HORIZONTAL, weightx: 100))
downloadLocationButton = button(text : "Choose", constraints : gbc(gridx: 1, gridy: 3), actionPerformed : showDownloadChooser)
label(text : "Directory for storing incomplete files", constraints : gbc(gridx:0, gridy: 4))
incompleteLocationField = textField(text : defaultIncompleteLocation.getAbsolutePath(),
incompleteLocationField = textField(text : defaults.incompleteLocation,
constraints : gbc(gridx:0, gridy:5, fill : GridBagConstraints.HORIZONTAL, weightx: 100))
incompleteLocationButton = button(text : "Choose", constraints : gbc(gridx: 1, gridy: 5), actionPerformed : showIncompleteChooser)
}

View File

@ -15,31 +15,29 @@ class EmbeddedRouterStep extends WizardStep {
def inBwField
def outBwField
public EmbeddedRouterStep() {
super("router")
public EmbeddedRouterStep(WizardDefaults defaults) {
super("router", defaults)
}
@Override
protected void buildUI(FactoryBuilderSupport builder) {
Random r = new Random()
int port = 9151 + r.nextInt(1 + 30777 - 9151) // this range matches what the i2p router would choose
builder.panel(constraints : getConstraint()) {
gridBagLayout()
panel(border : titledBorder(title : "Port Settings", border : etchedBorder(), titlePosition : TitledBorder.TOP,
constraints : gbc(gridx: 0, gridy : 0, fill : GridBagConstraints.HORIZONTAL, weightx: 100))) {
gridBagLayout()
label(text : "TCP port", constraints : gbc(gridx :0, gridy: 0, anchor : GridBagConstraints.LINE_START, weightx : 100))
tcpPortField = textField(text : String.valueOf(port), columns : 4, constraints : gbc(gridx:1, gridy:0, anchor : GridBagConstraints.LINE_END))
tcpPortField = textField(text : String.valueOf(defaults.i2npTcpPort), columns : 4, constraints : gbc(gridx:1, gridy:0, anchor : GridBagConstraints.LINE_END))
label(text : "UDP port", constraints : gbc(gridx :0, gridy: 1, anchor : GridBagConstraints.LINE_START, weightx : 100))
udpPortField = textField(text : String.valueOf(port), columns : 4, constraints : gbc(gridx:1, gridy:1, anchor : GridBagConstraints.LINE_END))
udpPortField = textField(text : String.valueOf(defaults.i2npUdpPort), columns : 4, constraints : gbc(gridx:1, gridy:1, anchor : GridBagConstraints.LINE_END))
}
panel( border : titledBorder(title : "Bandwidth Settings", border : etchedBorder(), titlePosition : TitledBorder.TOP),
constraints : gbc(gridx : 0, gridy : 1, fill : GridBagConstraints.HORIZONTAL, weightx : 100)) {
gridBagLayout()
label(text : "Inbound bandwidth (KB)", constraints : gbc(gridx: 0, gridy : 0, anchor : GridBagConstraints.LINE_START, weightx : 100))
inBwField = textField(text : "512", columns : 3, constraints : gbc(gridx : 1, gridy : 0, anchor : GridBagConstraints.LINE_END))
inBwField = textField(text : String.valueOf(defaults.inBw), columns : 3, constraints : gbc(gridx : 1, gridy : 0, anchor : GridBagConstraints.LINE_END))
label(text : "Outbound bandwidth (KB)", constraints : gbc(gridx: 0, gridy : 1, anchor : GridBagConstraints.LINE_START, weightx : 100))
outBwField = textField(text : "256", columns : 3, constraints : gbc(gridx : 1, gridy : 1, anchor : GridBagConstraints.LINE_END))
outBwField = textField(text : String.valueOf(defaults.outBw), columns : 3, constraints : gbc(gridx : 1, gridy : 1, anchor : GridBagConstraints.LINE_END))
}
panel (constraints : gbc(gridx: 0, gridy : 2, weighty: 100))
}

View File

@ -11,8 +11,8 @@ class ExternalRouterStep extends WizardStep {
def addressField
def portField
public ExternalRouterStep() {
super("router")
public ExternalRouterStep(WizardDefaults defaults) {
super("router", defaults)
}
@Override
@ -24,10 +24,10 @@ class ExternalRouterStep extends WizardStep {
gridBagLayout()
label(text : "Host", constraints : gbc(gridx: 0, gridy : 0, anchor : GridBagConstraints.LINE_START, weightx : 100))
addressField = textField(text : "127.0.0.1", constraints : gbc(gridx:1, gridy:0, anchor: GridBagConstraints.LINE_END))
addressField = textField(text : defaults.i2cpHost, constraints : gbc(gridx:1, gridy:0, anchor: GridBagConstraints.LINE_END))
label(text : "Port", constraints : gbc(gridx: 0, gridy : 1, anchor : GridBagConstraints.LINE_START, weightx : 100))
portField = textField(text : "7654", constraints : gbc(gridx:1, gridy:1, anchor: GridBagConstraints.LINE_END))
portField = textField(text : String.valueOf(defaults.i2cpPort), constraints : gbc(gridx:1, gridy:1, anchor: GridBagConstraints.LINE_END))
}
panel(constraints : gbc(gridx:0, gridy:1, weighty: 100))
}

View File

@ -7,7 +7,7 @@ class LastStep extends WizardStep {
private final boolean embeddedRouterAvailable
public LastStep(boolean embeddedRouterAvailable) {
super("last")
super("last", null)
this.embeddedRouterAvailable = embeddedRouterAvailable
}

View File

@ -9,7 +9,7 @@ class NicknameStep extends WizardStep {
volatile def nickField
public NicknameStep() {
super("nickname")
super("nickname", null)
}
@Override

View File

@ -13,8 +13,8 @@ class TunnelStep extends WizardStep {
def tunnelLengthSlider
def tunnelQuantitySlider
public TunnelStep() {
super("tunnels")
public TunnelStep(WizardDefaults defaults) {
super("tunnels", defaults)
}
@Override
@ -26,7 +26,7 @@ class TunnelStep extends WizardStep {
def lengthTable = new Hashtable()
lengthTable.put(1, new JLabel("Max Speed"))
lengthTable.put(3, new JLabel("Max Anonymity"))
tunnelLengthSlider = slider(minimum : 1, maximum : 3, value : 3,
tunnelLengthSlider = slider(minimum : 1, maximum : 3, value : defaults.tunnelLength,
majorTickSpacing : 1, snapToTicks: true, paintTicks: true, labelTable : lengthTable,
paintLabels : true)
}
@ -35,7 +35,7 @@ class TunnelStep extends WizardStep {
def quantityTable = new Hashtable()
quantityTable.put(1, new JLabel("Min Resources"))
quantityTable.put(6, new JLabel("Max Reliability"))
tunnelQuantitySlider = slider(minimum : 1, maximum : 6, value : 4,
tunnelQuantitySlider = slider(minimum : 1, maximum : 6, value : defaults.tunnelQuantity,
majorTickSpacing : 1, snapToTicks : true, paintTicks: true, labelTable : quantityTable,
paintLabels : true)
}

View File

@ -0,0 +1,41 @@
package com.muwire.gui.wizard
class WizardDefaults {
String downloadLocation
String incompleteLocation
String i2cpHost
int i2cpPort
int i2npTcpPort
int i2npUdpPort
int inBw, outBw
int tunnelLength, tunnelQuantity
WizardDefaults() {
this(new Properties())
}
WizardDefaults(Properties props) {
downloadLocation = props.getProperty("downloadLocation", getDefaultPath("MuWire Downloads"))
incompleteLocation = props.getProperty("incompleteLocation", getDefaultPath("MuWire Incompletes"))
i2cpHost = props.getProperty("i2cpHost","127.0.0.1")
i2cpPort = Integer.parseInt(props.getProperty("i2cpPort","7654"))
Random r = new Random()
int randomPort = 9151 + r.nextInt(1 + 30777 - 9151) // this range matches what the i2p router would choose
i2npTcpPort = Integer.parseInt(props.getProperty("i2npTcpPort", String.valueOf(randomPort)))
i2npUdpPort = Integer.parseInt(props.getProperty("i2npUdpPort", String.valueOf(randomPort)))
inBw = Integer.parseInt(props.getProperty("inBw","512"))
outBw = Integer.parseInt(props.getProperty("outBw","256"))
tunnelLength = Integer.parseInt(props.getProperty("tunnelLength","3"))
tunnelQuantity = Integer.parseInt(props.getProperty("tunnelQuantity","4"))
}
private static String getDefaultPath(String pathName) {
File f = new File(System.getProperty("user.home"), pathName)
f.getAbsolutePath()
}
}

View File

@ -5,9 +5,11 @@ import com.muwire.core.MuWireSettings
abstract class WizardStep {
final String constraint
final WizardDefaults defaults
protected WizardStep(String constraint) {
protected WizardStep(String constraint, WizardDefaults defaults) {
this.constraint = constraint
this.defaults = defaults
}