View Javadoc

1   package net.sourceforge.jpotpourri.gui.log4jlog.gui;
2   
3   import java.awt.Color;
4   import java.awt.Component;
5   import java.awt.Graphics;
6   import java.util.ArrayList;
7   import java.util.List;
8   
9   import javax.swing.JMenuItem;
10  import javax.swing.JOptionPane;
11  import javax.swing.JTable;
12  import javax.swing.table.TableCellRenderer;
13  
14  import net.sourceforge.jpotpourri.gui.log4jlog.De;
15  import net.sourceforge.jpotpourri.gui.log4jlog.Log4jGuiTableDefinition;
16  import net.sourceforge.jpotpourri.gui.table.ITableBodyContextListener;
17  import net.sourceforge.jpotpourri.gui.table.ITableFillEmptyRowsReceiver;
18  import net.sourceforge.jpotpourri.gui.table.TableBodyContext;
19  import net.sourceforge.jpotpourri.gui.table.TableEmptyRowsPainter;
20  
21  import org.jdesktop.swingx.JXTable;
22  
23  /**
24   * @author christoph_pickl@users.sourceforge.net
25   */
26  class Log4jTable extends JXTable implements ITableFillEmptyRowsReceiver, ITableBodyContextListener {
27  
28  	private static final long serialVersionUID = -5142437725186427922L;
29  
30  	private final TableEmptyRowsPainter emptyRowsPainter;
31  	
32  	private Color colorRowBackgroundEven;
33  	private final Color colorRowBackgroundOdd;
34  	
35  
36  	private final Color colorRowForeground;
37  	private final Color colorSelectedRowForeground;
38  	private final Color colorSelectedRowBackground;
39  
40  
41      private static final String CMD_EXCEPTION_DETAILS = "CMD_EXCEPTION_DETAILS";
42  	
43      
44  	public Log4jTable(final Log4jTableModel tableModel, final Log4jGuiTableDefinition tableDefinition) {
45  		super(tableModel);
46  		// this.addHighlighter() ... no, we are painting alternating rows at our own
47  		De.bug("Creating new Log4jTable with defintion: " + tableDefinition);
48  		
49  		if(tableDefinition.getRows() != Log4jGuiTableDefinition.DEFAULT_ROWS) {
50  			this.setVisibleRowCount(tableDefinition.getRows());
51  		}
52  		
53  		this.colorRowBackgroundEven = tableDefinition.getColorRowBackgroundEven();
54  		this.colorRowBackgroundOdd = tableDefinition.getColorRowBackgroundOdd();
55  		
56  		this.colorRowForeground = tableDefinition.getColorRowForeground();
57  		this.colorSelectedRowForeground = tableDefinition.getColorSelectedRowForeground();
58  		this.colorSelectedRowBackground = tableDefinition.getColorSelectedRowBackground();
59  		
60  		this.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
61          this.setColumnSelectionAllowed(false);
62          this.setRowSelectionAllowed(true);
63          
64          this.setColumnControlVisible(true);
65          this.setShowGrid(false);
66          this.packAll();
67          
68          this.emptyRowsPainter = new TableEmptyRowsPainter(this);
69          this.initContextMenu();
70  	}
71  	
72  	private void initContextMenu() {
73  		final List<JMenuItem> itemsSingle = new ArrayList<JMenuItem>();
74  		TableBodyContext.newJMenuItem(itemsSingle, "Exception Details", CMD_EXCEPTION_DETAILS); // optional icon
75          
76  
77          final List<JMenuItem> itemsMultiple = new ArrayList<JMenuItem>();
78          final JMenuItem exceptionDetailsMultiple =
79          	TableBodyContext.newJMenuItem(itemsMultiple, "Exception Details", CMD_EXCEPTION_DETAILS); // optional icon
80          exceptionDetailsMultiple.setEnabled(false);
81          new TableBodyContext(this, itemsSingle, itemsMultiple, this);
82  	}
83  
84  	@Override
85  	public final void paint(final Graphics g) {
86  		super.paint(g);
87  		this.emptyRowsPainter.delegatePaint(g);
88  	}
89  
90  	public final Color getColorRowBackgroundEven() {
91  		return this.colorRowBackgroundEven;
92  	}
93  
94  	public final Color getColorRowBackgroundOdd() {
95  		return this.colorRowBackgroundOdd;
96  	}
97  	
98  
99  
100 	@Override
101 	public final Component prepareRenderer(final TableCellRenderer renderer, final int row, final int column) {
102 		final Component c = super.prepareRenderer(renderer, row, column);
103 
104 		final Color bgColor;
105 		final Color fgColor;
106 //        boolean focused = hasFocus();
107         boolean selected = isCellSelected(row, column);
108         
109         if(selected) {
110         	bgColor = this.colorSelectedRowBackground;
111         	fgColor = this.colorSelectedRowForeground;
112         } else {
113         	final boolean isEven = row % 2 == 0;
114         	bgColor = isEven ? this.colorRowBackgroundEven : this.colorRowBackgroundOdd;
115         	fgColor = this.colorRowForeground;
116         }
117         c.setBackground(bgColor);
118 		c.setForeground(fgColor);
119         
120 		return c;
121 	}
122 
123     
124 	public final void contextMenuClicked(final JMenuItem item, final int tableRowSelected) {
125 		final String cmd = item.getActionCommand();
126 		if(cmd.equals(CMD_EXCEPTION_DETAILS)) {
127 			JOptionPane.showMessageDialog(this, "Work in progress...", "Exception Details",
128 					JOptionPane.WARNING_MESSAGE); // FIXME exception details
129 		} else {
130 			throw new IllegalArgumentException("actionCommand=" + cmd);
131 		}
132 	}
133 
134 
135 	public final void contextMenuClickedMultiple(final JMenuItem item, final int[] tableRowsSelected) {
136 		final String cmd = item.getActionCommand();
137 		if(cmd.equals(CMD_EXCEPTION_DETAILS)) {
138 			assert(false); // may not happen
139 		} else {
140 			throw new IllegalArgumentException("actionCommand=" + cmd);
141 		}
142 	}
143 
144 }