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    
029    package edu.wisc.ssec.mcidasv.data;
030    
031    import edu.wisc.ssec.mcidas.AreaDirectory;
032    import edu.wisc.ssec.mcidas.AREAnav;
033    
034    import edu.wisc.ssec.mcidasv.Constants;
035    
036    import java.awt.Component;
037    import java.awt.Dimension;
038    import java.awt.event.*;
039    import java.awt.Insets;
040    import java.awt.geom.Rectangle2D;
041    import java.rmi.RemoteException;
042    import java.util.ArrayList;
043    import java.util.Enumeration;
044    import java.util.Hashtable;
045    import java.util.List;
046    
047    import javax.accessibility.*;
048    import javax.swing.*;
049    import javax.swing.event.*;
050    
051    import org.slf4j.Logger;
052    import org.slf4j.LoggerFactory;
053    
054    import ucar.unidata.data.DataChoice;
055    import ucar.unidata.data.DataSelection;
056    import ucar.unidata.data.DataSourceImpl;
057    import ucar.unidata.data.DataSelectionComponent;
058    import ucar.unidata.data.GeoLocationInfo;
059    import ucar.unidata.data.GeoSelection;
060    import ucar.unidata.geoloc.LatLonPoint;
061    import ucar.unidata.idv.ui.IdvUIManager;
062    import ucar.unidata.ui.LatLonWidget;
063    import ucar.unidata.util.GuiUtils;
064    import ucar.unidata.util.Misc;
065    import ucar.unidata.util.StringUtil;
066    
067    import visad.VisADException;
068    import visad.data.mcidas.AREACoordinateSystem;
069    import visad.georef.*;
070    
071    
072    public class GeoLatLonSelection extends DataSelectionComponent implements Constants {
073    
074        private static final Logger logger = LoggerFactory.getLogger(GeoLatLonSelection.class);
075        
076          private GeoLocationInfo geoLocInfo;
077    
078          /** The spacing used in the grid layout */
079          protected static final int GRID_SPACING = 3;
080    
081          /** Used by derived classes when they do a GuiUtils.doLayout */
082          protected static final Insets GRID_INSETS = new Insets(GRID_SPACING,
083                                                          GRID_SPACING,
084                                                          GRID_SPACING,
085                                                          GRID_SPACING);
086    
087          DataChoice dataChoice;
088          MapProjection sampleProjection;
089    
090          /** earth coordinates */
091          protected static final String TYPE_LATLON = "Latitude/Longitude";
092    
093          /** image */
094          protected static final String TYPE_IMAGE = "Image Coordinates";
095    
096          /** area */
097          protected static final String TYPE_AREA = "Area Coordinates";
098    
099          /** flag for center */
100          protected static final String PLACE_CENTER = "CENTER";
101    
102          /** flag for upper left */
103          protected static final String PLACE_ULEFT = "ULEFT";
104    
105          /** Property for image default value lat/lon */
106          protected static final String PROP_LATLON = "LATLON";
107    
108          /** Property for image default value line/ele */
109          protected static final String PROP_LINEELE = "LINELE";
110    
111          /** Property for image default value loc */
112          protected static final String PROP_LOC = "LOC";
113    
114          /** Property for image default value mag */
115          protected static final String PROP_MAG = "MAG";
116          protected static final String PROP_LMAG = "LMAG";
117          protected static final String PROP_EMAG = "EMAG";
118    
119          /** Property for image default value place */
120          protected static final String PROP_PLACE = "PLACE";
121    
122          /** Property for image default value size */
123          protected static final String PROP_SIZE = "SIZE";
124    
125          /** Property for image default value unit */
126          protected static final String PROP_TYPE = "TYPE";
127    
128          /** Property for line resolution */
129          protected static final String PROP_LRES = "LRES";
130          protected static final String PROP_PLRES = "PLRES";
131    
132          /** Property for element resolution */
133          protected static final String PROP_ERES = "ERES";
134          protected static final String PROP_PERES = "PERES";
135    
136          protected static final String PROP_READOUT = "READOUT";
137    
138          /** This is the list of properties that are used in the advanced gui */
139          private static final String[] ADVANCED_PROPS = {
140              PROP_TYPE, PROP_PLACE, PROP_LOC, PROP_SIZE,  PROP_MAG,
141              PROP_LMAG, PROP_EMAG, PROP_READOUT
142          };
143    
144          /** This is the list of labels used for the advanced gui */
145          private static final String[] ADVANCED_LABELS = {
146            "Coordinate Type:", "Placement:", "Location:", "   Image Size:",
147            "Magnification:", "", "", "Approx. Area: "
148          };
149    
150          private static final String[] readoutLabels = {
151              "Center     ",
152              "Upper Left ",
153              "Upper Right",
154              "Lower Left ",
155              "Lower Right"
156          };
157    
158          private String kmLbl = " km";
159    
160          /** Input for lat/lon center point */
161          protected LatLonWidget latLonWidget = new LatLonWidget();
162    
163          /** Widget to hold the number of elements in the advanced */
164          JTextField numElementsFld = new JTextField();
165    
166          /** Widget to hold  the number of lines   in the advanced */
167          JTextField numLinesFld = new JTextField();
168    
169          /** Widget for the line  center point in the advanced section */
170          JTextField centerLineFld = new JTextField();
171    
172          /** Widget for the element  center point in the advanced section */
173          JTextField centerElementFld = new JTextField();
174    
175          JTextField lineMagFld = new JTextField();
176          JTextField eleMagFld = new JTextField();
177    
178          /** Label used for the line center */
179          private JLabel centerLineLbl = new JLabel();
180    
181          /** Label used for the element center */
182          private JLabel centerElementLbl = new JLabel();
183    
184          /** Label used for the center latitude */
185          private JLabel centerLatLbl = new JLabel();
186    
187          /** Label used for the center longitude */
188          private JLabel centerLonLbl = new JLabel();
189    
190          /** _more_ */
191          private JToggleButton lockBtn;
192          private JButton fullResBtn;
193    
194          private JPanel lMagPanel;
195          private JPanel eMagPanel;
196    
197          /** Widget for the line magnfication in the advanced section */
198          protected JSlider lineMagSlider;
199    
200          /** Label for the line mag. in the advanced section */
201          JLabel lineMagLbl = new JLabel();
202          JLabel lineResLbl = new JLabel();
203    
204           JLabel rawSizeLbl = new JLabel();
205    
206          /** Widget for the element magnfication in the advanced section */
207          protected JSlider elementMagSlider;
208    
209          /** Label for the element mag. in the advanced section */
210          JLabel elementMagLbl = new JLabel();
211          JLabel elementResLbl = new JLabel();
212    
213          /** location panel */
214          protected GuiUtils.CardLayoutPanel locationPanel;
215    
216          /** flag for setting properties */
217          private boolean amSettingProperties = false;
218    
219          JComboBox coordinateTypeComboBox;
220          JComboBox locationComboBox;
221    
222          String[] coordinateTypes = { TYPE_LATLON, TYPE_IMAGE, TYPE_AREA };
223          String[] locations = {"Center", "Upper Left"};
224    
225    //      static double dNaN = Double.NaN;
226    
227    /** the place string */
228          private String defaultType = TYPE_LATLON;
229          private String place;
230          private String defaultPlace = PLACE_CENTER;
231          private int defaultNumLines = 1000;
232          private int defaultNumEles = 1000;
233          private int numLines = defaultNumLines;
234          private int numEles = defaultNumEles;
235          private double latitude;
236          private double defaultLat = Double.NaN;
237          private double longitude;
238          private double defaultLon = Double.NaN;
239          private  boolean resetLatLon = true;
240          private int imageLine;
241          private int areaLine;
242          private int defaultLine = -1;
243          private int imageElement;
244          private int areaElement;
245          private int defaultElement = -1;
246          private int lineMag;
247          private double dLineMag;
248          private int defaultLineMag;
249          private int elementMag;
250          private double dElementMag;
251          private int defaultElementMag;
252          private boolean isLineEle = false;
253          private double lRes;
254          protected double baseLRes = 0.0;
255          private double eRes;
256          protected double baseERes = 0.0;
257    
258          private Hashtable properties;
259          private int uLLine;
260          private int uLEle;
261          private int centerLine;
262          private int centerEle;
263          protected boolean amUpdating = false;
264    
265    
266          /** Maps the PROP_ property name to the gui component */
267          private Hashtable propToComps = new Hashtable();
268    
269          /** size label */ JLabel sizeLbl;
270    
271          /** base number of lines */
272          private double baseNumLines;
273    
274          /** base number of elements */
275          private double baseNumElements;
276    
277          private DataSourceImpl dataSource;
278          private static DataSourceImpl lastDataSource;
279          private AreaDirectory previewDir;
280          private AREAnav previewNav;
281          private AREAnav areaNav;
282    
283          private List latLonLbls = new ArrayList();
284          private List linEleImageLbls = new ArrayList();
285          private List linEleAreaLbls = new ArrayList();
286          private JPanel latLonPanel;
287          private JPanel lineElementPanel;
288    
289          /**
290           * limit of slider
291           */
292          private static final int SLIDER_MAX = 1;
293          private static final int SLIDER_MIN = -29;
294          private static final int SLIDER_WIDTH = 150;
295          private static final int SLIDER_HEIGHT = 16;
296    
297          /**
298           *  Keep track of the lines to element ratio
299           */
300          private double linesToElements = 1.0;
301     
302          double[][] imageEL = new double[2][5];
303          double[][] areaEL = new double[2][5];
304          double[][] displayEL = new double[2][5];
305          double[][] latLon = new double[2][5];
306    
307          private int[] previewDirBlk;
308    
309          private int previewLineRes = 1;
310          private int previewEleRes = 1;
311          private int maxLines = 0;
312          private int maxEles = 0;
313    
314          private double bLRes = 0.0;
315          private double bERes = 0.0;
316    
317          private List readoutLLWidget = new ArrayList();
318          private List readoutLatFld = new ArrayList();
319          private List readoutLonFld = new ArrayList();
320          private JPanel latLonReadoutPanel;
321    
322          private List readoutImageLinFld = new ArrayList();
323          private List readoutImageEleFld = new ArrayList();
324          private JPanel lineElementImageReadoutPanel;
325    
326          private List readoutAreaLinFld = new ArrayList();
327          private List readoutAreaEleFld = new ArrayList();
328          private JPanel lineElementAreaReadoutPanel;
329    
330          private GuiUtils.CardLayoutPanel readoutPanel;
331    
332          public GeoLatLonSelection(DataSourceImpl dataSource,
333                 DataChoice dataChoice, Hashtable initProps, MapProjection sample,
334                 AreaDirectory dir, AREAnav nav) 
335                  throws VisADException, RemoteException {
336              super("Advanced");
337    
338              if (dataSource != lastDataSource) {
339                  this.resetLatLon = true;
340              }
341              lastDataSource = dataSource;
342    
343              this.properties = initProps;
344              this.dataSource = dataSource;
345              this.dataChoice = dataChoice;
346              this.sampleProjection = sample;
347              this.previewDir = dir;
348    
349              setBaseNumLines(dir.getLines());
350              setBaseNumElements(dir.getElements());
351              this.previewNav = nav;
352              previewDirBlk = this.previewDir.getDirectoryBlock();
353              int areaLinRes = previewDirBlk[11];
354              int areaEleRes = previewDirBlk[12];
355              this.areaNav = this.previewNav;
356              this.areaNav.setRes(areaLinRes, areaEleRes);
357              this.areaNav.setImageStart(previewDirBlk[5], previewDirBlk[6]);
358    
359              int numberOfLines;
360              int numberOfElements;
361              if (properties.containsKey(PROP_SIZE)) {
362                  String str = (String)properties.get(PROP_SIZE);
363                  String[] strs = StringUtil.split(str, " ", 2);
364                  numberOfLines = Integer.parseInt(strs[0]);
365                  numberOfElements = Integer.parseInt(strs[1]);
366              } else {
367                  try {
368                      numberOfLines = this.previewDir.getLines();
369                      numberOfElements = this.previewDir.getElements();
370                      if (numberOfLines < defaultNumLines)
371                          defaultNumLines = numberOfLines;
372                      if (numberOfElements < defaultNumEles)
373                          defaultNumEles = numberOfElements;
374                      numberOfLines = defaultNumLines;
375                      numberOfElements = defaultNumEles;
376                  } catch (Exception e) {
377                      logger.error("no directory", e);
378                      return;
379                  }
380              }
381              setNumLines(numberOfLines);
382              setNumEles(numberOfElements);
383              if (properties.containsKey(PROP_MAG)) {
384                  String str = (String)properties.get(PROP_MAG);
385                  String[] strs = StringUtil.split(str, " ", 2);
386                  this.defaultLineMag = Integer.parseInt(strs[0]);
387                  this.defaultElementMag = Integer.parseInt(strs[1]);
388                  this.dLineMag = (double)this.defaultLineMag;
389                  this.dElementMag = (double)this.defaultElementMag;
390              } else {
391                  this.dLineMag = -(double)this.previewDir.getLines()/(double)numberOfLines;
392                  this.dElementMag = -(double)this.previewDir.getElements()/(double)numberOfElements;
393                  this.defaultLineMag = (int)(Math.floor(dLineMag));
394                  this.defaultElementMag = (int)(Math.floor(dElementMag));
395              }
396              setLineMag(this.defaultLineMag);
397              setElementMag(this.defaultElementMag);
398    
399              try {
400                  if (properties.containsKey(PROP_LRES)) {
401                      this.bLRes = Double.parseDouble((String)properties.get(PROP_LRES));
402                      //if (dir.getValue(11) == 1) this.bLRes = this.previewDir.getCenterLatitudeResolution();
403                      this.baseLRes = this.bLRes * (double)(dir.getValue(11));
404                      setLRes(this.baseLRes * Math.abs(this.defaultLineMag));
405                  }
406                  if (properties.containsKey(PROP_ERES)) {
407                      this.bERes = Double.parseDouble((String)properties.get(PROP_ERES));
408                      //if (dir.getValue(12) == 1) this.bERes = this.previewDir.getCenterLongitudeResolution();
409                      this.baseERes = this.bERes * (double)(dir.getValue(12));
410                      setERes(this.baseERes * Math.abs(this.defaultElementMag));
411                  }
412              } catch (Exception e) {
413                  logger.error("unable to get resolution", e);
414                 
415              }
416              setBLRes(this.bLRes);
417              setBERes(this.bERes);
418              if (this.baseLRes == 0.0)
419                  this.baseLRes = this.previewDir.getCenterLatitudeResolution();
420              if (this.baseERes == 0.0)
421                  this.baseERes = this.previewDir.getCenterLongitudeResolution();
422    
423              this.place = getPlace();
424              if (properties.containsKey(PROP_PLACE)) {
425                  setPlace((String)properties.get(PROP_PLACE));
426              }
427    
428              if (properties.containsKey(PROP_PLRES)) {
429                  this.previewLineRes = Integer.parseInt((String)properties.get(PROP_PLRES));
430              }
431              if (properties.containsKey(PROP_PERES)) {
432                  this.previewEleRes = Integer.parseInt((String)properties.get(PROP_PERES));
433              }
434    
435              if (this.resetLatLon) {
436                  if (this.previewDir != null) {
437                      setLatitude(this.previewDir.getCenterLatitude());
438                      setLongitude(this.previewDir.getCenterLongitude());
439                  }
440              } else {
441                  setLatitude(this.latitude);
442                  setLongitude(this.longitude);
443              }
444              convertToLineEle();
445    
446              if (properties.containsKey(PROP_LATLON)) {
447                  String str = (String)properties.get(PROP_LATLON);
448                  String[] strs = StringUtil.split(str, " ", 2);
449                  setLatitude(Double.parseDouble(strs[0]));
450                  setLongitude(Double.parseDouble(strs[1]));
451                  convertToLineEle();
452                  this.isLineEle = false;
453              } else if (properties.containsKey(PROP_LINEELE)) {
454                  String str = (String)properties.get(PROP_LINEELE);
455                  String[] strs = StringUtil.split(str, " ", 3);
456                  setLine(Integer.parseInt(strs[0]));
457                  setElement(Integer.parseInt(strs[1]));
458                  convertToLatLon();
459                  this.isLineEle = true;
460              }
461    
462              if (this.defaultLineMag > 1) {
463                  numberOfLines = numberOfLines * this.defaultLineMag;
464                  setNumLines(numberOfLines);
465                  setLRes(lRes/this.defaultLineMag);
466                  this.defaultLineMag = 1;
467                  setLineMag(this.defaultLineMag);
468              }
469              if (this.defaultElementMag > 1) {
470                  numberOfElements = numberOfElements * this.defaultElementMag;
471                  setNumEles(numberOfElements);
472                  setERes(lRes/this.defaultElementMag);
473                  this.defaultElementMag = 1;
474                  setElementMag(this.defaultElementMag);
475              }
476          }
477    
478          protected JComponent doMakeContents() {
479              String[] propArray  = getAdvancedProps();
480              String[] labelArray = getAdvancedLabels();
481              Insets  dfltGridSpacing = new Insets(4, 0, 4, 0);
482              String  dfltLblSpacing  = " ";
483              List allComps = new ArrayList();
484    
485              for (int propIdx = 0; propIdx < propArray.length; propIdx++) {
486                  JComponent propComp = null;
487                  String     prop     = propArray[propIdx];
488                  if (prop.equals(PROP_TYPE)) {
489                      allComps.add(new JLabel(" "));
490                      allComps.add(new JLabel(" "));
491                      coordinateTypeComboBox = new JComboBox(coordinateTypes);
492                      coordinateTypeComboBox.addActionListener(new ActionListener() {
493                          public void actionPerformed(ActionEvent ae) {
494                              int selectedIndex = coordinateTypeComboBox.getSelectedIndex();
495                              flipLocationPanel(selectedIndex);
496                              flipReadoutPanel(selectedIndex);
497                          }
498                      });
499                      propComp = (JComponent)coordinateTypeComboBox;
500                  }
501                  else if (prop.equals(PROP_LOC)) {
502                      locationComboBox = new JComboBox(locations);
503                      setPlace(this.place);
504                      locationComboBox.addActionListener(new ActionListener() {
505                          public void actionPerformed(ActionEvent ae) {
506                              String selected = getPlace();
507                              cyclePlace();
508                          }
509                      });
510                      propComp = (JComponent)locationComboBox;
511                      addPropComp(PROP_LOC, propComp);
512    
513                      ActionListener latLonChange =new ActionListener() {
514                          public void actionPerformed(ActionEvent ae) {
515                              String type = getCoordinateType();
516                              if (type.equals(TYPE_LATLON)) {
517                                  setLatitude();
518                                  setLongitude();
519                                  convertToLineEle();
520                                  getGeoLocationInfo();
521                              } else {
522                                  setLineElement();
523                                  convertToLatLon();
524                                  getGeoLocationInfo();
525                              }
526                          }
527                      };
528    
529                      FocusListener linEleFocusChange = new FocusListener() {
530                          public void focusGained(FocusEvent fe) {
531                          }
532                          public void focusLost(FocusEvent fe) {
533                              setLineElement();
534                              convertToLatLon();
535                              getGeoLocationInfo();
536                          }
537                      };
538    
539                      if (latLonWidget == null)
540                          latLonWidget     = new LatLonWidget(latLonChange);
541    
542                      FocusListener latLonFocusChange = new FocusListener() {
543                          public void focusGained(FocusEvent fe) {
544                              JTextField latFld = latLonWidget.getLatField();
545                              latFld.setCaretPosition(latFld.getText().length());
546                              JTextField lonFld = latLonWidget.getLonField();
547                              lonFld.setCaretPosition(lonFld.getText().length());
548                          }
549                          public void focusLost(FocusEvent fe) {
550                              setLatitude();
551                              setLongitude();
552                              convertToLineEle();
553                              getGeoLocationInfo();
554                          }
555                      };
556    
557                      if (!this.isLineEle) {
558                          latLonWidget.setLatLon(this.latitude, this.longitude);
559                      }
560                      String lineStr = "";
561                      String eleStr = "";
562                      setLine(this.imageLine);
563                      setElement(this.imageElement);
564                      if ((this.imageLine >= 0) && (this.imageElement >= 0)) {
565                          lineStr =Integer.toString(this.imageLine);
566                          eleStr =Integer.toString(this.imageElement);
567                      }
568                      centerLineFld    = new JTextField(lineStr, 3);
569                      centerLineFld.addActionListener(latLonChange);
570                      centerLineFld.addFocusListener(linEleFocusChange);
571                      final String lineField = "";
572                      centerElementFld = new JTextField(eleStr, 3);
573                      centerElementFld.addActionListener(latLonChange);
574                      centerElementFld.addFocusListener(linEleFocusChange);
575                      final JButton centerPopupBtn =
576                          GuiUtils.getImageButton(
577                            "/auxdata/ui/icons/MapIcon16.png", getClass());
578                      centerPopupBtn.setToolTipText("Center on current displays");
579    
580                      centerPopupBtn.addActionListener(new ActionListener() {
581                          public void actionPerformed(ActionEvent ae) {
582                              dataSource.getDataContext().getIdv().getIdvUIManager().popupCenterMenu(
583                                  centerPopupBtn, latLonWidget);
584                          }
585                      });
586    
587                      JComponent centerPopup = GuiUtils.inset(centerPopupBtn,
588                                                 new Insets(0, 0, 0, 4));
589    
590    
591                      GuiUtils.tmpInsets = dfltGridSpacing;
592                      JTextField latFld = latLonWidget.getLatField();
593                      JTextField lonFld = latLonWidget.getLonField();
594                      latFld.addFocusListener(latLonFocusChange);
595                      lonFld.addFocusListener(latLonFocusChange);
596                      latLonPanel = GuiUtils.hbox(new Component[] {
597                          centerLatLbl = GuiUtils.rLabel(" Lat:" + dfltLblSpacing),
598                          latFld,
599                          centerLonLbl = GuiUtils.rLabel(" Lon:" + dfltLblSpacing),
600                          lonFld,
601                          new JLabel(" "), centerPopup
602                      });
603    
604                      lineElementPanel =
605                          GuiUtils.hbox(new Component[] {
606                              centerLineLbl =
607                                  GuiUtils.rLabel(" Line:" + dfltLblSpacing),
608                              centerLineFld,
609                              centerElementLbl = GuiUtils.rLabel(" Element:"
610                                  + dfltLblSpacing),
611                              centerElementFld });
612    
613                      locationPanel = new GuiUtils.CardLayoutPanel();
614                      locationPanel.addCard(latLonPanel);
615                      locationPanel.addCard(lineElementPanel);
616    
617                      if (propComp != null) {
618                          allComps.add(GuiUtils.rLabel(labelArray[propIdx]));
619                          allComps.add(GuiUtils.left(propComp));
620                      }
621                      propComp = GuiUtils.hbox(new Component[] { locationPanel }, 1);
622                      if (propComp != null) {
623                          allComps.add(GuiUtils.rLabel("  "));
624                          allComps.add(GuiUtils.left(propComp));
625                      }
626                      propComp = null;
627                  } else if (prop.equals(PROP_SIZE)) {
628                      ActionListener sizeChange =new ActionListener() {
629                          public void actionPerformed(ActionEvent ae) {
630                              int lines = getNumLines() * Math.abs(getLineMag());
631                              if (lines > maxLines) lines = maxLines;
632                              setBaseNumLines(lines);
633                              int eles = getNumEles() * Math.abs(getElementMag());
634                              if (eles > maxEles) eles = maxEles;
635                              setBaseNumElements(eles);
636                              getGeoLocationInfo();
637                          }
638                      };
639                      FocusListener sizeFocusChange = new FocusListener() {
640                          public void focusGained(FocusEvent fe) {
641                          }
642                          public void focusLost(FocusEvent fe) {
643                              int lines = getNumLines() * Math.abs(getLineMag());
644                              if (lines > maxLines) lines = maxLines;
645                              setBaseNumLines(lines);
646                              int eles = getNumEles() * Math.abs(getElementMag());
647                              if (eles > maxEles) eles = maxEles;
648                              setBaseNumElements(eles);
649                              getGeoLocationInfo();
650                          }
651                      };
652                      
653                      this.maxLines = this.previewDir.getLines();
654                      this.maxEles = this.previewDir.getElements();
655                      
656                      int lmag = getLineMag();
657                      int emag = getElementMag();
658                      if (lmag < 0) this.numLines = this.maxLines / Math.abs(lmag);
659                      if (emag < 0) this.numEles = this.maxEles / Math.abs(emag);
660    
661                      setNumLines(this.numLines);
662                      numLinesFld    = new JTextField(Integer.toString(this.numLines), 4);
663                      numLinesFld.addActionListener(sizeChange);
664                      numLinesFld.addFocusListener(sizeFocusChange);
665                      setNumEles(this.numEles);
666                      numElementsFld = new JTextField(Integer.toString(this.numEles), 4);
667                      numElementsFld.addActionListener(sizeChange);
668                      numElementsFld.addFocusListener(sizeFocusChange);
669                      numLinesFld.setToolTipText("Number of lines");
670                      numElementsFld.setToolTipText("Number of elements");
671                      GuiUtils.tmpInsets = dfltGridSpacing;
672                      sizeLbl            = GuiUtils.lLabel("");
673    
674                      fullResBtn = GuiUtils.makeImageButton(
675                          "/auxdata/ui/icons/arrow_out.png", this,
676                          "setToFullResolution");
677                      fullResBtn.setContentAreaFilled(false);
678                      fullResBtn.setToolTipText("Set fields to retrieve full image");
679    
680                      lockBtn =
681                              GuiUtils.getToggleImageButton(IdvUIManager.ICON_UNLOCK,
682                                              IdvUIManager.ICON_LOCK, 0, 0, true);
683                      lockBtn.setContentAreaFilled(false);
684                      lockBtn.setSelected(true);
685                      lockBtn.setToolTipText(
686                                      "Unlock to automatically change size when changing magnification");
687    
688                      rawSizeLbl = new JLabel(" Raw size: " + this.maxLines + " X " + 
689                                                              this.maxEles);
690                      JPanel sizePanel =
691                          GuiUtils.left(GuiUtils.doLayout(new Component[] {
692                              numLinesFld,
693                              new JLabel(" X "), numElementsFld, sizeLbl, new JLabel(" "),
694                              fullResBtn, new JLabel("  "), lockBtn,
695                              rawSizeLbl }, 9, GuiUtils.WT_N, GuiUtils.WT_N));
696                      addPropComp(PROP_SIZE, propComp = sizePanel);
697                  } else if (prop.equals(PROP_MAG)) {
698                      propComp = GuiUtils.hbox(new Component[] { new JLabel("") }, 1);
699                      addPropComp(PROP_MAG, propComp);
700                  } else if (prop.equals(PROP_LMAG)) {
701                      boolean oldAmSettingProperties = amSettingProperties;
702                      amSettingProperties = true;
703                      ChangeListener lineListener =
704                          new javax.swing.event.ChangeListener() {
705                          public void stateChanged(ChangeEvent evt) {
706                              if (amSettingProperties) {
707                                  return;
708                              }
709                              int val = getMagValue(lineMagSlider);
710                              setLineMag(val);
711                              amUpdating = true;
712                              lineMagSliderChanged(!lockBtn.isSelected());
713                              amUpdating = false;
714                              getGeoLocationInfo();
715                          }
716                      };
717                      ActionListener lineMagChange =new ActionListener() {
718                          public void actionPerformed(ActionEvent ae) {
719                              if (amSettingProperties) {
720                                  return;
721                              }
722                              setLineMag();
723                              changeLineMagSlider(!lockBtn.isSelected());
724                              getGeoLocationInfo();
725                          }
726                      };
727                      FocusListener lineMagFocusChange = new FocusListener() {
728                          public void focusGained(FocusEvent fe) {
729                          }
730                          public void focusLost(FocusEvent fe) {
731                              if (amSettingProperties) {
732                                  return;
733                              }
734                              setLineMag();
735                              changeLineMagSlider(!lockBtn.isSelected());
736                              getGeoLocationInfo();
737                          }
738                      };
739                      JComponent[] lineMagComps =
740                          GuiUtils.makeSliderPopup(SLIDER_MIN, SLIDER_MAX, 0,
741                                                   lineListener);
742                      lineMagSlider = (JSlider) lineMagComps[1];
743                      lineMagSlider.setPreferredSize(new Dimension(SLIDER_WIDTH,SLIDER_HEIGHT));
744                      lineMagSlider.setMajorTickSpacing(1);
745                      lineMagSlider.setSnapToTicks(true);
746                      lineMagSlider.setExtent(1);
747                      int mag = getLineMag();
748                      setLineMagSlider(mag);
749                      lineMagComps[0].setToolTipText(
750                          "Change the line magnification");
751                      lineMagSlider.setToolTipText(
752                          "Slide to set line magnification factor");
753                      String str = "Line Mag=";
754                      lineMagFld = new JTextField(Integer.toString(mag),3);
755                      lineMagFld.addFocusListener(lineMagFocusChange);
756                      lineMagFld.addActionListener(lineMagChange);
757                      lineMagLbl =
758                          GuiUtils.getFixedWidthLabel(StringUtil.padLeft(str, 4));
759                      str = truncateNumericString(Double.toString(this.baseLRes*Math.abs(getLineMag())), 1);
760                      str = " Res=" + str + kmLbl;
761                      lineResLbl =
762                          GuiUtils.getFixedWidthLabel(StringUtil.padLeft(str, 4));
763                      amSettingProperties = oldAmSettingProperties;
764    
765                      GuiUtils.tmpInsets  = dfltGridSpacing;
766                      lMagPanel = GuiUtils.doLayout(new Component[] {
767                                            lineMagLbl, lineMagFld,
768                                            GuiUtils.inset(lineMagComps[1],
769                                                new Insets(0, 4, 0, 0)), lineResLbl, }, 5,
770                                                    GuiUtils.WT_N, GuiUtils.WT_N);
771                      propComp = GuiUtils.hbox(new Component[] { new JLabel(" "), lMagPanel }, 2);
772                      addPropComp(PROP_LMAG, propComp = lMagPanel);
773                  } else if (prop.equals(PROP_EMAG)) {
774                      boolean oldAmSettingProperties = amSettingProperties;
775                      amSettingProperties = true;
776                      ChangeListener elementListener = new ChangeListener() {
777                          public void stateChanged(
778                                  javax.swing.event.ChangeEvent evt) {
779                              if (amSettingProperties) {
780                                  return;
781                              }
782                              int val = getMagValue(elementMagSlider);
783                              setElementMag(val);
784                              amUpdating = true;
785                              elementMagSliderChanged(true);
786                              amUpdating = false;
787                              getGeoLocationInfo();
788                          }
789                      };
790                      ActionListener eleMagChange =new ActionListener() {
791                          public void actionPerformed(ActionEvent ae) {
792                              if (amSettingProperties) {
793                                  return;
794                              }
795                              setElementMag();
796                              changeEleMagSlider(!lockBtn.isSelected());
797                              getGeoLocationInfo();
798                          }
799                      };
800                      FocusListener eleMagFocusChange = new FocusListener() {
801                          public void focusGained(FocusEvent fe) {
802                          }
803                          public void focusLost(FocusEvent fe) {
804                              if (amSettingProperties) {
805                                  return;
806                              }
807                              setElementMag();
808                              changeEleMagSlider(!lockBtn.isSelected());
809                              getGeoLocationInfo();
810                          }
811                      };
812                      JComponent[] elementMagComps =
813                          GuiUtils.makeSliderPopup(SLIDER_MIN, SLIDER_MAX, 0,
814                                                   elementListener);
815                      elementMagSlider = (JSlider) elementMagComps[1];
816                      elementMagSlider.setPreferredSize(new Dimension(SLIDER_WIDTH,SLIDER_HEIGHT));
817                      elementMagSlider.setExtent(1);
818                      elementMagSlider.setMajorTickSpacing(1);
819                      elementMagSlider.setSnapToTicks(true);
820                      int mag = getElementMag();
821                      setElementMagSlider(mag);
822                      elementMagComps[0].setToolTipText(
823                          "Change the element magnification");
824                      elementMagSlider.setToolTipText(
825                          "Slide to set element magnification factor");
826                      eleMagFld = new JTextField(Integer.toString(mag),3);
827                      eleMagFld.addFocusListener(eleMagFocusChange);
828                      eleMagFld.addActionListener(eleMagChange);
829                      String str = "Ele  Mag=";
830                      elementMagLbl =
831                          GuiUtils.getFixedWidthLabel(StringUtil.padLeft(str, 4));
832                      str = truncateNumericString(Double.toString(this.baseERes*Math.abs(getElementMag())), 1);
833                      str = " Res=" + str + kmLbl;
834                      elementResLbl =
835                          GuiUtils.getFixedWidthLabel(StringUtil.padLeft(str, 4));
836                      amSettingProperties = oldAmSettingProperties;
837    
838                      GuiUtils.tmpInsets  = dfltGridSpacing;
839                      eMagPanel = GuiUtils.doLayout(new Component[] {
840                                            elementMagLbl, eleMagFld,
841                                            GuiUtils.inset(elementMagComps[1],
842                                                new Insets(0, 4, 0, 0)), elementResLbl, }, 5,
843                                                    GuiUtils.WT_N, GuiUtils.WT_N);
844                      propComp = GuiUtils.hbox(new Component[] { new JLabel(" "), eMagPanel }, 2);
845                      addPropComp(PROP_EMAG, propComp = eMagPanel);
846                  } else if (prop.equals(PROP_READOUT)) {
847                      allComps.add(new JLabel(" "));
848                      allComps.add(new JLabel(" "));
849                      for (int i=0; i<5; i++) {
850                          latLonLbls.add(GuiUtils.getFixedWidthLabel(" "));
851                          linEleImageLbls.add(GuiUtils.getFixedWidthLabel(" "));
852                          linEleAreaLbls.add(GuiUtils.getFixedWidthLabel(" "));
853                      }
854                      latLonReadoutPanel = GuiUtils.left(GuiUtils.doLayout(
855                          latLonLbls, 1, GuiUtils.WT_N, GuiUtils.WT_Y));
856                      lineElementImageReadoutPanel = GuiUtils.left(GuiUtils.doLayout(
857                          linEleImageLbls, 1, GuiUtils.WT_N, GuiUtils.WT_Y));
858                      lineElementAreaReadoutPanel = GuiUtils.left(GuiUtils.doLayout(
859                          linEleAreaLbls, 1, GuiUtils.WT_N, GuiUtils.WT_Y));
860    
861                      readoutPanel = new GuiUtils.CardLayoutPanel();
862                      readoutPanel.addCard(latLonReadoutPanel);
863                      readoutPanel.addCard(lineElementImageReadoutPanel);
864                      readoutPanel.addCard(lineElementAreaReadoutPanel);
865    
866                      propComp = GuiUtils.hbox(new Component[] { readoutPanel }, 1);
867                      addPropComp(PROP_READOUT, propComp);
868                      if (propComp != null) {
869                          allComps.add(GuiUtils.rLabel(labelArray[propIdx]));
870                          allComps.add(GuiUtils.left(propComp));
871                      }
872                      propComp = null;
873                  }
874                  if (propComp != null) {
875                      allComps.add(GuiUtils.rLabel(labelArray[propIdx]));
876                      allComps.add(GuiUtils.left(propComp));
877                  }
878              }
879              GuiUtils.tmpInsets = GRID_INSETS;
880              JPanel imagePanel = GuiUtils.doLayout(allComps, 2, GuiUtils.WT_NY,
881                                      GuiUtils.WT_N);
882              getGeoLocationInfo();
883              return GuiUtils.top(imagePanel);
884          }
885    
886          private void updateReadout() {
887              int numCols = 7;
888              for (int i=0; i<5; i++) {
889                  String str = readoutLabels[i] +
890                               " Lat: " + formatDoubleCoord(numCols, latLon[0][i]) +
891                               " Lon: " + formatDoubleCoord(numCols, latLon[1][i]);
892                  ((JLabel)latLonLbls.get(i)).setText(str);
893    
894                  String lineStr = formatIntegerCoord(numCols, imageEL[1][i]);
895                  String eleStr = formatIntegerCoord(numCols, imageEL[0][i]);
896                  str = readoutLabels[i] + " Line: " + lineStr +
897                                           " Element: " + eleStr;
898                  ((JLabel)linEleImageLbls.get(i)).setText(str);
899    
900                  lineStr = formatIntegerCoord(numCols, areaEL[1][i]);
901                  eleStr = formatIntegerCoord(numCols, areaEL[0][i]);
902                  str = readoutLabels[i] + " Line: " + lineStr +
903                                           " Element: " + eleStr;
904                  ((JLabel)linEleAreaLbls.get(i)).setText(str);
905              }
906          }
907    
908          private String formatIntegerCoord(int cols, double val) {
909              String outStr = Misc.MISSING;
910              Double dbl = new Double(val);
911              if (!dbl.isNaN()) {
912                  int ival = (int)Math.floor(val + 0.5);
913                  outStr = Integer.toString(ival);
914              }
915              int len = outStr.length() + 1;
916              while (len < cols) {
917                  outStr = new String(" ").concat(outStr);
918                  len++;
919              }
920              return outStr;
921          }
922    
923          private String formatDoubleCoord(int cols, double val) {
924              String outStr = Misc.MISSING;
925              Double dbl = new Double(val);
926              if (!dbl.isNaN()) {
927                  outStr = Double.toString(val);
928                  if (outStr.length() > cols)
929                      outStr = outStr.substring(0, cols);
930              }
931              int len = outStr.length();
932              while (len < cols) {
933                  outStr = new String(" ").concat(outStr);
934                  len++;
935              }
936              return outStr;
937          }
938    
939          /**
940           * Change coordinate type panel
941           */
942          protected void flipLocationPanel(int locPanel) {
943              int nowPlaying = locationPanel.getVisibleIndex();
944              if (locPanel > 0) {
945                  if (nowPlaying == 0) {
946                      locationPanel.flip();
947                  }
948                  setIsLineEle(true);
949                  String type = getCoordinateType();
950                  int ele = this.imageElement;
951                  int lin = this.imageLine;
952                  if (type.equals(TYPE_AREA)) {
953                      ele = this.areaElement;
954                      lin = this.areaLine;
955                  }
956                  setElement(ele);
957                  setLine(lin);
958              } else {
959                  if (nowPlaying > 0) locationPanel.flip();
960                  setIsLineEle(false);
961              }
962          }
963    
964          /**
965           * Change readout type panel
966           */
967          protected void flipReadoutPanel(int roPanel) {
968              readoutPanel.show(roPanel);
969          }
970    
971          /**
972           * Set to full resolution
973           */
974          public void setToFullResolution() {
975              setPlace(PLACE_CENTER);
976              setLatitude(this.previewDir.getCenterLatitude());
977              setLongitude(this.previewDir.getCenterLongitude());
978              convertToLinEle();
979              setNumLines(this.maxLines);
980              setNumEles(this.maxEles);
981              setBaseNumLines(this.maxLines);
982              setBaseNumElements(this.maxEles);
983              setLineMag(1);
984              setElementMag(1);
985              setLineMagSlider(1);
986              setLRes(this.baseLRes);
987              setElementMagSlider(1);
988              setERes(this.baseERes);
989              amUpdating = true;
990              lineMagSliderChanged(false);
991              elementMagSliderChanged(false);
992              amUpdating = false;
993              getGeoLocationInfo();
994          }
995    
996          @Override public void applyToDataSelection(DataSelection dataSelection) {
997              logger.trace("dataSelection={}", dataSelection);
998    
999             if (dataSelection == null) {
1000                 dataSelection = new DataSelection(true);
1001             }
1002    
1003             if (geoLocInfo == null) {
1004                 getGeoLocationInfo();
1005             }
1006             
1007             GeoLocationInfo geoInfo = geoLocInfo;
1008             if (geoInfo == null) {
1009                 logger.trace("err, wtf?");
1010                 dataSelection = null;
1011                 return;
1012             }
1013    
1014             String coordType = getCoordinateType();
1015    
1016             if (!isLineEle) {
1017                 logger.trace("dealing with LALO coords for datasel={}", dataSelection);
1018                 double lat = getLatitude();
1019                 double lon = getLongitude();
1020                 if (lat > 90.0 && lon> 360.0) {
1021                     convertToLatLon();
1022                     lat = getLatitude();
1023                     lon = getLongitude();
1024                 }
1025    //             if ((lat == dNaN) || (lon == dNaN)) {
1026    //                 return;
1027    //             }
1028                 if ((Double.isNaN(lat) || (Double.isNaN(lon)))) {
1029                     return;
1030                 }
1031    
1032                 String latString = Double.toString(lat);
1033                 if (latString.length() > 8) {
1034                     latString = latString.substring(0,7);
1035                 }
1036                 String lonString = Double.toString(lon);
1037                 if (lonString.length() > 9) {
1038                     lonString = lonString.substring(0,8);
1039                 }
1040                 dataSelection.putProperty(PROP_LATLON, (latString + " " + lonString));
1041             } else {
1042                 logger.trace("dealing with line-ele coords for datasel={}", dataSelection);
1043                 int lin = getLine();
1044                 int ele = getElement();
1045                 if ((Double.isNaN(lin)) || (Double.isNaN(ele))) {
1046                     return;
1047                 }
1048    //             if ((lin == dNaN) || (ele == dNaN)) return;
1049    
1050                 String typeStr = " I";
1051                 if (coordType.equals(TYPE_AREA)) typeStr = " F";
1052                 String linString = Integer.toString(lin);
1053                 String eleString = Integer.toString(ele);
1054                 dataSelection.putProperty(PROP_LINEELE, (linString + " " + eleString + typeStr));
1055             }
1056    
1057             dataSelection.putProperty(PROP_PLACE, getPlace());
1058             dataSelection.putProperty(PROP_MAG, (getLineMag() + " " + getElementMag()));
1059    
1060             GeoSelection geoSelection = new GeoSelection(geoInfo);
1061             dataSelection.setGeoSelection(geoSelection);
1062    
1063             int nlins = getNumLines();
1064             int neles = getNumEles();
1065             if (nlins > 0 && neles > 0) {
1066                 dataSelection.putProperty(PROP_SIZE, (nlins + " " + neles));
1067             }
1068             logger.trace("dataChoice={} dataSelection={}", dataChoice, dataSelection);
1069             dataChoice.setDataSelection(dataSelection);
1070             
1071             this.dataSource.setDataSelection(dataSelection);
1072        }
1073    
1074          @Override public boolean getShowInControlProperties() {
1075              return false;
1076          }
1077    
1078        public GeoLocationInfo getGeoLocationInfo() {
1079            geoLocInfo = null;
1080            double[][] el = convertToDisplayCoords();
1081            int ele = (int)Math.floor(el[0][0] + 0.5);
1082            if (ele < 0) ele = 0;
1083            int lin = (int)Math.floor(el[1][0] + 0.5);
1084            if (lin < 0) lin = 0;
1085            geoLocInfo = getGeoLocationInfo(lin, ele);
1086            return geoLocInfo;
1087        }
1088    
1089        protected GeoLocationInfo getGeoLocationInfo(int lin, int ele) {
1090            int nLin = getNumLines();
1091            if (nLin > 0) {
1092                int nEle = getNumEles();
1093                if (nEle > 0) {
1094                    int lMag = getLineMag();
1095                    if (lMag > 1) return geoLocInfo;
1096                    int eMag = getElementMag();
1097                    if (eMag > 1) return geoLocInfo;
1098                    geoLocInfo = makeGeoLocationInfo(lin, ele, nLin, nEle,
1099                                 lMag, eMag);
1100                    return geoLocInfo;
1101                }
1102            }
1103            for (int i = 0; i < 2; i++) {
1104                for (int j = 0; j < 5; j++) {
1105                    latLon[i][j] = Double.NaN;
1106                    imageEL[i][j] = Double.NaN;
1107                    areaEL[i][j] = Double.NaN;
1108                    displayEL[i][j] = Double.NaN;
1109                }
1110            }
1111            updateReadout();
1112            setLine(-1);
1113            setElement(-1);
1114            return null;
1115        }
1116    
1117        private GeoLocationInfo makeGeoLocationInfo(int lin, int ele, int nlins, int neles,
1118                                int linMag, int eleMag) {
1119             geoLocInfo = null;
1120    
1121             String plc = getPlace();
1122             String type = getCoordinateType();
1123    
1124             AREACoordinateSystem macs = (AREACoordinateSystem)sampleProjection;
1125             Rectangle2D mapArea = macs.getDefaultMapArea();
1126             double previewXDim = mapArea.getWidth();
1127             double previewYDim = mapArea.getHeight();
1128    
1129             double dLine = (double)nlins/(2.0*this.previewLineRes)*Math.abs(linMag);
1130             double dEle = (double)neles/(2.0*this.previewEleRes)*Math.abs(eleMag);
1131    
1132             if (plc.equals(PLACE_CENTER)) {
1133                 displayEL[0][0] = ele;
1134                 displayEL[1][0] = lin;
1135                 displayEL[0][1] = ele - dEle;
1136                 if (displayEL[0][1] < 0) displayEL[0][1] = 0.0;
1137                 displayEL[1][1] = lin + dLine;
1138                 if (displayEL[1][1] > previewYDim) displayEL[1][1] = previewYDim;
1139             } else if (plc.equals(PLACE_ULEFT)) {
1140                 displayEL[0][0] = ele + dEle;
1141                 if (displayEL[0][0] > previewXDim) displayEL[0][0] = previewXDim;
1142                 displayEL[1][0] = lin - dLine;
1143                 if (displayEL[1][0] < 0) displayEL[1][0] = 0.0;
1144                 displayEL[0][1] = ele;
1145                 displayEL[1][1] = lin;
1146             }
1147             int cEle = (int)Math.ceil(displayEL[0][0]);
1148             int cLin = (int)Math.ceil(displayEL[1][0]);
1149             displayEL[0][2] = cEle + dEle;
1150             if (displayEL[0][2] > previewXDim) displayEL[0][2] = previewXDim;
1151             displayEL[1][2] = cLin + dLine;
1152             if (displayEL[1][2] > previewYDim) displayEL[1][2] = previewYDim;
1153             displayEL[0][3] = cEle - dEle;
1154             if (displayEL[0][3] < 0) displayEL[0][3] = 0.0;
1155             displayEL[1][3] = cLin - dLine;
1156             if (displayEL[1][3] < 0) displayEL[1][3] = 0.0;
1157             displayEL[0][4] = cEle + dEle;
1158             if (displayEL[0][4] > previewXDim) displayEL[0][4] = previewXDim;
1159             displayEL[1][4] = cLin - dLine;
1160             if (displayEL[1][4] < 0) displayEL[1][4] = 0.0;
1161    /*
1162             System.out.println("\nDisplay:");
1163             System.out.println("    0: " + displayEL[1][0] + " " + displayEL[0][0]);
1164             System.out.println("    1: " + displayEL[1][1] + " " + displayEL[0][1]);
1165             System.out.println("    2: " + displayEL[1][2] + " " + displayEL[0][2]);
1166             System.out.println("    3: " + displayEL[1][3] + " " + displayEL[0][3]);
1167             System.out.println("    4: " + displayEL[1][4] + " " + displayEL[0][4]);
1168    */
1169             areaEL = displayCoordToAreaCoord(displayEL);
1170    /*
1171             System.out.println("\nArea:");
1172             System.out.println("    0: " + areaEL[1][0] + " " + areaEL[0][0]);
1173             System.out.println("    1: " + areaEL[1][1] + " " + areaEL[0][1]);
1174             System.out.println("    2: " + areaEL[1][2] + " " + areaEL[0][2]);
1175             System.out.println("    3: " + areaEL[1][3] + " " + areaEL[0][3]);
1176             System.out.println("    4: " + areaEL[1][4] + " " + areaEL[0][4]);
1177    */
1178             for (int i=0; i<5; i++) {
1179                 if (areaEL[0][i] < 0.0) areaEL[0][i] = 0.0;
1180                 if (areaEL[0][i] > this.maxEles) areaEL[0][i] = (double)this.maxEles;
1181                 if (areaEL[1][i] < 0.0) areaEL[1][i] = 0.0;
1182                 if (areaEL[1][i] > this.maxLines) areaEL[1][i] = (double)this.maxLines;
1183             }
1184    
1185             try {
1186                 latLon = macs.toReference(displayEL);
1187             } catch (Exception e) {
1188                 logger.error("converting input latitude/longitude", e);
1189             }
1190    /*
1191             System.out.println("\nLat/Lon:");
1192             System.out.println("    0: " + latLon[0][0] + " " + latLon[1][0]);
1193             System.out.println("    1: " + latLon[0][1] + " " + latLon[1][1]);
1194             System.out.println("    2: " + latLon[0][2] + " " + latLon[1][2]);
1195             System.out.println("    3: " + latLon[0][3] + " " + latLon[1][3]);
1196             System.out.println("    4: " + latLon[0][4] + " " + latLon[1][4]);
1197    */
1198             double maxLat = latLon[0][1];
1199             if (latLon[0][2] > maxLat) maxLat = latLon[0][2];
1200             double minLat = latLon[0][3];
1201             if (latLon[0][4] < minLat) minLat = latLon[0][4];
1202             double maxLon = latLon[1][4];
1203             if (latLon[1][2] > maxLon) maxLon = latLon[1][2];
1204             double minLon = latLon[1][1];
1205             if (latLon[1][3] < minLon) minLon = latLon[1][3];
1206    
1207             imageEL = this.previewNav.areaCoordToImageCoord(areaEL);
1208    /*
1209             System.out.println("\nImage:");
1210             System.out.println("    0: " + imageEL[1][0] + " " + imageEL[0][0]);
1211             System.out.println("    1: " + imageEL[1][1] + " " + imageEL[0][1]);
1212             System.out.println("    2: " + imageEL[1][2] + " " + imageEL[0][2]);
1213             System.out.println("    3: " + imageEL[1][3] + " " + imageEL[0][3]);
1214             System.out.println("    4: " + imageEL[1][4] + " " + imageEL[0][4]);
1215    */
1216             updateReadout();
1217    
1218             geoLocInfo = new GeoLocationInfo(maxLat, minLon, minLat, maxLon);
1219    
1220             return geoLocInfo;
1221        }
1222    
1223        /**
1224         * Get the list of advanced property names
1225         *
1226         * @return array of advanced property names
1227         */
1228        protected String[] getAdvancedProps() {
1229            return ADVANCED_PROPS;
1230        }
1231    
1232        /**
1233         * Get the list of advanced property labels
1234         *
1235         * @return list of advanced property labels
1236         */
1237        protected String[] getAdvancedLabels() {
1238            return ADVANCED_LABELS;
1239        }
1240    
1241    
1242        /**
1243         * Cycle the place
1244         */
1245        public void cyclePlace() {
1246            
1247            String type = getCoordinateType();
1248            int dLine = getNumLines()/2 * Math.abs(getLineMag());
1249            int dEle = getNumEles()/2 * Math.abs(getElementMag());
1250            if (this.place.equals(PLACE_CENTER)) {
1251                int newVal = this.areaLine + dLine;
1252                if (newVal > this.maxLines/2) newVal = this.maxLines/2;
1253                this.areaLine = newVal;
1254                newVal = this.areaElement + dEle;
1255                if (newVal > this.maxEles/2) newVal = this.maxEles/2;
1256                this.areaElement = newVal;
1257            } else {
1258                int newVal = this.areaLine - dLine;
1259                if (newVal < 0) newVal = 0;
1260                this.areaLine = newVal;
1261                newVal = this.areaElement - dEle;
1262                if (newVal < 0) newVal = 0;
1263                this.areaElement = newVal;
1264            }
1265            double[][] el = new double[2][1];
1266            el[0][0] = this.areaElement;
1267            el[1][0] = this.areaLine;
1268            double[][] vals = this.areaNav.areaCoordToImageCoord(el);
1269            this.imageElement = (int)Math.floor(vals[0][0] + 0.5);
1270            this.imageLine = (int)Math.floor(vals[1][0] + 0.5);
1271    
1272            if (type.equals(TYPE_AREA)) {
1273                setLine(this.areaLine);
1274                setElement(this.areaElement);
1275            } else if (type.equals(TYPE_IMAGE)) {
1276                setLine(this.imageLine);
1277                setElement(this.imageElement);
1278            }
1279            double[][] ll = this.areaNav.toLatLon(el);
1280            setLatitude(ll[0][0]);
1281            setLongitude(ll[1][0]);
1282        }
1283    
1284    
1285        /**
1286         * Associates the goven JComponent with the PROP_ property
1287         * identified  by the given propId
1288         *
1289         * @param propId The property
1290         * @param comp The gui component that allows the user to set the property
1291         *
1292         * @return Just returns the given comp
1293         */
1294        protected JComponent addPropComp(String propId, JComponent comp) {
1295            Object oldComp = propToComps.get(propId);
1296            if (oldComp != null) {
1297                throw new IllegalStateException(
1298                    "Already have a component defined:" + propId);
1299            }
1300            propToComps.put(propId, comp);
1301            return comp;
1302        }
1303    
1304        /**
1305         * Translate a place name into a human readable form
1306         *
1307         * @param place raw name
1308         *
1309         * @return human readable name
1310         */
1311        protected String translatePlace(String thisPlace) {
1312            if (thisPlace.equals("Upper Left")) {
1313                return PLACE_ULEFT;
1314            }
1315            if (thisPlace.equals("Center")) {
1316                return PLACE_CENTER;
1317            }
1318            return thisPlace;
1319        }
1320    
1321        private void setNumberOfLines(int val) {
1322            numLinesFld.setText(Integer.toString(val));
1323        }
1324    
1325        private void setNumberOfElements(int val) {
1326            numElementsFld.setText(Integer.toString(val));
1327        }
1328    
1329        public String getPlace() {
1330            try {
1331                this.place = translatePlace((String)locationComboBox.getSelectedItem());
1332            } catch (Exception e) {
1333                this.place = defaultPlace;
1334            }
1335            return this.place;
1336        }
1337    
1338        public void setPlace(String str) {
1339            if (str.equals("")) str = defaultPlace;
1340            this.place = str;
1341            if (str.equals(PLACE_CENTER))
1342                locationComboBox.setSelectedItem("Center");
1343            else
1344                locationComboBox.setSelectedItem("Upper Left");
1345        }
1346    
1347        public int getNumLines() {
1348            int val = -1;
1349            try {
1350                val = Integer.parseInt(numLinesFld.getText().trim());
1351            } catch (Exception e) {
1352                logger.error("problem within getNumLines", e);
1353            }
1354            setNumLines(val);
1355            return this.numLines;
1356        }
1357    
1358        public void setNumLines(int val) {
1359            this.numLines = val;
1360            if (val >= 0) setNumberOfLines(val);
1361        }
1362    
1363        public int getNumEles() {
1364            int val = -1;
1365            try {
1366                val = Integer.parseInt(numElementsFld.getText().trim());
1367            } catch (Exception e) {
1368                logger.error("problem within getNumEles", e);
1369            }
1370            setNumEles(val);
1371            return this.numEles;
1372        }
1373    
1374        public void setNumEles(int val) {
1375            val = (int)((double)val/4.0 + 0.5)*4;
1376            this.numEles = val;
1377            if (val >= 0) setNumberOfElements(val);
1378        }
1379    
1380        public int getLine() {
1381            int val = -1;
1382            try {
1383                if (!(centerLineFld.getText().equals(Misc.MISSING)))
1384                    val = Integer.parseInt(centerLineFld.getText().trim());
1385            } catch (Exception e) {
1386            }
1387            return val;
1388        }
1389    
1390        protected void setLineElement() {
1391            double[][] el = getLineElement();
1392            String type = getCoordinateType();
1393            if (type.equals(TYPE_IMAGE)) {
1394                this.imageElement = (int)Math.floor(el[0][0] + 0.5);
1395                this.imageLine = (int)Math.floor(el[1][0] + 0.5);
1396                double[][] vals = this.areaNav.imageCoordToAreaCoord(el);
1397                this.areaElement = (int)Math.floor(vals[0][0] + 0.5);
1398                this.areaLine = (int)Math.floor(vals[1][0] + 0.5);
1399            } else {
1400                this.areaElement = (int)Math.floor(el[0][0] + 0.5);
1401                this.areaLine = (int)Math.floor(el[1][0] + 0.5);
1402                double[][] vals = this.areaNav.areaCoordToImageCoord(el);
1403                this.imageElement = (int)Math.floor(vals[0][0] + 0.5);
1404                this.imageLine = (int)Math.floor(vals[1][0] + 0.5);
1405            }
1406        }
1407    
1408        public void setLine(int val) {
1409            if (val < 0)
1410                centerLineFld.setText(Misc.MISSING);
1411            else
1412                centerLineFld.setText(Integer.toString(val));
1413        }
1414    
1415        public int getElement() {
1416            int val =-1;
1417            try {
1418                val = Integer.parseInt(centerElementFld.getText().trim());
1419            } catch (Exception e) {
1420            }
1421            return val;
1422        }
1423    
1424        private double[][] getLineElement() {
1425            double[][] el = new double[2][1];
1426            el[0][0] = (double)getElement();
1427            el[1][0] = (double)getLine();
1428            return el;
1429        }
1430    
1431        public void setElement(int val) {
1432            if (val < 0)
1433                centerElementFld.setText(Misc.MISSING);
1434            else
1435                centerElementFld.setText(Integer.toString(val));
1436        }
1437    
1438        public int getLineMag() {
1439            return this.lineMag;
1440        }
1441    
1442        private void setElementMag() {
1443            int val = 1;
1444            try {
1445                val = Integer.parseInt(eleMagFld.getText().trim());
1446            } catch (Exception e) {
1447                logger.error("problem setting element mag", e);
1448                return;
1449            }
1450            setElementMag(val);
1451        }
1452    
1453        public void setLineMag(int val) {
1454            if (val > SLIDER_MAX) val = SLIDER_MAX;
1455            if (val < SLIDER_MIN-1) val = SLIDER_MIN-1;
1456            if (val == -1) val = 1;
1457            this.lineMag = val;
1458            setDLineMag((double)val);
1459        }
1460    
1461        private void setLineMagSlider(int val) {
1462            if (val == 1) val = -1;
1463            if (val > SLIDER_MAX) val = -1;
1464            if (val < SLIDER_MIN) val = SLIDER_MIN-1;
1465            lineMagSlider.setValue(val + 1);
1466        }
1467    
1468        public int getElementMag() {
1469            return this.elementMag;
1470        }
1471    
1472        private void setLineMag() {
1473            int val = 1;
1474            try {
1475                val = Integer.parseInt(lineMagFld.getText().trim());
1476            } catch (Exception e) {
1477                logger.error("problem setting line mag", e);
1478            }
1479            setLineMag(val);
1480        }
1481    
1482        public void setDLineMag(double val) {
1483            this.dLineMag = val;
1484        }
1485    
1486        public double getDLineMag() {
1487            return this.dLineMag;
1488        }
1489    
1490        public void setDElementMag(double val) {
1491            this.dElementMag = val;
1492        }
1493    
1494        private void setElementMagSlider(int val) {
1495            if (val == 1) val = -1;
1496            if (val > SLIDER_MAX) val = -1;
1497            if (val < SLIDER_MIN) val = SLIDER_MIN-1;
1498            elementMagSlider.setValue(val + 1);
1499        }
1500    
1501        public double getDElementMag() {
1502            return this.dElementMag;
1503        }
1504    
1505        public void setElementMag(int val) {
1506            if (val > SLIDER_MAX) val = SLIDER_MAX;
1507            if (val < SLIDER_MIN-1) val = SLIDER_MIN-1;
1508            if (val == -1) val = 1;
1509            this.elementMag = val;
1510            setDElementMag((double)val);
1511        }
1512    
1513        public double getLatitude() {
1514            double val = latLonWidget.getLat();
1515    //        Double dbl = new Double(val);
1516            if (Double.isNaN(val)) val = defaultLat;
1517            if (val < -90.0 || val > 90.0) val = defaultLat;
1518            setLatitude(val);
1519            return this.latitude;
1520        }
1521    
1522        private void setLatitude() {
1523            this.latitude = latLonWidget.getLat();
1524        }
1525    
1526        public void setLatitude(double val) {
1527            if (val < -90.0 || val > 90.0)
1528                val = defaultLat;
1529            latLonWidget.setLat(val);
1530            this.latitude = val;
1531            this.resetLatLon = false;
1532        }
1533    
1534        private void setLongitude() {
1535            this.longitude = latLonWidget.getLon();
1536        }
1537    
1538        public double getLongitude() {
1539            double val = latLonWidget.getLon();
1540    //        Double dbl = new Double(val);
1541            if (Double.isNaN(val)) val = defaultLon;
1542            if (val < -180.0 || val > 180.0) val = defaultLon;
1543            setLongitude(val);
1544            return this.longitude;
1545        }
1546    
1547        public void setLongitude(double val) {
1548            if (val < -180.0 || val > 180.0)
1549                val = defaultLon;
1550            latLonWidget.setLon(val);
1551            this.longitude = val;
1552            this.resetLatLon = false;
1553        }
1554    
1555        protected void convertToLineEle() {
1556            double[][] ll = new double[2][1];
1557            ll[0][0] = getLatitude();
1558            ll[1][0] = getLongitude();
1559            double[][] el = this.areaNav.toLinEle(ll);
1560            this.areaElement = (int)Math.floor(el[0][0] + 0.5);
1561            this.areaLine = (int)Math.floor(el[1][0] + 0.5);
1562            el = this.areaNav.areaCoordToImageCoord(el);
1563            this.imageElement = (int)Math.floor(el[0][0] + 0.5);
1564            this.imageLine = (int)Math.floor(el[1][0] + 0.5);
1565        }
1566    
1567        protected void convertToLatLon() {
1568            double[][] el = getLineElement();
1569            double[][] ll = new double[2][1];
1570            String coordType = getCoordinateType();
1571            if (coordType.equals(TYPE_IMAGE))
1572                el = this.previewNav.imageCoordToAreaCoord(el);
1573    
1574            try {
1575                AREACoordinateSystem macs = (AREACoordinateSystem)sampleProjection;
1576                ll = macs.toReference(el);
1577                setLatitude(ll[0][0]);
1578                setLongitude(ll[1][0]);
1579                getGeoLocationInfo();
1580            } catch (Exception e) {
1581                logger.error("problem converting to latitude/longitude", e);
1582            }
1583        }
1584    
1585        protected void convertToLatLon(int ele, int lin) {
1586            try {
1587                double[][] el = new double[2][1];
1588                double[][] ll = new double[2][1];
1589                AREACoordinateSystem macs = (AREACoordinateSystem)sampleProjection;
1590                el[0][0] = (double)ele;
1591                el[1][0] = (double)lin;
1592                ll = macs.toReference(el);
1593                setLatitude(ll[0][0]);
1594                setLongitude(ll[1][0]);
1595                double[][] imageLE = new double[2][1];
1596                double[][] areaLE = new double[2][1];
1597                areaLE = this.previewNav.toLinEle(ll);
1598                imageLE = this.previewNav.areaCoordToImageCoord(areaLE);
1599                setCenterCoords((int)imageLE[0][0], (int)imageLE[1][0]);
1600                getGeoLocationInfo();
1601            } catch (Exception e) {
1602                logger.error("problem converting to latitude/longitude", e);
1603            }
1604        }
1605    
1606        protected double[][] convertToDisplayCoords() {
1607            double[][] el = getLineElement();
1608            try {
1609                double[][] ll = new double[2][1];
1610                AREACoordinateSystem macs = (AREACoordinateSystem)sampleProjection;
1611                String type = getCoordinateType();
1612                if (type.equals(TYPE_LATLON)) {
1613                    ll[0][0] = getLatitude();
1614                    ll[1][0] = getLongitude();
1615                    el = macs.fromReference(ll);
1616                } else {
1617                    int[] dirB = macs.getDirBlock();
1618                    int previewLineMag = dirB[11];
1619                    int previewEleMag = dirB[12];
1620                    int dirLMag = this.previewDir.getValue(11);
1621                    int dirEMag = this.previewDir.getValue(12);
1622                    if (type.equals(TYPE_IMAGE))
1623                        el = this.areaNav.imageCoordToAreaCoord(el);
1624                    Rectangle2D mapArea = macs.getDefaultMapArea();
1625                    int previewXDim = new Long(new Double(mapArea.getMaxX() - mapArea.getMinX()).longValue()).intValue();
1626                    int previewYDim = new Long(new Double(mapArea.getMaxY() - mapArea.getMinY()).longValue()).intValue();
1627                    el[0][0] = el[0][0] * dirEMag / previewEleMag;
1628                    el[1][0] = previewYDim - 1 - el[1][0] * dirLMag / previewLineMag;;
1629                }
1630            } catch (Exception e) {
1631                logger.error("problem converting to display coordinates", e);
1632            }
1633            return el;
1634        }
1635    
1636        private double[][] displayCoordToAreaCoord(double[][] disp) {
1637            double[][] area = new double[2][disp[0].length];
1638            try {
1639                if (sampleProjection != null) {
1640                    AREACoordinateSystem macs = (AREACoordinateSystem)sampleProjection;
1641                    double[][] ll = macs.toReference(disp);
1642                    double[][] el = this.areaNav.toLinEle(ll);
1643                    int midEle = (int)Math.floor(el[0][0] + 0.5);
1644                    int midLin = (int)Math.floor(el[1][0] + 0.5);
1645    
1646                    int width = (int)Math.floor(Math.abs(disp[0][2] - disp[0][1])
1647                                * getPreviewEleRes() + 0.5);
1648    
1649                    int height = (int)Math.floor(Math.abs(disp[1][3] - disp[1][1]) 
1650                                * getPreviewLineRes() + 0.5);
1651                    int deltaEle = width/2;
1652                    int deltaLin = height/2;
1653    
1654                    area[0][0] = midEle;
1655                    area[1][0] = midLin;
1656                    area[0][1] = midEle - deltaEle;
1657                    area[1][1] = midLin - deltaLin;
1658                    area[0][2] = midEle + deltaEle;
1659                    area[1][2] = midLin - deltaLin;
1660                    area[0][3] = midEle - deltaEle;
1661                    area[1][3] = midLin + deltaLin;
1662                    area[0][4] = midEle + deltaEle;
1663                    area[1][4] = midLin + deltaLin;
1664    
1665                }
1666            } catch (Exception e) {
1667                logger.error("problem converting display coordinates to area coordinates", e);
1668            }
1669            return area;
1670        }
1671    
1672        private double[][] areaCoordToDisplayCoord(double[][] area) {
1673            double[][] disp = new double[2][area[0].length];
1674            try {
1675                if (sampleProjection != null) {
1676                    AREACoordinateSystem macs = (AREACoordinateSystem)sampleProjection;
1677                    int[] dirB = macs.getDirBlock();
1678                    int previewLineMag = dirB[11];
1679                    int previewEleMag = dirB[12];
1680                    Rectangle2D mapArea = macs.getDefaultMapArea();
1681                    int previewXDim = new Long(new Double(mapArea.getMaxX() - mapArea.getMinX()).longValue()).intValue();
1682                    int previewYDim = new Long(new Double(mapArea.getMaxY() - mapArea.getMinY()).longValue()).intValue();
1683                    for (int i=0; i<area[0].length; i++) {
1684                        disp[0][i] = area[0][i] / previewEleMag;
1685                        disp[1][i] = previewYDim - 1 - area[1][i] / previewLineMag;
1686                    }
1687                }
1688            } catch (Exception e) {
1689                logger.error("problem converting area coordinates to display coordinates", e);
1690            }
1691            return disp;
1692        }
1693    
1694        protected void convertToLinEle() {
1695            try {
1696                double[][] el = new double[2][1];
1697                double[][] ll = new double[2][1];
1698                AREACoordinateSystem macs = (AREACoordinateSystem)sampleProjection;
1699                ll[0][0] = getLatitude();
1700                ll[1][0] = getLongitude();
1701                String coordType = getCoordinateType();
1702                el = this.previewNav.toLinEle(ll);
1703                if (coordType.equals(TYPE_IMAGE))
1704                    el = this.previewNav.areaCoordToImageCoord(el);
1705                setLine((int)el[1][0]);
1706                setElement((int)el[0][0]);
1707                getGeoLocationInfo();
1708            } catch (Exception e) {
1709                logger.error("problem converting to lines/elements", e);
1710            }
1711        }
1712    
1713        public String getCoordinateType() {
1714            String ret = defaultType;
1715            try {
1716                ret = (String)coordinateTypeComboBox.getSelectedItem();
1717            } catch (Exception e) {
1718            }
1719            return ret;
1720        }
1721    
1722        protected void setCoordinateType(String type) {
1723            if (!type.equals(TYPE_IMAGE)) {
1724                if (!type.equals(TYPE_AREA)) {
1725                    type = TYPE_LATLON;
1726                }
1727            }
1728            coordinateTypeComboBox.setSelectedItem(type);
1729        }
1730    
1731        protected void setLockOn(boolean val) {
1732            lockBtn.setSelected(val);
1733        }
1734    
1735        public boolean getLockOn() {
1736            return lockBtn.isSelected();
1737        }
1738            
1739        protected void setULCoords(double x, double y) {
1740            uLLine = (int)y;
1741            uLEle = (int)x;
1742        }
1743     
1744        protected void setCenterCoords(int x, int y) {
1745            centerLine = y;
1746            setLine(y);
1747            centerEle = x;
1748            setElement(x);
1749        }
1750    
1751        protected void elementMagSliderChanged(boolean recomputeLineEleRatio) {
1752            int value = getElementMag();
1753            if (!amUpdating) {
1754              value = getElementMagValue();
1755              setElementMag(value);
1756            }
1757            if ((Math.abs(value) < SLIDER_MAX)) {
1758                int lineMag = getLineMagValue();
1759                if (lineMag > value) {
1760                    linesToElements = Math.abs(lineMag
1761                                               / (double) value);
1762                } else {
1763                    linesToElements = Math.abs((double) value
1764                                               / lineMag);
1765                }
1766            }
1767            elementMagLbl.setText("Ele  Mag=");
1768            eleMagFld.setText(new Integer(value).toString());
1769            String str = " Res=" +
1770                truncateNumericString(Double.toString(this.baseERes*Math.abs(value)), 1);
1771            elementResLbl.setText(StringUtil.padLeft(str, 4) + kmLbl);
1772    
1773            if (!lockBtn.isSelected()) {
1774                if (value > 0) {
1775                    setNumberOfElements((int)(this.baseNumElements * value));
1776                } else {
1777                    setNumberOfElements((int)(this.baseNumElements
1778                                                    / (double) -value));
1779                }
1780            }
1781        }
1782    
1783        private void changeLineMagSlider(boolean autoSetSize) {
1784            int value = getLineMag();
1785            setLineMagSlider(value);
1786        }
1787    
1788        private void changeEleMagSlider(boolean autoSetSize) {
1789            int value = getElementMag();
1790            setElementMagSlider(value);
1791        }
1792    
1793        /**
1794         * Handle the line mag slider changed event 
1795         *
1796         * @param evt  the event
1797         */
1798        protected void lineMagSliderChanged(boolean autoSetSize) {
1799            try {
1800                int value = getLineMag();
1801                if (!amUpdating) {
1802                    value = getLineMagValue();
1803                    setLineMag(value);
1804                }
1805                lineMagLbl.setText("Line Mag=");
1806                lineMagFld.setText(new Integer(value).toString());
1807                String str = " Res=" +
1808                    truncateNumericString(Double.toString(this.baseLRes*Math.abs(value)), 1);
1809                lineResLbl.setText(StringUtil.padLeft(str, 4) + kmLbl);
1810    
1811                if (autoSetSize) {
1812                    if (value > 0) {
1813                        setNumberOfLines((int)(this.baseNumLines * value));
1814                    } else {
1815                        setNumberOfLines((int)(this.baseNumLines
1816                                                        / (double) -value));
1817                    }
1818                }
1819    
1820                if (value == 1) {                     // special case
1821                    if (linesToElements < 1.0) {
1822                        value = (int) (-value / linesToElements);
1823                    } else {
1824                        value = (int) (value * linesToElements);
1825                    }
1826                } else if (value > 1) {
1827                    value = (int) (value * linesToElements);
1828                } else {
1829                    value = (int) (value / linesToElements);
1830                }
1831    
1832                amSettingProperties = true;
1833                setElementMag(value);
1834                setElementMagSlider(value);
1835                amSettingProperties = false;
1836                elementMagSliderChanged(false);
1837            } catch (Exception exc) {
1838                logger.error("could not set line magnifiction", exc);
1839            }
1840        }
1841    
1842        /**
1843         * Get the value of the line magnification slider.
1844         *
1845         * @return The magnification value for the line
1846         */
1847        protected int getLineMagValue() {
1848            int val = getMagValue(lineMagSlider);
1849            return val;
1850        }
1851    
1852        /**
1853         * Get the value of the element magnification slider.
1854         *
1855         * @return The magnification value for the element
1856         */
1857        protected int getElementMagValue() {
1858            int val = getMagValue(elementMagSlider) - 1;
1859            setElementMag(val);
1860            return val;
1861        }
1862    
1863        /**
1864         * Get the value of the given  magnification slider.
1865         *
1866         * @param slider The slider to get the value from
1867         * @return The magnification value
1868         */
1869        private int getMagValue(JSlider slider) {
1870            //Value is [-SLIDER_MAX,SLIDER_MAX]. We change 0 and -1 to 1
1871            int value = slider.getValue();
1872            if (value == 0) {
1873                value = SLIDER_MAX;
1874                return value;
1875            } else if (value < SLIDER_MIN) {
1876                value = SLIDER_MIN;
1877            }
1878            return value - 1;
1879        }
1880    
1881    
1882        /**
1883         * _more_
1884         *
1885         * @param el _more_
1886         * @param name _more_
1887         * @param listener _more_
1888         *
1889         * @return _more_
1890         */
1891    
1892        private JMenuItem makeLocationMenuItem(final LatLonPoint llp,
1893                                               final String name) {
1894            JMenuItem mi = null;
1895            try {
1896                double alt = 0.0;
1897                EarthLocationTuple elt = 
1898                    new EarthLocationTuple(llp.getLatitude(), llp.getLongitude(), alt);
1899                mi =
1900                new JMenuItem(
1901                    StringUtil.padRight(name + ": ", 15, " ")
1902                    + dataSource.getDataContext().getIdv().getDisplayConventions()
1903                    .formatLatLonPoint(elt.getLatLonPoint()));
1904                GuiUtils.setFixedWidthFont(mi);
1905            } catch (Exception e) {
1906                logger.error("could not create location menu item", e);
1907            }
1908            return mi;
1909        }
1910    
1911    
1912        public boolean getIsLineEle() {
1913            return this.isLineEle;
1914        }
1915    
1916        public void setIsLineEle(boolean val) {
1917            this.isLineEle = val;
1918        }
1919    
1920    
1921        public double getLRes() {
1922            return this.lRes;
1923        }
1924    
1925        public void setLRes(double val) {
1926            if (val < 1) val = this.baseLRes;
1927            this.lRes = val;
1928        }
1929    
1930        public void setBLRes(double val) {
1931            this.bLRes = val;
1932        }
1933    
1934        public void setBERes(double val) {
1935            this.bERes = val;
1936        }
1937    
1938        public double getBLRes() {
1939            return this.bLRes;
1940        }
1941    
1942        public double getBERes() {
1943            return this.bERes;
1944        }
1945    
1946        public double getERes() {
1947            return this.eRes;
1948        }
1949    
1950        public void setERes(double val) {
1951            if (val < 1) val = this.baseERes;
1952            this.eRes = val;
1953        }
1954    
1955        public int getPreviewLineRes() {
1956            return this.previewLineRes;
1957        }
1958    
1959        public void setPreviewLineRes(int val) {
1960            this.previewLineRes = val;
1961        }
1962    
1963        public int getPreviewEleRes() {
1964            return this.previewEleRes;
1965        }
1966    
1967        public void setPreviewEleRes(int val) {
1968            this.previewEleRes = val;
1969        }
1970    
1971        private String truncateNumericString(String str, int numDec) {
1972            int indx = str.indexOf(".") + numDec + 1;
1973            if (indx >= str.length()) indx = str.length();
1974            return str.substring(0,indx);
1975        }
1976        
1977        public String getLatLonType() {
1978            return TYPE_LATLON;
1979        }
1980    
1981        protected double[][] getLatLonPoints() {
1982            return latLon;
1983        }
1984    
1985        protected double[][] getImagePoints() {
1986            return imageEL;
1987        }
1988    
1989        protected double[][] getAreaPoints() {
1990            return areaEL;
1991        }
1992    
1993        protected double[][] getDisplayELPoints() {
1994            return displayEL;
1995        }
1996    
1997        protected double getBaseLRes() {
1998            return this.baseLRes;
1999        }
2000    
2001        protected double getBaseERes() {
2002            return this.baseERes;
2003        }
2004    
2005        protected void setBaseNumLines(int val) {
2006            this.baseNumLines = (double)val;
2007        }
2008    
2009        public void setDataChoice(DataChoice choice) {
2010            logger.trace("oldChoice={} newChoice={}", this.dataChoice, choice);
2011            this.dataChoice = choice;
2012        }
2013    
2014        public DataChoice getDataChoice() {
2015            return this.dataChoice;
2016        }
2017        
2018        protected void setBaseNumElements(int val) {
2019            this.baseNumElements = (double)val;
2020        }
2021    
2022        public void update(AreaDirectory dir, MapProjection sample, AREAnav nav, 
2023                              String coordType, double[] coords) {
2024            boolean saveLock = getLockOn();
2025            setLockOn(true);
2026            this.maxLines = dir.getLines();
2027            this.maxEles = dir.getElements();
2028            sampleProjection = sample;
2029    
2030            double baseLResOld = getBaseLRes();
2031            double baseEResOld = getBaseERes();
2032            double lDMagOld = getDLineMag();
2033            double eDMagOld = getDElementMag();
2034            int lMagOld = getLineMag();
2035            int eMagOld = getElementMag();
2036            int lSizeOld = getNumLines();
2037            int eSizeOld = getNumEles();
2038            
2039            double baseLResNew = getBLRes();
2040            double baseEResNew = getBERes();
2041            try {
2042                baseLResNew *= (double)(dir.getValue(11));
2043                baseEResNew *= (double)(dir.getValue(12));
2044            } catch (Exception e) {
2045            }
2046    
2047            double lDMagNew = lDMagOld * baseLResOld / baseLResNew;
2048            int lMagNew = (int)Math.ceil(lDMagNew - 0.5);
2049            if (lMagNew > -2) lMagNew = 1;
2050            double eDMagNew = eDMagOld * baseEResOld / baseEResNew;
2051            int eMagNew = (int)Math.ceil(eDMagNew - 0.5);
2052            if (eMagNew > -2) eMagNew = 1;
2053    
2054            double lResOld = Math.abs(lMagOld) * baseLResOld;
2055            double eResOld = Math.abs(eMagOld) * baseEResOld;
2056            double lResNew = Math.abs(lMagNew) * baseLResNew;
2057            double eResNew = Math.abs(eMagNew) * baseEResNew;
2058    
2059            int lSizeNew = (int)Math.floor(((double)lSizeOld * lResOld / lResNew) + 0.5);
2060            if (lSizeNew > this.maxLines) lSizeNew = this.maxLines;
2061            int eSizeNew = (int)Math.floor(((double)eSizeOld * eResOld / eResNew) + 0.5);
2062            if (eSizeNew > this.maxEles) eSizeNew = this.maxEles;        
2063            setNumLines(lSizeNew);
2064            setNumEles(eSizeNew);
2065            
2066            this.baseLRes = baseLResNew;
2067            this.baseERes = baseEResNew;
2068    
2069            amUpdating = true;
2070            amSettingProperties = true;
2071            int newVal = 0;
2072            try {
2073                this.defaultLineMag = lMagNew;
2074                setLRes(lResNew);
2075                newVal = lMagNew;
2076                if (newVal > -2)  newVal = 1;
2077                setLineMag(newVal);
2078                changeLineMagSlider(!lockBtn.isSelected());
2079            } catch (Exception e) {
2080                logger.error("error adjusting line mag slider", e);
2081            }
2082    
2083            try {
2084                this.defaultElementMag = eMagNew;
2085                setERes(eResNew);
2086                newVal = eMagNew;
2087                if (newVal > -2) newVal = 1;
2088            } catch (Exception e) {
2089                logger.error("error adjusting element mag slider", e);
2090            }
2091            amUpdating = false;
2092            amSettingProperties = false;
2093    
2094            int ele = 0;
2095            AREACoordinateSystem macs = (AREACoordinateSystem)sampleProjection;
2096            Rectangle2D mapArea = macs.getDefaultMapArea();
2097            double previewYDim = mapArea.getHeight();
2098            int line = (int)Math.floor(previewYDim);
2099            try {
2100                int lat = (int)Math.floor(getLatitude() + 0.5);
2101                if ((lat <= 90.0) && (lat >= -90.0)) {
2102                    double[][] ll = new double[2][1];
2103                    ll[0][0] = lat;
2104                    ll[1][0] = getLongitude();
2105                    double[][] el = sample.fromReference(ll);
2106                    ele = (int)Math.floor(el[0][0] + 0.5);
2107                    line = (int)Math.floor(el[1][0] + 0.5);
2108                }
2109                this.areaNav = nav;
2110                int areaLinRes = dir.getValue(11);
2111                int areaEleRes = dir.getValue(12);
2112                int startLine = dir.getValue(5);
2113                int startEle = dir.getValue(6);
2114                this.previewDir = dir;
2115                this.areaNav = this.previewNav;
2116                this.areaNav.setRes(areaLinRes, areaEleRes);
2117                this.areaNav.setImageStart(startLine, startEle);
2118                
2119                setCoordinateType(coordType);
2120                if (coordType.equals(TYPE_LATLON)) {
2121                    setLatitude(coords[0]);
2122                    setLongitude(coords[1]);
2123                    convertToLineEle();
2124                } else if (coordType.equals(TYPE_AREA)) {
2125                    double dCoord = coords[0] * baseLResOld/baseLResNew;
2126                    setLine((int)Math.floor(dCoord+0.5));
2127                    dCoord = coords[1] * baseEResOld/baseEResNew;
2128                    setElement((int)Math.floor(dCoord+0.5));
2129                    setLineElement();
2130                    convertToLatLon();
2131                }
2132    
2133            } catch (Exception e) {
2134                logger.error("error updating", e);
2135            }
2136    
2137            try {
2138                rawSizeLbl.setText(" Raw size: " + this.maxLines + " X " + this.maxEles);
2139            } catch (Exception e) {
2140                logger.error("could not update raw size", e);
2141            }
2142    
2143            amUpdating = true;
2144            lineMagSliderChanged(false);
2145            setElementMag(newVal);
2146            elementMagSliderChanged(false);
2147            amUpdating = false;
2148            changeEleMagSlider(!lockBtn.isSelected());
2149            setLockOn(saveLock);
2150            getGeoLocationInfo(line, ele);
2151        }
2152    }