Java Code Examples for org.eclipse.swt.widgets.TableColumn#getWidth()

The following examples show how to use org.eclipse.swt.widgets.TableColumn#getWidth() . 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: SWTTable.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void adjustColumnWidth(TableColumn column) {
	int minimumWidth = 0;
	
	String headerText = column.getText();
	if( headerText != null && headerText.length() > 0 ) {
		GC gc = new GC(this.getControl());
		minimumWidth = (gc.stringExtent(headerText).x + (TABLE_COLUMN_MARGIN * 2));
		gc.dispose();
	}
	
	column.pack();
	if( column.getWidth() < minimumWidth ) {
		column.setWidth(minimumWidth);
	}
	
}
 
Example 2
Source File: TmfEventsTable.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
private IAction createAutoFitAction(TableColumn column) {
    final IAction autoFitAction = new Action(Messages.TmfEventsTable_AutoFit, IAction.AS_CHECK_BOX) {
        @Override
        public void run() {
            boolean isChecked = isChecked();
            int index = (int) column.getData(Key.INDEX);
            if (isChecked) {
                fPacking = true;
                column.pack();
                fPacking = false;
                column.setData(Key.WIDTH, SWT.DEFAULT);
                fColumnSize[index] = SWT.DEFAULT;
            } else {
                fColumnSize[index] = column.getWidth();
                column.setData(Key.WIDTH, fColumnSize[index]);
            }
        }
    };
    autoFitAction.setChecked(Objects.equals(column.getData(Key.WIDTH), SWT.DEFAULT));
    return autoFitAction;
}
 
Example 3
Source File: TmfEventsTable.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
private void packSingleColumn(int i, final TableColumn column) {
    if (i != MARGIN_COLUMN_INDEX && !column.getResizable()) {
        return;
    }
    int minWidth = column.getWidth();
    fPacking = true;
    column.pack();
    /*
     * Workaround for Linux which doesn't consider the image width of
     * search/filter row in TableColumn.pack() after having executed
     * TableItem.setImage(null) for other rows than search/filter row.
     */
    if (IS_LINUX && (i == MARGIN_COLUMN_INDEX) && fCollapseFilterEnabled) {
        column.setWidth(column.getWidth() + SEARCH_IMAGE.getBounds().width);
    }

    if (column.getWidth() < minWidth) {
        column.setWidth(minWidth);
    }
    fPacking = false;
}
 
Example 4
Source File: TmfEventsTable.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns true if the column is expanded to take extra available space.
 * This is the last non-zero-width visible column in the column order on
 * Linux. This column's width should not be persisted.
 *
 * @param column
 *            the column
 * @return true if the column is expanded.
 */
private static boolean isExpanded(TableColumn column) {
    if (IS_LINUX) {
        Table table = column.getParent();
        int[] order = table.getColumnOrder();
        for (int i = order.length - 1; i >= 0; i--) {
            TableColumn col = table.getColumn(order[i]);
            if (col == column) {
                return true;
            }
            if (col.getWidth() > 0) {
                return false;
            }
        }
    }
    return false;
}
 
Example 5
Source File: CloneDiffTooltip.java    From JDeodorant with MIT License 6 votes vote down vote up
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);
    }
}
 
Example 6
Source File: TmfEventsTable.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void controlResized(ControlEvent e) {
    TableColumn column = (TableColumn) e.widget;
    if (fPacking) {
        /* Don't update column width if resize due to packing */
        return;
    }
    if (column.getResizable() && !isExpanded(column)) {
        int index = (int) column.getData(Key.INDEX);
        fColumnSize[index] = column.getWidth();
        /* Turns off AutoFit */
        column.setData(Key.WIDTH, fColumnSize[index]);
    }
}
 
Example 7
Source File: TmfEventsTable.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Get the array of item strings (e.g., what to display in each cell of the
 * table row) corresponding to the columns and trace event passed in
 * parameter. The order of the Strings in the returned array will correspond
 * to the iteration order of 'columns'.
 *
 * <p>
 * To ensure consistent results, make sure only call this within a scope
 * synchronized on 'columns'! If the order of 'columns' changes right after
 * this method is called, the returned value won't be ordered correctly
 * anymore.
 */
