1 16 package com.google.gwt.sample.kitchensink.client; 17 18 import com.google.gwt.sample.kitchensink.client.Sink.SinkInfo; 19 import com.google.gwt.user.client.DOM; 20 import com.google.gwt.user.client.Event; 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.Hyperlink; 24 import com.google.gwt.user.client.ui.Widget; 25 26 import java.util.ArrayList ; 27 28 32 public class SinkList extends Composite { 33 34 private class MouseLink extends Hyperlink { 35 36 private int index; 37 38 public MouseLink(String name, int index) { 39 super(name, name); 40 this.index = index; 41 sinkEvents(Event.MOUSEEVENTS); 42 } 43 44 public void onBrowserEvent(Event event) { 45 switch (DOM.eventGetType(event)) { 46 case Event.ONMOUSEOVER: 47 mouseOver(index); 48 break; 49 50 case Event.ONMOUSEOUT: 51 mouseOut(index); 52 break; 53 } 54 55 super.onBrowserEvent(event); 56 } 57 } 58 59 private HorizontalPanel list = new HorizontalPanel(); 60 private ArrayList sinks = new ArrayList (); 61 62 private int selectedSink = -1; 63 64 public SinkList(Sink.Images images) { 65 initWidget(list); 66 list.add(images.gwtLogo().createImage()); 67 setStyleName("ks-List"); 68 } 69 70 public void addSink(final SinkInfo info) { 71 String name = info.getName(); 72 int index = list.getWidgetCount() - 1; 73 74 MouseLink link = new MouseLink(name, index); 75 list.add(link); 76 sinks.add(info); 77 78 list.setCellVerticalAlignment(link, HorizontalPanel.ALIGN_BOTTOM); 79 styleSink(index, false); 80 } 81 82 public SinkInfo find(String sinkName) { 83 for (int i = 0; i < sinks.size(); ++i) { 84 SinkInfo info = (SinkInfo) sinks.get(i); 85 if (info.getName().equals(sinkName)) { 86 return info; 87 } 88 } 89 90 return null; 91 } 92 93 public void setSinkSelection(String name) { 94 if (selectedSink != -1) { 95 styleSink(selectedSink, false); 96 } 97 98 for (int i = 0; i < sinks.size(); ++i) { 99 SinkInfo info = (SinkInfo) sinks.get(i); 100 if (info.getName().equals(name)) { 101 selectedSink = i; 102 styleSink(selectedSink, true); 103 return; 104 } 105 } 106 } 107 108 private void colorSink(int index, boolean on) { 109 String color = ""; 110 if (on) { 111 color = ((SinkInfo) sinks.get(index)).getColor(); 112 } 113 114 Widget w = list.getWidget(index + 1); 115 DOM.setStyleAttribute(w.getElement(), "backgroundColor", color); 116 } 117 118 private void mouseOut(int index) { 119 if (index != selectedSink) { 120 colorSink(index, false); 121 } 122 } 123 124 private void mouseOver(int index) { 125 if (index != selectedSink) { 126 colorSink(index, true); 127 } 128 } 129 130 private void styleSink(int index, boolean selected) { 131 String style = (index == 0) ? "ks-FirstSinkItem" : "ks-SinkItem"; 132 if (selected) { 133 style += "-selected"; 134 } 135 136 Widget w = list.getWidget(index + 1); 137 w.setStyleName(style); 138 139 colorSink(index, selected); 140 } 141 } 142 | Popular Tags |