--- /dev/null
+/*
+ * Copyright 2000-2016 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.client.ui.colorpicker;
+
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.event.dom.client.HasClickHandlers;
+import com.vaadin.client.communication.StateChangeEvent;
+import com.vaadin.client.ui.AbstractComponentConnector;
+import com.vaadin.shared.ui.colorpicker.ColorPickerState;
+
+/**
+ * An abstract class that defines default implementation for a color picker
+ * connector.
+ *
+ * @since 7.0.0
+ */
+public abstract class AbstractColorPickerConnector
+ extends AbstractComponentConnector implements ClickHandler {
+
+ private static final String DEFAULT_WIDTH_STYLE = "v-default-caption-width";
+
+ @Override
+ public ColorPickerState getState() {
+ return (ColorPickerState) super.getState();
+ }
+
+ @Override
+ public boolean delegateCaptionHandling() {
+ return false;
+ }
+
+ @Override
+ public void onStateChanged(StateChangeEvent stateChangeEvent) {
+ // NOTE: this method is called after @DelegateToWidget
+ super.onStateChanged(stateChangeEvent);
+ if (stateChangeEvent.hasPropertyChanged("color")) {
+ refreshColor();
+
+ if (getState().showDefaultCaption && (getState().caption == null
+ || "".equals(getState().caption))) {
+
+ setCaption(getState().color);
+ }
+ }
+ if (stateChangeEvent.hasPropertyChanged("caption")
+ || stateChangeEvent.hasPropertyChanged("htmlContentAllowed")
+ || stateChangeEvent.hasPropertyChanged("showDefaultCaption")) {
+
+ setCaption(getCaption());
+ refreshDefaultCaptionStyle();
+ }
+ }
+
+ @Override
+ public void init() {
+ super.init();
+ if (getWidget() instanceof HasClickHandlers) {
+ ((HasClickHandlers) getWidget()).addClickHandler(this);
+ }
+ }
+
+ /**
+ * Get caption for the color picker widget.
+ *
+ * @return
+ */
+ protected String getCaption() {
+ if (getState().showDefaultCaption && (getState().caption == null
+ || "".equals(getState().caption))) {
+ return getState().color;
+ }
+ return getState().caption;
+ }
+
+ /**
+ * Add/remove default caption style.
+ */
+ protected void refreshDefaultCaptionStyle() {
+ if (getState().showDefaultCaption
+ && (getState().caption == null || getState().caption.isEmpty())
+ && getState().width.isEmpty()) {
+ getWidget().addStyleName(DEFAULT_WIDTH_STYLE);
+ } else {
+ getWidget().removeStyleName(DEFAULT_WIDTH_STYLE);
+ }
+ }
+
+ /**
+ * Set caption of the color picker widget.
+ *
+ * @param caption
+ */
+ protected abstract void setCaption(String caption);
+
+ /**
+ * Update the widget to show the currently selected color.
+ */
+ protected abstract void refreshColor();
+
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.client.ui.colorpicker;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.client.VCaption;
+import com.vaadin.client.communication.RpcProxy;
+import com.vaadin.client.ui.VColorPickerArea;
+import com.vaadin.shared.ui.Connect;
+import com.vaadin.shared.ui.Connect.LoadStyle;
+import com.vaadin.shared.ui.colorpicker.ColorPickerServerRpc;
+import com.vaadin.ui.ColorPickerArea;
+
+/**
+ * A class that defines an implementation for a color picker connector. Connects
+ * the server side {@link com.vaadin.ui.ColorPickerArea} with the client side
+ * counterpart {@link VColorPickerArea}
+ *
+ * @since 7.0.0
+ */
+@Connect(value = ColorPickerArea.class, loadStyle = LoadStyle.LAZY)
+public class ColorPickerAreaConnector extends AbstractColorPickerConnector {
+
+ private ColorPickerServerRpc rpc = RpcProxy
+ .create(ColorPickerServerRpc.class, this);
+
+ @Override
+ protected Widget createWidget() {
+ return GWT.create(VColorPickerArea.class);
+ }
+
+ @Override
+ public VColorPickerArea getWidget() {
+ return (VColorPickerArea) super.getWidget();
+ }
+
+ @Override
+ public void onClick(ClickEvent event) {
+ rpc.openPopup(getWidget().isOpen());
+ }
+
+ @Override
+ protected void setCaption(String caption) {
+ VCaption.setCaptionText(getWidget(), getState());
+ }
+
+ @Override
+ protected void refreshColor() {
+ getWidget().refreshColor();
+ }
+
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.client.ui.colorpicker;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.client.communication.RpcProxy;
+import com.vaadin.client.ui.VColorPicker;
+import com.vaadin.shared.ui.Connect;
+import com.vaadin.shared.ui.Connect.LoadStyle;
+import com.vaadin.shared.ui.colorpicker.ColorPickerServerRpc;
+import com.vaadin.ui.ColorPicker;
+
+/**
+ * A class that defines default implementation for a color picker connector.
+ * Connects the server side {@link com.vaadin.ui.ColorPicker} with the client
+ * side counterpart {@link VColorPicker}
+ *
+ * @since 7.0.0
+ */
+@Connect(value = ColorPicker.class, loadStyle = LoadStyle.LAZY)
+public class ColorPickerConnector extends AbstractColorPickerConnector {
+
+ private ColorPickerServerRpc rpc = RpcProxy
+ .create(ColorPickerServerRpc.class, this);
+
+ @Override
+ protected Widget createWidget() {
+ return GWT.create(VColorPicker.class);
+ }
+
+ @Override
+ public VColorPicker getWidget() {
+ return (VColorPicker) super.getWidget();
+ }
+
+ @Override
+ public void onClick(ClickEvent event) {
+ rpc.openPopup(getWidget().isOpen());
+ }
+
+ @Override
+ protected void setCaption(String caption) {
+ if (getState().captionAsHtml) {
+ getWidget().setHtml(caption);
+ } else {
+ getWidget().setText(caption);
+ }
+ }
+
+ @Override
+ protected void refreshColor() {
+ getWidget().refreshColor();
+ }
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.client.ui.colorpicker;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.event.dom.client.MouseUpEvent;
+import com.google.gwt.event.dom.client.MouseUpHandler;
+import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.client.communication.RpcProxy;
+import com.vaadin.client.communication.StateChangeEvent;
+import com.vaadin.client.ui.AbstractComponentConnector;
+import com.vaadin.shared.ui.Connect;
+import com.vaadin.shared.ui.Connect.LoadStyle;
+import com.vaadin.shared.ui.colorpicker.ColorPickerGradientServerRpc;
+import com.vaadin.shared.ui.colorpicker.ColorPickerGradientState;
+import com.vaadin.ui.components.colorpicker.ColorPickerGradient;
+
+/**
+ * A class that defines the default implementation for a color picker gradient
+ * connector. Connects the server side
+ * {@link com.vaadin.ui.components.colorpicker.ColorPickerGradient} with the
+ * client side counterpart {@link VColorPickerGradient}
+ *
+ * @since 7.0.0
+ */
+@Connect(value = ColorPickerGradient.class, loadStyle = LoadStyle.LAZY)
+public class ColorPickerGradientConnector extends AbstractComponentConnector
+ implements MouseUpHandler {
+
+ private ColorPickerGradientServerRpc rpc = RpcProxy
+ .create(ColorPickerGradientServerRpc.class, this);
+
+ @Override
+ protected Widget createWidget() {
+ return GWT.create(VColorPickerGradient.class);
+ }
+
+ @Override
+ public VColorPickerGradient getWidget() {
+ return (VColorPickerGradient) super.getWidget();
+ }
+
+ @Override
+ public ColorPickerGradientState getState() {
+ return (ColorPickerGradientState) super.getState();
+ }
+
+ @Override
+ public void onMouseUp(MouseUpEvent event) {
+ rpc.select(getWidget().getCursorX(), getWidget().getCursorY());
+ }
+
+ @Override
+ public void onStateChanged(StateChangeEvent stateChangeEvent) {
+ super.onStateChanged(stateChangeEvent);
+ if (stateChangeEvent.hasPropertyChanged("cursorX")
+ || stateChangeEvent.hasPropertyChanged("cursorY")) {
+
+ getWidget().setCursor(getState().cursorX, getState().cursorY);
+ }
+ if (stateChangeEvent.hasPropertyChanged("bgColor")) {
+ getWidget().setBGColor(getState().bgColor);
+ }
+ }
+
+ @Override
+ protected void init() {
+ super.init();
+ getWidget().addMouseUpHandler(this);
+ }
+
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.client.ui.colorpicker;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.client.communication.RpcProxy;
+import com.vaadin.client.communication.StateChangeEvent;
+import com.vaadin.client.ui.AbstractComponentConnector;
+import com.vaadin.shared.ui.Connect;
+import com.vaadin.shared.ui.Connect.LoadStyle;
+import com.vaadin.shared.ui.colorpicker.ColorPickerGridServerRpc;
+import com.vaadin.shared.ui.colorpicker.ColorPickerGridState;
+import com.vaadin.ui.components.colorpicker.ColorPickerGrid;
+
+/**
+ * A class that defines the default implementation for a color picker grid
+ * connector. Connects the server side
+ * {@link com.vaadin.ui.components.colorpicker.ColorPickerGrid} with the
+ * client side counterpart {@link VColorPickerGrid}
+ *
+ * @since 7.0.0
+ */
+@Connect(value = ColorPickerGrid.class, loadStyle = LoadStyle.LAZY)
+public class ColorPickerGridConnector extends AbstractComponentConnector
+ implements ClickHandler {
+
+ private ColorPickerGridServerRpc rpc = RpcProxy
+ .create(ColorPickerGridServerRpc.class, this);
+
+ @Override
+ protected Widget createWidget() {
+ return GWT.create(VColorPickerGrid.class);
+ }
+
+ @Override
+ public VColorPickerGrid getWidget() {
+ return (VColorPickerGrid) super.getWidget();
+ }
+
+ @Override
+ public ColorPickerGridState getState() {
+ return (ColorPickerGridState) super.getState();
+ }
+
+ @Override
+ public void onClick(ClickEvent event) {
+ rpc.select(getWidget().getSelectedX(), getWidget().getSelectedY());
+ }
+
+ @Override
+ public void onStateChanged(StateChangeEvent stateChangeEvent) {
+ super.onStateChanged(stateChangeEvent);
+ if (stateChangeEvent.hasPropertyChanged("rowCount")
+ || stateChangeEvent.hasPropertyChanged("columnCount")
+ || stateChangeEvent.hasPropertyChanged("updateGrid")) {
+
+ getWidget().updateGrid(getState().rowCount, getState().columnCount);
+ }
+ if (stateChangeEvent.hasPropertyChanged("changedX")
+ || stateChangeEvent.hasPropertyChanged("changedY")
+ || stateChangeEvent.hasPropertyChanged("changedColor")
+ || stateChangeEvent.hasPropertyChanged("updateColor")) {
+
+ getWidget().updateColor(getState().changedColor,
+ getState().changedX, getState().changedY);
+
+ if (!getWidget().isGridLoaded()) {
+ rpc.refresh();
+ }
+ }
+ }
+
+ @Override
+ protected void init() {
+ super.init();
+ getWidget().addClickHandler(this);
+ }
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.client.ui.colorpicker;
+
+import com.google.gwt.dom.client.Style.Unit;
+import com.google.gwt.event.dom.client.MouseDownEvent;
+import com.google.gwt.event.dom.client.MouseDownHandler;
+import com.google.gwt.event.dom.client.MouseMoveEvent;
+import com.google.gwt.event.dom.client.MouseMoveHandler;
+import com.google.gwt.event.dom.client.MouseUpEvent;
+import com.google.gwt.event.dom.client.MouseUpHandler;
+import com.google.gwt.user.client.ui.AbsolutePanel;
+import com.google.gwt.user.client.ui.FocusPanel;
+import com.google.gwt.user.client.ui.HTML;
+import com.vaadin.client.ui.SubPartAware;
+
+/**
+ * Client side implementation for ColorPickerGradient.
+ *
+ * @since 7.0.0
+ *
+ */
+public class VColorPickerGradient extends FocusPanel implements
+ MouseDownHandler, MouseUpHandler, MouseMoveHandler, SubPartAware {
+
+ /** Set the CSS class name to allow styling. */
+ public static final String CLASSNAME = "v-colorpicker-gradient";
+ public static final String CLASSNAME_BACKGROUND = CLASSNAME + "-background";
+ public static final String CLASSNAME_FOREGROUND = CLASSNAME + "-foreground";
+ public static final String CLASSNAME_LOWERBOX = CLASSNAME + "-lowerbox";
+ public static final String CLASSNAME_HIGHERBOX = CLASSNAME + "-higherbox";
+ public static final String CLASSNAME_CONTAINER = CLASSNAME + "-container";
+ public static final String CLASSNAME_CLICKLAYER = CLASSNAME + "-clicklayer";
+ private static final String CLICKLAYER_ID = "clicklayer";
+
+ private final HTML background;
+ private final HTML foreground;
+ private final HTML lowercross;
+ private final HTML highercross;
+ private final HTML clicklayer;
+ private final AbsolutePanel container;
+
+ private boolean mouseIsDown = false;
+
+ private int cursorX;
+ private int cursorY;
+
+ private int width = 220;
+ private int height = 220;
+
+ /**
+ * Instantiates the client side component for a color picker gradient.
+ */
+ public VColorPickerGradient() {
+ super();
+
+ setStyleName(CLASSNAME);
+
+ background = new HTML();
+ background.setStyleName(CLASSNAME_BACKGROUND);
+ background.setPixelSize(width, height);
+
+ foreground = new HTML();
+ foreground.setStyleName(CLASSNAME_FOREGROUND);
+ foreground.setPixelSize(width, height);
+
+ clicklayer = new HTML();
+ clicklayer.setStyleName(CLASSNAME_CLICKLAYER);
+ clicklayer.setPixelSize(width, height);
+ clicklayer.addMouseDownHandler(this);
+ clicklayer.addMouseUpHandler(this);
+ clicklayer.addMouseMoveHandler(this);
+
+ lowercross = new HTML();
+ lowercross.setPixelSize(width / 2, height / 2);
+ lowercross.setStyleName(CLASSNAME_LOWERBOX);
+
+ highercross = new HTML();
+ highercross.setPixelSize(width / 2, height / 2);
+ highercross.setStyleName(CLASSNAME_HIGHERBOX);
+
+ container = new AbsolutePanel();
+ container.setStyleName(CLASSNAME_CONTAINER);
+ container.setPixelSize(width, height);
+ container.add(background, 0, 0);
+ container.add(foreground, 0, 0);
+ container.add(lowercross, 0, height / 2);
+ container.add(highercross, width / 2, 0);
+ container.add(clicklayer, 0, 0);
+
+ add(container);
+ }
+
+ /**
+ * Returns the latest x-coordinate for pressed-down mouse cursor.
+ */
+ protected int getCursorX() {
+ return cursorX;
+ }
+
+ /**
+ * Returns the latest y-coordinate for pressed-down mouse cursor.
+ */
+ protected int getCursorY() {
+ return cursorY;
+ }
+
+ /**
+ * Sets the given css color as the background.
+ *
+ * @param bgColor
+ */
+ protected void setBGColor(String bgColor) {
+ if (bgColor == null) {
+ background.getElement().getStyle().clearBackgroundColor();
+ } else {
+ background.getElement().getStyle().setBackgroundColor(bgColor);
+ }
+ }
+
+ @Override
+ public void onMouseDown(MouseDownEvent event) {
+ event.preventDefault();
+
+ mouseIsDown = true;
+ setCursor(event.getX(), event.getY());
+ }
+
+ @Override
+ public void onMouseUp(MouseUpEvent event) {
+ event.preventDefault();
+ mouseIsDown = false;
+ setCursor(event.getX(), event.getY());
+
+ cursorX = event.getX();
+ cursorY = event.getY();
+ }
+
+ @Override
+ public void onMouseMove(MouseMoveEvent event) {
+ event.preventDefault();
+
+ if (mouseIsDown) {
+ setCursor(event.getX(), event.getY());
+ }
+ }
+
+ /**
+ * Sets the latest coordinates for pressed-down mouse cursor and updates the
+ * cross elements.
+ *
+ * @param x
+ * @param y
+ */
+ public void setCursor(int x, int y) {
+ cursorX = x;
+ cursorY = y;
+ if (x >= 0) {
+ lowercross.getElement().getStyle().setWidth(x, Unit.PX);
+ }
+ if (y >= 0) {
+ lowercross.getElement().getStyle().setTop(y, Unit.PX);
+ }
+ if (y >= 0) {
+ lowercross.getElement().getStyle().setHeight(height - y, Unit.PX);
+ }
+
+ if (x >= 0) {
+ highercross.getElement().getStyle().setWidth(width - x, Unit.PX);
+ }
+ if (x >= 0) {
+ highercross.getElement().getStyle().setLeft(x, Unit.PX);
+ }
+ if (y >= 0) {
+ highercross.getElement().getStyle().setHeight(y, Unit.PX);
+ }
+ }
+
+ @Override
+ public com.google.gwt.user.client.Element getSubPartElement(
+ String subPart) {
+ if (subPart.equals(CLICKLAYER_ID)) {
+ return clicklayer.getElement();
+ }
+
+ return null;
+ }
+
+ @Override
+ public String getSubPartName(
+ com.google.gwt.user.client.Element subElement) {
+ if (clicklayer.getElement().isOrHasChild(subElement)) {
+ return CLICKLAYER_ID;
+ }
+
+ return null;
+ }
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.client.ui.colorpicker;
+
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.event.dom.client.HasClickHandlers;
+import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.user.client.ui.AbsolutePanel;
+import com.google.gwt.user.client.ui.Grid;
+import com.google.gwt.user.client.ui.HTMLTable.Cell;
+
+/**
+ * Client side implementation for ColorPickerGrid.
+ *
+ * @since 7.0.0
+ *
+ */
+public class VColorPickerGrid extends AbsolutePanel
+ implements ClickHandler, HasClickHandlers {
+
+ private int rows = 1;
+ private int columns = 1;
+
+ private Grid grid;
+
+ private boolean gridLoaded = false;
+
+ private int selectedX;
+ private int selectedY;
+
+ /**
+ * Instantiates the client side component for a color picker grid.
+ */
+ public VColorPickerGrid() {
+ super();
+
+ this.add(createGrid(), 0, 0);
+ }
+
+ /**
+ * Creates a grid according to the current row and column count information.
+ *
+ * @return grid
+ */
+ private Grid createGrid() {
+ grid = new Grid(rows, columns);
+ grid.setWidth("100%");
+ grid.setHeight("100%");
+ grid.addClickHandler(this);
+ return grid;
+ }
+
+ /**
+ * Updates the row and column count and creates a new grid based on them.
+ * The new grid replaces the old grid if one existed.
+ *
+ * @param rowCount
+ * @param columnCount
+ */
+ protected void updateGrid(int rowCount, int columnCount) {
+ rows = rowCount;
+ columns = columnCount;
+ this.remove(grid);
+ this.add(createGrid(), 0, 0);
+ }
+
+ /**
+ * Updates the changed colors within the grid based on the given x- and
+ * y-coordinates. Nothing happens if any of the parameters is null or the
+ * parameter lengths don't match.
+ *
+ * @param changedColor
+ * @param changedX
+ * @param changedY
+ */
+ protected void updateColor(String[] changedColor, String[] changedX,
+ String[] changedY) {
+ if (changedColor != null && changedX != null && changedY != null) {
+ if (changedColor.length == changedX.length
+ && changedX.length == changedY.length) {
+ for (int c = 0; c < changedColor.length; c++) {
+ Element element = grid.getCellFormatter().getElement(
+ Integer.parseInt(changedX[c]),
+ Integer.parseInt(changedY[c]));
+ element.getStyle().setProperty("background",
+ changedColor[c]);
+ }
+ }
+
+ gridLoaded = true;
+ }
+ }
+
+ /**
+ * Returns currently selected x-coordinate of the grid.
+ */
+ protected int getSelectedX() {
+ return selectedX;
+ }
+
+ /**
+ * Returns currently selected y-coordinate of the grid.
+ */
+ protected int getSelectedY() {
+ return selectedY;
+ }
+
+ /**
+ * Returns true if the colors have been successfully updated at least once,
+ * false otherwise.
+ */
+ protected boolean isGridLoaded() {
+ return gridLoaded;
+ }
+
+ @Override
+ public void onClick(ClickEvent event) {
+ Cell cell = grid.getCellForEvent(event);
+ if (cell == null) {
+ return;
+ }
+
+ selectedY = cell.getRowIndex();
+ selectedX = cell.getCellIndex();
+ }
+
+ @Override
+ public HandlerRegistration addClickHandler(ClickHandler handler) {
+ return addDomHandler(handler, ClickEvent.getType());
+ }
+
+}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.client.ui.colorpicker;
-
-import com.google.gwt.event.dom.client.ClickHandler;
-import com.google.gwt.event.dom.client.HasClickHandlers;
-import com.vaadin.client.communication.StateChangeEvent;
-import com.vaadin.client.ui.AbstractComponentConnector;
-import com.vaadin.shared.ui.colorpicker.ColorPickerState;
-
-/**
- * An abstract class that defines default implementation for a color picker
- * connector.
- *
- * @since 7.0.0
- */
-public abstract class AbstractColorPickerConnector
- extends AbstractComponentConnector implements ClickHandler {
-
- private static final String DEFAULT_WIDTH_STYLE = "v-default-caption-width";
-
- @Override
- public ColorPickerState getState() {
- return (ColorPickerState) super.getState();
- }
-
- @Override
- public boolean delegateCaptionHandling() {
- return false;
- }
-
- @Override
- public void onStateChanged(StateChangeEvent stateChangeEvent) {
- // NOTE: this method is called after @DelegateToWidget
- super.onStateChanged(stateChangeEvent);
- if (stateChangeEvent.hasPropertyChanged("color")) {
- refreshColor();
-
- if (getState().showDefaultCaption && (getState().caption == null
- || "".equals(getState().caption))) {
-
- setCaption(getState().color);
- }
- }
- if (stateChangeEvent.hasPropertyChanged("caption")
- || stateChangeEvent.hasPropertyChanged("htmlContentAllowed")
- || stateChangeEvent.hasPropertyChanged("showDefaultCaption")) {
-
- setCaption(getCaption());
- refreshDefaultCaptionStyle();
- }
- }
-
- @Override
- public void init() {
- super.init();
- if (getWidget() instanceof HasClickHandlers) {
- ((HasClickHandlers) getWidget()).addClickHandler(this);
- }
- }
-
- /**
- * Get caption for the color picker widget.
- *
- * @return
- */
- protected String getCaption() {
- if (getState().showDefaultCaption && (getState().caption == null
- || "".equals(getState().caption))) {
- return getState().color;
- }
- return getState().caption;
- }
-
- /**
- * Add/remove default caption style.
- */
- protected void refreshDefaultCaptionStyle() {
- if (getState().showDefaultCaption
- && (getState().caption == null || getState().caption.isEmpty())
- && getState().width.isEmpty()) {
- getWidget().addStyleName(DEFAULT_WIDTH_STYLE);
- } else {
- getWidget().removeStyleName(DEFAULT_WIDTH_STYLE);
- }
- }
-
- /**
- * Set caption of the color picker widget.
- *
- * @param caption
- */
- protected abstract void setCaption(String caption);
-
- /**
- * Update the widget to show the currently selected color.
- */
- protected abstract void refreshColor();
-
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.client.ui.colorpicker;
-
-import com.google.gwt.core.client.GWT;
-import com.google.gwt.event.dom.client.ClickEvent;
-import com.google.gwt.user.client.ui.Widget;
-import com.vaadin.client.VCaption;
-import com.vaadin.client.communication.RpcProxy;
-import com.vaadin.client.ui.VColorPickerArea;
-import com.vaadin.shared.ui.Connect;
-import com.vaadin.shared.ui.Connect.LoadStyle;
-import com.vaadin.shared.ui.colorpicker.ColorPickerServerRpc;
-import com.vaadin.v7.ui.ColorPickerArea;
-
-/**
- * A class that defines an implementation for a color picker connector. Connects
- * the server side {@link com.vaadin.v7.ui.ColorPickerArea} with the client side
- * counterpart {@link VColorPickerArea}
- *
- * @since 7.0.0
- */
-@Connect(value = ColorPickerArea.class, loadStyle = LoadStyle.LAZY)
-public class ColorPickerAreaConnector extends AbstractColorPickerConnector {
-
- private ColorPickerServerRpc rpc = RpcProxy
- .create(ColorPickerServerRpc.class, this);
-
- @Override
- protected Widget createWidget() {
- return GWT.create(VColorPickerArea.class);
- }
-
- @Override
- public VColorPickerArea getWidget() {
- return (VColorPickerArea) super.getWidget();
- }
-
- @Override
- public void onClick(ClickEvent event) {
- rpc.openPopup(getWidget().isOpen());
- }
-
- @Override
- protected void setCaption(String caption) {
- VCaption.setCaptionText(getWidget(), getState());
- }
-
- @Override
- protected void refreshColor() {
- getWidget().refreshColor();
- }
-
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.client.ui.colorpicker;
-
-import com.google.gwt.core.client.GWT;
-import com.google.gwt.event.dom.client.ClickEvent;
-import com.google.gwt.user.client.ui.Widget;
-import com.vaadin.client.communication.RpcProxy;
-import com.vaadin.client.ui.VColorPicker;
-import com.vaadin.shared.ui.Connect;
-import com.vaadin.shared.ui.Connect.LoadStyle;
-import com.vaadin.shared.ui.colorpicker.ColorPickerServerRpc;
-import com.vaadin.v7.ui.ColorPicker;
-
-/**
- * A class that defines default implementation for a color picker connector.
- * Connects the server side {@link com.vaadin.v7.ui.ColorPicker} with the client
- * side counterpart {@link VColorPicker}
- *
- * @since 7.0.0
- */
-@Connect(value = ColorPicker.class, loadStyle = LoadStyle.LAZY)
-public class ColorPickerConnector extends AbstractColorPickerConnector {
-
- private ColorPickerServerRpc rpc = RpcProxy
- .create(ColorPickerServerRpc.class, this);
-
- @Override
- protected Widget createWidget() {
- return GWT.create(VColorPicker.class);
- }
-
- @Override
- public VColorPicker getWidget() {
- return (VColorPicker) super.getWidget();
- }
-
- @Override
- public void onClick(ClickEvent event) {
- rpc.openPopup(getWidget().isOpen());
- }
-
- @Override
- protected void setCaption(String caption) {
- if (getState().captionAsHtml) {
- getWidget().setHtml(caption);
- } else {
- getWidget().setText(caption);
- }
- }
-
- @Override
- protected void refreshColor() {
- getWidget().refreshColor();
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.client.ui.colorpicker;
-
-import com.google.gwt.core.client.GWT;
-import com.google.gwt.event.dom.client.MouseUpEvent;
-import com.google.gwt.event.dom.client.MouseUpHandler;
-import com.google.gwt.user.client.ui.Widget;
-import com.vaadin.client.communication.RpcProxy;
-import com.vaadin.client.communication.StateChangeEvent;
-import com.vaadin.client.ui.AbstractComponentConnector;
-import com.vaadin.shared.ui.Connect;
-import com.vaadin.shared.ui.Connect.LoadStyle;
-import com.vaadin.shared.ui.colorpicker.ColorPickerGradientServerRpc;
-import com.vaadin.shared.ui.colorpicker.ColorPickerGradientState;
-import com.vaadin.v7.ui.components.colorpicker.ColorPickerGradient;
-
-/**
- * A class that defines the default implementation for a color picker gradient
- * connector. Connects the server side
- * {@link com.vaadin.v7.ui.components.colorpicker.ColorPickerGradient} with the
- * client side counterpart {@link VColorPickerGradient}
- *
- * @since 7.0.0
- */
-@Connect(value = ColorPickerGradient.class, loadStyle = LoadStyle.LAZY)
-public class ColorPickerGradientConnector extends AbstractComponentConnector
- implements MouseUpHandler {
-
- private ColorPickerGradientServerRpc rpc = RpcProxy
- .create(ColorPickerGradientServerRpc.class, this);
-
- @Override
- protected Widget createWidget() {
- return GWT.create(VColorPickerGradient.class);
- }
-
- @Override
- public VColorPickerGradient getWidget() {
- return (VColorPickerGradient) super.getWidget();
- }
-
- @Override
- public ColorPickerGradientState getState() {
- return (ColorPickerGradientState) super.getState();
- }
-
- @Override
- public void onMouseUp(MouseUpEvent event) {
- rpc.select(getWidget().getCursorX(), getWidget().getCursorY());
- }
-
- @Override
- public void onStateChanged(StateChangeEvent stateChangeEvent) {
- super.onStateChanged(stateChangeEvent);
- if (stateChangeEvent.hasPropertyChanged("cursorX")
- || stateChangeEvent.hasPropertyChanged("cursorY")) {
-
- getWidget().setCursor(getState().cursorX, getState().cursorY);
- }
- if (stateChangeEvent.hasPropertyChanged("bgColor")) {
- getWidget().setBGColor(getState().bgColor);
- }
- }
-
- @Override
- protected void init() {
- super.init();
- getWidget().addMouseUpHandler(this);
- }
-
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.client.ui.colorpicker;
-
-import com.google.gwt.core.client.GWT;
-import com.google.gwt.event.dom.client.ClickEvent;
-import com.google.gwt.event.dom.client.ClickHandler;
-import com.google.gwt.user.client.ui.Widget;
-import com.vaadin.client.communication.RpcProxy;
-import com.vaadin.client.communication.StateChangeEvent;
-import com.vaadin.client.ui.AbstractComponentConnector;
-import com.vaadin.shared.ui.Connect;
-import com.vaadin.shared.ui.Connect.LoadStyle;
-import com.vaadin.shared.ui.colorpicker.ColorPickerGridServerRpc;
-import com.vaadin.shared.ui.colorpicker.ColorPickerGridState;
-import com.vaadin.v7.ui.components.colorpicker.ColorPickerGrid;
-
-/**
- * A class that defines the default implementation for a color picker grid
- * connector. Connects the server side
- * {@link com.vaadin.v7.ui.components.colorpicker.ColorPickerGrid} with the
- * client side counterpart {@link VColorPickerGrid}
- *
- * @since 7.0.0
- */
-@Connect(value = ColorPickerGrid.class, loadStyle = LoadStyle.LAZY)
-public class ColorPickerGridConnector extends AbstractComponentConnector
- implements ClickHandler {
-
- private ColorPickerGridServerRpc rpc = RpcProxy
- .create(ColorPickerGridServerRpc.class, this);
-
- @Override
- protected Widget createWidget() {
- return GWT.create(VColorPickerGrid.class);
- }
-
- @Override
- public VColorPickerGrid getWidget() {
- return (VColorPickerGrid) super.getWidget();
- }
-
- @Override
- public ColorPickerGridState getState() {
- return (ColorPickerGridState) super.getState();
- }
-
- @Override
- public void onClick(ClickEvent event) {
- rpc.select(getWidget().getSelectedX(), getWidget().getSelectedY());
- }
-
- @Override
- public void onStateChanged(StateChangeEvent stateChangeEvent) {
- super.onStateChanged(stateChangeEvent);
- if (stateChangeEvent.hasPropertyChanged("rowCount")
- || stateChangeEvent.hasPropertyChanged("columnCount")
- || stateChangeEvent.hasPropertyChanged("updateGrid")) {
-
- getWidget().updateGrid(getState().rowCount, getState().columnCount);
- }
- if (stateChangeEvent.hasPropertyChanged("changedX")
- || stateChangeEvent.hasPropertyChanged("changedY")
- || stateChangeEvent.hasPropertyChanged("changedColor")
- || stateChangeEvent.hasPropertyChanged("updateColor")) {
-
- getWidget().updateColor(getState().changedColor,
- getState().changedX, getState().changedY);
-
- if (!getWidget().isGridLoaded()) {
- rpc.refresh();
- }
- }
- }
-
- @Override
- protected void init() {
- super.init();
- getWidget().addClickHandler(this);
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.client.ui.colorpicker;
-
-import com.google.gwt.dom.client.Style.Unit;
-import com.google.gwt.event.dom.client.MouseDownEvent;
-import com.google.gwt.event.dom.client.MouseDownHandler;
-import com.google.gwt.event.dom.client.MouseMoveEvent;
-import com.google.gwt.event.dom.client.MouseMoveHandler;
-import com.google.gwt.event.dom.client.MouseUpEvent;
-import com.google.gwt.event.dom.client.MouseUpHandler;
-import com.google.gwt.user.client.ui.AbsolutePanel;
-import com.google.gwt.user.client.ui.FocusPanel;
-import com.google.gwt.user.client.ui.HTML;
-import com.vaadin.client.ui.SubPartAware;
-
-/**
- * Client side implementation for ColorPickerGradient.
- *
- * @since 7.0.0
- *
- */
-public class VColorPickerGradient extends FocusPanel implements
- MouseDownHandler, MouseUpHandler, MouseMoveHandler, SubPartAware {
-
- /** Set the CSS class name to allow styling. */
- public static final String CLASSNAME = "v-colorpicker-gradient";
- public static final String CLASSNAME_BACKGROUND = CLASSNAME + "-background";
- public static final String CLASSNAME_FOREGROUND = CLASSNAME + "-foreground";
- public static final String CLASSNAME_LOWERBOX = CLASSNAME + "-lowerbox";
- public static final String CLASSNAME_HIGHERBOX = CLASSNAME + "-higherbox";
- public static final String CLASSNAME_CONTAINER = CLASSNAME + "-container";
- public static final String CLASSNAME_CLICKLAYER = CLASSNAME + "-clicklayer";
- private static final String CLICKLAYER_ID = "clicklayer";
-
- private final HTML background;
- private final HTML foreground;
- private final HTML lowercross;
- private final HTML highercross;
- private final HTML clicklayer;
- private final AbsolutePanel container;
-
- private boolean mouseIsDown = false;
-
- private int cursorX;
- private int cursorY;
-
- private int width = 220;
- private int height = 220;
-
- /**
- * Instantiates the client side component for a color picker gradient.
- */
- public VColorPickerGradient() {
- super();
-
- setStyleName(CLASSNAME);
-
- background = new HTML();
- background.setStyleName(CLASSNAME_BACKGROUND);
- background.setPixelSize(width, height);
-
- foreground = new HTML();
- foreground.setStyleName(CLASSNAME_FOREGROUND);
- foreground.setPixelSize(width, height);
-
- clicklayer = new HTML();
- clicklayer.setStyleName(CLASSNAME_CLICKLAYER);
- clicklayer.setPixelSize(width, height);
- clicklayer.addMouseDownHandler(this);
- clicklayer.addMouseUpHandler(this);
- clicklayer.addMouseMoveHandler(this);
-
- lowercross = new HTML();
- lowercross.setPixelSize(width / 2, height / 2);
- lowercross.setStyleName(CLASSNAME_LOWERBOX);
-
- highercross = new HTML();
- highercross.setPixelSize(width / 2, height / 2);
- highercross.setStyleName(CLASSNAME_HIGHERBOX);
-
- container = new AbsolutePanel();
- container.setStyleName(CLASSNAME_CONTAINER);
- container.setPixelSize(width, height);
- container.add(background, 0, 0);
- container.add(foreground, 0, 0);
- container.add(lowercross, 0, height / 2);
- container.add(highercross, width / 2, 0);
- container.add(clicklayer, 0, 0);
-
- add(container);
- }
-
- /**
- * Returns the latest x-coordinate for pressed-down mouse cursor.
- */
- protected int getCursorX() {
- return cursorX;
- }
-
- /**
- * Returns the latest y-coordinate for pressed-down mouse cursor.
- */
- protected int getCursorY() {
- return cursorY;
- }
-
- /**
- * Sets the given css color as the background.
- *
- * @param bgColor
- */
- protected void setBGColor(String bgColor) {
- if (bgColor == null) {
- background.getElement().getStyle().clearBackgroundColor();
- } else {
- background.getElement().getStyle().setBackgroundColor(bgColor);
- }
- }
-
- @Override
- public void onMouseDown(MouseDownEvent event) {
- event.preventDefault();
-
- mouseIsDown = true;
- setCursor(event.getX(), event.getY());
- }
-
- @Override
- public void onMouseUp(MouseUpEvent event) {
- event.preventDefault();
- mouseIsDown = false;
- setCursor(event.getX(), event.getY());
-
- cursorX = event.getX();
- cursorY = event.getY();
- }
-
- @Override
- public void onMouseMove(MouseMoveEvent event) {
- event.preventDefault();
-
- if (mouseIsDown) {
- setCursor(event.getX(), event.getY());
- }
- }
-
- /**
- * Sets the latest coordinates for pressed-down mouse cursor and updates the
- * cross elements.
- *
- * @param x
- * @param y
- */
- public void setCursor(int x, int y) {
- cursorX = x;
- cursorY = y;
- if (x >= 0) {
- lowercross.getElement().getStyle().setWidth(x, Unit.PX);
- }
- if (y >= 0) {
- lowercross.getElement().getStyle().setTop(y, Unit.PX);
- }
- if (y >= 0) {
- lowercross.getElement().getStyle().setHeight(height - y, Unit.PX);
- }
-
- if (x >= 0) {
- highercross.getElement().getStyle().setWidth(width - x, Unit.PX);
- }
- if (x >= 0) {
- highercross.getElement().getStyle().setLeft(x, Unit.PX);
- }
- if (y >= 0) {
- highercross.getElement().getStyle().setHeight(y, Unit.PX);
- }
- }
-
- @Override
- public com.google.gwt.user.client.Element getSubPartElement(
- String subPart) {
- if (subPart.equals(CLICKLAYER_ID)) {
- return clicklayer.getElement();
- }
-
- return null;
- }
-
- @Override
- public String getSubPartName(
- com.google.gwt.user.client.Element subElement) {
- if (clicklayer.getElement().isOrHasChild(subElement)) {
- return CLICKLAYER_ID;
- }
-
- return null;
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.client.ui.colorpicker;
-
-import com.google.gwt.dom.client.Element;
-import com.google.gwt.event.dom.client.ClickEvent;
-import com.google.gwt.event.dom.client.ClickHandler;
-import com.google.gwt.event.dom.client.HasClickHandlers;
-import com.google.gwt.event.shared.HandlerRegistration;
-import com.google.gwt.user.client.ui.AbsolutePanel;
-import com.google.gwt.user.client.ui.Grid;
-import com.google.gwt.user.client.ui.HTMLTable.Cell;
-
-/**
- * Client side implementation for ColorPickerGrid.
- *
- * @since 7.0.0
- *
- */
-public class VColorPickerGrid extends AbsolutePanel
- implements ClickHandler, HasClickHandlers {
-
- private int rows = 1;
- private int columns = 1;
-
- private Grid grid;
-
- private boolean gridLoaded = false;
-
- private int selectedX;
- private int selectedY;
-
- /**
- * Instantiates the client side component for a color picker grid.
- */
- public VColorPickerGrid() {
- super();
-
- this.add(createGrid(), 0, 0);
- }
-
- /**
- * Creates a grid according to the current row and column count information.
- *
- * @return grid
- */
- private Grid createGrid() {
- grid = new Grid(rows, columns);
- grid.setWidth("100%");
- grid.setHeight("100%");
- grid.addClickHandler(this);
- return grid;
- }
-
- /**
- * Updates the row and column count and creates a new grid based on them.
- * The new grid replaces the old grid if one existed.
- *
- * @param rowCount
- * @param columnCount
- */
- protected void updateGrid(int rowCount, int columnCount) {
- rows = rowCount;
- columns = columnCount;
- this.remove(grid);
- this.add(createGrid(), 0, 0);
- }
-
- /**
- * Updates the changed colors within the grid based on the given x- and
- * y-coordinates. Nothing happens if any of the parameters is null or the
- * parameter lengths don't match.
- *
- * @param changedColor
- * @param changedX
- * @param changedY
- */
- protected void updateColor(String[] changedColor, String[] changedX,
- String[] changedY) {
- if (changedColor != null && changedX != null && changedY != null) {
- if (changedColor.length == changedX.length
- && changedX.length == changedY.length) {
- for (int c = 0; c < changedColor.length; c++) {
- Element element = grid.getCellFormatter().getElement(
- Integer.parseInt(changedX[c]),
- Integer.parseInt(changedY[c]));
- element.getStyle().setProperty("background",
- changedColor[c]);
- }
- }
-
- gridLoaded = true;
- }
- }
-
- /**
- * Returns currently selected x-coordinate of the grid.
- */
- protected int getSelectedX() {
- return selectedX;
- }
-
- /**
- * Returns currently selected y-coordinate of the grid.
- */
- protected int getSelectedY() {
- return selectedY;
- }
-
- /**
- * Returns true if the colors have been successfully updated at least once,
- * false otherwise.
- */
- protected boolean isGridLoaded() {
- return gridLoaded;
- }
-
- @Override
- public void onClick(ClickEvent event) {
- Cell cell = grid.getCellForEvent(event);
- if (cell == null) {
- return;
- }
-
- selectedY = cell.getRowIndex();
- selectedX = cell.getCellIndex();
- }
-
- @Override
- public HandlerRegistration addClickHandler(ClickHandler handler) {
- return addDomHandler(handler, ClickEvent.getType());
- }
-
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.ui;
-
-import java.io.Serializable;
-import java.lang.reflect.Method;
-import java.util.Collection;
-
-import org.jsoup.nodes.Attributes;
-import org.jsoup.nodes.Element;
-
-import com.vaadin.shared.ui.colorpicker.Color;
-import com.vaadin.shared.ui.colorpicker.ColorPickerServerRpc;
-import com.vaadin.shared.ui.colorpicker.ColorPickerState;
-import com.vaadin.ui.AbstractComponent;
-import com.vaadin.ui.UI;
-import com.vaadin.ui.Window.CloseEvent;
-import com.vaadin.ui.Window.CloseListener;
-import com.vaadin.ui.declarative.DesignAttributeHandler;
-import com.vaadin.ui.declarative.DesignContext;
-import com.vaadin.v7.ui.components.colorpicker.ColorChangeEvent;
-import com.vaadin.v7.ui.components.colorpicker.ColorChangeListener;
-import com.vaadin.v7.ui.components.colorpicker.ColorPickerPopup;
-import com.vaadin.v7.ui.components.colorpicker.ColorSelector;
-
-/**
- * An abstract class that defines default implementation for a color picker
- * component.
- *
- * @since 7.0.0
- */
-public abstract class AbstractColorPicker extends AbstractComponent
- implements CloseListener, ColorSelector {
- private static final Method COLOR_CHANGE_METHOD;
- static {
- try {
- COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
- "colorChanged", new Class[] { ColorChangeEvent.class });
- } catch (final java.lang.NoSuchMethodException e) {
- // This should never happen
- throw new java.lang.RuntimeException(
- "Internal error finding methods in ColorPicker");
- }
- }
-
- /**
- * Interface for converting 2d-coordinates to a Color
- */
- public interface Coordinates2Color extends Serializable {
-
- /**
- * Calculate color from coordinates
- *
- * @param x
- * the x-coordinate
- * @param y
- * the y-coordinate
- *
- * @return the color
- */
- public Color calculate(int x, int y);
-
- /**
- * Calculate coordinates from color
- *
- * @param c
- * the c
- *
- * @return the integer array with the coordinates
- */
- public int[] calculate(Color c);
- }
-
- public enum PopupStyle {
- POPUP_NORMAL("normal"), POPUP_SIMPLE("simple");
-
- private String style;
-
- PopupStyle(String styleName) {
- style = styleName;
- }
-
- @Override
- public String toString() {
- return style;
- }
- }
-
- private ColorPickerServerRpc rpc = new ColorPickerServerRpc() {
-
- @Override
- public void openPopup(boolean open) {
- showPopup(open);
- }
- };
-
- protected static final String STYLENAME_DEFAULT = "v-colorpicker";
- protected static final String STYLENAME_BUTTON = "v-button";
- protected static final String STYLENAME_AREA = "v-colorpicker-area";
-
- protected PopupStyle popupStyle = PopupStyle.POPUP_NORMAL;
-
- /** The popup window. */
- private ColorPickerPopup window;
-
- /** The color. */
- protected Color color;
-
- /** The UI. */
- private UI parent;
-
- protected String popupCaption = null;
- private int positionX = 0;
- private int positionY = 0;
-
- protected boolean rgbVisible = true;
- protected boolean hsvVisible = true;
- protected boolean swatchesVisible = true;
- protected boolean historyVisible = true;
- protected boolean textfieldVisible = true;
-
- /**
- * Instantiates a new color picker.
- */
- public AbstractColorPicker() {
- this("Colors", Color.WHITE);
- }
-
- /**
- * Instantiates a new color picker.
- *
- * @param popupCaption
- * the caption of the popup window
- */
- public AbstractColorPicker(String popupCaption) {
- this(popupCaption, Color.WHITE);
- }
-
- /**
- * Instantiates a new color picker.
- *
- * @param popupCaption
- * the caption of the popup window
- * @param initialColor
- * the initial color
- */
- public AbstractColorPicker(String popupCaption, Color initialColor) {
- super();
- registerRpc(rpc);
- setColor(initialColor);
- this.popupCaption = popupCaption;
- setDefaultStyles();
- setCaption("");
- }
-
- @Override
- public void setColor(Color color) {
- this.color = color;
-
- if (window != null) {
- window.setColor(color);
- }
- getState().color = color.getCSS();
- }
-
- @Override
- public Color getColor() {
- return color;
- }
-
- /**
- * Set true if the component should show a default caption (css-code for the
- * currently selected color, e.g. #ffffff) when no other caption is
- * available.
- *
- * @param enabled
- */
- public void setDefaultCaptionEnabled(boolean enabled) {
- getState().showDefaultCaption = enabled;
- }
-
- /**
- * Returns true if the component shows the default caption (css-code for the
- * currently selected color, e.g. #ffffff) if no other caption is available.
- */
- public boolean isDefaultCaptionEnabled() {
- return getState(false).showDefaultCaption;
- }
-
- /**
- * Sets the position of the popup window
- *
- * @param x
- * the x-coordinate
- * @param y
- * the y-coordinate
- */
- public void setPosition(int x, int y) {
- positionX = x;
- positionY = y;
-
- if (window != null) {
- window.setPositionX(x);
- window.setPositionY(y);
- }
- }
-
- @Override
- public void addColorChangeListener(ColorChangeListener listener) {
- addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
- }
-
- @Override
- public void removeColorChangeListener(ColorChangeListener listener) {
- removeListener(ColorChangeEvent.class, listener);
- }
-
- @Override
- public void windowClose(CloseEvent e) {
- if (e.getWindow() == window) {
- getState().popupVisible = false;
- }
- }
-
- /**
- * Fired when a color change event occurs
- *
- * @param event
- * The color change event
- */
- protected void colorChanged(ColorChangeEvent event) {
- setColor(event.getColor());
- fireColorChanged();
- }
-
- /**
- * Notifies the listeners that the selected color has changed
- */
- public void fireColorChanged() {
- fireEvent(new ColorChangeEvent(this, color));
- }
-
- /**
- * The style for the popup window
- *
- * @param style
- * The style
- */
- public void setPopupStyle(PopupStyle style) {
- popupStyle = style;
-
- switch (style) {
- case POPUP_NORMAL: {
- setRGBVisibility(true);
- setHSVVisibility(true);
- setSwatchesVisibility(true);
- setHistoryVisibility(true);
- setTextfieldVisibility(true);
- break;
- }
-
- case POPUP_SIMPLE: {
- setRGBVisibility(false);
- setHSVVisibility(false);
- setSwatchesVisibility(true);
- setHistoryVisibility(false);
- setTextfieldVisibility(false);
- break;
- }
- }
- }
-
- /**
- * Gets the style for the popup window
- *
- * @since 7.5.0
- * @return popup window style
- */
- public PopupStyle getPopupStyle() {
- return popupStyle;
- }
-
- /**
- * Set the visibility of the RGB Tab
- *
- * @param visible
- * The visibility
- */
- public void setRGBVisibility(boolean visible) {
-
- if (!visible && !hsvVisible && !swatchesVisible) {
- throw new IllegalArgumentException("Cannot hide all tabs.");
- }
-
- rgbVisible = visible;
- if (window != null) {
- window.setRGBTabVisible(visible);
- }
- }
-
- /**
- * Gets the visibility of the RGB Tab
- *
- * @since 7.5.0
- * @return visibility of the RGB tab
- */
- public boolean getRGBVisibility() {
- return rgbVisible;
- }
-
- /**
- * Set the visibility of the HSV Tab
- *
- * @param visible
- * The visibility
- */
- public void setHSVVisibility(boolean visible) {
- if (!visible && !rgbVisible && !swatchesVisible) {
- throw new IllegalArgumentException("Cannot hide all tabs.");
- }
-
- hsvVisible = visible;
- if (window != null) {
- window.setHSVTabVisible(visible);
- }
- }
-
- /**
- * Gets the visibility of the HSV Tab
- *
- * @since 7.5.0
- * @return visibility of the HSV tab
- */
- public boolean getHSVVisibility() {
- return hsvVisible;
- }
-
- /**
- * Set the visibility of the Swatches Tab
- *
- * @param visible
- * The visibility
- */
- public void setSwatchesVisibility(boolean visible) {
- if (!visible && !hsvVisible && !rgbVisible) {
- throw new IllegalArgumentException("Cannot hide all tabs.");
- }
-
- swatchesVisible = visible;
- if (window != null) {
- window.setSwatchesTabVisible(visible);
- }
- }
-
- /**
- * Gets the visibility of the Swatches Tab
- *
- * @since 7.5.0
- * @return visibility of the swatches tab
- */
- public boolean getSwatchesVisibility() {
- return swatchesVisible;
- }
-
- /**
- * Sets the visibility of the Color History
- *
- * @param visible
- * The visibility
- */
- public void setHistoryVisibility(boolean visible) {
- historyVisible = visible;
- if (window != null) {
- window.setHistoryVisible(visible);
- }
- }
-
- /**
- * Gets the visibility of the Color History
- *
- * @since 7.5.0
- * @return visibility of color history
- */
- public boolean getHistoryVisibility() {
- return historyVisible;
- }
-
- /**
- * Sets the visibility of the CSS color code text field
- *
- * @param visible
- * The visibility
- */
- public void setTextfieldVisibility(boolean visible) {
- textfieldVisible = visible;
- if (window != null) {
- window.setPreviewVisible(visible);
- }
- }
-
- /**
- * Gets the visibility of CSS color code text field
- *
- * @since 7.5.0
- * @return visibility of css color code text field
- */
- public boolean getTextfieldVisibility() {
- return textfieldVisible;
- }
-
- @Override
- protected ColorPickerState getState() {
- return (ColorPickerState) super.getState();
- }
-
- @Override
- protected ColorPickerState getState(boolean markAsDirty) {
- return (ColorPickerState) super.getState(markAsDirty);
- }
-
- /**
- * Sets the default styles of the component
- *
- */
- abstract protected void setDefaultStyles();
-
- /**
- * Shows a popup-window for color selection.
- */
- public void showPopup() {
- showPopup(true);
- }
-
- /**
- * Hides a popup-window for color selection.
- */
- public void hidePopup() {
- showPopup(false);
- }
-
- /**
- * Shows or hides popup-window depending on the given parameter. If there is
- * no such window yet, one is created.
- *
- * @param open
- */
- protected void showPopup(boolean open) {
- if (open && !isReadOnly()) {
- if (parent == null) {
- parent = getUI();
- }
-
- if (window == null) {
-
- // Create the popup
- window = new ColorPickerPopup(color);
- window.setCaption(popupCaption);
-
- window.setRGBTabVisible(rgbVisible);
- window.setHSVTabVisible(hsvVisible);
- window.setSwatchesTabVisible(swatchesVisible);
- window.setHistoryVisible(historyVisible);
- window.setPreviewVisible(textfieldVisible);
-
- window.setImmediate(true);
- window.addCloseListener(this);
- window.addColorChangeListener(new ColorChangeListener() {
- @Override
- public void colorChanged(ColorChangeEvent event) {
- AbstractColorPicker.this.colorChanged(event);
- }
- });
-
- window.getHistory().setColor(color);
- parent.addWindow(window);
- window.setVisible(true);
- window.setPositionX(positionX);
- window.setPositionY(positionY);
-
- } else if (!parent.equals(window.getParent())) {
-
- window.setRGBTabVisible(rgbVisible);
- window.setHSVTabVisible(hsvVisible);
- window.setSwatchesTabVisible(swatchesVisible);
- window.setHistoryVisible(historyVisible);
- window.setPreviewVisible(textfieldVisible);
-
- window.setColor(color);
- window.getHistory().setColor(color);
- window.setVisible(true);
- parent.addWindow(window);
- }
-
- } else if (window != null) {
- window.setVisible(false);
- parent.removeWindow(window);
- }
- getState().popupVisible = open;
- }
-
- /**
- * Set whether the caption text is rendered as HTML or not. You might need
- * to re-theme component to allow higher content than the original text
- * style.
- *
- * If set to true, the captions are passed to the browser as html and the
- * developer is responsible for ensuring no harmful html is used. If set to
- * false, the content is passed to the browser as plain text.
- *
- * @param htmlContentAllowed
- * <code>true</code> if caption is rendered as HTML,
- * <code>false</code> otherwise
- * @deprecated as of , use {@link #setCaptionAsHtml(boolean)} instead
- */
- @Deprecated
- public void setHtmlContentAllowed(boolean htmlContentAllowed) {
- setCaptionAsHtml(htmlContentAllowed);
- }
-
- /**
- * Return HTML rendering setting
- *
- * @return <code>true</code> if the caption text is to be rendered as HTML,
- * <code>false</code> otherwise
- * @deprecated as of , use {@link #isCaptionAsHtml()} instead
- */
- @Deprecated
- public boolean isHtmlContentAllowed() {
- return isCaptionAsHtml();
- }
-
- @Override
- public void readDesign(Element design, DesignContext designContext) {
- super.readDesign(design, designContext);
-
- Attributes attributes = design.attributes();
- if (design.hasAttr("color")) {
- // Ignore the # character
- String hexColor = DesignAttributeHandler
- .readAttribute("color", attributes, String.class)
- .substring(1);
- setColor(new Color(Integer.parseInt(hexColor, 16)));
- }
- if (design.hasAttr("popup-style")) {
- setPopupStyle(PopupStyle.valueOf(
- "POPUP_" + attributes.get("popup-style").toUpperCase()));
- }
- if (design.hasAttr("position")) {
- String[] position = attributes.get("position").split(",");
- setPosition(Integer.parseInt(position[0]),
- Integer.parseInt(position[1]));
- }
- }
-
- @Override
- public void writeDesign(Element design, DesignContext designContext) {
- super.writeDesign(design, designContext);
-
- Attributes attribute = design.attributes();
- DesignAttributeHandler.writeAttribute("color", attribute,
- color.getCSS(), Color.WHITE.getCSS(), String.class);
- DesignAttributeHandler.writeAttribute("popup-style", attribute,
- (popupStyle == PopupStyle.POPUP_NORMAL ? "normal" : "simple"),
- "normal", String.class);
- DesignAttributeHandler.writeAttribute("position", attribute,
- positionX + "," + positionY, "0,0", String.class);
- }
-
- @Override
- protected Collection<String> getCustomAttributes() {
- Collection<String> result = super.getCustomAttributes();
- result.add("color");
- result.add("position");
- result.add("popup-style");
- return result;
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.ui;
-
-import com.vaadin.shared.ui.colorpicker.Color;
-
-/**
- * A class that defines default (button-like) implementation for a color picker
- * component.
- *
- * @since 7.0.0
- *
- * @see ColorPickerArea
- *
- */
-public class ColorPicker extends AbstractColorPicker {
-
- /**
- * Instantiates a new color picker.
- */
- public ColorPicker() {
- super();
- }
-
- /**
- * Instantiates a new color picker.
- *
- * @param popupCaption
- * caption of the color select popup
- */
- public ColorPicker(String popupCaption) {
- super(popupCaption);
- }
-
- /**
- * Instantiates a new color picker.
- *
- * @param popupCaption
- * caption of the color select popup
- * @param initialColor
- * the initial color
- */
- public ColorPicker(String popupCaption, Color initialColor) {
- super(popupCaption, initialColor);
- setDefaultCaptionEnabled(true);
- }
-
- @Override
- protected void setDefaultStyles() {
- setPrimaryStyleName(STYLENAME_BUTTON);
- addStyleName(STYLENAME_DEFAULT);
- }
-
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.ui;
-
-import com.vaadin.shared.ui.colorpicker.Color;
-
-/**
- * A class that defines area-like implementation for a color picker component.
- *
- * @since 7.0.0
- *
- * @see ColorPicker
- *
- */
-public class ColorPickerArea extends AbstractColorPicker {
-
- /**
- * Instantiates a new color picker.
- */
- public ColorPickerArea() {
- super();
- }
-
- /**
- * Instantiates a new color picker.
- *
- * @param popupCaption
- * caption of the color select popup
- */
- public ColorPickerArea(String popupCaption) {
- super(popupCaption);
- }
-
- /**
- * Instantiates a new color picker.
- *
- * @param popupCaption
- * caption of the color select popup
- * @param initialColor
- * the initial color
- */
- public ColorPickerArea(String popupCaption, Color initialColor) {
- super(popupCaption, initialColor);
- setDefaultCaptionEnabled(false);
- }
-
- @Override
- protected void setDefaultStyles() {
- // state already has correct default
- }
-
- @Override
- public void beforeClientResponse(boolean initial) {
- super.beforeClientResponse(initial);
-
- if ("".equals(getState().height)) {
- getState().height = "30px";
- }
- if ("".equals(getState().width)) {
- getState().width = "30px";
- }
- }
-
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.ui.components.colorpicker;
-
-import com.vaadin.shared.ui.colorpicker.Color;
-import com.vaadin.ui.Component;
-import com.vaadin.ui.Component.Event;
-
-/**
- * The color changed event which is passed to the listeners when a color change
- * occurs.
- *
- * @since 7.0.0
- */
-public class ColorChangeEvent extends Event {
- private final Color color;
-
- public ColorChangeEvent(Component source, Color color) {
- super(source);
-
- this.color = color;
- }
-
- /**
- * Returns the new color.
- */
- public Color getColor() {
- return color;
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.ui.components.colorpicker;
-
-import java.io.Serializable;
-
-/**
- * The listener interface for receiving colorChange events. The class that is
- * interested in processing a {@link ColorChangeEvent} implements this
- * interface, and the object created with that class is registered with a
- * component using the component's <code>addColorChangeListener</code> method.
- * When the colorChange event occurs, that object's appropriate method is
- * invoked.
- *
- * @since 7.0.0
- *
- * @see ColorChangeEvent
- */
-public interface ColorChangeListener extends Serializable {
-
- /**
- * Called when a new color has been selected.
- *
- * @param event
- * An event containing information about the color change.
- */
- void colorChanged(ColorChangeEvent event);
-
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.ui.components.colorpicker;
-
-import java.lang.reflect.Method;
-
-import com.vaadin.shared.ui.colorpicker.Color;
-import com.vaadin.shared.ui.colorpicker.ColorPickerGradientServerRpc;
-import com.vaadin.shared.ui.colorpicker.ColorPickerGradientState;
-import com.vaadin.ui.AbstractComponent;
-import com.vaadin.v7.ui.AbstractColorPicker.Coordinates2Color;
-
-/**
- * A component that represents a color gradient within a color picker.
- *
- * @since 7.0.0
- */
-public class ColorPickerGradient extends AbstractComponent
- implements ColorSelector {
-
- private static final Method COLOR_CHANGE_METHOD;
- static {
- try {
- COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
- "colorChanged", new Class[] { ColorChangeEvent.class });
- } catch (final java.lang.NoSuchMethodException e) {
- // This should never happen
- throw new java.lang.RuntimeException(
- "Internal error finding methods in ColorPicker");
- }
- }
-
- private ColorPickerGradientServerRpc rpc = new ColorPickerGradientServerRpc() {
-
- @Override
- public void select(int cursorX, int cursorY) {
- x = cursorX;
- y = cursorY;
- color = converter.calculate(x, y);
-
- fireColorChanged(color);
- }
- };
-
- /** The converter. */
- private Coordinates2Color converter;
-
- /** The foreground color. */
- private Color color;
-
- /** The x-coordinate. */
- private int x = 0;
-
- /** The y-coordinate. */
- private int y = 0;
-
- private ColorPickerGradient() {
- registerRpc(rpc);
- // width and height must be set here instead of in theme, otherwise
- // coordinate calculations fail
- getState().width = "220px";
- getState().height = "220px";
- }
-
- /**
- * Instantiates a new color picker gradient.
- *
- * @param id
- * the id
- * @param converter
- * the converter
- */
- public ColorPickerGradient(String id, Coordinates2Color converter) {
- this();
- addStyleName(id);
- this.converter = converter;
- }
-
- @Override
- public void setColor(Color c) {
- color = c;
-
- int[] coords = converter.calculate(c);
- x = coords[0];
- y = coords[1];
-
- getState().cursorX = x;
- getState().cursorY = y;
-
- }
-
- @Override
- public void addColorChangeListener(ColorChangeListener listener) {
- addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
- }
-
- @Override
- public void removeColorChangeListener(ColorChangeListener listener) {
- removeListener(ColorChangeEvent.class, listener);
- }
-
- /**
- * Sets the background color.
- *
- * @param color
- * the new background color
- */
- public void setBackgroundColor(Color color) {
- getState().bgColor = color.getCSS();
- }
-
- @Override
- public Color getColor() {
- return color;
- }
-
- /**
- * Notifies the listeners that the color has changed
- *
- * @param color
- * The color which it changed to
- */
- public void fireColorChanged(Color color) {
- fireEvent(new ColorChangeEvent(this, color));
- }
-
- @Override
- protected ColorPickerGradientState getState() {
- return (ColorPickerGradientState) super.getState();
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.ui.components.colorpicker;
-
-import java.awt.Point;
-import java.lang.reflect.Method;
-import java.util.HashMap;
-import java.util.Map;
-
-import com.vaadin.shared.ui.colorpicker.Color;
-import com.vaadin.shared.ui.colorpicker.ColorPickerGridServerRpc;
-import com.vaadin.shared.ui.colorpicker.ColorPickerGridState;
-import com.vaadin.ui.AbstractComponent;
-
-/**
- * A component that represents a color selection grid within a color picker.
- *
- * @since 7.0.0
- */
-public class ColorPickerGrid extends AbstractComponent
- implements ColorSelector {
-
- private static final String STYLENAME = "v-colorpicker-grid";
-
- private static final Method COLOR_CHANGE_METHOD;
- static {
- try {
- COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
- "colorChanged", new Class[] { ColorChangeEvent.class });
- } catch (final java.lang.NoSuchMethodException e) {
- // This should never happen
- throw new java.lang.RuntimeException(
- "Internal error finding methods in ColorPicker");
- }
- }
-
- private ColorPickerGridServerRpc rpc = new ColorPickerGridServerRpc() {
-
- @Override
- public void select(int x, int y) {
- ColorPickerGrid.this.x = x;
- ColorPickerGrid.this.y = y;
-
- fireColorChanged(colorGrid[y][x]);
- }
-
- @Override
- public void refresh() {
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < columns; col++) {
- changedColors.put(new Point(row, col), colorGrid[row][col]);
- }
- }
- sendChangedColors();
- markAsDirty();
- }
- };
-
- /** The x-coordinate. */
- private int x = 0;
-
- /** The y-coordinate. */
- private int y = 0;
-
- /** The rows. */
- private int rows;
-
- /** The columns. */
- private int columns;
-
- /** The color grid. */
- private Color[][] colorGrid = new Color[1][1];
-
- /** The changed colors. */
- private final Map<Point, Color> changedColors = new HashMap<Point, Color>();
-
- /**
- * Instantiates a new color picker grid.
- */
- public ColorPickerGrid() {
- registerRpc(rpc);
- setPrimaryStyleName(STYLENAME);
- setColorGrid(new Color[1][1]);
- setColor(Color.WHITE);
- }
-
- /**
- * Instantiates a new color picker grid.
- *
- * @param rows
- * the rows
- * @param columns
- * the columns
- */
- public ColorPickerGrid(int rows, int columns) {
- registerRpc(rpc);
- setPrimaryStyleName(STYLENAME);
- setColorGrid(new Color[rows][columns]);
- setColor(Color.WHITE);
- }
-
- /**
- * Instantiates a new color picker grid.
- *
- * @param colors
- * the colors
- */
- public ColorPickerGrid(Color[][] colors) {
- registerRpc(rpc);
- setPrimaryStyleName(STYLENAME);
- setColorGrid(colors);
- }
-
- private void setColumnCount(int columns) {
- this.columns = columns;
- getState().columnCount = columns;
- }
-
- private void setRowCount(int rows) {
- this.rows = rows;
- getState().rowCount = rows;
- }
-
- private void sendChangedColors() {
- if (!changedColors.isEmpty()) {
- String[] colors = new String[changedColors.size()];
- String[] XCoords = new String[changedColors.size()];
- String[] YCoords = new String[changedColors.size()];
- int counter = 0;
- for (Point p : changedColors.keySet()) {
- Color c = changedColors.get(p);
- if (c == null) {
- continue;
- }
-
- String color = c.getCSS();
-
- colors[counter] = color;
- XCoords[counter] = String.valueOf((int) p.getX());
- YCoords[counter] = String.valueOf((int) p.getY());
- counter++;
- }
- getState().changedColor = colors;
- getState().changedX = XCoords;
- getState().changedY = YCoords;
-
- changedColors.clear();
- }
- }
-
- /**
- * Sets the color grid.
- *
- * @param colors
- * the new color grid
- */
- public void setColorGrid(Color[][] colors) {
- setRowCount(colors.length);
- setColumnCount(colors[0].length);
- colorGrid = colors;
-
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < columns; col++) {
- changedColors.put(new Point(row, col), colorGrid[row][col]);
- }
- }
- sendChangedColors();
-
- markAsDirty();
- }
-
- /**
- * Adds a color change listener
- *
- * @param listener
- * The color change listener
- */
- @Override
- public void addColorChangeListener(ColorChangeListener listener) {
- addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
- }
-
- @Override
- public Color getColor() {
- return colorGrid[x][y];
- }
-
- /**
- * Removes a color change listener
- *
- * @param listener
- * The listener
- */
- @Override
- public void removeColorChangeListener(ColorChangeListener listener) {
- removeListener(ColorChangeEvent.class, listener);
- }
-
- @Override
- public void setColor(Color color) {
- colorGrid[x][y] = color;
- changedColors.put(new Point(x, y), color);
- sendChangedColors();
- markAsDirty();
- }
-
- /**
- * Sets the position.
- *
- * @param x
- * the x
- * @param y
- * the y
- */
- public void setPosition(int x, int y) {
- if (x >= 0 && x < columns && y >= 0 && y < rows) {
- this.x = x;
- this.y = y;
- }
- }
-
- /**
- * Gets the position.
- *
- * @return the position
- */
- public int[] getPosition() {
- return new int[] { x, y };
- }
-
- /**
- * Notifies the listeners that a color change has occurred
- *
- * @param color
- * The color which it changed to
- */
- public void fireColorChanged(Color color) {
- fireEvent(new ColorChangeEvent(this, color));
- }
-
- @Override
- protected ColorPickerGridState getState() {
- return (ColorPickerGridState) super.getState();
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.ui.components.colorpicker;
-
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-import java.util.concurrent.ArrayBlockingQueue;
-
-import com.vaadin.shared.ui.colorpicker.Color;
-import com.vaadin.ui.CustomComponent;
-
-/**
- * A component that represents color selection history within a color picker.
- *
- * @since 7.0.0
- */
-public class ColorPickerHistory extends CustomComponent
- implements ColorSelector, ColorChangeListener {
-
- private static final String STYLENAME = "v-colorpicker-history";
-
- private static final Method COLOR_CHANGE_METHOD;
- static {
- try {
- COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
- "colorChanged", new Class[] { ColorChangeEvent.class });
- } catch (final java.lang.NoSuchMethodException e) {
- // This should never happen
- throw new java.lang.RuntimeException(
- "Internal error finding methods in ColorPicker");
- }
- }
-
- /** The rows. */
- private static final int rows = 4;
-
- /** The columns. */
- private static final int columns = 15;
-
- /** Temporary color history for when the component is detached. */
- private ArrayBlockingQueue<Color> tempHistory = new ArrayBlockingQueue<Color>(
- rows * columns);
-
- /** The grid. */
- private final ColorPickerGrid grid;
-
- /**
- * Instantiates a new color picker history.
- */
- public ColorPickerHistory() {
- setPrimaryStyleName(STYLENAME);
-
- grid = new ColorPickerGrid(rows, columns);
- grid.setWidth("100%");
- grid.setPosition(0, 0);
- grid.addColorChangeListener(this);
-
- setCompositionRoot(grid);
- }
-
- @Override
- public void attach() {
- super.attach();
- createColorHistoryIfNecessary();
- }
-
- private void createColorHistoryIfNecessary() {
- List<Color> tempColors = new ArrayList<Color>(tempHistory);
- if (getSession().getAttribute("colorPickerHistory") == null) {
- getSession().setAttribute("colorPickerHistory",
- new ArrayBlockingQueue<Color>(rows * columns));
- }
- for (Color color : tempColors) {
- setColor(color);
- }
- tempHistory.clear();
- }
-
- @SuppressWarnings("unchecked")
- private ArrayBlockingQueue<Color> getColorHistory() {
- if (isAttached()) {
- Object colorHistory = getSession()
- .getAttribute("colorPickerHistory");
- if (colorHistory instanceof ArrayBlockingQueue<?>) {
- return (ArrayBlockingQueue<Color>) colorHistory;
- }
- }
- return tempHistory;
- }
-
- @Override
- public void setHeight(String height) {
- super.setHeight(height);
- grid.setHeight(height);
- }
-
- @Override
- public void setColor(Color color) {
-
- ArrayBlockingQueue<Color> colorHistory = getColorHistory();
-
- // Check that the color does not already exist
- boolean exists = false;
- Iterator<Color> iter = colorHistory.iterator();
- while (iter.hasNext()) {
- if (color.equals(iter.next())) {
- exists = true;
- break;
- }
- }
-
- // If the color does not exist then add it
- if (!exists) {
- if (!colorHistory.offer(color)) {
- colorHistory.poll();
- colorHistory.offer(color);
- }
- }
-
- List<Color> colorList = new ArrayList<Color>(colorHistory);
-
- // Invert order of colors
- Collections.reverse(colorList);
-
- // Move the selected color to the front of the list
- Collections.swap(colorList, colorList.indexOf(color), 0);
-
- // Create 2d color map
- Color[][] colors = new Color[rows][columns];
- iter = colorList.iterator();
-
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < columns; col++) {
- if (iter.hasNext()) {
- colors[row][col] = iter.next();
- } else {
- colors[row][col] = Color.WHITE;
- }
- }
- }
-
- grid.setColorGrid(colors);
- grid.markAsDirty();
- }
-
- @Override
- public Color getColor() {
- return getColorHistory().peek();
- }
-
- /**
- * Gets the history.
- *
- * @return the history
- */
- public List<Color> getHistory() {
- ArrayBlockingQueue<Color> colorHistory = getColorHistory();
- Color[] array = colorHistory.toArray(new Color[colorHistory.size()]);
- return Collections.unmodifiableList(Arrays.asList(array));
- }
-
- /**
- * Checks if the history contains given color.
- *
- * @param c
- * the color
- *
- * @return true, if successful
- */
- public boolean hasColor(Color c) {
- return getColorHistory().contains(c);
- }
-
- /**
- * Adds a color change listener
- *
- * @param listener
- * The listener
- */
- @Override
- public void addColorChangeListener(ColorChangeListener listener) {
- addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
- }
-
- /**
- * Removes a color change listener
- *
- * @param listener
- * The listener
- */
- @Override
- public void removeColorChangeListener(ColorChangeListener listener) {
- removeListener(ColorChangeEvent.class, listener);
- }
-
- @Override
- public void colorChanged(ColorChangeEvent event) {
- fireEvent(new ColorChangeEvent(this, event.getColor()));
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.ui.components.colorpicker;
-
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import com.vaadin.shared.ui.MarginInfo;
-import com.vaadin.shared.ui.colorpicker.Color;
-import com.vaadin.ui.Alignment;
-import com.vaadin.ui.Button;
-import com.vaadin.ui.Button.ClickEvent;
-import com.vaadin.ui.Button.ClickListener;
-import com.vaadin.ui.Component;
-import com.vaadin.ui.HorizontalLayout;
-import com.vaadin.ui.Layout;
-import com.vaadin.ui.Slider;
-import com.vaadin.ui.Slider.ValueOutOfBoundsException;
-import com.vaadin.ui.TabSheet;
-import com.vaadin.ui.VerticalLayout;
-import com.vaadin.ui.Window;
-import com.vaadin.v7.ui.AbstractColorPicker.Coordinates2Color;
-
-/**
- * A component that represents color selection popup within a color picker.
- *
- * @since 7.0.0
- */
-public class ColorPickerPopup extends Window
- implements ClickListener, ColorChangeListener, ColorSelector {
-
- private static final String STYLENAME = "v-colorpicker-popup";
-
- private static final Method COLOR_CHANGE_METHOD;
- static {
- try {
- COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
- "colorChanged", new Class[] { ColorChangeEvent.class });
- } catch (final java.lang.NoSuchMethodException e) {
- // This should never happen
- throw new java.lang.RuntimeException(
- "Internal error finding methods in ColorPicker");
- }
- }
-
- /** The tabs. */
- private final TabSheet tabs = new TabSheet();
-
- private Component rgbTab;
-
- private Component hsvTab;
-
- private Component swatchesTab;
-
- /** The layout. */
- private final VerticalLayout layout;
-
- /** The ok button. */
- private final Button ok = new Button("OK");
-
- /** The cancel button. */
- private final Button cancel = new Button("Cancel");
-
- /** The resize button. */
- private final Button resize = new Button("show/hide history");
-
- /** The selected color. */
- private Color selectedColor = Color.WHITE;
-
- /** The history. */
- private ColorPickerHistory history;
-
- /** The history container. */
- private Layout historyContainer;
-
- /** The rgb gradient. */
- private ColorPickerGradient rgbGradient;
-
- /** The hsv gradient. */
- private ColorPickerGradient hsvGradient;
-
- /** The red slider. */
- private Slider redSlider;
-
- /** The green slider. */
- private Slider greenSlider;
-
- /** The blue slider. */
- private Slider blueSlider;
-
- /** The hue slider. */
- private Slider hueSlider;
-
- /** The saturation slider. */
- private Slider saturationSlider;
-
- /** The value slider. */
- private Slider valueSlider;
-
- /** The preview on the rgb tab. */
- private ColorPickerPreview rgbPreview;
-
- /** The preview on the hsv tab. */
- private ColorPickerPreview hsvPreview;
-
- /** The preview on the swatches tab. */
- private ColorPickerPreview selPreview;
-
- /** The color select. */
- private ColorPickerSelect colorSelect;
-
- /** The selectors. */
- private final Set<ColorSelector> selectors = new HashSet<ColorSelector>();
-
- /**
- * Set true while the slider values are updated after colorChange. When
- * true, valueChange reactions from the sliders are disabled, because
- * otherwise the set color may become corrupted as it is repeatedly re-set
- * in valueChangeListeners using values from sliders that may not have been
- * updated yet.
- */
- private boolean updatingColors = false;
-
- private ColorPickerPopup() {
- // Set the layout
- layout = new VerticalLayout();
- layout.setSpacing(false);
- layout.setMargin(false);
- layout.setWidth("100%");
- layout.setHeight(null);
-
- setContent(layout);
- setStyleName(STYLENAME);
- setResizable(false);
- setImmediate(true);
- // Create the history
- history = new ColorPickerHistory();
- history.addColorChangeListener(this);
- }
-
- /**
- * Instantiates a new color picker popup.
- */
- public ColorPickerPopup(Color initialColor) {
- this();
- selectedColor = initialColor;
- initContents();
- }
-
- private void initContents() {
- // Create the preview on the rgb tab
- rgbPreview = new ColorPickerPreview(selectedColor);
- rgbPreview.setWidth("240px");
- rgbPreview.setHeight("20px");
- rgbPreview.addColorChangeListener(this);
- selectors.add(rgbPreview);
-
- // Create the preview on the hsv tab
- hsvPreview = new ColorPickerPreview(selectedColor);
- hsvPreview.setWidth("240px");
- hsvPreview.setHeight("20px");
- hsvPreview.addColorChangeListener(this);
- selectors.add(hsvPreview);
-
- // Create the preview on the swatches tab
- selPreview = new ColorPickerPreview(selectedColor);
- selPreview.setWidth("100%");
- selPreview.setHeight("20px");
- selPreview.addColorChangeListener(this);
- selectors.add(selPreview);
-
- // Create the tabs
- rgbTab = createRGBTab(selectedColor);
- tabs.addTab(rgbTab, "RGB", null);
-
- hsvTab = createHSVTab(selectedColor);
- tabs.addTab(hsvTab, "HSV", null);
-
- swatchesTab = createSelectTab();
- tabs.addTab(swatchesTab, "Swatches", null);
-
- // Add the tabs
- tabs.setWidth("100%");
-
- layout.addComponent(tabs);
-
- // Add the history
- history.setWidth("97%");
- history.setHeight("22px");
-
- // Create the default colors
- List<Color> defaultColors = new ArrayList<Color>();
- defaultColors.add(Color.BLACK);
- defaultColors.add(Color.WHITE);
-
- // Create the history
- VerticalLayout innerContainer = new VerticalLayout();
- innerContainer.setWidth("100%");
- innerContainer.setHeight(null);
- innerContainer.addComponent(history);
-
- VerticalLayout outerContainer = new VerticalLayout();
- outerContainer.setWidth("99%");
- outerContainer.setHeight("27px");
- outerContainer.addComponent(innerContainer);
- historyContainer = outerContainer;
-
- layout.addComponent(historyContainer);
-
- // Add the resize button for the history
- resize.addClickListener(this);
- resize.setData(new Boolean(false));
- resize.setWidth("100%");
- resize.setHeight("10px");
- resize.setPrimaryStyleName("resize-button");
- layout.addComponent(resize);
-
- // Add the buttons
- ok.setWidth("70px");
- ok.addClickListener(this);
-
- cancel.setWidth("70px");
- cancel.addClickListener(this);
-
- HorizontalLayout buttons = new HorizontalLayout();
- buttons.addComponent(ok);
- buttons.addComponent(cancel);
- buttons.setWidth("100%");
- buttons.setHeight("30px");
- buttons.setComponentAlignment(ok, Alignment.MIDDLE_CENTER);
- buttons.setComponentAlignment(cancel, Alignment.MIDDLE_CENTER);
- layout.addComponent(buttons);
- }
-
- /**
- * Creates the RGB tab.
- *
- * @return the component
- */
- private Component createRGBTab(Color color) {
- VerticalLayout rgbLayout = new VerticalLayout();
- rgbLayout.setMargin(new MarginInfo(false, false, true, false));
- rgbLayout.addComponent(rgbPreview);
- rgbLayout.setStyleName("rgbtab");
-
- // Add the RGB color gradient
- rgbGradient = new ColorPickerGradient("rgb-gradient", RGBConverter);
- rgbGradient.setColor(color);
- rgbGradient.addColorChangeListener(this);
- rgbLayout.addComponent(rgbGradient);
- selectors.add(rgbGradient);
-
- // Add the RGB sliders
- VerticalLayout sliders = new VerticalLayout();
- sliders.setStyleName("rgb-sliders");
-
- redSlider = createRGBSlider("Red", "red");
- greenSlider = createRGBSlider("Green", "green");
- blueSlider = createRGBSlider("Blue", "blue");
- setRgbSliderValues(color);
-
- redSlider.addValueChangeListener(e -> {
- double red = e.getValue();
- if (!updatingColors) {
- Color newColor = new Color((int) red, selectedColor.getGreen(),
- selectedColor.getBlue());
- setColor(newColor);
- }
- });
-
- sliders.addComponent(redSlider);
-
- greenSlider.addValueChangeListener(e -> {
- double green = e.getValue();
- if (!updatingColors) {
- Color newColor = new Color(selectedColor.getRed(), (int) green,
- selectedColor.getBlue());
- setColor(newColor);
- }
- });
- sliders.addComponent(greenSlider);
-
- blueSlider.addValueChangeListener(e -> {
- double blue = e.getValue();
- if (!updatingColors) {
- Color newColor = new Color(selectedColor.getRed(),
- selectedColor.getGreen(), (int) blue);
- setColor(newColor);
- }
- });
- sliders.addComponent(blueSlider);
-
- rgbLayout.addComponent(sliders);
-
- return rgbLayout;
- }
-
- private Slider createRGBSlider(String caption, String styleName) {
- Slider redSlider = new Slider(caption, 0, 255);
- redSlider.setImmediate(true);
- redSlider.setStyleName("rgb-slider");
- redSlider.setWidth("220px");
- redSlider.addStyleName(styleName);
- return redSlider;
- }
-
- /**
- * Creates the hsv tab.
- *
- * @return the component
- */
- private Component createHSVTab(Color color) {
- VerticalLayout hsvLayout = new VerticalLayout();
- hsvLayout.setMargin(new MarginInfo(false, false, true, false));
- hsvLayout.addComponent(hsvPreview);
- hsvLayout.setStyleName("hsvtab");
-
- // Add the hsv gradient
- hsvGradient = new ColorPickerGradient("hsv-gradient", HSVConverter);
- hsvGradient.setColor(color);
- hsvGradient.addColorChangeListener(this);
- hsvLayout.addComponent(hsvGradient);
- selectors.add(hsvGradient);
-
- VerticalLayout sliders = new VerticalLayout();
- sliders.setStyleName("hsv-sliders");
-
- hueSlider = new Slider("Hue", 0, 360);
- saturationSlider = new Slider("Saturation", 0, 100);
- valueSlider = new Slider("Value", 0, 100);
-
- float[] hsv = color.getHSV();
- setHsvSliderValues(hsv);
-
- hueSlider.setStyleName("hsv-slider");
- hueSlider.addStyleName("hue-slider");
- hueSlider.setWidth("220px");
- hueSlider.setImmediate(true);
- hueSlider.addValueChangeListener(event -> {
- if (!updatingColors) {
- float hue = (Float.parseFloat(event.getValue().toString()))
- / 360f;
- float saturation = (Float
- .parseFloat(saturationSlider.getValue().toString()))
- / 100f;
- float value = (Float
- .parseFloat(valueSlider.getValue().toString())) / 100f;
-
- // Set the color
- Color newColor = new Color(
- Color.HSVtoRGB(hue, saturation, value));
- setColor(newColor);
-
- /*
- * Set the background color of the hue gradient. This has to be
- * done here since in the conversion the base color information
- * is lost when color is black/white
- */
- Color bgColor = new Color(Color.HSVtoRGB(hue, 1f, 1f));
- hsvGradient.setBackgroundColor(bgColor);
- }
- });
- sliders.addComponent(hueSlider);
-
- saturationSlider.setStyleName("hsv-slider");
- saturationSlider.setWidth("220px");
- saturationSlider.setImmediate(true);
- saturationSlider.addValueChangeListener(event -> {
- if (!updatingColors) {
- float hue = (Float.parseFloat(hueSlider.getValue().toString()))
- / 360f;
- float saturation = (Float
- .parseFloat(event.getValue().toString())) / 100f;
- float value = (Float
- .parseFloat(valueSlider.getValue().toString())) / 100f;
- Color newColor = new Color(
- Color.HSVtoRGB(hue, saturation, value));
- setColor(newColor);
- }
- });
- sliders.addComponent(saturationSlider);
-
- valueSlider.setStyleName("hsv-slider");
- valueSlider.setWidth("220px");
- valueSlider.setImmediate(true);
- valueSlider.addValueChangeListener(event -> {
- if (!updatingColors) {
- float hue = (Float.parseFloat(hueSlider.getValue().toString()))
- / 360f;
- float saturation = (Float
- .parseFloat(saturationSlider.getValue().toString()))
- / 100f;
- float value = (Float.parseFloat(event.getValue().toString()))
- / 100f;
-
- Color newColor = new Color(
- Color.HSVtoRGB(hue, saturation, value));
- setColor(newColor);
- }
- });
-
- sliders.addComponent(valueSlider);
- hsvLayout.addComponent(sliders);
-
- return hsvLayout;
- }
-
- /**
- * Creates the select tab.
- *
- * @return the component
- */
- private Component createSelectTab() {
- VerticalLayout selLayout = new VerticalLayout();
- selLayout.setMargin(new MarginInfo(false, false, true, false));
- selLayout.addComponent(selPreview);
- selLayout.addStyleName("seltab");
-
- colorSelect = new ColorPickerSelect();
- colorSelect.addColorChangeListener(this);
- selLayout.addComponent(colorSelect);
-
- return selLayout;
- }
-
- @Override
- public void buttonClick(ClickEvent event) {
- // History resize was clicked
- if (event.getButton() == resize) {
- boolean state = (Boolean) resize.getData();
-
- // minimize
- if (state) {
- historyContainer.setHeight("27px");
- history.setHeight("22px");
-
- // maximize
- } else {
- historyContainer.setHeight("90px");
- history.setHeight("85px");
- }
-
- resize.setData(new Boolean(!state));
- }
-
- // Ok button was clicked
- else if (event.getButton() == ok) {
- history.setColor(getColor());
- fireColorChanged();
- close();
- }
-
- // Cancel button was clicked
- else if (event.getButton() == cancel) {
- close();
- }
-
- }
-
- /**
- * Notifies the listeners that the color changed
- */
- public void fireColorChanged() {
- fireEvent(new ColorChangeEvent(this, getColor()));
- }
-
- /**
- * Gets the history.
- *
- * @return the history
- */
- public ColorPickerHistory getHistory() {
- return history;
- }
-
- @Override
- public void setColor(Color color) {
- if (color == null) {
- return;
- }
-
- selectedColor = color;
-
- hsvGradient.setColor(selectedColor);
- hsvPreview.setColor(selectedColor);
-
- rgbGradient.setColor(selectedColor);
- rgbPreview.setColor(selectedColor);
-
- selPreview.setColor(selectedColor);
- }
-
- @Override
- public Color getColor() {
- return selectedColor;
- }
-
- /**
- * Gets the color history.
- *
- * @return the color history
- */
- public List<Color> getColorHistory() {
- return Collections.unmodifiableList(history.getHistory());
- }
-
- @Override
- public void colorChanged(ColorChangeEvent event) {
- setColor(event.getColor());
-
- updatingColors = true;
-
- setRgbSliderValues(selectedColor);
- float[] hsv = selectedColor.getHSV();
- setHsvSliderValues(hsv);
-
- updatingColors = false;
-
- for (ColorSelector s : selectors) {
- if (event.getSource() != s && s != this
- && s.getColor() != selectedColor) {
- s.setColor(selectedColor);
- }
- }
- }
-
- private void setRgbSliderValues(Color color) {
- try {
- redSlider.setValue(((Integer) color.getRed()).doubleValue());
- blueSlider.setValue(((Integer) color.getBlue()).doubleValue());
- greenSlider.setValue(((Integer) color.getGreen()).doubleValue());
- } catch (ValueOutOfBoundsException e) {
- getLogger().log(Level.WARNING,
- "Unable to set RGB color value to " + color.getRed() + ","
- + color.getGreen() + "," + color.getBlue(),
- e);
- }
- }
-
- private void setHsvSliderValues(float[] hsv) {
- try {
- hueSlider.setValue(((Float) (hsv[0] * 360f)).doubleValue());
- saturationSlider.setValue(((Float) (hsv[1] * 100f)).doubleValue());
- valueSlider.setValue(((Float) (hsv[2] * 100f)).doubleValue());
- } catch (ValueOutOfBoundsException e) {
- getLogger().log(Level.WARNING, "Unable to set HSV color value to "
- + hsv[0] + "," + hsv[1] + "," + hsv[2], e);
- }
- }
-
- @Override
- public void addColorChangeListener(ColorChangeListener listener) {
- addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
- }
-
- @Override
- public void removeColorChangeListener(ColorChangeListener listener) {
- removeListener(ColorChangeEvent.class, listener);
- }
-
- /**
- * Checks the visibility of the given tab
- *
- * @param tab
- * The tab to check
- * @return true if tab is visible, false otherwise
- */
- private boolean tabIsVisible(Component tab) {
- Iterator<Component> tabIterator = tabs.getComponentIterator();
- while (tabIterator.hasNext()) {
- if (tabIterator.next() == tab) {
- return true;
- }
- }
- return false;
- }
-
- /**
- * How many tabs are visible
- *
- * @return The number of tabs visible
- */
- private int tabsNumVisible() {
- Iterator<Component> tabIterator = tabs.getComponentIterator();
- int tabCounter = 0;
- while (tabIterator.hasNext()) {
- tabIterator.next();
- tabCounter++;
- }
- return tabCounter;
- }
-
- /**
- * Checks if tabs are needed and hides them if not
- */
- private void checkIfTabsNeeded() {
- tabs.hideTabs(tabsNumVisible() == 1);
- }
-
- /**
- * Set RGB tab visibility
- *
- * @param visible
- * The visibility of the RGB tab
- */
- public void setRGBTabVisible(boolean visible) {
- if (visible && !tabIsVisible(rgbTab)) {
- tabs.addTab(rgbTab, "RGB", null);
- checkIfTabsNeeded();
- } else if (!visible && tabIsVisible(rgbTab)) {
- tabs.removeComponent(rgbTab);
- checkIfTabsNeeded();
- }
- }
-
- /**
- * Set HSV tab visibility
- *
- * @param visible
- * The visibility of the HSV tab
- */
- public void setHSVTabVisible(boolean visible) {
- if (visible && !tabIsVisible(hsvTab)) {
- tabs.addTab(hsvTab, "HSV", null);
- checkIfTabsNeeded();
- } else if (!visible && tabIsVisible(hsvTab)) {
- tabs.removeComponent(hsvTab);
- checkIfTabsNeeded();
- }
- }
-
- /**
- * Set Swatches tab visibility
- *
- * @param visible
- * The visibility of the Swatches tab
- */
- public void setSwatchesTabVisible(boolean visible) {
- if (visible && !tabIsVisible(swatchesTab)) {
- tabs.addTab(swatchesTab, "Swatches", null);
- checkIfTabsNeeded();
- } else if (!visible && tabIsVisible(swatchesTab)) {
- tabs.removeComponent(swatchesTab);
- checkIfTabsNeeded();
- }
- }
-
- /**
- * Set the History visibility
- *
- * @param visible
- */
- public void setHistoryVisible(boolean visible) {
- historyContainer.setVisible(visible);
- resize.setVisible(visible);
- }
-
- /**
- * Set the preview visibility
- *
- * @param visible
- */
- public void setPreviewVisible(boolean visible) {
- hsvPreview.setVisible(visible);
- rgbPreview.setVisible(visible);
- selPreview.setVisible(visible);
- }
-
- /** RGB color converter */
- private Coordinates2Color RGBConverter = new Coordinates2Color() {
-
- @Override
- public Color calculate(int x, int y) {
- float h = (x / 220f);
- float s = 1f;
- float v = 1f;
-
- if (y < 110) {
- s = y / 110f;
- } else if (y > 110) {
- v = 1f - (y - 110f) / 110f;
- }
-
- return new Color(Color.HSVtoRGB(h, s, v));
- }
-
- @Override
- public int[] calculate(Color color) {
-
- float[] hsv = color.getHSV();
-
- int x = Math.round(hsv[0] * 220f);
- int y = 0;
-
- // lower half
- if (hsv[1] == 1f) {
- y = Math.round(110f - (hsv[1] + hsv[2]) * 110f);
- } else {
- y = Math.round(hsv[1] * 110f);
- }
-
- return new int[] { x, y };
- }
- };
-
- /** HSV color converter */
- Coordinates2Color HSVConverter = new Coordinates2Color() {
- @Override
- public int[] calculate(Color color) {
-
- float[] hsv = color.getHSV();
-
- // Calculate coordinates
- int x = Math.round(hsv[2] * 220.0f);
- int y = Math.round(220 - hsv[1] * 220.0f);
-
- // Create background color of clean color
- Color bgColor = new Color(Color.HSVtoRGB(hsv[0], 1f, 1f));
- hsvGradient.setBackgroundColor(bgColor);
-
- return new int[] { x, y };
- }
-
- @Override
- public Color calculate(int x, int y) {
- float saturation = 1f - (y / 220.0f);
- float value = (x / 220.0f);
- float hue = Float.parseFloat(hueSlider.getValue().toString())
- / 360f;
-
- Color color = new Color(Color.HSVtoRGB(hue, saturation, value));
- return color;
- }
- };
-
- private static Logger getLogger() {
- return Logger.getLogger(ColorPickerPopup.class.getName());
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.ui.components.colorpicker;
-
-import java.lang.reflect.Method;
-
-import com.vaadin.shared.ui.colorpicker.Color;
-import com.vaadin.ui.Component;
-import com.vaadin.ui.CssLayout;
-import com.vaadin.v7.data.Property.ValueChangeEvent;
-import com.vaadin.v7.data.Property.ValueChangeListener;
-import com.vaadin.v7.ui.TextField;
-
-/**
- * A component that represents color selection preview within a color picker.
- *
- * @since 7.0.0
- */
-public class ColorPickerPreview extends CssLayout
- implements ColorSelector, ValueChangeListener {
-
- private static final String STYLE_DARK_COLOR = "v-textfield-dark";
- private static final String STYLE_LIGHT_COLOR = "v-textfield-light";
-
- private static final Method COLOR_CHANGE_METHOD;
- static {
- try {
- COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
- "colorChanged", new Class[] { ColorChangeEvent.class });
- } catch (final java.lang.NoSuchMethodException e) {
- // This should never happen
- throw new java.lang.RuntimeException(
- "Internal error finding methods in ColorPicker");
- }
- }
-
- /** The color. */
- private Color color;
-
- /** The field. */
- private final TextField field;
-
- /** The old value. */
- private String oldValue;
-
- private ColorPickerPreview() {
- setStyleName("v-colorpicker-preview");
- setImmediate(true);
- field = new TextField();
- field.setImmediate(true);
- field.setSizeFull();
- field.setStyleName("v-colorpicker-preview-textfield");
- field.setData(this);
- field.addValueChangeListener(this);
- addComponent(field);
- }
-
- /**
- * Instantiates a new color picker preview.
- */
- public ColorPickerPreview(Color color) {
- this();
- setColor(color);
- }
-
- @Override
- public void setColor(Color color) {
- this.color = color;
-
- // Unregister listener
- field.removeValueChangeListener(this);
-
- String colorCSS = color.getCSS();
- field.setValue(colorCSS);
-
- if (field.isValid()) {
- oldValue = colorCSS;
- } else {
- field.setValue(oldValue);
- }
-
- // Re-register listener
- field.addValueChangeListener(this);
-
- // Set the text color
- field.removeStyleName(STYLE_DARK_COLOR);
- field.removeStyleName(STYLE_LIGHT_COLOR);
- if (this.color.getRed() + this.color.getGreen()
- + this.color.getBlue() < 3 * 128) {
- field.addStyleName(STYLE_DARK_COLOR);
- } else {
- field.addStyleName(STYLE_LIGHT_COLOR);
- }
-
- markAsDirty();
- }
-
- @Override
- public Color getColor() {
- return color;
- }
-
- @Override
- public void addColorChangeListener(ColorChangeListener listener) {
- addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
- }
-
- @Override
- public void removeColorChangeListener(ColorChangeListener listener) {
- removeListener(ColorChangeEvent.class, listener);
- }
-
- @Override
- public void valueChange(ValueChangeEvent event) {
- String value = (String) event.getProperty().getValue();
- try {
- if (value != null) {
- /*
- * Description of supported formats see
- * http://www.w3schools.com/cssref/css_colors_legal.asp
- */
- if (value.length() == 7 && value.startsWith("#")) {
- // CSS color format (e.g. #000000)
- int red = Integer.parseInt(value.substring(1, 3), 16);
- int green = Integer.parseInt(value.substring(3, 5), 16);
- int blue = Integer.parseInt(value.substring(5, 7), 16);
- color = new Color(red, green, blue);
-
- } else if (value.startsWith("rgb")) {
- // RGB color format rgb/rgba(255,255,255,0.1)
- String[] colors = value.substring(value.indexOf("(") + 1,
- value.length() - 1).split(",");
-
- int red = Integer.parseInt(colors[0]);
- int green = Integer.parseInt(colors[1]);
- int blue = Integer.parseInt(colors[2]);
- if (colors.length > 3) {
- int alpha = (int) (Double.parseDouble(colors[3])
- * 255d);
- color = new Color(red, green, blue, alpha);
- } else {
- color = new Color(red, green, blue);
- }
-
- } else if (value.startsWith("hsl")) {
- // HSL color format hsl/hsla(100,50%,50%,1.0)
- String[] colors = value.substring(value.indexOf("(") + 1,
- value.length() - 1).split(",");
-
- int hue = Integer.parseInt(colors[0]);
- int saturation = Integer
- .parseInt(colors[1].replace("%", ""));
- int lightness = Integer
- .parseInt(colors[2].replace("%", ""));
- int rgb = Color.HSLtoRGB(hue, saturation, lightness);
-
- if (colors.length > 3) {
- int alpha = (int) (Double.parseDouble(colors[3])
- * 255d);
- color = new Color(rgb);
- color.setAlpha(alpha);
- } else {
- color = new Color(rgb);
- }
- }
-
- oldValue = value;
- fireEvent(new ColorChangeEvent((Component) field.getData(),
- color));
- }
-
- } catch (NumberFormatException nfe) {
- // Revert value
- field.setValue(oldValue);
- }
- }
-
- /**
- * Called when the component is refreshing
- */
- @Override
- protected String getCss(Component c) {
- return "background: " + color.getCSS();
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.ui.components.colorpicker;
-
-import com.vaadin.shared.ui.colorpicker.Color;
-import com.vaadin.ui.CustomComponent;
-import com.vaadin.ui.VerticalLayout;
-import com.vaadin.v7.data.Property.ValueChangeEvent;
-import com.vaadin.v7.data.Property.ValueChangeListener;
-import com.vaadin.v7.ui.ComboBox;
-
-/**
- * A component that represents color selection swatches within a color picker.
- *
- * @since 7.0.0
- */
-public class ColorPickerSelect extends CustomComponent
- implements ColorSelector, ValueChangeListener {
-
- /** The range. */
- private final ComboBox range;
-
- /** The grid. */
- private final ColorPickerGrid grid;
-
- /**
- * The Enum ColorRangePropertyId.
- */
- private enum ColorRangePropertyId {
- ALL("All colors"), RED("Red colors"), GREEN("Green colors"), BLUE(
- "Blue colors");
-
- /** The caption. */
- private String caption;
-
- /**
- * Instantiates a new color range property id.
- *
- * @param caption
- * the caption
- */
- ColorRangePropertyId(String caption) {
- this.caption = caption;
- }
-
- @Override
- public String toString() {
- return caption;
- }
- }
-
- /**
- * Instantiates a new color picker select.
- *
- * @param rows
- * the rows
- * @param columns
- * the columns
- */
- public ColorPickerSelect() {
-
- VerticalLayout layout = new VerticalLayout();
- setCompositionRoot(layout);
-
- setStyleName("colorselect");
- setWidth("100%");
-
- range = new ComboBox();
- range.setImmediate(true);
- range.setImmediate(true);
- range.setNullSelectionAllowed(false);
- range.setNewItemsAllowed(false);
- range.setWidth("100%");
- range.addValueChangeListener(this);
-
- for (ColorRangePropertyId id : ColorRangePropertyId.values()) {
- range.addItem(id);
- }
- range.select(ColorRangePropertyId.ALL);
-
- layout.addComponent(range);
-
- grid = new ColorPickerGrid(createAllColors(14, 10));
- grid.setWidth("100%");
-
- layout.addComponent(grid);
- }
-
- /**
- * Creates the all colors.
- *
- * @param rows
- * the rows
- * @param columns
- * the columns
- *
- * @return the color[][]
- */
- private Color[][] createAllColors(int rows, int columns) {
- Color[][] colors = new Color[rows][columns];
-
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < columns; col++) {
-
- // Create the color grid by varying the saturation and value
- if (row < (rows - 1)) {
- // Calculate new hue value
- float hue = ((float) col / (float) columns);
- float saturation = 1f;
- float value = 1f;
-
- // For the upper half use value=1 and variable
- // saturation
- if (row < (rows / 2)) {
- saturation = ((row + 1f) / (rows / 2f));
- } else {
- value = 1f - ((row - (rows / 2f)) / (rows / 2f));
- }
-
- colors[row][col] = new Color(
- Color.HSVtoRGB(hue, saturation, value));
- }
-
- // The last row should have the black&white gradient
- else {
- float hue = 0f;
- float saturation = 0f;
- float value = 1f - ((float) col / (float) columns);
-
- colors[row][col] = new Color(
- Color.HSVtoRGB(hue, saturation, value));
- }
- }
- }
-
- return colors;
- }
-
- /**
- * Creates the color.
- *
- * @param color
- * the color
- * @param rows
- * the rows
- * @param columns
- * the columns
- *
- * @return the color[][]
- */
- private Color[][] createColors(Color color, int rows, int columns) {
- Color[][] colors = new Color[rows][columns];
-
- float[] hsv = color.getHSV();
-
- float hue = hsv[0];
- float saturation = 1f;
- float value = 1f;
-
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < columns; col++) {
-
- int index = row * columns + col;
- saturation = 1f;
- value = 1f;
-
- if (index <= ((rows * columns) / 2)) {
- saturation = index
- / (((float) rows * (float) columns) / 2f);
- } else {
- index -= ((rows * columns) / 2);
- value = 1f
- - index / (((float) rows * (float) columns) / 2f);
- }
-
- colors[row][col] = new Color(
- Color.HSVtoRGB(hue, saturation, value));
- }
- }
-
- return colors;
- }
-
- @Override
- public Color getColor() {
- return grid.getColor();
- }
-
- @Override
- public void setColor(Color color) {
- grid.getColor();
- }
-
- @Override
- public void addColorChangeListener(ColorChangeListener listener) {
- grid.addColorChangeListener(listener);
- }
-
- @Override
- public void removeColorChangeListener(ColorChangeListener listener) {
- grid.removeColorChangeListener(listener);
- }
-
- @Override
- public void valueChange(ValueChangeEvent event) {
- if (grid == null) {
- return;
- }
-
- if (event.getProperty().getValue() == ColorRangePropertyId.ALL) {
- grid.setColorGrid(createAllColors(14, 10));
- } else if (event.getProperty().getValue() == ColorRangePropertyId.RED) {
- grid.setColorGrid(createColors(new Color(0xFF, 0, 0), 14, 10));
- } else if (event.getProperty()
- .getValue() == ColorRangePropertyId.GREEN) {
- grid.setColorGrid(createColors(new Color(0, 0xFF, 0), 14, 10));
- } else if (event.getProperty()
- .getValue() == ColorRangePropertyId.BLUE) {
- grid.setColorGrid(createColors(new Color(0, 0, 0xFF), 14, 10));
- }
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.ui.components.colorpicker;
-
-import java.io.Serializable;
-
-import com.vaadin.shared.ui.colorpicker.Color;
-
-/**
- * An interface for a color selector.
- *
- * @since 7.0.0
- */
-public interface ColorSelector extends Serializable, HasColorChangeListener {
-
- /**
- * Sets the color.
- *
- * @param color
- * the new color
- */
- public void setColor(Color color);
-
- /**
- * Gets the color.
- *
- * @return the color
- */
- public Color getColor();
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.ui.components.colorpicker;
-
-import java.io.Serializable;
-
-public interface HasColorChangeListener extends Serializable {
-
- /**
- * Adds a {@link ColorChangeListener} to the component.
- *
- * @param listener
- */
- void addColorChangeListener(ColorChangeListener listener);
-
- /**
- * Removes a {@link ColorChangeListener} from the component.
- *
- * @param listener
- */
- void removeColorChangeListener(ColorChangeListener listener);
-
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.tests.server.component.colorpicker;
-
-import org.junit.Test;
-
-import com.vaadin.shared.ui.colorpicker.Color;
-import com.vaadin.tests.design.DeclarativeTestBase;
-import com.vaadin.v7.ui.AbstractColorPicker;
-import com.vaadin.v7.ui.AbstractColorPicker.PopupStyle;
-import com.vaadin.v7.ui.ColorPicker;
-import com.vaadin.v7.ui.ColorPickerArea;
-
-public class AbstractColorPickerDeclarativeTest
- extends DeclarativeTestBase<AbstractColorPicker> {
-
- @Test
- public void testAllAbstractColorPickerFeatures() {
- String design = "<vaadin7-color-picker color='#fafafa' default-caption-enabled position='100,100'"
- + " popup-style='simple' rgb-visibility='false' hsv-visibility='false'"
- + " history-visibility=false textfield-visibility=false />";
- ColorPicker colorPicker = new ColorPicker();
- int colorInt = Integer.parseInt("fafafa", 16);
- colorPicker.setColor(new Color(colorInt));
- colorPicker.setDefaultCaptionEnabled(true);
- colorPicker.setPosition(100, 100);
- colorPicker.setPopupStyle(PopupStyle.POPUP_SIMPLE);
- colorPicker.setRGBVisibility(false);
- colorPicker.setHSVVisibility(false);
- colorPicker.setSwatchesVisibility(true);
- colorPicker.setHistoryVisibility(false);
- colorPicker.setTextfieldVisibility(false);
-
- testWrite(design, colorPicker);
- testRead(design, colorPicker);
- }
-
- @Test
- public void testEmptyColorPicker() {
- String design = "<vaadin7-color-picker />";
- ColorPicker colorPicker = new ColorPicker();
- testRead(design, colorPicker);
- testWrite(design, colorPicker);
- }
-
- @Test
- public void testAllAbstractColorPickerAreaFeatures() {
- String design = "<vaadin7-color-picker-area color='#fafafa' default-caption-enabled position='100,100'"
- + " popup-style='simple' rgb-visibility='false' hsv-visibility='false'"
- + " history-visibility=false textfield-visibility=false />";
- AbstractColorPicker colorPicker = new ColorPickerArea();
- int colorInt = Integer.parseInt("fafafa", 16);
- colorPicker.setColor(new Color(colorInt));
- colorPicker.setDefaultCaptionEnabled(true);
- colorPicker.setPosition(100, 100);
- colorPicker.setPopupStyle(PopupStyle.POPUP_SIMPLE);
- colorPicker.setRGBVisibility(false);
- colorPicker.setHSVVisibility(false);
- colorPicker.setSwatchesVisibility(true);
- colorPicker.setHistoryVisibility(false);
- colorPicker.setTextfieldVisibility(false);
-
- testWrite(design, colorPicker);
- testRead(design, colorPicker);
- }
-
- @Test
- public void testEmptyColorPickerArea() {
- String design = "<vaadin7-color-picker-area />";
- AbstractColorPicker colorPicker = new ColorPickerArea();
- testRead(design, colorPicker);
- testWrite(design, colorPicker);
- }
-}
+++ /dev/null
-/*
- * Copyright 2000-2016 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.v7.tests.server.component.colorpicker;
-
-import static org.junit.Assert.assertEquals;
-
-import org.junit.Test;
-
-import com.vaadin.shared.ui.colorpicker.Color;
-
-public class ColorConversionsTest {
-
- @Test
- public void convertHSL2RGB() {
-
- int rgb = Color.HSLtoRGB(100, 50, 50);
- Color c = new Color(rgb);
- assertEquals(106, c.getRed());
- assertEquals(191, c.getGreen());
- assertEquals(64, c.getBlue());
- assertEquals("#6abf40", c.getCSS());
-
- rgb = Color.HSLtoRGB(0, 50, 50);
- c = new Color(rgb);
- assertEquals(191, c.getRed());
- assertEquals(64, c.getGreen());
- assertEquals(64, c.getBlue());
- assertEquals("#bf4040", c.getCSS());
-
- rgb = Color.HSLtoRGB(50, 0, 50);
- c = new Color(rgb);
- assertEquals(128, c.getRed());
- assertEquals(128, c.getGreen());
- assertEquals(128, c.getBlue());
- assertEquals("#808080", c.getCSS());
-
- rgb = Color.HSLtoRGB(50, 100, 0);
- c = new Color(rgb);
- assertEquals(0, c.getRed(), 0);
- assertEquals(0, c.getGreen(), 0);
- assertEquals(0, c.getBlue(), 0);
- assertEquals("#000000", c.getCSS());
- }
-}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.lang.reflect.Method;
+import java.util.Collection;
+
+import org.jsoup.nodes.Attributes;
+import org.jsoup.nodes.Element;
+
+import com.vaadin.shared.ui.colorpicker.Color;
+import com.vaadin.shared.ui.colorpicker.ColorPickerServerRpc;
+import com.vaadin.shared.ui.colorpicker.ColorPickerState;
+import com.vaadin.ui.Window.CloseEvent;
+import com.vaadin.ui.Window.CloseListener;
+import com.vaadin.ui.components.colorpicker.ColorChangeEvent;
+import com.vaadin.ui.components.colorpicker.ColorChangeListener;
+import com.vaadin.ui.components.colorpicker.ColorPickerPopup;
+import com.vaadin.ui.components.colorpicker.ColorSelector;
+import com.vaadin.ui.declarative.DesignAttributeHandler;
+import com.vaadin.ui.declarative.DesignContext;
+
+/**
+ * An abstract class that defines default implementation for a color picker
+ * component.
+ *
+ * @since 7.0.0
+ */
+public abstract class AbstractColorPicker extends AbstractComponent
+ implements CloseListener, ColorSelector {
+ private static final Method COLOR_CHANGE_METHOD;
+ static {
+ try {
+ COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
+ "colorChanged", new Class[] { ColorChangeEvent.class });
+ } catch (final java.lang.NoSuchMethodException e) {
+ // This should never happen
+ throw new java.lang.RuntimeException(
+ "Internal error finding methods in ColorPicker");
+ }
+ }
+
+ /**
+ * Interface for converting 2d-coordinates to a Color
+ */
+ public interface Coordinates2Color extends Serializable {
+
+ /**
+ * Calculate color from coordinates
+ *
+ * @param x
+ * the x-coordinate
+ * @param y
+ * the y-coordinate
+ *
+ * @return the color
+ */
+ public Color calculate(int x, int y);
+
+ /**
+ * Calculate coordinates from color
+ *
+ * @param c
+ * the c
+ *
+ * @return the integer array with the coordinates
+ */
+ public int[] calculate(Color c);
+ }
+
+ public enum PopupStyle {
+ POPUP_NORMAL("normal"), POPUP_SIMPLE("simple");
+
+ private String style;
+
+ PopupStyle(String styleName) {
+ style = styleName;
+ }
+
+ @Override
+ public String toString() {
+ return style;
+ }
+ }
+
+ private ColorPickerServerRpc rpc = new ColorPickerServerRpc() {
+
+ @Override
+ public void openPopup(boolean open) {
+ showPopup(open);
+ }
+ };
+
+ protected static final String STYLENAME_DEFAULT = "v-colorpicker";
+ protected static final String STYLENAME_BUTTON = "v-button";
+ protected static final String STYLENAME_AREA = "v-colorpicker-area";
+
+ protected PopupStyle popupStyle = PopupStyle.POPUP_NORMAL;
+
+ /** The popup window. */
+ private ColorPickerPopup window;
+
+ /** The color. */
+ protected Color color;
+
+ /** The UI. */
+ private UI parent;
+
+ protected String popupCaption = null;
+ private int positionX = 0;
+ private int positionY = 0;
+
+ protected boolean rgbVisible = true;
+ protected boolean hsvVisible = true;
+ protected boolean swatchesVisible = true;
+ protected boolean historyVisible = true;
+ protected boolean textfieldVisible = true;
+
+ /**
+ * Instantiates a new color picker.
+ */
+ public AbstractColorPicker() {
+ this("Colors", Color.WHITE);
+ }
+
+ /**
+ * Instantiates a new color picker.
+ *
+ * @param popupCaption
+ * the caption of the popup window
+ */
+ public AbstractColorPicker(String popupCaption) {
+ this(popupCaption, Color.WHITE);
+ }
+
+ /**
+ * Instantiates a new color picker.
+ *
+ * @param popupCaption
+ * the caption of the popup window
+ * @param initialColor
+ * the initial color
+ */
+ public AbstractColorPicker(String popupCaption, Color initialColor) {
+ super();
+ registerRpc(rpc);
+ setColor(initialColor);
+ this.popupCaption = popupCaption;
+ setDefaultStyles();
+ setCaption("");
+ }
+
+ @Override
+ public void setColor(Color color) {
+ this.color = color;
+
+ if (window != null) {
+ window.setColor(color);
+ }
+ getState().color = color.getCSS();
+ }
+
+ @Override
+ public Color getColor() {
+ return color;
+ }
+
+ /**
+ * Set true if the component should show a default caption (css-code for the
+ * currently selected color, e.g. #ffffff) when no other caption is
+ * available.
+ *
+ * @param enabled
+ */
+ public void setDefaultCaptionEnabled(boolean enabled) {
+ getState().showDefaultCaption = enabled;
+ }
+
+ /**
+ * Returns true if the component shows the default caption (css-code for the
+ * currently selected color, e.g. #ffffff) if no other caption is available.
+ */
+ public boolean isDefaultCaptionEnabled() {
+ return getState(false).showDefaultCaption;
+ }
+
+ /**
+ * Sets the position of the popup window
+ *
+ * @param x
+ * the x-coordinate
+ * @param y
+ * the y-coordinate
+ */
+ public void setPosition(int x, int y) {
+ positionX = x;
+ positionY = y;
+
+ if (window != null) {
+ window.setPositionX(x);
+ window.setPositionY(y);
+ }
+ }
+
+ @Override
+ public void addColorChangeListener(ColorChangeListener listener) {
+ addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
+ }
+
+ @Override
+ public void removeColorChangeListener(ColorChangeListener listener) {
+ removeListener(ColorChangeEvent.class, listener);
+ }
+
+ @Override
+ public void windowClose(CloseEvent e) {
+ if (e.getWindow() == window) {
+ getState().popupVisible = false;
+ }
+ }
+
+ /**
+ * Fired when a color change event occurs
+ *
+ * @param event
+ * The color change event
+ */
+ protected void colorChanged(ColorChangeEvent event) {
+ setColor(event.getColor());
+ fireColorChanged();
+ }
+
+ /**
+ * Notifies the listeners that the selected color has changed
+ */
+ public void fireColorChanged() {
+ fireEvent(new ColorChangeEvent(this, color));
+ }
+
+ /**
+ * The style for the popup window
+ *
+ * @param style
+ * The style
+ */
+ public void setPopupStyle(PopupStyle style) {
+ popupStyle = style;
+
+ switch (style) {
+ case POPUP_NORMAL: {
+ setRGBVisibility(true);
+ setHSVVisibility(true);
+ setSwatchesVisibility(true);
+ setHistoryVisibility(true);
+ setTextfieldVisibility(true);
+ break;
+ }
+
+ case POPUP_SIMPLE: {
+ setRGBVisibility(false);
+ setHSVVisibility(false);
+ setSwatchesVisibility(true);
+ setHistoryVisibility(false);
+ setTextfieldVisibility(false);
+ break;
+ }
+ }
+ }
+
+ /**
+ * Gets the style for the popup window
+ *
+ * @since 7.5.0
+ * @return popup window style
+ */
+ public PopupStyle getPopupStyle() {
+ return popupStyle;
+ }
+
+ /**
+ * Set the visibility of the RGB Tab
+ *
+ * @param visible
+ * The visibility
+ */
+ public void setRGBVisibility(boolean visible) {
+
+ if (!visible && !hsvVisible && !swatchesVisible) {
+ throw new IllegalArgumentException("Cannot hide all tabs.");
+ }
+
+ rgbVisible = visible;
+ if (window != null) {
+ window.setRGBTabVisible(visible);
+ }
+ }
+
+ /**
+ * Gets the visibility of the RGB Tab
+ *
+ * @since 7.5.0
+ * @return visibility of the RGB tab
+ */
+ public boolean getRGBVisibility() {
+ return rgbVisible;
+ }
+
+ /**
+ * Set the visibility of the HSV Tab
+ *
+ * @param visible
+ * The visibility
+ */
+ public void setHSVVisibility(boolean visible) {
+ if (!visible && !rgbVisible && !swatchesVisible) {
+ throw new IllegalArgumentException("Cannot hide all tabs.");
+ }
+
+ hsvVisible = visible;
+ if (window != null) {
+ window.setHSVTabVisible(visible);
+ }
+ }
+
+ /**
+ * Gets the visibility of the HSV Tab
+ *
+ * @since 7.5.0
+ * @return visibility of the HSV tab
+ */
+ public boolean getHSVVisibility() {
+ return hsvVisible;
+ }
+
+ /**
+ * Set the visibility of the Swatches Tab
+ *
+ * @param visible
+ * The visibility
+ */
+ public void setSwatchesVisibility(boolean visible) {
+ if (!visible && !hsvVisible && !rgbVisible) {
+ throw new IllegalArgumentException("Cannot hide all tabs.");
+ }
+
+ swatchesVisible = visible;
+ if (window != null) {
+ window.setSwatchesTabVisible(visible);
+ }
+ }
+
+ /**
+ * Gets the visibility of the Swatches Tab
+ *
+ * @since 7.5.0
+ * @return visibility of the swatches tab
+ */
+ public boolean getSwatchesVisibility() {
+ return swatchesVisible;
+ }
+
+ /**
+ * Sets the visibility of the Color History
+ *
+ * @param visible
+ * The visibility
+ */
+ public void setHistoryVisibility(boolean visible) {
+ historyVisible = visible;
+ if (window != null) {
+ window.setHistoryVisible(visible);
+ }
+ }
+
+ /**
+ * Gets the visibility of the Color History
+ *
+ * @since 7.5.0
+ * @return visibility of color history
+ */
+ public boolean getHistoryVisibility() {
+ return historyVisible;
+ }
+
+ /**
+ * Sets the visibility of the CSS color code text field
+ *
+ * @param visible
+ * The visibility
+ */
+ public void setTextfieldVisibility(boolean visible) {
+ textfieldVisible = visible;
+ if (window != null) {
+ window.setPreviewVisible(visible);
+ }
+ }
+
+ /**
+ * Gets the visibility of CSS color code text field
+ *
+ * @since 7.5.0
+ * @return visibility of css color code text field
+ */
+ public boolean getTextfieldVisibility() {
+ return textfieldVisible;
+ }
+
+ @Override
+ protected ColorPickerState getState() {
+ return (ColorPickerState) super.getState();
+ }
+
+ @Override
+ protected ColorPickerState getState(boolean markAsDirty) {
+ return (ColorPickerState) super.getState(markAsDirty);
+ }
+
+ /**
+ * Sets the default styles of the component
+ *
+ */
+ abstract protected void setDefaultStyles();
+
+ /**
+ * Shows a popup-window for color selection.
+ */
+ public void showPopup() {
+ showPopup(true);
+ }
+
+ /**
+ * Hides a popup-window for color selection.
+ */
+ public void hidePopup() {
+ showPopup(false);
+ }
+
+ /**
+ * Shows or hides popup-window depending on the given parameter. If there is
+ * no such window yet, one is created.
+ *
+ * @param open
+ */
+ protected void showPopup(boolean open) {
+ if (open && !isReadOnly()) {
+ if (parent == null) {
+ parent = getUI();
+ }
+
+ if (window == null) {
+
+ // Create the popup
+ window = new ColorPickerPopup(color);
+ window.setCaption(popupCaption);
+
+ window.setRGBTabVisible(rgbVisible);
+ window.setHSVTabVisible(hsvVisible);
+ window.setSwatchesTabVisible(swatchesVisible);
+ window.setHistoryVisible(historyVisible);
+ window.setPreviewVisible(textfieldVisible);
+
+ window.setImmediate(true);
+ window.addCloseListener(this);
+ window.addColorChangeListener(new ColorChangeListener() {
+ @Override
+ public void colorChanged(ColorChangeEvent event) {
+ AbstractColorPicker.this.colorChanged(event);
+ }
+ });
+
+ window.getHistory().setColor(color);
+ parent.addWindow(window);
+ window.setVisible(true);
+ window.setPositionX(positionX);
+ window.setPositionY(positionY);
+
+ } else if (!parent.equals(window.getParent())) {
+
+ window.setRGBTabVisible(rgbVisible);
+ window.setHSVTabVisible(hsvVisible);
+ window.setSwatchesTabVisible(swatchesVisible);
+ window.setHistoryVisible(historyVisible);
+ window.setPreviewVisible(textfieldVisible);
+
+ window.setColor(color);
+ window.getHistory().setColor(color);
+ window.setVisible(true);
+ parent.addWindow(window);
+ }
+
+ } else if (window != null) {
+ window.setVisible(false);
+ parent.removeWindow(window);
+ }
+ getState().popupVisible = open;
+ }
+
+ /**
+ * Set whether the caption text is rendered as HTML or not. You might need
+ * to re-theme component to allow higher content than the original text
+ * style.
+ *
+ * If set to true, the captions are passed to the browser as html and the
+ * developer is responsible for ensuring no harmful html is used. If set to
+ * false, the content is passed to the browser as plain text.
+ *
+ * @param htmlContentAllowed
+ * <code>true</code> if caption is rendered as HTML,
+ * <code>false</code> otherwise
+ * @deprecated as of , use {@link #setCaptionAsHtml(boolean)} instead
+ */
+ @Deprecated
+ public void setHtmlContentAllowed(boolean htmlContentAllowed) {
+ setCaptionAsHtml(htmlContentAllowed);
+ }
+
+ /**
+ * Return HTML rendering setting
+ *
+ * @return <code>true</code> if the caption text is to be rendered as HTML,
+ * <code>false</code> otherwise
+ * @deprecated as of , use {@link #isCaptionAsHtml()} instead
+ */
+ @Deprecated
+ public boolean isHtmlContentAllowed() {
+ return isCaptionAsHtml();
+ }
+
+ @Override
+ public void readDesign(Element design, DesignContext designContext) {
+ super.readDesign(design, designContext);
+
+ Attributes attributes = design.attributes();
+ if (design.hasAttr("color")) {
+ // Ignore the # character
+ String hexColor = DesignAttributeHandler
+ .readAttribute("color", attributes, String.class)
+ .substring(1);
+ setColor(new Color(Integer.parseInt(hexColor, 16)));
+ }
+ if (design.hasAttr("popup-style")) {
+ setPopupStyle(PopupStyle.valueOf(
+ "POPUP_" + attributes.get("popup-style").toUpperCase()));
+ }
+ if (design.hasAttr("position")) {
+ String[] position = attributes.get("position").split(",");
+ setPosition(Integer.parseInt(position[0]),
+ Integer.parseInt(position[1]));
+ }
+ }
+
+ @Override
+ public void writeDesign(Element design, DesignContext designContext) {
+ super.writeDesign(design, designContext);
+
+ Attributes attribute = design.attributes();
+ DesignAttributeHandler.writeAttribute("color", attribute,
+ color.getCSS(), Color.WHITE.getCSS(), String.class);
+ DesignAttributeHandler.writeAttribute("popup-style", attribute,
+ (popupStyle == PopupStyle.POPUP_NORMAL ? "normal" : "simple"),
+ "normal", String.class);
+ DesignAttributeHandler.writeAttribute("position", attribute,
+ positionX + "," + positionY, "0,0", String.class);
+ }
+
+ @Override
+ protected Collection<String> getCustomAttributes() {
+ Collection<String> result = super.getCustomAttributes();
+ result.add("color");
+ result.add("position");
+ result.add("popup-style");
+ return result;
+ }
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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 com.vaadin.shared.ui.colorpicker.Color;
+
+/**
+ * A class that defines default (button-like) implementation for a color picker
+ * component.
+ *
+ * @since 7.0.0
+ *
+ * @see ColorPickerArea
+ *
+ */
+public class ColorPicker extends AbstractColorPicker {
+
+ /**
+ * Instantiates a new color picker.
+ */
+ public ColorPicker() {
+ super();
+ }
+
+ /**
+ * Instantiates a new color picker.
+ *
+ * @param popupCaption
+ * caption of the color select popup
+ */
+ public ColorPicker(String popupCaption) {
+ super(popupCaption);
+ }
+
+ /**
+ * Instantiates a new color picker.
+ *
+ * @param popupCaption
+ * caption of the color select popup
+ * @param initialColor
+ * the initial color
+ */
+ public ColorPicker(String popupCaption, Color initialColor) {
+ super(popupCaption, initialColor);
+ setDefaultCaptionEnabled(true);
+ }
+
+ @Override
+ protected void setDefaultStyles() {
+ setPrimaryStyleName(STYLENAME_BUTTON);
+ addStyleName(STYLENAME_DEFAULT);
+ }
+
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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 com.vaadin.shared.ui.colorpicker.Color;
+
+/**
+ * A class that defines area-like implementation for a color picker component.
+ *
+ * @since 7.0.0
+ *
+ * @see ColorPicker
+ *
+ */
+public class ColorPickerArea extends AbstractColorPicker {
+
+ /**
+ * Instantiates a new color picker.
+ */
+ public ColorPickerArea() {
+ super();
+ }
+
+ /**
+ * Instantiates a new color picker.
+ *
+ * @param popupCaption
+ * caption of the color select popup
+ */
+ public ColorPickerArea(String popupCaption) {
+ super(popupCaption);
+ }
+
+ /**
+ * Instantiates a new color picker.
+ *
+ * @param popupCaption
+ * caption of the color select popup
+ * @param initialColor
+ * the initial color
+ */
+ public ColorPickerArea(String popupCaption, Color initialColor) {
+ super(popupCaption, initialColor);
+ setDefaultCaptionEnabled(false);
+ }
+
+ @Override
+ protected void setDefaultStyles() {
+ // state already has correct default
+ }
+
+ @Override
+ public void beforeClientResponse(boolean initial) {
+ super.beforeClientResponse(initial);
+
+ if ("".equals(getState().height)) {
+ getState().height = "30px";
+ }
+ if ("".equals(getState().width)) {
+ getState().width = "30px";
+ }
+ }
+
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.components.colorpicker;
+
+import com.vaadin.shared.ui.colorpicker.Color;
+import com.vaadin.ui.Component;
+import com.vaadin.ui.Component.Event;
+
+/**
+ * The color changed event which is passed to the listeners when a color change
+ * occurs.
+ *
+ * @since 7.0.0
+ */
+public class ColorChangeEvent extends Event {
+ private final Color color;
+
+ public ColorChangeEvent(Component source, Color color) {
+ super(source);
+
+ this.color = color;
+ }
+
+ /**
+ * Returns the new color.
+ */
+ public Color getColor() {
+ return color;
+ }
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.components.colorpicker;
+
+import java.io.Serializable;
+
+/**
+ * The listener interface for receiving colorChange events. The class that is
+ * interested in processing a {@link ColorChangeEvent} implements this
+ * interface, and the object created with that class is registered with a
+ * component using the component's <code>addColorChangeListener</code> method.
+ * When the colorChange event occurs, that object's appropriate method is
+ * invoked.
+ *
+ * @since 7.0.0
+ *
+ * @see ColorChangeEvent
+ */
+public interface ColorChangeListener extends Serializable {
+
+ /**
+ * Called when a new color has been selected.
+ *
+ * @param event
+ * An event containing information about the color change.
+ */
+ void colorChanged(ColorChangeEvent event);
+
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.components.colorpicker;
+
+import java.lang.reflect.Method;
+
+import com.vaadin.shared.ui.colorpicker.Color;
+import com.vaadin.shared.ui.colorpicker.ColorPickerGradientServerRpc;
+import com.vaadin.shared.ui.colorpicker.ColorPickerGradientState;
+import com.vaadin.ui.AbstractComponent;
+import com.vaadin.ui.AbstractColorPicker.Coordinates2Color;
+
+/**
+ * A component that represents a color gradient within a color picker.
+ *
+ * @since 7.0.0
+ */
+public class ColorPickerGradient extends AbstractComponent
+ implements ColorSelector {
+
+ private static final Method COLOR_CHANGE_METHOD;
+ static {
+ try {
+ COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
+ "colorChanged", new Class[] { ColorChangeEvent.class });
+ } catch (final java.lang.NoSuchMethodException e) {
+ // This should never happen
+ throw new java.lang.RuntimeException(
+ "Internal error finding methods in ColorPicker");
+ }
+ }
+
+ private ColorPickerGradientServerRpc rpc = new ColorPickerGradientServerRpc() {
+
+ @Override
+ public void select(int cursorX, int cursorY) {
+ x = cursorX;
+ y = cursorY;
+ color = converter.calculate(x, y);
+
+ fireColorChanged(color);
+ }
+ };
+
+ /** The converter. */
+ private Coordinates2Color converter;
+
+ /** The foreground color. */
+ private Color color;
+
+ /** The x-coordinate. */
+ private int x = 0;
+
+ /** The y-coordinate. */
+ private int y = 0;
+
+ private ColorPickerGradient() {
+ registerRpc(rpc);
+ // width and height must be set here instead of in theme, otherwise
+ // coordinate calculations fail
+ getState().width = "220px";
+ getState().height = "220px";
+ }
+
+ /**
+ * Instantiates a new color picker gradient.
+ *
+ * @param id
+ * the id
+ * @param converter
+ * the converter
+ */
+ public ColorPickerGradient(String id, Coordinates2Color converter) {
+ this();
+ addStyleName(id);
+ this.converter = converter;
+ }
+
+ @Override
+ public void setColor(Color c) {
+ color = c;
+
+ int[] coords = converter.calculate(c);
+ x = coords[0];
+ y = coords[1];
+
+ getState().cursorX = x;
+ getState().cursorY = y;
+
+ }
+
+ @Override
+ public void addColorChangeListener(ColorChangeListener listener) {
+ addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
+ }
+
+ @Override
+ public void removeColorChangeListener(ColorChangeListener listener) {
+ removeListener(ColorChangeEvent.class, listener);
+ }
+
+ /**
+ * Sets the background color.
+ *
+ * @param color
+ * the new background color
+ */
+ public void setBackgroundColor(Color color) {
+ getState().bgColor = color.getCSS();
+ }
+
+ @Override
+ public Color getColor() {
+ return color;
+ }
+
+ /**
+ * Notifies the listeners that the color has changed
+ *
+ * @param color
+ * The color which it changed to
+ */
+ public void fireColorChanged(Color color) {
+ fireEvent(new ColorChangeEvent(this, color));
+ }
+
+ @Override
+ protected ColorPickerGradientState getState() {
+ return (ColorPickerGradientState) super.getState();
+ }
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.components.colorpicker;
+
+import java.awt.Point;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+import com.vaadin.shared.ui.colorpicker.Color;
+import com.vaadin.shared.ui.colorpicker.ColorPickerGridServerRpc;
+import com.vaadin.shared.ui.colorpicker.ColorPickerGridState;
+import com.vaadin.ui.AbstractComponent;
+
+/**
+ * A component that represents a color selection grid within a color picker.
+ *
+ * @since 7.0.0
+ */
+public class ColorPickerGrid extends AbstractComponent
+ implements ColorSelector {
+
+ private static final String STYLENAME = "v-colorpicker-grid";
+
+ private static final Method COLOR_CHANGE_METHOD;
+ static {
+ try {
+ COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
+ "colorChanged", new Class[] { ColorChangeEvent.class });
+ } catch (final java.lang.NoSuchMethodException e) {
+ // This should never happen
+ throw new java.lang.RuntimeException(
+ "Internal error finding methods in ColorPicker");
+ }
+ }
+
+ private ColorPickerGridServerRpc rpc = new ColorPickerGridServerRpc() {
+
+ @Override
+ public void select(int x, int y) {
+ ColorPickerGrid.this.x = x;
+ ColorPickerGrid.this.y = y;
+
+ fireColorChanged(colorGrid[y][x]);
+ }
+
+ @Override
+ public void refresh() {
+ for (int row = 0; row < rows; row++) {
+ for (int col = 0; col < columns; col++) {
+ changedColors.put(new Point(row, col), colorGrid[row][col]);
+ }
+ }
+ sendChangedColors();
+ markAsDirty();
+ }
+ };
+
+ /** The x-coordinate. */
+ private int x = 0;
+
+ /** The y-coordinate. */
+ private int y = 0;
+
+ /** The rows. */
+ private int rows;
+
+ /** The columns. */
+ private int columns;
+
+ /** The color grid. */
+ private Color[][] colorGrid = new Color[1][1];
+
+ /** The changed colors. */
+ private final Map<Point, Color> changedColors = new HashMap<Point, Color>();
+
+ /**
+ * Instantiates a new color picker grid.
+ */
+ public ColorPickerGrid() {
+ registerRpc(rpc);
+ setPrimaryStyleName(STYLENAME);
+ setColorGrid(new Color[1][1]);
+ setColor(Color.WHITE);
+ }
+
+ /**
+ * Instantiates a new color picker grid.
+ *
+ * @param rows
+ * the rows
+ * @param columns
+ * the columns
+ */
+ public ColorPickerGrid(int rows, int columns) {
+ registerRpc(rpc);
+ setPrimaryStyleName(STYLENAME);
+ setColorGrid(new Color[rows][columns]);
+ setColor(Color.WHITE);
+ }
+
+ /**
+ * Instantiates a new color picker grid.
+ *
+ * @param colors
+ * the colors
+ */
+ public ColorPickerGrid(Color[][] colors) {
+ registerRpc(rpc);
+ setPrimaryStyleName(STYLENAME);
+ setColorGrid(colors);
+ }
+
+ private void setColumnCount(int columns) {
+ this.columns = columns;
+ getState().columnCount = columns;
+ }
+
+ private void setRowCount(int rows) {
+ this.rows = rows;
+ getState().rowCount = rows;
+ }
+
+ private void sendChangedColors() {
+ if (!changedColors.isEmpty()) {
+ String[] colors = new String[changedColors.size()];
+ String[] XCoords = new String[changedColors.size()];
+ String[] YCoords = new String[changedColors.size()];
+ int counter = 0;
+ for (Point p : changedColors.keySet()) {
+ Color c = changedColors.get(p);
+ if (c == null) {
+ continue;
+ }
+
+ String color = c.getCSS();
+
+ colors[counter] = color;
+ XCoords[counter] = String.valueOf((int) p.getX());
+ YCoords[counter] = String.valueOf((int) p.getY());
+ counter++;
+ }
+ getState().changedColor = colors;
+ getState().changedX = XCoords;
+ getState().changedY = YCoords;
+
+ changedColors.clear();
+ }
+ }
+
+ /**
+ * Sets the color grid.
+ *
+ * @param colors
+ * the new color grid
+ */
+ public void setColorGrid(Color[][] colors) {
+ setRowCount(colors.length);
+ setColumnCount(colors[0].length);
+ colorGrid = colors;
+
+ for (int row = 0; row < rows; row++) {
+ for (int col = 0; col < columns; col++) {
+ changedColors.put(new Point(row, col), colorGrid[row][col]);
+ }
+ }
+ sendChangedColors();
+
+ markAsDirty();
+ }
+
+ /**
+ * Adds a color change listener
+ *
+ * @param listener
+ * The color change listener
+ */
+ @Override
+ public void addColorChangeListener(ColorChangeListener listener) {
+ addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
+ }
+
+ @Override
+ public Color getColor() {
+ return colorGrid[x][y];
+ }
+
+ /**
+ * Removes a color change listener
+ *
+ * @param listener
+ * The listener
+ */
+ @Override
+ public void removeColorChangeListener(ColorChangeListener listener) {
+ removeListener(ColorChangeEvent.class, listener);
+ }
+
+ @Override
+ public void setColor(Color color) {
+ colorGrid[x][y] = color;
+ changedColors.put(new Point(x, y), color);
+ sendChangedColors();
+ markAsDirty();
+ }
+
+ /**
+ * Sets the position.
+ *
+ * @param x
+ * the x
+ * @param y
+ * the y
+ */
+ public void setPosition(int x, int y) {
+ if (x >= 0 && x < columns && y >= 0 && y < rows) {
+ this.x = x;
+ this.y = y;
+ }
+ }
+
+ /**
+ * Gets the position.
+ *
+ * @return the position
+ */
+ public int[] getPosition() {
+ return new int[] { x, y };
+ }
+
+ /**
+ * Notifies the listeners that a color change has occurred
+ *
+ * @param color
+ * The color which it changed to
+ */
+ public void fireColorChanged(Color color) {
+ fireEvent(new ColorChangeEvent(this, color));
+ }
+
+ @Override
+ protected ColorPickerGridState getState() {
+ return (ColorPickerGridState) super.getState();
+ }
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.components.colorpicker;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.ArrayBlockingQueue;
+
+import com.vaadin.shared.ui.colorpicker.Color;
+import com.vaadin.ui.CustomComponent;
+
+/**
+ * A component that represents color selection history within a color picker.
+ *
+ * @since 7.0.0
+ */
+public class ColorPickerHistory extends CustomComponent
+ implements ColorSelector, ColorChangeListener {
+
+ private static final String STYLENAME = "v-colorpicker-history";
+
+ private static final Method COLOR_CHANGE_METHOD;
+ static {
+ try {
+ COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
+ "colorChanged", new Class[] { ColorChangeEvent.class });
+ } catch (final java.lang.NoSuchMethodException e) {
+ // This should never happen
+ throw new java.lang.RuntimeException(
+ "Internal error finding methods in ColorPicker");
+ }
+ }
+
+ /** The rows. */
+ private static final int rows = 4;
+
+ /** The columns. */
+ private static final int columns = 15;
+
+ /** Temporary color history for when the component is detached. */
+ private ArrayBlockingQueue<Color> tempHistory = new ArrayBlockingQueue<Color>(
+ rows * columns);
+
+ /** The grid. */
+ private final ColorPickerGrid grid;
+
+ /**
+ * Instantiates a new color picker history.
+ */
+ public ColorPickerHistory() {
+ setPrimaryStyleName(STYLENAME);
+
+ grid = new ColorPickerGrid(rows, columns);
+ grid.setWidth("100%");
+ grid.setPosition(0, 0);
+ grid.addColorChangeListener(this);
+
+ setCompositionRoot(grid);
+ }
+
+ @Override
+ public void attach() {
+ super.attach();
+ createColorHistoryIfNecessary();
+ }
+
+ private void createColorHistoryIfNecessary() {
+ List<Color> tempColors = new ArrayList<Color>(tempHistory);
+ if (getSession().getAttribute("colorPickerHistory") == null) {
+ getSession().setAttribute("colorPickerHistory",
+ new ArrayBlockingQueue<Color>(rows * columns));
+ }
+ for (Color color : tempColors) {
+ setColor(color);
+ }
+ tempHistory.clear();
+ }
+
+ @SuppressWarnings("unchecked")
+ private ArrayBlockingQueue<Color> getColorHistory() {
+ if (isAttached()) {
+ Object colorHistory = getSession()
+ .getAttribute("colorPickerHistory");
+ if (colorHistory instanceof ArrayBlockingQueue<?>) {
+ return (ArrayBlockingQueue<Color>) colorHistory;
+ }
+ }
+ return tempHistory;
+ }
+
+ @Override
+ public void setHeight(String height) {
+ super.setHeight(height);
+ grid.setHeight(height);
+ }
+
+ @Override
+ public void setColor(Color color) {
+
+ ArrayBlockingQueue<Color> colorHistory = getColorHistory();
+
+ // Check that the color does not already exist
+ boolean exists = false;
+ Iterator<Color> iter = colorHistory.iterator();
+ while (iter.hasNext()) {
+ if (color.equals(iter.next())) {
+ exists = true;
+ break;
+ }
+ }
+
+ // If the color does not exist then add it
+ if (!exists) {
+ if (!colorHistory.offer(color)) {
+ colorHistory.poll();
+ colorHistory.offer(color);
+ }
+ }
+
+ List<Color> colorList = new ArrayList<Color>(colorHistory);
+
+ // Invert order of colors
+ Collections.reverse(colorList);
+
+ // Move the selected color to the front of the list
+ Collections.swap(colorList, colorList.indexOf(color), 0);
+
+ // Create 2d color map
+ Color[][] colors = new Color[rows][columns];
+ iter = colorList.iterator();
+
+ for (int row = 0; row < rows; row++) {
+ for (int col = 0; col < columns; col++) {
+ if (iter.hasNext()) {
+ colors[row][col] = iter.next();
+ } else {
+ colors[row][col] = Color.WHITE;
+ }
+ }
+ }
+
+ grid.setColorGrid(colors);
+ grid.markAsDirty();
+ }
+
+ @Override
+ public Color getColor() {
+ return getColorHistory().peek();
+ }
+
+ /**
+ * Gets the history.
+ *
+ * @return the history
+ */
+ public List<Color> getHistory() {
+ ArrayBlockingQueue<Color> colorHistory = getColorHistory();
+ Color[] array = colorHistory.toArray(new Color[colorHistory.size()]);
+ return Collections.unmodifiableList(Arrays.asList(array));
+ }
+
+ /**
+ * Checks if the history contains given color.
+ *
+ * @param c
+ * the color
+ *
+ * @return true, if successful
+ */
+ public boolean hasColor(Color c) {
+ return getColorHistory().contains(c);
+ }
+
+ /**
+ * Adds a color change listener
+ *
+ * @param listener
+ * The listener
+ */
+ @Override
+ public void addColorChangeListener(ColorChangeListener listener) {
+ addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
+ }
+
+ /**
+ * Removes a color change listener
+ *
+ * @param listener
+ * The listener
+ */
+ @Override
+ public void removeColorChangeListener(ColorChangeListener listener) {
+ removeListener(ColorChangeEvent.class, listener);
+ }
+
+ @Override
+ public void colorChanged(ColorChangeEvent event) {
+ fireEvent(new ColorChangeEvent(this, event.getColor()));
+ }
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.components.colorpicker;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import com.vaadin.shared.ui.MarginInfo;
+import com.vaadin.shared.ui.colorpicker.Color;
+import com.vaadin.ui.Alignment;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.Button.ClickListener;
+import com.vaadin.ui.Component;
+import com.vaadin.ui.HorizontalLayout;
+import com.vaadin.ui.Layout;
+import com.vaadin.ui.Slider;
+import com.vaadin.ui.Slider.ValueOutOfBoundsException;
+import com.vaadin.ui.TabSheet;
+import com.vaadin.ui.VerticalLayout;
+import com.vaadin.ui.Window;
+import com.vaadin.ui.AbstractColorPicker.Coordinates2Color;
+
+/**
+ * A component that represents color selection popup within a color picker.
+ *
+ * @since 7.0.0
+ */
+public class ColorPickerPopup extends Window
+ implements ClickListener, ColorChangeListener, ColorSelector {
+
+ private static final String STYLENAME = "v-colorpicker-popup";
+
+ private static final Method COLOR_CHANGE_METHOD;
+ static {
+ try {
+ COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
+ "colorChanged", new Class[] { ColorChangeEvent.class });
+ } catch (final java.lang.NoSuchMethodException e) {
+ // This should never happen
+ throw new java.lang.RuntimeException(
+ "Internal error finding methods in ColorPicker");
+ }
+ }
+
+ /** The tabs. */
+ private final TabSheet tabs = new TabSheet();
+
+ private Component rgbTab;
+
+ private Component hsvTab;
+
+ private Component swatchesTab;
+
+ /** The layout. */
+ private final VerticalLayout layout;
+
+ /** The ok button. */
+ private final Button ok = new Button("OK");
+
+ /** The cancel button. */
+ private final Button cancel = new Button("Cancel");
+
+ /** The resize button. */
+ private final Button resize = new Button("show/hide history");
+
+ /** The selected color. */
+ private Color selectedColor = Color.WHITE;
+
+ /** The history. */
+ private ColorPickerHistory history;
+
+ /** The history container. */
+ private Layout historyContainer;
+
+ /** The rgb gradient. */
+ private ColorPickerGradient rgbGradient;
+
+ /** The hsv gradient. */
+ private ColorPickerGradient hsvGradient;
+
+ /** The red slider. */
+ private Slider redSlider;
+
+ /** The green slider. */
+ private Slider greenSlider;
+
+ /** The blue slider. */
+ private Slider blueSlider;
+
+ /** The hue slider. */
+ private Slider hueSlider;
+
+ /** The saturation slider. */
+ private Slider saturationSlider;
+
+ /** The value slider. */
+ private Slider valueSlider;
+
+ /** The preview on the rgb tab. */
+ private ColorPickerPreview rgbPreview;
+
+ /** The preview on the hsv tab. */
+ private ColorPickerPreview hsvPreview;
+
+ /** The preview on the swatches tab. */
+ private ColorPickerPreview selPreview;
+
+ /** The color select. */
+ private ColorPickerSelect colorSelect;
+
+ /** The selectors. */
+ private final Set<ColorSelector> selectors = new HashSet<ColorSelector>();
+
+ /**
+ * Set true while the slider values are updated after colorChange. When
+ * true, valueChange reactions from the sliders are disabled, because
+ * otherwise the set color may become corrupted as it is repeatedly re-set
+ * in valueChangeListeners using values from sliders that may not have been
+ * updated yet.
+ */
+ private boolean updatingColors = false;
+
+ private ColorPickerPopup() {
+ // Set the layout
+ layout = new VerticalLayout();
+ layout.setSpacing(false);
+ layout.setMargin(false);
+ layout.setWidth("100%");
+ layout.setHeight(null);
+
+ setContent(layout);
+ setStyleName(STYLENAME);
+ setResizable(false);
+ setImmediate(true);
+ // Create the history
+ history = new ColorPickerHistory();
+ history.addColorChangeListener(this);
+ }
+
+ /**
+ * Instantiates a new color picker popup.
+ */
+ public ColorPickerPopup(Color initialColor) {
+ this();
+ selectedColor = initialColor;
+ initContents();
+ }
+
+ private void initContents() {
+ // Create the preview on the rgb tab
+ rgbPreview = new ColorPickerPreview(selectedColor);
+ rgbPreview.setWidth("240px");
+ rgbPreview.setHeight("20px");
+ rgbPreview.addColorChangeListener(this);
+ selectors.add(rgbPreview);
+
+ // Create the preview on the hsv tab
+ hsvPreview = new ColorPickerPreview(selectedColor);
+ hsvPreview.setWidth("240px");
+ hsvPreview.setHeight("20px");
+ hsvPreview.addColorChangeListener(this);
+ selectors.add(hsvPreview);
+
+ // Create the preview on the swatches tab
+ selPreview = new ColorPickerPreview(selectedColor);
+ selPreview.setWidth("100%");
+ selPreview.setHeight("20px");
+ selPreview.addColorChangeListener(this);
+ selectors.add(selPreview);
+
+ // Create the tabs
+ rgbTab = createRGBTab(selectedColor);
+ tabs.addTab(rgbTab, "RGB", null);
+
+ hsvTab = createHSVTab(selectedColor);
+ tabs.addTab(hsvTab, "HSV", null);
+
+ swatchesTab = createSelectTab();
+ tabs.addTab(swatchesTab, "Swatches", null);
+
+ // Add the tabs
+ tabs.setWidth("100%");
+
+ layout.addComponent(tabs);
+
+ // Add the history
+ history.setWidth("97%");
+ history.setHeight("22px");
+
+ // Create the default colors
+ List<Color> defaultColors = new ArrayList<Color>();
+ defaultColors.add(Color.BLACK);
+ defaultColors.add(Color.WHITE);
+
+ // Create the history
+ VerticalLayout innerContainer = new VerticalLayout();
+ innerContainer.setWidth("100%");
+ innerContainer.setHeight(null);
+ innerContainer.addComponent(history);
+
+ VerticalLayout outerContainer = new VerticalLayout();
+ outerContainer.setWidth("99%");
+ outerContainer.setHeight("27px");
+ outerContainer.addComponent(innerContainer);
+ historyContainer = outerContainer;
+
+ layout.addComponent(historyContainer);
+
+ // Add the resize button for the history
+ resize.addClickListener(this);
+ resize.setData(new Boolean(false));
+ resize.setWidth("100%");
+ resize.setHeight("10px");
+ resize.setPrimaryStyleName("resize-button");
+ layout.addComponent(resize);
+
+ // Add the buttons
+ ok.setWidth("70px");
+ ok.addClickListener(this);
+
+ cancel.setWidth("70px");
+ cancel.addClickListener(this);
+
+ HorizontalLayout buttons = new HorizontalLayout();
+ buttons.addComponent(ok);
+ buttons.addComponent(cancel);
+ buttons.setWidth("100%");
+ buttons.setHeight("30px");
+ buttons.setComponentAlignment(ok, Alignment.MIDDLE_CENTER);
+ buttons.setComponentAlignment(cancel, Alignment.MIDDLE_CENTER);
+ layout.addComponent(buttons);
+ }
+
+ /**
+ * Creates the RGB tab.
+ *
+ * @return the component
+ */
+ private Component createRGBTab(Color color) {
+ VerticalLayout rgbLayout = new VerticalLayout();
+ rgbLayout.setMargin(new MarginInfo(false, false, true, false));
+ rgbLayout.addComponent(rgbPreview);
+ rgbLayout.setStyleName("rgbtab");
+
+ // Add the RGB color gradient
+ rgbGradient = new ColorPickerGradient("rgb-gradient", RGBConverter);
+ rgbGradient.setColor(color);
+ rgbGradient.addColorChangeListener(this);
+ rgbLayout.addComponent(rgbGradient);
+ selectors.add(rgbGradient);
+
+ // Add the RGB sliders
+ VerticalLayout sliders = new VerticalLayout();
+ sliders.setStyleName("rgb-sliders");
+
+ redSlider = createRGBSlider("Red", "red");
+ greenSlider = createRGBSlider("Green", "green");
+ blueSlider = createRGBSlider("Blue", "blue");
+ setRgbSliderValues(color);
+
+ redSlider.addValueChangeListener(e -> {
+ double red = e.getValue();
+ if (!updatingColors) {
+ Color newColor = new Color((int) red, selectedColor.getGreen(),
+ selectedColor.getBlue());
+ setColor(newColor);
+ }
+ });
+
+ sliders.addComponent(redSlider);
+
+ greenSlider.addValueChangeListener(e -> {
+ double green = e.getValue();
+ if (!updatingColors) {
+ Color newColor = new Color(selectedColor.getRed(), (int) green,
+ selectedColor.getBlue());
+ setColor(newColor);
+ }
+ });
+ sliders.addComponent(greenSlider);
+
+ blueSlider.addValueChangeListener(e -> {
+ double blue = e.getValue();
+ if (!updatingColors) {
+ Color newColor = new Color(selectedColor.getRed(),
+ selectedColor.getGreen(), (int) blue);
+ setColor(newColor);
+ }
+ });
+ sliders.addComponent(blueSlider);
+
+ rgbLayout.addComponent(sliders);
+
+ return rgbLayout;
+ }
+
+ private Slider createRGBSlider(String caption, String styleName) {
+ Slider redSlider = new Slider(caption, 0, 255);
+ redSlider.setImmediate(true);
+ redSlider.setStyleName("rgb-slider");
+ redSlider.setWidth("220px");
+ redSlider.addStyleName(styleName);
+ return redSlider;
+ }
+
+ /**
+ * Creates the hsv tab.
+ *
+ * @return the component
+ */
+ private Component createHSVTab(Color color) {
+ VerticalLayout hsvLayout = new VerticalLayout();
+ hsvLayout.setMargin(new MarginInfo(false, false, true, false));
+ hsvLayout.addComponent(hsvPreview);
+ hsvLayout.setStyleName("hsvtab");
+
+ // Add the hsv gradient
+ hsvGradient = new ColorPickerGradient("hsv-gradient", HSVConverter);
+ hsvGradient.setColor(color);
+ hsvGradient.addColorChangeListener(this);
+ hsvLayout.addComponent(hsvGradient);
+ selectors.add(hsvGradient);
+
+ VerticalLayout sliders = new VerticalLayout();
+ sliders.setStyleName("hsv-sliders");
+
+ hueSlider = new Slider("Hue", 0, 360);
+ saturationSlider = new Slider("Saturation", 0, 100);
+ valueSlider = new Slider("Value", 0, 100);
+
+ float[] hsv = color.getHSV();
+ setHsvSliderValues(hsv);
+
+ hueSlider.setStyleName("hsv-slider");
+ hueSlider.addStyleName("hue-slider");
+ hueSlider.setWidth("220px");
+ hueSlider.setImmediate(true);
+ hueSlider.addValueChangeListener(event -> {
+ if (!updatingColors) {
+ float hue = (Float.parseFloat(event.getValue().toString()))
+ / 360f;
+ float saturation = (Float
+ .parseFloat(saturationSlider.getValue().toString()))
+ / 100f;
+ float value = (Float
+ .parseFloat(valueSlider.getValue().toString())) / 100f;
+
+ // Set the color
+ Color newColor = new Color(
+ Color.HSVtoRGB(hue, saturation, value));
+ setColor(newColor);
+
+ /*
+ * Set the background color of the hue gradient. This has to be
+ * done here since in the conversion the base color information
+ * is lost when color is black/white
+ */
+ Color bgColor = new Color(Color.HSVtoRGB(hue, 1f, 1f));
+ hsvGradient.setBackgroundColor(bgColor);
+ }
+ });
+ sliders.addComponent(hueSlider);
+
+ saturationSlider.setStyleName("hsv-slider");
+ saturationSlider.setWidth("220px");
+ saturationSlider.setImmediate(true);
+ saturationSlider.addValueChangeListener(event -> {
+ if (!updatingColors) {
+ float hue = (Float.parseFloat(hueSlider.getValue().toString()))
+ / 360f;
+ float saturation = (Float
+ .parseFloat(event.getValue().toString())) / 100f;
+ float value = (Float
+ .parseFloat(valueSlider.getValue().toString())) / 100f;
+ Color newColor = new Color(
+ Color.HSVtoRGB(hue, saturation, value));
+ setColor(newColor);
+ }
+ });
+ sliders.addComponent(saturationSlider);
+
+ valueSlider.setStyleName("hsv-slider");
+ valueSlider.setWidth("220px");
+ valueSlider.setImmediate(true);
+ valueSlider.addValueChangeListener(event -> {
+ if (!updatingColors) {
+ float hue = (Float.parseFloat(hueSlider.getValue().toString()))
+ / 360f;
+ float saturation = (Float
+ .parseFloat(saturationSlider.getValue().toString()))
+ / 100f;
+ float value = (Float.parseFloat(event.getValue().toString()))
+ / 100f;
+
+ Color newColor = new Color(
+ Color.HSVtoRGB(hue, saturation, value));
+ setColor(newColor);
+ }
+ });
+
+ sliders.addComponent(valueSlider);
+ hsvLayout.addComponent(sliders);
+
+ return hsvLayout;
+ }
+
+ /**
+ * Creates the select tab.
+ *
+ * @return the component
+ */
+ private Component createSelectTab() {
+ VerticalLayout selLayout = new VerticalLayout();
+ selLayout.setMargin(new MarginInfo(false, false, true, false));
+ selLayout.addComponent(selPreview);
+ selLayout.addStyleName("seltab");
+
+ colorSelect = new ColorPickerSelect();
+ colorSelect.addColorChangeListener(this);
+ selLayout.addComponent(colorSelect);
+
+ return selLayout;
+ }
+
+ @Override
+ public void buttonClick(ClickEvent event) {
+ // History resize was clicked
+ if (event.getButton() == resize) {
+ boolean state = (Boolean) resize.getData();
+
+ // minimize
+ if (state) {
+ historyContainer.setHeight("27px");
+ history.setHeight("22px");
+
+ // maximize
+ } else {
+ historyContainer.setHeight("90px");
+ history.setHeight("85px");
+ }
+
+ resize.setData(new Boolean(!state));
+ }
+
+ // Ok button was clicked
+ else if (event.getButton() == ok) {
+ history.setColor(getColor());
+ fireColorChanged();
+ close();
+ }
+
+ // Cancel button was clicked
+ else if (event.getButton() == cancel) {
+ close();
+ }
+
+ }
+
+ /**
+ * Notifies the listeners that the color changed
+ */
+ public void fireColorChanged() {
+ fireEvent(new ColorChangeEvent(this, getColor()));
+ }
+
+ /**
+ * Gets the history.
+ *
+ * @return the history
+ */
+ public ColorPickerHistory getHistory() {
+ return history;
+ }
+
+ @Override
+ public void setColor(Color color) {
+ if (color == null) {
+ return;
+ }
+
+ selectedColor = color;
+
+ hsvGradient.setColor(selectedColor);
+ hsvPreview.setColor(selectedColor);
+
+ rgbGradient.setColor(selectedColor);
+ rgbPreview.setColor(selectedColor);
+
+ selPreview.setColor(selectedColor);
+ }
+
+ @Override
+ public Color getColor() {
+ return selectedColor;
+ }
+
+ /**
+ * Gets the color history.
+ *
+ * @return the color history
+ */
+ public List<Color> getColorHistory() {
+ return Collections.unmodifiableList(history.getHistory());
+ }
+
+ @Override
+ public void colorChanged(ColorChangeEvent event) {
+ setColor(event.getColor());
+
+ updatingColors = true;
+
+ setRgbSliderValues(selectedColor);
+ float[] hsv = selectedColor.getHSV();
+ setHsvSliderValues(hsv);
+
+ updatingColors = false;
+
+ for (ColorSelector s : selectors) {
+ if (event.getSource() != s && s != this
+ && s.getColor() != selectedColor) {
+ s.setColor(selectedColor);
+ }
+ }
+ }
+
+ private void setRgbSliderValues(Color color) {
+ try {
+ redSlider.setValue(((Integer) color.getRed()).doubleValue());
+ blueSlider.setValue(((Integer) color.getBlue()).doubleValue());
+ greenSlider.setValue(((Integer) color.getGreen()).doubleValue());
+ } catch (ValueOutOfBoundsException e) {
+ getLogger().log(Level.WARNING,
+ "Unable to set RGB color value to " + color.getRed() + ","
+ + color.getGreen() + "," + color.getBlue(),
+ e);
+ }
+ }
+
+ private void setHsvSliderValues(float[] hsv) {
+ try {
+ hueSlider.setValue(((Float) (hsv[0] * 360f)).doubleValue());
+ saturationSlider.setValue(((Float) (hsv[1] * 100f)).doubleValue());
+ valueSlider.setValue(((Float) (hsv[2] * 100f)).doubleValue());
+ } catch (ValueOutOfBoundsException e) {
+ getLogger().log(Level.WARNING, "Unable to set HSV color value to "
+ + hsv[0] + "," + hsv[1] + "," + hsv[2], e);
+ }
+ }
+
+ @Override
+ public void addColorChangeListener(ColorChangeListener listener) {
+ addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
+ }
+
+ @Override
+ public void removeColorChangeListener(ColorChangeListener listener) {
+ removeListener(ColorChangeEvent.class, listener);
+ }
+
+ /**
+ * Checks the visibility of the given tab
+ *
+ * @param tab
+ * The tab to check
+ * @return true if tab is visible, false otherwise
+ */
+ private boolean tabIsVisible(Component tab) {
+ Iterator<Component> tabIterator = tabs.getComponentIterator();
+ while (tabIterator.hasNext()) {
+ if (tabIterator.next() == tab) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * How many tabs are visible
+ *
+ * @return The number of tabs visible
+ */
+ private int tabsNumVisible() {
+ Iterator<Component> tabIterator = tabs.getComponentIterator();
+ int tabCounter = 0;
+ while (tabIterator.hasNext()) {
+ tabIterator.next();
+ tabCounter++;
+ }
+ return tabCounter;
+ }
+
+ /**
+ * Checks if tabs are needed and hides them if not
+ */
+ private void checkIfTabsNeeded() {
+ tabs.hideTabs(tabsNumVisible() == 1);
+ }
+
+ /**
+ * Set RGB tab visibility
+ *
+ * @param visible
+ * The visibility of the RGB tab
+ */
+ public void setRGBTabVisible(boolean visible) {
+ if (visible && !tabIsVisible(rgbTab)) {
+ tabs.addTab(rgbTab, "RGB", null);
+ checkIfTabsNeeded();
+ } else if (!visible && tabIsVisible(rgbTab)) {
+ tabs.removeComponent(rgbTab);
+ checkIfTabsNeeded();
+ }
+ }
+
+ /**
+ * Set HSV tab visibility
+ *
+ * @param visible
+ * The visibility of the HSV tab
+ */
+ public void setHSVTabVisible(boolean visible) {
+ if (visible && !tabIsVisible(hsvTab)) {
+ tabs.addTab(hsvTab, "HSV", null);
+ checkIfTabsNeeded();
+ } else if (!visible && tabIsVisible(hsvTab)) {
+ tabs.removeComponent(hsvTab);
+ checkIfTabsNeeded();
+ }
+ }
+
+ /**
+ * Set Swatches tab visibility
+ *
+ * @param visible
+ * The visibility of the Swatches tab
+ */
+ public void setSwatchesTabVisible(boolean visible) {
+ if (visible && !tabIsVisible(swatchesTab)) {
+ tabs.addTab(swatchesTab, "Swatches", null);
+ checkIfTabsNeeded();
+ } else if (!visible && tabIsVisible(swatchesTab)) {
+ tabs.removeComponent(swatchesTab);
+ checkIfTabsNeeded();
+ }
+ }
+
+ /**
+ * Set the History visibility
+ *
+ * @param visible
+ */
+ public void setHistoryVisible(boolean visible) {
+ historyContainer.setVisible(visible);
+ resize.setVisible(visible);
+ }
+
+ /**
+ * Set the preview visibility
+ *
+ * @param visible
+ */
+ public void setPreviewVisible(boolean visible) {
+ hsvPreview.setVisible(visible);
+ rgbPreview.setVisible(visible);
+ selPreview.setVisible(visible);
+ }
+
+ /** RGB color converter */
+ private Coordinates2Color RGBConverter = new Coordinates2Color() {
+
+ @Override
+ public Color calculate(int x, int y) {
+ float h = (x / 220f);
+ float s = 1f;
+ float v = 1f;
+
+ if (y < 110) {
+ s = y / 110f;
+ } else if (y > 110) {
+ v = 1f - (y - 110f) / 110f;
+ }
+
+ return new Color(Color.HSVtoRGB(h, s, v));
+ }
+
+ @Override
+ public int[] calculate(Color color) {
+
+ float[] hsv = color.getHSV();
+
+ int x = Math.round(hsv[0] * 220f);
+ int y = 0;
+
+ // lower half
+ if (hsv[1] == 1f) {
+ y = Math.round(110f - (hsv[1] + hsv[2]) * 110f);
+ } else {
+ y = Math.round(hsv[1] * 110f);
+ }
+
+ return new int[] { x, y };
+ }
+ };
+
+ /** HSV color converter */
+ Coordinates2Color HSVConverter = new Coordinates2Color() {
+ @Override
+ public int[] calculate(Color color) {
+
+ float[] hsv = color.getHSV();
+
+ // Calculate coordinates
+ int x = Math.round(hsv[2] * 220.0f);
+ int y = Math.round(220 - hsv[1] * 220.0f);
+
+ // Create background color of clean color
+ Color bgColor = new Color(Color.HSVtoRGB(hsv[0], 1f, 1f));
+ hsvGradient.setBackgroundColor(bgColor);
+
+ return new int[] { x, y };
+ }
+
+ @Override
+ public Color calculate(int x, int y) {
+ float saturation = 1f - (y / 220.0f);
+ float value = (x / 220.0f);
+ float hue = Float.parseFloat(hueSlider.getValue().toString())
+ / 360f;
+
+ Color color = new Color(Color.HSVtoRGB(hue, saturation, value));
+ return color;
+ }
+ };
+
+ private static Logger getLogger() {
+ return Logger.getLogger(ColorPickerPopup.class.getName());
+ }
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.components.colorpicker;
+
+import java.lang.reflect.Method;
+
+import com.vaadin.data.HasValue.ValueChange;
+import com.vaadin.shared.Registration;
+import com.vaadin.shared.ui.colorpicker.Color;
+import com.vaadin.ui.Component;
+import com.vaadin.ui.CssLayout;
+import com.vaadin.ui.TextField;
+
+/**
+ * A component that represents color selection preview within a color picker.
+ *
+ * @since 7.0.0
+ */
+public class ColorPickerPreview extends CssLayout implements ColorSelector {
+
+ private static final String STYLE_DARK_COLOR = "v-textfield-dark";
+ private static final String STYLE_LIGHT_COLOR = "v-textfield-light";
+
+ private static final Method COLOR_CHANGE_METHOD;
+ static {
+ try {
+ COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
+ "colorChanged", new Class[] { ColorChangeEvent.class });
+ } catch (final java.lang.NoSuchMethodException e) {
+ // This should never happen
+ throw new java.lang.RuntimeException(
+ "Internal error finding methods in ColorPicker");
+ }
+ }
+
+ /** The color. */
+ private Color color;
+
+ /** The field. */
+ private final TextField field;
+
+ /** The old value. */
+ private String oldValue;
+ private Registration valueChangeListenerRegistration = null;
+
+ private ColorPickerPreview() {
+ setStyleName("v-colorpicker-preview");
+ setImmediate(true);
+ field = new TextField();
+ field.setImmediate(true);
+ field.setSizeFull();
+ field.setStyleName("v-colorpicker-preview-textfield");
+ field.setData(this);
+ valueChangeListenerRegistration = field
+ .addValueChangeListener(this::valueChange);
+ addComponent(field);
+ }
+
+ /**
+ * Instantiates a new color picker preview.
+ */
+ public ColorPickerPreview(Color color) {
+ this();
+ setColor(color);
+ }
+
+ @Override
+ public void setColor(Color color) {
+ this.color = color;
+
+ // Unregister listener
+ valueChangeListenerRegistration.remove();
+
+ String colorCSS = color.getCSS();
+ field.setValue(colorCSS);
+
+ oldValue = colorCSS;
+
+ // Re-register listener
+ valueChangeListenerRegistration = field
+ .addValueChangeListener(this::valueChange);
+
+ // Set the text color
+ field.removeStyleName(STYLE_DARK_COLOR);
+ field.removeStyleName(STYLE_LIGHT_COLOR);
+ if (this.color.getRed() + this.color.getGreen()
+ + this.color.getBlue() < 3 * 128) {
+ field.addStyleName(STYLE_DARK_COLOR);
+ } else {
+ field.addStyleName(STYLE_LIGHT_COLOR);
+ }
+
+ markAsDirty();
+ }
+
+ @Override
+ public Color getColor() {
+ return color;
+ }
+
+ @Override
+ public void addColorChangeListener(ColorChangeListener listener) {
+ addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
+ }
+
+ @Override
+ public void removeColorChangeListener(ColorChangeListener listener) {
+ removeListener(ColorChangeEvent.class, listener);
+ }
+
+ public void valueChange(ValueChange<String> event) {
+ String value = event.getValue();
+ try {
+ if (value != null) {
+ /*
+ * Description of supported formats see
+ * http://www.w3schools.com/cssref/css_colors_legal.asp
+ */
+ if (value.length() == 7 && value.startsWith("#")) {
+ // CSS color format (e.g. #000000)
+ int red = Integer.parseInt(value.substring(1, 3), 16);
+ int green = Integer.parseInt(value.substring(3, 5), 16);
+ int blue = Integer.parseInt(value.substring(5, 7), 16);
+ color = new Color(red, green, blue);
+
+ } else if (value.startsWith("rgb")) {
+ // RGB color format rgb/rgba(255,255,255,0.1)
+ String[] colors = value.substring(value.indexOf("(") + 1,
+ value.length() - 1).split(",");
+
+ int red = Integer.parseInt(colors[0]);
+ int green = Integer.parseInt(colors[1]);
+ int blue = Integer.parseInt(colors[2]);
+ if (colors.length > 3) {
+ int alpha = (int) (Double.parseDouble(colors[3])
+ * 255d);
+ color = new Color(red, green, blue, alpha);
+ } else {
+ color = new Color(red, green, blue);
+ }
+
+ } else if (value.startsWith("hsl")) {
+ // HSL color format hsl/hsla(100,50%,50%,1.0)
+ String[] colors = value.substring(value.indexOf("(") + 1,
+ value.length() - 1).split(",");
+
+ int hue = Integer.parseInt(colors[0]);
+ int saturation = Integer
+ .parseInt(colors[1].replace("%", ""));
+ int lightness = Integer
+ .parseInt(colors[2].replace("%", ""));
+ int rgb = Color.HSLtoRGB(hue, saturation, lightness);
+
+ if (colors.length > 3) {
+ int alpha = (int) (Double.parseDouble(colors[3])
+ * 255d);
+ color = new Color(rgb);
+ color.setAlpha(alpha);
+ } else {
+ color = new Color(rgb);
+ }
+ }
+
+ oldValue = value;
+ fireEvent(new ColorChangeEvent((Component) field.getData(),
+ color));
+ }
+
+ } catch (NumberFormatException nfe) {
+ // Revert value
+ field.setValue(oldValue);
+ }
+ }
+
+ /**
+ * Called when the component is refreshing
+ */
+ @Override
+ protected String getCss(Component c) {
+ return "background: " + color.getCSS();
+ }
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.components.colorpicker;
+
+import java.util.EnumSet;
+
+import com.vaadin.data.HasValue.ValueChange;
+import com.vaadin.shared.ui.colorpicker.Color;
+import com.vaadin.ui.ComboBox;
+import com.vaadin.ui.CustomComponent;
+import com.vaadin.ui.VerticalLayout;
+
+/**
+ * A component that represents color selection swatches within a color picker.
+ *
+ * @since 7.0.0
+ */
+public class ColorPickerSelect extends CustomComponent
+ implements ColorSelector {
+
+ /** The range. */
+ private final ComboBox<ColorRangePropertyId> range;
+
+ /** The grid. */
+ private final ColorPickerGrid grid;
+
+ /**
+ * The Enum ColorRangePropertyId.
+ */
+ private enum ColorRangePropertyId {
+ ALL("All colors"), RED("Red colors"), GREEN("Green colors"), BLUE(
+ "Blue colors");
+
+ /** The caption. */
+ private String caption;
+
+ /**
+ * Instantiates a new color range property id.
+ *
+ * @param caption
+ * the caption
+ */
+ ColorRangePropertyId(String caption) {
+ this.caption = caption;
+ }
+
+ @Override
+ public String toString() {
+ return caption;
+ }
+ }
+
+ /**
+ * Instantiates a new color picker select.
+ *
+ * @param rows
+ * the rows
+ * @param columns
+ * the columns
+ */
+ public ColorPickerSelect() {
+
+ VerticalLayout layout = new VerticalLayout();
+ setCompositionRoot(layout);
+
+ setStyleName("colorselect");
+ setWidth("100%");
+
+ range = new ComboBox<>(null, EnumSet.allOf(ColorRangePropertyId.class));
+ range.setEmptySelectionAllowed(false);
+ range.setWidth("100%");
+ range.addValueChangeListener(this::valueChange);
+
+ range.select(ColorRangePropertyId.ALL);
+
+ layout.addComponent(range);
+
+ grid = new ColorPickerGrid(createAllColors(14, 10));
+ grid.setWidth("100%");
+
+ layout.addComponent(grid);
+ }
+
+ /**
+ * Creates the all colors.
+ *
+ * @param rows
+ * the rows
+ * @param columns
+ * the columns
+ *
+ * @return the color[][]
+ */
+ private Color[][] createAllColors(int rows, int columns) {
+ Color[][] colors = new Color[rows][columns];
+
+ for (int row = 0; row < rows; row++) {
+ for (int col = 0; col < columns; col++) {
+
+ // Create the color grid by varying the saturation and value
+ if (row < (rows - 1)) {
+ // Calculate new hue value
+ float hue = ((float) col / (float) columns);
+ float saturation = 1f;
+ float value = 1f;
+
+ // For the upper half use value=1 and variable
+ // saturation
+ if (row < (rows / 2)) {
+ saturation = ((row + 1f) / (rows / 2f));
+ } else {
+ value = 1f - ((row - (rows / 2f)) / (rows / 2f));
+ }
+
+ colors[row][col] = new Color(
+ Color.HSVtoRGB(hue, saturation, value));
+ }
+
+ // The last row should have the black&white gradient
+ else {
+ float hue = 0f;
+ float saturation = 0f;
+ float value = 1f - ((float) col / (float) columns);
+
+ colors[row][col] = new Color(
+ Color.HSVtoRGB(hue, saturation, value));
+ }
+ }
+ }
+
+ return colors;
+ }
+
+ /**
+ * Creates the color.
+ *
+ * @param color
+ * the color
+ * @param rows
+ * the rows
+ * @param columns
+ * the columns
+ *
+ * @return the color[][]
+ */
+ private Color[][] createColors(Color color, int rows, int columns) {
+ Color[][] colors = new Color[rows][columns];
+
+ float[] hsv = color.getHSV();
+
+ float hue = hsv[0];
+ float saturation = 1f;
+ float value = 1f;
+
+ for (int row = 0; row < rows; row++) {
+ for (int col = 0; col < columns; col++) {
+
+ int index = row * columns + col;
+ saturation = 1f;
+ value = 1f;
+
+ if (index <= ((rows * columns) / 2)) {
+ saturation = index
+ / (((float) rows * (float) columns) / 2f);
+ } else {
+ index -= ((rows * columns) / 2);
+ value = 1f
+ - index / (((float) rows * (float) columns) / 2f);
+ }
+
+ colors[row][col] = new Color(
+ Color.HSVtoRGB(hue, saturation, value));
+ }
+ }
+
+ return colors;
+ }
+
+ @Override
+ public Color getColor() {
+ return grid.getColor();
+ }
+
+ @Override
+ public void setColor(Color color) {
+ grid.setColor(color);
+ }
+
+ @Override
+ public void addColorChangeListener(ColorChangeListener listener) {
+ grid.addColorChangeListener(listener);
+ }
+
+ @Override
+ public void removeColorChangeListener(ColorChangeListener listener) {
+ grid.removeColorChangeListener(listener);
+ }
+
+ public void valueChange(ValueChange<ColorRangePropertyId> event) {
+ if (grid == null) {
+ return;
+ }
+
+ if (event.getValue() == ColorRangePropertyId.ALL) {
+ grid.setColorGrid(createAllColors(14, 10));
+ } else if (event.getValue() == ColorRangePropertyId.RED) {
+ grid.setColorGrid(createColors(new Color(0xFF, 0, 0), 14, 10));
+ } else if (event.getValue() == ColorRangePropertyId.GREEN) {
+ grid.setColorGrid(createColors(new Color(0, 0xFF, 0), 14, 10));
+ } else if (event.getValue() == ColorRangePropertyId.BLUE) {
+ grid.setColorGrid(createColors(new Color(0, 0, 0xFF), 14, 10));
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.components.colorpicker;
+
+import java.io.Serializable;
+
+import com.vaadin.shared.ui.colorpicker.Color;
+
+/**
+ * An interface for a color selector.
+ *
+ * @since 7.0.0
+ */
+public interface ColorSelector extends Serializable, HasColorChangeListener {
+
+ /**
+ * Sets the color.
+ *
+ * @param color
+ * the new color
+ */
+ public void setColor(Color color);
+
+ /**
+ * Gets the color.
+ *
+ * @return the color
+ */
+ public Color getColor();
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.components.colorpicker;
+
+import java.io.Serializable;
+
+public interface HasColorChangeListener extends Serializable {
+
+ /**
+ * Adds a {@link ColorChangeListener} to the component.
+ *
+ * @param listener
+ */
+ void addColorChangeListener(ColorChangeListener listener);
+
+ /**
+ * Removes a {@link ColorChangeListener} from the component.
+ *
+ * @param listener
+ */
+ void removeColorChangeListener(ColorChangeListener listener);
+
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.tests.server.component.colorpicker;
+
+import org.junit.Test;
+
+import com.vaadin.shared.ui.colorpicker.Color;
+import com.vaadin.tests.design.DeclarativeTestBase;
+import com.vaadin.ui.AbstractColorPicker;
+import com.vaadin.ui.AbstractColorPicker.PopupStyle;
+import com.vaadin.ui.ColorPicker;
+import com.vaadin.ui.ColorPickerArea;
+
+public class AbstractColorPickerDeclarativeTest
+ extends DeclarativeTestBase<AbstractColorPicker> {
+
+ @Test
+ public void testAllAbstractColorPickerFeatures() {
+ String design = "<vaadin-color-picker color='#fafafa' default-caption-enabled position='100,100'"
+ + " popup-style='simple' rgb-visibility='false' hsv-visibility='false'"
+ + " history-visibility=false textfield-visibility=false />";
+ ColorPicker colorPicker = new ColorPicker();
+ int colorInt = Integer.parseInt("fafafa", 16);
+ colorPicker.setColor(new Color(colorInt));
+ colorPicker.setDefaultCaptionEnabled(true);
+ colorPicker.setPosition(100, 100);
+ colorPicker.setPopupStyle(PopupStyle.POPUP_SIMPLE);
+ colorPicker.setRGBVisibility(false);
+ colorPicker.setHSVVisibility(false);
+ colorPicker.setSwatchesVisibility(true);
+ colorPicker.setHistoryVisibility(false);
+ colorPicker.setTextfieldVisibility(false);
+
+ testWrite(design, colorPicker);
+ testRead(design, colorPicker);
+ }
+
+ @Test
+ public void testEmptyColorPicker() {
+ String design = "<vaadin-color-picker />";
+ ColorPicker colorPicker = new ColorPicker();
+ testRead(design, colorPicker);
+ testWrite(design, colorPicker);
+ }
+
+ @Test
+ public void testAllAbstractColorPickerAreaFeatures() {
+ String design = "<vaadin-color-picker-area color='#fafafa' default-caption-enabled position='100,100'"
+ + " popup-style='simple' rgb-visibility='false' hsv-visibility='false'"
+ + " history-visibility=false textfield-visibility=false />";
+ AbstractColorPicker colorPicker = new ColorPickerArea();
+ int colorInt = Integer.parseInt("fafafa", 16);
+ colorPicker.setColor(new Color(colorInt));
+ colorPicker.setDefaultCaptionEnabled(true);
+ colorPicker.setPosition(100, 100);
+ colorPicker.setPopupStyle(PopupStyle.POPUP_SIMPLE);
+ colorPicker.setRGBVisibility(false);
+ colorPicker.setHSVVisibility(false);
+ colorPicker.setSwatchesVisibility(true);
+ colorPicker.setHistoryVisibility(false);
+ colorPicker.setTextfieldVisibility(false);
+
+ testWrite(design, colorPicker);
+ testRead(design, colorPicker);
+ }
+
+ @Test
+ public void testEmptyColorPickerArea() {
+ String design = "<vaadin-color-picker-area />";
+ AbstractColorPicker colorPicker = new ColorPickerArea();
+ testRead(design, colorPicker);
+ testWrite(design, colorPicker);
+ }
+}
--- /dev/null
+/*
+ * Copyright 2000-2016 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.tests.server.component.colorpicker;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import com.vaadin.shared.ui.colorpicker.Color;
+
+public class ColorConversionsTest {
+
+ @Test
+ public void convertHSL2RGB() {
+
+ int rgb = Color.HSLtoRGB(100, 50, 50);
+ Color c = new Color(rgb);
+ assertEquals(106, c.getRed());
+ assertEquals(191, c.getGreen());
+ assertEquals(64, c.getBlue());
+ assertEquals("#6abf40", c.getCSS());
+
+ rgb = Color.HSLtoRGB(0, 50, 50);
+ c = new Color(rgb);
+ assertEquals(191, c.getRed());
+ assertEquals(64, c.getGreen());
+ assertEquals(64, c.getBlue());
+ assertEquals("#bf4040", c.getCSS());
+
+ rgb = Color.HSLtoRGB(50, 0, 50);
+ c = new Color(rgb);
+ assertEquals(128, c.getRed());
+ assertEquals(128, c.getGreen());
+ assertEquals(128, c.getBlue());
+ assertEquals("#808080", c.getCSS());
+
+ rgb = Color.HSLtoRGB(50, 100, 0);
+ c = new Color(rgb);
+ assertEquals(0, c.getRed(), 0);
+ assertEquals(0, c.getGreen(), 0);
+ assertEquals(0, c.getBlue(), 0);
+ assertEquals("#000000", c.getCSS());
+ }
+}
import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.ui.colorpicker.Color;
import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.ColorPickerArea;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
-import com.vaadin.v7.ui.ColorPickerArea;
/**
* Tests the HSV tab slider values when initially opening the tab.
import com.vaadin.shared.ui.colorpicker.Color;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.AbstractColorPicker;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.CheckBox;
+import com.vaadin.ui.ColorPicker;
+import com.vaadin.ui.ColorPickerArea;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
-import com.vaadin.v7.ui.AbstractColorPicker;
-import com.vaadin.v7.ui.ColorPicker;
-import com.vaadin.v7.ui.ColorPickerArea;
-import com.vaadin.v7.ui.components.colorpicker.ColorChangeEvent;
-import com.vaadin.v7.ui.components.colorpicker.ColorChangeListener;
+import com.vaadin.ui.components.colorpicker.ColorChangeEvent;
+import com.vaadin.ui.components.colorpicker.ColorChangeListener;
public class ColorPickerTestUI extends AbstractTestUI
implements ColorChangeListener {
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Button;
+import com.vaadin.ui.ColorPicker;
import com.vaadin.ui.Button.ClickEvent;
-import com.vaadin.v7.ui.ColorPicker;
/**
* Test for color picker with default caption.
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
+import com.vaadin.ui.ColorPicker;
+import com.vaadin.ui.ColorPickerArea;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
-import com.vaadin.v7.ui.ColorPicker;
-import com.vaadin.v7.ui.ColorPickerArea;
import com.vaadin.v7.ui.TextField;
/**
import com.vaadin.shared.ui.colorpicker.Color;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
+import com.vaadin.ui.ColorPicker;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
+import com.vaadin.ui.components.colorpicker.ColorChangeEvent;
+import com.vaadin.ui.components.colorpicker.ColorChangeListener;
import com.vaadin.v7.data.Property.ValueChangeEvent;
import com.vaadin.v7.data.Property.ValueChangeListener;
-import com.vaadin.v7.ui.ColorPicker;
import com.vaadin.v7.ui.ComboBox;
import com.vaadin.v7.ui.TextArea;
-import com.vaadin.v7.ui.components.colorpicker.ColorChangeEvent;
-import com.vaadin.v7.ui.components.colorpicker.ColorChangeListener;
public class CSSInjectWithColorpicker extends UI {
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.shared.ui.colorpicker.Color;
+import com.vaadin.ui.ColorPicker;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
+import com.vaadin.ui.AbstractColorPicker.PopupStyle;
import com.vaadin.ui.themes.ValoTheme;
-import com.vaadin.v7.ui.AbstractColorPicker.PopupStyle;
-import com.vaadin.v7.ui.ColorPicker;
public class ColorPickers extends VerticalLayout implements View {
public ColorPickers() {