1 /*
2 Copyright 2002-2006 Martin van den Bemt
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
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 org.xulux.guilayer.swing.core;
17
18 import java.awt.Component;
19
20 import javax.swing.JComponent;
21
22 import org.xulux.api.gui.INativeWidgetHandler;
23 import org.xulux.api.gui.IWidget;
24 import org.xulux.api.logging.ILog;
25 import org.xulux.core.XuluxContext;
26 import org.xulux.utils.ClassLoaderUtils;
27
28 /**
29 * The native widgets handler for swing.
30 *
31 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
32 * @version $Id: NativeWidgetHandler.java,v 1.3 2004/03/31 09:37:59 mvdb Exp $
33 */
34 public class SwingNativeWidgetHandler implements INativeWidgetHandler {
35
36 /**
37 *
38 */
39 public SwingNativeWidgetHandler() {
40 }
41
42 /**
43 * Adds the specified object (if it is an instanceof JComponent)
44 * to the specified parent.
45 *
46 * @see org.xulux.api.gui.INativeWidgetHandler#getWidget(java.lang.Object, org.xulux.api.gui.IWidget)
47 */
48 public IWidget getWidget(Object nativeWidget, IWidget parent) {
49 if (nativeWidget != null && nativeWidget instanceof JComponent) {
50 if (parent.canContainChildren()) {
51 Object nativeParent = parent.getNativeWidget();
52 if (nativeParent instanceof JComponent) {
53 ((JComponent) nativeParent).add((JComponent) nativeWidget);
54 return parent;
55 }
56 }
57 } else {
58 XuluxContext.getLogger().log(ILog.WARN, "NativeWidgetHandler", "Native widget cannot be added, since it is not of type JComponent or null");
59 }
60 return null;
61 }
62
63 /**
64 * Adds a JComponent to the widget specified as parent
65 *
66 * @see org.xulux.api.gui.INativeWidgetHandler#getWidget(java.lang.String, org.xulux.api.gui.IWidget)
67 */
68 public IWidget getWidget(String clazz, IWidget parent) {
69 Object nativeWidget = ClassLoaderUtils.getObjectFromClassString(clazz);
70 return getWidget(nativeWidget, parent);
71 }
72
73 /**
74 * @see org.xulux.api.gui.INativeWidgetHandler#setLocationOnWidget(org.xulux.api.gui.IWidget, int, int)
75 */
76 public void setLocationOnWidget(IWidget parent, int x, int y) {
77 if (parent != null && parent.getNativeWidget() != null) {
78 JComponent comp = (JComponent) parent.getNativeWidget();
79 // set the location on the last component added..
80 if (comp.getComponentCount() > 0) {
81 Component childComp = comp.getComponent(comp.getComponentCount() - 1);
82 setLocationOnWidget(childComp, x, y);
83 }
84 }
85 }
86
87 /**
88 * @see org.xulux.api.gui.INativeWidgetHandler#setLocationOnWidget(java.lang.Object, int, int)
89 */
90 public void setLocationOnWidget(Object widget, int x, int y) {
91 if (!(widget instanceof Component)) {
92 return;
93 }
94 Component comp = (Component) widget;
95 comp.setLocation(x, y);
96 }
97
98 /**
99 * @see org.xulux.api.gui.INativeWidgetHandler#addWidgetToParent(org.xulux.api.gui.IWidget, java.lang.Object)
100 */
101 public void addWidgetToParent(IWidget widget, Object parentWidget) {
102 if (!(parentWidget instanceof JComponent) || widget == null) {
103 return;
104 }
105 JComponent comp = (JComponent) parentWidget;
106 Object nativeWidget = widget.getNativeWidget();
107 if (nativeWidget instanceof Component) {
108 comp.add((Component) widget.getNativeWidget(), widget);
109 }
110 }
111
112 /**
113 * @see org.xulux.api.gui.INativeWidgetHandler#refresh(java.lang.Object)
114 */
115 public void refresh(Object widget) {
116 Component component = (Component) widget;
117 component.setVisible(false);
118 component.setVisible(true);
119 }
120 }