3 import java.awt.AlphaComposite;
\r
4 import java.awt.Color;
\r
5 import java.awt.Dimension;
\r
6 import java.awt.Font;
\r
7 import java.awt.Graphics;
\r
8 import java.awt.Graphics2D;
\r
9 import java.awt.RenderingHints;
\r
10 import java.awt.font.FontRenderContext;
\r
11 import java.awt.font.LineBreakMeasurer;
\r
12 import java.awt.font.TextAttribute;
\r
13 import java.awt.font.TextLayout;
\r
14 import java.awt.image.BufferedImage;
\r
15 import java.text.AttributedCharacterIterator;
\r
16 import java.text.AttributedString;
\r
17 import java.util.ArrayList;
\r
19 import javax.swing.JButton;
\r
20 import javax.swing.JLabel;
\r
23 public class JTXTButton extends JLabel {
\r
25 private static final long serialVersionUID = 1L;
\r
27 /*******************************************************************************
\r
29 ******************************************************************************/
\r
34 public static enum FontStyle {
\r
39 private String name;
\r
41 private FontStyle(String name) {
\r
46 public String toString() {
\r
51 public String getId() {
\r
52 return super.toString();
\r
55 public static FontStyle get(String id) {
\r
56 for ( FontStyle fs : FontStyle.values() ) {
\r
57 if ( fs.getId().equals(id) ) {
\r
65 private static final float DRAWTAB = 2.0F;
\r
68 /*******************************************************************************
\r
70 ******************************************************************************/
\r
73 private BufferedImage image = null; // ビットマップ
\r
75 private int vrow; // 仮想座標縦位置
\r
76 private int vcolumn; // 仮想座標横位置
\r
77 private int vheight; // 仮想座標高さ
\r
78 private int vwidth; // 仮想座標幅
\r
81 private ProgDetailList tvd = null; // 番組情報そのまま
\r
84 private static boolean showStart = true;
\r
85 private static boolean splitEpno = false;
\r
86 private static boolean showDetail = true;
\r
87 private static float detailTab = 2.0F;
\r
88 private static int detailRows = 3;
\r
90 private static Font defaultFont = new JLabel().getFont();
\r
91 private static Font titleFont = defaultFont;
\r
92 private static int titleFontSize = defaultFont.getSize();
\r
93 private static Color titleFontColor = Color.BLUE;
\r
94 private static int titleFontStyle = Font.BOLD;
\r
95 private static boolean titleFontUL = true;
\r
96 private static Font detailFont = defaultFont;
\r
97 private static int detailFontSize = defaultFont.getSize();
\r
98 private static Color detailFontColor = Color.DARK_GRAY;
\r
99 private static int detailFontStyle = defaultFont.getStyle();
\r
100 private static boolean detailFontUL = false;
\r
101 private static Object aahint = RenderingHints.VALUE_TEXT_ANTIALIAS_ON;
\r
103 private static int columnWidth = 0;
\r
104 private static float heightMultiplier = 0;
\r
106 /*******************************************************************************
\r
108 ******************************************************************************/
\r
113 /*******************************************************************************
\r
115 ******************************************************************************/
\r
118 // setVisible(false)するとリソースが解放されてしまうのか再描画に時間がかかるようになるので表示範囲外に出して隠してしまう
\r
119 public void clean() {
\r
122 setBounds(-1,-1,0,0);
\r
126 public void forceRepaint() {
\r
132 //public void setVRow(int n) { vrow = n; }
\r
133 public int getVRow() { return vrow; }
\r
134 //public void setVColumn(int n) { vcolumn = n; }
\r
135 public int getVColumn() { return vcolumn; }
\r
136 //public void setVHeight(int n) { vheight = n; }
\r
137 public int getVHeight() { return vheight; }
\r
139 public static void setColumnWidth(int n) { columnWidth = n; }
\r
140 public static void setHeightMultiplier(float f) { heightMultiplier = f; }
\r
142 public void setVBounds(int x, int y, int width, int height) {
\r
148 vcolumn*columnWidth,
\r
149 (int) Math.ceil(((float)vrow)*heightMultiplier),
\r
150 vwidth*columnWidth,
\r
151 (int) Math.ceil(((float)vheight)*heightMultiplier));
\r
154 public void reVBounds() {
\r
156 vcolumn*columnWidth,
\r
157 (int) Math.ceil(((float)vrow)*heightMultiplier),
\r
158 vwidth*columnWidth,
\r
159 (int) Math.ceil(((float)vheight)*heightMultiplier));
\r
163 public void setInfo(ProgDetailList tvd) {
\r
165 this.setText(null); // 簡易表示時代の名残
\r
167 this.setVerticalAlignment(JButton.TOP);
\r
168 this.setHorizontalAlignment(JButton.LEFT);
\r
169 //this.setBorder(new LineBorder(Color.BLACK,1));
\r
170 this.setOpaque(true);
\r
172 public ProgDetailList getInfo() {
\r
176 // 予約待機枠を表示するかどうかの確認
\r
177 public boolean isStandby() { return tvd.marked && tvd.showinstandby; }
\r
180 public static void setShowStart(boolean b) {
\r
183 public static void setSplitEpno(boolean b) {
\r
186 public static void setShowDetail(boolean b) {
\r
189 public static void setDetailTab(float n) {
\r
192 public static void setDetailRows(int n) {
\r
197 public static void setTitleFont(String fn) {
\r
198 if ( fn != null && ! fn.equals("") ) {
\r
199 Font f = new Font(fn,titleFontStyle,titleFontSize);
\r
205 //titleFont = this.getFont();
\r
207 public static void setTitleFontSize(int n) {
\r
209 titleFont = titleFont.deriveFont((float)titleFontSize);
\r
211 public static void setTitleFontColor(Color c) {
\r
212 titleFontColor = c;
\r
214 public static void setDetailFont(String fn) {
\r
215 if ( fn != null && ! fn.equals("") ) {
\r
216 Font f = new Font(fn,detailFontStyle,detailFontSize);
\r
222 //detailFont = new JLabel().getFont();
\r
224 public static void setDetailFontSize(int n) {
\r
225 detailFontSize = n;
\r
226 detailFont = detailFont.deriveFont((float)detailFontSize);
\r
228 public static void setDetailFontColor(Color c) {
\r
229 detailFontColor = c;
\r
233 public static void setTitleFontStyle(ArrayList<FontStyle> fsa) {
\r
234 setTmpFontStyle(fsa);
\r
235 titleFontStyle = tmpFontStyle;
\r
236 titleFontUL = tmpFontUL;
\r
237 titleFont = titleFont.deriveFont((titleFont.getStyle() & ~(Font.BOLD|Font.ITALIC)) | titleFontStyle);
\r
239 public static void setDetailFontStyle(ArrayList<FontStyle> fsa) {
\r
240 setTmpFontStyle(fsa);
\r
241 detailFontStyle = tmpFontStyle;
\r
242 detailFontUL = tmpFontUL;
\r
243 detailFont = detailFont.deriveFont((detailFont.getStyle() & ~(Font.BOLD|Font.ITALIC)) | detailFontStyle);
\r
245 private static void setTmpFontStyle(ArrayList<FontStyle> fsa) {
\r
248 for ( FontStyle fs : fsa ) {
\r
251 tmpFontStyle |= Font.BOLD;
\r
254 tmpFontStyle |= Font.ITALIC;
\r
262 private static int tmpFontStyle;
\r
263 private static boolean tmpFontUL;
\r
266 public static void setAAHint(Object o) {
\r
271 /*******************************************************************************
\r
273 ******************************************************************************/
\r
279 protected void paintComponent(Graphics g) {
\r
281 super.paintComponent(g);
\r
284 if (image == null) {
\r
286 Dimension d = getSize();
\r
287 int imgw = d.width;
\r
288 int imgh = d.height;
\r
290 float draww = (float)imgw-DRAWTAB*2.0F;
\r
291 float drawh = (float)imgh;
\r
292 float detailw = draww-detailTab;
\r
294 image = new BufferedImage(imgw, imgh, BufferedImage.TYPE_INT_ARGB);
\r
295 Graphics2D g2 = (Graphics2D)image.createGraphics();
\r
297 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,aahint); // アンチエイリアスの設定
\r
298 g2.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_SPEED);
\r
299 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
\r
301 float baseline = 0.0F;
\r
304 if (showStart && tvd.start != null && tvd.start.length() > 0) {
\r
306 Font fs = detailFont;
\r
307 String sStr = tvd.start+" "+tvd.extension_mark;
\r
309 Font f = fs.deriveFont(fs.getStyle() | Font.BOLD);
\r
310 AttributedString as = new AttributedString(sStr);
\r
311 as.addAttribute(TextAttribute.FONT, f);
\r
312 as.addAttribute(TextAttribute.FOREGROUND, Color.BLACK, 0, 5);
\r
313 if (sStr.length() > 6) {
\r
314 as.addAttribute(TextAttribute.FOREGROUND, Color.RED, 6, sStr.length());
\r
316 AttributedCharacterIterator ac = as.getIterator();
\r
317 FontRenderContext fc = g2.getFontRenderContext();
\r
318 LineBreakMeasurer m = new LineBreakMeasurer(ac,fc);
\r
319 while ( m.getPosition() < sStr.length() ) {
\r
320 TextLayout tl = m.nextLayout(draww);
\r
321 baseline += tl.getAscent();
\r
322 tl.draw(g2, DRAWTAB, baseline);
\r
323 baseline += tl.getDescent() + tl.getLeading();
\r
328 String title = ( splitEpno ) ? tvd.splitted_title : tvd.title;
\r
329 if (title.length() > 0) {
\r
332 if (showStart && tvd.start.length() > 0) {
\r
333 aMark = tvd.prefix_mark + tvd.newlast_mark;
\r
336 if (tvd.start.length() > 0) {
\r
337 aMark = tvd.extension_mark + tvd.prefix_mark + tvd.newlast_mark;
\r
340 aMark = tvd.prefix_mark + tvd.newlast_mark;
\r
343 String tStr = aMark+title+tvd.postfix_mark;
\r
345 AttributedString as = new AttributedString(tStr);
\r
346 as.addAttribute(TextAttribute.FONT, titleFont);
\r
349 as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_ONE_PIXEL, aMark.length(), aMark.length()+title.length());
\r
351 as.addAttribute(TextAttribute.FOREGROUND, titleFontColor, aMark.length(), tStr.length());
\r
352 if (aMark.length() > 0) {
\r
353 as.addAttribute(TextAttribute.FOREGROUND, Color.RED, 0, aMark.length());
\r
356 AttributedCharacterIterator ac = as.getIterator();
\r
357 FontRenderContext fc = g2.getFontRenderContext();
\r
358 LineBreakMeasurer m = new LineBreakMeasurer(ac,fc);
\r
359 while (m.getPosition() < tStr.length()) {
\r
360 TextLayout tl = m.nextLayout(draww);
\r
361 baseline += tl.getAscent();
\r
362 tl.draw(g2, DRAWTAB, baseline);
\r
363 baseline += tl.getDescent() + tl.getLeading();
\r
368 if ( showDetail ) {
\r
371 detail = tvd.splitted_detail;
\r
374 detail = tvd.detail;
\r
376 if ( detail.length() > 0 ) {
\r
377 AttributedString as = new AttributedString(detail);
\r
378 as.addAttribute(TextAttribute.FONT, detailFont);
\r
379 if (detailFontUL) {
\r
380 as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_ONE_PIXEL);
\r
382 as.addAttribute(TextAttribute.FOREGROUND, detailFontColor);
\r
383 AttributedCharacterIterator ac = as.getIterator();
\r
384 FontRenderContext fc = g2.getFontRenderContext();
\r
385 LineBreakMeasurer m = new LineBreakMeasurer(ac,fc);
\r
386 for ( int row=0; m.getPosition()<detail.length() && baseline<=drawh && (detailRows>0 && row<detailRows); row++ ) {
\r
387 TextLayout tl = m.nextLayout(detailw);
\r
388 baseline += tl.getAscent();
\r
389 tl.draw(g2, (DRAWTAB+detailTab), baseline);
\r
390 baseline += tl.getDescent() + tl.getLeading();
\r
397 g.drawImage(image, 0, 0, this);
\r