HighlightElementsAction.java :  » UML » model-analysis-framework » de » uniAugsburg » MAF » bridge » jwt » ui » actions » Java Open Source

Java Open Source » UML » model analysis framework 
model analysis framework » de » uniAugsburg » MAF » bridge » jwt » ui » actions » HighlightElementsAction.java
/**
 * File: HighlightElementsAction.java
 * Created: 25.08.2010
 * 
 * /****************************************************************************
 * *** Copyright (c) 2009-2010 University of Augsburg, Germany <www.ds-lab.org>
 * 
 * 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: Christian Saad, Programming distributed Systems Lab, University
 * of Augsburg - initial API and implementation
 *******************************************************************************/
package de.uniAugsburg.MAF.bridge.jwt.ui.actions;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.draw2d.Figure;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.jface.action.Action;
import org.eclipse.jwt.meta.model.core.ReferenceableElement;
import org.eclipse.jwt.meta.model.processes.Activity;
import org.eclipse.jwt.meta.model.processes.ActivityLinkNode;
import org.eclipse.jwt.meta.model.processes.Scope;
import org.eclipse.jwt.we.editors.WEEditor;
import org.eclipse.jwt.we.editors.pages.activityEditor.WEEditorSheet;
import org.eclipse.jwt.we.misc.util.GeneralHelper;
import org.eclipse.jwt.we.model.view.Reference;
import org.eclipse.swt.graphics.Color;

import de.uniAugsburg.MAF.bridge.jwt.ui.helper.UIVisualizerHelper;


/**
 * Action that highlights a set of result elements.
 */
public class HighlightElementsAction extends Action
{

  /**
   * RefElement.
   */
  private Set<ReferenceableElement> refElements;

  /**
   * Paths of the result elements to highlight.
   */
  private Set<List<EObject>> resultElementPaths;

  /**
   * Map<ActivityPath, Map<OriginalElement, CopyElement>>
   */
  private Map<List<EObject>, Map<EObject, EObject>> activitiesContentsOrigToCopyMap;


  /**
   * The constructor.
   */
  public HighlightElementsAction(String text,
      Map<List<EObject>, Map<EObject, EObject>> activityPathCopyMap,
      Set<List<EObject>> resultElementPaths, Set<ReferenceableElement> refElements)
  {
    super();
    this.activitiesContentsOrigToCopyMap = activityPathCopyMap;
    this.resultElementPaths = resultElementPaths;
    this.refElements = refElements;

    this.setText(text);
  }


  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.jface.action.Action#run()
   */
  @Override
  public void run()
  {
    WEEditor weeditor = GeneralHelper.getActiveInstance();
    if (weeditor == null)
      return;

    // for all activities...
    for (Entry<List<EObject>, Map<EObject, EObject>> entry : activitiesContentsOrigToCopyMap
        .entrySet())
    {
      // extract values
      List<EObject> origActivityPath = entry.getKey();
      EObject activityOriginal = origActivityPath.get(origActivityPath.size() - 1);
      Map<EObject, EObject> originalToCopyMap = entry.getValue();

      // get the corresponding displayed activity (editorsheet)
      Activity displayedActivity = null;
      if (activityOriginal instanceof Activity)
        displayedActivity = (Activity) originalToCopyMap.get(activityOriginal);
      else if (activityOriginal instanceof ActivityLinkNode)
        displayedActivity = (Activity) originalToCopyMap
            .get(((ActivityLinkNode) activityOriginal).getLinksto());
      WEEditorSheet weeditorsheet = (WEEditorSheet) weeditor
          .getPageForActivity(displayedActivity);

      // for all elements in displayed activity...
      for (Object origElement : originalToCopyMap.keySet())
      {
        // get editpart figure for element
        EditPart editpart = (EditPart) weeditorsheet.getGraphicalViewer()
            .getEditPartRegistry().get(originalToCopyMap.get(origElement));

        if (!(editpart instanceof GraphicalEditPart))
          continue;

        Figure figure = (Figure) ((GraphicalEditPart) editpart).getFigure();

        if (editpart.getModel() instanceof Scope)
          continue;

        // check if contained
        boolean found = false;
        for (List<EObject> elementPath : resultElementPaths)
        {
          // extract activity path of result element
          List<EObject> resultElementActivityPath = UIVisualizerHelper
              .extractActivityPath(elementPath);
          Object resultElement = UIVisualizerHelper.extractElement(elementPath);

          // check if correct object in correct activity copy
          if (origActivityPath.equals(resultElementActivityPath)
              && origElement.equals(resultElement))
          {
            // element in activity found
            elementFound(origActivityPath, elementPath, origElement, editpart, figure);
            found = true;
            break;
          }
          else if (origElement instanceof Reference && refElements != null
              && refElements.contains(((Reference) origElement).getReference()))
          {
            // reference found
            elementFound(origActivityPath, elementPath, origElement, editpart, figure);
            found = true;
            break;
          }
        }
        if (!found)
          elementNotFound(origActivityPath, origElement, editpart, figure);
      }
    }
  }


  protected void elementFound(List<EObject> activityPath, List<EObject> elementPath,
      Object origElement, EditPart editpart, Figure figure)
  {
    Color color = null;
    if (origElement instanceof Reference)
      color = org.eclipse.draw2d.ColorConstants.blue;
    else
      color = org.eclipse.draw2d.ColorConstants.green;

    // set the color
    if (editpart instanceof ConnectionEditPart)
      figure.setForegroundColor(color);
    else
    {
      figure.setBackgroundColor(color);

      if (origElement instanceof Reference)
        for (Object cfigure : figure.getChildren())
          for (Object ccfigure : ((Figure) cfigure).getChildren())
          {
            ((Figure) ccfigure).setForegroundColor(color);
            ((Figure) ccfigure).setBackgroundColor(color);
          }
    }
  }


  protected void elementNotFound(List<EObject> activityPath, Object origElement,
      EditPart editpart, Figure figure)
  {
    Color color = null;
    if (origElement instanceof Reference)
      color = org.eclipse.draw2d.ColorConstants.black;
    else
      color = org.eclipse.draw2d.ColorConstants.red;

    // set the color
    if (editpart instanceof ConnectionEditPart)
      figure.setForegroundColor(color);
    else
    {
      figure.setBackgroundColor(color);

      if (origElement instanceof Reference)
        for (Object cfigure : figure.getChildren())
          for (Object ccfigure : ((Figure) cfigure).getChildren())
          {
            ((Figure) ccfigure).setForegroundColor(color);
            ((Figure) ccfigure).setBackgroundColor(color);
          }
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.