private String[] getItemStrings(List<TmfEventTableColumn> columns, ITmfEvent event) {
    if (event == null) {
        return EMPTY_STRING_ARRAY;
    }
    synchronized (columns) {
        String[] itemStrings = new String[columns.size()];
        TableColumn[] tableColumns = fTable.getColumns();
        for (int i = 0; i < columns.size(); i++) {
            TmfEventTableColumn column = columns.get(i);
            ITmfEvent passedEvent = event;
            if (!(column instanceof TmfMarginColumn) && (event instanceof CachedEvent)) {
                /*
                 * Make sure that the event object from the trace is passed to all columns but
                 * the TmfMarginColumn
                 */
                passedEvent = ((CachedEvent) event).event;
            }
            // Check if column is hidden but not a margin column.
            TableColumn tableColumn = tableColumns[fColumns.indexOf(column)];
            if (passedEvent == null || (!tableColumn.getResizable() && tableColumn.getWidth() == 0)) {
                itemStrings[i] = EMPTY_STRING;
            } else {
                String s = column.getItemString(passedEvent);
                s = s.replaceAll("\\n", " "); //$NON-NLS-1$//$NON-NLS-2$
                s = s.replaceAll("\\r", " "); //$NON-NLS-1$//$NON-NLS-2$
                itemStrings[i] = s;
            }
        }
        return itemStrings;
    }
}
 
Example 8
Source File: PyEditorHoverConfigurationBlock.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public void showColumn(TableColumn column, boolean show) {
    if (column.getWidth() != 0) {
        fColWidthsMap.put(column, column.getWidth());
    }
    if (fColWidthsMap.get(column) != null) {
        column.setWidth(show ? fColWidthsMap.get(column) : 0);
    }
}
 
Example 9
Source File: TableView.java    From hop with Apache License 2.0 4 votes vote down vote up
public void optWidth( boolean header, int nrLines ) {
  for ( int c = 0; c < table.getColumnCount(); c++ ) {
    TableColumn tc = table.getColumn( c );
    int max = 0;
    if ( header ) {
      max = TableView.dummyGC.textExtent( tc.getText(), SWT.DRAW_TAB | SWT.DRAW_DELIMITER ).x;

      // Check if the column has a sorted mark set. In that case, we need the
      // header to be a bit wider...
      //
      if ( c == sortfield && sortable ) {
        max += 15;
      }
    }
    Set<String> columnStrings = new HashSet<String>();

    boolean haveToGetTexts = false;
    if ( c > 0 ) {
      final ColumnInfo column = columns[ c - 1 ];
      if ( column != null ) {
        switch ( column.getType() ) {
          case ColumnInfo.COLUMN_TYPE_TEXT:
            haveToGetTexts = true;
            break;
          case ColumnInfo.COLUMN_TYPE_CCOMBO:
          case ColumnInfo.COLUMN_TYPE_FORMAT:
            haveToGetTexts = true;
            if ( column.getComboValues() != null ) {
              for ( String comboValue : columns[ c - 1 ].getComboValues() ) {
                columnStrings.add( comboValue );
              }
            }
            break;
          case ColumnInfo.COLUMN_TYPE_BUTTON:
            columnStrings.add( column.getButtonText() );
            break;
          default:
            break;

        }
      }
    } else {
      haveToGetTexts = true;
    }

    if ( haveToGetTexts ) {
      for ( int r = 0; r < table.getItemCount() && ( r < nrLines || nrLines <= 0 ); r++ ) {
        TableItem ti = table.getItem( r );
        if ( ti != null ) {
          columnStrings.add( ti.getText( c ) );
        }
      }
    }

    for ( String str : columnStrings ) {
      int len = TableView.dummyGC.textExtent( str == null ? "" : str, SWT.DRAW_TAB | SWT.DRAW_DELIMITER ).x;
      if ( len > max ) {
        max = len;
      }
    }

    try {
      int extra = 15;
      if ( Const.isWindows() || Const.isLinux() ) {
        extra += 15;
      }

      if ( tc.getWidth() != max + extra ) {
        if ( columns[ c ].getWidth() == -1 ) {
          tc.setWidth( max + extra );
        } else {
          tc.setWidth( columns[ c ].getWidth() );
        }
      }
    } catch ( Exception e ) {
      // Ignore errors
    }
  }
  if ( table.isListening( SWT.Resize ) ) {
    Event resizeEvent = new Event();
    resizeEvent.widget = table;
    resizeEvent.type = SWT.Resize;
    resizeEvent.display = getDisplay();
    resizeEvent.setBounds( table.getBounds() );
    table.notifyListeners( SWT.Resize, resizeEvent );
  }
  unEdit();
}
 
