Permalink
Browse files

refactor swingbuilder

 - move all factory classes to classes of their own in a package of their own
 - expose needed methods for those factories to work
 - expose the registerFactory and registerBeanFactory methods so external beans can be registered.  (will this work Geertjan?)

git-svn-id: http://svn.codehaus.org/groovy/trunk/groovy/groovy-core@6099 a5544e8c-8a19-0410-ba12-f9af4593a198
  • Loading branch information...
1 parent 8ebe8f3 commit 3a9204bca97929a8282b4ebfe6e1f9b47a432926 @shemnon shemnon committed Apr 21, 2007

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,88 @@
+/*
+ * $Id: $
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package groovy.swing.factory;
+
+import groovy.lang.Closure;
+import groovy.lang.MissingPropertyException;
+import groovy.swing.SwingBuilder;
+import groovy.swing.impl.DefaultAction;
+import java.util.Iterator;
+import java.util.Map;
+import javax.swing.Action;
+import javax.swing.KeyStroke;
+import org.codehaus.groovy.runtime.InvokerHelper;
+
+/**
+ *
+ * @author Danno Ferrin
+ */
+public class ActionFactory implements Factory {
+
+ public Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
+ Action action = null;
+ if (value instanceof Action) {
+ action = (Action) value;
+ } else if (properties.get(name) instanceof Action) {
+ action = (Action) properties.remove(name);
+ } else {
+ action = new DefaultAction();
+ }
+
+ if ((properties.get("closure") instanceof Closure) && (action instanceof DefaultAction)){
+ Closure closure = (Closure) properties.remove("closure");
+ ((DefaultAction)action).setClosure(closure);
+ }
+
+ Object accel = properties.remove("accelerator");
+ if (accel != null) {
+ KeyStroke stroke = null;
+ if (accel instanceof KeyStroke) {
+ stroke = (KeyStroke) accel;
+ } else {
+ stroke = KeyStroke.getKeyStroke(accel.toString());
+ }
+ action.putValue(Action.ACCELERATOR_KEY, stroke);
+ }
+
+ Object mnemonic = properties.remove("mnemonic");
+ if (mnemonic != null) {
+ if (!(mnemonic instanceof Number)) {
+ mnemonic = new Integer(mnemonic.toString().charAt(0));
+ }
+ action.putValue(Action.MNEMONIC_KEY, mnemonic);
+ }
+
+ for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) {
+ Map.Entry entry = (Map.Entry) iter.next();
+ String propertyName = (String) entry.getKey();
+ // first attempt to set as a straight proeprty
+ try {
+ InvokerHelper.setProperty(action, propertyName, entry.getValue());
+ } catch (MissingPropertyException mpe) {
+ // failing that store them in the action values list
+ // typically standard Action names start with upper case, so lets upper case it
+ propertyName = SwingBuilder.capitalize(propertyName);
+ action.putValue(propertyName, entry.getValue());
+ }
+
+ }
+ properties.clear();
+
+ return action;
+ }
+}
@@ -0,0 +1,124 @@
+/*
+ * $Id: $
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package groovy.swing.factory;
+
+import groovy.swing.SwingBuilder;
+import java.awt.Dimension;
+import java.util.Map;
+import javax.swing.Box;
+import javax.swing.BoxLayout;
+
+/**
+ *
+ * @author Danno Ferrin
+ */
+public class BoxFactory implements Factory {
+
+ public Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
+ int axis = BoxLayout.X_AXIS; // default to X so it behaves like FlowLayout
+ if (properties.containsKey("axis")) {
+ Object o = properties.remove("axis");
+ if (o instanceof Number) {
+ axis = ((Number)o).intValue();
+ }
+ }
+ return new Box(axis);
+ }
+
+
+ public static class HBoxFactory implements Factory {
+ public Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
+ return Box.createHorizontalBox();
+ }
+ }
+
+ public static class HGlueFactory implements Factory {
+ public Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
+ return Box.createHorizontalGlue();
+ }
+ }
+
+ public static class HStrutFactory implements Factory {
+ public Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
+ Object num;
+ if (value != null) {
+ num = value;
+ } else {
+ num = properties.remove("width");
+ }
+ if (num instanceof Number) {
+ return Box.createHorizontalStrut(((Number) num).intValue());
+ } else {
+ return Box.createHorizontalStrut(6);
+ }
+ }
+ }
+
+ public static class VBoxFactory implements Factory {
+ public Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
+ return Box.createVerticalBox();
+ }
+ }
+
+ public static class VGlueFactory implements Factory {
+ public Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
+ return Box.createVerticalGlue();
+ }
+ }
+
+ public static class VStrutFactory implements Factory {
+ public Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
+ Object num;
+ if (value != null) {
+ num = value;
+ } else {
+ num = properties.remove("height");
+ }
+ if (num instanceof Number) {
+ return Box.createVerticalStrut(((Number) num).intValue());
+ } else {
+ return Box.createVerticalStrut(6);
+ }
+ }
+ }
+
+ public static class GlueFactory implements Factory {
+ public Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
+ return Box.createGlue();
+ }
+ }
+
+ public static class RigidAreaFactory implements Factory {
+ public Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
+ Dimension dim;
+ Object o = properties.remove("size");
+ if (o instanceof Dimension) {
+ dim = (Dimension) o;
+ } else {
+ int w, h;
+ o = properties.remove("width");
+ w = ((o instanceof Number)) ? ((Number) o).intValue() : 6;
+ o = properties.remove("height");
+ h = ((o instanceof Number)) ? ((Number) o).intValue() : 6;
+ dim = new Dimension(w, h);
+ }
+ return Box.createRigidArea(dim);
+ }
+ }
+
+}
@@ -0,0 +1,53 @@
+/*
+ * $Id: $
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package groovy.swing.factory;
+
+import groovy.swing.SwingBuilder;
+import java.awt.Container;
+import java.util.Map;
+import javax.swing.BoxLayout;
+import org.codehaus.groovy.runtime.InvokerHelper;
+
+/**
+ *
+ * @author Danno Ferrin
+ */
+public class BoxLayoutFactory implements Factory {
+
+ public Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
+ Object parent = builder.getCurrent();
+ if (parent instanceof Container) {
+ Object axisObject = properties.remove("axis");
+ int axis = BoxLayout.X_AXIS;
+ if (axisObject != null) {
+ Integer i = (Integer) axisObject;
+ axis = i.intValue();
+ }
+
+ Container target = SwingBuilder.getLayoutTarget(parent);
+ BoxLayout answer = new BoxLayout(target, axis);
+
+ // now let's try to set the layout property
+ InvokerHelper.setProperty(target, "layout", answer);
+ return answer;
+ } else {
+ throw new RuntimeException("Must be nested inside a Container");
+ }
+ }
+
+}
@@ -0,0 +1,46 @@
+/*
+ * $Id: $
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package groovy.swing.factory;
+
+import groovy.swing.SwingBuilder;
+import java.util.List;
+import java.util.Map;
+import java.util.Vector;
+import javax.swing.JComboBox;
+
+/**
+ *
+ * @author Danno Ferrin
+ */
+public class ComboBoxFactory implements Factory {
+
+ public Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
+ Object items = properties.remove("items");
+ if (items instanceof Vector) {
+ return new JComboBox((Vector) items);
+ } else if (items instanceof List) {
+ List list = (List) items;
+ return new JComboBox(list.toArray());
+ } else if (items instanceof Object[]) {
+ return new JComboBox((Object[]) items);
+ } else {
+ return new JComboBox();
+ }
+ }
+
+}
@@ -0,0 +1,52 @@
+/*
+ * $Id: $
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package groovy.swing.factory;
+
+import groovy.swing.SwingBuilder;
+import java.awt.Dialog;
+import java.awt.Frame;
+import java.util.LinkedList;
+import java.util.Map;
+import javax.swing.JDialog;
+
+/**
+ *
+ * @author Danno Ferrin
+ */
+public class DialogFactory implements Factory {
+
+ public Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException {
+ JDialog dialog;
+ Object owner = properties.remove("owner");
+ LinkedList containingWindows = builder.getContainingWindows();
+ // if owner not explicit, use the last window type in the list
+ if ((owner == null) && !containingWindows.isEmpty()) {
+ owner = containingWindows.getLast();
+ }
+ if (owner instanceof Frame) {
+ dialog = new JDialog((Frame) owner);
+ } else if (owner instanceof Dialog) {
+ dialog = new JDialog((Dialog) owner);
+ } else {
+ dialog = new JDialog();
+ }
+ containingWindows.add(dialog);
+ return dialog;
+ }
+
+}
@@ -43,8 +43,9 @@
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-package groovy.swing.impl;
+package groovy.swing.factory;
+import groovy.swing.SwingBuilder;
import java.util.Map;
/**
@@ -58,5 +59,5 @@
/**
* Create a new instance
*/
- Object newInstance(Map properties) throws InstantiationException, IllegalAccessException;
+ Object newInstance(SwingBuilder builder, Object name, Object value, Map properties) throws InstantiationException, IllegalAccessException;
}
Oops, something went wrong.

0 comments on commit 3a9204b

Please sign in to comment.