下面列出了org.eclipse.swt.widgets.Table#getColumnCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static int getClickedItemColumnIndex(Table table, TableItem item,
Point point){
int column = -1;
if (item != null) {
// Determine which column was selected
for (int i = 0, n = table.getColumnCount(); i < n; i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(point)) {
// This is the selected column
column = i;
break;
}
}
}
return column;
}
public static int getClickedItemColumnIndex(Table table, TableItem item,
Point point) {
int column = -1;
if (item != null) {
// Determine which column was selected
for (int i = 0, n = table.getColumnCount(); i < n; i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(point)) {
// This is the selected column
column = i;
break;
}
}
}
return column;
}
/**
* Sets the segment provider, use only in test, only run in display thread
*
* @param segmentProvider
* the segment provider
* @since 1.2
*/
@VisibleForTesting
public void setSegmentProvider(ISegmentStoreProvider segmentProvider) {
fSegmentProvider = segmentProvider;
// Sort order of the content provider is by start time by default
getTableViewer().setContentProvider(new SegmentStoreContentProvider());
Table table = getTableViewer().getTable();
table.setRedraw(false);
while (table.getColumnCount() > 0) {
table.getColumn(0).dispose();
}
createColumns();
createProviderColumns();
getTableViewer().getTable().addSelectionListener(new TableSelectionListener());
addPackListener();
table.setRedraw(true);
}
protected void packAndFillLastColumn(Table table) {
int columnsWidth = 0;
for (int i = 0; i < table.getColumnCount() - 1; i++) {
columnsWidth += table.getColumn(i).getWidth();
}
TableColumn lastColumn = table.getColumn(table.getColumnCount() - 1);
lastColumn.pack();
Rectangle area = table.getClientArea();
Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
int width = area.width - 2*table.getBorderWidth();
if (preferredSize.y > area.height + table.getHeaderHeight()) {
// Subtract the scrollbar width from the total column width
// if a vertical scrollbar will be required
Point vBarSize = table.getVerticalBar().getSize();
width -= vBarSize.x;
}
// last column is packed, so that is the minimum. If more space is available, add it.
if(lastColumn.getWidth() < width - columnsWidth) {
lastColumn.setWidth(width - columnsWidth + 4);
}
}
public void render(Section section, FormToolkit toolkit) {
Composite composite = UI.sectionClient(section, toolkit, 1);
String[] columnTitles = getColumnTitles();
viewer = Tables.createViewer(composite, columnTitles);
viewer.setLabelProvider(new FactorLabel());
Action copy = TableClipboard.onCopy(viewer);
Actions.bind(viewer, copy);
Tables.bindColumnWidths(viewer, 0.2, 0.1, 0.1, 0.1);
createModifySupport();
Table table = viewer.getTable();
for (int i = 0; i < table.getColumnCount(); i++) {
if (i < 4)
continue;
TableColumn column = table.getColumn(i);
if (!editor.hasAnyComment("allocationFactors") || i % 2 == 0) {
column.setWidth(80);
column.setToolTipText(columnTitles[i]);
} else {
column.setWidth(24);
}
}
for (int i = 3; i < table.getColumnCount(); i++) {
viewer.getTable().getColumns()[i].setAlignment(SWT.RIGHT);
}
}
protected void storeLastWidths(Table table) {
if (lastWidths == null) {
lastWidths = new int[table.getColumnCount()];
}
TableColumn[] columns = table.getColumns();
for (int col = 0; col < columns.length; col++) {
lastWidths[col] = columns[col].getWidth();
}
}
public static void resizeColumnsByCaption(Table table){
GC gc = new GC(table);
try {
for (int i = 0; i < table.getColumnCount(); i++) {
int maxTextExtent = getColumnTextWidth(table, gc, i);
table.getColumn(i).setWidth(maxTextExtent);
}
} finally {
gc.dispose();
}
}
public static void resizeColumnsByCaption(Table table) {
GC gc = new GC(table);
try {
for (int i = 0; i < table.getColumnCount(); i++) {
int maxTextExtent = getColumnTextWidth(table, gc, i);
table.getColumn(i).setWidth(maxTextExtent);
}
} finally {
gc.dispose();
}
}
/**
* Export content of a table to TSV file
* @param table
* the table to export
* @param stream
* the output stream
*/
public static void exportTableToTsv(Table table, @Nullable OutputStream stream) {
if (table == null || stream == null) {
return;
}
try (PrintWriter pw = new PrintWriter(stream)) {
int size = table.getItemCount();
List<String> columns = new ArrayList<>();
for (int i = 0; i < table.getColumnCount(); i++) {
TableColumn column = table.getColumn(i);
if (column == null) {
return;
}
String columnName = String.valueOf(column.getText());
if (columnName.isEmpty() && i == table.getColumnCount() - 1) {
// Linux GTK2 undocumented feature
break;
}
columns.add(columnName);
}
pw.println(Joiner.on('\t').join(columns));
for (int i = 0; i < size; i++) {
TableItem item = table.getItem(i);
if (item == null) {
continue;
}
List<String> data = new ArrayList<>();
for (int col = 0; col < columns.size(); col++) {
data.add(String.valueOf(item.getText(col)));
}
pw.println(Joiner.on('\t').join(data));
}
}
}
private static void copyItems(Table table, Converter converter,
StringBuilder buffer) {
int cols = table.getColumnCount();
for (TableItem item : table.getSelection()) {
for (int col = 0; col < cols; col++) {
String s = converter.asString(item, col);
buffer.append(s);
if (col != (cols - 1)) {
buffer.append('\t');
}
}
buffer.append('\n');
}
}
private void saveResults() {
FileDialog fd = new FileDialog(getSite().getWorkbenchWindow().getShell(), SWT.SAVE);
fd.setText("Save Results");
String[] filterExt = { "*.txt" };
fd.setFilterExtensions(filterExt);
String selected = fd.open();
if(selected != null) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(selected));
Table table = tableViewer.getTable();
TableColumn[] columns = table.getColumns();
for(int i=0; i<columns.length; i++) {
if(i == columns.length-1)
out.write(columns[i].getText());
else
out.write(columns[i].getText() + "\t");
}
out.newLine();
for(int i=0; i<table.getItemCount(); i++) {
TableItem tableItem = table.getItem(i);
for(int j=0; j<table.getColumnCount(); j++) {
if(j == table.getColumnCount()-1)
out.write(tableItem.getText(j));
else
out.write(tableItem.getText(j) + "\t");
}
out.newLine();
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private ItemPkg getCell(Event event, Table table) {
ItemPkg result = null;
TableItem item = table.getItem(new Point(event.x,event.y));
if (item != null) {
for (int i = 0; i < table.getColumnCount(); i++) {
if (item.getBounds(i).contains(event.x, event.y)) {
result = new ItemPkg(item,i);
break;
}
}
}
return result;
}
private ItemPkg getCell(MouseEvent event, Table table) {
ItemPkg result = null;
TableItem item = table.getItem(new Point(event.x,event.y));
if (item != null) {
for (int i = 0; i < table.getColumnCount(); i++) {
if (item.getBounds(i).contains(event.x, event.y)) {
result = new ItemPkg(item,i);
break;
}
}
}
return result;
}
private static void copyHeaders(Table table, StringBuilder text) {
int cols = table.getColumnCount();
for (int col = 0; col < cols; col++) {
TableColumn column = table.getColumn(col);
String s = column.getText();
text.append(s == null ? "" : s);
if (col != (cols - 1))
text.append('\t');
}
text.append('\n');
}
public static void sortTable(Table table, TableColumn column) {
TableItem[] items = table.getItems();
int rows = items.length;
int dir = table.getSortDirection() == SWT.DOWN ? 1 : -1;
TableColumn currentColumn = table.getSortColumn();
int columnNum = 0;
for (int j = 0; j < table.getColumnCount(); j++) {
if (table.getColumn(j).equals(column)) {
columnNum = j;
break;
}
}
if (column.equals(currentColumn))
dir = -dir;
else
dir = 1;
// sort an index map, then move the actual rows
int[] map = new int[rows];
for (int i = 0; i < rows; i++)
map[i] = i;
for (int i = 0; i < rows - 1; i++) {
for (int j = i + 1; j < rows; j++) {
TableItem a = items[map[i]];
TableItem b = items[map[j]];
if ((a.getText(columnNum).toLowerCase().compareTo(b.getText(columnNum).toLowerCase()) * dir > 0)) {
int t = map[i];
map[i] = map[j];
map[j] = t;
}
}
}
// can't move existing items or delete first, so append new items to the end and then delete existing rows
for (int i = 0; i < rows; i++) {
int n = map[i];
TableItem item = new TableItem(table, SWT.NONE);
for (int j = 0; j < table.getColumnCount(); j++) {
item.setText(j, items[n].getText(j));
}
item.setData(items[n].getData());
items[n].dispose();
}
table.setSortDirection(dir == 1 ? SWT.DOWN : SWT.UP);
table.setSortColumn(column);
}
private boolean resizedColumnIsNotTheLastColumn(int resizedColumnNumber, Table table) {
return resizedColumnNumber < table.getColumnCount()-1;
}
public static void resizeColumsByContent(Table table){
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumn(i).pack();
}
}
public static void resizeColumsByContent(Table table) {
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumn(i).pack();
}
}
public void saveColumnWeights(Table table, String qualifier) {
for (int i = 0; i < table.getColumnCount(); i++) {
settings.put(qualifier + ".columnWeight" + i, getColumnWeight(table, i)); //$NON-NLS-1$
}
}
public boolean createLaborblatt(final Patient pat, final String[] header, final TableItem[] rows){
Brief br =
text.createFromTemplateName(text.getAktuelleKons(), TT_LABPAPER,
Brief.LABOR,
pat, null);
if (br == null) {
return false;
}
Table table = rows[0].getParent();
int cols = table.getColumnCount();
int[] colsizes = new int[cols];
float first = 25;
float second = 10;
if (cols > 2) {
int rest = Math.round((100f - first - second) / (cols - 2f));
for (int i = 2; i < cols; i++) {
colsizes[i] = rest;
}
}
colsizes[0] = Math.round(first);
colsizes[1] = Math.round(second);
LinkedList<String[]> usedRows = new LinkedList<String[]>();
usedRows.add(header);
for (int i = 0; i < rows.length; i++) {
boolean used = false;
String[] row = new String[cols];
for (int j = 0; j < cols; j++) {
row[j] = rows[i].getText(j);
if ((j > 1) && (row[j].length() > 0)) {
used = true;
// break;
}
}
if (used == true) {
usedRows.add(row);
}
}
String[][] fld = usedRows.toArray(new String[0][]);
boolean ret = text.getPlugin().insertTable("[Laborwerte]", //$NON-NLS-1$
ITextPlugin.FIRST_ROW_IS_HEADER, fld, colsizes);
text.saveBrief(br, Brief.LABOR);
return ret;
}