/*
  * (non-Javadoc)
  *
  * @see org.talend.commons.ui.swt.tableviewer.behavior.DefaultTableLabelProvider#getBackground(java.lang.Object,
  * int)
  */
 @Override
 public Color getBackground(Object element, int columnIndex) {
   if (!(element instanceof IMetadataColumn)) {
     return null;
   }
   IMetadataColumn column = (IMetadataColumn) element;
   TableViewerCreatorColumnNotModifiable tableColumn =
       (TableViewerCreatorColumnNotModifiable) tableViewerCreator.getColumns().get(columnIndex);
   if (column.isCustom()) {
     if (column.isReadOnly()
         || readOnly
         || tableColumn.getId().equals(AbstractMetadataTableEditorView.ID_COLUMN_NAME)) {
       return CELL_READ_ONLY_COLOR;
     } else {
       return CUSTOM_CELL_BG_COLOR;
     }
   }
   if (column.isReadOnly()) {
     return TABLE_READ_ONLY_COLOR;
   }
   // qli modified to fix the bug 6654.
   if (tableEditorView.isShowTalendTypeColumn() && tableEditorView.getCurrentDbms() != null) {
     if (tableColumn.getId().equals(AbstractMetadataTableEditorView.ID_COLUMN_DBTYPE)
         && !"".equals(column.getType()) // $NON-NLS-1$
         && !TypesManager.checkDBType(
             tableEditorView.getCurrentDbms(), column.getTalendType(), column.getType())) {
       return CELL_WRONG_DB_TYPE_COLOR;
     }
   }
   return super.getBackground(element, columnIndex);
 }
  /** DOC sgandon Comment method "setupColumnSortListener". */
  @SuppressWarnings("rawtypes")
  private void setupColumnSortListener() {
    List<TableViewerCreatorColumnNotModifiable> columns = tableViewerCreator.getColumns();
    IColumnSortedListener columnSortedListener =
        new IColumnSortedListener() {

          @Override
          public void handle() {
            BusyIndicator.showWhile(
                Display.getDefault(),
                new Runnable() {

                  @Override
                  public void run() {
                    addInstallButtons();
                  }
                });
          }
        };
    for (TableViewerCreatorColumnNotModifiable column : columns) {
      column.getTableColumnSelectionListener().addColumnSortedListener(columnSortedListener);
    }
  }
  /**
   * DOC amaumont Comment method "resizeControl".
   *
   * @param e
   */
  private synchronized void controlResizedExecute(ControlEvent e) {
    final TableColumn currentTableColumn = (TableColumn) e.widget;
    if (!WindowSystem.isGTK() && !columnsResizingByLayout && (fillHorizontal || continuousLayout)) {
      // System.out.println("controlResizedExecute");
      if (continuousLayout && !fillHorizontal) {
        // asyncThreadingForManualColumnResizingFalse.interrupt();
        // if (!fillHorizontal) {
        // manualResizing = false;
        // }
      }
      if (!manualResizing) {
        manualResizing = true;
        final Table table = currentTableColumn.getParent();
        Rectangle bounds = table.getClientArea();
        // System.out.println("currentTableColumn.getWidth()=" + currentTableColumn.getWidth());
        // System.out.println("columnsResizingByLayout=" + columnsResizingByLayout);
        // System.out.println("currentTableColumn.hashCode()=" + currentTableColumn.hashCode());

        if (table.getHorizontalBar().getSelection() == 0) {
          if (!WindowSystem.isGTK()) {
            changeColumnLayoutData(currentTableColumn, bounds);
          }

          lastDisplayedWidth = bounds.width + widthAdjustValue;
          // System.out.println("lastWidth="+lastDisplayedWidth);
          // System.out.println("referenceWidth="+referenceWidth);

          referenceWidth = computeCurrentTableWidth();

          TableColumn[] tableColumns = table.getColumns();
          if (fillHorizontal && tableColumns.length - 1 >= 0) {
            int widthAll = referenceWidth;

            int indexLastColumn = tableColumns.length - 1;
            TableColumn lastTableColumn = tableColumns[indexLastColumn];
            TableViewerCreatorColumnNotModifiable tableViewerCreatorColumn =
                (TableViewerCreatorColumnNotModifiable)
                    tableViewerCreator.getColumns().get(indexLastColumn);

            ColumnLayoutData columnLayoutData = columnsLayoutData.get(indexLastColumn);
            int minimumWidth = 0;
            if (columnLayoutData instanceof ColumnWeightData) {
              minimumWidth = ((ColumnWeightData) columnLayoutData).minimumWidth;
            } else if (columnLayoutData instanceof ColumnPixelData) {
              minimumWidth = ((ColumnPixelData) columnLayoutData).width;
            }

            int widthLastColumn = lastTableColumn.getWidth();
            int newColumnWidth = lastDisplayedWidth - (widthAll - widthLastColumn);
            if (newColumnWidth > minimumWidth) {
              if (referenceWidth - widthLastColumn < lastDisplayedWidth) {
                if (newColumnWidth > 0) {
                  // System.out.println("change");
                  lastTableColumn.setWidth(newColumnWidth);
                  changeColumnLayoutData(lastTableColumn, bounds);
                }
              } else {
                int width = tableViewerCreatorColumn.getWidth();
                // System.out.println("weight=" + weight);
                // System.out.println("width=" + width);
                if (columnLayoutData instanceof ColumnWeightData) {
                  lastTableColumn.setWidth(width);
                  changeColumnLayoutData(lastTableColumn, bounds);
                }
              }
            }
            referenceWidth = computeCurrentTableWidth() + widthAdjustValue;
            // System.out.println("referenceWidth=" + referenceWidth);
          }
        }
        if (continuousLayout && !fillHorizontal) {
          // asyncThreadingForManualColumnResizingFalse.start();
          Runnable runable =
              new Runnable() {

                public void run() {
                  try {
                    synchronized (this) {
                      wait(500);
                      manualResizing = false;
                    }

                  } catch (InterruptedException e) {
                    manualResizing = false;
                  }
                }
              };
          new Thread(runable).start();
        }
      }

      if (fillHorizontal) {
        manualResizing = false;
      }

      tableViewerCreator.redrawTableEditorControls();
    }
  }