1 | |
package org.kuali.student.common.ui.client.configurable.mvc.sections; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.List; |
5 | |
|
6 | |
import org.kuali.student.common.assembly.data.MetadataInterrogator; |
7 | |
import org.kuali.student.common.ui.client.application.Application; |
8 | |
import org.kuali.student.common.ui.client.application.ApplicationContext; |
9 | |
import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor; |
10 | |
import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle; |
11 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
12 | |
import org.kuali.student.common.ui.client.widgets.KSLabel; |
13 | |
import org.kuali.student.common.ui.client.widgets.field.layout.layouts.FieldLayout; |
14 | |
import org.kuali.student.common.ui.client.widgets.field.layout.layouts.VerticalFieldLayout; |
15 | |
|
16 | |
import com.google.gwt.event.dom.client.ClickEvent; |
17 | |
import com.google.gwt.event.dom.client.ClickHandler; |
18 | |
import com.google.gwt.user.client.ui.Anchor; |
19 | |
import com.google.gwt.user.client.ui.Composite; |
20 | |
import com.google.gwt.user.client.ui.FlowPanel; |
21 | |
import com.google.gwt.user.client.ui.Widget; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 0 | public class RequiredContainer extends WarnContainer { |
29 | |
|
30 | 0 | final static ApplicationContext context = Application.getApplicationContext(); |
31 | |
|
32 | 0 | private boolean showAll = false; |
33 | |
private Section mainSection; |
34 | |
|
35 | |
private KSLabel label; |
36 | |
private Anchor link; |
37 | |
|
38 | 0 | private List<Callback<Boolean>> callbacks = new ArrayList<Callback<Boolean>>(); |
39 | 0 | private List<FieldDescriptor> excludedDescriptors = new ArrayList<FieldDescriptor>(); |
40 | |
|
41 | |
public RequiredContainer() { |
42 | 0 | super(); |
43 | 0 | label = new KSLabel(context.getMessage("showingRequiredFields")); |
44 | 0 | link = new Anchor(context.getMessage("showAllFields")); |
45 | |
|
46 | 0 | this.showWarningLayout(true); |
47 | 0 | this.addHandler(); |
48 | |
|
49 | 0 | this.addWarnWidget(label); |
50 | 0 | this.addWarnWidget(link); |
51 | 0 | } |
52 | |
|
53 | |
private void addHandler() { |
54 | |
|
55 | 0 | link.addClickHandler(new ClickHandler() { |
56 | |
|
57 | |
@Override |
58 | |
public void onClick(ClickEvent event) { |
59 | 0 | if (showAll) { |
60 | 0 | label.setText(context.getMessage("showingRequiredFields")); |
61 | 0 | link.setText(context.getMessage("showAllFields")); |
62 | 0 | showAll = false; |
63 | |
} else { |
64 | 0 | label.setText(context.getMessage("showingAllFields")); |
65 | 0 | link.setText(context.getMessage("showRequiredFields")); |
66 | 0 | showAll = true; |
67 | |
} |
68 | 0 | processMainSection(); |
69 | 0 | } |
70 | |
}); |
71 | 0 | } |
72 | |
|
73 | |
public boolean isShowAll() { |
74 | 0 | return showAll; |
75 | |
} |
76 | |
|
77 | |
public void setShowAll(boolean showAll) { |
78 | 0 | this.showAll = showAll; |
79 | 0 | } |
80 | |
|
81 | |
public Section getMainSection() { |
82 | 0 | return mainSection; |
83 | |
} |
84 | |
|
85 | |
public void setMainSection(Section mainSection) { |
86 | 0 | this.mainSection = mainSection; |
87 | 0 | this.processMainSection(); |
88 | 0 | } |
89 | |
|
90 | |
public void addCallback(Callback<Boolean> callback) { |
91 | 0 | callbacks.add(callback); |
92 | 0 | } |
93 | |
|
94 | |
public List<Callback<Boolean>> getCallbacks() { |
95 | 0 | return callbacks; |
96 | |
} |
97 | |
|
98 | |
public void registerExclusion(FieldDescriptor descriptor) { |
99 | 0 | excludedDescriptors.add(descriptor); |
100 | 0 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public void processMainSection() { |
107 | |
|
108 | 0 | for (Section innerSection : mainSection.getSections()) { |
109 | 0 | boolean hasComponents = processInnerSection(innerSection, showAll); |
110 | 0 | setLinkVisibility(innerSection, !hasComponents); |
111 | 0 | } |
112 | |
|
113 | 0 | for (Callback<Boolean> c : callbacks) { |
114 | 0 | c.exec(showAll); |
115 | |
} |
116 | |
|
117 | 0 | } |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public boolean processInnerSection(Section section, boolean showAll) { |
127 | 0 | boolean hasComponents = false; |
128 | 0 | for (FieldDescriptor field : section.getUnnestedFields()) { |
129 | 0 | if (processFieldDescriptor(field, showAll)) { |
130 | 0 | hasComponents = true; |
131 | |
} |
132 | |
} |
133 | |
|
134 | 0 | for (Section innerSection : section.getSections()) { |
135 | 0 | if (processInnerSection(innerSection, showAll)) { |
136 | 0 | hasComponents = true; |
137 | 0 | setSectionVisibility(innerSection, true); |
138 | |
} else { |
139 | 0 | setSectionVisibility(innerSection, false); |
140 | |
} |
141 | |
} |
142 | |
|
143 | 0 | return hasComponents; |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
private void setLinkVisibility(Section section, boolean visibility) { |
153 | 0 | if (section instanceof VerticalSection) { |
154 | 0 | if (((VerticalSection) section).getShowAllLink() != null) { |
155 | 0 | ((VerticalSection) section).getShowAllLink().setVisible(visibility); |
156 | |
} |
157 | |
} |
158 | 0 | } |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
private void setSectionVisibility(Section section, boolean visibility) { |
168 | 0 | if (section instanceof VerticalSection) { |
169 | 0 | FieldLayout layout = ((BaseSection) section).getLayout(); |
170 | 0 | if (layout instanceof VerticalFieldLayout){ |
171 | 0 | FlowPanel flowPanel = ((VerticalFieldLayout)layout).getVerticalLayout(); |
172 | 0 | flowPanel.setVisible(visibility); |
173 | 0 | } else { |
174 | 0 | SectionTitle sectionTitle = layout.getLayoutTitle(); |
175 | 0 | if (sectionTitle != null){ |
176 | 0 | sectionTitle.setVisible(visibility); |
177 | |
} |
178 | |
} |
179 | |
} |
180 | 0 | if(section instanceof CollapsableSection) |
181 | 0 | ((CollapsableSection) section).getLayout().setVisible(visibility); |
182 | 0 | } |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
private boolean processFieldDescriptor(FieldDescriptor descriptor, boolean showAll) { |
193 | |
|
194 | 0 | if (descriptor.isIgnoreShowRequired()) { |
195 | 0 | return true; |
196 | |
} |
197 | |
|
198 | 0 | if (!MetadataInterrogator.isRequired(descriptor.getMetadata()) && (!MetadataInterrogator.isRequiredForNextState(descriptor.getMetadata()))) { |
199 | 0 | descriptor.getFieldElement().setVisible(showAll); |
200 | 0 | return showAll; |
201 | |
} |
202 | 0 | return true; |
203 | |
} |
204 | |
|
205 | |
public Composite createShowAllLink(ClickHandler handler) { |
206 | 0 | return new ShowAllLink(handler); |
207 | |
} |
208 | |
|
209 | |
public class ShowAllLink extends Composite { |
210 | 0 | private FlowPanel layout = new FlowPanel(); |
211 | |
|
212 | 0 | public ShowAllLink(ClickHandler handler) { |
213 | 0 | layout.addStyleName("ks-message-static-margin"); |
214 | 0 | add(new KSLabel(context.getMessage("requiredFields"))); |
215 | |
|
216 | |
|
217 | 0 | Anchor link = new Anchor(context.getMessage("allFields")); |
218 | 0 | link.addClickHandler(handler); |
219 | 0 | add(link); |
220 | |
|
221 | 0 | this.initWidget(layout); |
222 | 0 | } |
223 | |
|
224 | |
public void add(Widget w) { |
225 | 0 | layout.add(w); |
226 | 0 | w.getElement().setAttribute("style", "display: inline"); |
227 | 0 | } |
228 | |
} |
229 | |
} |