NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.

To learn about what's going on, see About the Archive Site.

Re: Constant Map Dialog

Hi Ugo,

Here's the source of a ConstantMappingDialog I did two years ago. This
was made from scratch so don't expect too much. It works with a helper
class called SciViSDisplay which is kind of wrapper for VisADDisplays.
Should be no problem to adept it for VisADDisplays.

Cheers, Mathias

> Hi,
> 
> has anyone out there coded a "ConstantMap Dialog"? Let me explain: My 
> app has some data, whose layout characteristics are defined by 
> ConstantMaps (colour, transparency, fixed position, etc.). I found a 
> very nice method in VisAD bag of goodies, the util package:
> 
> visad.util.Util.getColorMaps(myFancyColor);
> 
> returns a ConstantMap[] of colours.
> 
> Now, I want to extend that by providing a GUI with the following:
> 
> 1) A "Data Attributes" button, click to pop up a
> 
> 2) Const. map Dialog
> 
> This is simply a dialog with toggle buttons indicating the const map 
> (much like in the style of the visad.ss.MappingDialog) and some sort of 
> interface (combo box, spinner, text field, VisAD slider) to enter the 
> value of the respective map. Then, clicking on an "OK" button would 
> return a ConstantMap[].
> 
> So, back to the question: has anyone got that and is willing to share?
> 
> Thanks in advance,
> 
> Ugo
package de.scivis;

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import visad.*;
import visad.ss.MappingDialog;
import de.util.*;

/** ConstantMappingDialog stellt Möglichkeiten zur Manipulation von
  * ConstantMaps zur Verfügung. ConstantMaps weisen im Gegensatz zu
  * ScalarMaps einer Display-Variablen einen konstanten Wert zu. So kann
  * z.B. die Transparenz (Alpha) auf einen konstanten Wert gesetzt werden.
  * Andererseits können bei einer Farbdarstellung die Farbwerte auf 0 gesetzt 
werden.
  * Möchte man z.B. eine Darstellung der Gaskonzentration als blaues Gas, müssen
  * die Farbwerte von Rot und Grün mit Hilfe des ConstantMappingDialogs auf 0
  * gesetzt werden da deren Standardwerte immer 1 sind.
  */

