Package es.iiia.shapegrammar.model

Source Code of es.iiia.shapegrammar.model.GeometryPropertySource

package es.iiia.shapegrammar.model;

import java.awt.geom.Point2D;
import java.util.ArrayList;

import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

public class GeometryPropertySource implements IPropertySource {
  private static String positionXID = "positionGeometryX";
  private static String positionYID = "positionGeometryY";
  private static String scaleID = "scaleGeometry";
  private static String rotateID = "rotateGeometry";
  private static String ID = "ID";
 
  private double undoScale = 1;
  private double undoRotate = 0;
 

  protected GeometryModel node;
  // private ArrayList<IPropertyDescriptor> properties;

  public GeometryPropertySource(GeometryModel node) {
    this.node = node;
  }

  public Object getEditableValue() {
    return null;
  }

  protected ArrayList<IPropertyDescriptor> getProperties() {
    ArrayList<IPropertyDescriptor> properties = new ArrayList<IPropertyDescriptor>();
    if (this.node instanceof GeometryModel) {
      properties.add(new PropertyDescriptor(ID, "ID"));
     
      TextPropertyDescriptor desc = new TextPropertyDescriptor(
          NodeModel.PROPERTY_RENAME, "Name");
      properties.add(desc);
      desc = new TextPropertyDescriptor(positionXID, "x");
      desc.setCategory("Geometry");
      properties.add(desc);

      desc = new TextPropertyDescriptor(positionYID, "y");
      desc.setCategory("Geometry");
      properties.add(desc);

      desc = new TextPropertyDescriptor(scaleID, "Scale");
      desc.setCategory("Geometry");
      properties.add(desc);

      desc = new TextPropertyDescriptor(rotateID, "Rotate");
      desc.setCategory("Geometry");
      properties.add(desc);
    }
    return properties;
  }

  public IPropertyDescriptor[] getPropertyDescriptors() {
    return this.getProperties().toArray(new IPropertyDescriptor[0]);
  }

  // TODO: Solve UNDO of resize and rotate
 
  public Object getPropertyValue(Object id) {
    if (id.equals(NodeModel.PROPERTY_RENAME)) {
      return node.getName();
    } else if (id.equals(ID)) {
      return Integer.toString(node.getId());
    } else if (id.equals(positionXID)) {
      return Double.toString(node.getLocation().getX());
    } else if (id.equals(positionYID)) {
      return Double.toString(node.getLocation().getY());
    } else if (id.equals(scaleID)) {
      return "1";
    } else if (id.equals(rotateID)) {
      return "0";
    }
    return null;
  }

  public boolean isPropertySet(Object id) {
    return false;
  }

  public void resetPropertyValue(Object id) {
   
  }

  public void setPropertyValue(Object id, Object value) {
    if (id.equals(NodeModel.PROPERTY_RENAME)) {
      node.setName((String) value);
    } else {
      // number values
      try {
        double number = Double.parseDouble(value.toString());
        if (id.equals(positionXID)) {
          node.setLocation(new Point2D.Double(number, node.getLocation().getY()));
          // node.translate(number -
          // node.getPointList().getBounds().x, 0);
        } else if (id.equals(positionYID)) {
          node.setLocation(new Point2D.Double(node.getLocation().getX(), number));
          // node.translate(0, number -
          // node.getPointList().getBounds().y);
        } else if (id.equals(scaleID)) {
          node.scale(number, number);
        } else if (id.equals(rotateID)) {
          Point2D center = node.getCenter();
          node.rotate(new Point2D.Double(center.getX(),
              center.getY()), (number * Math.PI) / 180);
        }

      } catch (NumberFormatException ex) {
        // TODO: Better show!
        // JOptionPane.showMessageDialog(null, "This is not a number!");
      }
    }
  }

}
TOP

Related Classes of es.iiia.shapegrammar.model.GeometryPropertySource

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.