Java Code Examples for org.eclipse.swt.widgets.Table#getColumns()

The following examples show how to use org.eclipse.swt.widgets.Table#getColumns() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ColumnsResizer.java    From hop with Apache License 2.0 6 votes vote down vote up
protected void applyWeigths( Table table ) {
  if ( resizing ) {
    return;
  }
  float width = getTableWidth( table );

  TableColumn[] columns = table.getColumns();

  int f = 0;
  for ( int w : weights ) {
    f += w;
  }
  int len = Math.min( weights.length, columns.length );
  resizing = true;
  for ( int i = 0; i < len; i++ ) {
    int cw = weights[ i ] == 0 ? 0 : Math.round( width / f * weights[ i ] );
    width -= cw + 1;
    columns[ i ].setWidth( cw );
    f -= weights[ i ];
  }
  resizing = false;
}
 
Example 2
Source File: RelationDisplayTableControl.java    From depan with Apache License 2.0 5 votes vote down vote up
private void configSorters(Table table) {
  int index = 0;
  for (TableColumn column : table.getColumns()) {
    final int colIndex = index++;

    column.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent event) {
        updateSortColumn((TableColumn) event.widget, colIndex);
      }
    });
  }
}
 
Example 3
Source File: Tables.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
public static void bindColumnWidths(Table table, int minimum, double... percents) {
	if (table == null || percents == null)
		return;
	TableResizeListener tableListener = new TableResizeListener(table, percents, minimum);
	// see resize listener declaration for comment on why this is done
	ColumnResizeListener columnListener = new ColumnResizeListener(tableListener);
	for (TableColumn column : table.getColumns())
		column.addControlListener(columnListener);
	table.addControlListener(tableListener);
}
 
Example 4
Source File: AbstractSection.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Pack table.
 *
 * @param table the table
 */
public void packTable(Table table) {
  TableColumn[] columns = table.getColumns();
  for (int i = 0; i < columns.length; i++) {
    columns[i].pack();
  }
}
 
Example 5
Source File: ClipboardHandlerTable.java    From arx with Apache License 2.0 5 votes vote down vote up
/**
 * Renders the table into a string.
 *
 * @param table
 * @return
 */
private String getText(Table table){

    StringBuilder builder = new StringBuilder();
    
    TableColumn[] columns = table.getColumns();
    for (int i = 0; i < columns.length; i++) {
        TableColumn column = columns[i];
        builder.append(column.getText());
        if (i < columns.length - 1) {
            builder.append(";");
        } else {
            builder.append("\n");
        }
    }
    
    for (TableItem item : table.getItems()) {
        for (int i = 0; i < columns.length; i++) {
            String value = item.getText(i);
            if (value != null && !value.equals("")) { //$NON-NLS-1$
                builder.append(value); //$NON-NLS-1$
            } else if (item.getData(String.valueOf(i)) != null && 
                       item.getData(String.valueOf(i)) instanceof Double) {
                builder.append(SWTUtil.getPrettyString(((Double) item.getData(String.valueOf(i))).doubleValue() * 100d) + "%"); //$NON-NLS-1$
            }
            if (i < columns.length - 1) {
                builder.append(";");
            } else {
                builder.append("\n");
            }
        }
    }
    
    return builder.toString();
}
 
Example 6
Source File: AbstractOrganizationWizardPage.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
protected void addTableColumLayout(final Table table) {
    final TableColumnLayout tcLayout = new TableColumnLayout();
    for (final TableColumn col : table.getColumns()) {
        tcLayout.setColumnData(col, new ColumnWeightData(1));
    }
    table.getParent().setLayout(tcLayout);
}
 
Example 7
Source File: ICETableComponentSectionPart.java    From ice with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Packs all columns in the {@link #tableComponentViewer}. This can be used
 * to resize each column to fit the maximum width of its content.
 */
private void packTableColumns() {

	if (tableComponentViewer != null) {
		Table table = tableComponentViewer.getTable();
		for (TableColumn column : table.getColumns()) {
			column.pack();
		}
	}

	return;
}
 
Example 8
Source File: FeatureEnvy.java    From JDeodorant with MIT License 5 votes vote down vote up
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();
		}
	}
}
 
