/* * Copyright 2000-2014 Vaadin Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.vaadin.ui; import java.io.Serializable; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import org.jsoup.nodes.Attributes; import org.jsoup.nodes.Element; import org.jsoup.nodes.Node; import com.vaadin.event.LayoutEvents.LayoutClickEvent; import com.vaadin.event.LayoutEvents.LayoutClickListener; import com.vaadin.event.LayoutEvents.LayoutClickNotifier; import com.vaadin.server.Sizeable; import com.vaadin.shared.Connector; import com.vaadin.shared.EventId; import com.vaadin.shared.MouseEventDetails; import com.vaadin.shared.ui.absolutelayout.AbsoluteLayoutServerRpc; import com.vaadin.shared.ui.absolutelayout.AbsoluteLayoutState; import com.vaadin.ui.declarative.DesignAttributeHandler; import com.vaadin.ui.declarative.DesignContext; /** * AbsoluteLayout is a layout implementation that mimics html absolute * positioning. * */ @SuppressWarnings("serial") public class AbsoluteLayout extends AbstractLayout implements LayoutClickNotifier { // constants for design attributes private static final String ATTR_TOP = ":top"; private static final String ATTR_RIGHT = ":right"; private static final String ATTR_BOTTOM = ":bottom"; private static final String ATTR_LEFT = ":left"; private static final String ATTR_Z_INDEX = ":z-index"; private AbsoluteLayoutServerRpc rpc = new AbsoluteLayoutServerRpc() { @Override public void layoutClick(MouseEventDetails mouseDetails, Connector clickedConnector) { fireEvent(LayoutClickEvent.createEvent(AbsoluteLayout.this, mouseDetails, clickedConnector)); } }; // Maps each component to a position private LinkedHashMap componentToCoordinates = new LinkedHashMap(); /** * Creates an AbsoluteLayout with full size. */ public AbsoluteLayout() { registerRpc(rpc); setSizeFull(); } @Override protected AbsoluteLayoutState getState() { return (AbsoluteLayoutState) super.getState(); } /** * Gets an iterator for going through all components enclosed in the * absolute layout. */ @Override public Iterator iterator() { return componentToCoordinates.keySet().iterator(); } /** * Gets the number of contained components. Consistent with the iterator * returned by {@link #getComponentIterator()}. * * @return the number of contained components */ @Override public int getComponentCount() { return componentToCoordinates.size(); } /** * Replaces one component with another one. The new component inherits the * old components position. */ @Override public void replaceComponent(Component oldComponent, Component newComponent) { ComponentPosition position = getPosition(oldComponent); removeComponent(oldComponent); addComponent(newComponent, position); } /* * (non-Javadoc) * * @see * com.vaadin.ui.AbstractComponentContainer#addComponent(com.vaadin.ui.Component * ) */ @Override public void addComponent(Component c) { addComponent(c, new ComponentPosition()); } /** * Adds a component to the layout. The component can be positioned by * providing a string formatted in CSS-format. *

* For example the string "top:10px;left:10px" will position the component * 10 pixels from the left and 10 pixels from the top. The identifiers: * "top","left","right" and "bottom" can be used to specify the position. *

* * @param c * The component to add to the layout * @param cssPosition * The css position string */ public void addComponent(Component c, String cssPosition) { ComponentPosition position = new ComponentPosition(); position.setCSSString(cssPosition); addComponent(c, position); } /** * Adds the component using the given position. Ensures the position is only * set if the component is added correctly. * * @param c * The component to add * @param position * The position info for the component. Must not be null. * @throws IllegalArgumentException * If adding the component failed */ private void addComponent(Component c, ComponentPosition position) throws IllegalArgumentException { if (equals(c.getParent())) { removeComponent(c); } /* * Create position instance and add it to componentToCoordinates map. We * need to do this before we call addComponent so the attachListeners * can access this position. #6368 */ internalSetPosition(c, position); try { super.addComponent(c); } catch (IllegalArgumentException e) { internalRemoveComponent(c); throw e; } } /** * Removes the component from all internal data structures. Does not * actually remove the component from the layout (this is assumed to have * been done by the caller). * * @param c * The component to remove */ private void internalRemoveComponent(Component c) { componentToCoordinates.remove(c); } @Override public void beforeClientResponse(boolean initial) { super.beforeClientResponse(initial); // This could be in internalRemoveComponent and internalSetComponent if // Map was supported. We cannot get the child // connectorId unless the component is attached to the application so // the String->String map cannot be populated in internal* either. Map connectorToPosition = new HashMap(); for (Iterator ci = getComponentIterator(); ci.hasNext();) { Component c = ci.next(); connectorToPosition.put(c.getConnectorId(), getPosition(c) .getCSSString()); }
<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>jQuery UI Button - Checkboxes</title>
	<link rel="stylesheet" href="../../themes/base/all.css">
	<script src="../../external/jquery/jquery.js"></script>
	<script src="../../ui/core.js"></script>
	<script src="../../ui/widget.js"></script>
	<script src="../../ui/button.js"></script>
	<link rel="stylesheet" href="../demos.css">
	<script>
	$(function() {
		$( "#check" ).button();
		$( "#format" ).buttonset();
	});
	</script>
	<style>
	#format { margin-top: 2em; }
	</style>
</head>
<body>

<input type="checkbox" id="check" /><label for="check">Toggle</label>

<div id="format">
	<input type="checkbox" id="check1" /><label for="check1">B</label>
	<input type="checkbox" id="check2" /><label for="check2">I</label>
	<input type="checkbox" id="check3" /><label for="check3">U</label>
</div>

<div class="demo-description">
<p>A checkbox is styled as a toggle button with the button widget. The label element associated with the checkbox is used for the button text.</p>
<p>This demo also demonstrates three checkboxes styled as a button set by calling <code>.buttonset()</code> on a common container.</p>
</div>
</body>
</html>
omUnits; } /** * Sets the unit for the 'bottom' attribute * * @param bottomUnits * See {@link Sizeable} UNIT_SYMBOLS for a description of the * available units. */ public void setBottomUnits(Unit bottomUnits) { this.bottomUnits = bottomUnits; markAsDirty(); } /** * Gets the unit for the 'left' attribute * * @return See {@link Sizeable} UNIT_SYMBOLS for a description of the * available units. */ public Unit getLeftUnits() { return leftUnits; } /** * Sets the unit for the 'left' attribute * * @param leftUnits * See {@link Sizeable} UNIT_SYMBOLS for a description of the * available units. */ public void setLeftUnits(Unit leftUnits) { this.leftUnits = leftUnits; markAsDirty(); } /** * Gets the 'z-index' attribute. * * @return the zIndex The z-index attribute */ public int getZIndex() { return zIndex; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return getCSSString(); } } @Override public void addLayoutClickListener(LayoutClickListener listener) { addListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER, LayoutClickEvent.class, listener, LayoutClickListener.clickMethod); } /** * @deprecated As of 7.0, replaced by * {@link #addLayoutClickListener(LayoutClickListener)} **/ @Override @Deprecated public void addListener(LayoutClickListener listener) { addLayoutClickListener(listener); } @Override public void removeLayoutClickListener(LayoutClickListener listener) { removeListener(EventId.LAYOUT_CLICK_EVENT_IDENTIFIER, LayoutClickEvent.class, listener); } /** * @deprecated As of 7.0, replaced by * {@link #removeLayoutClickListener(LayoutClickListener)} **/ @Override @Deprecated public void removeListener(LayoutClickListener listener) { removeLayoutClickListener(listener); } /* * (non-Javadoc) * * @see com.vaadin.ui.AbstractComponent#readDesign(org.jsoup.nodes .Node, * com.vaadin.ui.declarative.DesignContext) */ @Override public void readDesign(Element design, DesignContext designContext) { // process default attributes super.readDesign(design, designContext); // handle children for (Element childComponent : design.children()) { Attributes attr = childComponent.attributes(); Component newChild = designContext.readDesign(childComponent); StringBuilder css = new StringBuilder(); if (attr.hasKey(ATTR_TOP)) { css.append("top:").append(attr.get(ATTR_TOP)).append(";"); } if (attr.hasKey(ATTR_RIGHT)) { css.append("right:").append(attr.get(ATTR_RIGHT)).append(";"); } if (attr.hasKey(ATTR_BOTTOM)) { css.append("bottom:").append(attr.get(ATTR_BOTTOM)).append(";"); } if (attr.hasKey(ATTR_LEFT)) { css.append("left:").append(attr.get(ATTR_LEFT)).append(";"); } if (attr.hasKey(ATTR_Z_INDEX)) { css.append("z-index:").append(attr.get(ATTR_Z_INDEX)) .append(";"); } addComponent(newChild, css.toString()); } } /* * (non-Javadoc) * * @see com.vaadin.ui.AbstractComponent#writeDesign(org.jsoup.nodes.Node, * com.vaadin.ui.declarative.DesignContext) */ @Override public void writeDesign(Element design, DesignContext designContext) { super.writeDesign(design, designContext); AbsoluteLayout def = designContext.getDefaultInstance(this); if (!designContext.shouldWriteChildren(this, def)) { return; } // handle children for (Component child : this) { Element childElement = designContext.createElement(child); design.appendChild(childElement); // handle position ComponentPosition position = getPosition(child); writePositionAttribute(childElement, ATTR_TOP, position .getTopUnits().getSymbol(), position.getTopValue()); writePositionAttribute(childElement, ATTR_RIGHT, position .getRightUnits().getSymbol(), position.getRightValue()); writePositionAttribute(childElement, ATTR_BOTTOM, position .getBottomUnits().getSymbol(), position.getBottomValue()); writePositionAttribute(childElement, ATTR_LEFT, position .getLeftUnits().getSymbol(), position.getLeftValue()); // handle z-index if (position.getZIndex() >= 0) { childElement .attr(ATTR_Z_INDEX, String.valueOf(position.zIndex)); } } } /** * Private method for writing position attributes * * @since 7.4 * @param node * target node * @param key * attribute key * @param symbol * value symbol * @param value * the value */ private void writePositionAttribute(Node node, String key, String symbol, Float value) { if (value != null) { String valueString = DesignAttributeHandler.getFormatter().format( value); node.attr(key, valueString + symbol); } } }