Changeset 4032 in josm


Ignore:
Timestamp:
2011-04-16T23:43:53+02:00 (13 years ago)
Author:
stoecker
Message:

allow to define icon and name for search and background layer actions in toolbar

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java

    r3878 r4032  
    1111import org.openstreetmap.josm.gui.layer.ImageryLayer;
    1212
    13 public class AddImageryLayerAction extends JosmAction {
     13public class AddImageryLayerAction extends JosmAction implements AdaptableAction {
    1414
    1515    private final ImageryInfo info;
  • trunk/src/org/openstreetmap/josm/actions/ParameterizedAction.java

    r3720 r4032  
    88import javax.swing.Action;
    99
    10 public interface ParameterizedAction extends Action {
     10public interface ParameterizedAction extends AdaptableAction {
    1111
    1212    List<ActionParameter<?>> getActionParameters();
  • trunk/src/org/openstreetmap/josm/gui/preferences/ToolbarPreferences.java

    r3737 r4032  
    2727import java.util.Map;
    2828
     29import javax.swing.AbstractAction;
    2930import javax.swing.Action;
    3031import javax.swing.DefaultListCellRenderer;
    3132import javax.swing.DefaultListModel;
    3233import javax.swing.Icon;
     34import javax.swing.ImageIcon;
    3335import javax.swing.JButton;
    3436import javax.swing.JComponent;
     
    5759import org.openstreetmap.josm.Main;
    5860import org.openstreetmap.josm.actions.ActionParameter;
     61import org.openstreetmap.josm.actions.AdaptableAction;
    5962import org.openstreetmap.josm.actions.ParameterizedAction;
    6063import org.openstreetmap.josm.actions.ParameterizedActionDecorator;
     
    7073    public static class ActionDefinition {
    7174        private final Action action;
     75        private String name = "";
     76        private String icon = "";
    7277        private final Map<String, Object> parameters = new HashMap<String, Object>();
    7378
     
    8994        public Action getAction() {
    9095            return action;
     96        }
     97
     98        public String getName() {
     99            return name;
     100        }
     101
     102        public void setName(String name) {
     103            this.name = name;
     104            action.putValue(AbstractAction.SHORT_DESCRIPTION, Main.platform.makeTooltip(name, null));
     105            action.putValue(AbstractAction.NAME, name);
     106        }
     107
     108        public String getIcon() {
     109            return icon;
     110        }
     111
     112        public void setIcon(String icon) {
     113            this.icon = icon;
     114            ImageIcon ico = ImageProvider.getIfAvailable("", icon);
     115            if(ico != null)
     116                action.putValue(AbstractAction.SMALL_ICON, ico);
    91117        }
    92118
     
    135161            this.s = actionName.toCharArray();
    136162
    137             String name = readTillChar('(', '(');
     163            String name = readTillChar('(', '{');
    138164            Action action = actions.get(name);
    139165
     
    152178                }
    153179
    154                 do {
     180                while (index < s.length && s[index] != ')') {
    155181                    String paramName = readTillChar('=', '=');
    156182                    skip('=');
     
    163189                    }
    164190                    skip(',');
    165                 } while (index < s.length && s[index] != ')');
     191                }
     192                skip(')');
     193            }
     194            if (action instanceof AdaptableAction) {
     195                skip('{');
     196
     197                while (index < s.length && s[index] != '}') {
     198                    String paramName = readTillChar('=', '=');
     199                    skip('=');
     200                    String paramValue = readTillChar(',','}');
     201                    if ("icon".equals(paramName) && paramValue.length() > 0)
     202                        result.setIcon(paramValue);
     203                    else if("name".equals(paramName) && paramValue.length() > 0)
     204                        result.setName(paramValue);
     205                    skip(',');
     206                }
     207                skip('}');
    166208            }
    167209
     
    172214            for (int i=0; i<s.length(); i++) {
    173215                char ch = s.charAt(i);
    174                 if (ch == '\\' || ch == '(' || ch == ',' || ch == ')' || ch == '=') {
     216                if (ch == '\\' || ch == '(' || ch == '{' || ch == ',' || ch == ')' || ch == '}' || ch == '=') {
    175217                    result.append('\\');
    176218                    result.append(ch);
     
    204246                }
    205247            }
     248            if (action.getAction() instanceof AdaptableAction) {
     249                boolean first = true;
     250                String tmp = action.getName();
     251                if(tmp.length() != 0) {
     252                    result.append(first ? "{" : ",");
     253                    result.append("name=");
     254                    escape(tmp);
     255                    first = false;
     256                }
     257                tmp = action.getIcon();
     258                if(tmp.length() != 0) {
     259                    result.append(first ? "{" : ",");
     260                    result.append("icon=");
     261                    escape(tmp);
     262                    first = false;
     263                }
     264                if(!first)
     265                    result.append('}');
     266            }
    206267
    207268            return result.toString();
     
    221282                return 0;
    222283            ParameterizedAction pa = (ParameterizedAction)currentAction.getAction();
    223             return pa.getActionParameters().size();
     284            return pa.getActionParameters().size()
     285            + ((currentAction.getAction() instanceof ParameterizedAction) ? 2 : 0);
    224286        }
    225287
     
    231293
    232294        public Object getValueAt(int rowIndex, int columnIndex) {
     295            if(currentAction.getAction() instanceof ParameterizedAction)
     296            {
     297                if (rowIndex < 2) {
     298                    switch (columnIndex) {
     299                    case 0:
     300                        return rowIndex == 0 ? tr("Tooltip") : tr("Icon");
     301                    case 1:
     302                        return rowIndex == 0 ? currentAction.getName() : currentAction.getIcon();
     303                    default:
     304                        return null;
     305                    }
     306                } else
     307                    rowIndex -= 2;
     308            }
    233309            ActionParameter<Object> param = getParam(rowIndex);
    234310            switch (columnIndex) {
     
    249325        @Override
    250326        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
     327            if(currentAction.getAction() instanceof ParameterizedAction)
     328            {
     329                if (rowIndex == 0) {
     330                     currentAction.setName((String)aValue);
     331                     return;
     332                } else if (rowIndex == 1) {
     333                     currentAction.setIcon((String)aValue);
     334                     return;
     335                } else
     336                    rowIndex -= 2;
     337            }
    251338            ActionParameter<Object> param = getParam(rowIndex);
    252339            currentAction.getParameters().put(param.getName(), param.readFromString((String)aValue));
Note: See TracChangeset for help on using the changeset viewer.