Package hidb2.gui.util

Source Code of hidb2.gui.util.PojoCompEditor

/**
* 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.Application;
import hidb2.kern.AttrImage;
import hidb2.kern.AttrLayout;
import hidb2.kern.DataPath;
import hidb2.kern.HIDBConst;
import hidb2.kern.StatusCycle;

import java.sql.Date;
import java.sql.Time;
import java.util.Calendar;
import java.util.logging.Logger;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapData;

/**
* Attribute's Editor to be used in generated form.
*
*/
public class PojoCompEditor implements HIDBConst
  {
  final static Logger log = Logger.getLogger("hidb2.gui.editor");

  protected AttrLayout _al;

  protected AbstractEditor _edPart;

  protected Label _lab;

  protected Control _wid;

  protected FormEntry _fEntry;

  /** Local data pointer for dialog return via callback */
  protected Object _data = null;

  /** When the attribut is an image, shall keep a reference for redraw purpose */
  //  private Image _displayedImage = null;
  //
  //  private Point _origin = new Point(0, 0);

  public PojoCompEditor(AbstractEditor edPart, Composite parent, AttrLayout attrLayout)
    {
    _al = attrLayout;

    _edPart = edPart;

    createEditor(edPart.toolkit, parent);
    }

  private void createLabel(Composite parent)
    {
    _lab = new Label(parent, SWT.NONE);

    _edPart.toolkit.adapt(_lab, false, false);

    _lab.setText(_al.getAttr().getName() + " :");

    TableWrapData twd = new TableWrapData(TableWrapData.FILL_GRAB);

    _lab.setLayoutData(twd);
    }

  /**
   * Create the Table Cell Editor for the given attribute.
   *
   * Used by description editors ? duplicated in Parts ?!?
   *
   * @param table   Future Parent Table
   * @return
   */
  public Control createEditor(FormToolkit toolkit, Composite parent)
    {
    if (_wid == null)
      {
      switch (_al.getAttr().getType())
        {
        case T_Integer:
        case T_String:
        case T_Double:
          _fEntry = new FormEntry(parent, toolkit, _al.getAttr().getName() + " :", null, false);
          _fEntry.setFormEntryListener(new FormEntryAdapter(_edPart, null)
            {
              public void textValueChanged(FormEntry entry)
                {
                //                getFD().setIconAttrIndex(Integer.parseInt(entry.getValue()));
                //                getFD().modify();
                _edPart.setDirty(true);
                }
            });

          _fEntry.setEditable(_edPart.isEditable());
          break;

        case T_Boolean:
          createLabel(parent);
          _wid = new Button(parent, SWT.CHECK);
          ((Button) _wid).addSelectionListener(new SelectionAdapter()
            {
              @Override
              public void widgetSelected(SelectionEvent e)
                {
                _edPart.setDirty(true);
                }
            });

          break;

        case T_Date:
          createLabel(parent);
          _wid = new DateTime(parent, SWT.DATE | SWT.DROP_DOWN);
          break;

        case T_Time:
          createLabel(parent);
          _wid = new DateTime(parent, SWT.TIME);
          break;

        case T_TimeStamp:
          createLabel(parent);
          _wid = new DateTime(parent, SWT.DATE | SWT.DROP_DOWN);
          break;

        case T_Color: // Color

          createLabel(parent);
          _wid = new Button(parent, SWT.PUSH);

          // Add the callback
          ((Button) _wid).addSelectionListener(new SelectionAdapter()
            {
              @Override
              public void widgetSelected(SelectionEvent e)
                {
                ColorDialog cdlg = new ColorDialog(_wid.getShell());

                if (_data != null)
                  {
                  cdlg.setRGB((RGB) _data);
                  }

                RGB rrgb = cdlg.open();

                if (rrgb != null)
                  {
                  _edPart.setDirty(true);
                  _data = rrgb;
                  ((Button) _wid).setText(rrgb.toString());
                  ((Button) _wid).setBackground(new Color(_wid.getDisplay(), rrgb));
                  }
                }
            });
          break;

        case T_RefList: // RefList
          log.warning("T_RefList : Not Implemented yet");
          break;

        case T_TypeEnum:
          log.warning("Not Allowed here");
          break;

        case T_Image:
          createLabel(parent);
          _wid = new ImageCanvas(parent, SWT.NO_BACKGROUND);
         
          _wid.addMouseListener(new MouseListener()
            {
              FileDialog _fd;

              //          Tracker tracker = new Tracker(_drawArea, SWT.RESIZE);
              @Override
              public void mouseDoubleClick(MouseEvent e)
                {
                AttrImage ai = (AttrImage) _data;

                // Set initial values (directory)
                if (_fd == null)
                  {
                  _fd = new FileDialog(_wid.getShell());
                  }

                if (ai == null)
                  {
                  _data = ai = new AttrImage();
                  ai.setChecker(_al.getAttr().getChecker());
                  }

                String fullname = ai.getAbsolutePath();

                if (fullname != null)
                  {
                  _fd.setFileName(fullname);
                  }

                fullname = _fd.open();

                if (fullname != null)
                  {
                  // Find & Use correct DataPath               
                  DataPath rDp = Application.getDataStore().findDataPath(fullname);

                  ai.setDataPath(rDp);

                  if (rDp != null)
                    {
                    fullname = fullname.substring(rDp.getPath().length());
                    }

                  ai.setValue(fullname);

                  if (ai.getStatus() == StatusCycle.UNCHANGED)
                    {
                    ai.setStatus(StatusCycle.MODIFIED);
                    }

                  _edPart.setDirty(true);
                  }
                else
                  {
                  _data = ai = null;
                  }
                }

              @Override
              public void mouseDown(MouseEvent e)
                {
                }

              @Override
              public void mouseUp(MouseEvent e)
                {
                }
            });

          break;

        default:
          throw new IllegalArgumentException();
        }

      if (_wid != null)
        {
        toolkit.adapt(_wid, true, true);
        _wid.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
        }
      }

    return _wid;
    }

  /**
   * Fill the input widget with the given data.
   *
   * @param v
   */
  public void setValue(Object v)
    {
    Calendar cd = null;

    _data = v;

    switch (_al.getAttr().getType())
      {
      case T_Integer:
      case T_String:
      case T_Double:
        _fEntry.setValue((v == null) ? "" : v.toString(), true);
        break;

      case T_Boolean:
        ((Button) _wid).setSelection((Boolean) v);
        break;

      case T_Date:
        cd = Calendar.getInstance();
        cd.clear();
        cd.setTime((Date) v);
        ((DateTime) _wid).setDate(cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH));
        break;

      case T_Time:
        cd = Calendar.getInstance();
        cd.clear();
        cd.setTime((Time) v);
        ((DateTime) _wid).setTime(cd.get(Calendar.HOUR_OF_DAY), cd.get(Calendar.MINUTE), cd.get(Calendar.SECOND));
        break;

      case T_TimeStamp: // TODO: T_TimeStamp
        //          _cellEdt = Parts.createDateTimeEditor(table, SWT.CALENDAR | SWT.TIME);
        break;

      case T_Color: // Color
        ((Button) _wid).setText(v.toString());
        ((Button) _wid).setBackground(new Color(_wid.getDisplay(), (RGB) v));
        break;

      case T_RefList: // TODO: RefList
        log.warning("T_RefList : Not Implemented yet");
        break;

      case T_TypeEnum:
        log.warning("Not Allowed here");
        break;

      case T_Image:
        log.warning("Not fully Implemented yet");
        AttrImage ai = (AttrImage) v;

        if (ai != null)
          {
          // TODO : Use right size for the square icon
          ((ImageCanvas) _wid).setImage(ai.getSqrImage(_wid.getDisplay(), 64));
          _wid.redraw();
          }

        break;

      default:
        throw new IllegalArgumentException();
      }
    }

  public Object getValue()
    {
    Object or = null;
    Calendar cd = null;
    String s = null;

    switch (_al.getAttr().getType())
      {
      case T_Integer:
        s = _fEntry.getValue();
        or = Integer.parseInt(s);
        break;

      case T_String:
        or = _fEntry.getValue();
        break;

      case T_Double:
        s = _fEntry.getValue();
        or = Double.parseDouble(s);
        break;

      case T_Boolean:
        or = ((Button) _wid).getSelection();
        break;

      case T_Date:
        cd = Calendar.getInstance();
        cd.clear();
        cd.set(((DateTime) _wid).getYear(), ((DateTime) _wid).getMonth(), ((DateTime) _wid).getDay());
        or = new Date(cd.getTimeInMillis());
        break;

      case T_Time:
        cd = Calendar.getInstance();
        cd.clear();
        cd.set(((DateTime) _wid).getYear(), ((DateTime) _wid).getMonth(), ((DateTime) _wid).getDay(), ((DateTime) _wid)
            .getHours(), ((DateTime) _wid).getMinutes(), ((DateTime) _wid).getSeconds());
        or = new Time(cd.getTimeInMillis());
        break;

      case T_TimeStamp: // TODO : Read TimeStamp
        break;

      case T_Color: // Read Color
        or = _data;
        break;

      case T_RefList: // RefList
        log.warning("T_RefList : Not Implemented yet");
        break;

      case T_TypeEnum:
        log.warning("Not Allowed here");
        break;

      case T_Image: // Nothing to do
        break;

      default:
        throw new IllegalArgumentException();
      }

    return or;
    }

  }
TOP

Related Classes of hidb2.gui.util.PojoCompEditor

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.