public class ConstantMappingDialog extends JDialog implements ActionListener, 
MouseListener {
  private static Image mapsImage = null;
  private JPanel imageCanvas;
  private JPanel mainPanel = new JPanel();
  private JPanel mapFromPanel = new JPanel();
  private JPanel mapToPanel = new JPanel();
  private JPanel currentMapsPanel = new JPanel();
  private JPanel upperControlsPanel = new JPanel();
  private JPanel lowerControlsPanel = new JPanel();
  private JLabel mapFromLabel = new JLabel("Map from:");
  private JLabel mapValueLabel = new JLabel("Map value:");
  private JLabel mapToLabel = new JLabel("Map to:");
  private JLabel currentMapsLabel = new JLabel("Current maps:");
  private JTextField mapValueTextField = new JTextField(10);
  private JList mapFromList;
  private JList currentMapsList;
  private JScrollPane currentMapsScrollPane;
  private JButton clearAllButton = new JButton("Clear all");
  private JButton clearSelectedButton = new JButton("Clear selected");
  private JButton doneButton = new JButton("Done");
  private JButton cancelButton = new JButton("Cancel");
  private BoxLayout mainPanelBoxLayout = new BoxLayout(mainPanel, 
BoxLayout.X_AXIS);
  private BoxLayout mapFromBoxLayout = new BoxLayout(mapFromPanel, 
BoxLayout.Y_AXIS);
  private BoxLayout mapToBoxLayout = new BoxLayout(mapToPanel, 
BoxLayout.Y_AXIS);
  private BoxLayout currentMapsBoxLayout = new BoxLayout(currentMapsPanel, 
BoxLayout.Y_AXIS);
  private FlowLayout upperControlsPanelFlowLayout = new 
FlowLayout(FlowLayout.CENTER);
  private FlowLayout lowerControlsPanelFlowLayout = new 
FlowLayout(FlowLayout.CENTER);

  private boolean okPressed = false;
  private int oldMouseX = -1, oldMouseY = -1;
  private SciViSDisplay panel;
  private Vector cMaps = new Vector();

  /** Konstruiert einen ConstantMappingDialog. Als Parameter sind der
    * aufrufende Frame und die Start-ConstantMaps nötig.
    */
  ConstantMappingDialog(Frame parent, SciViSDisplay panel) {
    super(parent, "Set up constant mappings", true);
    this.panel = panel;
    cMaps.add(panel.getConstantMapVector());
    String[] dataNames = panel.getDataNames();
    if (dataNames != null) {
      for (int i = 0 ; i < dataNames.length ; i++) {
        ConstantMap[] cm = panel.getDisplayData(dataNames[i]).getConstantMaps();
        Vector cms = new Vector();
        if (cm != null)
          for (int j = 0 ; j < cm.length ; j++)
            cms.add(cm[j]);
        cMaps.add(cms);
      }
    }
    init();
    mapFromList.setSelectedIndex(0);
  }

  // Initialisiert den Dialog
  private void init() {
    cancelButton.setActionCommand("cancel");
    cancelButton.addActionListener(this);
    doneButton.setActionCommand("done");
    doneButton.addActionListener(this);
    clearSelectedButton.setActionCommand("clearSelected");
    clearSelectedButton.addActionListener(this);
    clearAllButton.setActionCommand("clearAll");
    clearAllButton.addActionListener(this);
    currentMapsList = new JList();
    currentMapsScrollPane = new JScrollPane(currentMapsList);
    currentMapsPanel.setLayout(currentMapsBoxLayout);
    currentMapsPanel.add(currentMapsLabel);
    currentMapsPanel.add(currentMapsScrollPane);
    lowerControlsPanel.setLayout(lowerControlsPanelFlowLayout);
    lowerControlsPanel.add(doneButton);
    lowerControlsPanel.add(cancelButton);
    upperControlsPanel.setLayout(upperControlsPanelFlowLayout);
    upperControlsPanel.add(clearAllButton);
    upperControlsPanel.add(clearSelectedButton);
    mapToPanel.setLayout(mapToBoxLayout);
    mapToPanel.add(mapToLabel);
    if (mapsImage == null) {
      MediaTracker tracker = new MediaTracker(this);
      URL url = MappingDialog.class.getResource("display.gif");
      mapsImage = Toolkit.getDefaultToolkit().getImage(url);
      tracker.addImage(mapsImage, 0);
      try {
        tracker.waitForID(0);
      } catch (InterruptedException exc) {
        exc.printStackTrace();
      }
    }
    imageCanvas = new JPanel() {
      public void paint(Graphics g) {
        g.drawImage(mapsImage, 0, 0, this);
      }
    };
    Dimension imageSize = new Dimension(mapsImage.getWidth(this), 
mapsImage.getHeight(this));
    imageCanvas.setMaximumSize(imageSize);
    imageCanvas.setMinimumSize(imageSize);
    imageCanvas.setPreferredSize(imageSize);
    imageCanvas.addMouseListener(this);
    mapToPanel.add(imageCanvas);
    mapToPanel.add(upperControlsPanel);
    mapToPanel.add(lowerControlsPanel);
    Vector mapFromVec = new Vector();
    mapFromVec.add(panel.getName());
    String[] dataNames = panel.getDataNames();
    if (dataNames != null)
      for (int i = 0 ; i < dataNames.length ; i++)
        mapFromVec.add(dataNames[i]);
    mapFromList = new JList(mapFromVec);
    mapFromList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    mapFromList.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        updateCurrentMapsList();
      }
    });
    mapFromPanel.setLayout(mapFromBoxLayout);
    mapFromPanel.add(mapFromLabel);
    mapFromPanel.add(new JScrollPane(mapFromList));
    mapFromPanel.add(mapValueLabel);
    mapFromPanel.add(mapValueTextField);
    mainPanel.setLayout(mainPanelBoxLayout);
    mainPanel.add(mapFromPanel);
    mainPanel.add(Box.createRigidArea(new Dimension(5, 1)));
    mainPanel.add(mapToPanel);
    mainPanel.add(Box.createRigidArea(new Dimension(5, 1)));
    mainPanel.add(currentMapsPanel);
    JPanel controlPanel = new JPanel(new BorderLayout());
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(mainPanel, BorderLayout.CENTER);
    getContentPane().add(controlPanel, BorderLayout.NORTH);
    mapValueTextField.setMaximumSize(new Dimension(4096, 
mapValueTextField.getFont().getSize()+12));
    this.pack();
    Dimension size = mapToPanel.getSize();
    mapToPanel.setMaximumSize(size);
    mapToPanel.setMinimumSize(size);
    size = this.getSize();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    this.setLocation((screenSize.width-size.width)/2, 
(screenSize.height-size.height)/2);
  }

  /** Bereitet den Dialog zur Darstellung vor. Wird initDialog() vor der ersten
    * Darstellung aufgerufen, beschleunigt sich die Darstellung u.U. erheblich.
    * initDialog() lädt zur Darstellung benötigte graphische Daten in den 
Speicher.
    * Dies geschieht nur einmal, da die graphischen Daten statisch im Speicher
    * abgelegt werden, also allen Instanzen von ConstantMappingDialog zur
    * Verfügung stehen.
    */
  public static void initDialog() {
    if (mapsImage == null) {
      URL url = MappingDialog.class.getResource("display.gif");
      mapsImage = Toolkit.getDefaultToolkit().getImage(url);
    }
  }


  /** Macht den Dialog sichtbar.
    */
  public void display() {
    setVisible(true);
  }


  /** Nach Beendigung des Dialogs können hier die modifizierten ConstantMaps
    * abgerufen werden. Es wird empfohlen diese Methode nur nach Aufruf der
    * Methode okPressed() zu benutzen und dann auch nur falls deren Ergebnis
    * positiv (true) war.
    */
  public ConstantMap[] getMaps() {
  /*  ConstantMap[] newMaps = new ConstantMap[maps.size()];
    maps.toArray(newMaps);
    return newMaps;*/
    return null;
  }


  protected void updateCurrentMapsList() {
    int index = mapFromList.getSelectedIndex();
    if (index > -1)
      currentMapsList.setListData(makeMapNames((Vector)cMaps.get(index)));
  }

  private Vector makeMapNames(Vector maps) {
    Vector mapNames = new Vector();
    ConstantMap map;
    for (int i = 0 ; i < maps.size() ; i++) {
      map = (ConstantMap) maps.get(i);
      String text = new String(map.getConstant()+" -> "+map.getDisplayScalar());
      mapNames.add(text);
    }
    return mapNames;
  }


  /** Standardmethode des ActionListener-Interface.
    */
  public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    if (cmd.equals("clearSelected")) {
      int index = mapFromList.getSelectedIndex();
      Vector maps = (Vector)cMaps.get(index);
      int[] indices = currentMapsList.getSelectedIndices();
      Arrays.sort(indices);
      for (int i = indices.length-1 ; i > -1 ; i--)
        maps.remove(indices[i]);
      updateCurrentMapsList();
    } else if (cmd.equals("clearAll")) {
      int index = mapFromList.getSelectedIndex();
      if (index > -1) {
        ((Vector)cMaps.get(index)).clear();
        updateCurrentMapsList();
      }
    } else if (cmd.equals("done")) {
      panel.setMaps(panel.getScalarMaps());
      Vector cVec = (Vector)cMaps.get(0);
      if (cVec.size() > 0) {
        ConstantMap[] maps = new ConstantMap[cVec.size()];
        cVec.toArray(maps);
        panel.addMaps(maps);
      }
      String[] dataNames = panel.getDataNames();
      for (int i = 1 ; i < cMaps.size() ; i++) {
        cVec = (Vector)cMaps.get(i);
        ConstantMap[] maps = null;
        if (cVec.size() > 0) {
          maps = new ConstantMap[cVec.size()];
          cVec.toArray(maps);
        }
        panel.getDisplayData(dataNames[i-1]).setConstantMaps(maps);
      }
      setVisible(false);
      panel.refreshDisplay();
    } else if (cmd.equals("cancel")) {
      setVisible(false);
    }
  }


  /** Standardmethode des MouseListenerInterface.
    */
  public void mouseClicked(MouseEvent e) {
  }

  /** Standardmethode des MouseListenerInterface.
    */
  public void mousePressed(MouseEvent e) {
    if (e.getComponent().equals(imageCanvas)) {
      int x = e.getX()/40;
      int y = e.getY()/40;
      oldMouseX = x;
      oldMouseY = y;
      Graphics g = imageCanvas.getGraphics();
      g.setXORMode(Color.white);
      g.fillRect(x*40, y*40, 40, 40);
      try {
        String text = mapValueTextField.getText();
        double value = Double.parseDouble(text);
        DisplayRealType type = null;
        switch (6*y+x) {
          case 0: type = Display.XAxis; break;
          case 1: type = Display.YAxis; break;
          case 2: type = Display.ZAxis; break;
          case 3: type = Display.YAxisOffset; break;
          case 4: type = Display.YAxisOffset; break;
          case 5: type = Display.ZAxisOffset; break;
          case 6: type = Display.Latitude; break;
          case 7: type = Display.Longitude; break;
          case 8: type = Display.Radius; break;
          case 9: type = Display.CylRadius; break;
          case 10: type = Display.CylAzimuth; break;
          case 11: type = Display.CylZAxis; break;
          case 12: type = Display.Flow1X; break;
          case 13: type = Display.Flow1Y; break;
          case 14: type = Display.Flow1Z; break;
          case 15: type = Display.Flow2X; break;
          case 16: type = Display.Flow2Y; break;
          case 17: type = Display.Flow2Z; break;
          case 18: type = Display.Flow1Elevation; break;
          case 19: type = Display.Flow1Azimuth; break;
          case 20: type = Display.Flow1Radial; break;
          case 21: type = Display.Flow2Elevation; break;
          case 22: type = Display.Flow2Azimuth; break;
          case 23: type = Display.Flow2Radial; break;
          case 24: type = Display.Red; break;
          case 25: type = Display.Green; break;
          case 26: type = Display.Blue; break;
          case 27: type = Display.RGB; break;
          case 28: type = Display.RGBA; break;
          case 29: type = Display.Alpha; break;
          case 30: type = Display.Cyan; break;
          case 31: type = Display.Magenta; break;
          case 32: type = Display.Yellow; break;
          case 33: type = Display.CMY; break;
          case 34: type = Display.Animation; break;
          case 35: type = Display.IsoContour; break;
          case 36: type = Display.Hue; break;
          case 37: type = Display.Saturation; break;
          case 38: type = Display.Value; break;
          case 39: type = Display.HSV; break;
          case 40: type = Display.SelectValue; break;
          case 41: type = Display.SelectRange; break;
          case 43: type = Display.Text; break;
          case 46: type = Display.Shape; break;
        }
        if (type == null) return;
        ConstantMap map = new ConstantMap(value, type);
        int index = mapFromList.getSelectedIndex();
        if (index > -1) {
          Vector cm = (Vector)cMaps.get(index);
          for (int i = 0 ; i < cm.size() ; i++) {
            if (((ConstantMap)cm.get(i)).getDisplayScalar().equals(type))
              cm.remove(i);
          }
          cm.add(map);
          updateCurrentMapsList();
        }
      } catch(NullPointerException exc1) {
        JOptionPane pane = new JOptionPane("Please type in a valid double 
value",
                                           JOptionPane.ERROR_MESSAGE);
        pane.createDialog(this, "").show();
        exc1.printStackTrace();
      } catch(NumberFormatException exc2) {
        JOptionPane pane = new JOptionPane("Please type in a valid double 
value",
                                           JOptionPane.ERROR_MESSAGE);
        pane.createDialog(this, "").show();
      } catch(VisADException exc3) {
        JOptionPane pane = new JOptionPane(exc3.getMessage(), 
JOptionPane.ERROR_MESSAGE);
        pane.createDialog(this, "").show();
      }
    }
  }

  /** Standardmethode des MouseListenerInterface.
    */
  public void mouseReleased(MouseEvent e) {
    if ((e.getComponent().equals(imageCanvas)) && (oldMouseX != -1) && 
(oldMouseY != -1)) {
      Graphics g = imageCanvas.getGraphics();
      g.setXORMode(Color.white);
      g.fillRect(oldMouseX*40, oldMouseY*40, 40, 40);
      oldMouseX = -1;
      oldMouseY = -1;
    }
  }

  /** Standardmethode des MouseListenerInterface.
    */
  public void mouseEntered(MouseEvent e) {
  }

  /** Standardmethode des MouseListenerInterface.
    */
  public void mouseExited(MouseEvent e) {
  }
}
  • 2003 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the visad archives: