001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.markup.html.image.resource;
018
019import java.util.Locale;
020
021import org.apache.wicket.IResourceFactory;
022import org.apache.wicket.WicketRuntimeException;
023import org.apache.wicket.request.resource.IResource;
024import org.apache.wicket.util.parse.metapattern.Group;
025import org.apache.wicket.util.parse.metapattern.IntegerGroup;
026import org.apache.wicket.util.parse.metapattern.MetaPattern;
027import org.apache.wicket.util.parse.metapattern.OptionalMetaPattern;
028import org.apache.wicket.util.parse.metapattern.parsers.MetaPatternParser;
029
030
031/**
032 * A factory which creates default button images.
033 * 
034 * @author Jonathan Locke
035 */
036public class DefaultButtonImageResourceFactory implements IResourceFactory
037{
038        /**
039         * 
040         * @see org.apache.wicket.IResourceFactory#newResource(java.lang.String, java.util.Locale,
041         *      java.lang.String, java.lang.String)
042         */
043        @Override
044        public IResource newResource(final String specification, final Locale locale,
045                final String style, final String variation)
046        {
047                final Parser parser = new Parser(specification);
048                if (parser.matches())
049                {
050                        return new DefaultButtonImageResource(parser.getWidth(), parser.getHeight(),
051                                parser.getLabel());
052                }
053                else
054                {
055                        throw new WicketRuntimeException(
056                                "DefaultButtonImageResourceFactory does not recognized the specification " +
057                                        specification);
058                }
059        }
060
061        /**
062         * Parses image value specifications.
063         * 
064         * @author Jonathan Locke
065         */
066        private static final class Parser extends MetaPatternParser
067        {
068                /** Group value. */
069                private static final IntegerGroup width = new IntegerGroup();
070
071                /** Group value. */
072                private static final IntegerGroup height = new IntegerGroup();
073
074                /** Label */
075                private static final Group label = new Group(MetaPattern.ANYTHING);
076
077                /** Meta pattern. */
078                private static final MetaPattern pattern = new MetaPattern(
079                        new OptionalMetaPattern(
080                                new MetaPattern[] { width, MetaPattern.COMMA, height, MetaPattern.COLON }), label);
081
082                /**
083                 * Construct.
084                 * 
085                 * @param input
086                 *            to parse
087                 */
088                public Parser(final CharSequence input)
089                {
090                        super(pattern, input);
091                }
092
093                /**
094                 * @return The label
095                 */
096                public String getLabel()
097                {
098                        return label.get(matcher());
099                }
100
101                /**
102                 * @return Any width
103                 */
104                public int getWidth()
105                {
106                        return width.getInt(matcher(), -1);
107                }
108
109                /**
110                 * @return Any height
111                 */
112                public int getHeight()
113                {
114                        return height.getInt(matcher(), -1);
115                }
116        }
117}