001/*
002 * $Id: NearCastTrajControl.java,v 1.5 2011/03/24 16:06:32 davep Exp $
003 *
004 * This file is part of McIDAS-V
005 *
006 * Copyright 2007-2011
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 */
030package edu.wisc.ssec.mcidasv.control;
031
032import ucar.unidata.data.DataChoice;
033import ucar.unidata.data.DataDataChoice;
034import ucar.unidata.data.DirectDataChoice;
035import ucar.unidata.data.DataSelection;
036import ucar.unidata.data.DataCategory;
037
038import ucar.unidata.data.grid.GridUtil;
039
040import ucar.unidata.idv.DisplayConventions;
041import ucar.unidata.idv.IntegratedDataViewer;
042import ucar.unidata.idv.control.DisplayControlImpl;
043import ucar.unidata.idv.DisplayControl;
044
045import ucar.visad.display.DisplayMaster;
046import ucar.visad.display.DisplayableData;
047import ucar.visad.display.Displayable;
048import ucar.visad.display.LineDrawing;
049import ucar.visad.display.RGBDisplayable;
050
051import edu.wisc.ssec.mcidasv.data.hydra.HydraRGBDisplayable;
052import edu.wisc.ssec.mcidasv.data.NearCastTrajDataSource;
053
054import visad.*;
055import visad.BaseColorControl;
056import visad.VisADException;
057import visad.RemoteVisADException;
058import visad.ScalarMapListener;
059import visad.ScalarMapControlEvent;
060import visad.ScalarMapEvent;
061import visad.ReferenceException;
062import visad.georef.MapProjection;
063import visad.georef.TrivialMapProjection;
064
065
066import ucar.unidata.idv.ViewManager;
067import ucar.unidata.idv.ViewDescriptor;
068
069import ucar.unidata.util.ColorTable;
070import ucar.unidata.util.Range;
071import ucar.unidata.util.Misc;
072import ucar.unidata.util.GuiUtils;
073
074import ucar.unidata.util.LogUtil;
075
076import java.awt.Component;
077import java.awt.Container;
078import java.awt.FlowLayout;
079import java.awt.BorderLayout;
080import java.awt.GridLayout;
081import java.awt.event.*;
082import java.awt.geom.Rectangle2D;
083
084
085import javax.swing.*;
086import javax.swing.event.*;
087
088
089import java.util.HashMap;
090import java.util.Hashtable;
091import java.util.Enumeration;
092import java.util.Iterator;
093import java.util.List;
094
095import java.awt.Color;
096import java.awt.Font;
097
098
099import java.rmi.RemoteException;
100
101
102public class NearCastTrajControl extends DisplayControlImpl {
103
104   /** Displayable for the data */
105   LineDrawing trajDisplay;
106
107   private DisplayMaster displayMaster;
108
109   FieldImpl trajField = null;
110
111   RealType rgbRealType = null;
112   String paramName = null;
113
114   NearCastTrajDataSource dataSource;
115
116   private DisplayableData rgbDisp;
117
118   float lineWidth = 1.0f;
119
120
121   public NearCastTrajControl() {
122     super();
123     setAttributeFlags(FLAG_COLORTABLE | FLAG_LINEWIDTH);
124   }
125
126   public boolean init(DataChoice dataChoice) throws VisADException, RemoteException {
127     displayMaster = getViewManager().getMaster();
128     dataSource = (NearCastTrajDataSource) ((DirectDataChoice) dataChoice).getDataSource();
129
130     DataSelection dataSelection = getDataSelection();
131     trajField = (FieldImpl) dataChoice.getData(dataSelection);
132
133     FlatField fltFld = null;
134     if (GridUtil.isSequence(trajField)) {
135       FieldImpl fld = (FieldImpl) trajField.getSample(0);
136       if (GridUtil.isSequence(fld)) {
137         fltFld = (FlatField)  fld.getSample(0);
138       }
139     }
140     else {
141       fltFld = (FlatField) trajField;
142     }
143     rgbRealType = (RealType) ((FunctionType)fltFld.getType()).getRange();
144     paramName = rgbRealType.getName();
145
146     rgbDisp = new RGBDisplayableImpl("traj", rgbRealType, null, true);
147     rgbDisp.setData(trajField);
148
149     addDisplayable(rgbDisp, FLAG_COLORTABLE);
150
151     return true;
152   }
153
154   public void initDone() {
155   }
156
157   protected void applyLineWidth() throws VisADException, RemoteException {
158     float lw = getLineWidth();
159     if (lw != lineWidth) {
160       rgbDisp.setLineWidth(lw);
161       lineWidth = lw;
162     }
163   }
164
165   protected Range getInitialRange() throws VisADException, RemoteException {
166     //Range rng = getDisplayConventions().getParamRange(paramName, null);
167     Range rng = null;
168     if (rng == null) {
169       return dataSource.getParamRange();
170     }
171     return rng;
172   }
173
174   /**
175   protected ColorTable getInitialColorTable() {
176     return getDisplayConventions().getParamColorTable("image");
177   }
178   */
179
180   private class RGBDisplayableImpl extends RGBDisplayable {
181       RGBDisplayableImpl(String name, RealType rgbRealType, float[][] colorPalette, boolean alphaflag)
182           throws VisADException, RemoteException {
183         super(name, rgbRealType, colorPalette, alphaflag);
184       }
185    }
186
187    @Override public MapProjection getDataProjection() {
188        MapProjection mp = null;
189        Range lonRange = dataSource.getLonRange();
190        Range latRange = dataSource.getLatRange();
191        float delLon = (float) (lonRange.getMax() - lonRange.getMin());
192        float delLat = (float) (latRange.getMax() - latRange.getMin());
193
194        try {
195           mp = new TrivialMapProjection(RealTupleType.SpatialEarth2DTuple, 
196                   new Rectangle2D.Float((float)lonRange.getMin(), (float)latRange.getMin(), 
197                                         delLon, delLat));
198        } catch (Exception e) {
199            logException("MultiSpectralControl.getDataProjection", e);
200        }
201
202        return mp;
203    }
204
205
206}