Skip to content

Commit

Permalink
Make ColorPicker subcomponents HasValues
Browse files Browse the repository at this point in the history
Change-Id: Ia0502ef515e2b44f105a3a4c6ae7b0b62b8ab6bb
  • Loading branch information
jdahlstrom authored and Vaadin Code Review committed Oct 5, 2016
1 parent 6b22997 commit 2f6334f
Show file tree
Hide file tree
Showing 16 changed files with 225 additions and 602 deletions.
Expand Up @@ -19,8 +19,8 @@
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.annotations.OnStateChange;
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;
Expand Down Expand Up @@ -63,23 +63,19 @@ 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);
}

@OnStateChange({ "cursorX", "cursorY" })
void updateCursor() {
getWidget().setCursor(getState().cursorX, getState().cursorY);
}

@OnStateChange("bgColor")
void updateBgColor() {
getWidget().setBGColor(getState().bgColor);
}
}
7 changes: 7 additions & 0 deletions server/src/main/java/com/vaadin/data/HasValue.java
Expand Up @@ -16,11 +16,13 @@
package com.vaadin.data;

import java.io.Serializable;
import java.lang.reflect.Method;

import com.vaadin.event.ConnectorEvent;
import com.vaadin.event.EventListener;
import com.vaadin.server.ClientConnector;
import com.vaadin.shared.Registration;
import com.vaadin.util.ReflectTools;

/**
* A generic interface for field components and other user interface objects
Expand Down Expand Up @@ -117,6 +119,11 @@ public boolean isUserOriginated() {
public interface ValueChangeListener<V> extends
EventListener<ValueChange<V>> {

@Deprecated
public static final Method VALUE_CHANGE_METHOD = ReflectTools
.findMethod(ValueChangeListener.class, "accept",
ValueChange.class);

/**
* Invoked when this listener receives a value change event from an
* event source to which it has been added.
Expand Down
12 changes: 5 additions & 7 deletions server/src/main/java/com/vaadin/ui/AbstractColorPicker.java
Expand Up @@ -101,13 +101,11 @@ public void openPopup(boolean open) {

protected PopupStyle popupStyle = PopupStyle.POPUP_NORMAL;

/** The popup window. */
private ColorPickerPopup window;

/** The currently selected color. */
protected Color color;

/** The UI. */
private UI parent;

private String popupCaption = null;
Expand Down Expand Up @@ -452,10 +450,10 @@ protected void showPopup(boolean open) {
window.setImmediate(true);
window.addCloseListener(
event -> getState().popupVisible = false);
window.addColorChangeListener(
event -> setValue(event.getColor()));
window.addValueChangeListener(
event -> setValue(event.getValue()));

window.getHistory().setColor(color);
window.getHistory().setValue(color);
window.setPositionX(positionX);
window.setPositionY(positionY);
window.setVisible(true);
Expand All @@ -471,8 +469,8 @@ protected void showPopup(boolean open) {
window.setHistoryVisible(historyVisible);
window.setPreviewVisible(textfieldVisible);

window.setColor(color);
window.getHistory().setColor(color);
window.setValue(color);
window.getHistory().setValue(color);
window.setVisible(true);

parent.addWindow(window);
Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -15,44 +15,24 @@
*/
package com.vaadin.ui.components.colorpicker;

import java.lang.reflect.Method;

import com.vaadin.shared.Registration;
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.AbstractColorPicker.Coordinates2Color;
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.AbstractField;

/**
* 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");
}
}
public class ColorPickerGradient extends AbstractField<Color> {

private ColorPickerGradientServerRpc rpc = new ColorPickerGradientServerRpc() {

@Override
public void select(int cursorX, int cursorY) {
x = cursorX;
y = cursorY;
color = converter.calculate(x, y);

fireColorChanged(color);
setValue(converter.calculate(cursorX, cursorY), true);
}
};

Expand All @@ -62,12 +42,6 @@ public void select(int cursorX, int cursorY) {
/** 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
Expand All @@ -91,28 +65,16 @@ public ColorPickerGradient(String id, Coordinates2Color 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 Registration addColorChangeListener(ColorChangeListener listener) {
addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
return () -> removeListener(ColorChangeEvent.class, listener);
public Color getValue() {
return color;
}

@Override
@Deprecated
public void removeColorChangeListener(ColorChangeListener listener) {
removeListener(ColorChangeEvent.class, listener);
protected void doSetValue(Color color) {
this.color = color;
int[] coords = converter.calculate(color);
getState().cursorX = coords[0];
getState().cursorY = coords[1];
}

/**
Expand All @@ -125,21 +87,6 @@ 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();
Expand Down

0 comments on commit 2f6334f

Please sign in to comment.