"Fossies" - the Fresh Open Source Software Archive 
Member "groovy-4.0.12/subprojects/groovy-swing/src/main/groovy/groovy/swing/factory/ActionFactory.groovy" (31 Jan 1980, 5162 Bytes) of package /linux/misc/apache-groovy-src-4.0.12.zip:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package groovy.swing.factory
20
21 import groovy.swing.impl.DefaultAction
22 import org.codehaus.groovy.runtime.InvokerHelper
23
24 import javax.swing.*
25
26 class ActionFactory extends AbstractFactory {
27
28 boolean isHandlesNodeChildren() {
29 return true
30 }
31
32 Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException {
33 Action action
34 if (FactoryBuilderSupport.checkValueIsTypeNotString(value, name, Action)) {
35 action = (Action) value
36 } else if (attributes.get(name) instanceof Action) {
37 action = (Action) attributes.remove(name)
38 } else {
39 action = new DefaultAction()
40 }
41 return action
42 }
43
44 boolean onHandleNodeAttributes( FactoryBuilderSupport builder, Object action, Map attributes) {
45 if ((attributes.get("closure") instanceof Closure) && (action instanceof DefaultAction)){
46 Closure closure = (Closure) attributes.remove("closure")
47 ((DefaultAction)action).setClosure(closure)
48 }
49
50 Object accel = attributes.remove("accelerator")
51 if (accel != null) {
52 KeyStroke stroke
53 if (accel instanceof KeyStroke) {
54 stroke = (KeyStroke) accel
55 } else {
56 stroke = KeyStroke.getKeyStroke(accel.toString())
57 }
58 action.putValue(Action.ACCELERATOR_KEY, stroke)
59 }
60
61 Object mnemonic = attributes.remove("mnemonic")
62 if (mnemonic != null) {
63 if (!(mnemonic instanceof Number)) {
64 mnemonic = mnemonic.toString().charAt(0)
65 }
66 action.putValue(Action.MNEMONIC_KEY, mnemonic as Integer)
67 }
68
69 for (entry in attributes.entrySet()) {
70 String propertyName = (String) entry.getKey()
71 // first attempt to set as a straight property
72 try {
73 InvokerHelper.setProperty(action, propertyName, entry.getValue())
74 } catch (MissingPropertyException mpe) {
75 // failing that store them in the action values list
76 // typically standard Action names start with upper case, so lets upper case it
77 propertyName = capitalize(propertyName)
78 action.putValue(propertyName, entry.getValue())
79 }
80
81 }
82
83 return false
84 }
85
86 boolean onNodeChildren(FactoryBuilderSupport builder, Object node, Closure childContent) {
87 if (!(node instanceof DefaultAction)) {
88 throw new RuntimeException("$builder.currentName only accepts a closure content when the action is generated by the node")
89 }
90 if (node.closure != null) {
91 throw new RuntimeException("$builder.currentName already has an action set via the closure attribute, child content as action not allowed")
92 }
93 node.closure = childContent
94 return false
95 }
96
97 void setParent(FactoryBuilderSupport builder, Object parent, Object action) {
98 try {
99 InvokerHelper.setProperty(parent, "action", action)
100 } catch (RuntimeException re) {
101 // must not have an action property...
102 // so we ignore it and go on
103 }
104 Object keyStroke = action.getValue("KeyStroke")
105 if (parent instanceof JComponent) {
106 JComponent component = (JComponent) parent
107 KeyStroke stroke = null
108 if (keyStroke instanceof GString) keyStroke = keyStroke as String
109 if (keyStroke instanceof String) {
110 stroke = KeyStroke.getKeyStroke((String) keyStroke)
111 } else if (keyStroke instanceof KeyStroke) {
112 stroke = (KeyStroke) keyStroke
113 }
114 if (stroke != null) {
115 String key = action.toString()
116 component.getInputMap().put(stroke, key)
117 component.getActionMap().put(key, action)
118 }
119 }
120 }
121
122
123 String capitalize(String text) {
124 char ch = text.charAt(0)
125 if (Character.isUpperCase(ch)) {
126 return text
127 }
128 StringBuffer buffer = new StringBuffer(text.length())
129 buffer.append(Character.toUpperCase(ch))
130 buffer.append(text, 1, text.length())
131 return buffer.toString()
132 }
133
134 }