Package de.mpi.rgblab.gui.menu

Source Code of de.mpi.rgblab.gui.menu.MenuControl$RecentSwatchListener

package de.mpi.rgblab.gui.menu;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.TextField;
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 java.awt.event.MouseMotionListener;
import java.io.Serializable;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;

import de.mpi.rgblab.apps.GuiApp;
import de.mpi.rgblab.data.ColorReader;
import de.mpi.rgblab.gui.map.GuiMap;
import de.mpi.rgblab.gui.palette.RecentSwatchPanel;
import de.mpi.rgblab.utils.VisUtil;

public class MenuControl extends JPanel implements MouseListener,
    MouseMotionListener, ActionListener {

  String inputColorsFile;
  String vis_salPaletteFile;
  String dataFile;

  CanvasSV csv;
  CanvasHS chs = new CanvasHS();
  CanvasB cb = new CanvasB();

  private final JFileChooser fc = new JFileChooser(System
      .getProperty("user.dir"));
  private Button import_std_files_btn = new Button("Import Standard Files");
  private Button open_data__btn = new Button("Import Data File");
  private Button open_input_color_btn = new Button("Import Label Colors");
  private Button open_vis_sal_palette_btn = new Button(
      "Import Visibility/Saliency Palette");
  private Button show_data_btn = new Button("Show Data");
  private JPanel fileChooserPanel = new JPanel();

  private JPanel buttonPanel = new JPanel();
  private JPanel sliderPanel = new JPanel();
  private JPanel thresholdPanel = new JPanel();
  private JPanel radiusPanel = new JPanel();
  private JPanel levelPanel = new JPanel();
  private JPanel parameterPanel = new JPanel();
  private JPanel resolutionPanel = new JPanel();
  private JPanel displayDistancePanel = new JPanel();

  JSlider thresholdSlider = new JSlider();
  JSlider radiusSlider = new JSlider();
  JSlider levelSlider = new JSlider();
  JSlider resolutionSlider = new JSlider();
  JSlider displayDistanceSlider = new JSlider();

  TextField tf_r = new TextField("127", 2);
  TextField tf_g = new TextField("127", 2);
  TextField tf_b = new TextField("127", 2);
  TextField tf_hc = new TextField("808080", 6);

  Button export_data_btn = new Button("Export Data");
  Button optimize_btn = new Button("Optimize");
  Button compute_sal_but = new Button("Compute Saliency");
  Button comp_vis_btn = new Button("Compute Visibility");
  Button identify_bad_visibility_btn = new Button("Identify Bad Visibility"); // identify
  // bad
  // visible
  // regions
  // Label label_threshold = new Label("Threshold:");
  TextField threshold = new TextField("15", 2);
  // Label label_radius = new Label("Radius:");
  TextField radius = new TextField("3", 2);
  TextField resolution = new TextField("100", 2);
  TextField level = new TextField("-1", 2);
  TextField displayDistance = new TextField("13", 2);

  RecentSwatchPanel recentSwatchPanel;

  Canvas cc = new Canvas();

  // Color[] colors;
  Vector<Color> v_clr = new Vector<Color>();
  Color cur_color = new Color(255, 255, 255);

  GuiMap map;
  boolean saliencyComputed = false;
  boolean visibilityComputed = false;

  public MenuControl(GuiMap map) { // , String sal_vis_palette, String
    // inputColors) { // , GuiMap map) {

    // inputColorsFile = inputColors;
    // vis_salPaletteFile = sal_vis_palette;
    this.map = map;
    csv = new CanvasSV(); // (map.getSaliencyColors());
    map.setSalVisPalette(csv.getColors());
   
    // v_clr = ColorReader.getColorsAsColor(inputColorsFile);
    recentSwatchPanel = new RecentSwatchPanel(7);
    // recentSwatchPanel = new RecentSwatchPanel(1, 4);
    // updateSwatchPanel();

    // colors = new Color[columns];

    // for (int i = 0; i < v_clr.size(); i++)
    // colors[i] = v_clr.get(i);

    // for (int i = 0; i < columns; i++) {
    // colors[i] = Color.WHITE;

    setLayout(new BorderLayout());
    setBackground(Color.white);

    buttonPanel.setLayout(new BoxLayout(buttonPanel,
        javax.swing.BoxLayout.Y_AXIS));

    fileChooserPanel.setLayout(new BoxLayout(fileChooserPanel,
        javax.swing.BoxLayout.Y_AXIS));

    fileChooserPanel.add(this.import_std_files_btn);
    fileChooserPanel.add(Box.createRigidArea(new Dimension(15, 15)));
    fileChooserPanel.add(this.open_data__btn);
    fileChooserPanel.add(Box.createRigidArea(new Dimension(15, 15)));
    fileChooserPanel.add(this.open_input_color_btn);
    fileChooserPanel.add(Box.createRigidArea(new Dimension(15, 15)));
    fileChooserPanel.add(this.open_vis_sal_palette_btn);
    fileChooserPanel.add(Box.createRigidArea(new Dimension(15, 15)));
    fileChooserPanel.add(this.show_data_btn);
    fileChooserPanel.add(Box.createRigidArea(new Dimension(15, 15)));

    this.import_std_files_btn.addActionListener(this);
    this.open_data__btn.addActionListener(this);
    this.open_input_color_btn.addActionListener(this);
    this.show_data_btn.addActionListener(this);
    this.open_vis_sal_palette_btn.addActionListener(this);

    Panel p_canvas = new Panel(new FlowLayout(FlowLayout.CENTER, 20, 20));
    Panel p_field = new Panel(new FlowLayout(FlowLayout.CENTER, 20, 20));

    chs.addMouseMotionListener(this);
    chs.addMouseListener(this);
    cb.addMouseMotionListener(this);
    cb.addMouseListener(this);

    // add(chs);
    // p_canvas.add(recentSwatchPanel);
    p_canvas.add(fileChooserPanel);
    p_canvas.add(chs);
    p_canvas.add(cb);

    cc.resize(50, 50);
    cc.setBackground(Color.gray);

    comp_vis_btn.addActionListener(this);
    compute_sal_but.addActionListener(this);
    identify_bad_visibility_btn.addActionListener(this);
    export_data_btn.addActionListener(this);
    this.optimize_btn.addActionListener(this);

    buttonPanel.add(compute_sal_but);
    buttonPanel.add(Box.createRigidArea(new Dimension(15, 15)));
    buttonPanel.add(comp_vis_btn);
    buttonPanel.add(Box.createRigidArea(new Dimension(15, 15)));
    buttonPanel.add(identify_bad_visibility_btn);
    buttonPanel.add(Box.createRigidArea(new Dimension(15, 15)));
    buttonPanel.add(export_data_btn);
    buttonPanel.add(Box.createRigidArea(new Dimension(15, 15)));
    buttonPanel.add(optimize_btn);

    p_canvas.add(cc);
    // p_canvas.add(submit_btn);

    // RecentSwatchPanel recentSwatch = new RecentSwatchPanel();

    RecentSwatchListener recentSwatchListener = new RecentSwatchListener();
    recentSwatchPanel.addMouseListener(recentSwatchListener);

    // add(recentSwatchPanel, BorderLayout.EAST);

    // p_field.add(new Label("Red:"));
    // p_field.add(tf_r);
    // p_field.add(new Label("Green:"));
    // p_field.add(tf_g);
    // p_field.add(new Label("Blue:"));
    // p_field.add(tf_b);
    // p_field.add(new Label("Hex Code:"));
    // p_field.add(tf_hc);
    // p_field.add(cc);
    // p_field.add(submit_btn);

    sliderPanel.setLayout(new javax.swing.BoxLayout(sliderPanel,
        javax.swing.BoxLayout.Y_AXIS));

    // parameterPanel.setLayout(new javax.swing.BoxLayout(parameterPanel,
    // javax.swing.BoxLayout.Y_AXIS));

    parameterPanel.add(thresholdPanel);
    parameterPanel.add(radiusPanel);
    parameterPanel.add(levelPanel);
    parameterPanel.add(resolutionPanel);
    parameterPanel.add(displayDistancePanel);

    // thresholdPanel.add(label_threshold)
    thresholdPanel.add(thresholdSlider);
    thresholdPanel.add(threshold);

    // radiusPanel.add(label_radius);
    radiusPanel.add(radiusSlider);
    radiusPanel.add(radius);

    levelPanel.add(levelSlider);
    levelPanel.add(level);

    resolutionPanel.add(resolutionSlider);
    resolutionPanel.add(resolution);
   
    displayDistancePanel.add(displayDistanceSlider);
    displayDistancePanel.add(displayDistance);

    thresholdSlider.setMaximum(200);
    thresholdSlider.setBorder(new javax.swing.border.TitledBorder(
        new javax.swing.border.EtchedBorder(null, new java.awt.Color(
            134, 134, 134)), "Threshold"));
    thresholdSlider
        .addChangeListener(new javax.swing.event.ChangeListener() {
          public void stateChanged(javax.swing.event.ChangeEvent evt) {
            thresholdSliderStateChanged(evt);
          }
        });

    thresholdSlider.getAccessibleContext().setAccessibleName(
        "Threshold Slider");
    thresholdSlider.getAccessibleContext().setAccessibleDescription(
        "Threshold slider.");

    radiusSlider.setMaximum(10);
    radiusSlider.setMinimum(2);
    radiusSlider.setBorder(new javax.swing.border.TitledBorder(
        new javax.swing.border.EtchedBorder(null, new java.awt.Color(
            134, 134, 134)), "Surround Angle"));
    radiusSlider.addChangeListener(new javax.swing.event.ChangeListener() {
      public void stateChanged(javax.swing.event.ChangeEvent evt) {
        radiusSliderStateChanged(evt);
      }
    });
    // sliderPanel.add(radiusSlider);
    radiusSlider.getAccessibleContext().setAccessibleName("Radius Slider");
    radiusSlider.getAccessibleContext().setAccessibleDescription(
        "Radius slider.");

    levelSlider.setMinimum(-1);
    levelSlider.setMaximum(7);
    levelSlider.setBorder(new javax.swing.border.TitledBorder(
        new javax.swing.border.EtchedBorder(null, new java.awt.Color(
            134, 134, 134)), "Level"));
    levelSlider.addChangeListener(new javax.swing.event.ChangeListener() {
      public void stateChanged(javax.swing.event.ChangeEvent evt) {
        levelSliderStateChanged(evt);
      }
    });

    levelSlider.getAccessibleContext().setAccessibleName("Sector Slider");
    levelSlider.getAccessibleContext().setAccessibleDescription(
        "Level Slider.");

    resolutionSlider.setMinimum(0);
    resolutionSlider.setMaximum(100);
    resolutionSlider.setBorder(new javax.swing.border.TitledBorder(
        new javax.swing.border.EtchedBorder(null, new java.awt.Color(
            134, 134, 134)), "Resolution"));
    resolutionSlider
        .addChangeListener(new javax.swing.event.ChangeListener() {
          public void stateChanged(javax.swing.event.ChangeEvent evt) {
            resolutionSliderStateChanged(evt);
          }
        });

    resolutionSlider.getAccessibleContext().setAccessibleName(
        "Resolution Slider");
    resolutionSlider.getAccessibleContext().setAccessibleDescription(
        "Resolution Slider.");

    displayDistanceSlider.setMinimum(5);
    displayDistanceSlider.setMaximum(25);
    displayDistanceSlider.setBorder(new javax.swing.border.TitledBorder(
        new javax.swing.border.EtchedBorder(null, new java.awt.Color(
            134, 134, 134)), "Distance to the Display in Inches"));
    displayDistanceSlider
        .addChangeListener(new javax.swing.event.ChangeListener() {
          public void stateChanged(javax.swing.event.ChangeEvent evt) {
            displayDistanceSliderStateChanged(evt);
          }
        });

    displayDistanceSlider.getAccessibleContext().setAccessibleName(
        "Distance to the Dispaly Slider");
    displayDistanceSlider.getAccessibleContext().setAccessibleDescription(
        "Distance to the Dispaly Slider");
   
    sliderPanel.add(csv);
    sliderPanel.add(thresholdPanel);
    sliderPanel.add(radiusPanel);
    sliderPanel.add(levelPanel);
    sliderPanel.add(resolutionPanel);
    sliderPanel.add(displayDistancePanel);

    p_canvas.add(recentSwatchPanel);
    p_canvas.add(buttonPanel);

    // p_canvas.add(import_colors_btn);
    // p_field.add(label_threshold, "North");
    // p_field.add(threshold, "North");
    // p_field.add(submit_btn, "South");
    p_canvas.add(sliderPanel);
    // p_canvas.add(parameterPanel);
    // p_canvas.add(label_threshold);
    // p_canvas.add(threshold);
    //
    // p_canvas.add(label_radius);
    // p_canvas.add(radius);
    // p_canvas.add(p_field);
    // p_canvas.add(csv);
    add(p_canvas, "South");

    // add(sliderPanel);
    // p_canvas.add(submit_btn);
    // p_canvas.add(comp_vis_btn);
    // p_canvas.add(identify_bad_visibility_btn);

    // add(p_field, "Southeast");
    // add(p_field, "Center");

    levelSlider.setValue(new Integer(level.getText()).intValue());
    thresholdSlider.setValue(new Integer(threshold.getText()).intValue());
    radiusSlider.setValue(new Integer(radius.getText()).intValue());
    resolutionSlider.setValue(new Integer(resolution.getText()).intValue());
    displayDistanceSlider.setValue(new Integer(displayDistance.getText()).intValue());
  }

  public void mouseClicked(MouseEvent e) {
    processEvent(e);
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent e) {
  }

  public void mousePressed(MouseEvent e) {
  }

  public void mouseDragged(MouseEvent e) {
    processEvent(e);
  }

  public void mouseMoved(MouseEvent e) {
  }

  private void processEvent(MouseEvent e) {

    if (e.getComponent() instanceof CanvasHS) {
      cb.refresh(chs.getColorAt(e.getX(), e.getY()));
      cb.repaint();
      cur_color = cb.getColor();
      tf_r.setText(Integer.toString(cur_color.getRed()));
      tf_g.setText(Integer.toString(cur_color.getGreen()));
      tf_b.setText(Integer.toString(cur_color.getBlue()));

      tf_hc.setText(Integer.toHexString(cur_color.getRGB() & 0xFFFFFF)
          .toUpperCase());
      cc.setBackground(cur_color);

    }

    if (e.getComponent() instanceof CanvasB) {
      cur_color = cb.getColorAt(e.getX(), e.getY());
      tf_r.setText(Integer.toString(cur_color.getRed()));
      tf_g.setText(Integer.toString(cur_color.getGreen()));
      tf_b.setText(Integer.toString(cur_color.getBlue()));
      tf_hc.setText(Integer.toHexString(cur_color.getRGB() & 0xFFFFFF)
          .toUpperCase());
      cb.setCursor(e.getY());
      cb.repaint();
      cc.setBackground(cur_color);
    }

  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.import_std_files_btn) {
      this.dataFile = GuiApp.dataFile;
      this.vis_salPaletteFile = GuiApp.vis_sal_Palette;
      this.inputColorsFile = GuiApp.inputColors;

      map.importData(dataFile);
      importLabelColors();

      Vector clr = ColorReader.getColorsAsColor(vis_salPaletteFile);
      csv.setNewColors(clr);
      map.setSalVisPalette(clr);

      // map.update(-1, levelSlider.getValue());
      map.update(-1);
    } else if (e.getSource() == this.open_data__btn) {
      int returnVal = fc.showOpenDialog(MenuControl.this);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        this.dataFile = fc.getSelectedFile().toString();
        map.importData(dataFile);
        map.update(-3);
      }
    } else if (e.getSource() == this.open_input_color_btn) {
      int returnVal = fc.showOpenDialog(MenuControl.this);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        inputColorsFile = fc.getSelectedFile().toString();
        importLabelColors();
        map.update(-3);
      }
    } else if (e.getSource() == this.open_vis_sal_palette_btn) {
      int returnVal = fc.showOpenDialog(MenuControl.this);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        vis_salPaletteFile = fc.getSelectedFile().toString();
        Vector clr = ColorReader.getColorsAsColor(vis_salPaletteFile);
        csv.setNewColors(clr);
        map.setSalVisPalette(clr);
        // This is where a real application would open the file.
        // log.append("Opening: " + file.getName() + "." + newline);
      }
    } else if (e.getSource() == this.show_data_btn) {
      // to do
      int ret = map.checkInputDataComplience();
      if ( 0 == ret)
        map.update(-1);
      else
        JOptionPane.showMessageDialog(this,
            "You need a file with at least " + ret + " colors!");
       

    } else if (e.getSource() == this.export_data_btn) {
      map.update(-2);
    } else if (e.getSource() == this.optimize_btn) {
      int distance = Integer.valueOf(displayDistance.getText()).intValue();
      int salAngle = Integer.valueOf(radius.getText()).intValue();
      map.optimize(distance, salAngle);
    } else {
      String s = threshold.getText();
      map.setThreshold(Integer.valueOf(s).intValue());
      String viewAngle = radius.getText();

      int distance = Integer.valueOf(displayDistance.getText()).intValue();
      int salAngle = Integer.valueOf(radius.getText()).intValue();
     
      if (false == saliencyComputed) {
        visibilityComputed = false;
        map.computeSaliency(distance, salAngle);
        saliencyComputed = true;
      }
      if (e.getSource() == this.compute_sal_but) {
        map.update(0);
      } else if (e.getSource() == this.identify_bad_visibility_btn) {
        map.update(1);
      } else if (e.getSource() == this.comp_vis_btn) {
        if (false == visibilityComputed) {
          visibilityComputed = true;
          map.computeVisibility(distance);
        }
        map.update(2);
        // to do
      }
    }
  }

  public Color[] getColors() {
    return (Color[]) v_clr.toArray();
  }

  public void paint(Graphics g) {

  }

  private void importLabelColors() {
    v_clr = ColorReader.getColorsAsColor(inputColorsFile);
    map.setColors(v_clr);
    recentSwatchPanel.setElementNumber(v_clr.size());
    for (int i = 0; i < v_clr.size(); i++)
      recentSwatchPanel.setColorForCell(i, v_clr.get(i));
    recentSwatchPanel.updateUI();
  }

  class RecentSwatchListener extends MouseAdapter implements Serializable {
    public void mousePressed(MouseEvent e) {
      recentSwatchPanel
          .setColorForLocation(e.getX(), e.getY(), cur_color);
      int column = recentSwatchPanel.getColumn();
      int row = recentSwatchPanel.getRow();
      if (v_clr.get(row).getRGB() != cur_color.getRGB()) {
        v_clr.set(row, cur_color);
        map.setColors(v_clr);
        map.updateUI();
      }
      saliencyComputed = false;
      // System.out.println(row);
      // recentSwatchPanel.getColorForLocation(e.getX(), e.getY());
      // getColorSelectionModel().setSelectedColor(color);

      // map.updateUI();

    }
  }

  /**
   * This method is called when threshold slider position is changed. It sets
   * the current threshold value.
   *
   * @param evt
   *            ChangeEvent instance passed from stateChanged event.
   */
  private void thresholdSliderStateChanged(javax.swing.event.ChangeEvent evt) {// GEN-FIRST:event_redSliderStateChanged
    // threshold = thresholdSlider.getValue();
    threshold.setText(Integer.toString(thresholdSlider.getValue()));
  }// GEN-LAST:event_redSliderStateChanged

  /**
   * This method is called when radius slider position is changed. It sets the
   * current radius value.
   *
   * @param evt
   *            ChangeEvent instance passed from stateChanged event.
   */
  private void radiusSliderStateChanged(javax.swing.event.ChangeEvent evt) {// GEN-FIRST:event_redSliderStateChanged
    // threshold = thresholdSlider.getValue();
    radius.setText(Integer.toString(radiusSlider.getValue()));
    saliencyComputed = false;
    // radiusSlider.setToolTipText(Integer.toString(radiusSlider.getValue()));
  }// GEN-LAST:event_redSliderStateChanged

  private void levelSliderStateChanged(javax.swing.event.ChangeEvent evt) {// GEN-FIRST:event_redSliderStateChanged
    // threshold = thresholdSlider.getValue();
    level.setText(Integer.toString(levelSlider.getValue()));
    map.updateSector(levelSlider.getValue());
    // saliencyComputed = false;
    // radiusSlider.setToolTipText(Integer.toString(radiusSlider.getValue()));
  }// GEN-LAST:event_redSliderStateChanged

  private void resolutionSliderStateChanged(javax.swing.event.ChangeEvent evt) {// GEN-FIRST:event_redSliderStateChanged
    // threshold = thresholdSlider.getValue();
    resolution.setText(Integer.toString(resolutionSlider.getValue()));
    // map.resizeMap(resolutionSlider.getValue());
    map.setPercent(resolutionSlider.getValue());
    // saliencyComputed = false;
    // radiusSlider.setToolTipText(Integer.toString(radiusSlider.getValue()));
  }// GEN-LAST:event_redSliderStateChanged

  private void  displayDistanceSliderStateChanged(javax.swing.event.ChangeEvent evt) {// GEN-FIRST:event_redSliderStateChanged
    // threshold = thresholdSlider.getValue();
    displayDistance.setText(Integer.toString(displayDistanceSlider.getValue()));
    //map.updateDisplayDistance(displayDistanceSlider.getValue());
    // saliencyComputed = false;
    // radiusSlider.setToolTipText(Integer.toString(radiusSlider.getValue()));
  }// GEN-LAST:event_redSliderStateChanged
}
TOP

Related Classes of de.mpi.rgblab.gui.menu.MenuControl$RecentSwatchListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.