01 /*
02 * Copyright 2014-2017 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package griffon.builder.swing.factory;
17
18 import griffon.plugins.crystalicons.Crystal;
19 import griffon.swing.support.crystalicons.CrystalIcon;
20 import groovy.util.AbstractFactory;
21 import groovy.util.FactoryBuilderSupport;
22
23 import java.util.Map;
24
25 /**
26 * @author Andres Almiray
27 */
28 public class CrystalIconFactory extends AbstractFactory {
29 @Override
30 public Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException {
31 Object icon = attributes.remove("icon");
32 if (icon == null) {
33 icon = value;
34 }
35
36 if (icon == null) {
37 throw new IllegalArgumentException("In " + name + " you must define a node value or icon:");
38 }
39
40 int size = 16;
41 if (attributes.containsKey("size")) {
42 size = (int) attributes.remove("size");
43 }
44
45 if (icon instanceof CharSequence) {
46 return new CrystalIcon(Crystal.findByDescription(icon.toString()), size);
47 } else if (icon instanceof Crystal) {
48 return new CrystalIcon((Crystal) icon, size);
49 }
50
51 throw new IllegalArgumentException(name + " cannot parse " + icon + " as a CrystalIcon description.");
52 }
53 }
|