Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git
/*
 * 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 com.scrivenvar.options;

import com.scrivenvar.Messages;
import com.scrivenvar.ui.AbstractPane;
import com.scrivenvar.util.Item;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.SortedMap;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;

/**
 * General options pane.
 *
 * @author Karl Tauber
 */
public class GeneralOptionsPane extends AbstractPane {

  @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<>( Messages.get( "GeneralOptionsPane.platformDefault", defaultLineSeparatorStr ), null ),
      new Item<>( Messages.get( "GeneralOptionsPane.sepWindows" ), "\r\n" ),
      new Item<>( 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<>( Messages.get( "GeneralOptionsPane.platformDefault", Charset.defaultCharset().name() ), null ) );

    for( String name : availableCharsets.keySet() ) {
      values.add( new Item<>( name, name ) );
    }

    return values;
  }

  void load() {
    lineSeparatorField.setValue( new Item<>( getOptions().getLineSeparator(), getOptions().getLineSeparator() ) );
    encodingField.setValue( new Item<>( getOptions().getEncoding(), getOptions().getEncoding() ) );

  }

  void save() {
    getOptions().setLineSeparator( lineSeparatorField.getValue().value );
    getOptions().setEncoding( encodingField.getValue().value );

  }

  private void initComponents() {
    // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
    Label lineSeparatorLabel = new Label();
    lineSeparatorField = new ComboBox<>();
    Label lineSeparatorLabel2 = new Label();
    Label encodingLabel = new Label();
    encodingField = new ComboBox<>();

    //======== this ========
    setCols( "[fill][fill][fill]" );
    setRows( "[][]para[]" );

    //---- lineSeparatorLabel ----
    lineSeparatorLabel.setText( Messages.get( "GeneralOptionsPane.lineSeparatorLabel.text" ) );
    lineSeparatorLabel.setMnemonicParsing( true );
    add( lineSeparatorLabel, "cell 0 0" );
    add( lineSeparatorField, "cell 1 0" );

    //---- lineSeparatorLabel2 ----
    lineSeparatorLabel2.setText( Messages.get( "GeneralOptionsPane.lineSeparatorLabel2.text" ) );
    add( lineSeparatorLabel2, "cell 2 0" );

    //---- encodingLabel ----
    encodingLabel.setText( Messages.get( "GeneralOptionsPane.encodingLabel.text" ) );
    encodingLabel.setMnemonicParsing( true );
    add( encodingLabel, "cell 0 1" );

    //---- encodingField ----
    encodingField.setVisibleRowCount( 20 );
    add( encodingField, "cell 1 1" );
		// JFormDesigner - End of component initialization  //GEN-END:initComponents

    // TODO set this in JFormDesigner as soon as it supports labelFor
    lineSeparatorLabel.setLabelFor( lineSeparatorField );
    encodingLabel.setLabelFor( encodingField );
  }

  // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
  private ComboBox<Item<String>> lineSeparatorField;
  private ComboBox<Item<String>> encodingField;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}