| Author | Karl Tauber <email> |
|---|---|
| Date | 2015-08-06 16:09:48 GMT+0200 |
| Commit | adc48e791f35f6a555cd9c0a4b4265147ebf01cc |
| Parent | 62eda01 |
| Delta | 148 lines added, 2 lines removed, 146-line increase |
| +/* | ||
| + * Copyright (c) 2015 Karl Tauber <karl at jformdesigner dot com> | ||
| + * All rights reserved. | ||
| + * | ||
| + * Redistribution and use in source and binary forms, with or without | ||
| + * modification, are permitted provided that the following conditions are met: | ||
| + * | ||
| + * o Redistributions of source code must retain the above copyright | ||
| + * notice, this list of conditions and the following disclaimer. | ||
| + * | ||
| + * o Redistributions in binary form must reproduce the above copyright | ||
| + * notice, this list of conditions and the following disclaimer in the | ||
| + * documentation and/or other materials provided with the distribution. | ||
| + * | ||
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| + */ | ||
| + | ||
| +package org.markdownwriterfx.util; | ||
| + | ||
| +/** | ||
| + * Simple item for a ChoiceBox, ComboBox or ListView. | ||
| + * Consists of a string name and a value object. | ||
| + * toString() returns the name. | ||
| + * equals() compares the value and hashCode() returns the hash code of the value. | ||
| + * | ||
| + * @author Karl Tauber | ||
| + */ | ||
| +public class Item<V> | ||
| +{ | ||
| + public final String name; | ||
| + public final V value; | ||
| + | ||
| + public Item(String name, V value) { | ||
| + this.name = name; | ||
| + this.value = value; | ||
| + } | ||
| + | ||
| + @Override | ||
| + public boolean equals(Object obj) { | ||
| + if (this == obj) | ||
| + return true; | ||
| + if (!(obj instanceof Item)) | ||
| + return false; | ||
| + return Utils.safeEquals(value, ((Item<?>)obj).value); | ||
| + } | ||
| + | ||
| + @Override | ||
| + public int hashCode() { | ||
| + return (value != null) ? value.hashCode() : 0; | ||
| + } | ||
| + | ||
| + @Override | ||
| + public String toString() { | ||
| + return name; | ||
| + } | ||
| +} | ||
| public class Utils | ||
| { | ||
| + public static boolean safeEquals(Object o1, Object o2) { | ||
| + if (o1 == o2) | ||
| + return true; | ||
| + if (o1 == null || o2 == null) | ||
| + return false; | ||
| + return o1.equals(o2); | ||
| + } | ||
| + | ||
| public static boolean isNullOrEmpty(String s) { | ||
| return s == null || s.isEmpty(); | ||
| i--; | ||
| return s.substring(0, i + 1); | ||
| + } | ||
| + | ||
| + public static void putPrefs(Preferences prefs, String key, String value, String def) { | ||
| + if (value != def && !value.equals(def)) | ||
| + prefs.put(key, value); | ||
| + else | ||
| + prefs.remove(key); | ||
| + } | ||
| + | ||
| + public static void putPrefsInt(Preferences prefs, String key, int value, int def) { | ||
| + if (value != def) | ||
| + prefs.putInt(key, value); | ||
| + else | ||
| + prefs.remove(key); | ||
| + } | ||
| + | ||
| + public static void putPrefsBoolean(Preferences prefs, String key, boolean value, boolean def) { | ||
| + if (value != def) | ||
| + prefs.putBoolean(key, value); | ||
| + else | ||
| + prefs.remove(key); | ||
| } | ||
| import javafx.beans.property.IntegerProperty; | ||
| import javafx.beans.property.SimpleIntegerProperty; | ||
| +import javafx.beans.property.SimpleStringProperty; | ||
| +import javafx.beans.property.StringProperty; | ||
| +import org.markdownwriterfx.util.Utils; | ||
| import org.pegdown.Extensions; | ||
| Options.options = options; | ||
| + setLineSeparator(options.get("lineSeparator", null)); | ||
| + setEncoding(options.get("encoding", null)); | ||
| setMarkdownExtensions(options.getInt("markdownExtensions", Extensions.ALL)); | ||
| } | ||
| public static void save() { | ||
| - options.putInt("markdownExtensions", getMarkdownExtensions()); | ||
| + Utils.putPrefs(options, "lineSeparator", getLineSeparator(), null); | ||
| + Utils.putPrefs(options, "encoding", getEncoding(), null); | ||
| + Utils.putPrefsInt(options, "markdownExtensions", getMarkdownExtensions(), Extensions.ALL); | ||
| } | ||
| + | ||
| + // 'lineSeparator' property | ||
| + private static final StringProperty lineSeparator = new SimpleStringProperty(); | ||
| + public static String getLineSeparator() { return lineSeparator.get(); } | ||
| + public static void setLineSeparator(String lineSeparator) { Options.lineSeparator.set(lineSeparator); } | ||
| + public static StringProperty lineSeparatorProperty() { return lineSeparator; } | ||
| + | ||
| + // 'encoding' property | ||
| + private static final StringProperty encoding = new SimpleStringProperty(); | ||
| + public static String getEncoding() { return encoding.get(); } | ||
| + public static void setEncoding(String encoding) { Options.encoding.set(encoding); } | ||
| + public static StringProperty encodingProperty() { return encoding; } | ||
| // 'markdownExtensions' property | ||
| import javafx.scene.control.TabPane; | ||
| import javafx.stage.Window; | ||
| +import org.markdownwriterfx.MarkdownWriterFXApp; | ||
| /** | ||
| dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL); | ||
| + // save options on OK clicked | ||
| dialogPane.lookupButton(ButtonType.OK).addEventHandler(ActionEvent.ACTION, e -> { | ||
| save(); | ||
| e.consume(); | ||
| }); | ||
| + // load options | ||
| load(); | ||
| + | ||
| + // select last tab | ||
| + int tabIndex = MarkdownWriterFXApp.getState().getInt("lastOptionsTab", -1); | ||
| + if (tabIndex > 0) | ||
| + tabPane.getSelectionModel().select(tabIndex); | ||
| + | ||
| + // remember last selected tab | ||
| + setOnHidden(e -> { | ||
| + MarkdownWriterFXApp.getState().putInt("lastOptionsTab", tabPane.getSelectionModel().getSelectedIndex()); | ||
| + }); | ||
| } | ||
| private void load() { | ||
| + generalOptionsPane.load(); | ||
| markdownOptionsPane.load(); | ||
| } | ||
| private void save() { | ||
| + generalOptionsPane.save(); | ||
| markdownOptionsPane.save(); | ||
| Options.save(); | ||
| } | ||
| private void initComponents() { | ||
| // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents | ||
| tabPane = new TabPane(); | ||
| + generalTab = new Tab(); | ||
| + generalOptionsPane = new GeneralOptionsPane(); | ||
| markdownTab = new Tab(); | ||
| markdownOptionsPane = new MarkdownOptionsPane(); | ||
| //======== tabPane ======== | ||
| { | ||
| tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE); | ||
| + | ||
| + //======== generalTab ======== | ||
| + { | ||
| + generalTab.setText("General"); | ||
| + generalTab.setContent(generalOptionsPane); | ||
| + } | ||
| //======== markdownTab ======== | ||
| { | ||
| markdownTab.setText("Markdown"); | ||
| markdownTab.setContent(markdownOptionsPane); | ||
| } | ||
| - tabPane.getTabs().addAll(markdownTab); | ||
| + tabPane.getTabs().addAll(generalTab, markdownTab); | ||
| } | ||
| // JFormDesigner - End of component initialization //GEN-END:initComponents | ||
| } | ||
| // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables | ||
| private TabPane tabPane; | ||
| + private Tab generalTab; | ||
| + private GeneralOptionsPane generalOptionsPane; | ||
| private Tab markdownTab; | ||
| private MarkdownOptionsPane markdownOptionsPane; | ||
| "tabClosingPolicy": enum javafx.scene.control.TabPane$TabClosingPolicy UNAVAILABLE | ||
| add( new FormContainer( "javafx.scene.control.Tab", new FormLayoutManager( class javafx.scene.control.Tab ) ) { | ||
| + name: "generalTab" | ||
| + "text": "General" | ||
| + add( new FormComponent( "org.markdownwriterfx.options.GeneralOptionsPane" ) { | ||
| + name: "generalOptionsPane" | ||
| + } ) | ||
| + } ) | ||
| + add( new FormContainer( "javafx.scene.control.Tab", new FormLayoutManager( class javafx.scene.control.Tab ) ) { | ||
| name: "markdownTab" | ||
| "text": "Markdown" |