package org.markdownwriterfx.options;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.SortedMap;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import org.markdownwriterfx.Messages;
import org.markdownwriterfx.util.Item;
import org.tbee.javafx.scene.layout.fxml.MigPane;
public class GeneralOptionsPane
extends MigPane
{
@SuppressWarnings("unchecked")
public GeneralOptionsPane() {
initComponents();
String defaultLineSeparator = System.getProperty( "line.separator", "\n" );
String defaultLineSeparatorStr = defaultLineSeparator.replace("\r", "CR").replace("\n", "LF");
lineSeparatorField.getItems().addAll(
new Item<String>( Messages.get("GeneralOptionsPane.platformDefault", defaultLineSeparatorStr), null ),
new Item<String>( Messages.get("GeneralOptionsPane.sepWindows"), "\r\n" ),
new Item<String>( Messages.get("GeneralOptionsPane.sepUnix"), "\n" ));
encodingField.getItems().addAll(getAvailableEncodings());
}
private Collection<Item<String>> getAvailableEncodings() {
SortedMap<String, Charset> availableCharsets = Charset.availableCharsets();
ArrayList<Item<String>> values = new ArrayList<>(1 + availableCharsets.size());
values.add(new Item<String>(Messages.get("GeneralOptionsPane.platformDefault", Charset.defaultCharset().name()), null));
for (String name : availableCharsets.keySet())
values.add(new Item<String>(name, name));
return values;
}
void load() {
lineSeparatorField.setValue(new Item<String>(Options.getLineSeparator(), Options.getLineSeparator()));
encodingField.setValue(new Item<String>(Options.getEncoding(), Options.getEncoding()));
showWhitespaceCheckBox.setSelected(Options.isShowWhitespace());
}
void save() {
Options.setLineSeparator(lineSeparatorField.getValue().value);
Options.setEncoding(encodingField.getValue().value);
Options.setShowWhitespace(showWhitespaceCheckBox.isSelected());
}
private void initComponents() {
Label lineSeparatorLabel = new Label();
lineSeparatorField = new ComboBox<>();
Label lineSeparatorLabel2 = new Label();
Label encodingLabel = new Label();
encodingField = new ComboBox<>();
showWhitespaceCheckBox = new CheckBox();
setCols("[fill][fill][fill]");
setRows("[][]para[]");
lineSeparatorLabel.setText(Messages.get("GeneralOptionsPane.lineSeparatorLabel.text"));
lineSeparatorLabel.setMnemonicParsing(true);
add(lineSeparatorLabel, "cell 0 0");
add(lineSeparatorField, "cell 1 0");
lineSeparatorLabel2.setText(Messages.get("GeneralOptionsPane.lineSeparatorLabel2.text"));
add(lineSeparatorLabel2, "cell 2 0");
encodingLabel.setText(Messages.get("GeneralOptionsPane.encodingLabel.text"));
encodingLabel.setMnemonicParsing(true);
add(encodingLabel, "cell 0 1");
encodingField.setVisibleRowCount(20);
add(encodingField, "cell 1 1");
showWhitespaceCheckBox.setText(Messages.get("GeneralOptionsPane.showWhitespaceCheckBox.text"));
add(showWhitespaceCheckBox, "cell 0 2 3 1,growx 0,alignx left");
lineSeparatorLabel.setLabelFor(lineSeparatorField);
encodingLabel.setLabelFor(encodingField);
}
private ComboBox<Item<String>> lineSeparatorField;
private ComboBox<Item<String>> encodingField;
private CheckBox showWhitespaceCheckBox;
}