📄 stocktablecellrenderer.java
字号:
/*
* StockTableCellRender.java
*
* Created on May 1, 2007, 5:43 PM
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) 2007 Cheok YanCheng <yccheok@yahoo.com>
*/
package org.yccheok.jstock.gui;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
* @author yccheok
*/
public class StockTableCellRenderer extends javax.swing.table.DefaultTableCellRenderer {
/** Creates a new instance of StockTableCellRender */
public StockTableCellRenderer() {
super();
}
private void performCellBlinking(final Component cell, final double value, final double oldValue, final Color finalForegroundColor, final Color finalBackgroundColor) {
final JStockOptions jStockOptions = MainFrame.getJStockOptions();
if(value == oldValue) {
cell.setForeground(finalForegroundColor);
cell.setBackground(finalBackgroundColor);
return;
}
else {
cell.setForeground(jStockOptions.getAutoUpdateForegroundColor());
cell.setBackground(jStockOptions.getAutoUpdateBackgroundColor());
}
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
cell.setForeground(finalForegroundColor);
cell.setBackground(finalBackgroundColor);
}
});
}
private Color getBackgroundColor(int row) {
final JStockOptions jStockOptions = MainFrame.getJStockOptions();
if(row % 2 == 0) {
return jStockOptions.getFirstRowBackgroundColor();
}
return jStockOptions.getSecondRowBackgroundColor();
}
private Color getForegroundColor(double value, double ref) {
final JStockOptions jStockOptions = MainFrame.getJStockOptions();
if(value > ref) {
return jStockOptions.getHigherNumericalValueForegroundColor();
}
else if(value < ref) {
return jStockOptions.getLowerNumericalValueForegroundColor();
}
return jStockOptions.getNormalTextForegroundColor();
}
private Component getTableCellRendererComponentWithCellBlinking (
Component c, JTable table, Object color,
boolean isSelected, boolean hasFocus,
int row, int column) {
final JStockOptions jStockOptions = MainFrame.getJStockOptions();
AbstractTableModelWithMemory tableModel = (AbstractTableModelWithMemory)table.getModel();
double openPrice = (Double)tableModel.getValueAt(table.convertRowIndexToModel(row), tableModel.findColumn("Open"));
if(table.getColumnName(column).equalsIgnoreCase("buy"))
{
final int modelRow = table.convertRowIndexToModel(row);
final int modelCol = tableModel.findColumn("Buy");
final double buyPrice = (Double)tableModel.getValueAt(modelRow, modelCol);
final Object o = tableModel.getOldValueAt(modelRow, modelCol);
final double oldBuyPrice = ((o == null) ? buyPrice : (Double)o);
this.performCellBlinking(c, buyPrice, oldBuyPrice, this.getForegroundColor(buyPrice, openPrice), getBackgroundColor(row));
return c;
}
else if(table.getColumnName(column).equalsIgnoreCase("sell"))
{
final int modelRow = table.convertRowIndexToModel(row);
final int modelCol = tableModel.findColumn("Sell");
final double sellPrice = (Double)tableModel.getValueAt(modelRow, modelCol);
final Object o = tableModel.getOldValueAt(modelRow, modelCol);
final double oldSellPrice = ((o == null) ? sellPrice : (Double)o);
this.performCellBlinking(c, sellPrice, oldSellPrice, this.getForegroundColor(sellPrice, openPrice), getBackgroundColor(row));
return c;
}
else if(table.getColumnName(column).equalsIgnoreCase("last"))
{
final int modelRow = table.convertRowIndexToModel(row);
final int modelCol = tableModel.findColumn("Last");
final double lastPrice = (Double)tableModel.getValueAt(modelRow, modelCol);
final Object o = tableModel.getOldValueAt(modelRow, modelCol);
final double oldLastPrice = ((o == null) ? lastPrice : (Double)o);
this.performCellBlinking(c, lastPrice, oldLastPrice, this.getForegroundColor(lastPrice, openPrice), getBackgroundColor(row));
return c;
}
else if(table.getColumnName(column).equalsIgnoreCase("low"))
{
final int modelRow = table.convertRowIndexToModel(row);
final int modelCol = tableModel.findColumn("Low");
final double lowPrice = (Double)tableModel.getValueAt(modelRow, modelCol);
final Object o = tableModel.getOldValueAt(modelRow, modelCol);
final double oldLowPrice = ((o == null) ? lowPrice : (Double)o);
this.performCellBlinking(c, lowPrice, oldLowPrice, this.getForegroundColor(lowPrice, openPrice), getBackgroundColor(row));
return c;
}
else if(table.getColumnName(column).equalsIgnoreCase("high"))
{
final int modelRow = table.convertRowIndexToModel(row);
final int modelCol = tableModel.findColumn("High");
final double highPrice = (Double)tableModel.getValueAt(modelRow, modelCol);
final Object o = tableModel.getOldValueAt(modelRow, modelCol);
final double oldHighPrice = ((o == null) ? highPrice : (Double)o);
this.performCellBlinking(c, highPrice, oldHighPrice, this.getForegroundColor(highPrice, openPrice), getBackgroundColor(row));
return c;
}
else if(table.getColumnName(column).equalsIgnoreCase("chg"))
{
final int modelRow = table.convertRowIndexToModel(row);
final int modelCol = tableModel.findColumn("Chg");
final double changePrice = (Double)tableModel.getValueAt(modelRow, modelCol);
final Object o = tableModel.getOldValueAt(modelRow, modelCol);
final double oldChangePrice = ((o == null) ? changePrice : (Double)o);
this.performCellBlinking(c, changePrice, oldChangePrice, this.getForegroundColor(changePrice, 0.0), getBackgroundColor(row));
return c;
}
else if(table.getColumnName(column).equalsIgnoreCase("chg (%)"))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -