001    /*
002     * Copyright 2006 Mat Gessel <mat.gessel@gmail.com>
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 asquare.gwt.tk.client.ui;
017    
018    import java.util.ArrayList;
019    import java.util.Iterator;
020    
021    import asquare.gwt.tk.client.ui.commands.SelectAllCommand;
022    
023    import com.google.gwt.user.client.DeferredCommand;
024    import com.google.gwt.user.client.ui.*;
025    
026    /**
027     * A URL text box similar to that of a browser location bar. 
028     * 
029     * <p><em>This widget is a work in progress and therefore is subject to change. </em></p>
030     */
031    public class UrlLocation extends TextBox implements FocusListener, KeyboardListener
032    {
033            private ArrayList m_listeners = new ArrayList();
034            
035            public UrlLocation()
036            {
037                    addFocusListener(this);
038                    addKeyboardListener(this);
039            }
040            
041            public void addListener(URLListener listener)
042            {
043                    m_listeners.add(listener);
044            }
045            
046            public void removeListener(URLListener listener)
047            {
048                    m_listeners.remove(listener);
049            }
050            
051            public String getURL()
052            {
053                    String url = getText();
054                    if (url.indexOf("://") == -1)
055                    {
056                            url = "http://" + url;
057                    }
058                    return url;
059            }
060            
061            public void setURL(String url)
062            {
063                    setText(url);
064            }
065    
066            // FocusListener methods
067            public void onFocus(Widget sender)
068            {
069                    DeferredCommand.add(new SelectAllCommand(this));
070            }
071    
072            public void onLostFocus(Widget sender) {}
073    
074            // KeyboardListener methods
075            public void onKeyPress(Widget sender, char keyCode, int modifiers)
076            {
077                    if (keyCode == KeyboardListener.KEY_ENTER)
078                    {
079                            setFocus(false);
080                            for (Iterator iter = m_listeners.iterator(); iter.hasNext();)
081                            {
082                                    ((URLListener) iter.next()).urlEntered(this);
083                            }
084                    }
085            }
086            
087            public void onKeyDown(Widget sender, char keyCode, int modifiers) {}
088    
089            public void onKeyUp(Widget sender, char keyCode, int modifiers) {}
090    }