Commit e8f07b5c authored by Axel Howind's avatar Axel Howind

code cleanups

parent ccfe4690
Pipeline #56398654 failed with stages
in 17 minutes and 2 seconds
......@@ -181,12 +181,10 @@ public abstract class FxApplication<A extends FxApplication<A, C>, C extends FxC
stage.show();
// automatically update title on document change
final ChangeListener<Boolean> dirtyStateListener = (v, o, n) -> {
updateApplicationTitle();
};
final ChangeListener<URI> documentLocationListener = (v, o, n) -> {
updateApplicationTitle();
};
final ChangeListener<Boolean> dirtyStateListener = (v, o, n) -> updateApplicationTitle();
final ChangeListener<URI> documentLocationListener = (v, o, n) -> updateApplicationTitle();
controller.currentDocumentProperty.addListener(
(v, o, n) -> {
updateApplicationTitle();
......
......@@ -52,6 +52,8 @@ public abstract class FxController<A extends FxApplication<A, C>, C extends FxCo
/** Logger */
protected static final Logger LOG = Logger.getLogger(FxController.class.getSimpleName());
public static final String TITLE_ERROR = "Error";
public static final File USER_HOME = new File(System.getProperty("user.home"));
/** The application instance. */
private A app;
......@@ -207,7 +209,7 @@ public abstract class FxController<A extends FxApplication<A, C>, C extends FxCo
} catch (Exception e) {
LOG.log(Level.WARNING, "error creating document", e);
Dialogs.error()
.title("Error")
.title(TITLE_ERROR)
.header("Could not create a new document.")
.text(e.getMessage())
.build()
......@@ -233,7 +235,7 @@ public abstract class FxController<A extends FxApplication<A, C>, C extends FxCo
} else {
String lastDocument = getPreference(PREF_DOCUMENT, "");
if (lastDocument.isBlank()) {
parent = new File(System.getProperty("user.home")).toPath();
parent = USER_HOME.toPath();
} else {
Path path = Paths.get(URI.create(lastDocument));
parent = path.getParent();
......@@ -247,7 +249,7 @@ public abstract class FxController<A extends FxApplication<A, C>, C extends FxCo
File initialDir = parent != null ? parent.toFile() : null;
if (initialDir == null || !initialDir.isDirectory()) {
initialDir = new File(System.getProperty("user.home"));
initialDir = USER_HOME;
}
Optional<File> file = Dialogs
......@@ -271,7 +273,7 @@ public abstract class FxController<A extends FxApplication<A, C>, C extends FxCo
} catch (Exception e) {
LOG.log(Level.WARNING, "error opening document", e);
Dialogs.error()
.title("Error")
.title(TITLE_ERROR)
.header("'%s' could not be opened.", getDisplayName(uri))
.text(e.getMessage())
.build()
......@@ -336,7 +338,7 @@ public abstract class FxController<A extends FxApplication<A, C>, C extends FxCo
} else {
String lastDocument = getPreference(PREF_DOCUMENT, "");
if (lastDocument.isBlank()) {
parent = new File(System.getProperty("user.home")).toPath();
parent = USER_HOME.toPath();
} else {
Path path = Paths.get(URI.create(lastDocument));
parent = path.getParent();
......@@ -351,7 +353,7 @@ public abstract class FxController<A extends FxApplication<A, C>, C extends FxCo
File initialDir = parent!=null ? parent.toFile() : null;
if (initialDir == null || !initialDir.isDirectory()) {
initialDir = new File(System.getProperty("user.home"));
initialDir = USER_HOME;
}
Optional<File> file = Dialogs
......@@ -378,7 +380,7 @@ public abstract class FxController<A extends FxApplication<A, C>, C extends FxCo
} catch (Exception e) {
LOG.log(Level.WARNING, "error saving document", e);
Dialogs.error()
.title("Error")
.title(TITLE_ERROR)
.header("'%s' could not be saved.", getDisplayName(uri))
.text("%s: %s", e.getClass().getSimpleName(), e.getMessage())
.build()
......@@ -388,7 +390,7 @@ public abstract class FxController<A extends FxApplication<A, C>, C extends FxCo
}
protected String getDisplayName(URI uri) {
return getCurrentDocument().getLocation().toString();
return uri.toString();
}
@SuppressWarnings("static-method")
......
......@@ -18,11 +18,12 @@ public abstract class FxDocument {
public static final URI VOID_URI = URI.create("");
protected BooleanProperty dirtyProperty = new SimpleBooleanProperty(false);
protected ObjectProperty<URI> locationProperty = new SimpleObjectProperty<URI>(VOID_URI);
protected ObjectProperty<URI> locationProperty = new SimpleObjectProperty<>(VOID_URI);
protected FxDocument(URI location) {
this.locationProperty.set(Objects.requireNonNull(location));
}
public URI getLocation() {
return locationProperty.get();
}
......
......@@ -47,8 +47,8 @@ public class EditorSettingsDialog extends Dialog<ButtonType> {
private EditorSetting oldSetting;
// -- button types
public final ButtonType OK = ButtonType.OK;
public final ButtonType RESET = new ButtonType("RESET");
public static final ButtonType OK = ButtonType.OK;
public static final ButtonType RESET = new ButtonType("RESET");
// -- input controls
@FXML ComboBox<String> comboTheme;
......@@ -92,21 +92,15 @@ public class EditorSettingsDialog extends Dialog<ButtonType> {
String theme = editor.getTheme();
comboTheme.getItems().setAll("default", "xq-light", "xq-dark");
comboTheme.setValue(theme);
comboTheme.valueProperty().addListener( (ov,o,n) -> {
editor.setTheme(n);
});
comboTheme.valueProperty().addListener( (ov,o,n) -> editor.setTheme(n) );
// line numbers
toggleShowLineNumbers.setSelected(editor.isShowLineNumbers());
toggleShowLineNumbers.selectedProperty().addListener( (ov,o,n) -> {
editor.setShowLineNumbers(n);
});
toggleShowLineNumbers.selectedProperty().addListener( (ov,o,n) -> editor.setShowLineNumbers(n) );
// highlight current line
toggleHighlightCurrentLine.setSelected(editor.isHighlightCurrentLine());
toggleHighlightCurrentLine.selectedProperty().addListener( (ov,o,n) -> {
editor.setHighlightCurrentLine(n);
});
toggleHighlightCurrentLine.selectedProperty().addListener( (ov,o,n) -> editor.setHighlightCurrentLine(n) );
// font size
sliderFontSize.setMin(FONT_SIZE_MIN);
......
......@@ -32,6 +32,7 @@ import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
......@@ -244,6 +245,9 @@ public abstract class EditorBase extends BorderPane {
@Override
public String next() {
if (i>=getLineCount()) {
throw new NoSuchElementException();
}
return getLine(i++);
}
};
......
......@@ -150,12 +150,12 @@ public class JavaScriptBridge {
}
// bind properties
readOnlyProperty.addListener((v, ov, nv) -> {
Platform.runLater(() -> engine.executeScript("jSetReadOnly("+nv+");"));
});
promptTextProperty.addListener((v, ov, nv) -> {
Platform.runLater(() -> engine.executeScript("jSetPromptText(\""+escape(nv)+"\");"));
});
readOnlyProperty.addListener((v, ov, nv) ->
Platform.runLater(() -> engine.executeScript("jSetReadOnly("+nv+");"))
);
promptTextProperty.addListener((v, ov, nv) ->
Platform.runLater(() -> engine.executeScript("jSetPromptText(\""+escape(nv)+"\");"))
);
// sync properties
String script = String.format(
......
......@@ -10,7 +10,7 @@ import javafx.concurrent.Task;
public abstract class FxTask<V> extends Task<V>{
private static AtomicInteger TASK_COUNTER = new AtomicInteger(0);
private static final AtomicInteger TASK_COUNTER = new AtomicInteger(0);
final int taskId = TASK_COUNTER.incrementAndGet();
private StringProperty text = new SimpleStringProperty(this, "text", "");
......
......@@ -51,8 +51,8 @@ public class PlatformHelper {
try {
doneLatch.await();
} catch (InterruptedException e) {
// ignore exception
LOG.log(Level.FINE, "interrupted", e);
Thread.currentThread().interrupt();
}
}
......
......@@ -9,7 +9,7 @@ import javafx.util.StringConverter;
public class StringValueConverter extends StringConverter<Value<String>> {
private static StringValueConverter INSTANCE = new StringValueConverter();
private static final StringValueConverter INSTANCE = new StringValueConverter();
public static StringValueConverter instance() {
return INSTANCE;
......
......@@ -52,7 +52,7 @@ extends AbstractPaneBuilder<InputDialogPane<Void>, AlertPaneBuilder, Void> {
@Override
public InputDialogPane<Void> build() {
InputDialogPane<Void> inputPane = super.build();
applyIfNotNull((pane,text) -> pane.setContentText(text), inputPane, text);
applyIfNotNull((pane,t) -> pane.setContentText(t), inputPane, text);
inputPane.setValidate(getValidate());
return inputPane;
}
......
......@@ -47,18 +47,18 @@ public class FileInput extends HBox implements InputControl<File> {
initialDir=new File(".");
}
if (mode== InputBuilder.FileDialogMode.OPEN) {
if (this.mode==InputBuilder.FileDialogMode.OPEN) {
Dialogs.chooseFile()
.initialDir(initialDir)
.filter(filters)
.filter(this.filters)
.showOpenDialog(null)
.ifPresent(f -> value.setValue(f));
.ifPresent(value::setValue);
} else {
Dialogs.chooseFile()
.initialDir(initialDir)
.filter(filters)
.filter(this.filters)
.showSaveDialog(null)
.ifPresent(f -> value.setValue(f));
.ifPresent(value::setValue);
}
});
......
......@@ -134,16 +134,14 @@ public interface InputControl<R> {
static SimpleInputControl<TextField, String> stringInput(Supplier<String> dflt, Function<String, Optional<String>> validate) {
TextField control = new TextField();
StringProperty value = control.textProperty();
SimpleInputControl<TextField, String> inputControl = new SimpleInputControl<>(control, value, dflt, validate);
return inputControl;
return new SimpleInputControl<>(control, value, dflt, validate);
}
static <T> SimpleInputControl<TextField, T> stringInput(Supplier<T> dflt, Function<T, Optional<String>> validate, StringConverter<T> converter) {
TextField control = new TextField();
ObjectProperty<T> value = new SimpleObjectProperty<>();
Bindings.bindBidirectional(control.textProperty(), value, converter);
SimpleInputControl<TextField, T> inputControl = new SimpleInputControl<>(control, value, dflt, validate);
return inputControl;
return new SimpleInputControl<>(control, value, dflt, validate);
}
static SimpleInputControl<TextField, Integer> integerInput(Supplier<Integer> dflt, Function<Integer, Optional<String>> validate) {
......@@ -151,8 +149,7 @@ public interface InputControl<R> {
StringProperty textProperty = control.textProperty();
IntegerProperty value = new SimpleIntegerProperty();
textProperty.bindBidirectional(value, NumberFormat.getIntegerInstance(Locale.getDefault()));
SimpleInputControl<TextField,Integer> inputControl = new SimpleInputControl<>(control, value.asObject(), dflt, validate);
return inputControl;
return new SimpleInputControl<>(control, value.asObject(), dflt, validate);
}
static SimpleInputControl<TextField, Double> decimalInput(Supplier<Double> dflt, Function<Double, Optional<String>> validate) {
......@@ -160,25 +157,22 @@ public interface InputControl<R> {
StringProperty textProperty = control.textProperty();
DoubleProperty value = new SimpleDoubleProperty();
textProperty.bindBidirectional(value, NumberFormat.getInstance(Locale.getDefault()));
SimpleInputControl<TextField,Double> inputControl = new SimpleInputControl<>(control, value.asObject(), dflt, validate);
return inputControl;
return new SimpleInputControl<>(control, value.asObject(), dflt, validate);
}
static SimpleInputControl<CheckBox, Boolean> checkBoxInput(Supplier<Boolean> dflt, String text) {
CheckBox control = new CheckBox(text);
BooleanProperty value = control.selectedProperty();
SimpleInputControl<CheckBox,Boolean> inputControl = new SimpleInputControl<>(control, value.asObject(), dflt, r -> Optional.empty());
return inputControl;
return new SimpleInputControl<>(control, value.asObject(), dflt, r -> Optional.empty());
}
static <T> SimpleInputControl<ComboBox<T>, T> comboBoxInput(Collection<T> choices, Supplier<T> dflt) {
ComboBox<T> control = new ComboBox<>(FXCollections.observableArrayList(choices));
ObjectProperty<T> value = control.valueProperty();
SimpleInputControl<ComboBox<T>,T> inputControl = new SimpleInputControl<>(control, value, dflt, r -> Optional.empty());
return inputControl;
return new SimpleInputControl<>(control, value, dflt, r -> Optional.empty());
}
static InputControl<File> chooseFile(Supplier<File> dflt, Function<File, Optional<String>> validate, InputBuilder.FileDialogMode mode, FileChooser.ExtensionFilter... filters) {
static InputControl<File> chooseFile(Supplier<File> dflt, InputBuilder.FileDialogMode mode, FileChooser.ExtensionFilter... filters) {
return new FileInput(mode, dflt, filters);
}
}
\ No newline at end of file
......@@ -55,6 +55,7 @@ public abstract class InputDialogPane<R> extends DialogPane implements Supplier<
}
}
@Override
protected Node createButton(ButtonType buttonType) {
// a wizard dialog should only close when finish or cancel is clicked
if (LangUtil.isOneOf(buttonType, ButtonType.OK, ButtonType.FINISH, ButtonType.CANCEL)) {
......
......@@ -51,11 +51,8 @@ implements InputBuilder<InputGridBuilder> {
*/
@Override
public <T> InputGridBuilder add(String id, String label, Class<T> type, Supplier<T> dflt, InputControl<T> control) {
Objects.requireNonNull(id);
Meta<T> meta = new Meta<>(id, label, type, dflt, control);
Meta<?> prev = data.put(id, meta);
LangUtil.check(prev == null, "Input with id '" + id + "' already defined");
return this;
Objects.requireNonNull(label);
return doAdd(id, label, type, dflt, control);
}
/* (non-Javadoc)
......@@ -63,13 +60,17 @@ implements InputBuilder<InputGridBuilder> {
*/
@Override
public <T> InputGridBuilder add(String id, Class<T> type, Supplier<T> dflt, InputControl<T> control) {
Objects.requireNonNull(id);
Meta<T> meta = new Meta<>(id, null, type, dflt, control);
Meta<?> prev = data.put(id, meta);
LangUtil.check(prev == null, "Input with id '" + id + "' already defined");
return this;
return doAdd(id, null, type, dflt, control);
}
private <T> InputGridBuilder doAdd(String id, String label, Class<T> type, Supplier<T> dflt, InputControl<T> control) {
Objects.requireNonNull(id);
Meta<T> meta = new Meta<>(id, label, type, dflt, control);
Meta<?> prev = data.put(id, meta);
LangUtil.check(prev == null, "Input with id '" + id + "' already defined");
return this;
}
static class ControlWrapper implements InputControl<Void> {
private final Node node;
......
......@@ -134,11 +134,4 @@ implements InputBuilder<InputPaneBuilder> {
return this;
}
/* (non-Javadoc)
* @see com.dua3.fx.util.controls.InputBuilder#build()
*/
@Override
public InputPane build() {
return super.build();
}
}
......@@ -58,6 +58,7 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
this.state = new State<>(value, dflt);
}
@Override
public void init() {
getChildren().clear();
......@@ -93,16 +94,13 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
@SuppressWarnings("unchecked")
private <T> InputControl<Value<T>> createControl(OptionValues values, Option<T> option) {
InputControl<Value<T>> control;
if (option instanceof StringOption) {
InputControl<Value<String>> inputControl = InputControl.stringInput(
() -> (Value<String>) dflt.get().get(option),
r -> Optional.empty(),
StringValueConverter.instance());
inputControl.valueProperty().addListener( (v,o,n) -> {
values.put(option, n);
});
inputControl.valueProperty().addListener( (v,o,n) -> values.put(option, n) );
values.addChangeListener( (op,o,n) -> {
if (op.equals(option)) {
......@@ -117,13 +115,10 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
FileChooser.ExtensionFilter filters = new FileChooser.ExtensionFilter("supported Files", fop.getExtensions());
InputControl<File> fileInputControl = InputControl.chooseFile(
dfltValue,
file -> file == null ? Optional.of("no file selected") : Optional.empty(),
mode,
mode,
filters);
InputControl<Value<File>> inputControl = new ValueInputControl<>(fileInputControl);
inputControl.valueProperty().addListener( (v,o,n) -> {
values.put(option, n);
});
inputControl.valueProperty().addListener( (v,o,n) -> values.put(option, n));
values.addChangeListener( (op,o,n) -> {
if (op.equals(option)) {
......@@ -131,16 +126,12 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
}
});
return (InputControl) inputControl;
} else if (option instanceof Option.SimpleOption<?>) {
throw new UnsupportedOperationException();
} else if (option instanceof ChoiceOption<?>) {
Collection<Value<T>> choices = ((ChoiceOption<T>)option).getChoices();
Supplier<Value<T>> dfltValue = () -> (Value<T>) (dflt.get().get(option));
var inputControl = InputControl.comboBoxInput(choices, dfltValue);
inputControl.valueProperty().addListener( (v,o,n) -> {
values.put(option, Option.value(n));
});
inputControl.valueProperty().addListener( (v,o,n) -> values.put(option, Option.value(n)));
values.addChangeListener( (op,o,n) -> {
if (op.equals(option)) {
......@@ -148,6 +139,8 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
}
});
return inputControl;
} else if (option instanceof Option.SimpleOption<?>) {
throw new UnsupportedOperationException();
}
throw new UnsupportedOperationException("unsupported input type: "+option.getClass().getName());
......@@ -166,8 +159,8 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
for (var entry: items.entrySet()) {
Option<?> option = entry.getKey();
Property<?> property = entry.getValue().valueProperty();
Value<?> value = (Value<?>) property.getValue();
values.put(option, value);
Value<?> v = (Value<?>) property.getValue();
values.put(option, v);
}
return values;
}
......@@ -178,8 +171,8 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
for (var item: items.entrySet()) {
Option option = item.getKey();
Property property = item.getValue().valueProperty();
Value value = arg.get(option);
property.setValue(value.get());
Value v = arg.get(option);
property.setValue(v.get());
}
}
......@@ -190,8 +183,7 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
@Override
public void reset() {
// TODO Auto-generated method stub
items.forEach( (item,control) -> control.reset() );
}
@Override
......
......@@ -3,7 +3,6 @@ package com.dua3.fx.util.controls;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import java.util.function.Supplier;
import java.util.logging.Logger;
public class PromptPane extends InputDialogPane<String> {
......@@ -12,7 +11,6 @@ public class PromptPane extends InputDialogPane<String> {
protected static final Logger LOG = Logger.getLogger(PromptPane.class.getSimpleName());
private TextField text;
private Supplier<String> defaultValue = () -> "";
@Override
public String get() {
......
......@@ -61,9 +61,7 @@ public class RadioPane<T> extends VBox implements InputControl<T> {
this.state.setValidate( v -> v==null?Optional.of("Nothing selected.") : Optional.empty());
// update toggle, when state changes
state.valueProperty().addListener( (v,o,n) -> {
group.selectToggle(this.items.get(n));
});
state.valueProperty().addListener( (v,o,n) -> group.selectToggle(this.items.get(n)));
// set initial toggle
group.selectToggle(this.items.get(currentValue));
......
......@@ -18,7 +18,7 @@ class SimpleInputControl<C extends Control,R> implements InputControl<R> {
protected SimpleInputControl(C control, Property<R> value, Supplier<R> dflt, Function<R,Optional<String>> validate) {
this.control = Objects.requireNonNull(control);
this.state = new State<>(value);
this.state = new State<>(value, dflt, validate);
this.dflt = dflt;
reset();
......
......@@ -27,12 +27,8 @@ public class ValueInputControl<T> implements InputControl<Value<T>> {
this.value = new SimpleObjectProperty<>(current == null ? null : Option.value(current));
// bind value to wrapped value
value.addListener((v,o,n) -> {
wrapped.set(n.get());
});
wrapped.valueProperty().addListener((v,o,n) -> {
value.setValue((Option.value(n)));
});
value.addListener((v,o,n) -> wrapped.set(n.get()));
wrapped.valueProperty().addListener((v,o,n) -> value.setValue((Option.value(n))));
}
/**
......@@ -47,6 +43,7 @@ public class ValueInputControl<T> implements InputControl<Value<T>> {
/**
* Set/update control state.
*/
@Override
public void init() {
wrapped.init();
}
......
......@@ -86,9 +86,9 @@ public class WizardDialog extends Dialog<Map<String,Object>> {
}
boolean apply(ButtonType btn) {
R result = pane.get();
boolean done = resultHandler.handleResult(btn, result);
this.result = done ? result : null;
R r = pane.get();
boolean done = resultHandler.handleResult(btn, r);
this.result = done ? r : null;
return done;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment