📄 bufferedtablerow.java
字号:
/*
* JMule - Java file sharing client
* Copyright (C) 2007-2008 JMule team ( jmule@jmule.org / http://jmule.org )
*
* Any parts of this program derived from other projects, or contributed
* by third-party developers are copyrighted by their respective authors.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package org.jmule.ui.swt.tables;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.jmule.ui.swt.Utils;
/**
* A buffered Table Row.
*<p>
* We buffer certain properties of TableRow to save CPU cycles. For example,
* foreground_colors is cached because TableItem.getForegroundColor is expensive
* when there is no color set, and setForegroundColor is always expensive.
*<p>
* Text is not buffered as SWT does a good job of buffering it already.
*<p>
*
* @note For Virtual tables, we can not set any visual properties until
* SWT.SetData has been called. getData("SD") is set after SetData
* call, and the row is invalidated. Thus, there is no need to set
* any visual properties until the row #isVisible()
*
* @author parg<br>
* @author TuxPaper (SWT.Virtual Stuff)
* @author binary256
* @version $$Revision: 1.2 $$
* Last changed by $$Author: binary256_ $$ on $$Date: 2008/09/07 15:27:43 $$
*/
public class BufferedTableRow {
private List<BufferedTableRowCustomControl> custom_control_list = new LinkedList<BufferedTableRowCustomControl>();
public List<BufferedTableRowCustomControl> getControlList() {
return custom_control_list;
}
private static final int VALUE_SIZE_INC = 8;
private static Color[] alternatingColors = null;
// for checkWidget(int)
public final static int REQUIRE_TABLEITEM = 0;
public final static int REQUIRE_TABLEITEM_INITIALIZED = 1;
public final static int REQUIRE_VISIBILITY = 2;
protected Table table;
protected TableItem item;
protected Image[] image_values = new Image[0];
protected Color[] foreground_colors = new Color[0];
protected Color foreground;
// remove when things work
private static final boolean bDebug = false;
private Point ptIconSize = null;
/**
* Default constructor
*
* @param _table
*/
public BufferedTableRow(Table _table)
{
table = _table;
item = null;
}
public TableItem getTableItem() {
return item;
}
/**
* Create a row in the SWT table
*
*/
public void createSWTRow() {
item = new TableItem(table, SWT.NULL);
//setAlternatingBGColor(false);
}
public void setSWTRow(TableItem item) {
this.item = item;
//setAlternatingBGColor(false);
}
public void createSWTRow(int index) {
new TableItem(table, SWT.NULL);
setTableItem(index, false);
}
public void setAlternatingBGColor(boolean bEvenIfNotVisible) {
// if (Utils.TABLE_GRIDLINE_IS_ALTERNATING_COLOR)
// return;
// if ((table.getStyle() & SWT.VIRTUAL) != 0 && !bEvenIfNotVisible
// && !isVisible()) {
// return;
// } else if (item == null || item.isDisposed()) {
// return;
// }
int index = table.indexOf(item);
if (index == -1) {
return;
}
if (alternatingColors == null || alternatingColors[1].isDisposed()) {
alternatingColors = new Color[] {
table.getDisplay().getSystemColor(SWT.COLOR_WHITE),
table.getDisplay().getSystemColor(SWT.COLOR_WHITE) };
}
Color newColor = alternatingColors[index % alternatingColors.length];
if (!newColor.equals(getBackground()))
item.setBackground(newColor);
}
public void setBackgrounColor(Color color) {
item.setBackground(color);
}
/**
* Disposes of underlying SWT TableItem. If no TableItem has been
* assigned to the row yet, an unused TableItem will be disposed of, if
* available.
* <p>
* Disposing of fonts, colors and other resources are the responsibilty of
* the caller.
*/
public void
dispose()
{
if (table != null && !table.isDisposed() ) {
if (!checkWidget(REQUIRE_TABLEITEM)) {
// No assigned spot yet, or not our spot:
// find a row with no TableRow data
TableItem[] items = table.getItems();
for (int i = items.length - 1; i >= 0; i--) {
TableItem item = items[i];
if (!item.isDisposed()) {
Object itemRow = item.getData("TableRow");
if (itemRow == null || itemRow == this) {
this.item = item;
break;
}
}
}
}
if (item != null && !item.isDisposed())
item.dispose();
else if (table.getItemCount() > 0)
System.err.println("No table row was found to dispose");
} else {
}
item = null;
}
public Image getImage(int id) {
if (id >= image_values.length) return null;
return image_values[id];
}
/**
* Sets the receiver's image at a column.
*
* @param index the column index
* @param new_image the new image
*/
public void
setImage(
int index,
Image new_image )
{
// if (!checkWidget(REQUIRE_TABLEITEM_INITIALIZED))
// return;
if ( index >= image_values.length ){
int new_size = Math.max( index+1, image_values.length+VALUE_SIZE_INC );
Image[] new_images = new Image[new_size];
System.arraycopy( image_values, 0, new_images, 0, image_values.length );
image_values = new_images;
}
Image image = image_values[index];
if ( new_image == image ){
return;
}
image_values[index] = new_image;
item.setImage( index, new_image );
}
/**
* Checks if the widget is valid
*
* @param checkFlags REQUIRE_* flags (OR'd)
*
* @return True: Ok; False: Not ok
*/
public boolean checkWidget(int checkFlags) {
// boolean bWidgetOk = !table.isDisposed() && item != null
// && !item.isDisposed() && item.getData("TableRow") == this;
boolean bWidgetOk = true;
final boolean bCheckVisibility = (checkFlags & REQUIRE_VISIBILITY) > 0;
final boolean bCheckInitialized = (checkFlags & REQUIRE_TABLEITEM_INITIALIZED) > 0;
if (bWidgetOk && bCheckInitialized) {
bWidgetOk = (table.getStyle() & SWT.VIRTUAL) == 0
|| item.getData("SD") != null;
}
if (bWidgetOk && bCheckVisibility) {
if (_isVisible()) {
// Caller assumes that a visible item can be modified, so
// make sure we initialize it.
if (!bCheckInitialized && (table.getStyle() & SWT.VIRTUAL) != 0
&& item.getData("SD") == null) {
// This is catch is temporary for SWT 3212, because there are cases where
// it says it isn't disposed, when it really almost is
try {
item.setData("SD", "1");
} catch (NullPointerException badSWT) {
}
//setAlternatingBGColor(false);
setIconSize(ptIconSize);
invalidate();
}
} else {
bWidgetOk = false;
}
}
return bWidgetOk;
}
private boolean _isVisible() {
// Least time consuming checks first
int index = table.indexOf(item);
if (index == -1)
return false;
int iTopIndex = table.getTopIndex();
if (index < iTopIndex)
return false;
int iBottomIndex = Utils.getTableBottomIndex(table, iTopIndex);
if (index > iBottomIndex)
return false;
return true;
}
public Color
getForeground()
{
if (!checkWidget(REQUIRE_TABLEITEM_INITIALIZED))
return null;
return( item.getForeground());
}
public void setForeground(Color c ){
foreground = c;
item.setForeground(foreground);
}
public boolean
setForeground(
int index,
Color new_color )
{
if (!checkWidget(REQUIRE_TABLEITEM_INITIALIZED))
return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -