/ftr-gwt-library-date/src/main/java/eu/future/earth/gwt/client/date/horizontal/HorizontalDayField.java

http://ftr-gwt-library.googlecode.com/ · Java · 263 lines · 164 code · 47 blank · 52 comment · 39 complexity · cbb315b5b68b2ff7f14ee8f0f1b20a86 MD5 · raw file

  1. /*
  2. * Copyright 2007 Future Earth, info@future-earth.eu
  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 eu.future.earth.gwt.client.date.horizontal;
  17. import org.cobogw.gwt.user.client.CSS;
  18. import com.allen_sauer.gwt.dnd.client.util.Location;
  19. import com.allen_sauer.gwt.dnd.client.util.WidgetLocation;
  20. import com.google.gwt.user.client.ui.AbsolutePanel;
  21. import com.google.gwt.user.client.ui.FocusPanel;
  22. import com.google.gwt.user.client.ui.HorizontalPanel;
  23. import com.google.gwt.user.client.ui.SimplePanel;
  24. import com.google.gwt.user.client.ui.VerticalPanel;
  25. import com.google.gwt.user.client.ui.Widget;
  26. import eu.future.earth.gwt.client.FtrGwtDateCss;
  27. import eu.future.earth.gwt.client.date.EventPanel;
  28. public abstract class HorizontalDayField<T, M> extends EventPanel<T> {
  29. public static class DirectionConstant {
  30. public final int directionBits;
  31. public final String directionLetters;
  32. private DirectionConstant(int directionBits, String directionLetters) {
  33. this.directionBits = directionBits;
  34. this.directionLetters = directionLetters;
  35. }
  36. }
  37. /**
  38. * Specifies that resizing occur at the east edge.
  39. */
  40. public static final int DIRECTION_EAST = 0x0001;
  41. /**
  42. * Specifies that resizing occur at the both edge.
  43. */
  44. public static final int DIRECTION_NORTH = 0x0002;
  45. /**
  46. * Specifies that resizing occur at the south edge.
  47. */
  48. public static final int DIRECTION_SOUTH = 0x0004;
  49. /**
  50. * Specifies that resizing occur at the west edge.
  51. */
  52. public static final int DIRECTION_WEST = 0x0008;
  53. /**
  54. * Specifies that resizing occur at the east edge.
  55. */
  56. public static final DirectionConstant EAST = new DirectionConstant(DIRECTION_EAST, "e");
  57. /**
  58. * Specifies that resizing occur at the both edge.
  59. */
  60. public static final DirectionConstant NORTH = new DirectionConstant(DIRECTION_NORTH, "n");
  61. /**
  62. * Specifies that resizing occur at the north-east edge.
  63. */
  64. public static final DirectionConstant NORTH_EAST = new DirectionConstant(DIRECTION_NORTH | DIRECTION_EAST, "ne");
  65. /**
  66. * Specifies that resizing occur at the north-west edge.
  67. */
  68. public static final DirectionConstant NORTH_WEST = new DirectionConstant(DIRECTION_NORTH | DIRECTION_WEST, "nw");
  69. /**
  70. * Specifies that resizing occur at the south edge.
  71. */
  72. public static final DirectionConstant SOUTH = new DirectionConstant(DIRECTION_SOUTH, "s");
  73. /**
  74. * Specifies that resizing occur at the south-east edge.
  75. */
  76. public static final DirectionConstant SOUTH_EAST = new DirectionConstant(DIRECTION_SOUTH | DIRECTION_EAST, "se");
  77. /**
  78. * Specifies that resizing occur at the south-west edge.
  79. */
  80. public static final DirectionConstant SOUTH_WEST = new DirectionConstant(DIRECTION_SOUTH | DIRECTION_WEST, "sw");
  81. /**
  82. * Specifies that resizing occur at the west edge.
  83. */
  84. public static final DirectionConstant WEST = new DirectionConstant(DIRECTION_WEST, "w");
  85. private HorizontalItemResizeDragController<T, M> resizeDragController;
  86. private Widget eastWidget = null; // NOPMD;
  87. protected Widget dataWidget = null; // NOPMD;
  88. private HorizontalPanel holder = new HorizontalPanel();
  89. private HorizontalDateRenderer<T, M> horizontalDateRenderer = null;
  90. public HorizontalDayField(HorizontalDateRenderer<T, M> newRenderer, T data) {
  91. super(newRenderer, data);
  92. horizontalDateRenderer = newRenderer;
  93. setWidget(holder);
  94. holder.setSpacing(0);
  95. }
  96. private int width = 60;
  97. public int getWidth() {
  98. return width;
  99. }
  100. public void setWidth(int newWidth) {
  101. if (newWidth > 20) {
  102. width = newWidth;
  103. }
  104. holder.setWidth(width + "px");
  105. if (isDraggable()) {
  106. dataWidget.setWidth(width - 20 + "px");
  107. } else {
  108. dataWidget.setWidth(width + "px");
  109. }
  110. }
  111. public void setResizeDragController(HorizontalItemResizeDragController<T, M> newResizeDragController) {
  112. this.resizeDragController = newResizeDragController;
  113. if (eastWidget != null) {
  114. if (resizeDragController != null && isDraggable()) {
  115. resizeDragController.makeDraggable(eastWidget, direction);
  116. }
  117. }
  118. }
  119. private DirectionConstant direction = EAST;
  120. protected void setWidgetInsizeResize(Widget title, Widget newPanel) {
  121. SimplePanel wrap = new SimplePanel();
  122. wrap.setWidget(newPanel);
  123. wrap.setStyleName(FtrGwtDateCss.HOR_ITEM_CENTER_PANEL);
  124. dataWidget = wrap;
  125. if (super.getEventStyleName() == null) {
  126. holder.setStyleName(FtrGwtDateCss.HOR_ITEM_PANEL);
  127. } else {
  128. holder.setStyleName(getEventStyleName());
  129. }
  130. if (title != null) {
  131. holder.add(title);
  132. holder.setCellVerticalAlignment(title, VerticalPanel.ALIGN_TOP);
  133. }
  134. if (dataWidget != null) {
  135. holder.add(dataWidget);
  136. // holder.addStyleName(getEventStyleName());
  137. if (isDraggable()) {
  138. eastWidget = new FocusPanel();
  139. if (resizeDragController != null) {
  140. resizeDragController.makeDraggable(eastWidget, direction);
  141. }
  142. eastWidget.setSize("2px", "8px");
  143. eastWidget.addStyleName("eastDrag");
  144. holder.add(eastWidget);
  145. holder.setCellHorizontalAlignment(eastWidget, VerticalPanel.ALIGN_CENTER);
  146. holder.setCellVerticalAlignment(eastWidget, VerticalPanel.ALIGN_MIDDLE);
  147. }
  148. holder.setCellVerticalAlignment(dataWidget, VerticalPanel.ALIGN_MIDDLE);
  149. }
  150. }
  151. public void setEventStyleName(String newStyleName) {
  152. super.setEventStyleName(newStyleName);
  153. holder.setStyleName(getEventStyleName());
  154. if (dataWidget != null) {
  155. dataWidget.setStyleName(getEventStyleName());
  156. }
  157. if (eastWidget != null) {
  158. eastWidget.setStyleName(getEventStyleName());
  159. eastWidget.addStyleName("eastDrag");
  160. }
  161. }
  162. public void setBackGroundColor(String newColor) {
  163. CSS.setProperty(this, CSS.A.BACKGROUND_COLOR, newColor);
  164. CSS.setProperty(holder, CSS.A.BACKGROUND_COLOR, newColor);
  165. if (dataWidget != null) {
  166. CSS.setProperty(dataWidget, CSS.A.BACKGROUND_COLOR, newColor);
  167. }
  168. if (eastWidget != null) {
  169. CSS.setProperty(eastWidget, CSS.A.BACKGROUND_COLOR, newColor);
  170. }
  171. }
  172. public void setForeGroundColor(String newColor) {
  173. CSS.setProperty(this, CSS.A.COLOR, newColor);
  174. CSS.setProperty(holder, CSS.A.COLOR, newColor);
  175. if (dataWidget != null) {
  176. CSS.setProperty(dataWidget, CSS.A.COLOR, newColor);
  177. }
  178. if (eastWidget != null) {
  179. CSS.setProperty(eastWidget, CSS.A.COLOR, newColor);
  180. }
  181. }
  182. public abstract Widget getDraggableItem();
  183. public int getContentHeight() {
  184. return contentHeight;
  185. }
  186. private int contentHeight = 15;
  187. public void setContentHeight(int height) {
  188. super.setHeight(horizontalDateRenderer.getRowHeight() + "px");
  189. if (dataWidget != null) {
  190. dataWidget.setHeight((horizontalDateRenderer.getRowHeight() + 2) + "px");
  191. }
  192. contentHeight = height;
  193. if (dataWidget != null) {
  194. dataWidget.setVisible(true);
  195. }
  196. }
  197. public void moveBy(int right, int down) {
  198. final AbsolutePanel parent = getSizerPanel(this);
  199. final Location location = new WidgetLocation(this, parent);
  200. final int left = location.getLeft() + right;
  201. final int top = location.getTop() + down;
  202. parent.setWidgetPosition(this, left, top);
  203. }
  204. private AbsolutePanel getSizerPanel(Widget current) {
  205. Widget parent = current.getParent();
  206. if (parent instanceof AbsolutePanel) {
  207. return (AbsolutePanel) parent;
  208. } else {
  209. if (parent != null) {
  210. return getSizerPanel(parent);
  211. } else {
  212. return null;
  213. }
  214. }
  215. }
  216. }