/* 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed  under the  License is distributed on an "AS IS" BASIS,
 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 * implied.
 * 
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.jmeter.gui.util;

import java.net.URL;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;

import org.apache.jmeter.gui.action.ActionRouter;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JMeterException;
import org.apache.log.Logger;

/**
 * The JMeter main toolbar class
 *
 */
public class JMeterToolBar {
    
    private static final Logger log = LoggingManager.getLoggerForClass();

    // protected fields: JMeterToolBar class can be use to create another toolbar (plugin, etc.)    
    protected static final String defaultIconProp = "org/apache/jmeter/images/toolbar/icons-toolbar.properties"; //$NON-NLS-1$
    
    protected static final String keyIconProp = "jmeter.toolbar.icons"; //$NON-NLS-1$
    
    /**
     * Create the default JMeter toolbar
     * @return the JMeter toolbar
     */
    public static JToolBar createToolbar(boolean visible) {
        JToolBar toolBar = new JToolBar();
        toolBar.setFloatable(false);
        toolBar.setVisible(visible);

        LinkedHashMap<String, IconToolbarBean> icons = getIconMappings();
        if (icons != null) {
            Collection<IconToolbarBean> enumIcons = icons.values();
            for (IconToolbarBean iconToolbarBean : enumIcons) {
                if (iconToolbarBean == null) {
                    toolBar.addSeparator();
                } else {
                    toolBar.add(makeButtonItemRes(iconToolbarBean));
                }
            }
        }
        // implicit return empty toolbar if icons == null
        return toolBar;
    }
    
    /**
     * Generate a button component from icon bean
     * @param iconBean contains I18N key, ActionNames, icon path, optional icon path pressed
     * @return a button for toolbar
     */
    private static JButton makeButtonItemRes(IconToolbarBean iconBean) {
        final URL imageURL = JMeterUtils.class.getClassLoader().getResource(iconBean.getIconPath());
        JButton button = new JButton(new ImageIcon(imageURL));
        button.setToolTipText(JMeterUtils.getResString(iconBean.getI18nKey()));
        final URL imageURLPressed = JMeterUtils.class.getClassLoader().getResource(iconBean.getIconPathPressed());
        button.setPressedIcon(new ImageIcon(imageURLPressed));
        button.addActionListener(ActionRouter.getInstance());
        button.setActionCommand(iconBean.getActionNameResolve());
        return button;
    }
    
    /**
     * Parse icon set file.
     * @return List of icons/action definition
     */
    private static LinkedHashMap<String, IconToolbarBean> getIconMappings() {
        String iconProp = JMeterUtils.getPropDefault(keyIconProp, defaultIconProp); //$NON-NLS-1$
        Properties p = JMeterUtils.loadProperties(iconProp);
        if (p == null && !iconProp.equals(defaultIconProp)) {
            log.info(iconProp + " not found - using " + defaultIconProp);
            iconProp = defaultIconProp;
            p = JMeterUtils.loadProperties(defaultIconProp);
        }
        if (p == null) {
            JOptionPane.showMessageDialog(null, JMeterUtils
                    .getResString("toolbar_icon_set_not_found"), // $NON-NLS-1$
                    JMeterUtils.getResString("toolbar_icon_set_not_found"), // $NON-NLS-1$
                    JOptionPane.WARNING_MESSAGE);
            return null;
        }
        log.info("Loading toolbar icons properties from " + iconProp); //$NON-NLS-1$
        
        String order = p.getProperty("toolbar"); //$NON-NLS-1$
        p.remove("toolbar"); //$NON-NLS-1$
        String[] oList = order.split(","); //$NON-NLS-1$
        
        LinkedHashMap<String, IconToolbarBean> listIcons = new LinkedHashMap<String, IconToolbarBean>();
        int ctr = 1;
        for (String key : oList) {
            log.debug("Toolbar icon key: " + key); //$NON-NLS-1$
            if (key.trim().equals("|")) { //$NON-NLS-1$
                listIcons.put("space" + ctr++, null); //$NON-NLS-1$
            } else {
                try {
                    IconToolbarBean itb = new IconToolbarBean(p.getProperty(key));
                    listIcons.put(key, itb);
                } catch (JMeterException je) {
                    log.error("Toolbar icon loading error - key: " + key); //$NON-NLS-1$
                }
            }
        }
        return listIcons;
    }

}

@@ -885,26 +885,25 @@
 va.util.
-Collection
+ArrayList
 ;%0Aimport
@@ -915,27 +915,18 @@
 .util.Li
-nkedHashMap
+st
 ;%0Aimport
@@ -1440,24 +1440,175 @@
 orClass();%0A%0A
+    private static final String TOOLBAR_ENTRY_SEP = %22,%22;  //$NON-NLS-1$%0A%0A    private static final String TOOLBAR_PROP_NAME = %22toolbar%22; //$NON-NLS-1$%0A%0A
     // prote
@@ -2198,36 +2198,19 @@
       Li
-nkedHashMap%3CString, 
+st%3C
 IconTool
@@ -2278,76 +2278,8 @@
 ) %7B%0A
-            Collection%3CIconToolbarBean%3E enumIcons = icons.values();%0A
     
@@ -2329,13 +2329,9 @@
 n : 
-enumI
+i
 cons
@@ -3618,36 +3618,19 @@
 tatic Li
-nkedHashMap%3CString, 
+st%3C
 IconTool
@@ -4137,27 +4137,16 @@
 g(null, 
-JMeterUtils
 %0A       
@@ -4158,16 +4158,27 @@
         
+JMeterUtils
 .getResS
@@ -4540,76 +4540,64 @@
 rty(
-%22toolbar%22); //$NON-NLS-1$%0A        p.remove(%22toolbar%22); //$NON-NLS-1$
+TOOLBAR_PROP_NAME);%0A        p.remove(TOOLBAR_PROP_NAME);
 %0A   
@@ -4634,27 +4634,27 @@
 lit(
-%22,%22); //$NON-NLS-1$
+TOOLBAR_ENTRY_SEP);
 %0A   
@@ -4669,36 +4669,19 @@
       Li
-nkedHashMap%3CString, 
+st%3C
 IconTool
@@ -4709,69 +4709,36 @@
 new 
-LinkedHashMap%3CString, IconToolbarBean%3E();%0A        int ctr = 1
+ArrayList%3CIconToolbarBean%3E()
 ;%0A  
@@ -4921,49 +4921,18 @@
 ons.
-put(%22space%22 + ctr++, null); //$NON-NLS-1$
+add(null);
 %0A   
@@ -5088,17 +5088,12 @@
 ons.
-put(key, 
+add(
 itb)

Use ArrayList for icons to allow duplicates Use constants for some magic strings