Package org.asem.eclipse.mii.model.parts

Source Code of org.asem.eclipse.mii.model.parts.DiagramTreeEditPart

/*******************************************************************************
* Copyright (c) 2004, 2005 Elias Volanakis and others.
�* All rights reserved. This program and the accompanying materials
�* are made available under the terms of the Eclipse Public License v1.0
�* which accompanies this distribution, and is available at
�* http://www.eclipse.org/legal/epl-v10.html
�*
�* Contributors:
�*����Elias Volanakis - initial API and implementation
�*******************************************************************************/
package org.asem.eclipse.mii.model.parts;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.asem.eclipse.mii.model.abs.AbstractModelElement;
import org.asem.eclipse.mii.model.abs.ShapeConstants;
import org.asem.eclipse.mii.model.shapes.ShapesDiagram;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.RootEditPart;
import org.eclipse.gef.editparts.AbstractTreeEditPart;
import org.eclipse.gef.editpolicies.RootComponentEditPolicy;


/**
* TreeEditPart for a ShapesDiagram instance. This is used in the Outline View
* of the ShapesEditor.
* <p>
* This edit part must implement the PropertyChangeListener interface, so it can
* be notified of property changes in the corresponding model element.
* </p>
*
* @author Elias Volanakis
*/
@SuppressWarnings("rawtypes")
class DiagramTreeEditPart extends AbstractTreeEditPart implements
    PropertyChangeListener {

  /**
   * Create a new instance of this edit part using the given model element.
   *
   * @param model
   *            a non-null ShapesDiagram instance
   */
  DiagramTreeEditPart(ShapesDiagram model) {
    super(model);
  }

  /**
   * Upon activation, attach to the model element as a property change
   * listener.
   */
  public void activate() {
    if (!isActive()) {
      super.activate();
      ((AbstractModelElement) getModel()).addPropertyChangeListener(this);
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.gef.examples.shapes.parts.ShapeTreeEditPart#createEditPolicies
   * ()
   */
  protected void createEditPolicies() {
    // If this editpart is the root content of the viewer, then disallow
    // removal
    if (getParent() instanceof RootEditPart) {
      installEditPolicy(EditPolicy.COMPONENT_ROLE,
          new RootComponentEditPolicy());
    }
  }

  /**
   * Upon deactivation, detach from the model element as a property change
   * listener.
   */
  public void deactivate() {
    if (isActive()) {
      super.deactivate();
      ((AbstractModelElement) getModel()).removePropertyChangeListener(this);
    }
  }

  private ShapesDiagram getCastedModel() {
    return (ShapesDiagram) getModel();
  }

  /**
   * Convenience method that returns the EditPart corresponding to a given
   * child.
   *
   * @param child
   *            a model element instance
   * @return the corresponding EditPart or null
   */
  private EditPart getEditPartForChild(Object child) {
    return (EditPart) getViewer().getEditPartRegistry().get(child);
  }

  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren()
   */
    protected List getModelChildren() {
    return getCastedModel().getChildren(); // a list of shapes
  }

  /*
   * (non-Javadoc)
   *
   * @see java.beans.PropertyChangeListener#propertyChange(java.beans.
   * PropertyChangeEvent)
   */
  public void propertyChange(PropertyChangeEvent evt) {
    String prop = evt.getPropertyName();
    if (ShapeConstants.CHILD_ADDED_PROP.equals(prop)) {
      // add a child to this edit part
      // causes an additional entry to appear in the tree of the outline
      // view
      addChild(createChild(evt.getNewValue()), -1);
    } else if (ShapeConstants.CHILD_REMOVED_PROP.equals(prop)) {
      // remove a child from this edit part
      // causes the corresponding edit part to disappear from the tree in
      // the outline view
      removeChild(getEditPartForChild(evt.getNewValue()));
    } else {
      refreshVisuals();
    }
  }
}
TOP

Related Classes of org.asem.eclipse.mii.model.parts.DiagramTreeEditPart

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.