PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src-gwt/org/opencms/gwt/client/ui/input/CmsVfsSelection.java

http://github.com/alkacon/opencms-core
Java | 493 lines | 246 code | 93 blank | 154 comment | 37 complexity | 0ac1a411a9e5cc60528700752a92aa41 MD5 | raw file
Possible License(s): MIT, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. /*
  2. * This library is part of OpenCms -
  3. * the Open Source Content Management System
  4. *
  5. * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * For further information about Alkacon Software, please see the
  18. * company website: http://www.alkacon.com
  19. *
  20. * For further information about OpenCms, please see the
  21. * project website: http://www.opencms.org
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. package org.opencms.gwt.client.ui.input;
  28. import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants;
  29. import org.opencms.gwt.client.CmsCoreProvider;
  30. import org.opencms.gwt.client.ui.I_CmsAutoHider;
  31. import org.opencms.gwt.shared.CmsLinkBean;
  32. import org.opencms.util.CmsStringUtil;
  33. import com.google.gwt.event.dom.client.BlurEvent;
  34. import com.google.gwt.event.dom.client.BlurHandler;
  35. import com.google.gwt.event.dom.client.MouseUpEvent;
  36. import com.google.gwt.event.dom.client.MouseUpHandler;
  37. import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
  38. import com.google.gwt.event.logical.shared.ValueChangeHandler;
  39. import com.google.gwt.event.shared.HandlerRegistration;
  40. import com.google.gwt.user.client.Command;
  41. import com.google.gwt.user.client.DOM;
  42. import com.google.gwt.user.client.Event;
  43. import com.google.gwt.user.client.Event.NativePreviewEvent;
  44. import com.google.gwt.user.client.Event.NativePreviewHandler;
  45. import com.google.gwt.user.client.ui.Composite;
  46. import com.google.gwt.user.client.ui.FlowPanel;
  47. import com.google.gwt.user.client.ui.Panel;
  48. /**
  49. * Basic gallery widget for forms.<p>
  50. *
  51. * @since 8.0.0
  52. *
  53. */
  54. public class CmsVfsSelection extends Composite implements I_CmsFormWidget, HasValueChangeHandlers<String> {
  55. /**
  56. * Event preview handler.<p>
  57. *
  58. * To be used while popup open.<p>
  59. */
  60. protected class CloseEventPreviewHandler implements NativePreviewHandler {
  61. /**
  62. * @see com.google.gwt.user.client.Event.NativePreviewHandler#onPreviewNativeEvent(com.google.gwt.user.client.Event.NativePreviewEvent)
  63. */
  64. public void onPreviewNativeEvent(NativePreviewEvent event) {
  65. Event nativeEvent = Event.as(event.getNativeEvent());
  66. switch (DOM.eventGetType(nativeEvent)) {
  67. case Event.ONMOUSEMOVE:
  68. break;
  69. case Event.ONMOUSEUP:
  70. break;
  71. case Event.ONMOUSEDOWN:
  72. break;
  73. case Event.ONKEYUP:
  74. if (m_selectionInput.m_textbox.getValue().length() > 0) {
  75. close();
  76. } else {
  77. if (m_popup == null) {
  78. open();
  79. } else if (m_popup.isShowing()) {
  80. close();
  81. } else {
  82. open();
  83. }
  84. }
  85. break;
  86. case Event.ONMOUSEWHEEL:
  87. close();
  88. break;
  89. default:
  90. // do nothing
  91. }
  92. }
  93. }
  94. /** The download mode of this widget. */
  95. public static final String DOWNLOAD = "download";
  96. /** The download link mode of this widget. */
  97. public static final String DOWNLOAD_LINK = "download_link";
  98. /** The file link mode of this widget. */
  99. public static final String FILE_LINK = "file_link";
  100. /** The image link mode of this widget. */
  101. public static final String IMAGE_LINK = "image_link";
  102. /** The link mode of this widget. */
  103. public static final String LINK = "link";
  104. /** A counter used for giving text box widgets ids. */
  105. private static int idCounter;
  106. /** The old value. */
  107. protected String m_oldValue = "";
  108. /** The popup frame. */
  109. protected CmsFramePopup m_popup;
  110. /** The handler registration. */
  111. protected HandlerRegistration m_previewHandlerRegistration;
  112. /** The default rows set. */
  113. int m_defaultRows;
  114. /** The root panel containing the other components of this widget. */
  115. Panel m_panel = new FlowPanel();
  116. /** The container for the text area. */
  117. CmsSelectionInput m_selectionInput;
  118. /** The configuration string. */
  119. private String m_config;
  120. /** The error display for this widget. */
  121. private CmsErrorWidget m_error = new CmsErrorWidget();
  122. /** The field id. */
  123. private String m_id;
  124. /** The selection type. */
  125. private String m_type;
  126. /**
  127. * VsfSelection widget to open the gallery selection.<p>
  128. * @param iconImage the image of the icon shown in the
  129. * @param type the type of this widget
  130. * @param config the configuration for this widget
  131. */
  132. public CmsVfsSelection(String iconImage, String type, String config) {
  133. initWidget(m_panel);
  134. m_type = type;
  135. m_config = config;
  136. m_selectionInput = new CmsSelectionInput(iconImage);
  137. m_id = "CmsVfsSelection_" + (idCounter++);
  138. m_selectionInput.m_textbox.getElement().setId(m_id);
  139. m_panel.add(m_selectionInput);
  140. m_panel.add(m_error);
  141. m_selectionInput.m_textbox.addMouseUpHandler(new MouseUpHandler() {
  142. public void onMouseUp(MouseUpEvent event) {
  143. m_selectionInput.hideFader();
  144. setTitle("");
  145. if (m_popup == null) {
  146. open();
  147. } else if (m_popup.isShowing()) {
  148. close();
  149. } else {
  150. open();
  151. }
  152. }
  153. });
  154. m_selectionInput.m_textbox.addBlurHandler(new BlurHandler() {
  155. public void onBlur(BlurEvent event) {
  156. if ((m_selectionInput.m_textbox.getValue().length()
  157. * 6.88) > m_selectionInput.m_textbox.getOffsetWidth()) {
  158. setTitle(m_selectionInput.m_textbox.getValue());
  159. }
  160. m_selectionInput.showFader();
  161. }
  162. });
  163. m_selectionInput.setOpenCommand(new Command() {
  164. public void execute() {
  165. if (m_popup == null) {
  166. open();
  167. } else if (m_popup.isShowing()) {
  168. close();
  169. } else {
  170. open();
  171. }
  172. }
  173. });
  174. }
  175. /**
  176. * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
  177. */
  178. public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
  179. return m_selectionInput.m_textbox.addValueChangeHandler(handler);
  180. }
  181. /**
  182. * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getApparentValue()
  183. */
  184. public String getApparentValue() {
  185. return getFormValueAsString();
  186. }
  187. /**
  188. * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFieldType()
  189. */
  190. public FieldType getFieldType() {
  191. return I_CmsFormWidget.FieldType.STRING;
  192. }
  193. /**
  194. * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValue()
  195. */
  196. public Object getFormValue() {
  197. if (m_selectionInput.m_textbox.getText() == null) {
  198. return "";
  199. }
  200. return m_selectionInput.m_textbox.getValue();
  201. }
  202. /**
  203. * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValueAsString()
  204. */
  205. public String getFormValueAsString() {
  206. return (String)getFormValue();
  207. }
  208. /**
  209. * Returns the selected link as a bean.<p>
  210. *
  211. * @return the selected link as a bean
  212. */
  213. public CmsLinkBean getLinkBean() {
  214. String link = m_selectionInput.m_textbox.getValue();
  215. if (CmsStringUtil.isEmptyOrWhitespaceOnly(link)) {
  216. return null;
  217. }
  218. return new CmsLinkBean(m_selectionInput.m_textbox.getText(), true);
  219. }
  220. /**
  221. * Returns the text contained in the text area.<p>
  222. *
  223. * @return the text in the text area
  224. */
  225. public String getText() {
  226. return m_selectionInput.m_textbox.getValue();
  227. }
  228. /**
  229. * Returns the text box container of this widget.<p>
  230. *
  231. * @return the text box container
  232. */
  233. public CmsSelectionInput getTextAreaContainer() {
  234. return m_selectionInput;
  235. }
  236. /**
  237. * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#isEnabled()
  238. */
  239. public boolean isEnabled() {
  240. return m_selectionInput.m_textbox.isEnabled();
  241. }
  242. /**
  243. * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#reset()
  244. */
  245. public void reset() {
  246. m_selectionInput.m_textbox.setText("");
  247. }
  248. /**
  249. * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setAutoHideParent(org.opencms.gwt.client.ui.I_CmsAutoHider)
  250. */
  251. public void setAutoHideParent(I_CmsAutoHider autoHideParent) {
  252. // nothing to do
  253. }
  254. /**
  255. * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setEnabled(boolean)
  256. */
  257. public void setEnabled(boolean enabled) {
  258. m_selectionInput.m_textbox.setEnabled(enabled);
  259. }
  260. /**
  261. * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setErrorMessage(java.lang.String)
  262. */
  263. public void setErrorMessage(String errorMessage) {
  264. m_error.setText(errorMessage);
  265. }
  266. /**
  267. * Sets the value of the widget.<p>
  268. *
  269. * @param value the new value
  270. */
  271. public void setFormValue(Object value) {
  272. if (value == null) {
  273. value = "";
  274. }
  275. if (value instanceof String) {
  276. String strValue = (String)value;
  277. m_selectionInput.m_textbox.setText(strValue);
  278. setTitle(strValue);
  279. }
  280. }
  281. /**
  282. * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#setFormValueAsString(java.lang.String)
  283. */
  284. public void setFormValueAsString(String newValue) {
  285. setFormValue(newValue);
  286. }
  287. /**
  288. * Sets the link from a bean.<p>
  289. *
  290. * @param link the link bean
  291. */
  292. public void setLinkBean(CmsLinkBean link) {
  293. if (link == null) {
  294. link = new CmsLinkBean("", true);
  295. }
  296. m_selectionInput.m_textbox.setValue(link.getLink());
  297. }
  298. /**
  299. * Sets the name of the input field.<p>
  300. *
  301. * @param name of the input field
  302. * */
  303. public void setName(String name) {
  304. m_selectionInput.m_textbox.setName(name);
  305. }
  306. /**
  307. * Sets the text in the text area.<p>
  308. *
  309. * @param text the new text
  310. */
  311. public void setText(String text) {
  312. m_selectionInput.m_textbox.setValue(text);
  313. }
  314. /**
  315. * @see com.google.gwt.user.client.ui.UIObject#setTitle(java.lang.String)
  316. */
  317. @Override
  318. public void setTitle(String title) {
  319. m_selectionInput.m_textbox.getElement().setTitle(title);
  320. }
  321. /**
  322. * Creates the URL for the gallery dialog IFrame.<p>
  323. *
  324. * @return the URL for the gallery dialog IFrame
  325. */
  326. protected String buildGalleryUrl() {
  327. String basePath = "";
  328. basePath = "/system/workplace/commons/gallery.jsp";
  329. basePath += "?dialogmode=widget&fieldid=" + m_id;
  330. if (LINK.equals(m_type)) {
  331. basePath += "&" + I_CmsGalleryProviderConstants.CONFIG_RESOURCE_TYPES + "=pointer";
  332. }
  333. String pathparameter = m_selectionInput.m_textbox.getText();
  334. if (pathparameter.indexOf("/") > -1) {
  335. basePath += "&currentelement=" + pathparameter;
  336. }
  337. basePath += m_config;
  338. return CmsCoreProvider.get().link(basePath);
  339. }
  340. /**
  341. * Close the popup of this widget.<p>
  342. * */
  343. protected void close() {
  344. m_popup.hideDelayed();
  345. m_selectionInput.m_textbox.setFocus(true);
  346. m_selectionInput.m_textbox.setCursorPos(m_selectionInput.m_textbox.getText().length());
  347. }
  348. /**
  349. * Opens the popup of this widget.<p>
  350. * */
  351. protected void open() {
  352. m_oldValue = m_selectionInput.m_textbox.getValue();
  353. if (m_popup == null) {
  354. String title = org.opencms.gwt.client.Messages.get().key(
  355. org.opencms.gwt.client.Messages.GUI_GALLERY_SELECT_DIALOG_TITLE_0);
  356. m_popup = new CmsFramePopup(title, buildGalleryUrl());
  357. m_popup.setCloseHandler(new Runnable() {
  358. public void run() {
  359. String textboxValue = m_selectionInput.m_textbox.getText();
  360. if (!m_oldValue.equals(textboxValue)) {
  361. m_selectionInput.m_textbox.setValue("", true);
  362. m_selectionInput.m_textbox.setValue(textboxValue, true);
  363. }
  364. if (m_previewHandlerRegistration != null) {
  365. m_previewHandlerRegistration.removeHandler();
  366. m_previewHandlerRegistration = null;
  367. }
  368. m_selectionInput.m_textbox.setFocus(true);
  369. m_selectionInput.m_textbox.setCursorPos(m_selectionInput.m_textbox.getText().length());
  370. }
  371. });
  372. m_popup.setModal(false);
  373. m_popup.setId(m_id);
  374. m_popup.setWidth(717);
  375. m_popup.getFrame().setSize("705px", "640px");
  376. m_popup.addDialogClose(new Command() {
  377. public void execute() {
  378. close();
  379. }
  380. });
  381. } else {
  382. m_popup.getFrame().setUrl(buildGalleryUrl());
  383. }
  384. m_popup.setAutoHideEnabled(true);
  385. m_popup.center();
  386. if (m_previewHandlerRegistration == null) {
  387. m_previewHandlerRegistration = Event.addNativePreviewHandler(new CloseEventPreviewHandler());
  388. }
  389. }
  390. /**
  391. * Exporting the set principal function to the window scope.<p>
  392. */
  393. private native void exportSetPrincipalFunction()/*-{
  394. var self = this;
  395. $wnd.setPrincipalFormValue = function(typeFlag, principal) {
  396. self.@org.opencms.gwt.client.ui.input.CmsVfsSelection::setFormValueAsString(Ljava/lang/String;)(principal);
  397. self.@org.opencms.gwt.client.ui.input.CmsVfsSelection::close()();
  398. }
  399. }-*/;
  400. }