Index: Launcher.java
===================================================================
--- Launcher.java	(revision 38563)
+++ Launcher.java	(revision 38564)
@@ -76,6 +76,7 @@
 import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Comparator;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -108,22 +109,19 @@
 import org.apache.log4j.spi.LoggingEvent;
 import org.exolab.castor.xml.MarshalException;
 import org.exolab.castor.xml.ValidationException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
+import org.gvsig.andami.actioninfo.ActionInfo;
+import org.gvsig.andami.actioninfo.ActionInfoManager;
 import org.gvsig.andami.authentication.IAuthentication;
-import org.gvsig.andami.authentication.LoginUI;
 import org.gvsig.andami.config.generate.Andami;
 import org.gvsig.andami.config.generate.AndamiConfig;
 import org.gvsig.andami.config.generate.Plugin;
-import org.gvsig.andami.iconthemes.IIconTheme;
-import org.gvsig.andami.iconthemes.IconThemeManager;
 import org.gvsig.andami.messages.Messages;
 import org.gvsig.andami.messages.NotificationManager;
 import org.gvsig.andami.plugins.ExclusiveUIExtension;
 import org.gvsig.andami.plugins.ExtensionDecorator;
 import org.gvsig.andami.plugins.IExtension;
 import org.gvsig.andami.plugins.PluginClassLoader;
+import org.gvsig.andami.plugins.config.generate.Action;
 import org.gvsig.andami.plugins.config.generate.ActionTool;
 import org.gvsig.andami.plugins.config.generate.ComboButton;
 import org.gvsig.andami.plugins.config.generate.ComboButtonElement;
@@ -147,7 +145,6 @@
 import org.gvsig.andami.ui.ToolsWindowManager;
 import org.gvsig.andami.ui.fonts.FontUtils;
 import org.gvsig.andami.ui.mdiFrame.MDIFrame;
-import org.gvsig.andami.ui.mdiFrame.NewStatusBar;
 import org.gvsig.andami.ui.mdiManager.MDIManagerFactory;
 import org.gvsig.andami.ui.splash.MultiSplashWindow;
 import org.gvsig.andami.ui.theme.Theme;
@@ -162,10 +159,16 @@
 import org.gvsig.tools.exception.ListBaseException;
 import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
 import org.gvsig.tools.swing.api.ToolsSwingLocator;
+import org.gvsig.tools.swing.icontheme.IconTheme;
+import org.gvsig.tools.swing.icontheme.IconThemeManager;
+import org.gvsig.tools.util.FolderSet;
+import org.gvsig.tools.util.FolderSet.FolderEntry;
 import org.gvsig.utils.DateTime;
 import org.gvsig.utils.XMLEntity;
 import org.gvsig.utils.xml.XMLEncodingUtils;
 import org.gvsig.utils.xmlEntity.generate.XmlTag;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * <p>
@@ -226,8 +229,17 @@
 		}
 	}
 
