Package hidb2.gui.util

Source Code of hidb2.gui.util.PoserTableViewer

/**
* This file is part of HIDB2.
*
* HIDB2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HIDB2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with HIDB2.  If not, see <http://www.gnu.org/licenses/>.
*/
package hidb2.gui.util;

import hidb2.gui.ressources.RscMan;
import hidb2.kern.AttrImage;
import hidb2.kern.AttrType;
import hidb2.kern.HIDBConst;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.eclipse.jface.util.Util;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
* TableViewer dedicated to generic Attributs.
*/
public class PoserTableViewer extends TableViewer implements HIDBConst
  {
  final static Logger log = Logger.getLogger("hidb2.gui.util");

  protected List<PojoColumnDescr> _lstAttr = new ArrayList<PojoColumnDescr>();

  private String[] _columnProp;

  private PoserTableLiner _poserLiner = null;

  private CellEditor[] _cellEdt;

  private List<ModifyListener> _lstModLstn = new ArrayList<ModifyListener>();

  public PoserTableViewer(Composite parent)
    {
    super(parent, SWT.SINGLE | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
    }

  public PoserTableViewer(Composite parent, int style)
    {
    super(parent, style);
    }

  public void addAttribut(PojoColumnDescr attr)
    {
    _lstAttr.add(attr);
    }

  /**
   * Add a new editable attribute to be displayed in the TableViewer.
   * <b>Don't forget to call createMMI() after attributes definition.</b>
   *
   * @param pAttrName Java Name of attribut. Prefixed by "set", "get" or "is" according to the type.
   * @param pAttrType Type of the attribut. in the range:  AttrType
   * @param pLabel    Label of the GUI (title of the column)
   * @param colSize  Size of the editing column
   */
  public void addAttribut(String attrName, AttrType attrType, String label, int colSize)
    {
    PojoColumnDescr attr = new PojoColumnDescr(attrName, attrType, label, colSize, true);

    addAttribut(attr);
    }

  /**
   * Add a new Read-Only attribute to be displayed in the TableViewer.
   * <b>Don't forget to call createMMI() after attributes definition.</b>
   *
   * @param pAttrName Java Name of attribut. Prefixed by "set", "get" or "is" according to the type.
   * @param pAttrType Type of the attribut. in the range:   C_TYPE_INT, C_TYPE_STRING, C_TYPE_BOOLEAN,
   *                  C_TYPE_DOUBLE, C_TYPE_VECTOR3D, C_TYPE_COLOR3D, C_TYPE_MESHEDOBJECTREF, C_TYPE_CHANNELREF
   * @param pLabel    Label of the GUI (title of the column)
   * @param colSize  Size of the editing column
   */
  public void addROAttribut(String attrName, AttrType attrType, String label, int colSize)
    {
    PojoColumnDescr attr = new PojoColumnDescr(attrName, attrType, label, colSize, false);

    addAttribut(attr);
    }

  public void setPoserTableLiner(PoserTableLiner ptl)
    {
    _poserLiner = ptl;
    }

  public List<PojoColumnDescr> getLstAttr()
    {
    return _lstAttr;
    }


  /**
   * This method customizes the titles and the contents of the columns.
   * It installs the editors.
   * It shall be called after the definition of all attributes.
   *
   */
  public void createMMI()
    {
    createMMI(new GenericTabLabelProvider());
    }
 
  /**
   * This method customizes the titles and the contents of the columns.
   * It installs the editors.
   * It shall be called after the definition of all attributes.
   *
   */
  public void createMMI(ITableLabelProvider tblLabelProv)
    {
    // Check first that definition data are complete
    _columnProp = new String[_lstAttr.size()];
    for (int i = 0; i < _lstAttr.size(); i++)
      {
      _columnProp[i] = _lstAttr.get(i).attrName;
      }

    Table tabFile = getTable();

    tabFile.setHeaderVisible(true);
    tabFile.setLinesVisible(true);

    setContentProvider(new GenericTabContentProvider());
    setLabelProvider(tblLabelProv);

    // Column titles
    TableColumn colonne;
    TableSortSelectionListener listener;
    for (int indice = 0; indice < _lstAttr.size(); indice++)
      {
      PojoColumnDescr pcd = _lstAttr.get(indice);

      colonne = new TableColumn(tabFile, SWT.LEFT);
      colonne.setText(pcd.label);
      colonne.setWidth(pcd.columnSize);

      listener = new TableSortSelectionListener(this, colonne, new GenericTabSorter(pcd), SWT.UP, false);

      // Initialise sort for 1st column
      if (indice == 0)
        {
        listener.chooseColumnForSorting();
        }
      }

    //    for (int i = 0, n = tabFile.getColumnCount(); i < n; i++)
    //      {
    //      tabFile.getColumn(i).pack();
    //      }

    // Set the editors, cell modifier, and column properties
    setColumnProperties(_columnProp);
    setCellModifier(new PojoCellModifier(this));
    setCellEditors(createCellEditor());

    // Set Table Color
    TableItem item;
    for (int i = 0, n = tabFile.getItemCount(); i < n; i++)
      {
      item = tabFile.getItem(i);
      item.setBackground(RscMan.getColor(239, 239, 231));
      }

    //    tabFile.addListener(SWT.MeasureItem, new Listener()
    //      {
    //        public void handleEvent(Event event)
    //          {
    //          TableItem ti = (TableItem) event.item;
    //          System.out.println("TI.text=" + ti.getText());
    //          ti.setForeground(RessourceManager.getColor(255, 0, 0));        
    //          }
    //      });
    //
    //    tabFile.addListener(SWT.EraseItem, new Listener()
    //      {
    //        public void handleEvent(Event event)
    //          {
    //          // Efface toute la ligne : event.detail &= ~SWT.FOREGROUND;
    //          }
    //      });

    tabFile.addListener(SWT.PaintItem, new Listener()
      {
        public void handleEvent(Event event)
          {
          if (_poserLiner != null)
            {
            _poserLiner.paintItem(event);
            }
          }
      });

    }

  public CellEditor[] createCellEditor()
    {
    _cellEdt = new CellEditor[_columnProp.length];

    for (int i = 0; i < _lstAttr.size(); i++)
      {
      _cellEdt[i] = _lstAttr.get(i).createCellEditor(getTable());
      }

    return _cellEdt;
    }

  /**
   * Set the layout data on the undelying SWT table.
   * @param ld
   */
  public void setLayoutData(Object ld)
    {
    getTable().setLayoutData(ld);
    }

  /**
   * Adds the listener to the collection of listeners who will
   * be notified when the receiver's text is modified, by sending
   * it one of the messages defined in the <code>ModifyListener</code>
   * interface.
   *
   * @param listener the listener which should be notified
   *
   * @exception IllegalArgumentException <ul>
   *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
   * </ul>   *
   */
  public void addModifyListener(ModifyListener listener)
    {
    if (listener != null)
      {
      _lstModLstn.add(listener);
      }
    }

  public void fireEvent(Object data)
    {
    Event ev = new Event();
    ev.data = data;
    ev.widget = getTable();

    ModifyEvent me = new ModifyEvent(ev);

    for (ModifyListener ml : _lstModLstn)
      {
      ml.modifyText(me);
      }
    }
 
 
  class GenericTabLabelProvider implements ITableLabelProvider
    {
    public Image getColumnImage(Object element, int columnIndex)
      {
      Image img = null;

      PojoColumnDescr pcol = _lstAttr.get(columnIndex);
     
      // The Element is a ValuedInstance
      if (pcol.attrType == AttrType.T_Image)
        {
        AttrImage ai = (AttrImage) pcol.get(element);

        if (ai != null)
          {
          img = ai.getSqrImage(getTable().getDisplay(), 128);
          }
        }

      return img;
      }

    public String getColumnText(Object element, int columnIndex)
      {
      return _lstAttr.get(columnIndex).format(element);
      }

    public void addListener(ILabelProviderListener listener)
      {
      }

    public void dispose()
      {
      }

    public boolean isLabelProperty(Object element, String property)
      {
      return true;
      }

    public void removeListener(ILabelProviderListener listener)
      {
      }
    }

  // Input = List<DataFile>
  class GenericTabContentProvider implements IStructuredContentProvider
    {
    public GenericTabContentProvider()
      {

      }

    public Object[] getElements(Object inputElement)
      {
      return (inputElement instanceof List<?>) ? ((List<?>) inputElement).toArray() : (Object[]) inputElement;
      }

    public void dispose()
      {
      }

    public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
      {
      }
    }

  /**
   * Sorter for any Poser data represented in a Table.
   *
   */
  class GenericTabSorter extends AbstractInvertableSorter
    {
    /** Concerned Column */
    private PojoColumnDescr _pcd;

    /**
     * Constructor
     * @param attrDescr    Attribut Descriptor
     */
    public GenericTabSorter(PojoColumnDescr pcd)
      {
      _pcd = pcd;
      }

    public int compare(Viewer viewer, Object e1, Object e2)
      {
      return Util.compare((Comparable<?>) _pcd.get(e1), (Comparable<?>) _pcd.get(e2));
      }
    }

  final public int getPropIndex(String s)
    {
    int res = 0;
    while (res < _columnProp.length)
      {
      if (s.equals(_columnProp[res]))
        {
        return res;
        }
      else
        {
        res++;
        }
      }

    return res;
    }

  public boolean isModifiable(Object element, String property)
    {
    int idx = getPropIndex(property);

    boolean b = _lstAttr.get(idx).editable;
    return b;
    }

  }
TOP

Related Classes of hidb2.gui.util.PoserTableViewer

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.