LegendPanel.java
Go to the documentation of this file.
1 package com.generalrobotix.ui.view.graph;
2 
3 import java.awt.Dimension;
4 import java.util.ArrayList;
5 import java.util.ListIterator;
6 
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.events.PaintEvent;
9 import org.eclipse.swt.events.PaintListener;
10 import org.eclipse.swt.graphics.Color;
11 import org.eclipse.swt.graphics.Font;
12 import org.eclipse.swt.graphics.FontMetrics;
13 import org.eclipse.swt.widgets.Canvas;
14 import org.eclipse.swt.widgets.Composite;
15 
17 
22  public class LegendPanel extends Canvas implements PaintListener{
23 
24  // -----------------------------------------------------------------
25  // 定数
26  private static final int MARGIN_X = 15;
27  private static final int MARGIN_Y = 15;
28  private static final int GAP_X = 10;
29  private static final int GAP_Y = 5;
30  private static final int LEN_LINE = 20;
31 
32  // -----------------------------------------------------------------
33  // インスタンス変数
34  private ArrayList<LegendInfo> legendList_; // 凡例系列リスト
35  private Font font_; // ラベルフォント
36  private Color backColor_; // 背景色
37  private Color labelColor_; // ラベル色
38  private Dimension size_; // パネルサイズ
39 
40  // -----------------------------------------------------------------
41  // コンストラクタ
49  public LegendPanel(
50  Composite parent,
51  Font font,
52  Color backColor,
53  Color labelColor
54  ) {
55  super(parent, SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
56  font_ = font;
57  backColor_ = backColor;
58  labelColor_ = labelColor;
59  size_ = new Dimension(0, 0);
60  //setPreferredSize(size_);
61  legendList_ = new ArrayList<LegendInfo>();
62 
63  addPaintListener(this);
64  }
65 
71  public void addLegend(
72  LegendInfo legend
73  ) {
74  legendList_.add(legend);
75  updateSize();
76  }
77 
83  public void removeLegend(
84  LegendInfo legend
85  ) {
86  int ind = legendList_.indexOf(legend);
87  legendList_.remove(ind);
88  updateSize();
89  }
90 
96  public void setFont(
97  Font font
98  ) {
99  font_ = font;
100  }
101 
107  public void setBackColor(
108  Color color
109  ) {
110  backColor_ = color;
111  }
112 
118  public void setLabelColor(
119  Color color
120  ) {
121  labelColor_ = color;
122  }
123 
129  public void paintControl(PaintEvent e) {
130  // 背景
131  int width = getSize().x;
132  int height = getSize().y;
133  e.gc.setBackground(backColor_);
134  e.gc.fillRectangle(0, 0, width, height);
135  // 凡例
136  e.gc.setFont(font_);
137  FontMetrics metrics = e.gc.getFontMetrics(); // フォントメトリクス
138  int yofs = (int)(metrics.getHeight() / 3.5); // ラベルYオフセット
139  int ygap = metrics.getHeight() + GAP_Y; // Y間隔
140  ListIterator<LegendInfo> li = legendList_.listIterator();
141  int ypos = MARGIN_Y; // Y位置初期化
142  while (li.hasNext()) { // 全凡例をループ
143  LegendInfo legend = (LegendInfo)li.next(); // 次の凡例
144  e.gc.setForeground(legend.color); // 凡例線色
145  //g.drawLine(MARGIN_X, ypos - 1, MARGIN_X + LEN_LINE, ypos - 1);
146  e.gc.drawLine(MARGIN_X, ypos, MARGIN_X + LEN_LINE, ypos); // 線の描画
147  //g.drawLine(MARGIN_X, ypos + 1, MARGIN_X + LEN_LINE, ypos + 1);
148  e.gc.setForeground(labelColor_); // ラベル色
149  e.gc.drawString( // ラベルの描画
150  legend.label,
151  MARGIN_X + LEN_LINE + GAP_X,
152  ypos + yofs
153  );
154  ypos += ygap; // Y位置の更新
155  }
156  }
157 
164  public Dimension getMinimalSize() {
165  return size_;
166  }
167 
173  private void updateSize() {
174  //TODO hattori
175  //FontMetrics metrics = this.getgetFontMetrics(); // フォントメトリクス
176  int ygap = 0;//metrics.getHeight() + GAP_Y; // Y間隔
177  ListIterator<LegendInfo> li = legendList_.listIterator();
178  int ysize = MARGIN_Y; // 高さ
179  int max = 0; // ラベル最大長
180  while (li.hasNext()) { // 全凡例をループ
181  LegendInfo legend = (LegendInfo)li.next();
182  //TODO hattori
183  int len = 0;//metrics.stringWidth(legend.label); // ラベルの長さを取得
184  if (len > max) { // 最大長?
185  max = len; // 最大長を更新
186  }
187  if (li.hasNext()) { // 最後の凡例でない?
188  ysize += ygap; // 高さを更新
189  }
190  }
191  ysize += MARGIN_Y; // 下マージン
192  size_.width = MARGIN_X + LEN_LINE + GAP_X + max + MARGIN_X; // 幅更新
193  size_.height = ysize; // 高さ更新
194  //System.out.println("ygap = " + ygap);
195  //System.out.println("(" + size_.width + ", " + size_.height + ")");
196  }
197 }
com.generalrobotix.ui.view.graph.LegendPanel.LEN_LINE
static final int LEN_LINE
Definition: LegendPanel.java:30
width
png_infop png_uint_32 * width
Definition: png.h:2306
com.generalrobotix.ui.view
com.generalrobotix.ui.view.graph.LegendPanel.paintControl
void paintControl(PaintEvent e)
Definition: LegendPanel.java:129
com.generalrobotix.ui.view.graph.LegendPanel.backColor_
Color backColor_
Definition: LegendPanel.java:36
com.generalrobotix.ui.view.graph.LegendPanel.GAP_Y
static final int GAP_Y
Definition: LegendPanel.java:29
com.generalrobotix.ui.view.graph.LegendPanel.setBackColor
void setBackColor(Color color)
Definition: LegendPanel.java:107
hrp::max
static int max(int a, int b)
Definition: MatrixSolvers.cpp:54
com.generalrobotix.ui.view.graph.LegendInfo.label
String label
Definition: LegendInfo.java:14
com.generalrobotix.ui.view.graph.LegendPanel.updateSize
void updateSize()
Definition: LegendPanel.java:173
com.generalrobotix.ui.view.graph
Definition: AttributeInfo.java:10
com.generalrobotix.ui.view.graph.LegendPanel.labelColor_
Color labelColor_
Definition: LegendPanel.java:37
com.generalrobotix.ui.view.graph.LegendInfo.color
Color color
Definition: LegendInfo.java:13
int
typedef int
Definition: png.h:1111
com.generalrobotix.ui.view.graph.LegendPanel
Definition: LegendPanel.java:22
com.generalrobotix.ui.view.graph.LegendPanel.setFont
void setFont(Font font)
Definition: LegendPanel.java:96
com.generalrobotix.ui.view.graph.LegendPanel.removeLegend
void removeLegend(LegendInfo legend)
Definition: LegendPanel.java:83
com.generalrobotix.ui.view.graph.LegendPanel.MARGIN_Y
static final int MARGIN_Y
Definition: LegendPanel.java:27
com.generalrobotix.ui.view.graph.LegendPanel.setLabelColor
void setLabelColor(Color color)
Definition: LegendPanel.java:118
com.generalrobotix
com.generalrobotix.ui.view.graph.LegendPanel.MARGIN_X
static final int MARGIN_X
Definition: LegendPanel.java:26
com.generalrobotix.ui.view.graph.LegendPanel.LegendPanel
LegendPanel(Composite parent, Font font, Color backColor, Color labelColor)
Definition: LegendPanel.java:49
com.generalrobotix.ui.view.graph.LegendPanel.getMinimalSize
Dimension getMinimalSize()
Definition: LegendPanel.java:164
com
com.generalrobotix.ui.view.graph.LegendPanel.font_
Font font_
Definition: LegendPanel.java:35
height
png_infop png_uint_32 png_uint_32 * height
Definition: png.h:2306
com.generalrobotix.ui.view.graph.LegendInfo
Definition: LegendInfo.java:9
com.generalrobotix.ui.view.graph.LegendPanel.GAP_X
static final int GAP_X
Definition: LegendPanel.java:28
com.generalrobotix.ui
com.generalrobotix.ui.view.graph.LegendPanel.legendList_
ArrayList< LegendInfo > legendList_
Definition: LegendPanel.java:34
com.generalrobotix.ui.view.graph.LegendPanel.size_
Dimension size_
Definition: LegendPanel.java:38
com.generalrobotix.ui.view.graph.LegendPanel.addLegend
void addLegend(LegendInfo legend)
Definition: LegendPanel.java:71


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Wed Sep 7 2022 02:51:03