+	private static Launcher launcherInstance;
+
+	public static Launcher getInstance() {
+		if( launcherInstance == null ) {
+			launcherInstance = new Launcher();
+		}
+		return launcherInstance;
+	}
+	
 	public static void main(String[] args) throws Exception {
-		Launcher launcher = new Launcher();
+		Launcher launcher = getInstance();
 		boolean install = false;
 		for (int i = 0; i < args.length; i++) {
 			if (args[i].equalsIgnoreCase("--install")) {
@@ -301,6 +313,8 @@
 			public void refresh() {
 			}
 		});
+		
+		new PluginsLibrary().initialize();
 
 		try {
 			initIconThemes();
@@ -335,13 +349,13 @@
 		configureProxy();
 
 		// 2. TODO Buscar actualizaciones de los plugins
-		splashWindow.process(20, PluginServices.getText(Launcher.class,
-				"SplashWindow.looking_for_updates"));
-		try {
-			this.downloadExtensions(andamiConfig.getPluginsDirectory());
-		} catch (Exception ex) {
-			this.addError("Can't downloads plugins", ex);
-		}
+//		splashWindow.process(20, PluginServices.getText(Launcher.class,
+//				"SplashWindow.looking_for_updates"));
+//		try {
+//			this.downloadExtensions(andamiConfig.getPluginsDirectory());
+//		} catch (Exception ex) {
+//			this.addError("Can't downloads plugins", ex);
+//		}
 
 		// 2.5. Initialize andami libraries
 		splashWindow.process(25, PluginServices.getText(Launcher.class,
@@ -496,7 +510,12 @@
 				"creating_main_window"));
 		frame.setVisible(true);
 
-		// 20. Se ejecuta el postInitialize
+		/* 
+		 * Initialize installer local repository folders 
+		 */
+		initializeLocalAddOnRepositoryFolders();
+
+		// 19. Se ejecuta el postInitialize
 		splashWindow.process(190, PluginServices.getText(Launcher.class,
 				"SplashWindow.post_initializing_extensions"));
 		SwingUtilities.invokeAndWait(new Runnable() {
@@ -509,10 +528,8 @@
 
 		// Definimos un KeyEventDispatcher global para que las extensiones
 		// puedan registrar sus "teclas rápidas".
-		GlobalKeyEventDispatcher keyDispatcher = GlobalKeyEventDispatcher
-				.getInstance();
-		KeyboardFocusManager.getCurrentKeyboardFocusManager()
-				.addKeyEventDispatcher(keyDispatcher);
+		GlobalKeyEventDispatcher keyDispatcher = GlobalKeyEventDispatcher.getInstance();
+		KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(keyDispatcher);
 
 		SwingUtilities.invokeAndWait(new Runnable() {
 
@@ -520,6 +537,9 @@
 				frame.enableControls();
 			}
 		});
+
+		createDefaultIconTheme();
+
 		splashWindow.close();
 		if (launcherrors != null) {
 			NotificationManager.addError(launcherrors);
@@ -530,6 +550,39 @@
 
 	}
 
+	private void initializeLocalAddOnRepositoryFolders() {
+		InstallerManager installerManager = InstallerLocator.getInstallerManager();
+
+		File defaultAddonsRepository = PluginsLocator.getManager().getPluginsFolder();
+
+		installerManager.addLocalAddonRepository(defaultAddonsRepository);
+		installerManager.setDefaultLocalAddonRepository(defaultAddonsRepository);
+		
+		IconThemeManager iconManager = ToolsSwingLocator.getIconThemeManager();
+		FolderSet fset = iconManager.getRepository();
+		Iterator<FolderSet.FolderEntry> it = fset.iterator();
+		while( it.hasNext() ) {
+			FolderEntry entry = it.next();
+			installerManager.addLocalAddonRepository(entry.getFolder());
+		}
+	}
+	
+	private void createDefaultIconTheme() {
+		PluginsManager pluginsManager = PluginsLocator.getManager();
+		IconThemeManager iconManager = ToolsSwingLocator.getIconThemeManager();
+		
+		File f = new File(pluginsManager.getApplicationHomeFolder(),"icon-theme");
+		if( !f.exists() ) {
+			f.mkdir();
+		}
+		IconTheme theme = iconManager.getDefault();
+		File f2 = new File(f,theme.getID()) ;
+		if( !f2.exists() ) {
+			logger.info("Creating default icon theme in "+f.getAbsolutePath());
+			theme.export(f);
+		}
+	}
+	
 	/**
      * 
      */
@@ -697,34 +750,34 @@
 	 * Return the directory applicaction is installed.
 	 */
 	public static String getApplicationDirectory() {
-		return new File("").getAbsolutePath();
+		return getApplicationFolder().getAbsolutePath();
 	}
+	
+    public static File getApplicationFolder() {
+        // TODO: check if there is a better way to handle this
+        return new File(System.getProperty("user.dir"));
+    }
 
 	private void registerIcons() {
-		PluginServices.getIconTheme().registerDefault(
-				"login-gvsig",
-				LoginUI.class.getClassLoader().getResource(
-						"images/login_gvsig.png"));
-		PluginServices.getIconTheme().registerDefault(
-				"splash-gvsig",
-				MultiSplashWindow.class.getClassLoader().getResource(
-						"images/splash.png"));
-		PluginServices.getIconTheme().registerDefault(
-				"info-icon",
-				NewStatusBar.class.getClassLoader().getResource(
-						"images/info.gif"));
-		PluginServices.getIconTheme().registerDefault(
-				"error-icon",
-				NewStatusBar.class.getClassLoader().getResource(
-						"images/error.gif"));
-		PluginServices.getIconTheme().registerDefault(
-				"warning-icon",
-				NewStatusBar.class.getClassLoader().getResource(
-						"images/warning.gif"));
-		PluginServices.getIconTheme().registerDefault(
-				"no-icon",
-				NewStatusBar.class.getClassLoader().getResource(
-						"images/no_icon.png"));
+		IconTheme theme = PluginServices.getIconTheme();
+		ClassLoader loader = Launcher.class.getClassLoader();	
+		
+		String[][] icons = {
+				// MultiSplashWindow
+				{ "main", "splash-default" },
+				// NewStatusBar
+				{ "main", "statusbar-info" },
+				{ "main", "statusbar-warning" },
+				{ "main", "statusbar-error" }
+		};
+		for( int i=0; i<icons.length; i++) {
+			try {
+				IconThemeHelper.registerIcon(icons[i][0], icons[i][1], Launcher.class);
+			} catch(Exception e) {
+				logger.info("Can't register icon '"+icons[i][0]+"' ("+icons[i][1]+").");
+			}
+		}		
+		theme.setDefaultIcon(loader.getResource("images/main/default-icon.png" ));
 	}
 
 	/**
@@ -1242,7 +1295,175 @@
 		}
 	}
 
-	private void installPluginsMenus() {
+	private void registerActionOfExtensions(ActionInfoManager actionManager,
+			Enumeration<SkinExtensionType> extensiones, ClassLoader loader) {
+		ActionInfo actionInfo;
+		while (extensiones.hasMoreElements()) {
+			SkinExtensionType extension = extensiones.nextElement();
+			Class<? extends IExtension> classExtension;
+			try {
+				classExtension = (Class<? extends IExtension>) loader
+						.loadClass(extension.getClassName());
+
+				Enumeration<Action> actions = extension.enumerateAction();
+				while (actions.hasMoreElements()) {
+					Action action = actions.nextElement();
+					if (action.getName() == null) {
+						logger.info("invalid action name (null) in "+ extension.getClassName()+" of "+ loader.toString());
+					} else {
+						actionInfo = actionManager.createAction(
+								classExtension, action.getName(),
+								action.getLabel(), action.getActionCommand(),
+								action.getIcon(), null, action.getPosition(),
+								action.getTooltip());
+						actionManager.registerAction(actionInfo);
+						if( action.getPosition() < 100000000 ) {
+							logger.info("Invalid position in action ("+ actionInfo.toString()+ ").");
+							action.setPosition( action.getPosition() + 1000000000);
+						}
+					}
+				}
+
+				Enumeration<Menu> menus = extension.enumerateMenu();
+				while (menus.hasMoreElements()) {
+					Menu menu = menus.nextElement();
+					if (!menu.getIs_separator() ) {
+						actionInfo = actionManager.createAction(
+							classExtension, menu.getName(), menu.getText(),
+							menu.getActionCommand(), menu.getIcon(),
+							menu.getKey(), menu.getPosition(),
+							menu.getTooltip());
+						actionInfo = actionManager.registerAction(actionInfo);
+						if (actionInfo != null) {
+							menu.setActionCommand(actionInfo.getCommand());
+							menu.setTooltip(actionInfo.getTooltip());
+							menu.setIcon(actionInfo.getIconName());
+							menu.setPosition(actionInfo.getPosition());
+							menu.setKey(actionInfo.getAccelerator());
+							menu.setName(actionInfo.getName());
+						}
+					} 
+					if( menu.getPosition() < 100000000 ) {
+						logger.info("Invalid position in menu ("+ menu.getText() + ").");
+						menu.setPosition( menu.getPosition() + 1000000000);
+					}
+
+				}
+				Enumeration<ToolBar> toolBars = extension.enumerateToolBar();
+				while (toolBars.hasMoreElements()) {
+					ToolBar toolBar = toolBars.nextElement();
+
+					Enumeration<ActionTool> actionTools = toolBar
+							.enumerateActionTool();
+					while (actionTools.hasMoreElements()) {
+						ActionTool actionTool = actionTools.nextElement();
+						actionInfo = actionManager.createAction(
+								classExtension, actionTool.getName(),
+								actionTool.getText(),
+								actionTool.getActionCommand(),
+								actionTool.getIcon(),
+								null,
+								actionTool.getPosition(),
+								actionTool.getTooltip());
+						actionInfo = actionManager.registerAction(actionInfo);
+						if (actionInfo != null) {
+							actionTool.setActionCommand(actionInfo.getCommand());
+							actionTool.setTooltip(actionInfo.getTooltip());
+							actionTool.setIcon(actionInfo.getIconName());
+							actionTool.setPosition(actionInfo.getPosition());
+							actionTool.setName(actionInfo.getName());
+						}
+					}
+
+					Enumeration<SelectableTool> selectableTool = toolBar
+							.enumerateSelectableTool();
+					while (selectableTool.hasMoreElements()) {
+						SelectableTool actionTool = selectableTool
+								.nextElement();
+						actionInfo = actionManager.createAction(
+								classExtension, actionTool.getName(),
+								actionTool.getText(),
+								actionTool.getActionCommand(),
+								actionTool.getIcon(),
+								actionTool.getEnableText(),
+								actionTool.getPosition(),
+								actionTool.getTooltip());
+						actionInfo = actionManager.registerAction(actionInfo);
+						if (actionInfo != null) {
+							actionTool.setActionCommand(actionInfo.getCommand());
+							actionTool.setTooltip(actionInfo.getTooltip());
+							actionTool.setIcon(actionInfo.getIconName());
+							actionTool.setPosition(actionInfo.getPosition());
+							actionTool.setName(actionInfo.getName());
+						}
+					}
+				}
+			} catch (ClassNotFoundException e) {
+				logger.warn(
+						"Can't register actions of extension '"
+								+ extension.getClassName() + "'", e);
+			}
+		}
+	}
+	
+	@SuppressWarnings("unchecked")
+	private void registerActions() {
+		logger.info("registerActions");
+
+		ActionInfoManager actionManager = PluginsLocator.getActionInfoManager();
+		Iterator<String> it = pluginsConfig.keySet().iterator();
+
+		while (it.hasNext()) {
+			String pluginName = it.next();
+			PluginConfig pluginConfig = pluginsConfig.get(pluginName);
+			PluginServices pluginService = pluginsServices.get(pluginName);
+			PluginClassLoader loader =  pluginService.getClassLoader();
+
+			logger.info("registerActions of plugin '"+pluginName+"'.");
+
+			Extensions extensionConfig = pluginConfig.getExtensions();
+			
+			
+			Enumeration<SkinExtensionType> extensiones = extensionConfig.enumerateExtension();
+			registerActionOfExtensions(actionManager, extensiones, loader);
+
+			Enumeration<SkinExtensionType> skinSxtensiones = extensionConfig.enumerateSkinExtension();
+			registerActionOfExtensions(actionManager, skinSxtensiones, loader);
+
+			PopupMenus pluginPopupMenus = pluginConfig.getPopupMenus();
+			if (pluginPopupMenus != null) {
+				PopupMenu[] menus1 = pluginPopupMenus.getPopupMenu();
+				for (int j = 0; j < menus1.length; j++) {
+					PopupMenu popupMenu = menus1[j];
+					Enumeration<Menu> menus2 = popupMenu.enumerateMenu();
+					while (menus2.hasMoreElements()) {
+						Menu menu = menus2.nextElement();
+						if (!menu.getIs_separator() ) {
+							if( menu.getName() == null) {   
+								logger.info("Null name for popmenu '"+menu.getText()+"' in plugin "+ pluginService.getPluginName() );
+							} else {
+								ActionInfo actionInfo = actionManager.getAction(menu.getName());
+								if( actionInfo!=null ) {
+									menu.setActionCommand(actionInfo.getCommand());
+									menu.setTooltip(actionInfo.getTooltip());
+									menu.setIcon(actionInfo.getIconName());
+									menu.setPosition(actionInfo.getPosition());
+									menu.setText( actionInfo.getLabel());
+									menu.setKey(actionInfo.getAccelerator());
+								}
+							}
+						}
+					}
+				}
+			}
+			
+
+		}
+	}
+	
+
+	private TreeSet<SortableMenu> getOrderedMenus() { 
+
 		TreeSet<SortableMenu> orderedMenus = new TreeSet<SortableMenu>(
 				new MenuComparator());
 
@@ -1311,6 +1532,14 @@
 
 		}
 
+		return orderedMenus;
+	}
+
+	private void installPluginsMenus() {
+		logger.info("installPluginsMenus");
+
+		TreeSet<SortableMenu> orderedMenus = getOrderedMenus();
+
 		// Se itera por los menus ordenados
 		Iterator<SortableMenu> e = orderedMenus.iterator();
 
@@ -1319,6 +1548,8 @@
 			try {
 				SortableMenu sm = e.next();
 
+				logger.info(sm.menu.getPosition()+":"+sm.menu.getText()+":"+sm.loader.getPluginName()+":"+sm.extension.getClassName());
+				
 				frame.addMenu(sm.loader, sm.extension, sm.menu);
 
 			} catch (ClassNotFoundException ex) {
@@ -1343,11 +1574,84 @@
 		}
 	}
 
+	public class PluginMenuItem {
+		private Menu menu;
+		private PluginClassLoader loader;
+		private SkinExtensionType extension;
+
+		PluginMenuItem(PluginClassLoader loader,
+				SkinExtensionType extension, Menu menu) {
+			this.menu = menu;
+			this.loader = loader;
+			this.extension = extension;
+		}
+		
+		public PluginServices getPlugin() {
+			String pluginName = loader.getPluginName();
+			return PluginServices.getPluginServices(pluginName);
+		}
+		
+		public String getExtensionName() {
+			return this.extension.getClassName();
+		}
+		
+		public IExtension getExtension() {
+			Class<?> extensionClass;
+			try {
+				extensionClass = loader.loadClass(this.extension.getClassName());
+			} catch (ClassNotFoundException e) {
+				return null;
+			}
+			return PluginServices.getExtension(extensionClass);
+		}
+		
+		public String getText() {
+			return this.menu.getText();
+		}
+
+		public long getPosition() {
+			return this.menu.getPosition();
+		}
+		
+		public String getName() {
+			return this.menu.getName();
+		}
+		
+		public boolean isParent() {
+			return menu.getIs_separator();
+		}
+		
+		public String getPluginName() {
+			return this.loader.getPluginName();
+		}
+		
+		public ActionInfo getAction() {
+			ActionInfoManager manager = PluginsLocator.getActionInfoManager();
+			return manager.getAction(this.menu.getName());
+		}
+	}
+	
+	public List<PluginMenuItem> getPluginMenuItems() {
+		List<PluginMenuItem> menuItems = new ArrayList<Launcher.PluginMenuItem>();
+
+		TreeSet<SortableMenu> orderedMenus = getOrderedMenus();
+		Iterator<SortableMenu> e = orderedMenus.iterator();
+		while (e.hasNext()) {
+				SortableMenu sm = e.next();
+				PluginMenuItem item = new PluginMenuItem(sm.loader, sm.extension, sm.menu);
+				menuItems.add(item);
+		}
+		return menuItems;
+	}
+
+	
 	/**
 	 * Installs the menus, toolbars, actiontools, selectable toolbars and
 	 * combos. The order in which they are shown is determined here.
 	 */
 	private void installPluginsControls() {
+		logger.info("installPluginsControls (toolbars)");
+
 		Iterator<String> i = pluginsConfig.keySet().iterator();
 
 		Map<Extension, PluginServices> extensionPluginServices = new HashMap<Extension, PluginServices>();
@@ -1572,12 +1876,16 @@
 				}
 				// Install popup menus
 				PopupMenus pus = pc.getPopupMenus();
-
 				if (pus != null) {
 					PopupMenu[] menus = pus.getPopupMenu();
-
 					for (int j = 0; j < menus.length; j++) {
-						frame.addPopupMenu(ps.getClassLoader(), menus[j]);
+						String menuName = "(unknow)";
+						try  {
+							menuName = menus[j].getName();
+							frame.addPopupMenu(ps.getClassLoader(), menus[j]);
+						} catch(Throwable ex) {
+							addError("Error adding popup menu' "+ menuName +"' in plugin '"+pName+"'.");
+						}
 					}
 				}
 			} catch (Throwable e3) {
@@ -1767,6 +2075,7 @@
 				i = pluginsConfig.keySet().iterator();
 			}
 		}
+		registerActions();
 	}
 
 	private void pluginsMessages() {
@@ -1829,13 +2138,13 @@
 
 		for (int i = 0; i < pluginDirs.length; i++) {
 			if (pluginDirs[i].isDirectory()) {
+				String pluginName =  pluginDirs[i].getName();
 				File configXml = new File(pluginDirs[i].getAbsolutePath(),
 						"config.xml");
 
 				try {
 					FileInputStream is = new FileInputStream(configXml);
-					Reader xml = org.gvsig.utils.xml.XMLEncodingUtils
-							.getReader(is);
+					Reader xml = org.gvsig.utils.xml.XMLEncodingUtils.getReader(is);
 					if (xml == null) {
 						// the encoding was not correctly detected, use system
 						// default
@@ -1844,26 +2153,23 @@
 						// use a buffered reader to improve performance
 						xml = new BufferedReader(xml);
 					}
-					PluginConfig pConfig = (PluginConfig) PluginConfig
-							.unmarshal(xml);
+					PluginConfig pConfig = (PluginConfig) PluginConfig.unmarshal(xml);
 					pluginsConfig.put(pluginDirs[i].getName(), pConfig);
 				} catch (FileNotFoundException e) {
-					logger
-							.info("Plugin folder without config.xml. Skip plugin '"
-									+ pluginDirs[i].getAbsolutePath() + "'.");
+					logger.info("Plugin '"+pluginName+"' without config.xml ("
+									+ pluginDirs[i].getAbsolutePath() + ").");
 				} catch (MarshalException e) {
-					this.addError("Can't load plugin '"
-							+ pluginDirs[i].getAbsolutePath() + "'.", e);
+					this.addError("Can't load plugin '"+pluginName+"', incorrect config.xml." + e.getMessage() +" ("
+							+ pluginDirs[i].getAbsolutePath() + ").", e);
 				} catch (ValidationException e) {
-					this.addError("Can't load plugin '"
-							+ pluginDirs[i].getAbsolutePath() + "'.", e);
+					this.addError("Can't load plugin '"+pluginName+"', invalid config.xml." + e.getMessage() +" ("
+							+ pluginDirs[i].getAbsolutePath() + ").", e);
 				}
 			}
 		}
 
 		if (pluginsConfig.size() == 0) {
-			logger
-					.error("No valid plugin was found. The plugins directory currently is: "
+			logger.error("No valid plugin was found. The plugins directory currently is: "
 							+ pDir.getAbsolutePath()
 							+ "\n\tDid you specify the correct directory in the Launch Configuration parameters?\n\tExiting now...");
 			System.exit(-1);
@@ -2201,13 +2507,17 @@
 			if (e2.menu.hasPosition() && !e1.menu.hasPosition()) {
 				return Integer.MAX_VALUE;
 			}
-			if (e1.menu.getPosition() != e2.menu.getPosition()) {
+			
+			if( e1.menu.getPosition() == e2.menu.getPosition() ) {
 				// we don't return 0 unless both objects are the same, otherwise
 				// the objects get overwritten in the treemap
-				return e1.menu.getPosition() - e2.menu.getPosition();
-			} else {
 				return (e1.toString().compareTo(e2.toString()));
 			}
+    		if( e1.menu.getPosition() > e2.menu.getPosition() ) {
+				return Integer.MAX_VALUE;
+			}
+    		return Integer.MIN_VALUE;
+    		
 		}
 	}
 
@@ -2223,6 +2533,7 @@
 			menu = menu2;
 			this.loader = loader;
 		}
+		
 	}
 
 	private static class SortableTool {
@@ -2321,12 +2632,12 @@
 
 		public int compare(SortableTool e1, SortableTool e2) {
 			// compare the toolbars which contain the tools
-			int result = toolBarComp.compare(e1, e2);
+			long result = toolBarComp.compare(e1, e2);
 			if (result != 0) { // if the toolbars are different, use their order
-				return result;
+				return result>0? 1:-1;
 			}
 			// otherwise, compare the tools
-			int e1Position = -1, e2Position = -1;
+			long e1Position = -1, e2Position = -1;
 
 			if (e1.actiontool != null) {
 				if (e1.actiontool.hasPosition()) {
@@ -2359,7 +2670,7 @@
 				// we don't return 0 unless both objects are the same, otherwise
 				// the objects get overwritten in the treemap
 				if (result != 0) {
-					return result;
+					return  result>0? 1:-1;
 				}
 			}
 			return e1.toString().compareTo(e2.toString());
@@ -2552,6 +2863,10 @@
 	public static String getAppHomeDir() {
 		return appHomeDir;
 	}
+	
+    public static File getApplicationHomeFolder() {
+        return new File(getAppHomeDir());
+    }
 
 	/**
 	 * Sets Home Directory location of the application. May be set from outside
@@ -2648,11 +2963,24 @@
 	// }
 
 	public static void initIconThemes() {
-		IconThemeManager iconManager = IconThemeManager.getIconThemeManager();
-		IIconTheme icontheme = iconManager.getIconThemeFromConfig();
-		if (icontheme != null) {
-			iconManager.setCurrent(icontheme);
+		PluginsManager pluginsManager = PluginsLocator.getManager();
+		IconThemeManager iconManager = ToolsSwingLocator.getIconThemeManager();
+		
+		File f = new File(pluginsManager.getApplicationFolder(),"icon-theme");
+		if( !f.exists() ) { 
+			f.mkdir();
 		}
+		iconManager.getRepository().add(f,"_Global");
+		
+		f = new File(pluginsManager.getApplicationHomeFolder(),"icon-theme");
+		if( !f.exists() ) {
+			f.mkdir();
+		}
+		iconManager.getRepository().add(f,"_User");
+		
+		// TODO: falta por cargar de algun fichero de config el nombre del tema 
+		//		  por deecto a usar y seleccionarlo aqui como tema corriente.
+		//
 	}
 
 	/**
