Package org.cloudfoundry.ide.eclipse.server.ui.internal

Source Code of org.cloudfoundry.ide.eclipse.server.ui.internal.TableResizeHelper

/*******************************************************************************
* Copyright (c) 2012, 2014 Pivotal Software, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License,
* Version 2.0 (the "License�); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*  Contributors:
*     Pivotal Software, Inc. - initial API and implementation
********************************************************************************/
package org.cloudfoundry.ide.eclipse.server.ui.internal;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

/**
* Resizes table columns such that columns fit the table width. The table is
* initially resized when the resize is enabled, and its columns continue being
* resized automatically as the user resizes the table.
*
*/
public class TableResizeHelper {

  private final TableViewer tableViewer;

  public TableResizeHelper(TableViewer tableViewer) {
    this.tableViewer = tableViewer;
  }

  public void enableResizing() {
    tableViewer.getTable().addControlListener(new ControlListener() {

      public void controlResized(ControlEvent e) {
        resizeTable();
      }

      public void controlMoved(ControlEvent e) {

      }
    });

    // Initial resize of the columns
    resizeTable();

  }

  protected void resizeTable() {
    Composite tableComposite = tableViewer.getTable().getParent();
    Rectangle tableCompositeArea = tableComposite.getClientArea();
    int width = tableCompositeArea.width;
    resizeTableColumns(width, tableViewer.getTable());
  }

  protected void resizeTableColumns(int tableWidth, Table table) {
    TableColumn[] tableColumns = table.getColumns();

    if (tableColumns.length == 0) {
      return;
    }

    int total = 0;

    // resize only if there is empty space at the end of the table
    for (TableColumn column : tableColumns) {
      total += column.getWidth();
    }

    if (total < tableWidth) {
      // resize the last one
      TableColumn lastColumn = tableColumns[tableColumns.length - 1];
      int newWidth = (tableWidth - total) + lastColumn.getWidth();
      lastColumn.setWidth(newWidth);
    }

  }

}
TOP

Related Classes of org.cloudfoundry.ide.eclipse.server.ui.internal.TableResizeHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.