001    /*
002     * This file is part of McIDAS-V
003     *
004     * Copyright 2007-2013
005     * Space Science and Engineering Center (SSEC)
006     * University of Wisconsin - Madison
007     * 1225 W. Dayton Street, Madison, WI 53706, USA
008     * https://www.ssec.wisc.edu/mcidas
009     * 
010     * All Rights Reserved
011     * 
012     * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013     * some McIDAS-V source code is based on IDV and VisAD source code.  
014     * 
015     * McIDAS-V is free software; you can redistribute it and/or modify
016     * it under the terms of the GNU Lesser Public License as published by
017     * the Free Software Foundation; either version 3 of the License, or
018     * (at your option) any later version.
019     * 
020     * McIDAS-V is distributed in the hope that it will be useful,
021     * but WITHOUT ANY WARRANTY; without even the implied warranty of
022     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023     * GNU Lesser Public License for more details.
024     * 
025     * You should have received a copy of the GNU Lesser Public License
026     * along with this program.  If not, see http://www.gnu.org/licenses.
027     */
028    package edu.wisc.ssec.mcidasv.control;
029    
030    import ucar.unidata.data.DataChoice;
031    import ucar.unidata.data.DataDataChoice;
032    import ucar.unidata.data.DirectDataChoice;
033    import ucar.unidata.data.DataSelection;
034    import ucar.unidata.data.DataCategory;
035    
036    import ucar.unidata.data.grid.GridUtil;
037    
038    import ucar.unidata.idv.DisplayConventions;
039    import ucar.unidata.idv.IntegratedDataViewer;
040    import ucar.unidata.idv.control.DisplayControlImpl;
041    import ucar.unidata.idv.DisplayControl;
042    
043    import ucar.visad.display.DisplayMaster;
044    import ucar.visad.display.DisplayableData;
045    import ucar.visad.display.Displayable;
046    import ucar.visad.display.LineDrawing;
047    import ucar.visad.display.RGBDisplayable;
048    
049    import edu.wisc.ssec.mcidasv.data.hydra.HydraRGBDisplayable;
050    import edu.wisc.ssec.mcidasv.data.NearCastTrajDataSource;
051    
052    import visad.*;
053    import visad.BaseColorControl;
054    import visad.VisADException;
055    import visad.RemoteVisADException;
056    import visad.ScalarMapListener;
057    import visad.ScalarMapControlEvent;
058    import visad.ScalarMapEvent;
059    import visad.ReferenceException;
060    import visad.georef.MapProjection;
061    import visad.georef.TrivialMapProjection;
062    
063    
064    import ucar.unidata.idv.ViewManager;
065    import ucar.unidata.idv.ViewDescriptor;
066    
067    import ucar.unidata.util.ColorTable;
068    import ucar.unidata.util.Range;
069    import ucar.unidata.util.Misc;
070    import ucar.unidata.util.GuiUtils;
071    
072    import ucar.unidata.util.LogUtil;
073    
074    import java.awt.Component;
075    import java.awt.Container;
076    import java.awt.FlowLayout;
077    import java.awt.BorderLayout;
078    import java.awt.GridLayout;
079    import java.awt.event.*;
080    import java.awt.geom.Rectangle2D;
081    
082    
083    import javax.swing.*;
084    import javax.swing.event.*;
085    
086    
087    import java.util.HashMap;
088    import java.util.Hashtable;
089    import java.util.Enumeration;
090    import java.util.Iterator;
091    import java.util.List;
092    
093    import java.awt.Color;
094    import java.awt.Font;
095    
096    
097    import java.rmi.RemoteException;
098    
099    
100    public class NearCastTrajControl extends DisplayControlImpl {
101    
102       /** Displayable for the data */
103       LineDrawing trajDisplay;
104    
105       private DisplayMaster displayMaster;
106    
107       FieldImpl trajField = null;
108    
109       RealType rgbRealType = null;
110       String paramName = null;
111    
112       NearCastTrajDataSource dataSource;
113    
114       private DisplayableData rgbDisp;
115    
116       float lineWidth = 1.0f;
117    
118    
119       public NearCastTrajControl() {
120         super();
121         setAttributeFlags(FLAG_COLORTABLE | FLAG_LINEWIDTH);
122       }
123    
124       public boolean init(DataChoice dataChoice) throws VisADException, RemoteException {
125         displayMaster = getViewManager().getMaster();
126         dataSource = (NearCastTrajDataSource) ((DirectDataChoice) dataChoice).getDataSource();
127    
128         DataSelection dataSelection = getDataSelection();
129         trajField = (FieldImpl) dataChoice.getData(dataSelection);
130    
131         FlatField fltFld = null;
132         if (GridUtil.isSequence(trajField)) {
133           FieldImpl fld = (FieldImpl) trajField.getSample(0);
134           if (GridUtil.isSequence(fld)) {
135             fltFld = (FlatField)  fld.getSample(0);
136           }
137         }
138         else {
139           fltFld = (FlatField) trajField;
140         }
141         rgbRealType = (RealType) ((FunctionType)fltFld.getType()).getRange();
142         paramName = rgbRealType.getName();
143    
144         rgbDisp = new RGBDisplayableImpl("traj", rgbRealType, null, true);
145         rgbDisp.setData(trajField);
146    
147         addDisplayable(rgbDisp, FLAG_COLORTABLE);
148    
149         return true;
150       }
151    
152       public void initDone() {
153       }
154    
155       protected void applyLineWidth() throws VisADException, RemoteException {
156         float lw = getLineWidth();
157         if (lw != lineWidth) {
158           rgbDisp.setLineWidth(lw);
159           lineWidth = lw;
160         }
161       }
162    
163       protected Range getInitialRange() throws VisADException, RemoteException {
164         //Range rng = getDisplayConventions().getParamRange(paramName, null);
165         Range rng = null;
166         if (rng == null) {
167           return dataSource.getParamRange();
168         }
169         return rng;
170       }
171    
172       /**
173       protected ColorTable getInitialColorTable() {
174         return getDisplayConventions().getParamColorTable("image");
175       }
176       */
177    
178       private class RGBDisplayableImpl extends RGBDisplayable {
179           RGBDisplayableImpl(String name, RealType rgbRealType, float[][] colorPalette, boolean alphaflag)
180               throws VisADException, RemoteException {
181             super(name, rgbRealType, colorPalette, alphaflag);
182           }
183        }
184    
185        @Override public MapProjection getDataProjection() {
186            MapProjection mp = null;
187            Range lonRange = dataSource.getLonRange();
188            Range latRange = dataSource.getLatRange();
189            float delLon = (float) (lonRange.getMax() - lonRange.getMin());
190            float delLat = (float) (latRange.getMax() - latRange.getMin());
191    
192            try {
193               mp = new TrivialMapProjection(RealTupleType.SpatialEarth2DTuple, 
194                       new Rectangle2D.Float((float)lonRange.getMin(), (float)latRange.getMin(), 
195                                             delLon, delLat));
196            } catch (Exception e) {
197                logException("MultiSpectralControl.getDataProjection", e);
198            }
199    
200            return mp;
201        }
202    
203    
204    }