選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

GwtColorPicker.java 3.2KB

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