/*-------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
-------------------------------------------------------------------------------*/

package com.togethersoft.modules.patterns.UNIVERSAL.CLASS.Coad_Classes.Role;

import java.util.Vector;

import com.togethersoft.openapi.sci.*;
import com.togethersoft.openapi.sci.pattern.SciPattern;

import com.togethersoft.openapi.ide.message.IdeMessageManagerAccess;
import com.togethersoft.openapi.ide.message.IdeMessageType;

import com.togethersoft.modules.patterns.UNIVERSAL.CLASS.ClassPatternBase;
import com.togethersoft.modules.patterns.UNIVERSAL.PatternBaseException;
import com.togethersoft.modules.patterns.UNIVERSAL.PatternUIBuilder;

/**
 * Role from ArchetypesInColor
 * @author TogetherSoft
 */
public class Pattern extends ClassPatternBase implements SciPattern {

    private static final String PATTERN_NAME = "Role";

    public Pattern() {
        setPatternDisplayName(PATTERN_NAME);
    }

    protected void finalize() throws Throwable {
        super.finalize();
        if (myUIBuilder != null) {
            myUIBuilder.removeAllListenedInspectorProperties();
            myUIBuilder = null;
        }
        myRoleClass = null;
    }

    public boolean prepare() {
        try {
            super.prepare();
            notImplementedForIDL();

            cannotApplyToInterface(getSelectedClass());

            SciElement testElement = getElement();
            if (testElement instanceof SciClass) {
                setPropertyValue(NAME, testElement.getName());
                myRoleClass = (SciClass)testElement;
            }
            else {
                setPropertyValue(NAME, ROLE);
                myRoleClass = null;
            }

            initializeInspector();
            assignActivityResponse();
            return true;
        }
        catch(PatternBaseException e) {
            return false;
        }

    }

    public boolean canApply() {
        try {
            String name = classNameShouldBeCorrect(NAME);
            if (myRoleClass != null && !myRoleClass.canSetName(name)) {
                throw new PatternBaseException("Cannot rename class to " + name);
            }
            return true;
        }
        catch(PatternBaseException e) {
            return false;
        }
    }

    public void apply() {
        if (!canApply()) {
            return;
        }
        super.apply();

        myUIBuilder.removeAllListenedInspectorProperties();

/* Get or create Name. */

        String name = getStringPropertyValue(NAME);
        if (myRoleClass != null) {
            myRoleClass.setName(name);
            if (!myRoleClass.getName().equals(name)) {
                IdeMessageManagerAccess.printMessage(IdeMessageType.ERROR, "Couldn't rename class.");
            }
        }
        else {
            myRoleClass = getClassByName(name, false);
        }

  /* Create Role Class here. */

        myRoleClass.getTagList().setTagValue(STEREOTYPE, STYPE_ROLE);

        pasteNewAttribute(myRoleClass, "status");

        pasteNewOperation(myRoleClass, "assessAcrossMIs");
        pasteNewOperation(myRoleClass, "listMI");
        pasteNewOperation(myRoleClass, "listRoles", INT).setProperty(SciProperty.STATIC, true);
        pasteNewOperation(myRoleClass, "assessAcrossRoles", INT).setProperty(SciProperty.STATIC, true);
    }

    protected void initializeInspector() {
        try {
            myUIBuilder = null;
            myUIBuilder = createUIBuilder(true);

            String pageName1 = "Pattern properties";
            if (myUIBuilder.addInspectorPage(pageName1, PatternUIBuilder.UIPresentation.Table) == null) {
                throw new PatternBaseException(CANNOT_CREATE_PATTERN_UI);
            }
            // Add name property
            if (myUIBuilder.addPropertyToPage(pageName1, NAME, NAME,
                PatternUIBuilder.PropertyType.String) == null) {
                throw new PatternBaseException(CANNOT_CREATE_PATTERN_UI);
            }

            myUIBuilder.setCreatedPatternUI();

            Vector inspectorPropertiesVector = new Vector();
            inspectorPropertiesVector.addElement(NAME);
            myUIBuilder.assignListenedInspectorProperties(inspectorPropertiesVector);
        }
        catch (PatternBaseException ex) {
            myUIBuilder = null;
            getIdeInspector().clear();
            return;
        }
    }

    protected void assignActivityResponse() {
    }

    private PatternUIBuilder    myUIBuilder = null;
    private SciClass            myRoleClass = null;

    private static final String NAME = "Name";
    private static final String ROLE = "Role";
}
