You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GwtColorPicker.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.demo.colorpicker.gwt.client.ui;
  5. import com.google.gwt.user.client.DOM;
  6. import com.google.gwt.user.client.Element;
  7. import com.google.gwt.user.client.ui.Button;
  8. import com.google.gwt.user.client.ui.ClickListener;
  9. import com.google.gwt.user.client.ui.Composite;
  10. import com.google.gwt.user.client.ui.Grid;
  11. import com.google.gwt.user.client.ui.HorizontalPanel;
  12. import com.google.gwt.user.client.ui.Label;
  13. import com.google.gwt.user.client.ui.Widget;
  14. /**
  15. * A regular GWT component without integration with IT Mill Toolkit.
  16. */
  17. public class GwtColorPicker extends Composite implements ClickListener {
  18. /** Currently selected color name to give client-side feedback to the user. */
  19. protected Label currentcolor = new Label();
  20. public GwtColorPicker() {
  21. // Create a 4x4 grid of buttons with names for 16 colors
  22. final Grid grid = new Grid(4, 4);
  23. final String[] colors = new String[] { "aqua", "black", "blue",
  24. "fuchsia", "gray", "green", "lime", "maroon", "navy", "olive",
  25. "purple", "red", "silver", "teal", "white", "yellow" };
  26. int colornum = 0;
  27. for (int i = 0; i < 4; i++) {
  28. for (int j = 0; j < 4; j++, colornum++) {
  29. // Create a button for each color
  30. final Button button = new Button(colors[colornum]);
  31. button.addClickListener(this);
  32. // Put the button in the Grid layout
  33. grid.setWidget(i, j, button);
  34. // Set the button background colors.
  35. DOM.setStyleAttribute(button.getElement(), "background",
  36. colors[colornum]);
  37. // For dark colors, the button label must be in white.
  38. if ("black navy maroon blue purple".indexOf(colors[colornum]) != -1) {
  39. DOM
  40. .setStyleAttribute(button.getElement(), "color",
  41. "white");
  42. }
  43. }
  44. }
  45. // Create a panel with the color grid and currently selected color
  46. // indicator
  47. final HorizontalPanel panel = new HorizontalPanel();
  48. panel.add(grid);
  49. panel.add(currentcolor);
  50. // Set the class of the color selection feedback box to allow CSS
  51. // styling.
  52. // We need to obtain the DOM element for the current color label.
  53. // This assumes that the <td> element of the HorizontalPanel is
  54. // the parent of the label element. Notice that the element has no
  55. // parent
  56. // before the widget has been added to the horizontal panel.
  57. final Element panelcell = DOM.getParent(currentcolor.getElement());
  58. DOM.setElementProperty(panelcell, "className",
  59. "colorpicker-currentcolorbox");
  60. // Set initial color. This will be overridden with the value read from
  61. // server.
  62. setColor("white");
  63. // Composite GWT widgets must call initWidget().
  64. initWidget(panel);
  65. }
  66. /** Handles click on a color button. */
  67. public void onClick(Widget sender) {
  68. // Use the button label as the color name to set
  69. setColor(((Button) sender).getText());
  70. }
  71. /** Sets the currently selected color. */
  72. public void setColor(String newcolor) {
  73. // Give client-side feedback by changing the color name in the label
  74. currentcolor.setText(newcolor);
  75. // Obtain the DOM elements. This assumes that the <td> element
  76. // of the HorizontalPanel is the parent of the label element.
  77. final Element nameelement = currentcolor.getElement();
  78. final Element cell = DOM.getParent(nameelement);
  79. // Give feedback by changing the background color
  80. DOM.setStyleAttribute(cell, "background", newcolor);
  81. DOM.setStyleAttribute(nameelement, "background", newcolor);
  82. if ("black navy maroon blue purple".indexOf(newcolor) != -1) {
  83. DOM.setStyleAttribute(nameelement, "color", "white");
  84. } else {
  85. DOM.setStyleAttribute(nameelement, "color", "black");
  86. }
  87. }
  88. }