/**
 * $Revision$
 * $Date$
 *
 * Copyright (C) 1999-2005 Jive Software. All rights reserved.
 * This software is the proprietary information of Jive Software. Use is subject to license terms.
 */

package org.jivesoftware.sparkimpl.plugin.language;

import org.jivesoftware.resource.Res;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.plugin.Plugin;
import org.jivesoftware.spark.util.URLFileSystem;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;

import java.awt.event.ActionEvent;
import java.io.File;
import java.net.URL;
import java.util.Locale;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JOptionPane;

/**
 * Allows for changing of default languages within Spark.
 *
 * @author Derek DeMoro
 */
public class LanguagePlugin implements Plugin {

    private Locale[] locales;
    private JMenu languageMenu;

    public void initialize() {
        // Register with action menu
        final JMenu actionsMenu = SparkManager.getMainWindow().getMenuByName(Res.getString("menuitem.actions"));

        languageMenu = new JMenu(Res.getString("menuitem.languages"));

        locales = Locale.getAvailableLocales();

        // Load files
        URL url = getClass().getClassLoader().getResource("i18n");

        File[] files = URLFileSystem.url2File(url).listFiles();
        if(files == null){
            return;
        }
        
        for (int i = 0; i < files.length; i++) {
            File propertiesFile = files[i];
            String propertiesName = propertiesFile.getName();
            if (propertiesName.endsWith(".properties")) {
                int lastIndex = propertiesName.lastIndexOf("i18n_");
                int period = propertiesName.lastIndexOf(".");
                if (lastIndex == -1) {
                    addLanguage("en");
                }
                else {
                    String language = propertiesName.substring(lastIndex + 5, period);
                    addLanguage(language);
                }
            }
        }

        actionsMenu.add(languageMenu);
    }

    private void addLanguage(String language) {
        for (int i = 0; i < locales.length; i++) {
            final Locale locale = locales[i];
            if (locale.getLanguage().equals(language)) {
                Action action = new AbstractAction() {
                    public void actionPerformed(ActionEvent e) {
                        final LocalPreferences preferences = SettingsManager.getLocalPreferences();
                        preferences.setLanguage(locale.getLanguage());

                        int ok = JOptionPane.showConfirmDialog(SparkManager.getMainWindow(), Res.getString("message.restart.required"), Res.getString("title.confirmation"), JOptionPane.YES_NO_OPTION);
                        if (ok == JOptionPane.YES_OPTION) {
                            SparkManager.getMainWindow().shutdown();
                        }
                    }
                };

                action.putValue(Action.NAME, locale.getDisplayLanguage(locale));
                languageMenu.add(action);
                break;
            }
        }
    }

    public void shutdown() {
    }

    public boolean canShutDown() {
        return false;
    }


    public void uninstall() {
    }
}

@@ -421,16 +421,60 @@
 System;%0A
+import org.jivesoftware.spark.util.log.Log;%0A
 import o
@@ -614,111 +614,8 @@
 java
-.awt.event.ActionEvent;%0Aimport java.io.File;%0Aimport java.net.URL;%0Aimport java.util.Locale;%0A%0Aimport java
 x.sw
@@ -716,16 +716,219 @@
 ptionPan
+e;%0A%0Aimport java.awt.event.ActionEvent;%0Aimport java.net.URL;%0Aimport java.util.Enumeration;%0Aimport java.util.Locale;%0Aimport java.util.jar.JarEntry;%0Aimport java.util.jar.JarFile;%0Aimport java.util.zip.ZipFil
 e;%0A%0A/**%0A
@@ -1465,19 +1465,24 @@
     URL 
-url
+sparkJar
  = getCl
@@ -1517,20 +1517,25 @@
 source(%22
-i18n
+spark.jar
 %22);%0A%0A   
@@ -1543,23 +1543,56 @@
     
-File%5B%5D f
+try %7B%0A            ZipFile zipF
 ile
-s
  = 
+new JarFile(
 URLF
@@ -1614,82 +1614,95 @@
 ile(
-url).listFiles();%0A        if(files == null)%7B%0A            return;%0A        %7D
+sparkJar));%0A            for (Enumeration e = zipFile.entries(); e.hasMoreElements();) %7B
 %0A   
@@ -1706,25 +1706,24 @@
         
-%0A
         
 for (int
@@ -1718,49 +1718,56 @@
     
-for (int i = 0; i %3C files.length; i++) %7B%0A
+JarEntry entry = (JarEntry)e.nextElement();%0A    
     
@@ -1774,20 +1774,22 @@
         
-File
+String
  propert
@@ -1795,86 +1795,82 @@
 ties
-Fil
+Nam
 e = 
-files%5Bi%5D;%0A            String propertiesName = propertiesFile.getName();
+entry.getName();%0A                // Ignore any manifest.mf entries.
 %0A   
@@ -1874,24 +1874,28 @@
             
+    
 if (properti
@@ -1924,24 +1924,28 @@
 erties%22)) %7B%0A
+    
             
@@ -2013,24 +2013,28 @@
             
+ 
+   
 int period =
@@ -2079,24 +2079,28 @@
             
+    
 if (lastInde
@@ -2106,20 +2106,65 @@
 ex == -1
-) %7B%0A
+ && propertiesName.contains(%22spark_i18n%22)) %7B%0A    
         
@@ -2202,34 +2202,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -2259,24 +2259,28 @@
             
+  
+  
 String langu
@@ -2346,32 +2346,36 @@
                 
+    
 addLanguage(lang
@@ -2389,32 +2389,36 @@
                 
+    
 %7D%0A            %7D%0A
@@ -2407,33 +2407,200 @@
   %7D%0A            
-%7D
+    %7D%0A            %7D%0A            zipFile.close();%0A            zipFile = null;%0A        %7D%0A        catch (Throwable e) %7B%0A            Log.error(%22Error unzipping plugin%22, e);
 %0A        %7D%0A%0A    

Update build.