001    /*
002     * $Id: DragLine.java,v 1.2 2012/02/19 17:35:46 davep Exp $
003     *
004     * This file is part of McIDAS-V
005     *
006     * Copyright 2007-2012
007     * Space Science and Engineering Center (SSEC)
008     * University of Wisconsin - Madison
009     * 1225 W. Dayton Street, Madison, WI 53706, USA
010     * https://www.ssec.wisc.edu/mcidas
011     * 
012     * All Rights Reserved
013     * 
014     * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015     * some McIDAS-V source code is based on IDV and VisAD source code.  
016     * 
017     * McIDAS-V is free software; you can redistribute it and/or modify
018     * it under the terms of the GNU Lesser Public License as published by
019     * the Free Software Foundation; either version 3 of the License, or
020     * (at your option) any later version.
021     * 
022     * McIDAS-V is distributed in the hope that it will be useful,
023     * but WITHOUT ANY WARRANTY; without even the implied warranty of
024     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025     * GNU Lesser Public License for more details.
026     * 
027     * You should have received a copy of the GNU Lesser Public License
028     * along with this program.  If not, see http://www.gnu.org/licenses.
029     */
030    package edu.wisc.ssec.mcidasv.display.hydra;
031    
032    import java.awt.Color;
033    import java.awt.Component;
034    import java.awt.event.ActionEvent;
035    import java.awt.event.ActionListener;
036    import java.rmi.RemoteException;
037    import java.util.ArrayList;
038    import java.util.Enumeration;
039    import java.util.HashMap;
040    import java.util.Hashtable;
041    import java.util.List;
042    import java.util.Map;
043    
044    import ucar.unidata.util.LogUtil;
045    import edu.wisc.ssec.mcidasv.data.hydra.GrabLineRendererJ3D;
046    import visad.util.Util;
047    
048    
049    import visad.*;
050    
051    
052        public class DragLine extends CellImpl {
053            private final String selectorId = hashCode() + "_selector";
054            private final String lineId = hashCode() + "_line";
055            private final String controlId;
056    
057            private ConstantMap[] mappings = new ConstantMap[5];
058    
059            private DataReference line;
060    
061            private DataReference selector;
062    
063            private RealType domainType;
064            private RealType rangeType;
065    
066            private RealTupleType tupleType;
067    
068            private LocalDisplay display;
069    
070            private float[] YRANGE;
071    
072            protected float lastSelectedValue;
073    
074    
075            public DragLine(Gridded1DSet domain, RealType domainType, RealType rangeType,
076                final float lastSelectedValue, LocalDisplay display, final String controlId, 
077                final ConstantMap[] color, float[] YRANGE) throws Exception 
078            {
079                if (controlId == null)
080                    throw new NullPointerException("must provide a non-null control ID");
081                if (color == null)
082                    throw new NullPointerException("must provide a non-null color");
083    
084                this.controlId = controlId;
085                this.YRANGE = YRANGE;
086                this.display = display;
087                this.domainType = domainType;
088        
089    
090                for (int i = 0; i < color.length; i++) {
091                    mappings[i] = (ConstantMap)color[i].clone();
092                }
093                mappings[4] = new ConstantMap(-0.5, Display.YAxis);
094    
095    
096                tupleType = new RealTupleType(domainType, rangeType);
097    
098                selector = new DataReferenceImpl(selectorId);
099                line = new DataReferenceImpl(lineId);
100                
101                display.addReferences(new GrabLineRendererJ3D(domain), new DataReference[] { selector }, new ConstantMap[][] { mappings });
102                display.addReference(line, cloneMappedColor(color));
103    
104                addReference(selector);
105            }
106    
107            private static ConstantMap[] cloneMappedColor(final ConstantMap[] color) throws Exception {
108                assert color != null && color.length >= 3 : color;
109                return new ConstantMap[] { 
110                    (ConstantMap)color[0].clone(),
111                    (ConstantMap)color[1].clone(),
112                    (ConstantMap)color[2].clone(),
113                };
114            }
115    
116            public void annihilate() {
117                try {
118                    display.removeReference(selector);
119                    display.removeReference(line);
120                } catch (Exception e) {
121                    LogUtil.logException("DragLine.annihilate", e);
122                }
123            }
124    
125            public String getControlId() {
126                return controlId;
127            }
128    
129            /**
130             * Handles drag and drop updates.
131             */
132            public void doAction() throws VisADException, RemoteException {
133                setSelectedValue(getSelectedValue());
134            }
135    
136            public float getSelectedValue() {
137                float val = (float)display.getDisplayRenderer().getDirectAxisValue(domainType);
138                if (Float.isNaN(val))
139                    val = lastSelectedValue;
140                return val;
141            }
142    
143            public void setSelectedValue(final float val) throws VisADException,
144                RemoteException 
145            {
146                // don't do work for stupid values
147                if ((Float.isNaN(val)) 
148                    || (selector.getThing() != null && val == lastSelectedValue))
149                    return;
150    
151                line.setData(new Gridded2DSet(tupleType,
152                    new float[][] { { val, val }, { YRANGE[0], YRANGE[1] } }, 2));
153    
154                selector.setData(new Real(domainType, val));
155                lastSelectedValue = val;
156                this.update();
157            }
158    
159            //- applications can extend and override
160            public void update() {
161    
162            }
163        }