All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.cloudfarming.client.geoviewer.jxmap.layerlist.LayerSettingsPanel Maven / Gradle / Ivy

Go to download

AgroSense geoviewer JXMap implementation. Contains a map/geoviewer TopComponent based on the JXMap classes from swingx.

The newest version!
/**
 * Copyright (C) 2008-2012 AgroSense Foundation.
 *
 * AgroSense is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
 * this software, see the FLOSS License Exception
 * .
 *
 * AgroSense is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with AgroSense.  If not, see .
 */
package nl.cloudfarming.client.geoviewer.jxmap.layerlist;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import net.miginfocom.swing.MigLayout;
import nl.cloudfarming.client.geoviewer.Layer;
import nl.cloudfarming.client.geoviewer.LayerInfo;
import org.openide.util.NbBundle;

/**
 * Panel for showing (and editing) layer settings, like visibility and
 * transparency.
 *
 * @author Timon Veenstra
 */
@NbBundle.Messages({"LayerSettingsPanel Title label text=Title:",
    "LayerSettingsPanel Transparancy label text=Visibility:",
    "LayerSettingsPanel Visibility label text=Visible:",
    "LayerSettingsPanel icons displayed label text=Display icons",
    "LayerSettingsPanel Settings panel border title=Layer"})
public class LayerSettingsPanel extends JPanel {

    public static final String TITLE_LABEL_FONT_NAME = "Dialog";
    public static final int TITLE_LABEL_FONT_SIZE = 12;
    public static final int TITLE_LABEL_FONT_STYLE = 0;
    private final LayerInfo layerInfo;
    private Layer layer;
    private boolean isVisibleInit;
    private boolean initDialogFirstTime = true;
    private int transparancyInit;
    private JCheckBox visibilityCheckbox;
    private JLabel titleTextLabel;
    private JPanel settingsPanel;
    private JSlider transparancySlider;
    private JCheckBox iconsDisplayedCheckbox;
    static final String VISIBILITY_CHECKBOX_NAME = "visibilityCheckbox";
    static final String ICONS_DISPLAYED_CHECKBOX_NAME = "iconsDisplayedCheckbox";

    /**
     * Creates new form LayerSettingsPanel
     */
    public LayerSettingsPanel(Layer layer, LayerInfo layerInfo) {
        this.layer = layer;
        this.layerInfo = layerInfo;
        initComponents();
        initSettings();
    }

    /**
     * Initialize the settings in het panel based on the layer info.
     */
    private void initSettings() {
        isVisibleInit = layerInfo.isVisible();
        transparancyInit = recalcTrans(layerInfo.getTransparency());

        String displayName = layer.getName();

        titleTextLabel.setText(displayName);

        visibilityCheckbox.setSelected(isVisibleInit);
        transparancySlider.setValue(transparancyInit);
        iconsDisplayedCheckbox.setSelected(layerInfo.showIcons());
    }

    /**
     * Cancel the changes, so go back to the settings while the panel opened.
     */
    public void cancelSettings() {
        if (isVisibleInit) {
            layerInfo.setVisible(true);
        } else {
            layerInfo.setVisible(false);
        }
        layerInfo.setTransparency(recalcTrans(transparancyInit));
    }

    /**
     * recalculate transparency
     *
     * @param trans int value for transparency 0-100 where 100 is full
     * transparent
     * @return float transparency from 0.0 to 1.0 where 1.0 is full transparent
     */
    public static float recalcTrans(int trans) {
        return (float) ((float) trans / 100.0);
    }

    /**
     * recalculate transparency
     *
     * @param trans float transparency from 0.0 to 1.0 where 1.0 is full
     * transparent
     * @return trans int value for transparency 0-100 where 100 is full
     * transparent
     */
    public static int recalcTrans(float trans) {
        return (int) (trans * 100.0f);
    }

    /**
     * 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.
     */
    private void initComponents() {
        JLabel titleLabel;
        JLabel transparancyLabel;
        JLabel visibilityLabel;
        JLabel iconVisibleLabel;

        settingsPanel = new JPanel();
        titleLabel = new JLabel();
        titleTextLabel = new JLabel();
        visibilityLabel = new JLabel();
        transparancyLabel = new JLabel();
        transparancySlider = new JSlider();
        visibilityCheckbox = new JCheckBox();
        visibilityCheckbox.setName(VISIBILITY_CHECKBOX_NAME);
        iconVisibleLabel = new JLabel();
        iconsDisplayedCheckbox = new JCheckBox();
        iconsDisplayedCheckbox.setName(ICONS_DISPLAYED_CHECKBOX_NAME);

        settingsPanel.setBorder(BorderFactory.createTitledBorder(Bundle.LayerSettingsPanel_Settings_panel_border_title()));

        titleLabel.setText(Bundle.LayerSettingsPanel_Title_label_text());
        visibilityLabel.setText(Bundle.LayerSettingsPanel_Visibility_label_text());
        transparancyLabel.setText(Bundle.LayerSettingsPanel_Transparancy_label_text());
        iconVisibleLabel.setText(Bundle.LayerSettingsPanel_icons_displayed_label_text());

        titleTextLabel.setFont(new java.awt.Font(TITLE_LABEL_FONT_NAME, TITLE_LABEL_FONT_STYLE, TITLE_LABEL_FONT_SIZE));

        transparancySlider.addChangeListener(new TransparancyStateChangedStateListener());
        ItemStateChangeListener stateListener = new ItemStateChangeListener();
        visibilityCheckbox.addItemListener(stateListener);
        iconsDisplayedCheckbox.addItemListener(stateListener);

        MigLayout layout = new MigLayout("wrap 2", "[]100[]");
        settingsPanel.setLayout(layout);
        settingsPanel.add(titleLabel);
        settingsPanel.add(titleTextLabel);
        settingsPanel.add(visibilityLabel);
        settingsPanel.add(visibilityCheckbox);
        settingsPanel.add(transparancyLabel);
        settingsPanel.add(transparancySlider);
        settingsPanel.add(iconVisibleLabel);
        settingsPanel.add(iconsDisplayedCheckbox);
        this.add(settingsPanel);
        
    }

    private class TransparancyStateChangedStateListener implements ChangeListener {

        @Override
        public void stateChanged(ChangeEvent evt) {
            JSlider source = (JSlider) evt.getSource();
            layerInfo.setTransparency(recalcTrans(source.getValue()));
        }
    }

    private class ItemStateChangeListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent evt) {
            if (initDialogFirstTime) {
                initDialogFirstTime = false;
            } else {
                if (evt.getSource().equals(visibilityCheckbox)) {
                    layerInfo.setVisible(visibilityCheckbox.isSelected());
                } else if (evt.getSource().equals(iconsDisplayedCheckbox)) {
                    layerInfo.setShowIcons(iconsDisplayedCheckbox.isSelected());
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy