1 | |
package org.jtheque.primary.controller.impl; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import org.jtheque.core.managers.Managers; |
20 | |
import org.jtheque.core.managers.view.able.IViewManager; |
21 | |
import org.jtheque.core.managers.view.able.components.TabComponent; |
22 | |
import org.jtheque.core.managers.view.able.controller.AbstractController; |
23 | |
import org.jtheque.primary.controller.able.ControllerState; |
24 | |
import org.jtheque.primary.controller.able.FormBean; |
25 | |
import org.jtheque.primary.controller.able.IPrincipalController; |
26 | |
import org.jtheque.primary.od.able.Data; |
27 | |
|
28 | |
import javax.swing.JTree; |
29 | |
import javax.swing.event.TreeSelectionEvent; |
30 | |
import javax.swing.tree.TreePath; |
31 | |
import java.util.Collection; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public abstract class PrincipalController<T extends Data> extends AbstractController implements IPrincipalController<T> { |
39 | |
private ControllerState state; |
40 | |
private final ControllerState viewState; |
41 | |
private final ControllerState modifyState; |
42 | |
private final ControllerState newObjectState; |
43 | |
private final ControllerState autoAddState; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
protected PrincipalController(ControllerState viewState, ControllerState modifyState, |
54 | |
ControllerState newObjectState, ControllerState autoAddState){ |
55 | 0 | super(); |
56 | |
|
57 | 0 | this.viewState = viewState; |
58 | 0 | this.modifyState = modifyState; |
59 | 0 | this.newObjectState = newObjectState; |
60 | 0 | this.autoAddState = autoAddState; |
61 | |
|
62 | 0 | state = viewState; |
63 | 0 | } |
64 | |
|
65 | |
@Override |
66 | |
public final void displayView(){ |
67 | 0 | Managers.getManager(IViewManager.class).getViews().setSelectedView((TabComponent) getView()); |
68 | |
|
69 | 0 | super.displayView(); |
70 | 0 | } |
71 | |
|
72 | |
@Override |
73 | |
public final void valueChanged(TreeSelectionEvent event){ |
74 | 0 | TreePath current = ((JTree) event.getSource()).getSelectionPath(); |
75 | |
|
76 | 0 | if (current != null && current.getLastPathComponent() instanceof Data){ |
77 | 0 | T data = (T) current.getLastPathComponent(); |
78 | |
|
79 | 0 | if (data != null){ |
80 | 0 | view(data); |
81 | |
} |
82 | |
} |
83 | 0 | } |
84 | |
|
85 | |
@Override |
86 | |
public final void save(FormBean formBean){ |
87 | 0 | ControllerState newState = state.save(formBean); |
88 | |
|
89 | 0 | if (newState != null){ |
90 | 0 | setAndApplyState(newState); |
91 | |
} |
92 | 0 | } |
93 | |
|
94 | |
@Override |
95 | |
public final void view(T data){ |
96 | 0 | ControllerState newState = state.view(data); |
97 | |
|
98 | 0 | if (newState != null){ |
99 | 0 | setAndApplyState(newState); |
100 | |
} |
101 | 0 | } |
102 | |
|
103 | |
@Override |
104 | |
public final void manualEdit(){ |
105 | 0 | ControllerState newState = state.manualEdit(); |
106 | |
|
107 | 0 | if (newState != null){ |
108 | 0 | setAndApplyState(newState); |
109 | |
} |
110 | 0 | } |
111 | |
|
112 | |
@Override |
113 | |
public final void create(){ |
114 | 0 | ControllerState newState = state.create(); |
115 | |
|
116 | 0 | if (newState != null){ |
117 | 0 | setAndApplyState(newState); |
118 | |
} |
119 | 0 | } |
120 | |
|
121 | |
@Override |
122 | |
public final void deleteCurrent(){ |
123 | 0 | ControllerState newState = state.delete(); |
124 | |
|
125 | 0 | if (newState != null){ |
126 | 0 | setAndApplyState(newState); |
127 | |
} |
128 | 0 | } |
129 | |
|
130 | |
@Override |
131 | |
public final void cancel(){ |
132 | 0 | ControllerState newState = state.cancel(); |
133 | |
|
134 | 0 | if (newState != null){ |
135 | 0 | setAndApplyState(newState); |
136 | |
} |
137 | 0 | } |
138 | |
|
139 | |
@Override |
140 | |
public final ControllerState getViewState(){ |
141 | 0 | return viewState; |
142 | |
} |
143 | |
|
144 | |
@Override |
145 | |
public final ControllerState getAutoAddState(){ |
146 | 0 | return autoAddState; |
147 | |
} |
148 | |
|
149 | |
@Override |
150 | |
public final ControllerState getModifyState(){ |
151 | 0 | return modifyState; |
152 | |
} |
153 | |
|
154 | |
@Override |
155 | |
public final ControllerState getNewObjectState(){ |
156 | 0 | return newObjectState; |
157 | |
} |
158 | |
|
159 | |
@Override |
160 | |
public Collection<T> getDisplayList(){ |
161 | 0 | return getViewModel().getDisplayList(); |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
protected final ControllerState getState(){ |
170 | 0 | return state; |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
protected final void setAndApplyState(ControllerState state){ |
179 | 0 | this.state = state; |
180 | |
|
181 | 0 | state.apply(); |
182 | 0 | } |
183 | |
} |