Example 10
Source File: HeaderLayout.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
protected Point computeColumnSize(Widget columnObject, int wHint, int hHint, boolean flush) {
    TableColumn tableColumn = (TableColumn) columnObject;
    int currentWidth = tableColumn.getWidth();
    int headerHeight = tableColumn.getParent().getHeaderHeight();
    return new Point(currentWidth, headerHeight);
}
 
Example 11
Source File: HeaderLayout.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
private void adjustWeights(AbstractNativeHeader header, TableColumn resizedColumn) {
    int totalAvailableWidth = getAvailableWidth(header);
    int resizedColumnNumber = 0;
    int newTotalWidth = 0;
    
    TableColumn[] columns = resizedColumn.getParent().getColumns();
    for (int i = 0; i < columns.length; i++) {
        newTotalWidth += columns[i].getWidth();
        if (columns[i] == resizedColumn) {
            resizedColumnNumber = i;
        }
    }

    Table table = resizedColumn.getParent();
    int[] columnOrder = table.getColumnOrder();
    int resizedColumnPosition = 0;
    
    for (int i = 0; i < columnOrder.length; i++) {
        if (columnOrder[i] == resizedColumnNumber) {
            resizedColumnPosition = i;
            break;
        }
    } 
    
    if (resizedColumnIsNotTheLastColumn(resizedColumnPosition, resizedColumn.getParent())) {
        // Compute resized column width change and make sure the resized 
        // column's width is sane
        int resizedColumnWidth = resizedColumn.getWidth();
        
        // int columnWidthChange = lastWidths[resizedColumnPosition] - resizedColumnWidth;
        int columnWidthChange = lastWidths[columnOrder[resizedColumnPosition]] - resizedColumnWidth;
        
        int columnWidthChangeTooFar = MINIMUM_COL_WIDTH - resizedColumnWidth;
        if (columnWidthChangeTooFar > 0) {
            columnWidthChange -= columnWidthChangeTooFar;
            resizedColumnWidth = MINIMUM_COL_WIDTH;
            resizedColumn.setWidth(resizedColumnWidth);
        }
        
        // Fix the width of the column to the right of the resized column
        int columnToTheRightOfResizedColumnWidth = 
            lastWidths[columnOrder[resizedColumnPosition+1]] + columnWidthChange;
        
        // int columnToTheRightOfResizedColumnWidth = 
        //     lastWidths[resizedColumnPosition+1] + columnWidthChange;
        
        columnWidthChangeTooFar = MINIMUM_COL_WIDTH - columnToTheRightOfResizedColumnWidth;
        if (columnWidthChangeTooFar > 0) {
            columnWidthChange += columnWidthChangeTooFar;
            resizedColumnWidth -= columnWidthChangeTooFar;
            resizedColumn.setWidth(resizedColumnWidth);
            columnToTheRightOfResizedColumnWidth = MINIMUM_COL_WIDTH;
        }
        TableColumn columnToTheRightOfResizedColumn = columns[columnOrder[resizedColumnPosition+1]];
        columnToTheRightOfResizedColumn.setWidth(columnToTheRightOfResizedColumnWidth);

        if (isFittingHorizontally()) {
            adjustWeightedHeader(header, resizedColumnPosition,
                    resizedColumn, columnToTheRightOfResizedColumn,
                    totalAvailableWidth, newTotalWidth);
        } else {
            // Fix the weights based on if the column sizes are being scaled
            if (isWidthWiderThanAllColumns(header)) {
                adjustScaledAbsoluteWidthWeights(resizedColumnPosition,
                        resizedColumnWidth,
                        columnToTheRightOfResizedColumnWidth, 
                        header.getSize().x);
            } else {
                adjustNonScaledAbsoluteWidthWeights(resizedColumnPosition,
                        resizedColumnWidth,
                        columnToTheRightOfResizedColumnWidth);
            }
        }
        
        fireColumnResizedEvent(resizedColumnPosition,
                        resizedColumnWidth,
                        columnToTheRightOfResizedColumnWidth);
    } else {
        // Re-layout; the rightmost column can't be resized
        layout(header, true);
    }
}
 
