workspace/src/main/java/org/gcube/portlets/user/workspace/client/view/toolbars/GxtBreadcrumbPathPanel.java

187 lines
5.2 KiB
Java

package org.gcube.portlets.user.workspace.client.view.toolbars;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.gcube.portlets.user.workspace.client.AppController;
import org.gcube.portlets.user.workspace.client.event.PathElementSelectedEvent;
import org.gcube.portlets.user.workspace.client.model.FileModel;
import org.gcube.portlets.user.workspace.client.resources.Resources;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.WidgetComponent;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.ui.Image;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
*
*/
public class GxtBreadcrumbPathPanel {
private static final String ROOT_NAME = "Workspace";
private HorizontalPanel hpToolBar = new HorizontalPanel();
private Text txtPath = new Text("PATH ");
private LinkedHashMap<String, FileModel> hashFileModel = new LinkedHashMap<String, FileModel>();// Ordered-HashMap
boolean rootAdded = false;
protected WidgetComponent hardDiskIcon = new WidgetComponent(new Image (Resources.getImageHardDisk()));
private FileModel lastParent;
public GxtBreadcrumbPathPanel() {
hpToolBar = new HorizontalPanel();
// hpToolBar.setScrollMode(Scroll.AUTOX);
hpToolBar.setId("myToolbarPath");
hpToolBar.setHeight("25px");
initToolbar();
}
/*
* private void createScrollers() { int h = hpToolBar.getHeight(); El
* scrollLeft =
* hpToolBar.el().insertFirst("<div class='x-tab-scroller-left'></div>");
* addStyleOnOver(scrollLeft.dom, "x-tab-scroller-left-over");
* scrollLeft.setHeight(h);
*
* El scrollRight =
* hpToolBar.el().insertFirst("<div class='x-tab-scroller-right'></div>");
* addStyleOnOver(scrollRight.dom, "x-tab-scroller-right-over");
* scrollRight.setHeight(h);
*
* }
*
* public El fly(Element elem) { return El.fly(elem, "component"); }
*
* private Map<String, String> overElements;
*
*
* protected void addStyleOnOver(Element elem, String style) { if
* (overElements == null) { overElements = new FastMap<String>(); }
* overElements.put(fly(elem).getId(), style); }
*/
public HorizontalPanel getToolBarPathPanel() {
return hpToolBar;
}
public void setPath(List<FileModel> parents) {
initToolbarWithoutFakeRoot();
// refreshSize();
if (parents != null && parents.size() > 0) {
ArrayList<Button> listButtons = new ArrayList<Button>();
hashFileModel = new LinkedHashMap<String, FileModel>();
// arrayFileModel = new ArrayList<FileModel>();
for (FileModel parent : parents) {
Button butt = new Button(parent.getName());
butt.setId(parent.getIdentifier());
butt.setStyleAttribute("top", "-4px");
butt.setStyleName("button-hyperlink");
// butt.setHeight(12);
hashFileModel.put(parent.getIdentifier(), parent);
// arrayFileModel.set(parent.getIdentifier(),parent);
butt.addListener(Events.OnClick, new Listener<ButtonEvent>() {
@Override
public void handleEvent(ButtonEvent be) {
// Window.alert("Name " + be.getButton().getText() +
// " ID: "+ be.getButton().getId());
FileModel target = hashFileModel.get(be.getButton().getId());
AppController.getEventBus().fireEvent(new PathElementSelectedEvent(target));
}
});
listButtons.add(butt);
}
int size = listButtons.size();
for (int i = 0; i < size - 1; i++) {
hpToolBar.add(listButtons.get(i));
hpToolBar.add(new WidgetComponent(new Image(Resources.getImagePathSeparator())));
// toolBar.getWidth();
}
lastParent = parents.get(parents.size()-1);
hpToolBar.add(listButtons.get(size - 1)); // Add last element
hpToolBar.layout(true);
// int currentWidth = hpToolBar.el().getChild(0).getRegion().right;
// int maxWidth = hpToolBar.getWidth();
// GWT.log("toolBar maxWidth width is: "+maxWidth);
// GWT.log("toolBar currentWidth is: "+currentWidth);
}
}
public boolean breadcrumbIsEmpty(){
if(hashFileModel.size() == 0)
return true;
return false;
}
public FileModel getLastParent(){
return lastParent;
}
private void initToolbar() {
hpToolBar.removeAll();
hpToolBar.setStyleName("myToolbar");
hpToolBar.setStyleAttribute("padding-top", "5px");
txtPath.setStyleAttribute("padding-right", "10px");
hpToolBar.add(new WidgetComponent(new Image(Resources.getImagePathSeparator())));
hpToolBar.layout(true);
Button butt = new Button(ROOT_NAME);
butt.setId("");
butt.setStyleAttribute("top", "-4px");
butt.setStyleName("button-hyperlink");
hpToolBar.add(butt);
}
private void initToolbarWithoutFakeRoot() {
hpToolBar.removeAll();
hpToolBar.setStyleName("myToolbar");
hpToolBar.setStyleAttribute("padding-top", "5px");
txtPath.setStyleAttribute("padding-right", "10px");
hpToolBar.add(new WidgetComponent(new Image(Resources.getImagePathSeparator())));
hpToolBar.layout(true);
}
/**
*
*/
public void refreshSize() {
GWT.log("Refreshed size");
}
}