Coverage Report - org.kuali.student.common.ui.client.util.Elements
 
Classes in this File Line Coverage Branch Coverage Complexity
Elements
0%
0/22
N/A
1.133
Elements$1
0%
0/5
N/A
1.133
Elements$1$1
0%
0/9
0%
0/2
1.133
Elements$2
0%
0/5
N/A
1.133
Elements$2$1
0%
0/9
0%
0/2
1.133
Elements$EmptyFadeCallback
0%
0/3
N/A
1.133
Elements$FadeCallback
N/A
N/A
1.133
 
 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.util;
 17  
 
 18  
 import com.google.gwt.dom.client.Element;
 19  
 import com.google.gwt.dom.client.Style;
 20  
 import com.google.gwt.user.client.Command;
 21  
 import com.google.gwt.user.client.DeferredCommand;
 22  
 import com.google.gwt.user.client.Timer;
 23  
 import com.google.gwt.user.client.ui.UIObject;
 24  
 
 25  
 /**
 26  
  * Utility class for DOM element manipulations, UIObject effects, etc.
 27  
  * 
 28  
  */
 29  0
 public class Elements {
 30  
 
 31  
         public interface FadeCallback {
 32  
                 public void onFadeComplete();
 33  
 
 34  
                 public void onFadeStart();
 35  
         }
 36  
 
 37  0
         public static class EmptyFadeCallback implements FadeCallback {
 38  0
         public void onFadeComplete() {}
 39  0
         public void onFadeStart() {}
 40  
         }
 41  
         /**
 42  
          * Causes an element to "fade in" via opacity. Make sure that element to
 43  
          * fade is not visible before calling this method. When the callback is
 44  
          * invoked with onFadeStart, the element can be set to visible as it is
 45  
          * entirely transparent. When onFadeComplete is called, the element will be
 46  
          * entirely opaque.
 47  
          * 
 48  
          * @param target
 49  
          *            the UIObject to be faded in
 50  
          * @param milliseconds
 51  
          *            how long the fade effect should take to process
 52  
          * @param maxOpacity
 53  
          *                           final opacity
 54  
          * @param callback
 55  
          *            the callback to be invoked on fade progress
 56  
          */
 57  
         public static void fadeIn(final UIObject target, final int milliseconds, final int maxOpacity,
 58  
                         final FadeCallback callback) {
 59  0
             fadeIn(target.getElement(), milliseconds, maxOpacity, callback);
 60  0
         }
 61  
         public static void fadeIn(final Element e, final int milliseconds, final int maxOpacity,
 62  
             final FadeCallback callback) {
 63  
 
 64  0
             final int interval = milliseconds / 50;
 65  0
                 setOpacity(e, 0);
 66  0
                 DeferredCommand.addCommand(new Command() {
 67  
 
 68  
                         @Override
 69  
                         public void execute() {
 70  0
                                 callback.onFadeStart();
 71  
 
 72  0
                                 final Timer t = new Timer() {
 73  0
                                         int pct = 0;
 74  
 
 75  
                                         @Override
 76  
                                         public void run() {
 77  0
                                                 pct += 2;
 78  0
                                                 pct = Math.min(pct, maxOpacity);
 79  0
                                                 setOpacity(e, pct);
 80  0
                                                 if (pct == maxOpacity) {
 81  0
                                                         this.cancel();
 82  0
                                                         callback.onFadeComplete();
 83  
                                                 }
 84  0
                                         }
 85  
                                 };
 86  0
                                 t.scheduleRepeating(interval);
 87  
 
 88  0
                         }
 89  
                 });
 90  0
         }
 91  
 
 92  
         /**
 93  
          * Causes an element to "fade out" via opacity. When onFadeComplete is
 94  
          * called, the element will be entirely transparent.
 95  
          * 
 96  
          * @param target
 97  
          *            the UIObject to be faded out
 98  
          * @param milliseconds
 99  
          *            how long the fade effect should take to process
 100  
          * @param minOpacity
 101  
          *                           final opacity
 102  
          * @param callback
 103  
          *            the callback to be invoked on fade progress
 104  
          */
 105  
         public static void fadeOut(final UIObject target, final int milliseconds, final int minOpacity, 
 106  
                         final FadeCallback callback) {
 107  0
             fadeOut(target.getElement(), milliseconds, minOpacity, callback);
 108  0
         }
 109  
         public static void fadeOut(final Element e, final int milliseconds, final int minOpacity, 
 110  
                final FadeCallback callback) {
 111  
 
 112  0
                 final int interval = milliseconds / 50;
 113  0
                 DeferredCommand.addCommand(new Command() {
 114  
 
 115  
                         @Override
 116  
                         public void execute() {
 117  0
                                 callback.onFadeStart();
 118  
 
 119  0
                                 final Timer t = new Timer() {
 120  0
                                         int pct = 100;
 121  
 
 122  
                                         @Override
 123  
                                         public void run() {
 124  0
                                                 pct -= 2;
 125  0
                                                 pct = Math.max(pct, minOpacity);
 126  0
                                                 setOpacity(e, pct);
 127  0
                                                 if (pct == minOpacity) {
 128  0
                                                         this.cancel();
 129  0
                                                         callback.onFadeComplete();
 130  
                                                 }
 131  0
                                         }
 132  
                                 };
 133  0
                                 t.scheduleRepeating(interval);
 134  
 
 135  0
                         }
 136  
                 });
 137  0
         }
 138  
 
 139  
         /**
 140  
      * Sets a UIObject's opacity
 141  
      * 
 142  
      * @param u
 143  
      * @param percent
 144  
      */
 145  
     public static void setOpacity(final UIObject u, final int percent) {
 146  0
         setOpacity(u.getElement(), percent);
 147  0
     }
 148  
     
 149  
         /**
 150  
          * Sets a DOM element's opacity
 151  
          * 
 152  
          * @param e
 153  
          * @param percent
 154  
          */
 155  
         public static void setOpacity(final Element e, final int percent) {
 156  0
                 final Style s = e.getStyle();
 157  0
                 final double d = ((double) percent / (double) 100);
 158  0
                 s.setProperty("opacity", String.valueOf(d));
 159  0
                 s.setProperty("MozOpacity", String.valueOf(d));
 160  0
                 s.setProperty("KhtmlOpacity", String.valueOf(d));
 161  0
                 s.setProperty("filter", "alpha(opacity=" + percent + ")");
 162  0
         }
 163  
 
 164  
         /**
 165  
          * Enables/disables text selection for the specified element.
 166  
          * 
 167  
          * @param e
 168  
          * @param selectable
 169  
          */
 170  
         public static native void setTextSelectable(Element e, boolean selectable)/*-{
 171  
                                                                                                                                                                 if (selectable) {
 172  
                                                                                                                                                                 e.ondrag = null;
 173  
                                                                                                                                                                 e.onselectstart = null;
 174  
                                                                                                                                                                 e.style.MozUserSelect="text";
 175  
                                                                                                                                                                 } else {
 176  
                                                                                                                                                                 e.ondrag = function () { return false; };
 177  
                                                                                                                                                                 e.onselectstart = function () { return false; };
 178  
                                                                                                                                                                 e.style.MozUserSelect="none";
 179  
                                                                                                                                                                 }
 180  
                                                                                                                                                                 }-*/;
 181  
 }