1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.guilayer.swing.natives;
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
30
31
32
33
34 public class NativeWindowHandler implements INativeWidgetHandler {
35
36
37
38
39 public NativeWindowHandler() {
40 }
41
42
43
44
45
46
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
65
66
67
68 public IWidget getWidget(String clazz, IWidget parent) {
69 Object nativeWidget = ClassLoaderUtils.getObjectFromClassString(clazz);
70 return getWidget(nativeWidget, parent);
71 }
72
73
74
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
80 if (comp.getComponentCount() > 0) {
81 Component childComp = comp.getComponent(comp.getComponentCount() - 1);
82 setLocationOnWidget(childComp, x, y);
83 }
84 }
85 }
86
87
88
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
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
114
115 public void refresh(Object widget) {
116 Component component = (Component) widget;
117 component.setVisible(false);
118 component.setVisible(true);
119 }
120 }