?? xlist.java
字號:
package at.ac.uni_linz.tk.vchat;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
/************************************************************************
* A XList offers a list with extended functionality, such as
* changing columns' width dynamically, formating text, forcing
* line breaks, etc.
*
* @author Arno Huetter
* (C)opyright by the Institute for Computer Science, Telecooperation Department, University of Linz
************************************************************************/
public class XList extends Panel implements MouseListener, MouseMotionListener, KeyListener, AdjustmentListener {
private static final int VERTICAL_CELL_SPACING = 2;
private static final int HORIZONTAL_CELL_SPACING = 4;
private static final int MINIMUM_COLUMNWIDTH = 30;
private static final int MINIMUM_VISIBLELINES = 4;
public static final int LEFT = 0;
public static final int RIGHT = 1;
public static final int FLOW = 2;
private static final int SCROLLBAR_WIDTH = 16;
private Vector vecContent[], vecKey, vecColor;
private Scrollbar scbBar;
private int iNrOfColumns, iSelectedRow, iNrOfLines, iFirstVisibleLine, iNrOfVisibleLines, iColumnDraggedX, iColumnSeparator, iSortCriteria;
private Font fntFont;
private int iColumnWidth[], iColumnOrientation[], iNrOfLinesPerRow[], iNrOfLinesUntilRow[];
private String strColumnHeader[];
private boolean bSelectable, bSorted;
private Image imgBuffer;
/************************************************************************
* Creates a new XList.
* @param iNrOfColumnsParam the number of columns
************************************************************************/
public XList(int iNrOfColumnsParam) {
fntFont = ChatRepository.FIXED_FONT;
iNrOfColumns = Math.max(iNrOfColumnsParam, 1);
iNrOfVisibleLines = MINIMUM_VISIBLELINES;
iFirstVisibleLine = 0;
bSelectable = true;
bSorted = false;
iSortCriteria = 0;
iSelectedRow = 0;
vecContent = new Vector[iNrOfColumns];
vecKey = new Vector();
vecColor = new Vector();
strColumnHeader = new String[iNrOfColumns];
iColumnOrientation = new int[iNrOfColumns];
iColumnWidth = new int[iNrOfColumns];
for (int i = 0; i < iNrOfColumns; i++) {
vecContent[i] = new Vector();
strColumnHeader[i] = "";
iColumnOrientation[i] = FLOW;
}
scbBar = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 0);
setLayout(new BorderLayout());
add("East", scbBar);
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
scbBar.addAdjustmentListener(this);
setSize(getCalculatedSize());
}
/************************************************************************
* Sets the header for each column.
* @param strColumnHeaderParam an array of Strings which contains
* the headers
************************************************************************/
public void setColumnHeaders(String strColumnHeaderParam[]) {
if (strColumnHeaderParam.length == iNrOfColumns) {
strColumnHeader = strColumnHeaderParam;
}
repaint();
}
/************************************************************************
* Sets the width for each column.
* @param iColumnWidthParam an array of integers which contains
* the widths
************************************************************************/
public void setColumnWidths(int iColumnWidthParam[]) {
if (iColumnWidthParam.length == iNrOfColumns) {
iColumnWidth = iColumnWidthParam;
}
setSize(getCalculatedSize());
}
/************************************************************************
* Sets the orientation for each column.
* @param iColumnOrientationParam an array of integers which
* contains the orientations (must be
* XList.LEFT, XList.RIGHT or
* XList.FLOW)
************************************************************************/
public void setColumnOrientations(int iColumnOrientationParam[]) {
if (iColumnOrientationParam.length == iNrOfColumns) {
iColumnOrientation = iColumnOrientationParam;
}
repaint();
}
/************************************************************************
* Clears the XList.
************************************************************************/
public synchronized void clear() {
for (int i = 0; i < iNrOfColumns; i++) {
vecContent[i] = new Vector();
}
vecKey = new Vector();
vecColor = new Vector();
iNrOfLines = 0;
iFirstVisibleLine = 0;
iSelectedRow = 0;
repaint();
}
/************************************************************************
* Sets the text for a certain cell.
* @param iRowParam the index of the cell's row
* @param iColumnParam the index of the cell's column
* @param strContentParam the new cell text
************************************************************************/
public synchronized void setCellText(int iRowParam, int iColumnParam, String strContentParam) {
if (iRowParam < getNrOfRows() && iColumnParam < iNrOfColumns) {
vecContent[iColumnParam].removeElementAt(iRowParam);
vecContent[iColumnParam].insertElementAt(strContentParam, iRowParam);
repaint();
}
}
/************************************************************************
* Returns the text of a certain cell.
* @param iRowParam the index of the cell's row
* @param iColumnParam the index of the cell's column
************************************************************************/
public synchronized String getCellText(int iRowParam, int iColumnParam) {
if (iRowParam < getNrOfRows() && iColumnParam < iNrOfColumns) {
return (String)vecContent[iColumnParam].elementAt(iRowParam);
}
else {
return "";
}
}
/************************************************************************
* Sets the text Color for a certain row.
* @param iRowParam the index of the cell's row
* @param colParam the Color to be used
************************************************************************/
public synchronized void setRowColor(int iRowParam, Color colParam) {
if (iRowParam < getNrOfRows()) {
vecColor.setElementAt(colParam, iRowParam);
repaint();
}
}
/************************************************************************
* Adds a row at the bottom of the XList.
* @param strRowParam an array of Strings which contains each cell's
* text
************************************************************************/
public synchronized void addRow(String strRowParam[]) {
addRow(strRowParam, -1, Color.black);
}
/************************************************************************
* Adds a row at the bottom of the XList.
* @param strRowParam an array of Strings which contains each cell's
* text
* @param iKeyParam an int value working as an index key for the
* row
* @param colParam the text Color of the row
************************************************************************/
public synchronized void addRow(String strRowParam[], int iKeyParam, Color colParam) {
if (strRowParam.length == iNrOfColumns) {
if (bSorted) {
addSortedRow(strRowParam, iKeyParam, colParam);
}
else {
for (int i = 0; i < iNrOfColumns; i++) {
vecContent[i].addElement(strRowParam[i]);
}
vecKey.addElement(new Integer(iKeyParam));
vecColor.addElement(colParam);
}
repaint();
}
}
/************************************************************************
* Adds a row at its sorted index.
* @param strRowParam an array of Strings which contains each cell's
* text
* @param iKeyParam an int value working as an index key for the
* row
* @param colParam the text Color of the row
************************************************************************/
private synchronized void addSortedRow(String strRowParam[], int iKeyParam, Color colParam) {
int iIndex;
iIndex = getSortedRowIndex(strRowParam[iSortCriteria]);
for (int i = 0; i < iNrOfColumns; i++) {
vecContent[i].insertElementAt(strRowParam[i], iIndex);
}
vecKey.insertElementAt(new Integer(iKeyParam), iIndex);
vecColor.insertElementAt(colParam, iIndex);
repaint();
}
/************************************************************************
* Returns the row-index of a certain text depending on the XList's
* sorting criteria.
* @param strParam the text to be indexed
************************************************************************/
private synchronized int getSortedRowIndex(String strParam) {
Collator collator;
collator = Collator.getInstance();
for (int i = 0; i < getNrOfRows(); i++) {
if (collator.compare((String)vecContent[iSortCriteria].elementAt(i), strParam) > 0) {
return i;
}
}
return getNrOfRows();
}
/************************************************************************
* Returns the key value of a certain row.
* @param iRowParam the index of the row
************************************************************************/
public synchronized int getKey(int iRowParam) {
return ((Integer)vecKey.elementAt(iRowParam)).intValue();
}
/************************************************************************
* Returns the number of rows.
************************************************************************/
public synchronized int getNrOfRows() {
return vecContent[0].size();
}
/************************************************************************
* Returns the index of the currently selected row. Will be -1 in case
* the XList is marked as being unselectable.
************************************************************************/
public synchronized int getSelectedRow() {
return iSelectedRow;
}
/************************************************************************
* Determines wheter items of the BXList can be selected or not.
*
* @param bSelectableParam true if items should be selectable, false
* if not
************************************************************************/
public void setSelectable(boolean bSelectableParam) {
bSelectable = bSelectableParam;
repaint();
}
/************************************************************************
* Determines wheter the XList should be sorted or not.
*
* @param bSortedParam true if the XList should be sorted, false if
* not
************************************************************************/
public void setSorted(boolean bSortedParam) {
bSorted = bSortedParam;
}
/************************************************************************
* Returns the BXList's preferred size.
************************************************************************/
public Dimension getPreferredSize() {
return getCalculatedSize();
}
/************************************************************************
* Returns the BXList's minimum size.
************************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -