package cn.com.origin.autocode.handlers;

import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.commands.genericsaveas.ISaveAsService;
import com.teamcenter.rac.common.Activator;
import com.teamcenter.rac.kernel.*;
import com.teamcenter.rac.ui.commands.handlers.SaveASHandler;
import com.teamcenter.rac.util.*;
import com.teamcenter.rac.util.wizard.extension.*;
import java.text.MessageFormat;
import org.apache.log4j.Logger;
import org.eclipse.core.commands.*;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

public class ORItemSaveAsHandler extends AbstractHandler
{
    private class CreateSaveAsDialog
        implements Runnable
    {
    	
        public void run()
        {
            BaseExternalWizard baseexternalwizard = getWizard();
            if(baseexternalwizard != null)
            {
                baseexternalwizard.setForcePreviousAndNextButtons(true);
                ISaveAsService isaveasservice =(ISaveAsService)OSGIUtil.getService(Activator.getDefault(), ISaveAsService.class);
                if(isaveasservice != null)
                {
                    isaveasservice.setInput((TCComponent)m_selectedCmp);
                    baseexternalwizard.setWindowTitle(getWizardTitle());
                    baseexternalwizard.setContext(new StructuredSelection(m_selectedCmp));
                    Shell shell = UIUtilities.getCurrentModalShell();
                    BaseExternalWizardDialog baseexternalwizarddialog = new BaseExternalWizardDialog(shell, baseexternalwizard);
                    baseexternalwizarddialog.create();
                    ORItemSaveAsHandler.readDisplayParameters(baseexternalwizard, baseexternalwizarddialog);
                    Shell shell1 = baseexternalwizarddialog.getShell();
                    UIUtilities.setCurrentModalShell(shell1);
                    baseexternalwizarddialog.open();
                    UIUtilities.setCurrentModalShell(shell);
                    
                } else
                {
                    MessageBox.post(m_shell, Messages.getString("saveAsServiceNotAvailble.MEG"), Messages.getString("saveAs.TITLE"), 4);
                }
            }
        }

        private final Shell m_shell;
        final ORItemSaveAsHandler this$0;

        private CreateSaveAsDialog(Shell shell)
        {
        	super();
            this$0 = ORItemSaveAsHandler.this;
            m_shell = shell;
        }

        CreateSaveAsDialog(Shell shell, CreateSaveAsDialog createsaveasdialog)
        {
            this(shell);
        }
    }


    public ORItemSaveAsHandler()
    {
    }

    public Object execute(ExecutionEvent executionevent)
        throws ExecutionException
    {
        ISelection iselection = HandlerUtil.getCurrentSelection(executionevent);
        if(iselection instanceof StructuredSelection)
        {
            StructuredSelection structuredselection = (StructuredSelection)iselection;
            if(structuredselection.size() != 1)
            {
                MessageBox.post(HandlerUtil.getActiveWorkbenchWindow(executionevent).getShell(), Messages.getString("toomanyObjects.MSG"), Messages.getString("saveAs.TITLE"), 4);
            } else
            {
                m_selectedCmp = (InterfaceAIFComponent)AdapterUtil.getAdapter(iselection, InterfaceAIFComponent.class);
                if(m_selectedCmp.getType().endsWith("HX3_WLRevision")){
                	try {
						m_selectedCmp =((TCComponentItemRevision)m_selectedCmp).getItem();
					} catch (TCException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                }
                if(m_selectedCmp instanceof TCComponent)
                {
                    Object aobj[] = {
                        ((TCComponent)m_selectedCmp).toDisplayString()
                    };
                    m_selectedCmp = getUnderlyingComponent((TCComponent)m_selectedCmp);
                    if(m_selectedCmp != null)
                    {
                        Shell shell = HandlerUtil.getActiveShell(executionevent);
                        if(shell != null)
                        {
                            CreateSaveAsDialog createsaveasdialog = new CreateSaveAsDialog(shell, null);
                            createsaveasdialog.run();
                           
                        }
                    } else
                    {
                        MessageBox.post(HandlerUtil.getActiveWorkbenchWindow(executionevent).getShell(), MessageFormat.format(Messages.getString("saveAsNotSupported.MESSAGE"), aobj), Messages.getString("saveAs.TITLE"), 1);
                    }
                } else
                {
                    MessageBox.post(HandlerUtil.getActiveWorkbenchWindow(executionevent).getShell(), Messages.getString("invalidSelection.MESSAGE"), Messages.getString("saveAs.TITLE"), 1);
                }
            }
        }
        return null;
    }

    protected static void readDisplayParameters(Wizard wizard, WizardDialog wizarddialog)
    {
        String s = "DialogParameters";
        String s1 = wizard.getClass().getName();
        if(Cookie.exists(s, true))
            try
            {
                int i = 0;
                int j = 0;
                int k = 0;
                int l = 0;
                Cookie cookie = Cookie.getCookie(s, true);
                i = cookie.getNumber((new StringBuilder(String.valueOf(s1))).append(".x").toString());
                j = cookie.getNumber((new StringBuilder(String.valueOf(s1))).append(".y").toString());
                k = cookie.getNumber((new StringBuilder(String.valueOf(s1))).append(".w").toString());
                l = cookie.getNumber((new StringBuilder(String.valueOf(s1))).append(".h").toString());
                if(k > 0 && l > 0)
                    wizarddialog.getShell().setBounds(i, j, k, l);
            }
            catch(Exception _ex) { }
    }

    protected BaseExternalWizard getWizard()
    {
        String s = "com.teamcenter.rac.ui.commands.saveas.SaveAsWizard";
        return WizardExtensionHelper.getWizard(s);
    }

    protected String getWizardTitle()
    {
        String s = null;
        if(m_selectedCmp instanceof TCComponent)
        {
            Object aobj[] = {
                ((TCComponent)m_selectedCmp).getTypeComponent().getDisplayType()
            };
            s = MessageFormat.format(Messages.getString("saveAswizard.TITLE"), aobj);
        }
        return s;
    }

    private TCComponent getUnderlyingComponent(TCComponent tccomponent)
    {
        try
        {
            if(tccomponent.isRuntimeType())
                return tccomponent.getUnderlyingComponent().isRuntimeType() ? null : tccomponent.getUnderlyingComponent();
        }
        catch(TCException tcexception)
        {
            Logger.getLogger(SaveASHandler.class).error(tcexception.getLocalizedMessage(), tcexception);
        }
        return tccomponent;
    }

    private InterfaceAIFComponent m_selectedCmp;

}


/*
	DECOMPILATION REPORT

	Decompiled from: C:\TC\Siemens\Teamcenter11\portal\plugins\com.teamcenter.rac.ui.commands_11000.2.0.jar
	Total time: 29 ms
	Jad reported messages/errors:
	Exit status: 0
	Caught exceptions:
*/
