se.trixon.toolbox.photokml.config.ModuleDescriptionPanel.java Source code

Java tutorial

Introduction

Here is the source code for se.trixon.toolbox.photokml.config.ModuleDescriptionPanel.java

Source

/*
 * Copyright 2015 Patrik Karlsson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package se.trixon.toolbox.photokml.config;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.apache.commons.lang3.StringUtils;
import org.openide.util.NbBundle;
import se.trixon.almond.dictionary.Dict;

/**
 *
 * @author Patrik Karlsson <patrik@trixon.se>
 */
public class ModuleDescriptionPanel extends ModulePanel {

    /**
     * Creates new form ModuleDescriptionPanel
     */
    public ModuleDescriptionPanel() {
        initComponents();
        mTitle = Dict.DESCRIPTION.getString();
        init();
    }

    @Override
    public StringBuilder getHeaderBuilder() {
        StringBuilder sb = new StringBuilder();

        sb.append(Dict.DESCRIPTION.getString().toUpperCase()).append("\n");
        optAppend(sb, mOptions.isDescriptionPhoto(), Dict.PHOTO.getString());
        optAppend(sb, mOptions.isDescriptionDate(), Dict.DATE.getString());
        optAppend(sb, mOptions.isDescriptionCoordinate(), Dict.COORDINATE.getString());
        optAppend(sb, mOptions.isDescriptionAltitude(), Dict.ALTITUDE.getString());
        optAppend(sb, mOptions.isDescriptionBearing(), Dict.BEARING.getString());
        optAppend(sb, mOptions.isDescriptionCustom(), Dict.CUSTOM_TEXT.getString());

        if (mOptions.isDescriptionExternalFile()
                && !StringUtils.isEmpty(mOptions.getDescriptionExternalFileValue())) {
            optAppend(sb, true,
                    NbBundle.getMessage(this.getClass(), "ModuleDescriptionPanel.externalFileCheckBox.text"));
            sb.deleteCharAt(sb.length() - 1);
            sb.append(": ");
            sb.append(mOptions.getDescriptionCustomText()).append("\n");
        }

        if (mOptions.isDescriptionCustom() && !StringUtils.isEmpty(mOptions.getDescriptionCustomText())) {
            sb.append(MULTILINE_DIVIDER).append("\n");
            sb.append(mOptions.getDescriptionCustomText()).append("\n");
            sb.append(MULTILINE_DIVIDER);
        }

        sb.append("\n");

        return sb;
    }

    @Override
    public boolean hasValidSettings() {
        return true;
    }

    @Override
    public void restoreEnabledStates() {
        customTextArea.setEnabled(customCheckBox.isSelected());
        externalFileTextField.setEnabled(externalFileCheckBox.isSelected());
    }

