PathBar.java :  » GWT » gwt-s3 » com » nubotech » gwt » oss » browser » client » ui » Java Open Source

Java Open Source » GWT » gwt s3 
gwt s3 » com » nubotech » gwt » oss » browser » client » ui » PathBar.java
/*
 * Copyright (c) 2007, Nubo Technologies
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 *    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *    * Neither the name of the Nubo Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.nubotech.gwt.oss.browser.client.ui;

import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ChangeListenerCollection;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.nubotech.gwt.oss.browser.client.Main.Images;

/**
 *
 * @author jonnakkerud
 */
public class PathBar extends Composite {
    private HorizontalPanel hp;
    private TextBox tb;
    private ChangeListenerCollection changeListenerCollection = new ChangeListenerCollection();
    
    public PathBar(BrowserView.Images images) {
        initWidget(hp = new HorizontalPanel());
        hp.setSpacing(3);
        setWidth("100%");
        initGui(images);
    }
    
    private void initGui(BrowserView.Images images) {
        tb = new TextBox();
        tb.setWidth("100%");
        hp.add(tb);
        
        // fire event if user hits enter
        tb.addKeyboardListener(new KeyboardListenerAdapter() {
            public void onKeyPress(Widget sender, char keyCode, int modifiers) {
                if (keyCode == (char) KEY_ENTER) {
                    changeListenerCollection.fireChange(PathBar.this);
                }
            }
        });        
        
        // return button
        Image upDir = images.upDirectory().createImage();
        PushButton b = new PushButton(upDir);
        hp.add(b);
        hp.setCellVerticalAlignment(b, VerticalPanel.ALIGN_MIDDLE);
        b.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                pop();
            }
        });
    }
    
    public void setPath(String path) {
        if (getPath().equals(path)) return;
        if (path.startsWith(BrowserView.DEFAULT_DELIMITER) == false) {
            path = BrowserView.DEFAULT_DELIMITER + path;
        }
        
        if (path.endsWith(BrowserView.DEFAULT_DELIMITER) == false) {
            path = path + BrowserView.DEFAULT_DELIMITER;
        }
        
        tb.setText(path);
        changeListenerCollection.fireChange(this);
    }
    
    public String getPath() {
        return tb.getText();
    }
    
    public String getBucket() {
        String path = removeLeading(getPath());        
        String s[] = path.split(BrowserView.DEFAULT_DELIMITER, 2);
        return s[0].length() == 0 ? null : s[0];
    }
            
    public String getPrefix() {
        String key = null;
        String path = removeLeading(getPath());        
        String s[] = path.split(BrowserView.DEFAULT_DELIMITER, 2);
        if (s.length == 2) {
            key = s[1];
        }
        return removeTrailing(key); 
    }
    
    public void addChangeListener(ChangeListener cl) {
        changeListenerCollection.add(cl);
    }

    public void removeChangeListener(ChangeListener cl) {
        changeListenerCollection.remove(cl);
    }

    private void pop() {
        String path = removeTrailing(getPath());
        int idx = path.lastIndexOf(BrowserView.DEFAULT_DELIMITER);
        setPath(path.substring(0, idx));
    }
    
    public static String removeLeading(String inStr) {
        String outStr = null;
        if (inStr == null || inStr.length() == 0) {
            outStr = "";
        } 
        else {
            if (inStr.startsWith(BrowserView.DEFAULT_DELIMITER)) {
                outStr = inStr.substring(1);
            }
            else {
                outStr = inStr;
            }
        }
        
        return outStr;
    }

    public static String removeTrailing(String inStr) {
        String outStr = null;
        if (inStr == null || inStr.length() == 0) {
            outStr = "";
        } 
        else {
            if (inStr.endsWith(BrowserView.DEFAULT_DELIMITER)) {
                outStr = inStr.substring(0, inStr.length()-1);
            }
            else {
                outStr = inStr;
            }
        }
        
        return outStr;
    }
    
    public static String[] tokenizeKey(String key, String prefix) {
        String[] result = null;
        if (prefix != null && prefix.length() > 0) {
            String s = removeLeading(key.substring(prefix.length()));
            result = s.split(BrowserView.DEFAULT_DELIMITER);
        }
        else {
            //result = new String[]{key};
            result = key.split(BrowserView.DEFAULT_DELIMITER);
        }
        return result;
    }
   
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.