/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under 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.
 */

package org.apache.uima.pear.insd.edit.vars;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableColumn;

/**
 * Handles the display and management of an environment variables table.
 */
public class VarValViewerHandler extends AbstractVarValViewerHandler {

  /**
   * Instantiates a new var val viewer handler.
   *
   * @param parent
   *          the parent
   * @param columnNames
   *          the column names
   * @param numParentColumns
   *          the num parent columns
   * @param tableRowList
   *          the table row list
   */
  public VarValViewerHandler(Composite parent, String[] columnNames, int numParentColumns,
          VarValList tableRowList) {
    super(parent, columnNames, numParentColumns, tableRowList, new VarValLabelProvider());
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.uima.pear.insd.edit.vars.AbstractVarValViewerHandler#createCellModifiers()
   */
  @Override
  protected ICellModifier createCellModifiers() {
    return new VarValCellModifier(this, columnNames, tableRowList);
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.uima.pear.insd.edit.vars.AbstractVarValViewerHandler#createSorter()
   */
  @Override
  protected ViewerSorter createSorter() {
    return new VarValSorter(VarValSorter.VAR_NAME);
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.uima.pear.insd.edit.vars.AbstractVarValViewerHandler#createTableColumns()
   */
  @Override
  protected void createTableColumns() {
    // 1st column with image/checkboxes - NOTE: The SWT.CENTER has no effect!!
    TableColumn column = new TableColumn(table, SWT.LEFT, 0);
    column.setText("Property Name");
    column.setWidth(200);
    // Add listener to column so tableRows are sorted by description when clicked
    column.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        tableViewer.setSorter(new VarValSorter(VarValSorter.VAR_NAME));
      }
    });

    // 2nd column with tableRow Description
    column = new TableColumn(table, SWT.LEFT, 1);
    column.setText("Property Value");
    column.setWidth(250);
    // Add listener to column so tableRows are sorted by description when clicked
    column.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        tableViewer.setSorter(new VarValSorter(VarValSorter.VAR_VALUE));
      }
    });

  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.uima.pear.insd.edit.vars.AbstractVarValViewerHandler#createCellEditors()
   */
  @Override
  protected CellEditor[] createCellEditors() {

    CellEditor[] editors = new CellEditor[columnNames.length];
    TextCellEditor textEditor = new TextCellEditor(table);
    editors[0] = textEditor;

    textEditor = new TextCellEditor(table);
    editors[1] = textEditor;

    return editors;
  }
}