/*****************************************************************************
 *  Limpet - the Lightweight InforMation ProcEssing Toolkit
 *  http://limpet.info
 *
 *  (C) 2015-2016, Deep Blue C Technologies Ltd
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the Eclipse Public License v1.0
 *  (http://www.eclipse.org/legal/epl-v10.html)
 *
 *  This library 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.
 *****************************************************************************/
package info.limpet.ui.propertyeditors;

import java.text.DecimalFormat;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Slider;

public class SliderCellEditor extends CellEditor
{

  private Composite _myControl = null;
  private Label _myLabel;
  private Slider _theSlider;
  private int minValue;
  private int maxValue;

  public SliderCellEditor(final Composite parent, int minValue, int maxValue)
  {
    super(parent, SWT.NONE);
    this.minValue = minValue;
    this.maxValue = maxValue;
  }

  /**
   * @return
   */
  public LayoutData getLayoutData()
  {
    final CellEditor.LayoutData res = super.getLayoutData();
    res.grabHorizontal = true;
    return res;
  }

  protected Control createControl(final Composite parent)
  {
    final Font font = parent.getFont();
    final Color bg = parent.getBackground();

    _myControl = new Composite(parent, getStyle());
    _myControl.setFont(font);
    _myControl.setBackground(bg);

    final GridLayout rl = new GridLayout();
    rl.marginWidth = 0;
    rl.marginHeight = 0;
    rl.numColumns = 8;

    _myControl.setLayout(rl);
    _myLabel = new Label(_myControl, SWT.NONE);
    _myLabel.setText("000");
    _myLabel.setBackground(bg);
    final GridData gd1 = new GridData(GridData.FILL_HORIZONTAL);
    _myLabel.setLayoutData(gd1);

    _theSlider = new Slider(_myControl, SWT.HORIZONTAL);
    final GridData gd2 = new GridData(GridData.FILL_HORIZONTAL);
    gd2.horizontalSpan = 7;
    _theSlider.setLayoutData(gd2);
    _theSlider.addSelectionListener(new SelectionListener()
    {

      public void widgetSelected(final SelectionEvent e)
      {
        _myLabel.setText(formatMe(_theSlider.getSelection()));
        _myLabel.update();
      }

      public void widgetDefaultSelected(final SelectionEvent e)
      {

      }
    });

    return _myControl;
  }

  protected Object doGetValue()
  {
    Object res = null;
    if (_theSlider != null)
    {
      res = _theSlider.getSelection() + "";
    }
    return res;
  }

  protected void doSetFocus()
  {
    _theSlider.setFocus();
  }

  private DecimalFormat _df = null;

  protected String formatMe(final int value)
  {
    if (_df == null)
    {
      _df = new DecimalFormat("000");
    }

    return _df.format(value);
  }

  protected void doSetValue(final Object value)
  {
    final String curr = (String) value;
    if (_myLabel != null)
    {
      _myLabel.setText(curr);
    }
    if (_theSlider != null)
    {
      _theSlider.setMaximum(maxValue);
      _theSlider.setMinimum(minValue);
      _theSlider.setIncrement(1);
      _theSlider.setPageIncrement(5);
      _theSlider.setSelection(new Double(curr).intValue());
      _theSlider.setThumb(1);
    }
  }

}

@@ -1370,16 +1370,47 @@
 Slider;%0A
+  private Object _storedValue;%0A
   privat
@@ -2289,24 +2289,45 @@
 ext(%22000%22);%0A
+    _myLabel.pack();%0A
     _myLabel
@@ -3082,45 +3082,248 @@
     
-res = _theSlider.getSelection() + %22%22;
+// see what the stored type is%0A      if (_storedValue instanceof String)%0A      %7B%0A        res = _theSlider.getSelection() + %22%22;%0A      %7D%0A      else if (_storedValue instanceof Integer)%0A      %7B%0A        res = _theSlider.getSelection();%0A      %7D%0A
 %0A   
@@ -3665,36 +3665,22 @@
     
-final String curr = (String)
+_storedValue =
  val
@@ -3738,20 +3738,26 @@
 setText(
-curr
+%22%22 + value
 );%0A    %7D
@@ -3951,31 +3951,210 @@
     
-_theSlider.setS
+final Integer selection;%0A      if (value instanceof Integer)%0A      %7B%0A        selection = new Double((Integer) value).intValue();%0A      %7D%0A      else if (value instanceof String)%0A      %7B%0A        s
 election
 (new
@@ -4149,17 +4149,19 @@
 election
-(
+ = 
 new Doub
@@ -4167,26 +4167,185 @@
 ble(
-curr).intValue());
+(String) value).intValue();%0A      %7D%0A      else%0A      %7B%0A        selection = null;%0A      %7D%0A      if (selection != null)%0A      %7B%0A        _theSlider.setSelection(selection);%0A      %7D
 %0A   

allow the subject value to be specified as a string or an intenger