KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > sample > kitchensink > client > SinkList


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

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 JavaDoc;
27
28 /**
29  * The left panel that contains all of the sinks, along with a short description
30  * of each.
31  */

32 public class SinkList extends Composite {
33
34   private class MouseLink extends Hyperlink {
35
36     private int index;
37
38     public MouseLink(String JavaDoc 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 JavaDoc sinks = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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