/*
 * $Id$
 *
 * Copyright (c) 2004-2005 by the TeXlapse Team.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package net.sourceforge.texlipse.actions;

import net.sourceforge.texlipse.TexlipsePlugin;
import net.sourceforge.texlipse.builder.BuilderRegistry;
import net.sourceforge.texlipse.properties.TexlipseProperties;
import net.sourceforge.texlipse.viewer.TexLaunchConfigurationDelegate;
import net.sourceforge.texlipse.viewer.TexLaunchConfigurationTab;
import net.sourceforge.texlipse.viewer.ViewerAttributeRegistry;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager;
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchHistory;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;


/**
 * Preview action runs the previewer (that is configured in preferences)
 * on the current project.
 * 
 * @author Kimmo Karlsson
 * @author Tor Arne Vestb
 */
public class PreviewAction implements IWorkbenchWindowActionDelegate, IEditorActionDelegate {
    
	/**
     *  Launches either the most recent viewer configuration, or if there
     *  is no previous viewers, creates a new viewer launch configuration.
	 */
	public void run(IAction action) {
        try {      	
        	ILaunchConfiguration config = null;
        	
            
            // Get the output format
            IProject project = TexlipsePlugin.getCurrentProject();
            String outputFormat = TexlipseProperties.getProjectProperty(project,
                TexlipseProperties.OUTPUT_FORMAT);
            
            // Get the preferred viewer for the current output format
            ViewerAttributeRegistry var = new ViewerAttributeRegistry();
            String preferredViewer = var.getPreferredViewer(outputFormat);

            if (null == preferredViewer) {
                BuilderRegistry.printToConsole(TexlipsePlugin.getResourceString("builderErrorOutputFormatNotSet").replaceAll("%s", project.getName()));
                throw new CoreException(TexlipsePlugin.stat("No previewer found for the current output format."));
            }
            
//          Internal, but will probably not change very much *cross fingers* :P
            LaunchConfigurationManager configManager = DebugUIPlugin.getDefault().getLaunchConfigurationManager();
            if (configManager != null) {
                LaunchHistory history = configManager.getLaunchHistory("org.eclipse.ui.externaltools.launchGroup");
                if (history != null) {
                    ILaunchConfiguration[] configs = history.getHistory();
                    if (configs != null) {
                        // Try to find a recent viewer launch first
                        for (int i = 0; i < configs.length ; i++) {
                            ILaunchConfiguration c = configs[i];
                            if (c.getType().getIdentifier().equals(TexLaunchConfigurationDelegate.CONFIGURATION_ID)) {
                                if (c.getAttribute("viewerCurrent", "").equals(preferredViewer)) {
                                    config = c;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            
            // If there was no available viewer
            if (config == null)
            {
                // Create a new one
                ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
                ILaunchConfigurationType type = manager.getLaunchConfigurationType(
                        TexLaunchConfigurationDelegate.CONFIGURATION_ID);               
                ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(null,
                        manager.generateUniqueLaunchConfigurationNameFrom("Preview Document in " + preferredViewer));
                
                // Request another viewer than the topmost in the preferences
                workingCopy.setAttribute("viewerCurrent", preferredViewer);
                
                // For some reason the Eclipse API wants us to init default values
                // for new configurations using the ConfigurationTab dialog.
                TexLaunchConfigurationTab tab = new TexLaunchConfigurationTab();                
                tab.setDefaults(workingCopy);     
                
                config = workingCopy.doSave();
            }
            
            DebugUITools.launch(config, ILaunchManager.RUN_MODE);        
        	
        	
        } catch (CoreException e) {
            TexlipsePlugin.log("Launching viewer", e);
        }
	}

	/**
     * Nothing to do.
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

	/**
     * Nothing to do.
	 */
	public void dispose() {
	}

	/**
	 * Cache the window object in order to be able to provide
     * UI components for the action.
	 */
	public void init(IWorkbenchWindow window) {
	}

    /**
     * 
     */
    public void setActiveEditor(IAction action, IEditorPart targetEditor) {
    }
}

@@ -1074,219 +1074,8 @@
 er;%0A
-import org.eclipse.debug.internal.ui.DebugUIPlugin;%0Aimport org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager;%0Aimport org.eclipse.debug.internal.ui.launchConfigurations.LaunchHistory;%0A
 impo
@@ -2725,18 +2725,16 @@
        %0A
-//
         
@@ -2739,115 +2739,25 @@
     
-Internal, but will probably not change very much *cross fingers* :P%0A            LaunchConfiguration
+  ILaunch
 Manager 
 conf
@@ -2752,23 +2752,17 @@
 Manager 
-configM
+m
 anager =
@@ -2767,18 +2767,16 @@
  = Debug
-UI
 Plugin.g
@@ -2796,29 +2796,16 @@
 etLaunch
-Configuration
 Manager(
@@ -2823,200 +2823,160 @@
     
-if (configManager != null) %7B%0A                LaunchHistory history = configManager.getLaunchHistory(%22org.eclipse.ui.externaltools.launchGroup%22);%0A                if (history != null) %7B%0A
+ILaunchConfigurationType type = manager.getLaunchConfigurationType(%0A                    TexLaunchConfigurationDelegate.CONFIGURATION_ID);       
         
     
@@ -2963,32 +2963,33 @@
 ;               
+%0A
             ILau
@@ -3021,38 +3021,47 @@
 s = 
-history.getHistory();%0A        
+manager.getLaunchConfigurations(type);%0A
     
@@ -3087,32 +3087,24 @@
  != null) %7B%0A
-        
             
@@ -3171,88 +3171,13 @@
     
-      
-  
 for (
-int i = 0; i %3C configs.length ; i++) %7B%0A                            
 ILau
@@ -3199,17 +3199,17 @@
 n c 
-=
+:
  configs
 %5Bi%5D;
@@ -3208,21 +3208,12 @@
 figs
-%5Bi%5D;%0A        
+) %7B%0A
     
@@ -3339,32 +3339,24 @@
             
-        
 if (c.getAtt
@@ -3434,32 +3434,24 @@
             
-     
-   
 config = c;%0A
@@ -3450,24 +3450,16 @@
 ig = c;%0A
-        
         
@@ -3505,39 +3505,26 @@
             
-        %7D%0A     
+%7D%0A
             
@@ -3527,34 +3527,25 @@
             
-   %7D%0A     
+%7D
             
@@ -3552,34 +3552,8 @@
     
-   %7D%0A                    %7D
 %0A   
@@ -3728,267 +3728,8 @@
 one%0A
-                ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();%0A                ILaunchConfigurationType type = manager.getLaunchConfigurationType(%0A                        TexLaunchConfigurationDelegate.CONFIGURATION_ID);               %0A
     

Remove dependencies from internal eclipse classes. Beware that this could introduce new bugs ;).