Go to the documentation of this file.00001 package com.generalrobotix.ui.view.graph;
00002
00003 import java.awt.Dimension;
00004 import java.util.ArrayList;
00005 import java.util.ListIterator;
00006
00007 import org.eclipse.swt.SWT;
00008 import org.eclipse.swt.events.PaintEvent;
00009 import org.eclipse.swt.events.PaintListener;
00010 import org.eclipse.swt.graphics.Color;
00011 import org.eclipse.swt.graphics.Font;
00012 import org.eclipse.swt.graphics.FontMetrics;
00013 import org.eclipse.swt.widgets.Canvas;
00014 import org.eclipse.swt.widgets.Composite;
00015
00016 import com.generalrobotix.ui.view.graph.LegendInfo;
00017
00022 public class LegendPanel extends Canvas implements PaintListener{
00023
00024
00025
00026 private static final int MARGIN_X = 15;
00027 private static final int MARGIN_Y = 15;
00028 private static final int GAP_X = 10;
00029 private static final int GAP_Y = 5;
00030 private static final int LEN_LINE = 20;
00031
00032
00033
00034 private ArrayList<LegendInfo> legendList_;
00035 private Font font_;
00036 private Color backColor_;
00037 private Color labelColor_;
00038 private Dimension size_;
00039
00040
00041
00049 public LegendPanel(
00050 Composite parent,
00051 Font font,
00052 Color backColor,
00053 Color labelColor
00054 ) {
00055 super(parent, SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
00056 font_ = font;
00057 backColor_ = backColor;
00058 labelColor_ = labelColor;
00059 size_ = new Dimension(0, 0);
00060
00061 legendList_ = new ArrayList<LegendInfo>();
00062
00063 addPaintListener(this);
00064 }
00065
00071 public void addLegend(
00072 LegendInfo legend
00073 ) {
00074 legendList_.add(legend);
00075 updateSize();
00076 }
00077
00083 public void removeLegend(
00084 LegendInfo legend
00085 ) {
00086 int ind = legendList_.indexOf(legend);
00087 legendList_.remove(ind);
00088 updateSize();
00089 }
00090
00096 public void setFont(
00097 Font font
00098 ) {
00099 font_ = font;
00100 }
00101
00107 public void setBackColor(
00108 Color color
00109 ) {
00110 backColor_ = color;
00111 }
00112
00118 public void setLabelColor(
00119 Color color
00120 ) {
00121 labelColor_ = color;
00122 }
00123
00129 public void paintControl(PaintEvent e) {
00130
00131 int width = getSize().x;
00132 int height = getSize().y;
00133 e.gc.setBackground(backColor_);
00134 e.gc.fillRectangle(0, 0, width, height);
00135
00136 e.gc.setFont(font_);
00137 FontMetrics metrics = e.gc.getFontMetrics();
00138 int yofs = (int)(metrics.getHeight() / 3.5);
00139 int ygap = metrics.getHeight() + GAP_Y;
00140 ListIterator<LegendInfo> li = legendList_.listIterator();
00141 int ypos = MARGIN_Y;
00142 while (li.hasNext()) {
00143 LegendInfo legend = (LegendInfo)li.next();
00144 e.gc.setForeground(legend.color);
00145
00146 e.gc.drawLine(MARGIN_X, ypos, MARGIN_X + LEN_LINE, ypos);
00147
00148 e.gc.setForeground(labelColor_);
00149 e.gc.drawString(
00150 legend.label,
00151 MARGIN_X + LEN_LINE + GAP_X,
00152 ypos + yofs
00153 );
00154 ypos += ygap;
00155 }
00156 }
00157
00164 public Dimension getMinimalSize() {
00165 return size_;
00166 }
00167
00173 private void updateSize() {
00174
00175
00176 int ygap = 0;
00177 ListIterator<LegendInfo> li = legendList_.listIterator();
00178 int ysize = MARGIN_Y;
00179 int max = 0;
00180 while (li.hasNext()) {
00181 LegendInfo legend = (LegendInfo)li.next();
00182
00183 int len = 0;
00184 if (len > max) {
00185 max = len;
00186 }
00187 if (li.hasNext()) {
00188 ysize += ygap;
00189 }
00190 }
00191 ysize += MARGIN_Y;
00192 size_.width = MARGIN_X + LEN_LINE + GAP_X + max + MARGIN_X;
00193 size_.height = ysize;
00194
00195
00196 }
00197 }