Example 9
Source File: RelationSetTableControl.java    From depan with Apache License 2.0 5 votes vote down vote up
private void configSorters(Table table) {
  int index = 0;
  for (TableColumn column : table.getColumns()) {
    final int colIndex = index++;

    column.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent event) {
        updateSortColumn((TableColumn) event.widget, colIndex);
      }
    });
  }
}
 
Example 10
Source File: ColumnsResizer.java    From hop with Apache License 2.0 5 votes vote down vote up
public void addColumnResizeListeners( Table table ) {
  TableColumn[] columns = table.getColumns();
  int len = Math.min( weights.length, columns.length );
  for ( int i = 0; i < len - 1; i++ ) {
    if ( weights[ i ] > 0 ) {
      columns[ i ].addListener( SWT.Resize, getColumnResizeListener( i ) );
    }
  }
}
 
Example 11
Source File: EdgeDisplayTableControl.java    From depan with Apache License 2.0 5 votes vote down vote up
private void configSorters(Table table) {
  int index = 0;
  for (TableColumn column : table.getColumns()) {
    final int colIndex = index++;

    column.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent event) {
        updateSortColumn((TableColumn) event.widget, colIndex);
      }
    });
  }
}
 
Example 12
Source File: ColumnsResizer.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public void addColumnResizeListeners( Table table ) {
  TableColumn[] columns = table.getColumns();
  int len = Math.min( weights.length, columns.length );
  for ( int i = 0; i < len - 1; i++ ) {
    if ( weights[i] > 0 ) {
      columns[i].addListener( SWT.Resize, getColumnResizeListener( i ) );
    }
  }
}
 
Example 13
Source File: ManageableTableTreeEx.java    From SWET with MIT License 5 votes vote down vote up
private void createContents(final Shell shell) {
	shell.setLayout(new FillLayout());

	TableTree tableTree = new TableTree(shell, SWT.NONE);
	Table table = tableTree.getTable();
	table.setHeaderVisible(true);
	table.setLinesVisible(false);

	for (int col = 0; col < cols; col++) {
		new TableColumn(table, SWT.LEFT).setText("Column " + (col + 1));
	}

	for (int row = 0; row < rows; row++) {
		TableTreeItem parent = new TableTreeItem(tableTree, SWT.NONE);
		parent.setText(0, "Parent " + (row + 1));
		parent.setText(1, "Data");
		parent.setText(2, "More data");

		// Add children items
		for (int j = 0; j < rows; j++) {
			// Create a child item and add data to the columns
			TableTreeItem child = new TableTreeItem(parent, SWT.NONE);
			child.setText(0, "Child " + (j + 1));
			child.setText(1, "Some child data");
			child.setText(2, "More child data");
		}
		// Expand the parent item
		parent.setExpanded(true);
	}

	// Pack the columns
	TableColumn[] columns = table.getColumns();
	for (int i = 0, n = columns.length; i < n; i++) {
		columns[i].pack();
	}
}
 
Example 14
Source File: Snippet1.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
public static Print createPrint(Table table) {
	// Create GridPrint with all columns at default size.

	DefaultGridLook look = new DefaultGridLook();
	look.setCellBorder(new LineBorder());
	RGB background = table.getDisplay()
			.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND).getRGB();
	look.setHeaderBackground(background);
	look.setFooterBackground(background);

	GridPrint grid = new GridPrint(look);

	// Add header and footer to match table column names.
	TableColumn[] columns = table.getColumns();
	for (int i = 0; i < columns.length; i++) {
		TableColumn col = columns[i];

		// Add the column to the grid with alignment applied, default width,
		// no
		// weight
		grid.addColumn(new GridColumn(col.getAlignment(), SWT.DEFAULT, 0));

		Print cell = createCell(col.getImage(), col.getText(), SWT.CENTER);
		grid.addHeader(cell);
		grid.addFooter(cell);
	}

	// Add content rows
	TableItem[] items = table.getItems();
	for (int i = 0; i < items.length; i++) {
		TableItem item = items[i];
		for (int j = 0; j < columns.length; j++)
			grid.add(createCell(item.getImage(j), item.getText(j),
					columns[j].getAlignment()));
	}

	return grid;
}
 
