LegendPanel.java
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         //setPreferredSize(size_);
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);    // ラベルYオフセット
00139         int ygap = metrics.getHeight() + GAP_Y;         // Y間隔
00140         ListIterator<LegendInfo> li = legendList_.listIterator();
00141         int ypos = MARGIN_Y;    // Y位置初期化
00142         while (li.hasNext()) {  // 全凡例をループ
00143             LegendInfo legend = (LegendInfo)li.next();  // 次の凡例
00144             e.gc.setForeground(legend.color);   // 凡例線色
00145             //g.drawLine(MARGIN_X, ypos - 1, MARGIN_X + LEN_LINE, ypos - 1);
00146             e.gc.drawLine(MARGIN_X, ypos, MARGIN_X + LEN_LINE, ypos);  // 線の描画
00147             //g.drawLine(MARGIN_X, ypos + 1, MARGIN_X + LEN_LINE, ypos + 1);
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;   // Y位置の更新
00155         }
00156     }
00157 
00164     public Dimension getMinimalSize() {
00165         return size_;
00166     }
00167 
00173     private void updateSize() {
00174         //TODO hattori
00175         //FontMetrics metrics = this.getgetFontMetrics();    // フォントメトリクス
00176         int ygap = 0;//metrics.getHeight() + GAP_Y; // Y間隔
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             //TODO hattori
00183             int len = 0;//metrics.stringWidth(legend.label);    // ラベルの長さを取得
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         //System.out.println("ygap = " + ygap);
00195         //System.out.println("(" + size_.width + ", " + size_.height + ")");
00196     }
00197 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:17