Example 12
Source File: MenuStylesDialog.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
public void handleEvent( Event event )
{
	Object source = event.widget;
	if ( source == fComboStyle )
	{
		updateProperties( fCurrentStyleKeyType );
		switchProperties( MenuStylesKeyType.get( fComboStyle.getSelectionIndex( ) ) );
	}
	else if ( source == fBtnAdd )
	{
		doAdd( );
	}
	else if ( source == fBtnRemove )
	{
		doRemove( );
	}
	else if ( source == fTable )
	{
		if ( event.type == SWT.Resize )
		{
			int totalWidth = 0;
			int valuewidth = 0;
			int i = 0;
			for ( TableColumn tc : fTable.getColumns( ) )
			{
				totalWidth += tc.getWidth( );
				if ( i == 1 )
				{
					valuewidth = tc.getWidth( );
				}
				i++;
			}
			valuewidth += ( fTable.getClientArea( ).width - totalWidth );
			fTable.getColumn( 1 ).setWidth( valuewidth );
		}
		else if ( event.type == SWT.Selection )
		{
			updateButtonStatus( );
		}
		else if ( event.type == SWT.KeyDown )
		{
			if ( event.character == ' ' )
			{
				fTableViewer.editElement( fTableViewer.getElementAt( fTable.getSelectionIndex( ) ),
						0 );
			}
		}
	}
}
 
Example 13
Source File: TableView.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public void optWidth( boolean header, int nrLines ) {
  for ( int c = 0; c < table.getColumnCount(); c++ ) {
    TableColumn tc = table.getColumn( c );
    int max = 0;
    if ( header ) {
      max = TableView.dummyGC.textExtent( tc.getText(), SWT.DRAW_TAB | SWT.DRAW_DELIMITER ).x;

      // Check if the column has a sorted mark set. In that case, we need the
      // header to be a bit wider...
      //
      if ( c == sortfield && sortable ) {
        max += 15;
      }
    }
    Set<String> columnStrings = new HashSet<String>();

    boolean haveToGetTexts = false;
    if ( c > 0 ) {
      final ColumnInfo column = columns[c - 1];
      if ( column != null ) {
        switch ( column.getType() ) {
          case ColumnInfo.COLUMN_TYPE_TEXT:
            haveToGetTexts = true;
            break;
          case ColumnInfo.COLUMN_TYPE_CCOMBO:
          case ColumnInfo.COLUMN_TYPE_FORMAT:
            haveToGetTexts = true;
            if ( column.getComboValues() != null ) {
              for ( String comboValue : columns[c - 1].getComboValues() ) {
                columnStrings.add( comboValue );
              }
            }
            break;
          case ColumnInfo.COLUMN_TYPE_BUTTON:
            columnStrings.add( column.getButtonText() );
            break;
          default:
            break;

        }
      }
    } else {
      haveToGetTexts = true;
    }

    if ( haveToGetTexts ) {
      for ( int r = 0; r < table.getItemCount() && ( r < nrLines || nrLines <= 0 ); r++ ) {
        TableItem ti = table.getItem( r );
        if ( ti != null ) {
          columnStrings.add( ti.getText( c ) );
        }
      }
    }

    for ( String str : columnStrings ) {
      int len = TableView.dummyGC.textExtent( str == null ? "" : str, SWT.DRAW_TAB | SWT.DRAW_DELIMITER ).x;
      if ( len > max ) {
        max = len;
      }
    }

    try {
      int extra = 15;
      if ( Const.isWindows() || Const.isLinux() ) {
        extra += 15;
      }

      if ( tc.getWidth() != max + extra ) {
        if ( c > 0 ) {
          if ( columns[c - 1].getWidth() == -1 ) {
            tc.setWidth( max + extra );
          } else {
            tc.setWidth( columns[c - 1].getWidth() );
          }
        }
      }
    } catch ( Exception e ) {
      // Ignore errors
    }
  }
  if ( table.isListening( SWT.Resize ) ) {
    Event resizeEvent = new Event();
    resizeEvent.widget = table;
    resizeEvent.type = SWT.Resize;
    resizeEvent.display = getDisplay();
    resizeEvent.setBounds( table.getBounds() );
    table.notifyListeners( SWT.Resize, resizeEvent );
  }
  unEdit();
}
 
Example 14
Source File: TmfEventsTable.java    From tracecompass with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Returns true if the column is a visible event column.
 *
 * @param column
 *            the column
 * @return false if the column is the margin column or hidden, true
 *         otherwise
 */
private static boolean isVisibleEventColumn(TableColumn column) {
    if (column.getData(Key.ASPECT) == TmfMarginColumn.MARGIN_ASPECT) {
        return false;
    }
    return !(!column.getResizable() && column.getWidth() == 0);
}