Changeset 7450 in josm for trunk/src


Ignore:
Timestamp:
2014-08-28T09:57:36+02:00 (10 years ago)
Author:
bastiK
Message:

fixed #10421 - User-settings for mapcss mappaint styles

  • only boolean setting for now
  • no shortcut support so far
Location:
trunk/src/org/openstreetmap/josm/gui
Files:
1 added
4 edited

Legend:

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

    r7185 r7450  
    3535import javax.swing.JFileChooser;
    3636import javax.swing.JLabel;
     37import javax.swing.JMenu;
    3738import javax.swing.JPanel;
    3839import javax.swing.JPopupMenu;
     
    6667import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    6768import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener;
     69import org.openstreetmap.josm.gui.mappaint.StyleSetting;
    6870import org.openstreetmap.josm.gui.mappaint.StyleSource;
    6971import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
     
    667669            add(reloadAction);
    668670            add(new SaveAsAction());
     671
     672            JMenu setMenu = new JMenu(tr("Style settings"));
     673            setMenu.setIcon(ImageProvider.overlay(ImageProvider.get("preference"),
     674                ImageProvider.get("dialogs/mappaint/pencil.png"),
     675                ImageProvider.OverlayPosition.SOUTHEAST));
     676            add(setMenu);
     677
     678            int sel = tblStyles.getSelectionModel().getLeadSelectionIndex();
     679            StyleSource style = null;
     680            if (sel >= 0 && sel < model.getRowCount()) {
     681                style = model.getRow(sel);
     682            }
     683            if (style == null || style.settings.isEmpty()) {
     684                setMenu.setEnabled(false);
     685            } else {
     686                for (StyleSetting s : style.settings) {
     687                    s.addMenuEntry(setMenu);
     688                }
     689            }
     690
    669691            addSeparator();
    670692            add(new InfoAction());
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java

    r7248 r7450  
    1111import java.util.Collection;
    1212import java.util.Collections;
     13import java.util.HashMap;
    1314import java.util.List;
     15import java.util.Map;
    1416
    1517import javax.swing.ImageIcon;
     
    4143
    4244    public String icon;
     45
     46    /**
     47     * List of settings for user customization.
     48     */
     49    public final List<StyleSetting> settings = new ArrayList<>();
     50    /**
     51     * Values of the settings for efficient lookup.
     52     */
     53    public Map<String, Object> settingValues = new HashMap<>();
    4354
    4455    public StyleSource(String url, String name, String title) {
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java

    r7246 r7450  
    1818import java.util.Collections;
    1919import java.util.List;
    20 import java.util.Objects;
    2120import java.util.regex.Matcher;
    2221import java.util.regex.Pattern;
     
    645644        /**
    646645         * Get the number of tags for the current primitive.
    647          * @param env
     646         * @param env the environment
    648647         * @return number of tags
    649648         */
    650649        public static int number_of_tags(Environment env) {
    651650            return env.osm.getNumKeys();
     651        }
     652       
     653        /**
     654         * Get value of a setting.
     655         * @param env the environment
     656         * @param key setting key (given as layer identifier, e.g. setting::mykey {...})
     657         * @return the value of the setting (calculated when the style is loaded)
     658         */
     659        public static Object setting(Environment env, String key) {
     660            return env.source.settingValues.get(key);
    652661        }
    653662    }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r7448 r7450  
    3535import org.openstreetmap.josm.gui.mappaint.MultiCascade;
    3636import org.openstreetmap.josm.gui.mappaint.Range;
     37import org.openstreetmap.josm.gui.mappaint.StyleSetting;
     38import org.openstreetmap.josm.gui.mappaint.StyleSetting.BooleanStyleSetting;
    3739import org.openstreetmap.josm.gui.mappaint.StyleSource;
    3840import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.SimpleKeyValueCondition;
     
    7072    private String css = null;
    7173    private ZipFile zipFile;
    72 
     74   
    7375    /**
    7476     * This lock prevents concurrent execution of {@link MapCSSRuleIndex#clear() } /
     
    224226                    loadMeta();
    225227                    loadCanvas();
     228                    loadSettings();
    226229                } finally {
    227230                    closeSourceInputStream(in);
     
    276279                        break;
    277280                    case "meta":
     281                    case "setting":
    278282                        break;
    279283                    default:
     
    352356        }
    353357    }
     358   
     359    private void loadSettings() {
     360        settings.clear();
     361        settingValues.clear();
     362        MultiCascade mc = new MultiCascade();
     363        Node n = new Node();
     364        String code = LanguageInfo.getJOSMLocaleCode();
     365        n.put("lang", code);
     366        // create a fake environment to read the meta data block
     367        Environment env = new Environment(n, mc, "default", this);
     368
     369        for (MapCSSRule r : rules) {
     370            if ((r.selector instanceof GeneralSelector)) {
     371                GeneralSelector gs = (GeneralSelector) r.selector;
     372                if (gs.getBase().equals("setting")) {
     373                    if (!gs.matchesConditions(env)) {
     374                        continue;
     375                    }
     376                    env.layer = gs.getSubpart();
     377                    r.execute(env);
     378                }
     379            }
     380        }
     381        for (Entry<String, Cascade> e : mc.getLayers()) {
     382            if ("default".equals(e.getKey())) {
     383                Main.warn("setting requires layer identifier e.g. 'setting::my_setting {...}'");
     384                continue;
     385            }
     386            Cascade c = e.getValue();
     387            String type = c.get("type", null, String.class);
     388            StyleSetting set = null;
     389            if ("boolean".equals(type)) {
     390                set = BooleanStyleSetting.create(c, this, e.getKey());
     391            } else {
     392                Main.warn("Unkown setting type: "+type);
     393            }
     394            if (set != null) {
     395                settings.add(set);
     396                settingValues.put(e.getKey(), set.getValue());
     397            }
     398        }
     399    }
    354400
    355401    private Cascade constructSpecial(String type) {
Note: See TracChangeset for help on using the changeset viewer.