Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AbstractColorPicker.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.ui;
  17. import java.io.Serializable;
  18. import java.lang.reflect.Method;
  19. import java.util.Collection;
  20. import org.jsoup.nodes.Attributes;
  21. import org.jsoup.nodes.Element;
  22. import com.vaadin.ui.UI;
  23. import com.vaadin.ui.Window.CloseEvent;
  24. import com.vaadin.ui.Window.CloseListener;
  25. import com.vaadin.ui.declarative.DesignAttributeHandler;
  26. import com.vaadin.ui.declarative.DesignContext;
  27. import com.vaadin.v7.shared.ui.colorpicker.Color;
  28. import com.vaadin.v7.shared.ui.colorpicker.ColorPickerServerRpc;
  29. import com.vaadin.v7.shared.ui.colorpicker.ColorPickerState;
  30. import com.vaadin.v7.ui.components.colorpicker.ColorChangeEvent;
  31. import com.vaadin.v7.ui.components.colorpicker.ColorChangeListener;
  32. import com.vaadin.v7.ui.components.colorpicker.ColorPickerPopup;
  33. import com.vaadin.v7.ui.components.colorpicker.ColorSelector;
  34. /**
  35. * An abstract class that defines default implementation for a color picker
  36. * component.
  37. *
  38. * @since 7.0.0
  39. */
  40. @Deprecated
  41. public abstract class AbstractColorPicker extends AbstractLegacyComponent
  42. implements CloseListener, ColorSelector {
  43. private static final Method COLOR_CHANGE_METHOD;
  44. static {
  45. try {
  46. COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
  47. "colorChanged", new Class[] { ColorChangeEvent.class });
  48. } catch (final NoSuchMethodException e) {
  49. // This should never happen
  50. throw new RuntimeException(
  51. "Internal error finding methods in ColorPicker");
  52. }
  53. }
  54. /**
  55. * Interface for converting 2d-coordinates to a Color
  56. */
  57. @Deprecated
  58. public interface Coordinates2Color extends Serializable {
  59. /**
  60. * Calculate color from coordinates
  61. *
  62. * @param x
  63. * the x-coordinate
  64. * @param y
  65. * the y-coordinate
  66. *
  67. * @return the color
  68. */
  69. public Color calculate(int x, int y);
  70. /**
  71. * Calculate coordinates from color
  72. *
  73. * @param c
  74. * the c
  75. *
  76. * @return the integer array with the coordinates
  77. */
  78. public int[] calculate(Color c);
  79. }
  80. @Deprecated
  81. public enum PopupStyle {
  82. POPUP_NORMAL("normal"), POPUP_SIMPLE("simple");
  83. private String style;
  84. PopupStyle(String styleName) {
  85. style = styleName;
  86. }
  87. @Override
  88. public String toString() {
  89. return style;
  90. }
  91. }
  92. private ColorPickerServerRpc rpc = new ColorPickerServerRpc() {
  93. @Override
  94. public void openPopup(boolean open) {
  95. showPopup(open);
  96. }
  97. };
  98. protected static final String STYLENAME_DEFAULT = "v-colorpicker";
  99. protected static final String STYLENAME_BUTTON = "v-button";
  100. protected static final String STYLENAME_AREA = "v-colorpicker-area";
  101. protected PopupStyle popupStyle = PopupStyle.POPUP_NORMAL;
  102. /** The popup window. */
  103. private ColorPickerPopup window;
  104. /** The color. */
  105. protected Color color;
  106. /** The UI. */
  107. private UI parent;
  108. protected String popupCaption = null;
  109. private int positionX = 0;
  110. private int positionY = 0;
  111. protected boolean rgbVisible = true;
  112. protected boolean hsvVisible = true;
  113. protected boolean swatchesVisible = true;
  114. protected boolean historyVisible = true;
  115. protected boolean textfieldVisible = true;
  116. /**
  117. * Instantiates a new color picker.
  118. */
  119. public AbstractColorPicker() {
  120. this("Colors", Color.WHITE);
  121. }
  122. /**
  123. * Instantiates a new color picker.
  124. *
  125. * @param popupCaption
  126. * the caption of the popup window
  127. */
  128. public AbstractColorPicker(String popupCaption) {
  129. this(popupCaption, Color.WHITE);
  130. }
  131. /**
  132. * Instantiates a new color picker.
  133. *
  134. * @param popupCaption
  135. * the caption of the popup window
  136. * @param initialColor
  137. * the initial color
  138. */
  139. public AbstractColorPicker(String popupCaption, Color initialColor) {
  140. super();
  141. registerRpc(rpc);
  142. setColor(initialColor);
  143. this.popupCaption = popupCaption;
  144. setDefaultStyles();
  145. setCaption("");
  146. }
  147. @Override
  148. public void setColor(Color color) {
  149. this.color = color;
  150. if (window != null) {
  151. window.setColor(color);
  152. }
  153. getState().color = color.getCSS();
  154. }
  155. @Override
  156. public Color getColor() {
  157. return color;
  158. }
  159. /**
  160. * Set true if the component should show a default caption (css-code for the
  161. * currently selected color, e.g. #ffffff) when no other caption is
  162. * available.
  163. *
  164. * @param enabled
  165. */
  166. public void setDefaultCaptionEnabled(boolean enabled) {
  167. getState().showDefaultCaption = enabled;
  168. }
  169. /**
  170. * Returns true if the component shows the default caption (css-code for the
  171. * currently selected color, e.g. #ffffff) if no other caption is available.
  172. */
  173. public boolean isDefaultCaptionEnabled() {
  174. return getState(false).showDefaultCaption;
  175. }
  176. /**
  177. * Sets the position of the popup window
  178. *
  179. * @param x
  180. * the x-coordinate
  181. * @param y
  182. * the y-coordinate
  183. */
  184. public void setPosition(int x, int y) {
  185. positionX = x;
  186. positionY = y;
  187. if (window != null) {
  188. window.setPositionX(x);
  189. window.setPositionY(y);
  190. }
  191. }
  192. @Override
  193. public void addColorChangeListener(ColorChangeListener listener) {
  194. addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
  195. }
  196. @Override
  197. public void removeColorChangeListener(ColorChangeListener listener) {
  198. removeListener(ColorChangeEvent.class, listener);
  199. }
  200. @Override
  201. public void windowClose(CloseEvent e) {
  202. if (e.getWindow() == window) {
  203. getState().popupVisible = false;
  204. }
  205. }
  206. /**
  207. * Fired when a color change event occurs
  208. *
  209. * @param event
  210. * The color change event
  211. */
  212. protected void colorChanged(ColorChangeEvent event) {
  213. setColor(event.getColor());
  214. fireColorChanged();
  215. }
  216. /**
  217. * Notifies the listeners that the selected color has changed
  218. */
  219. public void fireColorChanged() {
  220. fireEvent(new ColorChangeEvent(this, color));
  221. }
  222. /**
  223. * The style for the popup window
  224. *
  225. * @param style
  226. * The style
  227. */
  228. public void setPopupStyle(PopupStyle style) {
  229. popupStyle = style;
  230. switch (style) {
  231. case POPUP_NORMAL: {
  232. setRGBVisibility(true);
  233. setHSVVisibility(true);
  234. setSwatchesVisibility(true);
  235. setHistoryVisibility(true);
  236. setTextfieldVisibility(true);
  237. break;
  238. }
  239. case POPUP_SIMPLE: {
  240. setRGBVisibility(false);
  241. setHSVVisibility(false);
  242. setSwatchesVisibility(true);
  243. setHistoryVisibility(false);
  244. setTextfieldVisibility(false);
  245. break;
  246. }
  247. }
  248. }
  249. /**
  250. * Gets the style for the popup window
  251. *
  252. * @since 7.5.0
  253. * @return popup window style
  254. */
  255. public PopupStyle getPopupStyle() {
  256. return popupStyle;
  257. }
  258. /**
  259. * Set the visibility of the RGB Tab
  260. *
  261. * @param visible
  262. * The visibility
  263. */
  264. public void setRGBVisibility(boolean visible) {
  265. if (!visible && !hsvVisible && !swatchesVisible) {
  266. throw new IllegalArgumentException("Cannot hide all tabs.");
  267. }
  268. rgbVisible = visible;
  269. if (window != null) {
  270. window.setRGBTabVisible(visible);
  271. }
  272. }
  273. /**
  274. * Gets the visibility of the RGB Tab
  275. *
  276. * @since 7.5.0
  277. * @return visibility of the RGB tab
  278. */
  279. public boolean getRGBVisibility() {
  280. return rgbVisible;
  281. }
  282. /**
  283. * Set the visibility of the HSV Tab
  284. *
  285. * @param visible
  286. * The visibility
  287. */
  288. public void setHSVVisibility(boolean visible) {
  289. if (!visible && !rgbVisible && !swatchesVisible) {
  290. throw new IllegalArgumentException("Cannot hide all tabs.");
  291. }
  292. hsvVisible = visible;
  293. if (window != null) {
  294. window.setHSVTabVisible(visible);
  295. }
  296. }
  297. /**
  298. * Gets the visibility of the HSV Tab
  299. *
  300. * @since 7.5.0
  301. * @return visibility of the HSV tab
  302. */
  303. public boolean getHSVVisibility() {
  304. return hsvVisible;
  305. }
  306. /**
  307. * Set the visibility of the Swatches Tab
  308. *
  309. * @param visible
  310. * The visibility
  311. */
  312. public void setSwatchesVisibility(boolean visible) {
  313. if (!visible && !hsvVisible && !rgbVisible) {
  314. throw new IllegalArgumentException("Cannot hide all tabs.");
  315. }
  316. swatchesVisible = visible;
  317. if (window != null) {
  318. window.setSwatchesTabVisible(visible);
  319. }
  320. }
  321. /**
  322. * Gets the visibility of the Swatches Tab
  323. *
  324. * @since 7.5.0
  325. * @return visibility of the swatches tab
  326. */
  327. public boolean getSwatchesVisibility() {
  328. return swatchesVisible;
  329. }
  330. /**
  331. * Sets the visibility of the Color History
  332. *
  333. * @param visible
  334. * The visibility
  335. */
  336. public void setHistoryVisibility(boolean visible) {
  337. historyVisible = visible;
  338. if (window != null) {
  339. window.setHistoryVisible(visible);
  340. }
  341. }
  342. /**
  343. * Gets the visibility of the Color History
  344. *
  345. * @since 7.5.0
  346. * @return visibility of color history
  347. */
  348. public boolean getHistoryVisibility() {
  349. return historyVisible;
  350. }
  351. /**
  352. * Sets the visibility of the CSS color code text field
  353. *
  354. * @param visible
  355. * The visibility
  356. */
  357. public void setTextfieldVisibility(boolean visible) {
  358. textfieldVisible = visible;
  359. if (window != null) {
  360. window.setPreviewVisible(visible);
  361. }
  362. }
  363. /**
  364. * Gets the visibility of CSS color code text field
  365. *
  366. * @since 7.5.0
  367. * @return visibility of css color code text field
  368. */
  369. public boolean getTextfieldVisibility() {
  370. return textfieldVisible;
  371. }
  372. @Override
  373. protected ColorPickerState getState() {
  374. return (ColorPickerState) super.getState();
  375. }
  376. @Override
  377. protected ColorPickerState getState(boolean markAsDirty) {
  378. return (ColorPickerState) super.getState(markAsDirty);
  379. }
  380. /**
  381. * Sets the default styles of the component
  382. *
  383. */
  384. protected abstract void setDefaultStyles();
  385. /**
  386. * Shows a popup-window for color selection.
  387. */
  388. public void showPopup() {
  389. showPopup(true);
  390. }
  391. /**
  392. * Hides a popup-window for color selection.
  393. */
  394. public void hidePopup() {
  395. showPopup(false);
  396. }
  397. /**
  398. * Shows or hides popup-window depending on the given parameter. If there is
  399. * no such window yet, one is created.
  400. *
  401. * @param open
  402. */
  403. protected void showPopup(boolean open) {
  404. if (open && !isReadOnly()) {
  405. if (parent == null) {
  406. parent = getUI();
  407. }
  408. if (window == null) {
  409. // Create the popup
  410. window = new ColorPickerPopup(color);
  411. window.setCaption(popupCaption);
  412. window.setRGBTabVisible(rgbVisible);
  413. window.setHSVTabVisible(hsvVisible);
  414. window.setSwatchesTabVisible(swatchesVisible);
  415. window.setHistoryVisible(historyVisible);
  416. window.setPreviewVisible(textfieldVisible);
  417. window.addCloseListener(this);
  418. window.addColorChangeListener(new ColorChangeListener() {
  419. @Override
  420. public void colorChanged(ColorChangeEvent event) {
  421. AbstractColorPicker.this.colorChanged(event);
  422. }
  423. });
  424. window.getHistory().setColor(color);
  425. parent.addWindow(window);
  426. window.setVisible(true);
  427. window.setPositionX(positionX);
  428. window.setPositionY(positionY);
  429. } else if (!parent.equals(window.getParent())) {
  430. window.setRGBTabVisible(rgbVisible);
  431. window.setHSVTabVisible(hsvVisible);
  432. window.setSwatchesTabVisible(swatchesVisible);
  433. window.setHistoryVisible(historyVisible);
  434. window.setPreviewVisible(textfieldVisible);
  435. window.setColor(color);
  436. window.getHistory().setColor(color);
  437. window.setVisible(true);
  438. parent.addWindow(window);
  439. }
  440. } else if (window != null) {
  441. window.setVisible(false);
  442. parent.removeWindow(window);
  443. }
  444. getState().popupVisible = open;
  445. }
  446. /**
  447. * Set whether the caption text is rendered as HTML or not. You might need
  448. * to re-theme component to allow higher content than the original text
  449. * style.
  450. *
  451. * If set to true, the captions are passed to the browser as html and the
  452. * developer is responsible for ensuring no harmful html is used. If set to
  453. * false, the content is passed to the browser as plain text.
  454. *
  455. * @param htmlContentAllowed
  456. * <code>true</code> if caption is rendered as HTML,
  457. * <code>false</code> otherwise
  458. * @deprecated as of , use {@link #setCaptionAsHtml(boolean)} instead
  459. */
  460. @Deprecated
  461. public void setHtmlContentAllowed(boolean htmlContentAllowed) {
  462. setCaptionAsHtml(htmlContentAllowed);
  463. }
  464. /**
  465. * Return HTML rendering setting
  466. *
  467. * @return <code>true</code> if the caption text is to be rendered as HTML,
  468. * <code>false</code> otherwise
  469. * @deprecated as of , use {@link #isCaptionAsHtml()} instead
  470. */
  471. @Deprecated
  472. public boolean isHtmlContentAllowed() {
  473. return isCaptionAsHtml();
  474. }
  475. @Override
  476. public void readDesign(Element design, DesignContext designContext) {
  477. super.readDesign(design, designContext);
  478. Attributes attributes = design.attributes();
  479. if (design.hasAttr("color")) {
  480. // Ignore the # character
  481. String hexColor = DesignAttributeHandler
  482. .readAttribute("color", attributes, String.class)
  483. .substring(1);
  484. setColor(new Color(Integer.parseInt(hexColor, 16)));
  485. }
  486. if (design.hasAttr("popup-style")) {
  487. setPopupStyle(PopupStyle.valueOf(
  488. "POPUP_" + attributes.get("popup-style").toUpperCase()));
  489. }
  490. if (design.hasAttr("position")) {
  491. String[] position = attributes.get("position").split(",");
  492. setPosition(Integer.parseInt(position[0]),
  493. Integer.parseInt(position[1]));
  494. }
  495. }
  496. @Override
  497. public void writeDesign(Element design, DesignContext designContext) {
  498. super.writeDesign(design, designContext);
  499. Attributes attribute = design.attributes();
  500. DesignAttributeHandler.writeAttribute("color", attribute,
  501. color.getCSS(), Color.WHITE.getCSS(), String.class,
  502. designContext);
  503. DesignAttributeHandler.writeAttribute("popup-style", attribute,
  504. popupStyle == PopupStyle.POPUP_NORMAL ? "normal" : "simple",
  505. "normal", String.class, designContext);
  506. DesignAttributeHandler.writeAttribute("position", attribute,
  507. positionX + "," + positionY, "0,0", String.class,
  508. designContext);
  509. }
  510. @Override
  511. protected Collection<String> getCustomAttributes() {
  512. Collection<String> result = super.getCustomAttributes();
  513. result.add("color");
  514. result.add("position");
  515. result.add("popup-style");
  516. return result;
  517. }
  518. }