PageRenderTime 25ms CodeModel.GetById 1ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

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