ProcessElementLabelProvider.java :  » Library » bonita » org » bonitasoft » studio » diagram » custom » Java Open Source

Java Open Source » Library » bonita 
bonita » org » bonitasoft » studio » diagram » custom » ProcessElementLabelProvider.java
package org.bonitasoft.studio.diagram.custom;
/**
 * Copyright (C) 2009 BonitaSoft S.A.
 * BonitaSoft, 31 rue Gustave Eiffel - 38000 Grenoble
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2.0 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


import org.bonitasoft.studio.model.process.Element;
import org.bonitasoft.studio.model.process.diagram.part.ProcessVisualIDRegistry;
import org.bonitasoft.studio.model.process.diagram.providers.ProcessElementTypes;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

/**
 * @author Mickael Istria
 *
 */
public class ProcessElementLabelProvider extends LabelProvider {

  public String getText(Object element) {
    element = unwrap(element);
    if(((IGraphicalEditPart)element).resolveSemanticElement() instanceof Element){
      Element item = (Element) ((IGraphicalEditPart)element).resolveSemanticElement();
      return (item.getLabel() != null ? item.getLabel() : "");
    }else{
      return "";
    }
  }

  public Image getImage(Object element) {
    IElementType etype = getElementType(getView(unwrap(element)));
    return etype == null ? null : ProcessElementTypes.getImage(etype);
  }

  private Object unwrap(Object element) {
    if (element instanceof IStructuredSelection) {
      return ((IStructuredSelection) element).getFirstElement();
    }
    return element;
  }

  private View getView(Object element) {
    if (element instanceof View) {
      return (View) element;
    }
    if (element instanceof IAdaptable) {
      return (View) ((IAdaptable) element).getAdapter(View.class);
    }
    return null;
  }

  private IElementType getElementType(View view) {
    // For intermediate views climb up the containment hierarchy to find the one associated with an element type.
    while (view != null) {
      int vid = ProcessVisualIDRegistry.getVisualID(view);
      IElementType etype = ProcessElementTypes.getElementType(vid);
      if (etype != null) {
        return etype;
      }
      view = view.eContainer() instanceof View ? (View) view.eContainer() : null;
    }
    return null;
  }
}
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.