OSDN Git Service

Tableの背景色の設定が即座に反映されるように修正
[nt-manager/nt-manager.git] / src / twitter / gui / component / TweetCommentRenderer.java
1 package twitter.gui.component;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.Desktop;
6 import java.awt.Point;
7 import java.awt.event.MouseEvent;
8 import java.awt.event.MouseListener;
9 import java.awt.event.MouseMotionListener;
10 import java.net.URL;
11
12 import javax.swing.BorderFactory;
13 import javax.swing.JEditorPane;
14 import javax.swing.JTable;
15 import javax.swing.table.TableCellRenderer;
16 import javax.swing.text.Style;
17 import javax.swing.text.StyleConstants;
18 import javax.swing.text.html.HTMLDocument;
19 import javax.swing.text.html.StyleSheet;
20 import twitter.gui.action.TweetMainAction;
21
22 /**
23  *
24  * @author nishio
25  *
26  */
27 public class TweetCommentRenderer extends JEditorPane implements
28                 TableCellRenderer, MouseListener, MouseMotionListener {
29
30         private int row = -1;
31         private int col = -1;
32         // あたらしく取得したセルが何行目までか
33         private int newTableRow = -1;
34         //main action
35         private TweetMainAction mainAction = null;
36
37         /**
38          *
39          */
40         public TweetCommentRenderer(TweetMainAction mainAction) {
41                 super();
42                 this.mainAction = mainAction;
43                 // setLineWrap(true);
44                 setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
45                 // HTMLコードをそのまま表示できるようにする
46                 super.setEditable(false);
47                 this.setContentType("text/html");
48         }
49
50         // @Override
51         public Component getTableCellRendererComponent(JTable table, Object value,
52                         boolean isSelected, boolean hasFocus, int row, int column) {
53                 /*
54                  * if (isSelected) { setForeground(table.getSelectionForeground());
55                  * setBackground(table.getSelectionBackground()); } else {
56                  * setForeground(table.getForeground());
57                  * setBackground(table.getBackground()); }
58                  */
59
60
61                 //一行ずつTableの色を変更する
62                 //TODO: ここも後で色を変更できるようにする
63                 if( row % 2 == 0 ) {
64                         setBackground(new Color(240,240,255));
65                 }else {
66                         setBackground(Color.white);
67                 }
68
69                 // TODO: あとでここのカラーを変える
70                 // NewCell
71                 if( column >= 2 ) {
72                         if (this.newTableRow >= 0 && row < this.newTableRow) {
73                                 setBackground(this.mainAction.getNewTableColor());
74                         } else {
75                                 setBackground(Color.white);
76                         }
77                 }
78
79                 // フォントを変更
80                 setFont(table.getFont());
81                 try {
82                         // htmlフォント変更
83                         HTMLDocument doc = (HTMLDocument) getDocument();
84                         StyleSheet[] style = doc.getStyleSheet().getStyleSheets();
85                         for (int i = style.length - 1; i >= 0; i--) {
86                                 Style body = style[i].getStyle("body");
87                                 if (body != null) {
88                                         StyleConstants.setFontFamily(body, table.getFont()
89                                                         .getFontName());
90                                         StyleConstants.setFontSize(body, table.getFont().getSize());
91                                 }
92                         }
93                 } catch (Exception e) {
94                         e.printStackTrace();
95                 }
96
97                 setText((value == null) ? "" : value.toString());
98
99                 if (!table.isEditing() && this.row == row && this.col == column) {
100                         setText("<html><u><font color='blue'>" + value.toString());
101                 } else if (hasFocus) {
102                         setText("<html><font color='blue'>" + value.toString());
103                 } else {
104                         setText(value.toString());
105                 }
106                 return this;
107         }
108
109         /**
110          * 何行目までのセルを新しいセルとしてnewTableColorで塗りつぶすか
111          *
112          * @param row
113          *            0以上の値で新しいセルとして指定した行を塗りつぶす
114          */
115         public void updateNewCellRow(int row) {
116                 this.newTableRow = row;
117         }
118
119         /**
120          * マウスがある位置のセルをrepaint
121          */
122         public void mouseMoved(MouseEvent e) {
123                 //この部分は処理が重いのでカットすることとした
124                 /*JTable table = (JTable) e.getSource();
125                 Point pt = e.getPoint();
126                 row = table.rowAtPoint(pt);
127                 col = table.columnAtPoint(pt);
128                 if (row < 0 || col < 0) {
129                         row = -1;
130                         col = -1;
131                 }
132                 table.repaint();*/
133         }
134
135         /**
136          * マウスが存在するとき
137          */
138         public void mouseExited(MouseEvent e) {
139                 JTable table = (JTable) e.getSource();
140                 row = -1;
141                 col = -1;
142                 table.repaint();
143         }
144
145         /**
146          * マウスをクリックした時の動作
147          */
148         public void mouseClicked(MouseEvent e) {
149                 JTable table = (JTable) e.getSource();
150                 Point pt = e.getPoint();
151                 int crow = table.rowAtPoint(pt);
152                 int ccol = table.columnAtPoint(pt);
153                 // if(table.convertColumnIndexToModel(ccol) == 2)
154                 if (table.getColumnClass(ccol).equals(URL.class)) {
155                         URL url = (URL) table.getValueAt(crow, ccol);
156                         System.out.println(url);
157                         // ブラウザを起動
158                         try {
159                                 Desktop.getDesktop().browse(url.toURI());
160                         } catch (Exception ex) {
161                                 ex.printStackTrace();
162                         }
163                 }
164         }
165
166         public void mouseDragged(MouseEvent e) {
167         }
168
169         public void mouseEntered(MouseEvent e) {
170         }
171
172         public void mousePressed(MouseEvent e) {
173                 mouseClicked(e);
174         }
175
176         public void mouseReleased(MouseEvent e) {
177         }
178
179 }