| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.command; |
| 16 | |
| 17 | import java.util.StringTokenizer; |
| 18 | |
| 19 | import org.apache.hivemind.ApplicationRuntimeException; |
| 20 | import org.apache.hivemind.HiveMind; |
| 21 | import org.apache.hivemind.Location; |
| 22 | import org.apache.hivemind.internal.Module; |
| 23 | import org.apache.hivemind.service.ObjectProvider; |
| 24 | |
| 25 | import net.sourceforge.hiveutils.service.ObjectBuilder; |
| 26 | |
| 27 | /** |
| 28 | * @author Jean-Francois Poilpret |
| 29 | */ |
| 30 | public class ActionObjectProvider implements ObjectProvider |
| 31 | { |
| 32 | public ActionObjectProvider(ObjectBuilder builder, ActionFactory factory) |
| 33 | { |
| 34 | _builder = builder; |
| 35 | _factory = factory; |
| 36 | } |
| 37 | |
| 38 | public Object provideObject( Module contributingModule, |
| 39 | Class propertyType, |
| 40 | String inputValue, |
| 41 | Location location) |
| 42 | { |
| 43 | if (HiveMind.isBlank(inputValue)) |
| 44 | { |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | StringTokenizer tokenizer = new StringTokenizer(inputValue, ":"); |
| 49 | if (tokenizer.countTokens() == NUM_TOKENS_ONLY_ID) |
| 50 | { |
| 51 | String action = tokenizer.nextToken(); |
| 52 | return _factory.getAction(action); |
| 53 | } |
| 54 | if (tokenizer.countTokens() == NUM_TOKENS_TARGET_ID) |
| 55 | { |
| 56 | String target = tokenizer.nextToken(); |
| 57 | String action = tokenizer.nextToken(); |
| 58 | return _factory.getAction(action, _builder.create(target)); |
| 59 | } |
| 60 | throw new ApplicationRuntimeException( |
| 61 | "Bad input value <" + inputValue + ">", location, null); |
| 62 | } |
| 63 | |
| 64 | static final private int NUM_TOKENS_ONLY_ID = 1; |
| 65 | static final private int NUM_TOKENS_TARGET_ID = 2; |
| 66 | |
| 67 | private final ObjectBuilder _builder; |
| 68 | private final ActionFactory _factory; |
| 69 | } |