/*******************************************************************************
 * This file is part of the Symfony eclipse plugin.
 * 
 * (c) Robert Gruendler <r.gruendler@gmail.com>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 ******************************************************************************/
package com.dubture.symfony.ui.utils;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.dltk.core.DLTKCore;
import org.eclipse.dltk.core.IScriptFolder;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.PlatformUI;
import org.pdtextensions.core.ui.wizards.NewClassWizard;

import com.dubture.composer.core.model.ModelAccess;
import com.dubture.symfony.core.log.Logger;
import com.dubture.symfony.core.resources.SymfonyMarker;

public class DialogUtils
{

    public static void launchClassWizardFromMarker(IMarker marker)
    {
        Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
        ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
        ISelection selection = selectionService.getSelection();

        NewClassWizard wizard = new NewClassWizard();
        String serviceClass = "";
        String className = "";
        IScriptFolder folder = null;

        try {
            serviceClass = (String) marker.getAttribute(SymfonyMarker.SERVICE_CLASS);
        } catch (CoreException e) {
            Logger.logException(e);
        }
        
        if (serviceClass == null) {
        	Logger.log(Logger.ERROR, "Could not retrieve service class to open class dialog");
        	return;
        }

        ModelAccess composer = ModelAccess.getInstance();
        IResource resource = marker.getResource();
        className  = serviceClass.substring(serviceClass.lastIndexOf("\\"), serviceClass.length());
        String ns = serviceClass.replace(className, "");
		IPath folderPath = composer.reverseResolve(resource.getProject(), ns);
			
        if (folderPath == null) {
        	MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error opening class wizard", "Could not open the New Class wizard. See the workspace log for details");
        	Logger.log(Logger.ERROR, "Unable to retrieve target folder from composer information");
        	return;
        }
        
        IFolder folder2 = resource.getProject().getFolder(folderPath);
    	folder = (IScriptFolder) DLTKCore.create(folder2);
        
        wizard.setClassName(className.replace("\\", ""));
        wizard.setNamespace(ns);
        wizard.setScriptFolder(folder);

        if (selection instanceof IStructuredSelection) {
            wizard.init(PlatformUI.getWorkbench(), (IStructuredSelection) selectionService.getSelection());
        }

        WizardDialog dialog = new WizardDialog(shell, wizard);
        dialog.create();
        dialog.open();
    }
}

@@ -511,24 +511,68 @@
 es.IMarker;%0A
+import org.eclipse.core.resources.IProject;%0A
 import org.e
@@ -690,16 +690,69 @@
 .IPath;%0A
+import org.eclipse.core.runtime.NullProgressMonitor;%0A
 import o
@@ -2966,23 +2966,28 @@
 IFolder 
-f
+targetF
 older
-2
  = resou
@@ -3026,16 +3026,221 @@
 rPath);%0A
+        if (targetFolder.exists() == false) %7B%0A        %09try %7B%0A%09%09%09%09targetFolder.create(true, true, new NullProgressMonitor());%0A%09%09%09%7D catch (CoreException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A        %7D%0A        %0A
     %09fol
@@ -3281,15 +3281,20 @@
 ate(
-f
+targetF
 older
-2
 );%0A 

Create target folder if not existing