Package hidb2.gui.util

Source Code of hidb2.gui.util.PojoPageEditor

/**
* 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.Attribut;
import hidb2.kern.AttributedDescriptionLayout;
import hidb2.kern.DataPath;
import hidb2.kern.HIDBConst;
import hidb2.kern.StatusCycle;
import hidb2.kern.ValuedInstance;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

/**
* Editing page of an Attributed Description.
*
*/
public class PojoPageEditor extends Composite implements HIDBConst
  {
  /** The desecription layout that gives access to the layout of attribut list */
  private AttributedDescriptionLayout _layout;

  /** Keep editorPart to fire modification events */
  private AbstractEditor _edPart;

  private ImageCanvas _imgArea = null;

  private int[] _mapIdx;

  private PojoCompEditor[] _mapCompEditor;

  private AttrImage _attrImg = null;

  private AttrLayout _attrImgLayout = null;

  public PojoPageEditor(AbstractEditor edPart, Composite parent, AttributedDescriptionLayout descrLayout)
    {
    super(parent, SWT.NONE); // descrLayout.isVertical() ? SWT.VERTICAL : SWT.HORIZONTAL);

    _edPart = edPart;

    _layout = descrLayout;

    createMMI();
    }

  private void createMMI()
    {
    int iai = _layout.getImageIdx();

    _edPart.toolkit.adapt(this);

    // Essai du 24-Juillet-2011 : OK
    setLayout(FormLayoutFactory.createClearGridLayout(false, 2));
    setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));

    if (iai >= 0)
      {
      _attrImgLayout = _layout.getLstLayout().get(iai);
      Point s = _attrImgLayout.getSize();

      // Have to manage an Image Input section
      _imgArea = new ImageCanvas(this, SWT.NONE);
      _edPart.toolkit.adapt(_imgArea);

      GridData gd = new GridData();
      gd.heightHint = s.y;
      gd.widthHint = s.x;

      _imgArea.setLayoutData(gd);

      _imgArea.setSize(s);

      _imgArea.initZoomMenu();

      _imgArea.addMouseListener(new MouseListener()
        {
          FileDialog _fd;

          @Override
          public void mouseDoubleClick(MouseEvent e)
            {
            // Set initial values (directory)
            if (_fd == null)
              {
              _fd = new FileDialog(getShell());
              }

            if (_attrImg == null)
              {
              _attrImg = new AttrImage();
              _attrImg.setChecker(_attrImgLayout.getAttr().getChecker());
              }

            String fullname = _attrImg.getAbsolutePath();

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

            fullname = _fd.open();

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

              _attrImg.setDataPath(rDp);

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

              _attrImg.setValue(fullname);

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

              _imgArea.setImagePath(_attrImg.getAbsolutePath());

              _imgArea.redraw();

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

          @Override
          public void mouseDown(MouseEvent e)
            {
            }

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

    // Create the composite that holds the 'standards' input fields
    Composite compColumn = _edPart.toolkit.createComposite(this);
    TableWrapLayout twl = FormLayoutFactory.createFormPaneTableWrapLayout(true, 2 * _layout.getColumnCount());
    twl.leftMargin = 3;
    twl.rightMargin = 3;
    compColumn.setLayout(twl);

    GridData gd2 = new GridData();
    gd2.horizontalAlignment = GridData.FILL;
    gd2.grabExcessHorizontalSpace = true;
    compColumn.setLayoutData(gd2);

    int nbcol = _layout.getDescr().getAttributList().size();

    _mapIdx = new int[nbcol];
    _mapCompEditor = new PojoCompEditor[nbcol];

    for (int i = 0; i < nbcol; i++)
      {
      Attribut attr = _layout.getDescr().getAttributList().get(i);
      _mapIdx[attr.getDisplayOrder()] = i;

      if (i != _layout.getImageIdx())
        {
        _mapCompEditor[attr.getDisplayOrder()] = new PojoCompEditor(_edPart, compColumn, _layout.getLstLayout().get(i));
        }
      }
    }

  /**
   * Fill the input fields with data of the valued instance
   * @param vi
   */
  public void setValues(ValuedInstance vi)
    {
    int iai = _layout.getImageIdx();

    if (iai >= 0)
      {
      _attrImg = (AttrImage) vi.getValue(iai);

      if (_attrImg != null)
        {
        _imgArea.setImagePath(_attrImg.getAbsolutePath());

        _imgArea.redraw();
        }
      }

    for (int i = 0; i < _layout.getDescr().getAttributList().size(); i++)
      {
      if (_mapIdx[i] != iai)
        {
        _mapCompEditor[i].setValue(vi.getValue(_mapIdx[i]));
        }
      }
    }

  /**
   * Retrieve values from fields and populate the valued instance.
   *
   * @param vi
   */
  public void getValues(ValuedInstance vi)
    {
    int iai = _layout.getImageIdx();

    for (int i = 0; i < _layout.getDescr().getAttributList().size(); i++)
      {
      if (_mapIdx[i] != iai)
        {
        vi.setValue(_mapIdx[i], _mapCompEditor[i].getValue());
        }
      else
        {
        //??
        }
      }
    }
  }
TOP

Related Classes of hidb2.gui.util.PojoPageEditor

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.