    private void init() {
        customTextArea.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void changedUpdate(DocumentEvent e) {
                saveOption();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                saveOption();
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                saveOption();
            }

            private void saveOption() {
                mOptions.setDescriptionCustomText(customTextArea.getText());
            }
        });

        externalFileTextField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void changedUpdate(DocumentEvent e) {
                saveOption();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                saveOption();
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                saveOption();
            }

            private void saveOption() {
                mOptions.setDescriptionExternalFileValue(externalFileTextField.getText());
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        bearingCheckBox = new javax.swing.JCheckBox();
        coordinateCheckBox = new javax.swing.JCheckBox();
        dateCheckBox = new javax.swing.JCheckBox();
        photoCheckBox = new javax.swing.JCheckBox();
        filenameCheckBox = new javax.swing.JCheckBox();
        altitudeCheckBox = new javax.swing.JCheckBox();
        externalFileCheckBox = new javax.swing.JCheckBox();
        externalFileTextField = new javax.swing.JTextField();
        customCheckBox = new javax.swing.JCheckBox();
        customScrollPane = new javax.swing.JScrollPane();
        customTextArea = new javax.swing.JTextArea();

        setLayout(new java.awt.GridBagLayout());

        org.openide.awt.Mnemonics.setLocalizedText(bearingCheckBox, Dict.BEARING.getString());
        bearingCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bearingCheckBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 0);
        add(bearingCheckBox, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(coordinateCheckBox, Dict.COORDINATE.getString());
        coordinateCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                coordinateCheckBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 3, 0);
        add(coordinateCheckBox, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(dateCheckBox, Dict.DATE.getString());
        dateCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dateCheckBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 0);
        add(dateCheckBox, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(photoCheckBox, Dict.PHOTO.getString());
        photoCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                photoCheckBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 3, 0);
        add(photoCheckBox, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(filenameCheckBox, Dict.FILENAME.getString());
        filenameCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filenameCheckBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 0);
        add(filenameCheckBox, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(altitudeCheckBox, Dict.ALTITUDE.getString());
        altitudeCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                altitudeCheckBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 0);
        add(altitudeCheckBox, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(externalFileCheckBox, org.openide.util.NbBundle
                .getMessage(ModuleDescriptionPanel.class, "ModuleDescriptionPanel.externalFileCheckBox.text")); // NOI18N
        externalFileCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                externalFileCheckBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        add(externalFileCheckBox, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(externalFileTextField, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(customCheckBox, Dict.CUSTOM_TEXT.getString());
        customCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                customCheckBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 0);
        add(customCheckBox, gridBagConstraints);

        customTextArea.setColumns(20);
        customTextArea.setRows(5);
        customScrollPane.setViewportView(customTextArea);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(customScrollPane, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void customCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_customCheckBoxActionPerformed
        mOptions.setDescriptionCustom(customCheckBox.isSelected());
        customTextArea.setEnabled(customCheckBox.isSelected());
    }//GEN-LAST:event_customCheckBoxActionPerformed

    private void bearingCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bearingCheckBoxActionPerformed
        mOptions.setDescriptionBearing(bearingCheckBox.isSelected());
    }//GEN-LAST:event_bearingCheckBoxActionPerformed

    private void coordinateCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_coordinateCheckBoxActionPerformed
        mOptions.setDescriptionCoordinate(coordinateCheckBox.isSelected());
    }//GEN-LAST:event_coordinateCheckBoxActionPerformed

    private void dateCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dateCheckBoxActionPerformed
        mOptions.setDescriptionDate(dateCheckBox.isSelected());
    }//GEN-LAST:event_dateCheckBoxActionPerformed

    private void photoCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_photoCheckBoxActionPerformed
        mOptions.setDescriptionPhoto(photoCheckBox.isSelected());
    }//GEN-LAST:event_photoCheckBoxActionPerformed

    private void altitudeCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_altitudeCheckBoxActionPerformed
        mOptions.setDescriptionAltitude(altitudeCheckBox.isSelected());
    }//GEN-LAST:event_altitudeCheckBoxActionPerformed

    private void filenameCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filenameCheckBoxActionPerformed
        mOptions.setDescriptionFilename(filenameCheckBox.isSelected());
    }//GEN-LAST:event_filenameCheckBoxActionPerformed

    private void externalFileCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_externalFileCheckBoxActionPerformed
        mOptions.setDescriptionExternalFile(externalFileCheckBox.isSelected());
        externalFileTextField.setEnabled(externalFileCheckBox.isSelected());
    }//GEN-LAST:event_externalFileCheckBoxActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox altitudeCheckBox;
    private javax.swing.JCheckBox bearingCheckBox;
    private javax.swing.JCheckBox coordinateCheckBox;
    private javax.swing.JCheckBox customCheckBox;
    private javax.swing.JScrollPane customScrollPane;
    private javax.swing.JTextArea customTextArea;
    private javax.swing.JCheckBox dateCheckBox;
    private javax.swing.JCheckBox externalFileCheckBox;
    private javax.swing.JTextField externalFileTextField;
    private javax.swing.JCheckBox filenameCheckBox;
    private javax.swing.JCheckBox photoCheckBox;
    // End of variables declaration//GEN-END:variables

    @Override
    public void load() {
        altitudeCheckBox.setSelected(mOptions.isDescriptionAltitude());
        bearingCheckBox.setSelected(mOptions.isDescriptionBearing());
        coordinateCheckBox.setSelected(mOptions.isDescriptionCoordinate());
        customCheckBox.setSelected(mOptions.isDescriptionCustom());
        dateCheckBox.setSelected(mOptions.isDescriptionDate());
        photoCheckBox.setSelected(mOptions.isDescriptionPhoto());
        filenameCheckBox.setSelected(mOptions.isDescriptionFilename());
        externalFileCheckBox.setSelected(mOptions.isDescriptionExternalFile());
        externalFileTextField.setText(mOptions.getDescriptionExternalFileValue());
        customTextArea.setText(mOptions.getDescriptionCustomText());

        restoreEnabledStates();
    }

    @Override
    public void save() {
    }
}