Example 15
Source File: PopulationInspectView.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
/**
 *
 */
public void saveAsCSV() {
	try {
		Files.newFolder(getScope(), exportFolder);
	} catch (final GamaRuntimeException e1) {
		e1.addContext("Impossible to create folder " + exportFolder);
		GAMA.reportError(getScope(), e1, false);
		e1.printStackTrace();
		return;
	}

	final String exportFileName = FileUtils.constructAbsoluteFilePath(getScope(),
			exportFolder + "/" + getSpeciesName() + "_population" + getScope().getClock().getCycle() + ".csv",
			false);
	final Table table = viewer.getTable();
	final TableColumn[] columns = table.getColumns();
	try (final CsvWriter writer = new CsvWriter(exportFileName);) {
		// AD 2/1/16 Replaces the comma by ';' to properly output points and
		// lists
		writer.setDelimiter(';');
		writer.setUseTextQualifier(false);

		final List<String[]> contents = new ArrayList<>();
		final String[] headers = new String[columns.length];
		int columnIndex = 0;
		for (final TableColumn column : columns) {
			headers[columnIndex++] = column.getText();
		}
		contents.add(headers);
		final TableItem[] items = table.getItems();
		for (final TableItem item : items) {
			final String[] row = new String[columns.length];
			for (int i = 0; i < columns.length; i++) {
				row[i] = item.getText(i);
			}
			contents.add(row);
		}
		try {
			for (final String[] ss : contents) {
				writer.writeRecord(ss);
			}

		} catch (final IOException e) {
			throw GamaRuntimeException.create(e, getScope());

		}
	}
}
 
Example 16
Source File: TableEditorEx.java    From SWET with MIT License 4 votes vote down vote up
public static void packTable(Table table) {
	for (TableColumn tc : table.getColumns()) {
		tc.pack();
	}
}
 
Example 17
Source File: Snippet1.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Executes the snippet.
 *
 * @param args
 *            command-line args.
 */
public static void main(String[] args) {
	Display display = Display.getDefault();
	final Shell shell = new Shell(display);
	shell.setText("Snippet1.java");
	shell.setBounds(100, 100, 640, 480);
	shell.setLayout(new GridLayout());

	Button button = new Button(shell, SWT.PUSH);
	button.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, true, false));
	button.setText("Print the table");

	final Table table = new Table(shell, SWT.BORDER);
	table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

	// Set up Table widget with dummy data.
	for (int i = 0; i < 5; i++)
		new TableColumn(table, SWT.LEFT).setText("Column " + i);

	for (int row = 0; row < 100; row++) {
		TableItem item = new TableItem(table, SWT.NONE);
		for (int col = 0; col < 5; col++)
			item.setText(col, "Cell [" + col + ", " + row + "]");
	}

	table.setHeaderVisible(true);
	TableColumn[] columns = table.getColumns();
	for (int i = 0; i < columns.length; i++)
		columns[i].pack();

	button.addListener(SWT.Selection, event -> {
		PrintDialog dialog = new PrintDialog(shell, SWT.NONE);
		PrinterData printerData = dialog.open();
		if (printerData != null) {
			Print print = createPrint(table);
			PaperClips.print(
					new PrintJob("Snippet1.java", print).setMargins(72),
					printerData);
		}
	});

	shell.setVisible(true);

	while (!shell.isDisposed())
		if (!display.readAndDispatch())
			display.sleep();

	display.dispose();
}
 
