View Javadoc

1   package org.kuali.student.lum.lu.ui.tools.client.widgets;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.kuali.student.r1.common.assembly.data.Data;
7   import org.kuali.student.r1.common.assembly.data.LookupMetadata;
8   import org.kuali.student.r1.common.assembly.data.LookupParamMetadata;
9   import org.kuali.student.r1.common.assembly.data.Data.DataValue;
10  import org.kuali.student.r1.common.assembly.data.Data.Value;
11  import org.kuali.student.r1.common.search.dto.SearchParam;
12  import org.kuali.student.common.ui.client.mvc.Callback;
13  import org.kuali.student.common.ui.client.mvc.HasDataValue;
14  import org.kuali.student.common.ui.client.widgets.KSLabel;
15  import org.kuali.student.lum.common.client.widgets.CluSetRangeModelUtil;
16  import org.kuali.student.r2.core.search.dto.SearchParamInfo;
17  import org.kuali.student.r2.lum.clu.dto.MembershipQueryInfo;
18  
19  import com.google.gwt.event.dom.client.ClickEvent;
20  import com.google.gwt.event.dom.client.ClickHandler;
21  import com.google.gwt.user.client.ui.Composite;
22  import com.google.gwt.user.client.ui.HorizontalPanel;
23  import com.google.gwt.user.client.ui.SimplePanel;
24  
25  public class CluSetRangeLabel extends Composite implements HasDataValue {
26  
27      private MembershipQueryInfo membershipQueryInfo;
28      protected SimplePanel mainPanel = new SimplePanel();
29      private KSLabel label = new KSLabel();
30      private KSLabel delete = new KSLabel(" X ");
31      private LookupMetadata lookupMetadata;
32      private List<Callback<Value>> valueChangeCallbacks =
33          new ArrayList<Callback<Value>>();
34      private boolean initialized;
35  
36      public CluSetRangeLabel() {
37          super.initWidget(mainPanel);
38      }
39      
40      @Override
41      public Value getValue() {
42          DataValue value = new DataValue(
43                  CluSetRangeModelUtil.INSTANCE.toData(membershipQueryInfo));
44          return value;
45      }
46  
47      @Override
48      public void setValue(Value value) {
49          membershipQueryInfo = null;
50          if (value != null) {
51              Data d = value.get();
52              membershipQueryInfo = CluSetRangeModelUtil.INSTANCE.toMembershipQueryInfo(d);
53          }
54          callHandlers();
55          redraw();
56      }
57      
58      @Override
59      public void addValueChangeCallback(Callback<Value> callback) {
60          valueChangeCallbacks.add(callback);
61      }
62      
63      private void callHandlers() {
64          if (valueChangeCallbacks != null) {
65  //            MyGwtEvent myEvent = new MyGwtEvent(getValue());
66              for (Callback<Data.Value> handler : valueChangeCallbacks) {
67                  handler.exec(getValue());
68              }
69          }
70      }
71  
72      private void redraw() {
73          StringBuilder labelText = new StringBuilder();
74          int paramCounter = 0;
75          HorizontalPanel labelPanel = new HorizontalPanel();
76          labelPanel.add(label);
77          mainPanel.clear();
78          mainPanel.setWidget(labelPanel);
79          label.setText("");
80          if (!initialized) {
81              delete.addClickHandler(new ClickHandler() {
82                  @Override
83                  public void onClick(ClickEvent event) {
84                      membershipQueryInfo = null;
85                      label.setText("");
86                      callHandlers();
87                  }
88              });
89              initialized = true;
90          }
91          if (membershipQueryInfo != null && membershipQueryInfo.getQueryParamValues() != null && 
92                  !membershipQueryInfo.getQueryParamValues().isEmpty()) {
93              labelText.append(getLookupDisplayName()).append(": ");
94              for (SearchParamInfo searchParam : membershipQueryInfo.getQueryParamValues()) {
95                  if (paramCounter > 0) {
96                      labelText.append(" ");
97                  }
98                  labelText.append(getParameterDisplayName(searchParam.getKey())).append(" ");
99                  labelText.append(searchParam.getValues().get(0));
100                 paramCounter++;
101             }
102             label.setText(labelText.toString());
103             labelPanel.add(delete);
104         }
105     }
106     
107     private String getLookupDisplayName() {
108         String lookupDisplayName = null;
109         if (lookupMetadata == null) {
110             lookupDisplayName = membershipQueryInfo.getSearchTypeKey();
111         } else {
112             lookupDisplayName = lookupMetadata.getName();
113         }
114         return lookupDisplayName;
115     }
116     
117     private String getParameterDisplayName(String parameterKey) {
118         String parameterDisplayName = null;
119         if (lookupMetadata == null) {
120             parameterDisplayName = parameterKey;
121         } else {
122             List<LookupParamMetadata> searchParams = lookupMetadata.getParams();
123             if (searchParams != null) {
124                 for (LookupParamMetadata searchParam : searchParams) {
125                     if (nullSafeEquals(searchParam.getKey(), parameterKey)) {
126                         parameterDisplayName = searchParam.getName();
127                     }
128                 }
129             }
130             if (parameterDisplayName == null) {
131                 parameterDisplayName = parameterKey;
132             }
133         }
134         return parameterDisplayName;
135     }
136     
137     private boolean nullSafeEquals(Object str1, Object str2) {
138         return (str1 == null && str2 == null ||
139                 str1 != null && str2 != null && str1.equals(str2));
140     }
141     
142 //    public class MyGwtEvent extends ValueChangeEvent<Data.Value> {
143 //        public MyGwtEvent(Data.Value value) {
144 //            super(value);
145 //        }
146 //    }
147 
148     public LookupMetadata getLookupMetadata() {
149         return lookupMetadata;
150     }
151 
152     public void setLookupMetadata(LookupMetadata lookupMetadata) {
153         this.lookupMetadata = lookupMetadata;
154     }
155 
156 }