1 | |
package org.jtheque.core.managers.view.impl; |
2 | |
|
3 | |
import org.jtheque.core.managers.Managers; |
4 | |
import org.jtheque.core.managers.state.AbstractState; |
5 | |
import org.jtheque.core.managers.state.NodeState; |
6 | |
import org.jtheque.core.managers.state.NodeStateAttribute; |
7 | |
import org.jtheque.core.managers.view.able.IView; |
8 | |
import org.jtheque.core.managers.view.able.IViewManager; |
9 | |
|
10 | |
import java.util.ArrayList; |
11 | |
import java.util.Collection; |
12 | |
import java.util.HashMap; |
13 | |
import java.util.Map; |
14 | |
import java.util.Map.Entry; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public final class WindowsConfiguration extends AbstractState { |
38 | 0 | private final Map<String, WindowConfiguration> configurations = new HashMap<String, WindowConfiguration>(10); |
39 | |
|
40 | |
@Override |
41 | |
public boolean isDelegated() { |
42 | 0 | return true; |
43 | |
} |
44 | |
|
45 | |
@Override |
46 | |
public void delegateLoad(Collection<NodeState> nodes) { |
47 | 0 | for (NodeState node : nodes) { |
48 | 0 | if ("window".equals(node.getName())) { |
49 | 0 | WindowConfiguration configuration = new WindowConfiguration(); |
50 | |
|
51 | 0 | for (NodeState child : node.getChildrens()) { |
52 | 0 | applyValueFromChild(configuration, child); |
53 | |
} |
54 | |
|
55 | 0 | add(node.getAttributeValue("name"), configuration); |
56 | 0 | } |
57 | |
} |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
private static void applyValueFromChild(WindowConfiguration configuration, NodeState child) { |
67 | 0 | if ("width".equals(child.getName())) { |
68 | 0 | configuration.setWidth(Integer.parseInt(child.getText())); |
69 | 0 | } else if ("height".equals(child.getName())) { |
70 | 0 | configuration.setHeight(Integer.parseInt(child.getText())); |
71 | 0 | } else if ("posX".equals(child.getName())) { |
72 | 0 | configuration.setPositionX(Integer.parseInt(child.getText())); |
73 | 0 | } else if ("posY".equals(child.getName())) { |
74 | 0 | configuration.setPositionY(Integer.parseInt(child.getText())); |
75 | |
} |
76 | 0 | } |
77 | |
|
78 | |
@Override |
79 | |
public Collection<NodeState> delegateSave() { |
80 | 0 | Collection<NodeState> states = new ArrayList<NodeState>(10); |
81 | |
|
82 | 0 | for (Entry<String, WindowConfiguration> configuration : configurations.entrySet()) { |
83 | 0 | NodeState state = new NodeState("window"); |
84 | |
|
85 | 0 | state.getAttributes().add(new NodeStateAttribute("name", configuration.getKey())); |
86 | 0 | state.getChildrens().add(new NodeState("width", Integer.toString(configuration.getValue().getWidth()))); |
87 | 0 | state.getChildrens().add(new NodeState("height", Integer.toString(configuration.getValue().getHeight()))); |
88 | 0 | state.getChildrens().add(new NodeState("posX", Integer.toString(configuration.getValue().getPositionX()))); |
89 | 0 | state.getChildrens().add(new NodeState("posY", Integer.toString(configuration.getValue().getPositionY()))); |
90 | |
|
91 | 0 | states.add(state); |
92 | 0 | } |
93 | |
|
94 | 0 | return states; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
private void add(String name, WindowConfiguration configuration) { |
104 | 0 | configurations.put(name, configuration); |
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
public void update(String name, IView view) { |
114 | 0 | if (Managers.getCore().getConfiguration().retainSizeAndPositionOfWindow()) { |
115 | 0 | WindowConfiguration configuration = get(name); |
116 | |
|
117 | 0 | if (configuration != null) { |
118 | 0 | Managers.getManager(IViewManager.class).getViewDelegate().fill(configuration, view); |
119 | |
} |
120 | |
} |
121 | 0 | } |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
private WindowConfiguration get(String name) { |
130 | 0 | return configurations.get(name); |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public void configure(String name, IView view, int defaultWidth, int defaultHeight) { |
142 | 0 | if (Managers.getCore().getConfiguration().retainSizeAndPositionOfWindow()) { |
143 | 0 | WindowConfiguration configuration = get(name); |
144 | |
|
145 | 0 | if (configuration == null) { |
146 | 0 | configuration = new WindowConfiguration(); |
147 | |
|
148 | 0 | configuration.setWidth(defaultWidth); |
149 | 0 | configuration.setHeight(defaultHeight); |
150 | 0 | configuration.setPositionX(-1); |
151 | 0 | configuration.setPositionY(-1); |
152 | |
|
153 | 0 | add(name, configuration); |
154 | |
} |
155 | |
|
156 | 0 | Managers.getManager(IViewManager.class).getViewDelegate().configure(configuration, view); |
157 | 0 | } else { |
158 | 0 | Managers.getManager(IViewManager.class).getViewDelegate().setSize(view, defaultWidth, defaultHeight); |
159 | |
} |
160 | 0 | } |
161 | |
} |