View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ui.client.widgets.table;
17  
18  import org.kuali.student.common.ui.client.widgets.rules.Token;
19  
20  import com.google.gwt.core.client.GWT;
21  import com.google.gwt.dom.client.EventTarget;
22  import com.google.gwt.dom.client.NativeEvent;
23  import com.google.gwt.event.logical.shared.ValueChangeEvent;
24  import com.google.gwt.event.logical.shared.ValueChangeHandler;
25  import com.google.gwt.event.shared.HandlerRegistration;
26  import com.google.gwt.user.client.DOM;
27  import com.google.gwt.user.client.Element;
28  import com.google.gwt.user.client.Event;
29  import com.google.gwt.user.client.Event.NativePreviewEvent;
30  import com.google.gwt.user.client.Event.NativePreviewHandler;
31  import com.google.gwt.user.client.ui.CheckBox;
32  import com.google.gwt.user.client.ui.HTML;
33  import com.google.gwt.user.client.ui.HasVerticalAlignment;
34  import com.google.gwt.user.client.ui.SimplePanel;
35  import com.google.gwt.user.client.ui.VerticalPanel;
36  
37  public class NodeWidget extends SimplePanel implements NativePreviewHandler {
38      private Node node;
39      HTML html = new HTML();
40      CheckBox checkBox = new CheckBox();
41      HandlerRegistration handlerRegistration;
42      VerticalPanel verticalPanel = new VerticalPanel();
43      public NodeWidget(Node<Token> n) {
44          Event.addNativePreviewHandler(this);
45          
46          node = n;
47          super.setWidth("100%");
48          super.setHeight("100%");
49          setNode(n);
50  
51  //        checkBox.addBlurHandler(new BlurHandler() {
52    //          @Override
53      //        public void onBlur(BlurEvent event) {
54        //          DOM.setStyleAttribute(NodeWidget.this.getElement(), "background", "#ffffff");
55          //    }
56  //        });
57    //      checkBox.addFocusHandler(new FocusHandler() {
58      //        @Override
59        //      public void onFocus(FocusEvent event) {
60          //        DOM.setStyleAttribute(NodeWidget.this.getElement(), "background", "#ffeeff");
61            //  }
62  //        });
63         
64    //      super.addBlurHandler(new BlurHandler() {
65      //        @Override
66        //      public void onBlur(BlurEvent event) {
67          //        DOM.setStyleAttribute(NodeWidget.this.getElement(), "background", "#ffffff");
68            //  }
69  //        });
70    //      super.addFocusHandler(new FocusHandler() {
71      //        @Override
72        //      public void onFocus(FocusEvent event) {
73          //        DOM.setStyleAttribute(NodeWidget.this.getElement(), "background", "#ffeeff");
74            //  }
75  //        });
76  //        super.addClickHandler(new ClickHandler() {
77    //          @Override
78      //        public void onClick(ClickEvent event) {
79        
80  //                boolean before = checkBox.getValue();
81    //              checkBox.setValue(!before);
82      //            ValueChangeEvent.fireIfNotEqual(checkBox, before,checkBox.getValue());
83        //          checkBox.setFocus(true);
84          //        setFocus(true);
85           //   }
86         // });
87          checkBox.addValueChangeHandler(new ValueChangeHandler(){
88              @Override
89              public void onValueChange(ValueChangeEvent event) {
90                  if(checkBox.getValue() == true){
91                      DOM.setStyleAttribute(NodeWidget.this.getElement(), "background", "#ffeeff");
92                  }else{
93                      DOM.setStyleAttribute(NodeWidget.this.getElement(), "background", "#ffffff");
94                  }
95              }
96              
97          });
98      }
99      @Override
100     public void onPreviewNativeEvent(NativePreviewEvent pevent) {
101         NativeEvent event = pevent.getNativeEvent();
102         EventTarget target = event.getEventTarget();
103      //   System.out.println(this.getElement().is(Element.as(target)));
104         if(checkBox.getElement().isOrHasChild(Element.as(target))){
105             return;
106         }else if(this.getElement().is(Element.as(target)) && 
107                 Event.as(event).getTypeInt() == Event.ONMOUSEDOWN ){
108                     GWT.log("doing", null);
109                   boolean before = checkBox.getValue();
110                                   checkBox.setValue(!before);
111                                   ValueChangeEvent.fireIfNotEqual(checkBox, before,checkBox.getValue());
112                                   //checkBox.setFocus(true);
113                           //        setFocus(true);
114                     
115         }
116 
117     }
118     public Node getNode() {
119         return node;
120     }
121 
122     public void installCheckBox() {
123         verticalPanel.clear();
124         verticalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
125         verticalPanel.setWidth("100%");
126         verticalPanel.setHeight("100%");
127         verticalPanel.add(checkBox);
128         super.setWidget(verticalPanel);
129         //contentPanel.setWidget(checkBox);
130     }
131 
132     public boolean isSelected() {
133         return checkBox.getValue() == true;
134     }
135 
136     public void addSelectionHandler(ValueChangeHandler<Boolean> ch) {
137         if (handlerRegistration != null) {
138             handlerRegistration.removeHandler();
139         }
140         handlerRegistration = checkBox.addValueChangeHandler(ch);
141     }
142 
143     public void setNode(Node n) {
144         node = n;
145         html.setHTML(node.getUserObject().toString());
146         checkBox.setHTML(node.getUserObject().toString());
147     }
148 }