001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.sample.kitchensink.client;
017:
018: import com.google.gwt.sample.kitchensink.client.Sink.SinkInfo;
019: import com.google.gwt.user.client.DOM;
020: import com.google.gwt.user.client.Event;
021: import com.google.gwt.user.client.ui.Composite;
022: import com.google.gwt.user.client.ui.HorizontalPanel;
023: import com.google.gwt.user.client.ui.Hyperlink;
024: import com.google.gwt.user.client.ui.Widget;
025:
026: import java.util.ArrayList;
027:
028: /**
029: * The left panel that contains all of the sinks, along with a short description
030: * of each.
031: */
032: public class SinkList extends Composite {
033:
034: private class MouseLink extends Hyperlink {
035:
036: private int index;
037:
038: public MouseLink(String name, int index) {
039: super (name, name);
040: this .index = index;
041: sinkEvents(Event.MOUSEEVENTS);
042: }
043:
044: @Override
045: public void onBrowserEvent(Event event) {
046: switch (DOM.eventGetType(event)) {
047: case Event.ONMOUSEOVER:
048: mouseOver(index);
049: break;
050:
051: case Event.ONMOUSEOUT:
052: mouseOut(index);
053: break;
054: }
055:
056: super .onBrowserEvent(event);
057: }
058: }
059:
060: private HorizontalPanel list = new HorizontalPanel();
061: private ArrayList<SinkInfo> sinks = new ArrayList<SinkInfo>();
062:
063: private int selectedSink = -1;
064:
065: public SinkList(Sink.Images images) {
066: initWidget(list);
067: list.add(images.gwtLogo().createImage());
068: setStyleName("ks-List");
069: }
070:
071: public void addSink(final SinkInfo info) {
072: String name = info.getName();
073: int index = list.getWidgetCount() - 1;
074:
075: MouseLink link = new MouseLink(name, index);
076: list.add(link);
077: sinks.add(info);
078:
079: list.setCellVerticalAlignment(link,
080: HorizontalPanel.ALIGN_BOTTOM);
081: styleSink(index, false);
082: }
083:
084: public SinkInfo find(String sinkName) {
085: for (int i = 0; i < sinks.size(); ++i) {
086: SinkInfo info = sinks.get(i);
087: if (info.getName().equals(sinkName)) {
088: return info;
089: }
090: }
091:
092: return null;
093: }
094:
095: public void setSinkSelection(String name) {
096: if (selectedSink != -1) {
097: styleSink(selectedSink, false);
098: }
099:
100: for (int i = 0; i < sinks.size(); ++i) {
101: SinkInfo info = sinks.get(i);
102: if (info.getName().equals(name)) {
103: selectedSink = i;
104: styleSink(selectedSink, true);
105: return;
106: }
107: }
108: }
109:
110: private void colorSink(int index, boolean on) {
111: String color = "";
112: if (on) {
113: color = sinks.get(index).getColor();
114: }
115:
116: Widget w = list.getWidget(index + 1);
117: DOM.setStyleAttribute(w.getElement(), "backgroundColor", color);
118: }
119:
120: private void mouseOut(int index) {
121: if (index != selectedSink) {
122: colorSink(index, false);
123: }
124: }
125:
126: private void mouseOver(int index) {
127: if (index != selectedSink) {
128: colorSink(index, true);
129: }
130: }
131:
132: private void styleSink(int index, boolean selected) {
133: String style = (index == 0) ? "ks-FirstSinkItem"
134: : "ks-SinkItem";
135: if (selected) {
136: style += "-selected";
137: }
138:
139: Widget w = list.getWidget(index + 1);
140: w.setStyleName(style);
141:
142: colorSink(index, selected);
143: }
144: }
|