Changeset 5803 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2013-03-27T12:20:35+01:00 (11 years ago)
Author:
akks
Message:

Do not delete custom menu items from ImageryMenu when it is refreshed, see #8441

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/ImageryMenu.java

    r5733 r5803  
    66
    77import java.awt.Component;
     8import java.awt.MenuComponent;
    89import java.awt.Toolkit;
    910import java.awt.event.ActionEvent;
     11import java.util.ArrayList;
    1012import java.util.HashSet;
    1113import java.util.Iterator;
     
    7981    private JMenuItem offsetMenuItem = singleOffset;
    8082    private Map_Rectifier_WMSmenuAction rectaction = new Map_Rectifier_WMSmenuAction();
     83    private boolean bottomItemAdded = false;
    8184
    8285    public ImageryMenu() {
     
    114117     */
    115118    public void refreshImageryMenu() {
    116         removeAll();
     119        removeDynamicItems();
    117120
    118121        // for each configured ImageryInfo, add a menu entry.
    119122        for (final ImageryInfo u : ImageryLayerInfo.instance.getLayers()) {
    120             add(new AddImageryLayerAction(u));
     123            addDynamic(new AddImageryLayerAction(u));
    121124        }
    122125
     
    150153            }
    151154            if (!inViewLayers.isEmpty()) {
    152                 addSeparator();
     155                addDynamicSeparator();
    153156                for (ImageryInfo i : inViewLayers) {
    154                     add(new AddImageryLayerAction(i));
    155                 }
    156             }
    157         }
    158 
    159         addSeparator();
    160         add(new JMenuItem(rectaction));
    161 
    162         addSeparator();
    163         add(offsetMenuItem);
     157                    addDynamic(new AddImageryLayerAction(i));
     158                }
     159            }
     160        }
     161
     162        addDynamicSeparator();
     163        addDynamic(rectaction);
     164
     165        addDynamicSeparator();
     166        addDynamic(offsetMenuItem);
    164167    }
    165168
     
    217220        }
    218221    }
     222
     223    /**
     224     * Collection to store temporary menu items. They will be deleted
     225     * (and possibly recreated) when refreshImageryMenu() is called.
     226     * @since 5803
     227     */
     228    private List <Object> dynamicItems = new ArrayList<Object>(20);
     229   
     230    /**
     231     * Remove all the items in @field dynamicItems collection
     232     * @since 5803
     233     */
     234    private void removeDynamicItems() {
     235        for (Object item : dynamicItems) {
     236            if (item instanceof JMenuItem) {
     237                remove((JMenuItem)item);
     238            }
     239            if (item instanceof MenuComponent) {
     240                remove((MenuComponent)item);
     241            }
     242            if (item instanceof Component) {
     243                remove((Component)item);
     244            }
     245        }
     246        dynamicItems.clear();
     247    }
     248
     249    private void addDynamicSeparator() {
     250        JPopupMenu.Separator s =  new JPopupMenu.Separator();
     251        dynamicItems.add(s);
     252        add(s);
     253    }
     254   
     255    private void addDynamic(Action a) {
     256        dynamicItems.add( this.add(a) );
     257    }
     258   
     259    private void addDynamic(JMenuItem it) {
     260        dynamicItems.add( this.add(it) );
     261    }
    219262}
Note: See TracChangeset for help on using the changeset viewer.