src/org/math/plot/components/LegendPanel.java

00001 package org.math.plot.components;
00002 
00003 import java.awt.*;
00004 import java.awt.event.*;
00005 import java.util.*;
00006 
00007 import javax.swing.*;
00008 
00009 import org.math.plot.*;
00010 import org.math.plot.canvas.*;
00011 import org.math.plot.plots.*;
00012 
00019 public class LegendPanel extends JPanel implements ComponentListener {
00020 
00021         private static final long serialVersionUID = 1L;
00022 
00023         PlotCanvas plotCanvas;
00024 
00025         PlotPanel plotPanel;
00026 
00027         LinkedList<Legend> legends;
00028 
00029         public static int INVISIBLE = -1;
00030 
00031         public static int VERTICAL = 0;
00032 
00033         public static int HORIZONTAL = 1;
00034 
00035         int orientation;
00036 
00037         private int maxHeight;
00038 
00039         private int maxWidth;
00040 
00041         JPanel container;
00042 
00043         private int inset = 5;
00044 
00045         public LegendPanel(PlotPanel _plotPanel, int _orientation) {
00046                 plotPanel = _plotPanel;
00047                 plotCanvas = plotPanel.plotCanvas;
00048                 plotCanvas.attachLegend(this);
00049                 orientation = _orientation;
00050 
00051                 container = new JPanel();
00052                 container.setBackground(plotCanvas.getBackground());
00053                 container.setLayout(new GridLayout(1, 1, inset, inset));
00054                 //container.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.BLACK, 1),null));
00055 
00056                 updateLegends();
00057 
00058                 setBackground(plotCanvas.getBackground());
00059                 addComponentListener(this);
00060                 setLayout(new GridBagLayout());
00061 
00062                 add(container);
00063         }
00064 
00065         public void updateLegends() {
00066                 if (orientation != INVISIBLE) {
00067                         container.removeAll();
00068 
00069                         maxHeight = 1;
00070                         maxWidth = 1;
00071 
00072                         legends = new LinkedList<Legend>();
00073                         for (Plot plot : plotCanvas.getPlots()) {
00074                                 Legend l = new Legend(plot);
00075                                 legends.add(l);
00076 
00077                                 maxWidth = (int) Math.max(maxWidth, l.getPreferredSize().getWidth());
00078                                 maxHeight = (int) Math.max(maxHeight, l.getPreferredSize().getHeight());
00079 
00080                                 container.add(l);
00081                         
00082                         }
00083 
00084                         updateSize();
00085                         //repaint();
00086                 }
00087         }
00088 
00089         private void updateSize() {
00090                 //System.out.println("LegendPanel.updateSize");
00091                 if (orientation == VERTICAL) {
00092                         int nh = 1;
00093                         if (maxHeight < plotCanvas.getHeight())
00094                                 nh = plotCanvas.getHeight() / (maxHeight + inset);
00095                         int nw = 1 + legends.size() / nh;
00096 
00097                         ((GridLayout) (container.getLayout())).setColumns(nw);
00098                         ((GridLayout) (container.getLayout())).setRows(1 + legends.size() / nw);
00099 
00100                         container.setPreferredSize(new Dimension((maxWidth + inset) * nw, (maxHeight + inset) * (1 + legends.size() / nw)));
00101 
00102                 } else if (orientation == HORIZONTAL) {
00103                         int nw = 1;
00104                         if (maxWidth < plotCanvas.getWidth())
00105                                 nw = plotCanvas.getWidth() / (maxWidth + inset);
00106                         int nh = 1 + legends.size() / nw;
00107 
00108                         ((GridLayout) (container.getLayout())).setRows(nh);
00109                         ((GridLayout) (container.getLayout())).setColumns(1 + legends.size() / nh);
00110 
00111                         container.setPreferredSize(new Dimension((maxWidth + inset) * (1 + legends.size() / nh), (maxHeight + inset) * nh));
00112                 }
00113                 container.updateUI();
00114         }
00115 
00116         public void note(int i) {
00117                 if (orientation != INVISIBLE) {
00118                         legends.get(i).setBackground(PlotCanvas.NOTE_COLOR);
00119                         legends.get(i).name.setForeground(plotPanel.getBackground());
00120                 }
00121         }
00122 
00123         public void nonote(int i) {
00124                 if (orientation != INVISIBLE) {
00125                         legends.get(i).setBackground(plotPanel.getBackground());
00126                         legends.get(i).name.setForeground(PlotCanvas.NOTE_COLOR);
00127                 }
00128         }
00129 
00130         public void componentResized(ComponentEvent e) {
00131                 //System.out.println("LegendPanel.componentResized");
00132                 //System.out.println("PlotCanvas : "+plotCanvas.panelSize[0]+" x "+plotCanvas.panelSize[1]);
00133                 if (orientation != INVISIBLE) {
00134                         updateSize();
00135                 }
00136         }
00137 
00138         public void componentMoved(ComponentEvent e) {
00139         }
00140 
00141         public void componentShown(ComponentEvent e) {
00142         }
00143 
00144         public void componentHidden(ComponentEvent e) {
00145         }
00146 
00147         public class Legend extends JPanel {
00148 
00149                 private static final long serialVersionUID = 1L;
00150 
00151                 JPanel color;
00152 
00153                 JLabel name;
00154 
00155                 Plot plot;
00156 
00157                 public Legend(Plot p) {
00158                         plot = p;
00159 
00160                         setLayout(new BorderLayout(2, 2));
00161 
00162                         color = new JPanel();
00163                         name = new JLabel();
00164 
00165                         setBackground(Color.WHITE);
00166 
00167                         update();
00168 
00169                         add(color, BorderLayout.WEST);
00170                         add(name, BorderLayout.CENTER);
00171 
00172                         name.addMouseListener(new MouseListener() {
00173                                 public void mouseClicked(MouseEvent e) {
00174                                         if (e.getModifiers() == MouseEvent.BUTTON1_MASK)
00175                                                 if (plotCanvas.allowEdit && e.getClickCount() > 1)
00176                                                         editText();
00177                                         if (plotCanvas.allowNote && e.getClickCount() <= 1)
00178                                                 note_nonote();
00179                                 }
00180 
00181                                 public void mousePressed(MouseEvent e) {
00182                                 }
00183 
00184                                 public void mouseReleased(MouseEvent e) {
00185                                 }
00186 
00187                                 public void mouseEntered(MouseEvent e) {
00188                                 }
00189 
00190                                 public void mouseExited(MouseEvent e) {
00191                                 }
00192                         });
00193 
00194                         color.addMouseListener(new MouseListener() {
00195                                 public void mouseClicked(MouseEvent e) {
00196                                         if (e.getModifiers() == MouseEvent.BUTTON1_MASK)
00197                                                 if (plotCanvas.allowEdit && e.getClickCount() > 1)
00198                                                         editColor();
00199                                         if (plotCanvas.allowNote && e.getClickCount() <= 1)
00200                                                 note_nonote();
00201                                 }
00202 
00203                                 public void mousePressed(MouseEvent e) {
00204                                 }
00205 
00206                                 public void mouseReleased(MouseEvent e) {
00207                                 }
00208 
00209                                 public void mouseEntered(MouseEvent e) {
00210                                 }
00211 
00212                                 public void mouseExited(MouseEvent e) {
00213                                 }
00214                         });
00215                 }
00216 
00217                 public void editText() {
00218                         String name1 = JOptionPane.showInputDialog(plotCanvas, "Choose name", plot.getName());
00219                         if (name1 != null) {
00220                                 plot.setName(name1);
00221                                 update();
00222                                 updateLegends();
00223                         }
00224                 }
00225 
00226                 public void editColor() {
00227                         Color c = JColorChooser.showDialog(plotCanvas, "Choose plot color", plot.getColor());
00228                         if (c != null) {
00229                                 plot.setColor(c);
00230                                 update();
00231                                 plotCanvas.repaint();
00232                         }
00233                 }
00234 
00235                 public void update() {
00236                         int size = name.getFont().getSize();
00237                         color.setSize(new Dimension(size, size));
00238                         color.setPreferredSize(new Dimension(size, size));
00239 
00240                         // TODO change legend when plot is invisible
00241                         /*if (!plot.visible)
00242                                 color.setBackground(Color.LIGHT_GRAY);
00243                         else*/
00244                                 color.setBackground(plot.getColor());
00245 
00246                         /*if (!plot.visible) {
00247                                 name.setFont(name.getFont().deriveFont(Font.ITALIC));
00248                                 name.setForeground(Color.LIGHT_GRAY);
00249                         }*/
00250                         name.setText(plot.getName());
00251                         repaint();
00252                 }
00253 
00254                 public void note_nonote() {
00255                         plot.noted = !plot.noted;
00256                         plotCanvas.repaint();
00257                 }
00258 
00259         }
00260 
00261 }

Generated on Wed Sep 5 21:44:01 2007 for jmathplot by  doxygen 1.5.1