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