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