Commit 06678872 authored by Radek Ošlejšek's avatar Radek Ošlejšek
Browse files

Merge branch '133-fix-gui-changes' into 'master'

Resolve "Fix GUI changes"

Closes #133

See merge request grp-fidentis/analyst2!141
parents 02943644 3fb8e0c9
Loading
Loading
Loading
Loading
+87 −6
Original line number Diff line number Diff line
package cz.fidentis.analyst.core;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * ComboSlider implements a combination of horizontal slider and input text field.
@@ -16,26 +22,61 @@ public abstract class ComboSlider extends JPanel {
 
    private final JSlider slider = new JSlider();
    private final JFormattedTextField inputField = new JFormattedTextField();
    private boolean continousSync;

    public static final int DEFAULT_TEXT_FIELD_WIDTH = 70;
    public static final int DEFAULT_TEXT_FIELD_HEIGHT = 40;
    
    private ChangeListener changeListener = new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            setValueFromSlider();
        }
    };
    
    private MouseListener mouseListener = new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
            setValueFromSlider();
        }
    };
    
    /**
     * Constructor.
     */
    public ComboSlider() {
        initComponents();
        
        setContinousSync(true); // update input field on slider's change and inform external listeners
        
        inputField.addActionListener((ActionEvent ae) -> { // update slider when the input field changed
            setValueFromInputField();
        });
        
    }

    /**
     * Adds the specified action listener to receive action events from this GUI item.
     * Connects the specified listener witch the input field.
     * The listener is invoked on the input field change, which is affected
     * by the {@link #setContinousSync(boolean)}.
     *
     * @param listener the action listener to be added
     */
    public synchronized void addActionListener(ActionListener listener) {
    public synchronized void addInputFieldListener(ActionListener listener) {
        inputField.addActionListener(listener);
    }
    
    /**
     * Connects the specified listener witch the slider.
     *
     * @param listener the action listener to be added
     */
    public synchronized void addSliderListener(ActionListener listener) {
        slider.addChangeListener((ChangeEvent e) -> {
            listener.actionPerformed(new ActionEvent(slider, ActionEvent.ACTION_PERFORMED, null));
        });
    }
    
    /**
     * The slider is on the left, followed by ti input field, by default.
     * This method switches the order.
@@ -45,17 +86,57 @@ public abstract class ComboSlider extends JPanel {
        add(slider);
    }
    
    protected JSlider getSlider() {
    /**
     * If {@code true}, then the input field is updated during the slider move.
     * Otherwise, the input field is updated only on mouse key release.
     * 
     * @param continousSync Whether to update input field continuously.
     */
    public final void setContinousSync(boolean continousSync) {
        this.continousSync = continousSync;
        if (this.continousSync) {
            slider.removeMouseListener(mouseListener);
            slider.addChangeListener(changeListener);
        } else {
            slider.addMouseListener(mouseListener);
            slider.removeChangeListener(changeListener);
        }
    }

    /**
     * If {@code true}, then the input field is updated during the slider move.
     * Otherwise, the input field is updated only on mouse key release.
     * 
     * @return {@code true} if the slider and input field are synchronized continuously. 
     */
    public boolean isContinousSync() {
        return continousSync;
    }
    
    public JSlider getSlider() {
        return slider;
    }
    
    protected JFormattedTextField getInputField() {
    public JFormattedTextField getInputField() {
        return inputField;
    }
    
    /**
     * Reads the value of the slider, scales it into the range of the input field, 
     * updates the input field and triggers input field change event.
     */
    protected abstract void setValueFromSlider();
    
    /**
     * Reads the value of the input field, scales it into the range of the slider, 
     * and updates the slider. No change event is triggered.
     */
    protected abstract void setValueFromInputField();
    
    protected final void initComponents() {
        inputField.setPreferredSize(new Dimension(DEFAULT_TEXT_FIELD_WIDTH, DEFAULT_TEXT_FIELD_HEIGHT));
        add(slider);
        add(inputField);
        inputField.setPreferredSize(new Dimension(DEFAULT_TEXT_FIELD_WIDTH, DEFAULT_TEXT_FIELD_HEIGHT));
    }
    
}
+15 −16
Original line number Diff line number Diff line
package cz.fidentis.analyst.core;

import java.awt.event.ActionEvent;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.event.ChangeEvent;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

@@ -27,7 +25,6 @@ public class ComboSliderDouble extends ComboSlider {
     */
    public ComboSliderDouble() {
        setRange(0.0, 1.0, 2);
        connect();
    }
    
    /**
@@ -56,8 +53,8 @@ public class ComboSliderDouble extends ComboSlider {
        getInputField().setFormatterFactory(new DefaultFormatterFactory(formatter));
        getInputField().setText(TextFieldUtils.doubleToStringLocale(this.min, fractionDigits));
        
        getSlider().setMinimum((int) (this.min * scale()));
        getSlider().setMaximum((int) (this.max * scale()));
        getSlider().setMinimum((int) (this.min * getRecomputationFactor()));
        getSlider().setMaximum((int) (this.max * getRecomputationFactor()));
        
        setValue(this.min);
    }
@@ -111,19 +108,21 @@ public class ComboSliderDouble extends ComboSlider {
        return this.fractionDigits;
    }
    
    protected final void connect() {
        // update input field on slider's change and inform external listeners
        getSlider().addChangeListener((ChangeEvent ce) -> { 
            setValue(getSlider().getValue() / scale()); // also triggers the event
        });
    @Override
    protected void setValueFromSlider() {
        setValue(getSlider().getValue() / getRecomputationFactor()); // also triggers the event
    }
    
        // update slider when the input field changed
        getInputField().addActionListener((ActionEvent ae) -> { 
            getSlider().setValue((int) (TextFieldUtils.parseLocaleDouble(getInputField()) * scale()));
        });
    @Override
    protected void setValueFromInputField() {
        getSlider().setValue((int) (TextFieldUtils.parseLocaleDouble(getInputField()) * getRecomputationFactor()));
    }
    
    protected final double scale() {
    /**
     * Returns 1, 10, 100, etc., based on the fraction digits.
     * @return 1, 10, 100, etc., based on the fraction digits.
     */
    public double getRecomputationFactor() {
        int scale = 1;
        for (int i = 0; i < fractionDigits; i++) {
            scale *= 10;
+9 −13
Original line number Diff line number Diff line
package cz.fidentis.analyst.core;

import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import javax.swing.event.ChangeEvent;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

@@ -21,7 +19,6 @@ public class ComboSliderInteger extends ComboSlider {
     */
    public ComboSliderInteger() {
        setRange(0, 100);
        connect();
    }
    
    /**
@@ -90,15 +87,14 @@ public class ComboSliderInteger extends ComboSlider {
        return this.max;
    }
    
    private void connect() {
        // update input field on slider's change and inform external listeners
        getSlider().addChangeListener((ChangeEvent ce) -> { 
    @Override
    protected void setValueFromSlider() {
        setValue(getSlider().getValue()); // also triggers the event
        });
    }
    
        // update slider when the input field changed
        getInputField().addActionListener((ActionEvent ae) -> { 
    @Override
    protected void setValueFromInputField() {
        getSlider().setValue(TextFieldUtils.parseLocaleInt(getInputField()));
        });
    }
    
}
+2 −3
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@ import cz.fidentis.analyst.Logger;
import cz.fidentis.analyst.canvas.Canvas;
import cz.fidentis.analyst.core.LoadedActionEvent;
import cz.fidentis.analyst.core.ControlPanelAction;
import cz.fidentis.analyst.core.ControlPanelBuilder;
import cz.fidentis.analyst.face.events.HausdorffDistanceComputed;
import cz.fidentis.analyst.face.events.HumanFaceEvent;
import cz.fidentis.analyst.face.events.HumanFaceListener;
@@ -22,7 +21,6 @@ import java.util.Map;
import java.util.stream.Collectors;
import javax.swing.JComboBox;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JToggleButton;

/**
@@ -140,6 +138,7 @@ public class DistanceAction extends ControlPanelAction implements HumanFaceListe
                break;
            case FeaturePointsPanel.ACTION_COMMAND_FEATURE_POINT_SELECT:
                selectFeaturePoint((LoadedActionEvent) ae);
                setVisibleSpheres();
                highlightSelectedSpheres();
                computeAndUpdateHausdorffDistance();
                break;
@@ -353,7 +352,7 @@ public class DistanceAction extends ControlPanelAction implements HumanFaceListe
     */
    private void resizeFeaturePoint(LoadedActionEvent actionEvent) {
        final int index = (int) actionEvent.getData();
        final double size = ControlPanelBuilder.parseLocaleDouble((JTextField) actionEvent.getSource());
        final double size = this.controlPanel.getFeaturePointsListPanel().getSphereSize(index);
        
        fpSpheres.setSize(index, size);
        featurePointTypes.replace(getTypeOfFeaturePoint(index), size);
+31 −12
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@
                  </Group>
                  <Component id="jPanel1" alignment="0" max="32767" attributes="0"/>
                  <Component id="jScrollPane1" alignment="0" pref="0" max="32767" attributes="0"/>
                  <Component id="jPanel2" alignment="0" max="32767" attributes="0"/>
                  <Component id="jPanel2" alignment="0" min="-2" max="-2" attributes="0"/>
              </Group>
              <EmptySpace max="32767" attributes="0"/>
          </Group>
@@ -72,22 +72,25 @@
          <Group type="103" groupAlignment="0" attributes="0">
              <Group type="102" alignment="0" attributes="0">
                  <EmptySpace max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="0" attributes="0">
                      <Group type="102" attributes="0">
                          <Component id="jComboBox1" min="-2" pref="188" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="1" attributes="0">
                      <Component id="jLabel5" min="-2" max="-2" attributes="0"/>
                      <Component id="jLabel4" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="0" attributes="0">
                      <Component id="jComboBox2" max="32767" attributes="0"/>
                          <EmptySpace max="-2" attributes="0"/>
                          <Component id="jComboBox3" min="-2" max="-2" attributes="0"/>
                          <EmptySpace min="-2" pref="52" max="-2" attributes="0"/>
                      <Component id="jComboBox1" max="32767" attributes="0"/>
                  </Group>
                  <EmptySpace max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="0" attributes="0">
                      <Group type="102" attributes="0">
                          <Component id="jLabel3" min="-2" max="-2" attributes="0"/>
                          <EmptySpace max="-2" attributes="0"/>
                          <Component id="jComboBox4" min="-2" max="-2" attributes="0"/>
                          <EmptySpace max="32767" attributes="0"/>
                      </Group>
                      <Component id="jComboBox3" alignment="0" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace min="-2" pref="102" max="-2" attributes="0"/>
              </Group>
          </Group>
        </DimensionLayout>
@@ -96,14 +99,16 @@
              <Group type="102" alignment="0" attributes="0">
                  <EmptySpace max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="3" attributes="0">
                      <Component id="jComboBox1" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="jComboBox2" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="jComboBox3" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="jLabel5" alignment="3" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace min="-2" pref="15" max="-2" attributes="0"/>
                  <Group type="103" groupAlignment="3" attributes="0">
                      <Component id="jLabel3" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="jComboBox4" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="jLabel4" alignment="3" min="-2" max="-2" attributes="0"/>
                      <Component id="jComboBox1" alignment="3" min="-2" max="-2" attributes="0"/>
                  </Group>
                  <EmptySpace max="32767" attributes="0"/>
              </Group>
@@ -172,6 +177,20 @@
            </Property>
          </Properties>
        </Component>
        <Component class="javax.swing.JLabel" name="jLabel4">
          <Properties>
            <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
              <ResourceString bundle="cz/fidentis/analyst/distance/Bundle.properties" key="DistancePanel.jLabel4.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
            </Property>
          </Properties>
        </Component>
        <Component class="javax.swing.JLabel" name="jLabel5">
          <Properties>
            <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
              <ResourceString bundle="cz/fidentis/analyst/distance/Bundle.properties" key="DistancePanel.jLabel5.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
            </Property>
          </Properties>
        </Component>
      </SubComponents>
    </Container>
    <Container class="javax.swing.JScrollPane" name="jScrollPane1">
@@ -229,7 +248,7 @@
                  <Component id="jLabel2" min="-2" max="-2" attributes="0"/>
                  <EmptySpace max="-2" attributes="0"/>
                  <Component id="jTextField2" min="-2" pref="60" max="-2" attributes="0"/>
                  <EmptySpace pref="35" max="32767" attributes="0"/>
                  <EmptySpace max="32767" attributes="0"/>
              </Group>
          </Group>
        </DimensionLayout>
Loading