001    package ui.renderer;
002    
003    import astronomy.data.spectra.*;
004    import java.awt.Component;
005    import java.util.*;
006    import javax.swing.*;
007    import javax.swing.tree.*;
008    import javax.swing.event.*;
009    
010    public class SpectraTreeCellRenderer extends DefaultTreeCellRenderer {
011    
012      public void SpectraTreeCellRenderer() {
013      }
014    
015      public Component getTreeCellRendererComponent(JTree aTree, Object anObject, boolean isSelected, boolean isExpanded, boolean isLeaf, int aRow, boolean hasFocus) {
016        super.getTreeCellRendererComponent(aTree, anObject, isSelected, isExpanded, isLeaf, aRow, hasFocus);
017    
018        // look up the classes and their corresponding icons and text representations
019        if (anObject instanceof Spectradatabase) {
020          //Spectradatabase sd = (Spectradatabase) aParent;
021          setText("spectra database");
022    
023        } else if (anObject instanceof SpectradatabaseType.TypeType) {
024          SpectradatabaseType.TypeType type = (SpectradatabaseType.TypeType) anObject;
025          setText(type.getName());
026    
027        } else if (anObject instanceof SpectradatabaseType.TypeType.SourceType) {
028    
029          SpectradatabaseType.TypeType.SourceType source = (SpectradatabaseType.TypeType.SourceType) anObject;
030          setText(source.getReference().getAdscode());
031          setIcon(ICON_PUBLICATION);
032          //TODO: Set ToolTip to author list, institution etc...
033    
034        } else if (anObject instanceof SpectraType) {
035          SpectraType spectraType = (SpectraType) anObject;
036          List ids = spectraType.getId();
037          setText( ((IdType) ids.get(0)).getSpectraname().get(0).toString());
038    
039        } else if (anObject instanceof PlotType) {
040          PlotType plot = (PlotType) anObject;
041          String label = null;
042          // setToolTip("page " + plot.getPage() ", " + plot.getResolution() + ", " + plot.getExposure());
043          List dateList = plot.getDate();
044          if ( dateList != null && dateList.size() > 0) {
045            Calendar date = (Calendar) dateList.get(0); // show first date only
046            // TODO: fix formatting of date such as adding leading zero to single digits etc...
047            label= date.get(Calendar.YEAR) + "-" + (date.get(Calendar.MONTH)+1) + "-" + date.get(Calendar.DATE);
048          }
049          if (label == null || label.length() == 0)
050            label = "page " + Long.toString(plot.getPage());
051          if (label == null || label.length() == 0)
052            label = plot.getPlotrange().getStartwavelength() + " - " + plot.getPlotrange().getEndwavelength();
053          setText(label);
054          setIcon(ICON_SPECTRA);
055    
056        } else if (anObject instanceof PlotType.PlotdataType) {
057          PlotType.PlotdataType data = (PlotType.PlotdataType) anObject;
058          String label = "plot";
059          if (data.getImagefile() != null) label += " [image]";
060          if (data.getPlotfile() != null) label += " [data]";
061          setText(label);
062          setIcon(ICON_PLOT);
063    
064        } else if(anObject instanceof PlotType.PlotdataType.PlotfileType) {
065          PlotType.PlotdataType.PlotfileType plotFile = (PlotType.PlotdataType.PlotfileType) anObject;
066          setText(plotFile.getName());
067          //setToolTip(plotFile.getFormat());
068          setIcon(ICON_DATA);
069    
070        } else if(anObject instanceof PlotType.PlotdataType.ImagefileType) {
071          PlotType.PlotdataType.ImagefileType imageFile = (PlotType.PlotdataType.ImagefileType) anObject;
072          setText(imageFile.getName());
073          //setToolTip(imageFile.getFormat());
074          setIcon(ICON_IMAGE);
075    
076        } else if(anObject instanceof LineType.TransitionType) {
077          LineType.TransitionType transition = (LineType.TransitionType) anObject;
078          //TODO: Format getWavelength() as XXXX.XX
079          String terms = transition.getLower() + " - " + transition.getUpper();
080          setText("<html>" + transition.getIon() + " " + transition.getWavelength() + " " + terms);
081          // BUG: ToolTips don't work because renderer is a 'rubber-stamp' not a true component
082          setToolTipText(terms);
083          setIcon(ICON_TRANSITION);
084          //TODO: Too much vertical space allocated for exponents, reduce the height
085    
086        } else if(anObject instanceof SpectraType.EmissionlineType) {
087          SpectraType.EmissionlineType line = (SpectraType.EmissionlineType) anObject;
088          float wavelength = line.getWavelength();
089          setText(Float.toString(wavelength));
090          setIcon(LineIcon.getIcon(wavelength, true));
091    
092        } else if(anObject instanceof SpectraType.AbsorptionlineType) {
093          SpectraType.AbsorptionlineType line = (SpectraType.AbsorptionlineType) anObject;
094          float wavelength = line.getWavelength();
095          setText(Float.toString(wavelength));
096          setIcon(LineIcon.getIcon(wavelength, false));
097    
098        } else if(anObject instanceof SpectraType.DiscontinuityType) {
099          SpectraType.DiscontinuityType discontinuity = (SpectraType.DiscontinuityType) anObject;
100          float wavelength = discontinuity.getWavelength();
101          setText(Float.toString(wavelength));
102    
103          if (discontinuity.isEmission()) {
104            setIcon(ICON_EMISSION_DISCONTINUITY);
105          } else {
106            setIcon(ICON_ABSORPTION_DISCONTINUITY);
107          }
108        }
109        return this;
110      }
111    
112      public static final Icon ICON_SPECTRA= getImageIcon("images/spectra.gif");
113      public static final Icon ICON_PLOT   = getImageIcon("images/plot.gif");
114      public static final Icon ICON_IMAGE  = getImageIcon("images/image.gif");
115      public static final Icon ICON_DATA   = getImageIcon("images/data.gif");
116      public static final Icon ICON_ABSORPTION_DISCONTINUITY = getImageIcon("images/absorption-discontinuity.gif");
117      public static final Icon ICON_EMISSION_DISCONTINUITY   = getImageIcon("images/emission-discontinuity.gif");
118      public static final Icon ICON_PUBLICATION= getImageIcon("images/publication.gif");
119      public static final Icon ICON_TRANSITION = getImageIcon("images/transition.gif");
120    
121      public static ImageIcon getImageIcon(String fileName) {
122        // TODO: handle case when images are a part of a resource such as a JAR file etc...
123        return new ImageIcon(fileName);
124      }
125    
126    }