Example 18
Source File: TableColumnSorter.java    From bonita-studio with GNU General Public License v2.0 4 votes vote down vote up
public void setColumn(TableColumn selectedColumn) {
	if (column == selectedColumn) {
		switch (direction) {
		case ASC:
			direction = DESC;
			break;
		case DESC:
			direction = ASC;
			break;
		default:
			direction = ASC;
			break;
		}
	} else {
		this.column = selectedColumn;
		this.direction = ASC;
	}

	Table table = viewer.getTable();
	switch (direction) {
	case ASC:
		table.setSortColumn(selectedColumn);
		table.setSortDirection(SWT.UP);
		break;
	case DESC:
		table.setSortColumn(selectedColumn);
		table.setSortDirection(SWT.DOWN);
		break;
	default:
		table.setSortColumn(null);
		table.setSortDirection(SWT.NONE);
		break;
	}

	TableColumn[] columns = table.getColumns();
	for (int i = 0; i < columns.length; i++) {
		TableColumn theColumn = columns[i];
		if (theColumn == this.column) columnIndex = i;
	}
	viewer.setComparator(null);
	viewer.setComparator(this);
}
 
Example 19
Source File: ColumnsResizer.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private Listener getColumnResizeListener( final int colIndex ) {
  return new Listener() {
    private int colIdx = colIndex;

    @Override
    public void handleEvent( Event event ) {
      if ( resizing ) {
        return;
      }

      TableColumn column = (TableColumn) event.widget;
      Table table = column.getParent();
      TableColumn[] columns = table.getColumns();
      int firstWidth = 0, restWidth = 0;
      int len = Math.min( weights.length, columns.length );
      for ( int i = 0; i <= colIdx; i++ ) {
        firstWidth += columns[i].getWidth();
      }
      float restWeightsBefore = 0;
      for ( int i = colIdx + 1; i < len; i++ ) {
        restWeightsBefore += weights[i];
        restWidth += columns[i].getWidth();
      }
      int tableWidth = getTableWidth( table );

      final int minWeight = 4;
      for ( int i = 0; i <= colIdx; i++ ) {
        if ( weights[i] > 0 ) {
          weights[i] = columns[i].getWidth();
        }
      }
      int columnsWidth = firstWidth + restWidth;
      int shortening = columnsWidth - tableWidth;
      float newRestWidth = restWidth - shortening;
      for ( int i = colIdx + 1; i < len; i++ ) {
        if ( weights[i] > 0 ) {
          float w = weights[i];
          w = w / restWeightsBefore * newRestWidth;
          weights[i] = Math.max( Math.round( w ), minWeight );
        }
      }
      applyWeigths( table );
    }
  };
}
 
Example 20
Source File: ColumnsResizer.java    From hop with Apache License 2.0 4 votes vote down vote up
private Listener getColumnResizeListener( final int colIndex ) {
  return new Listener() {
    private int colIdx = colIndex;

    @Override
    public void handleEvent( Event event ) {
      if ( resizing ) {
        return;
      }

      TableColumn column = (TableColumn) event.widget;
      Table table = column.getParent();
      TableColumn[] columns = table.getColumns();
      int firstWidth = 0, restWidth = 0;
      int len = Math.min( weights.length, columns.length );
      for ( int i = 0; i <= colIdx; i++ ) {
        firstWidth += columns[ i ].getWidth();
      }
      float restWeightsBefore = 0;
      for ( int i = colIdx + 1; i < len; i++ ) {
        restWeightsBefore += weights[ i ];
        restWidth += columns[ i ].getWidth();
      }
      int tableWidth = getTableWidth( table );

      final int minWeight = 4;
      for ( int i = 0; i <= colIdx; i++ ) {
        if ( weights[ i ] > 0 ) {
          weights[ i ] = columns[ i ].getWidth();
        }
      }
      int columnsWidth = firstWidth + restWidth;
      int shortening = columnsWidth - tableWidth;
      float newRestWidth = restWidth - shortening;
      for ( int i = colIdx + 1; i < len; i++ ) {
        if ( weights[ i ] > 0 ) {
          float w = weights[ i ];
          w = w / restWeightsBefore * newRestWidth;
          weights[ i ] = Math.max( Math.round( w ), minWeight );
        }
      }
      applyWeigths( table );
    }
  };
}