/*
 * 
 * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.installer;

import com.dmdirc.installer.Installer.ShortcutType;
import com.dmdirc.installer.ui.InstallerDialog;

/**
 * Listeners to all wizard and step changes and responds accordingly.
 */
public class InstallerListener implements WizardListener, StepListener {

    private Main main;

    /**
     * Instantiates a new installer listener.
     *
     * @param main Installer entry point
     */
    public InstallerListener(final Main main) {
        this.main = main;
    }

    /** {@inheritDoc} */
    @Override
    public void wizardFinished() {
        main.disposeOfInstaller();
        Main.getWizardFrame().dispose();
    }

    /** {@inheritDoc} */
    @Override
    public void wizardCancelled() {
        if (!"Install".equals(Main.getWizardFrame().getCurrentStepName()) && Main.getWizardFrame().
                showCancelConfirmation()) {
            Main.getWizardFrame().dispose();
        }
    }

    /** {@inheritDoc} */
    @Override
    public void stepAboutToDisplay(final Step step) {
        if ("Install".equals(step.getStepName())) {
            installerToBeDisplayed(step);
        } else if ("Confirm".equals(step.getStepName())) {
            confirmToBeDisplayed((TextStep) step);
        }
    }

    /** {@inheritDoc} */
    @Override
    public void stepHidden(final Step step) {
        //Ignore
    }

    private void installerToBeDisplayed(final Step step) {
        final InstallerDialog dialog = Main.getWizardFrame();
        dialog.enableNextStep(false);
        dialog.enablePreviousStep(false);
        Main.getInstaller().setInstallStep((TextStep) dialog.getStep("Install"));
        Main.getInstaller().start();
    }

    private void confirmToBeDisplayed(final TextStep step) {
        String shortcutText = "";

        final Settings settings = (Settings) Main.getWizardFrame().getStep(1);

        if (Main.getInstaller().supportsShortcut(ShortcutType.MENU) && settings.
                getShortcutMenuState()) {
            shortcutText = shortcutText + " - Create " + Main.getInstaller().
                    getMenuName() + " shortcut\n";
        }

        if (Main.getInstaller().supportsShortcut(ShortcutType.DESKTOP) &&
            settings.getShortcutDesktopState()) {
            shortcutText = shortcutText + " - Create desktop shortcut\n";
        }

        if (Main.getInstaller().supportsShortcut(ShortcutType.QUICKLAUNCH) &&
            settings.getShortcutQuickState()) {
            shortcutText = shortcutText + " - Create Quick Launch shortcut\n";
        }

        if (Main.getInstaller().supportsShortcut(ShortcutType.PROTOCOL) &&
            settings.getShortcutProtocolState()) {
            shortcutText = shortcutText +
                           " - Make DMDirc handle irc:// links\n";
        }


        final String installLocation = settings.getInstallLocation();


        if (installLocation.isEmpty()) {
            step.setText(
                    "You have chosen an invalid install location\n\n" +
                    "Please press the \"Previous\" button to go back and correct it.");
            Main.getWizardFrame().enableNextStep(false);
        } else {
            step.setText(
                    "Please review your chosen settings:\n\n" +
                    " - Install Location:\n" + "    " + installLocation +
                    "\n" + shortcutText + "\n" +
                    "If you wish to change any of these settings, press the \"Previous\" button," +
                    "otherwise click \"Next\" to begin the installation");
            Main.getWizardFrame().enableNextStep(true);
        }
    }
}

@@ -2376,26 +2376,15 @@
 yed(
-(TextStep) 
 step);%0A
+
     
@@ -2876,20 +2876,16 @@
 d(final 
-Text
 Step ste
@@ -4011,16 +4011,115 @@
 on();%0A%0A%0A
+        if (step instanceof TextStep) %7B%0A            final TextStep textStep = (TextStep) step;%0A    
         
@@ -4155,33 +4155,41 @@
 ) %7B%0A            
-s
+    textS
 tep.setText(%0A   
@@ -4177,32 +4177,36 @@
 xtStep.setText(%0A
+    
                 
@@ -4273,32 +4273,36 @@
                 
+  
+  
 %22Please press th
@@ -4357,32 +4357,35 @@
 %22);%0A            
+   
 Main.getWizardFr
@@ -4417,24 +4417,28 @@
 e);%0A        
+   
+ 
 %7D else %7B%0A   
@@ -4446,17 +4446,25 @@
         
-s
+    textS
 tep.setT
@@ -4460,32 +4460,36 @@
 xtStep.setText(%0A
+    
                 
@@ -4552,24 +4552,28 @@
             
+    
 %22 - Install 
@@ -4634,16 +4634,20 @@
         
+  
+  
 %22%5Cn%22 + s
@@ -4666,36 +4666,16 @@
 + %22%5Cn%22 +
-%0A                   
  %22If you
@@ -4690,16 +4690,45 @@
  change 
+%22 +%0A                        %22
 any of t
@@ -4769,15 +4769,8 @@
 s%5C%22 
-button,
 %22 +%0A
@@ -4789,17 +4789,29 @@
         
+ 
-%22
+   %22button, 
 otherwis
@@ -4860,32 +4860,36 @@
 %22);%0A            
+   
+ 
 Main.getWizardFr
@@ -4912,24 +4912,37 @@
 Step(true);%0A
+           %7D%0A
         %7D%0A  

fix some non issues