/*******************************************************************************
 * Copyright (c) 2010 Bolton University, UK.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the License
 * which accompanies this distribution in the file LICENSE.txt
 *******************************************************************************/
package uk.ac.bolton.archimate.editor.diagram.sketch.editparts;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.requests.LocationRequest;
import org.eclipse.gef.tools.DirectEditManager;

import uk.ac.bolton.archimate.editor.diagram.directedit.LabelCellEditorLocator;
import uk.ac.bolton.archimate.editor.diagram.directedit.LabelDirectEditManager;
import uk.ac.bolton.archimate.editor.diagram.editparts.AbstractConnectedEditPart;
import uk.ac.bolton.archimate.editor.diagram.editparts.IColoredEditPart;
import uk.ac.bolton.archimate.editor.diagram.editparts.ITextEditPart;
import uk.ac.bolton.archimate.editor.diagram.figures.IDiagramModelObjectFigure;
import uk.ac.bolton.archimate.editor.diagram.figures.IEditableLabelFigure;
import uk.ac.bolton.archimate.editor.diagram.policies.PartComponentEditPolicy;
import uk.ac.bolton.archimate.editor.diagram.policies.PartDirectEditTitlePolicy;
import uk.ac.bolton.archimate.editor.diagram.sketch.figures.SketchActorFigure;
import uk.ac.bolton.archimate.editor.diagram.sketch.policies.SketchConnectionPolicy;
import uk.ac.bolton.archimate.editor.ui.ViewManager;
import uk.ac.bolton.archimate.model.ISketchModelActor;


/**
 * Sketch Actor Edit Part
 * 
 * @author Phillip Beauvoir
 */
public class SketchActorEditPart extends AbstractConnectedEditPart
implements IColoredEditPart, ITextEditPart  {
    
    private DirectEditManager fDirectEditManager;
    private ConnectionAnchor fAnchor;

    @Override
    protected IFigure createFigure() {
        SketchActorFigure figure = new SketchActorFigure((ISketchModelActor)getModel());
        return figure;
    }

    @Override
    protected void createEditPolicies() {
        // Allow parts to be connected together
        installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new SketchConnectionPolicy());
        
        // Add a policy to handle directly editing the Part
        installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new PartDirectEditTitlePolicy());

        // Add a policy to handle editing the Parts (for example, deleting a part)
        installEditPolicy(EditPolicy.COMPONENT_ROLE, new PartComponentEditPolicy());
    }

    @Override
    protected void refreshFigure() {
        // Refresh the figure if necessary
        ((IDiagramModelObjectFigure)getFigure()).refreshVisuals();
    }

    @Override
    public void performRequest(Request request) {
        if(request.getType() == RequestConstants.REQ_DIRECT_EDIT) {
            // Edit the label if we clicked on it
            if(((IEditableLabelFigure)getFigure()).didClickLabel(((LocationRequest)request).getLocation().getCopy())) {
                if(fDirectEditManager == null) {
                    Label label = ((IEditableLabelFigure)getFigure()).getLabel();
                    fDirectEditManager = new LabelDirectEditManager(this, new LabelCellEditorLocator(label), label);
                }
                fDirectEditManager.show();
            }
        }
        else if(request.getType() == RequestConstants.REQ_OPEN) {
            ViewManager.showViewPart(ViewManager.PROPERTIES_VIEW, true);
        }
    }
    
    @Override
    protected ConnectionAnchor getConnectionAnchor() {
        if(fAnchor == null) {
            fAnchor = new ChopboxAnchor(getFigure());
        }
        return fAnchor;
    }

}

@@ -585,42 +585,8 @@
 e;%0D%0A
-import org.eclipse.draw2d.Label;%0D%0A
 impo
@@ -3087,16 +3087,66 @@
 ECT_EDIT
+ %7C%7C request.getType() == RequestConstants.REQ_OPEN
 ) %7B%0D%0A   
@@ -3209,12 +3209,38 @@
     
-if((
+IEditableLabelFigure figure = 
 (IEd
@@ -3268,17 +3268,40 @@
 Figure()
-)
+;%0D%0A            if(figure
 .didClic
@@ -3417,91 +3417,8 @@
  %7B%0D%0A
-                    Label label = ((IEditableLabelFigure)getFigure()).getLabel();%0D%0A
     
@@ -3518,21 +3518,74 @@
 tor(
-label), l
+figure.getLabel()),%0D%0A                            figure.getL
 abel
+()
 );%0D%0A
@@ -3662,31 +3662,24 @@
      %7D%0D%0A    
-    %7D%0D%0A
         else
@@ -3745,16 +3745,20 @@
         
+   
+ 
 ViewMana
@@ -3807,24 +3807,39 @@
 EW, true);%0D%0A
+            %7D%0D%0A
         %7D%0D%0A 

Allow Sketch Actor EditPart label editable on double-click