Changeset 3855 in josm


Ignore:
Timestamp:
Feb 5, 2011 10:40:39 AM (2 years ago)
Author:
bastiK
Message:

Extended mappaint style dialog. No longer required to restart JOSM for changes in mappaint preferences.

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
12 edited

Legend:

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

    r3843 r3855  
    335335        jb.add(toolBarActions); 
    336336 
    337         jb.addSeparator(new Dimension(0,10)); 
     337        jb.addSeparator(new Dimension(0,18)); 
    338338        toolBarToggle.setAlignmentX(0.5f); 
    339339        jb.add(toolBarToggle); 
  • trunk/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java

    r3848 r3855  
    77import java.awt.Dimension; 
    88import java.awt.Point; 
     9import java.awt.Rectangle; 
    910import java.awt.event.ActionEvent; 
    1011import java.awt.event.KeyEvent; 
    1112import java.awt.event.MouseEvent; 
    12 import java.util.ArrayList; 
    13 import java.util.Collection; 
    14 import java.util.List; 
    1513 
    1614import javax.swing.AbstractAction; 
     
    2018import javax.swing.JScrollPane; 
    2119import javax.swing.JTable; 
     20import javax.swing.JViewport; 
    2221import javax.swing.ListSelectionModel; 
    2322import javax.swing.SwingUtilities; 
     
    2625import javax.swing.event.ListSelectionListener; 
    2726import javax.swing.table.AbstractTableModel; 
     27import javax.swing.table.TableModel; 
    2828 
    2929import org.openstreetmap.josm.Main; 
    3030import org.openstreetmap.josm.gui.SideButton; 
    3131import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; 
    32 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintStyleLoader; 
     32import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener; 
    3333import org.openstreetmap.josm.gui.mappaint.StyleSource; 
     34import org.openstreetmap.josm.gui.preferences.PreferenceDialog; 
    3435import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher; 
    3536import org.openstreetmap.josm.tools.ImageProvider; 
     
    3839public class MapPaintDialog extends ToggleDialog { 
    3940 
    40     protected JTable tblStyles; 
     41    protected StylesTable tblStyles; 
    4142    protected StylesModel model; 
    4243    protected DefaultListSelectionModel selectionModel; 
     
    4748    public MapPaintDialog() { 
    4849        super(tr("Map Paint Styles"), "mapstyle", tr("configure the map painting style"), 
    49                 Shortcut.registerShortcut("subwindow:authors", tr("Toggle: {0}", tr("Authors")), KeyEvent.VK_M, Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT), 250); 
     50                Shortcut.registerShortcut("subwindow:authors", tr("Toggle: {0}", tr("Authors")), KeyEvent.VK_M, Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT), 150); 
    5051        build(); 
    5152    } 
     
    5657 
    5758        model = new StylesModel(); 
    58         model.setStyles(MapPaintStyles.getStyles().getStyleSources()); 
    5959         
    60         tblStyles = new JTable(model); 
     60        tblStyles = new StylesTable(model); 
    6161        tblStyles.setSelectionModel(selectionModel= new DefaultListSelectionModel()); 
    6262        tblStyles.addMouseListener(new PopupMenuHandler()); 
     
    7777    } 
    7878 
     79    protected static class StylesTable extends JTable { 
     80 
     81        public StylesTable(TableModel dm) { 
     82            super(dm); 
     83        } 
     84 
     85        public void scrollToVisible(int row, int col) { 
     86            if (!(getParent() instanceof JViewport)) 
     87                return; 
     88            JViewport viewport = (JViewport) getParent(); 
     89            Rectangle rect = getCellRect(row, col, true); 
     90            Point pt = viewport.getViewPosition(); 
     91            rect.setLocation(rect.x - pt.x, rect.y - pt.y); 
     92            viewport.scrollRectToVisible(rect); 
     93        } 
     94    } 
     95 
    7996    protected JPanel buildButtonRow() { 
    80         JPanel p = getButtonPanel(1); 
     97        JPanel p = getButtonPanel(4); 
    8198        reloadAction = new ReloadAction(); 
    8299        onoffAction = new OnOffAction(); 
     100        MoveUpDownAction up = new MoveUpDownAction(false); 
     101        MoveUpDownAction down = new MoveUpDownAction(true); 
    83102        selectionModel.addListSelectionListener(onoffAction); 
    84103        selectionModel.addListSelectionListener(reloadAction); 
     104        selectionModel.addListSelectionListener(up); 
     105        selectionModel.addListSelectionListener(down); 
    85106        p.add(new SideButton(onoffAction)); 
     107        p.add(new SideButton(up)); 
     108        p.add(new SideButton(down)); 
     109        p.add(new SideButton(new LaunchMapPaintPreferencesAction())); 
     110 
    86111        return p; 
    87112    } 
    88      
    89     protected class StylesModel extends AbstractTableModel { 
    90         List<StyleSource> data; 
    91  
    92         public StylesModel() { 
    93             this.data = new ArrayList<StyleSource>(); 
     113 
     114    @Override 
     115    public void showNotify() { 
     116        MapPaintStyles.addMapPaintSylesUpdateListener(model); 
     117    } 
     118 
     119    @Override 
     120    public void hideNotify() { 
     121        MapPaintStyles.removeMapPaintSylesUpdateListener(model); 
     122    } 
     123 
     124    protected class StylesModel extends AbstractTableModel implements MapPaintSylesUpdateListener { 
     125 
     126        private StyleSource getRow(int i) { 
     127            return MapPaintStyles.getStyles().getStyleSources().get(i); 
    94128        } 
    95129 
     
    101135        @Override 
    102136        public int getRowCount() { 
    103             return data.size(); 
     137            return MapPaintStyles.getStyles().getStyleSources().size(); 
    104138        } 
    105139         
     
    107141        public Object getValueAt(int row, int column) { 
    108142            if (column == 0) 
    109                 return data.get(row).active; 
     143                return getRow(row).active; 
    110144            else 
    111                 return data.get(row).getDisplayString(); 
     145                return getRow(row).getDisplayString(); 
    112146        } 
    113147 
     
    129163                return; 
    130164            if (column == 0) { 
    131                 toggleOnOff(row); 
    132             } 
    133         } 
    134  
    135         public void setStyles(Collection<? extends StyleSource> styles) { 
    136             data.clear(); 
    137             if (styles !=null) { 
    138                 data.addAll(styles); 
    139             } 
     165                MapPaintStyles.toggleStyleActive(row); 
     166            } 
     167        } 
     168 
     169        /** 
     170         * Make sure the first of the selected entry is visible in the 
     171         * views of this model. 
     172         */ 
     173        protected void ensureSelectedIsVisible() { 
     174            int index = selectionModel.getMinSelectionIndex(); 
     175            if (index < 0) return; 
     176            if (index >= getRowCount()) return; 
     177            tblStyles.scrollToVisible(index, 0); 
     178            tblStyles.repaint(); 
     179        } 
     180 
     181        /** 
     182         * MapPaintSylesUpdateListener interface 
     183         */ 
     184 
     185        @Override 
     186        public void mapPaintStylesUpdated() { 
    140187            fireTableDataChanged(); 
    141         } 
    142  
    143         public void toggleOnOff(int... rows) { 
    144             for (Integer p : rows) { 
    145                 StyleSource s = model.data.get(p); 
    146                 s.active = !s.active; 
    147             } 
    148             if (rows.length == 1) { 
    149                 model.fireTableCellUpdated(rows[0], 0); 
    150             } else { 
    151                 model.fireTableDataChanged(); 
    152             } 
    153             MapPaintStyles.getStyles().clearCached(); 
    154             Main.map.mapView.preferenceChanged(null); 
    155             Main.map.mapView.repaint(); 
     188            tblStyles.repaint(); 
     189        } 
     190 
     191        @Override 
     192        public void mapPaintStyleEntryUpdated(int idx) { 
     193            fireTableRowsUpdated(idx, idx); 
     194            tblStyles.repaint(); 
    156195        } 
    157196    } 
     
    176215        public void actionPerformed(ActionEvent e) { 
    177216            int[] pos = tblStyles.getSelectedRows(); 
    178             model.toggleOnOff(pos); 
     217            MapPaintStyles.toggleStyleActive(pos); 
     218            selectionModel.clearSelection(); 
     219            for (int p: pos) { 
     220                selectionModel.addSelectionInterval(p, p); 
     221            } 
     222        } 
     223    } 
     224 
     225    /** 
     226     * The action to move down the currently selected entries in the list. 
     227     */ 
     228    class MoveUpDownAction extends AbstractAction implements ListSelectionListener { 
     229        final int increment; 
     230        public MoveUpDownAction(boolean isDown) { 
     231            increment = isDown ? 1 : -1; 
     232            putValue(SMALL_ICON, isDown ? ImageProvider.get("dialogs", "down") : ImageProvider.get("dialogs", "up")); 
     233            putValue(SHORT_DESCRIPTION, isDown ? tr("Move the selected entry one row down.") : tr("Move the selected entry one row up.")); 
     234            updateEnabledState(); 
     235        } 
     236 
     237        public void updateEnabledState() { 
     238            int[] sel = tblStyles.getSelectedRows(); 
     239            setEnabled(MapPaintStyles.canMoveStyles(sel, increment)); 
     240        } 
     241 
     242        @Override 
     243        public void actionPerformed(ActionEvent e) { 
     244            int[] sel = tblStyles.getSelectedRows(); 
     245            MapPaintStyles.moveStyles(sel, increment); 
     246 
     247            selectionModel.clearSelection(); 
     248            for (int row: sel) { 
     249                selectionModel.addSelectionInterval(row + increment, row + increment); 
     250            } 
     251            model.ensureSelectedIsVisible(); 
     252        } 
     253 
     254        public void valueChanged(ListSelectionEvent e) { 
     255            updateEnabledState(); 
     256        } 
     257    } 
     258     
     259    /** 
     260     * Opens preferences window and selects the mappaint tab. 
     261     */ 
     262    class LaunchMapPaintPreferencesAction extends AbstractAction { 
     263        public LaunchMapPaintPreferencesAction() { 
     264            putValue(SMALL_ICON, ImageProvider.get("dialogs", "mappaintpreference")); 
     265        } 
     266 
     267        @Override 
     268        public void actionPerformed(ActionEvent e) { 
     269            final PreferenceDialog p =new PreferenceDialog(Main.parent); 
     270            p.selectMapPaintPreferenceTab(); 
     271            p.setVisible(true); 
    179272        } 
    180273    } 
     
    192285            boolean e = pos.length > 0; 
    193286            for (int i : pos) { 
    194                 if (!model.data.get(i).isLocal()) { 
     287                if (!model.getRow(i).isLocal()) { 
    195288                    e = false; 
    196289                    break; 
     
    207300        @Override 
    208301        public void actionPerformed(ActionEvent e) { 
    209  
    210302            final int[] rows = tblStyles.getSelectedRows(); 
    211             List<StyleSource> sources = new ArrayList<StyleSource>(); 
    212             for (int p : rows) { 
    213                 sources.add(model.data.get(p)); 
    214             } 
    215             Main.worker.submit(new MapPaintStyleLoader(sources)); 
     303            MapPaintStyles.reloadStyles(rows); 
    216304            Main.worker.submit(new Runnable() { 
    217305                @Override 
     
    220308                        @Override 
    221309                        public void run() { 
    222                             if (rows.length == 1) { 
    223                                 model.fireTableCellUpdated(rows[0], 1); 
    224                             } else { 
    225                                 model.fireTableDataChanged(); 
     310                            selectionModel.clearSelection(); 
     311                            for (int r: rows) { 
     312                                selectionModel.addSelectionInterval(r, r); 
    226313                            } 
    227                             MapPaintStyles.getStyles().clearCached(); 
    228                             Main.map.mapView.preferenceChanged(null); 
    229                             Main.map.mapView.repaint(); 
    230314                        } 
    231315                    }); 
     316 
    232317                } 
    233318            }); 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyles.java

    r3854 r3855  
    3535    } 
    3636 
    37     public void add(StyleSource style) { 
    38         styleSources.add(style); 
    39     } 
    40  
    41     public Collection<StyleSource> getStyleSources() { 
    42         return Collections.<StyleSource>unmodifiableCollection(styleSources); 
     37    public List<StyleSource> getStyleSources() { 
     38        return Collections.<StyleSource>unmodifiableList(styleSources); 
    4339    } 
    4440 
     
    241237        this.drawMultipolygon = drawMultipolygon; 
    242238    } 
     239 
     240    /** 
     241     * remove all style sources; only accessed from MapPaintStyles 
     242     */ 
     243    void clear() { 
     244        styleSources.clear(); 
     245    } 
     246 
     247    /** 
     248     * add a style source; only accessed from MapPaintStyles 
     249     */ 
     250    void add(StyleSource style) { 
     251        styleSources.add(style); 
     252    } 
     253 
     254    /** 
     255     * set the style sources; only accessed from MapPaintStyles 
     256     */ 
     257    void setStyleSources(Collection<StyleSource> sources) { 
     258        styleSources.clear(); 
     259        styleSources.addAll(sources); 
     260    } 
     261 
    243262} 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r3853 r3855  
    66import java.io.IOException; 
    77import java.io.InputStream; 
     8import java.util.ArrayList; 
     9import java.util.Arrays; 
    810import java.util.Collection; 
    911import java.util.Collections; 
    1012import java.util.LinkedList; 
    1113import java.util.List; 
     14import java.util.concurrent.CopyOnWriteArrayList; 
    1215 
    1316import javax.swing.ImageIcon; 
     17import javax.swing.SwingUtilities; 
    1418 
    1519import org.openstreetmap.josm.Main; 
     
    2327import org.openstreetmap.josm.tools.ImageProvider; 
    2428 
     29/** 
     30 * This class manages the ElemStyles instance. The object you get with 
     31 * getStyles() is read only, any manipulation happens via one of 
     32 * the wrapper methods here. (readFromPreferences, moveStyles, ...) 
     33 * 
     34 * On change, mapPaintSylesUpdated() is fired for all listeners. 
     35 */ 
    2536public class MapPaintStyles { 
    2637 
     
    7182    } 
    7283 
    73     @SuppressWarnings("null") 
    7484    public static void readFromPreferences() { 
     85        styles.clear(); 
    7586        iconDirs = Main.pref.getCollection("mappaint.icon.sources", Collections.<String>emptySet()); 
    7687        if(Main.pref.getBoolean("mappaint.icon.enable-defaults", true)) 
     
    8394        } 
    8495 
    85         Collection<? extends SourceEntry> sourceEntries = (new MapPaintPrefMigration()).get(); 
     96        Collection<? extends SourceEntry> sourceEntries = MapPaintPrefMigration.INSTANCE.get(); 
    8697 
    8798        for (SourceEntry entry : sourceEntries) { 
     
    120131            s.loadStyleSource(); 
    121132        } 
     133        fireMapPaintSylesUpdated(); 
     134    } 
     135 
     136    /** 
     137     * reload styles 
     138     * preferences are the same, but the file source may have changed 
     139     * @param sel the indices of styles to reload 
     140     */ 
     141    public static void reloadStyles(final int... sel) { 
     142        List<StyleSource> toReload = new ArrayList<StyleSource>(); 
     143        List<StyleSource> data = styles.getStyleSources(); 
     144        for (int i : sel) { 
     145            toReload.add(data.get(i)); 
     146        } 
     147        Main.worker.submit(new MapPaintStyleLoader(toReload)); 
    122148    } 
    123149 
     
    138164        @Override 
    139165        protected void finish() { 
     166            SwingUtilities.invokeLater(new Runnable() { 
     167                @Override 
     168                public void run() { 
     169                    fireMapPaintSylesUpdated(); 
     170                    styles.clearCached(); 
     171                    Main.map.mapView.preferenceChanged(null); 
     172                    Main.map.mapView.repaint(); 
     173                } 
     174            }); 
    140175        } 
    141176 
     
    154189    } 
    155190 
     191    /** 
     192     * Move position of entries in the current list of StyleSources 
     193     * @param sele The indices of styles to be moved. 
     194     * @param delta The number of lines it should move. positive int moves 
     195     *      down and negative moves up. 
     196     */ 
     197    public static void moveStyles(int[] sel, int delta) { 
     198        if (!canMoveStyles(sel, delta)) 
     199            return; 
     200        int[] selSorted = Arrays.copyOf(sel, sel.length); 
     201        Arrays.sort(selSorted); 
     202        List<StyleSource> data = new ArrayList<StyleSource>(styles.getStyleSources()); 
     203        for (int row: selSorted) { 
     204            StyleSource t1 = data.get(row); 
     205            StyleSource t2 = data.get(row + delta); 
     206            data.set(row, t2); 
     207            data.set(row + delta, t1); 
     208        } 
     209        styles.setStyleSources(data); 
     210        MapPaintPrefMigration.INSTANCE.put(data); 
     211        fireMapPaintSylesUpdated(); 
     212        styles.clearCached(); 
     213        Main.map.mapView.repaint(); 
     214    } 
     215 
     216    public static boolean canMoveStyles(int[] sel, int i) { 
     217        if (sel.length == 0) 
     218            return false; 
     219        int[] selSorted = Arrays.copyOf(sel, sel.length); 
     220        Arrays.sort(selSorted); 
     221 
     222        if (i < 0) { // Up 
     223            return selSorted[0] >= -i; 
     224        } else 
     225        if (i > 0) { // Down 
     226            return selSorted[selSorted.length-1] <= styles.getStyleSources().size() - 1 - i; 
     227        } else 
     228            return true; 
     229    } 
     230 
     231    public static void toggleStyleActive(int... sel) { 
     232        List<StyleSource> data = styles.getStyleSources(); 
     233        for (int p : sel) { 
     234            StyleSource s = data.get(p); 
     235            s.active = !s.active; 
     236        } 
     237        MapPaintPrefMigration.INSTANCE.put(data); 
     238        if (sel.length == 1) { 
     239            fireMapPaintStyleEntryUpdated(sel[0]); 
     240        } else { 
     241            fireMapPaintSylesUpdated(); 
     242        } 
     243        styles.clearCached(); 
     244        Main.map.mapView.repaint(); 
     245    } 
     246 
     247    /*********************************** 
     248     * MapPaintSylesUpdateListener & related code 
     249     *  (get informed when the list of MapPaint StyleSources changes) 
     250     */ 
     251 
     252    public interface MapPaintSylesUpdateListener { 
     253        public void mapPaintStylesUpdated(); 
     254        public void mapPaintStyleEntryUpdated(int idx); 
     255    } 
     256 
     257    protected static final CopyOnWriteArrayList<MapPaintSylesUpdateListener> listeners 
     258            = new CopyOnWriteArrayList<MapPaintSylesUpdateListener>(); 
     259 
     260    public static void addMapPaintSylesUpdateListener(MapPaintSylesUpdateListener listener) { 
     261        if (listener != null) { 
     262            listeners.addIfAbsent(listener); 
     263        } 
     264    } 
     265 
     266    public static void removeMapPaintSylesUpdateListener(MapPaintSylesUpdateListener listener) { 
     267        listeners.remove(listener); 
     268    } 
     269 
     270    public static void fireMapPaintSylesUpdated() { 
     271        for (MapPaintSylesUpdateListener l : listeners) { 
     272            l.mapPaintStylesUpdated(); 
     273        } 
     274    } 
     275 
     276    public static void fireMapPaintStyleEntryUpdated(int idx) { 
     277        for (MapPaintSylesUpdateListener l : listeners) { 
     278            l.mapPaintStyleEntryUpdated(idx); 
     279        } 
     280    } 
    156281} 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java

    r3836 r3855  
    9797                } 
    9898            } 
    99         } else if (primitive instanceof Relation) { 
     99        } else if (primitive instanceof Relation && icon != null) { 
    100100            painter.drawRestriction((Relation) primitive, this); 
    101101        } 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r3848 r3855  
    4141    public void loadStyleSource() { 
    4242        rules.clear(); 
     43        hasError = false; 
    4344        try { 
    4445            MirroredInputStream in = new MirroredInputStream(url); 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/xml/XmlStyleSource.java

    r3848 r3855  
    5050 
    5151    protected void init() { 
     52        hasError = false; 
    5253        icons.clear(); 
    5354        lines.clear(); 
  • trunk/src/org/openstreetmap/josm/gui/preferences/MapPaintPreference.java

    r3854 r3855  
    6868 
    6969        public MapPaintSourceEditor() { 
    70             super("http://josm.openstreetmap.de/styles"); 
     70            super(true, "http://josm.openstreetmap.de/styles"); 
    7171        } 
    7272 
    7373        @Override 
    7474        public Collection<? extends SourceEntry> getInitialSourcesList() { 
    75             return (new MapPaintPrefMigration()).get(); 
     75            return MapPaintPrefMigration.INSTANCE.get(); 
    7676        } 
    7777 
     
    8080            List<SourceEntry> activeStyles = activeSourcesModel.getSources(); 
    8181 
    82             boolean changed = (new MapPaintPrefMigration()).put(activeStyles); 
     82            boolean changed = MapPaintPrefMigration.INSTANCE.put(activeStyles); 
    8383 
    8484            if (tblIconPaths != null) { 
     
    9898        @Override 
    9999        public Collection<ExtendedSourceEntry> getDefault() { 
    100             return (new MapPaintPrefMigration()).getDefault(); 
     100            return MapPaintPrefMigration.INSTANCE.getDefault(); 
    101101        } 
    102102 
     
    143143 
    144144    public boolean ok() { 
    145         Boolean restart = false; 
    146         if(Main.pref.put("mappaint.icon.enable-defaults", enableIconDefault.isSelected())) { 
    147             restart = true; 
     145        boolean reload = Main.pref.put("mappaint.icon.enable-defaults", enableIconDefault.isSelected()); 
     146        reload |= sources.finish(); 
     147        if (reload) { 
     148            MapPaintStyles.readFromPreferences(); 
    148149        } 
    149         if(sources.finish()) { 
    150             restart = true; 
    151         } 
    152         if(Main.isDisplayingMapView()) 
     150        if (Main.isDisplayingMapView()) 
    153151        { 
    154152            MapPaintStyles.getStyles().clearCached(); 
    155153        } 
    156         return restart; 
     154        return false; 
    157155    } 
    158156 
     
    165163 
    166164    public static class MapPaintPrefMigration extends SourceEditor.SourcePrefMigration { 
     165 
     166        public final static MapPaintPrefMigration INSTANCE = new MapPaintPrefMigration(); 
    167167 
    168168        public MapPaintPrefMigration() { 
  • trunk/src/org/openstreetmap/josm/gui/preferences/PreferenceDialog.java

    r3501 r3855  
    131131        } 
    132132    } 
     133 
     134    public void selectMapPaintPreferenceTab() { 
     135        tpPreferences.setSelectedComponent(tpPreferences.map); 
     136        tpPreferences.mapcontent.setSelectedIndex(1); 
     137    } 
    133138} 
  • trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java

    r3816 r3855  
    1212import java.awt.GridBagLayout; 
    1313import java.awt.Insets; 
     14import java.awt.Rectangle; 
    1415import java.awt.event.ActionEvent; 
    1516import java.awt.event.FocusAdapter; 
     
    4344import javax.swing.DefaultListSelectionModel; 
    4445import javax.swing.JButton; 
     46import javax.swing.JCheckBox; 
    4547import javax.swing.JComponent; 
    4648import javax.swing.JFileChooser; 
     
    8183public abstract class SourceEditor extends JPanel { 
    8284 
     85    final protected boolean isMapPaint; 
     86     
    8387    protected JTable tblActiveSources; 
    8488    protected ActiveSourcesModel activeSourcesModel; 
     
    9296    /** 
    9397     * constructor 
     98     * @param isMapPaint true for MapPaintPreference subclass, false 
     99     *  for TaggingPresetPreference subclass 
    94100     * @param availableSourcesUrl the URL to the list of available sources 
    95101     */ 
    96     public SourceEditor(final String availableSourcesUrl) { 
    97  
     102    public SourceEditor(final boolean isMapPaint, final String availableSourcesUrl) { 
     103 
     104        this.isMapPaint = isMapPaint; 
    98105        DefaultListSelectionModel selectionModel = new DefaultListSelectionModel(); 
    99106        lstAvailableSources = new JList(availableSourcesModel = new AvailableSourcesListModel(selectionModel)); 
     
    103110 
    104111        selectionModel = new DefaultListSelectionModel(); 
    105         tblActiveSources = new JTable(activeSourcesModel = new ActiveSourcesModel(selectionModel)); 
     112        tblActiveSources = new JTable(activeSourcesModel = new ActiveSourcesModel(selectionModel)) { 
     113            // some kind of hack to prevent the table from scrolling slightly to the 
     114            // right when clicking on the text 
     115            public void scrollRectToVisible(Rectangle aRect) { 
     116                super.scrollRectToVisible(new Rectangle(0, aRect.y, aRect.width, aRect.height)); 
     117            } 
     118        }; 
    106119        tblActiveSources.putClientProperty("terminateEditOnFocusLost", true); 
    107120        tblActiveSources.setSelectionModel(selectionModel); 
     
    112125        tblActiveSources.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
    113126        SourceEntryTableCellRenderer sourceEntryRenderer = new SourceEntryTableCellRenderer(); 
    114         tblActiveSources.getColumnModel().getColumn(0).setCellRenderer(sourceEntryRenderer); 
     127        if (isMapPaint) { 
     128            tblActiveSources.getColumnModel().getColumn(0).setMaxWidth(1); 
     129            tblActiveSources.getColumnModel().getColumn(0).setResizable(false); 
     130            tblActiveSources.getColumnModel().getColumn(1).setCellRenderer(sourceEntryRenderer); 
     131        } else { 
     132            tblActiveSources.getColumnModel().getColumn(0).setCellRenderer(sourceEntryRenderer); 
     133        } 
     134 
    115135        activeSourcesModel.addTableModelListener(new TableModelListener() { 
    116136            // Force swing to show horizontal scrollbars for the JTable 
     
    118138            @Override 
    119139            public void tableChanged(TableModelEvent e) { 
    120                 adjustColumnWidth(tblActiveSources, 0); 
     140                adjustColumnWidth(tblActiveSources, isMapPaint ? 1 : 0); 
    121141            } 
    122142        }); 
     
    130150                if (e.getClickCount() == 2) { 
    131151                    int row = tblActiveSources.rowAtPoint(e.getPoint()); 
     152                    int col = tblActiveSources.columnAtPoint(e.getPoint()); 
    132153                    if (row < 0 || row >= tblActiveSources.getRowCount()) 
     154                        return; 
     155                    if (isMapPaint  && col != 1) 
    133156                        return; 
    134157                    editActiveSourceAction.actionPerformed(null); 
     
    141164        tblActiveSources.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0), "delete"); 
    142165        tblActiveSources.getActionMap().put("delete", removeActiveSourcesAction); 
     166 
     167        MoveUpDownAction moveUp = null; 
     168        MoveUpDownAction moveDown = null; 
     169        if (isMapPaint) { 
     170            moveUp = new MoveUpDownAction(false); 
     171            moveDown = new MoveUpDownAction(true); 
     172            tblActiveSources.getSelectionModel().addListSelectionListener(moveUp); 
     173            tblActiveSources.getSelectionModel().addListSelectionListener(moveDown); 
     174            activeSourcesModel.addTableModelListener(moveUp); 
     175            activeSourcesModel.addTableModelListener(moveDown); 
     176        } 
    143177 
    144178        ActivateSourcesAction activateSourcesAction = new ActivateSourcesAction(); 
     
    209243        sideButtonTB.add(editActiveSourceAction); 
    210244        sideButtonTB.add(removeActiveSourcesAction); 
     245        sideButtonTB.addSeparator(new Dimension(12, 30)); 
     246        if (isMapPaint) { 
     247            sideButtonTB.add(moveUp); 
     248            sideButtonTB.add(moveDown); 
     249        } 
    211250        add(sideButtonTB, gbc); 
    212251 
     
    435474    } 
    436475 
    437     protected static class ActiveSourcesModel extends AbstractTableModel { 
     476    protected class ActiveSourcesModel extends AbstractTableModel { 
    438477        private List<SourceEntry> data; 
    439478        private DefaultListSelectionModel selectionModel; 
     
    445484 
    446485        public int getColumnCount() { 
    447             return 1; 
     486            return isMapPaint ? 2 : 1; 
    448487        } 
    449488 
     
    453492 
    454493        @Override 
    455         public SourceEntry getValueAt(int rowIndex, int columnIndex) { 
    456             return data.get(rowIndex); 
     494        public Object getValueAt(int rowIndex, int columnIndex) { 
     495            if (isMapPaint && columnIndex == 0) 
     496                return data.get(rowIndex).active; 
     497            else 
     498                return data.get(rowIndex); 
    457499        } 
    458500 
    459501        @Override 
    460502        public boolean isCellEditable(int rowIndex, int columnIndex) { 
    461             return false; 
    462         } 
     503            return isMapPaint && columnIndex == 0; 
     504        } 
     505 
     506        Class<?>[] columnClasses = {Boolean.class, SourceEntry.class}; 
    463507 
    464508        @Override 
    465         public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 
    466             updateSource(rowIndex, (String)aValue); 
     509        public Class<?> getColumnClass(int column) { 
     510            return isMapPaint ? columnClasses[column] : SourceEntry.class; 
     511        } 
     512 
     513        @Override 
     514        public void setValueAt(Object aValue, int row, int column) { 
     515            if (row < 0 || row >= getRowCount() || aValue == null) 
     516                return; 
     517            if (isMapPaint && column == 0) { 
     518                data.get(row).active = ! data.get(row).active; 
     519            } 
    467520        } 
    468521 
     
    470523            data.clear(); 
    471524            if (sources != null) { 
    472                 data.addAll(sources); 
     525                for (SourceEntry e : sources) { 
     526                    data.add(new SourceEntry(e)); 
     527                } 
    473528            } 
    474529            fireTableDataChanged(); 
     
    485540        } 
    486541 
    487         public void updateSource(int pos, String src) { 
    488             if (src == null) return; 
    489             if (pos < 0 || pos >= getRowCount()) return; 
    490             data.get(pos).url = src; 
    491             fireTableDataChanged(); 
    492             int idx = data.indexOf(src); 
    493             if (idx >= 0) { 
    494                 selectionModel.setSelectionInterval(idx, idx); 
    495             } 
    496         } 
    497  
    498542        public void removeSelected() { 
    499543            Iterator<SourceEntry> it = data.iterator(); 
     
    538582            return new ArrayList<SourceEntry>(data); 
    539583        } 
     584 
     585        public boolean canMove(int i) { 
     586            int[] sel = tblActiveSources.getSelectedRows(); 
     587            if (sel.length == 0) 
     588                return false; 
     589            if (i < 0) { // Up 
     590                return sel[0] >= -i; 
     591            } else if (i > 0) { // Down 
     592                return sel[sel.length-1] <= getRowCount()-1 - i; 
     593            } else 
     594                return true; 
     595        } 
     596 
     597        public void move(int i) { 
     598            if (!canMove(i)) return; 
     599            int[] sel = tblActiveSources.getSelectedRows(); 
     600            for (int row: sel) { 
     601                SourceEntry t1 = data.get(row); 
     602                SourceEntry t2 = data.get(row + i); 
     603                data.set(row, t2); 
     604                data.set(row + i, t1); 
     605            } 
     606            selectionModel.clearSelection(); 
     607            for (int row: sel) { 
     608                selectionModel.addSelectionInterval(row + i, row + i); 
     609            } 
     610        } 
    540611    } 
    541612 
     
    590661        private JTextField tfName; 
    591662        private JTextField tfURL; 
     663        private JCheckBox cbActive; 
    592664 
    593665        public EditSourceEntryDialog(Component parent, String title, SourceEntry e) { 
     
    604676            tfURL = new JTextField(60); 
    605677            p.add(new JLabel(tr("URL / File:")), GBC.std().insets(15, 0, 5, 0)); 
    606             p.add(tfURL, GBC.std().insets(0, 0, 5, 0)); 
     678            p.add(tfURL, GBC.std().insets(0, 0, 5, 5)); 
    607679            JButton fileChooser = new JButton(new LaunchFileChooserAction()); 
    608680            fileChooser.setMargin(new Insets(0, 0, 0, 0)); 
    609             p.add(fileChooser, GBC.eol().insets(0, 0, 5, 0)); 
     681            p.add(fileChooser, GBC.eol().insets(0, 0, 5, 5)); 
    610682 
    611683            if (e != null) { 
     
    616688            } 
    617689 
     690            if (isMapPaint) { 
     691                cbActive = new JCheckBox(tr("active"), e != null ? e.active : true); 
     692                p.add(cbActive, GBC.eol().insets(15, 0, 5, 0)); 
     693            } 
    618694            setButtonIcons(new String[] {"ok", "cancel"}); 
    619695            setContent(p); 
     
    669745            return tfURL.getText(); 
    670746        } 
     747 
     748        public boolean active() { 
     749            if (!isMapPaint) 
     750                throw new UnsupportedOperationException(); 
     751            return cbActive.isSelected(); 
     752        } 
    671753    } 
    672754 
     
    685767            editEntryDialog.showDialog(); 
    686768            if (editEntryDialog.getValue() == 1) { 
     769                boolean active = true; 
     770                if (isMapPaint) { 
     771                    active = editEntryDialog.active(); 
     772                } 
    687773                activeSourcesModel.addSource(new SourceEntry( 
    688774                        editEntryDialog.getURL(), 
    689                         null, editEntryDialog.getShortdescription(), true)); 
     775                        null, editEntryDialog.getShortdescription(), active)); 
    690776                activeSourcesModel.fireTableDataChanged(); 
    691777            } 
     
    736822                return; 
    737823 
    738             SourceEntry e = activeSourcesModel.getValueAt(pos, 0); 
     824            SourceEntry e = (SourceEntry) activeSourcesModel.getValueAt(pos, 1); 
    739825 
    740826            EditSourceEntryDialog editEntryDialog = new EditSourceEntryDialog( 
     
    749835                } 
    750836                e.url = editEntryDialog.getURL(); 
    751                 activeSourcesModel.fireTableCellUpdated(pos, 0); 
    752             } 
     837                if (isMapPaint) { 
     838                    e.active = editEntryDialog.active(); 
     839                } 
     840                activeSourcesModel.fireTableRowsUpdated(pos, pos); 
     841            } 
     842        } 
     843    } 
     844 
     845    /** 
     846     * The action to move the currently selected entries up or down in the list. 
     847     */ 
     848    class MoveUpDownAction extends AbstractAction implements ListSelectionListener, TableModelListener { 
     849        final int increment; 
     850        public MoveUpDownAction(boolean isDown) { 
     851            increment = isDown ? 1 : -1; 
     852            putValue(SMALL_ICON, isDown ? ImageProvider.get("dialogs", "down") : ImageProvider.get("dialogs", "up")); 
     853            putValue(SHORT_DESCRIPTION, isDown ? tr("Move the selected entry one row down.") : tr("Move the selected entry one row up.")); 
     854            updateEnabledState(); 
     855        } 
     856 
     857        public void updateEnabledState() { 
     858            setEnabled(activeSourcesModel.canMove(increment)); 
     859        } 
     860 
     861        @Override 
     862        public void actionPerformed(ActionEvent e) { 
     863            activeSourcesModel.move(increment); 
     864        } 
     865 
     866        public void valueChanged(ListSelectionEvent e) { 
     867            updateEnabledState(); 
     868        } 
     869 
     870        public void tableChanged(TableModelEvent e) { 
     871            updateEnabledState(); 
    753872        } 
    754873    } 
     
    11071226            } 
    11081227            s.append(entry.url); 
    1109             if (entry.name != null) { 
     1228            if (entry.shortdescription != null) { 
    11101229                s.append(")"); 
    11111230            } 
  • trunk/src/org/openstreetmap/josm/gui/preferences/SourceEntry.java

    r3843 r3855  
    4646        this.shortdescription = equal(shortdescription, "") ? null : shortdescription; 
    4747        this.active = active; 
     48    } 
     49 
     50    public SourceEntry(SourceEntry e) { 
     51        this.url = e.url; 
     52        this.name = e.name; 
     53        this.shortdescription = e.shortdescription; 
     54        this.active = e.active; 
    4855    } 
    4956 
  • trunk/src/org/openstreetmap/josm/gui/preferences/TaggingPresetPreference.java

    r3797 r3855  
    166166 
    167167        public TaggingPresetSourceEditor() { 
    168             super("http://josm.openstreetmap.de/presets"); 
     168            super(false, "http://josm.openstreetmap.de/presets"); 
    169169        } 
    170170 
    171171        @Override 
    172172        public Collection<? extends SourceEntry> getInitialSourcesList() { 
    173             return (new PresetPrefMigration()).get(); 
     173            return PresetPrefMigration.INSTANCE.get(); 
    174174        } 
    175175 
     
    178178            List<SourceEntry> activeStyles = activeSourcesModel.getSources(); 
    179179 
    180             boolean changed = (new PresetPrefMigration()).put(activeStyles); 
     180            boolean changed = PresetPrefMigration.INSTANCE.put(activeStyles); 
    181181 
    182182            if (tblIconPaths != null) { 
     
    196196        @Override 
    197197        public Collection<ExtendedSourceEntry> getDefault() { 
    198             return (new PresetPrefMigration()).getDefault(); 
     198            return PresetPrefMigration.INSTANCE.getDefault(); 
    199199        } 
    200200 
     
    291291    public static class PresetPrefMigration extends SourceEditor.SourcePrefMigration { 
    292292 
     293        public final static PresetPrefMigration INSTANCE = new PresetPrefMigration(); 
     294 
    293295        public PresetPrefMigration() { 
    294296            super("taggingpreset.sources", 
Note: See TracChangeset for help on using the changeset viewer.