package org.gwtoolbox.widget.client.panel.contentpanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.HasCloseHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.impl.FocusImpl;
import org.gwtoolbox.commons.ui.client.event.CompoundHandlerRegistration;
import org.gwtoolbox.commons.ui.client.event.MouseHoverHandler;
import org.gwtoolbox.commons.ui.client.event.custom.HasToggleHandlers;
import org.gwtoolbox.widget.client.button.SimpleMenuButton;
import org.gwtoolbox.commons.ui.client.event.CommonMouseHandler;
import org.gwtoolbox.commons.ui.client.event.custom.ToggleEvent;
import org.gwtoolbox.commons.ui.client.event.custom.ToggleHandler;
import org.gwtoolbox.widget.client.menu.MenuBuilder;
import org.gwtoolbox.widget.client.panel.*;
import org.gwtoolbox.widget.client.panel.layout.VerticalLayoutData;
import org.gwtoolbox.widget.client.support.setter.ElementVisibilitySetter;
import org.gwtoolbox.widget.client.support.setter.GridCellStyleAttributeSetter;
import org.gwtoolbox.widget.client.support.setter.ValueSetter;
import org.gwtoolbox.widget.client.support.setter.WidgetStyleAttributeSetter;
/**
* @author Uri Boness
* @deprecated use {@link org.gwtoolbox.widget.client.panel.layout.PanelLayout} instead
*/
@Deprecated
public class ContentPanel extends LayoutComposite implements HasCloseHandlers<ContentPanel> {
private SimplePanel contentPane;
private TitleBar titleBar;
private boolean collapsed;
private boolean fixed;
private ValueSetter<String> contentPaddingTop;
private ValueSetter<String> contentPaddingRight;
private ValueSetter<String> contentPaddingBottom;
private ValueSetter<String> contentPaddingLeft;
private ValueSetter<String> titleBarHeight;
private ValueSetter<Boolean> contentVisibilty;
public ContentPanel(String title) {
this(title, null);
}
public ContentPanel(String title, Image image) {
this(title, image, false);
}
public ContentPanel(String title, boolean fiexed) {
this(title, null, fiexed);
}
public ContentPanel(String title, Image image, boolean fixed) {
this.fixed = fixed;
if (fixed) {
initWidget(createFixedWidget(title, image));
} else {
initWidget(createFlexibleWidget(title, image));
}
setStylePrimaryName("ContentPanel");
setShowBorder(false);
}
private Widget createFixedWidget(String title, Image image) {
FixedVerticalPanel main = new FixedVerticalPanel();
titleBar = new TitleBar(title, image);
titleBar.setSize("100%", "100%");
SimplePanel sp = new SimplePanel();
sp.setWidget(titleBar);
main.addWidget(sp, new VerticalLayoutData().setHeight("28px"));
contentPane = new SimplePanel();
contentPane.setStylePrimaryName("Content");
LayoutUtils.expandToFitParent(contentPane.getElement());
contentPaddingTop = new WidgetStyleAttributeSetter(contentPane, "paddingTop");
contentPaddingRight = new WidgetStyleAttributeSetter(contentPane, "paddingRight");
contentPaddingBottom = new WidgetStyleAttributeSetter(contentPane, "paddingBottom");
contentPaddingLeft = new WidgetStyleAttributeSetter(contentPane, "paddingLeft");
contentVisibilty = new ElementVisibilitySetter(contentPane.getElement());
sp = new SimplePanel();
sp.setWidget(contentPane);
main.addWidget(sp, new VerticalLayoutData("*"));
return main;
}
private Widget createFlexibleWidget(String title, Image image) {
Grid main = new Grid(2, 1);
main.setCellPadding(0);
main.setCellSpacing(0);
titleBar = new TitleBar(title, image);
titleBar.setSize("100%", "100%");
main.setWidget(0, 0, titleBar);
main.getCellFormatter().setWidth(0, 0, "100%");
main.getCellFormatter().setHeight(0, 0, "28px");
titleBarHeight = new GridCellStyleAttributeSetter(main, 0, 0, "height");
contentPane = new SimplePanel();
main.setWidget(1, 0, contentPane);
contentPane.setSize("100%", "100%");
main.getCellFormatter().setWidth(1, 0, "100%");
main.getCellFormatter().setHeight(1, 0, "100%");
main.getCellFormatter().setStyleName(1, 0, "Content");
// contentPaddingTop = new GridCellStyleAttributeSetter(main, 1, 0, "paddingTop");
// contentPaddingRight = new GridCellStyleAttributeSetter(main, 1, 0, "paddingRight");
// contentPaddingBottom = new GridCellStyleAttributeSetter(main, 1, 0, "paddingBottom");
// contentPaddingLeft = new GridCellStyleAttributeSetter(main, 1, 0, "paddingLeft");
contentPaddingTop = new WidgetStyleAttributeSetter(contentPane, "paddingTop");
contentPaddingRight = new WidgetStyleAttributeSetter(contentPane, "paddingRight");
contentPaddingBottom = new WidgetStyleAttributeSetter(contentPane, "paddingBottom");
contentPaddingLeft = new WidgetStyleAttributeSetter(contentPane, "paddingLeft");
contentVisibilty = new ElementVisibilitySetter(main.getCellFormatter().getElement(1, 0));
return main;
}
public void setTitle(String title) {
titleBar.setTitle(title);
}
public void setImage(Image image) {
titleBar.setImage(image);
}
public void setContent(Widget content) {
contentPane.setWidget(content);
}
public void setCollapsable(boolean collapsable) {
titleBar.setCollapsable(collapsable);
}
public HandlerRegistration addCloseHandler(CloseHandler<ContentPanel> handler) {
return addHandler(handler, CloseEvent.getType());
}
public void setClosable(boolean closable) {
titleBar.setClosable(closable);
}
public void collapse() {
contentVisibilty.set(false);
collapsed = true;
addStyleDependentName(fixed ? "collapsed" : "flex-collapsed");
if (titleBarHeight != null) {
titleBarHeight.set("27px");
}
}
public void expand() {
contentVisibilty.set(true);
collapsed = false;
removeStyleDependentName(fixed ? "collapsed" : "flex-collapsed");
if (titleBarHeight != null) {
titleBarHeight.set("28px");
}
}
public boolean isCollapsed() {
return collapsed;
}
public void setContentVerticalScrolling(Scrolling scrolling) {
DOM.setStyleAttribute(contentPane.getElement(), "overflowY", scrolling.getCssValue());
}
public void setContentHorizontalScrolling(Scrolling scrolling) {
DOM.setStyleAttribute(contentPane.getElement(), "overflowX", scrolling.getCssValue());
}
public void setContentPadding(String padding) {
setContentPadding(padding, padding, padding, padding);
}
public void setContentPadding(String top, String right, String bottom, String left) {
contentPaddingTop.set(top);
contentPaddingRight.set(right);
contentPaddingBottom.set(bottom);
contentPaddingLeft.set(left);
}
public void setShowBorder(boolean showBorder) {
if (showBorder) {
DOM.setStyleAttribute(getElement(), "borderWidth", "1px");
} else {
DOM.setStyleAttribute(getElement(), "borderWidth", "0");
}
}
public void setContentStyle(String propertyName, String value) {
DOM.setStyleAttribute(contentPane.getElement(), propertyName, value);
}
public SimpleMenuButton addMenuTool(Tool tool, MenuBuilder menuBuilder) {
return titleBar.addMenuButton(tool, menuBuilder);
}
/**
* Adds a tool button.
*
* @param tool The tool type
* @param handler notified when the tool is clicked.
* @return The Widget which represents the button.
*/
public ToolButton addTool(Tool tool, ClickHandler handler) {
return titleBar.addButton(tool, handler);
}
public ToolToggleButton addToggleTool(ToggleTool tool, ToggleHandler handler) {
return titleBar.addToggleButton(tool, handler);
}
public void clearTools() {
titleBar.clearButtons();
}
public HandlerRegistration addTitleMouseHandler(CommonMouseHandler handler) {
return titleBar.addCommonMouseHandler(handler);
}
//============================================== Helper Methods ====================================================
public void layout() {
super.layout();
Widget widget = contentPane.getWidget();
LayoutUtils.notifyLayout(widget);
}
//============================================== Inner Classes =====================================================
public static interface Tool {
String getStyleName();
}
public static interface ToggleTool {
String getUpStyleName();
String getDownStyleName();
}
public static class CustomTool implements Tool {
private final String styleName;
public CustomTool(String styleName) {
this.styleName = styleName;
}
public String getStyleName() {
return styleName;
}
}
public static class CustomToggleTool implements ToggleTool {
private String upStyleName;
private String downStyleName;
public CustomToggleTool(String upStyleName, String downStyleName) {
this.upStyleName = upStyleName;
this.downStyleName = downStyleName;
}
public String getUpStyleName() {
return upStyleName;
}
public String getDownStyleName() {
return downStyleName;
}
}
public static enum Tools implements Tool {
ADD("tool-add"),
REMOVE("tool-remove"),
MAXIMIZE("tool-maximize"),
UNMAXIMIZE("tool-unmaximize"),
MINIMIZE("tool-minimize"),
SLIDE_UP("tool-slide-up"),
SLIDE_DOWN("tool-slide-down"),
SLIDE_LEFT("tool-slide-left"),
SLIDE_RIGHT("tool-slide-right"),
GEAR("tool-gear");
private final String styleName;
private Tools(String styleName) {
this.styleName = styleName;
}
public String getStyleName() {
return styleName;
}
}
public static enum ToggleTools implements ToggleTool {
PIN("tool-pin-down", "tool-pin-up");
private final String upStyleName;
private final String downStyleName;
ToggleTools(String upStyleName, String downStyleName) {
this.upStyleName = upStyleName;
this.downStyleName = downStyleName;
}
public String getUpStyleName() {
return upStyleName;
}
public String getDownStyleName() {
return downStyleName;
}
}
protected class TitleBar extends Composite {
private Label titleLabel;
private SimplePanel imagePane;
private SimplePanel closeButtonPane;
private SimplePanel collapseButtonPane;
private HorizontalPanel tools;
public TitleBar(String title, Image image) {
HorizontalPanel main = new HorizontalPanel();
imagePane = new SimplePanel();
if (image != null) {
imagePane.setWidget(image);
}
main.add(imagePane);
imagePane.setStylePrimaryName("Image");
main.setCellWidth(imagePane, "25px");
main.setCellHeight(imagePane, "100%");
main.setCellHorizontalAlignment(imagePane, HorizontalPanel.ALIGN_CENTER);
main.setCellVerticalAlignment(imagePane, HorizontalPanel.ALIGN_MIDDLE);
titleLabel = new Label(title);
titleLabel.setStylePrimaryName("Label");
main.add(titleLabel);
main.setCellHorizontalAlignment(titleLabel, HorizontalPanel.ALIGN_LEFT);
main.setCellVerticalAlignment(titleLabel, HorizontalPanel.ALIGN_MIDDLE);
main.setCellWidth(titleLabel, "100%");
main.setCellHeight(titleLabel, "100%");
tools = new HorizontalPanel();
tools.setSpacing(0);
tools.setHeight("100%");
main.add(tools);
collapseButtonPane = new SimplePanel();
main.add(collapseButtonPane);
main.setCellWidth(collapseButtonPane, "25px");
main.setCellHeight(collapseButtonPane, "100%");
main.setCellHorizontalAlignment(collapseButtonPane, HorizontalPanel.ALIGN_CENTER);
main.setCellVerticalAlignment(collapseButtonPane, HorizontalPanel.ALIGN_MIDDLE);
closeButtonPane = new SimplePanel();
main.add(closeButtonPane);
main.setCellWidth(closeButtonPane, "25px");
main.setCellHeight(closeButtonPane, "100%");
main.setCellHorizontalAlignment(closeButtonPane, HorizontalPanel.ALIGN_CENTER);
main.setCellVerticalAlignment(closeButtonPane, HorizontalPanel.ALIGN_MIDDLE);
initWidget(main);
setStylePrimaryName("TitleBar");
}
public HandlerRegistration addCommonMouseHandler(CommonMouseHandler handler) {
CompoundHandlerRegistration registration = new CompoundHandlerRegistration();
registration.addRegistration(titleLabel.addMouseMoveHandler(handler));
registration.addRegistration(titleLabel.addMouseOutHandler(handler));
registration.addRegistration(titleLabel.addMouseOverHandler(handler));
registration.addRegistration(titleLabel.addMouseDownHandler(handler));
registration.addRegistration(titleLabel.addMouseUpHandler(handler));
return registration;
}
public void setTitle(String title) {
titleLabel.setText(title);
}
public void setImage(Image image) {
imagePane.setWidget(image);
}
public void setClosable(boolean closable) {
if (closable) {
ToolButton button = new ToolButton(new CustomTool("tool-close"), new ClickHandler() {
public void onClick(ClickEvent event) {
CloseEvent.fire(ContentPanel.this, ContentPanel.this);
}
});
DOM.setStyleAttribute(button.getElement(), "marginLeft", "3px");
closeButtonPane.setWidget(button);
} else {
closeButtonPane.setWidget(null);
}
}
public void setCollapsable(boolean collapsable) {
if (!collapsable) {
collapseButtonPane.clear();
collapseButtonPane.setVisible(false);
return;
}
collapseButtonPane.setVisible(true);
final ToolToggleButton button = new ToolToggleButton(new CustomToggleTool("tool-arrow-down", "tool-arrow-up"));
DOM.setStyleAttribute(button.getElement(), "marginLeft", "3px");
button.addToggleHandler(new ToggleHandler() {
public void onToggle(ToggleEvent event) {
if (button.isDown()) {
collapse();
} else {
expand();
}
}
});
collapseButtonPane.setWidget(button);
}
public ToolToggleButton addToggleButton(ToggleTool tool, ToggleHandler handler) {
ToolToggleButton button = new ToolToggleButton(tool, handler);
DOM.setStyleAttribute(button.getElement(), "marginLeft", "3px");
tools.add(button);
tools.setCellWidth(button, "25px");
tools.setCellHeight(button, "100%");
tools.setCellHorizontalAlignment(button, HorizontalPanel.ALIGN_CENTER);
tools.setCellVerticalAlignment(button, HorizontalPanel.ALIGN_MIDDLE);
return button;
}
public ToolButton addButton(Tool tool, ClickHandler handler) {
ToolButton button = new ToolButton(tool, handler);
DOM.setStyleAttribute(button.getElement(), "marginLeft", "3px");
tools.add(button);
tools.setCellWidth(button, "25px");
tools.setCellHeight(button, "100%");
tools.setCellHorizontalAlignment(button, HorizontalPanel.ALIGN_CENTER);
tools.setCellVerticalAlignment(button, HorizontalPanel.ALIGN_MIDDLE);
return button;
}
public SimpleMenuButton addMenuButton(Tool tool, MenuBuilder menuBuilder) {
SimpleMenuButton button = new SimpleMenuButton(new ToolButton(tool), true);
DOM.setStyleAttribute(button.getElement(), "marginLeft", "3px");
menuBuilder.build(button.getMenu());
tools.add(button);
tools.setCellWidth(button, "25px");
tools.setCellHeight(button, "100%");
tools.setCellHorizontalAlignment(button, HorizontalPanel.ALIGN_CENTER);
tools.setCellVerticalAlignment(button, HorizontalPanel.ALIGN_MIDDLE);
return button;
}
public void clearButtons() {
tools.clear();
}
}
public static class ToolButton extends ButtonBase {
private boolean enabled = true;
public ToolButton(Tool tool) {
this(tool, null);
}
private ToolButton(Tool tool, ClickHandler clickHandler) {
super(FocusImpl.getFocusImplForPanel().createFocusable());
setStylePrimaryName(tool.getStyleName());
addStyleName("transparent-75");
MouseHoverHandler hoverHandler = new MouseHoverHandler() {
@Override
public void onMouseOver(MouseOverEvent event) {
removeStyleName("transparent-75");
}
@Override
public void onMouseOut(MouseOutEvent event) {
addStyleName("transparent-75");
}
};
DOM.setStyleAttribute(getElement(), "width", "15px");
DOM.setStyleAttribute(getElement(), "height", "15px");
DOM.setStyleAttribute(getElement(), "cursor", "pointer");
addDomHandler(hoverHandler, MouseOverEvent.getType());
addDomHandler(hoverHandler, MouseOutEvent.getType());
if (clickHandler != null) {
addClickHandler(clickHandler);
}
}
@Override
public void onBrowserEvent(Event event) {
if (enabled) {
super.onBrowserEvent(event);
}
}
public HandlerRegistration addClickHandler(ClickHandler handler) {
return addDomHandler(handler, ClickEvent.getType());
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
if (enabled) {
removeStyleName("transparent-50");
addStyleName("transparent-75");
} else {
removeStyleName("transparent-75");
addStyleDependentName("transparent-50");
}
this.enabled = enabled;
}
@Override
public void setSize(String width, String height) {
throw new UnsupportedOperationException("The size of the ToolButton is fixed to 15x15 pixels");
}
@Override
public void setWidth(String width) {
throw new UnsupportedOperationException("The size of the ToolButton is fixed to 15x15 pixels");
}
@Override
public void setHeight(String height) {
throw new UnsupportedOperationException("The size of the ToolButton is fixed to 15x15 pixels");
}
}
public static class ToolToggleButton extends ButtonBase implements HasToggleHandlers {
private boolean enabled = true;
private boolean down;
private ToggleTool tool;
private ToolToggleButton(ToggleTool tool) {
this(tool, null);
}
private ToolToggleButton(ToggleTool tool, ToggleHandler toggleHandler) {
super(FocusImpl.getFocusImplForPanel().createFocusable());
this.tool = tool;
setStylePrimaryName(tool.getUpStyleName());
addStyleName("transparent-75");
DOM.setStyleAttribute(getElement(), "width", "15px");
DOM.setStyleAttribute(getElement(), "height", "15px");
DOM.setStyleAttribute(getElement(), "cursor", "pointer");
MouseHoverHandler hoverHandler = new MouseHoverHandler() {
@Override
public void onMouseOver(MouseOverEvent event) {
removeStyleName("transparent-75");
}
@Override
public void onMouseOut(MouseOutEvent event) {
addStyleName("transparent-75");
}
};
addDomHandler(hoverHandler, MouseOverEvent.getType());
addDomHandler(hoverHandler, MouseOutEvent.getType());
addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
toggle();
}
});
if (toggleHandler != null) {
addHandler(toggleHandler, ToggleEvent.getType());
}
}
@Override
public void onBrowserEvent(Event event) {
if (enabled) {
super.onBrowserEvent(event);
}
}
public HandlerRegistration addToggleHandler(ToggleHandler handler) {
return addHandler(handler, ToggleEvent.getType());
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
if (enabled) {
removeStyleName("transparent-50");
addStyleName("transparent-75");
} else {
removeStyleName("transparent-75");
addStyleDependentName("transparent-50");
}
this.enabled = enabled;
}
public boolean isDown() {
return down;
}
@Override
public void setSize(String width, String height) {
throw new UnsupportedOperationException("The size of the ToolButton is fixed to 15x15 pixels");
}
@Override
public void setWidth(String width) {
throw new UnsupportedOperationException("The size of the ToolButton is fixed to 15x15 pixels");
}
@Override
public void setHeight(String height) {
throw new UnsupportedOperationException("The size of the ToolButton is fixed to 15x15 pixels");
}
public void toggle() {
down = !down;
if (down) {
setStylePrimaryName(tool.getDownStyleName());
} else {
setStylePrimaryName(tool.getUpStyleName());
}
ToggleEvent.fire(this, down);
}
}
}