1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/*******************************************************************************
* Copyright (c) 2012,2013 BSI Business Systems Integration AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* BSI Business Systems Integration AG - initial API and implementation
******************************************************************************/
package org.eclipse.scout.svg.ui.rap.calendarfield;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;
import org.eclipse.scout.rt.client.ui.action.menu.IMenu;
import org.eclipse.scout.rt.client.ui.basic.calendar.CalendarComponent;
import org.eclipse.scout.rt.client.ui.basic.calendar.ICalendar;
import org.eclipse.scout.rt.client.ui.form.fields.calendarfield.ICalendarField;
import org.eclipse.scout.rt.ui.rap.RwtMenuUtility;
import org.eclipse.scout.rt.ui.rap.ext.MenuAdapterEx;
import org.eclipse.scout.rt.ui.svg.calendar.builder.AbstractCalendarDocumentBuilder;
import org.eclipse.scout.rt.ui.svg.calendar.builder.listener.ICalendarDocumentListener;
import org.eclipse.scout.svg.ui.rap.AbstractRwtScoutSvgComposite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Menu;
import org.w3c.dom.svg.SVGDocument;
public class RwtScoutCalendarField extends AbstractRwtScoutSvgComposite<ICalendarField<?>> implements IRwtScoutCalendarField {
private static final int CALENDAR_DISPLAY_MODE_COUNT = 4;
private static final int CONTEXT_MENU_POS_INSET = 10;
private P_InnerCalendarPropertyChangeListener m_innerCalPropertyListener;
private AbstractCalendarDocumentBuilder[] m_documentBuilders;
public RwtScoutCalendarField() {
m_documentBuilders = new AbstractCalendarDocumentBuilder[CALENDAR_DISPLAY_MODE_COUNT];
}
@Override
protected SVGDocument getSvgDocument() {
return getDocBuilder().getSVGDocument();
}
@Override
protected void updateSvgDocument() {
// inform the doc builder about the current size of the calendar (used for text scaling)
Rectangle r = getAbsoluteCalendarBounds();
getDocBuilder().setSize(r.width, r.height);
// continue refresh
super.updateSvgDocument();
}
@Override
protected void detachScout() {
if (m_innerCalPropertyListener != null && getScoutObject() != null && getScoutObject().getCalendar() != null) {
getScoutObject().getCalendar().removePropertyChangeListener(m_innerCalPropertyListener);
m_innerCalPropertyListener = null;
}
// release svg documents
for (int i = 0; i < m_documentBuilders.length; i++) {
if (m_documentBuilders[i] != null) {
m_documentBuilders[i].dispose();
}
m_documentBuilders[i] = null;
}
super.detachScout();
}
@Override
protected void attachScout() {
super.attachScout();
if (getScoutObject() != null && getScoutObject().getCalendar() != null) {
if (m_innerCalPropertyListener == null) {
m_innerCalPropertyListener = new P_InnerCalendarPropertyChangeListener();
getScoutObject().getCalendar().addPropertyChangeListener(m_innerCalPropertyListener);
}
initDocBuilder(getDocBuilder());
updateSvgDocument();
}
}
@Override
protected void handleScoutPropertyChange(String name, Object newValue) {
super.handleScoutPropertyChange(name, newValue);
if (ICalendar.PROP_VIEW_RANGE.equals(name)) {
updateSvgDocument();
}
else if (ICalendar.PROP_SELECTED_DATE.equals(name)) {
getDocBuilder().setNumContextMenus(getContextMenusFromScout().length);
updateSvgDocument();
}
else if (ICalendar.PROP_SELECTED_COMPONENT.equals(name)) {
getDocBuilder().setNumContextMenus(getContextMenusFromScout().length);
updateSvgDocument();
}
else if (ICalendar.PROP_COMPONENTS.equals(name)) {
getDocBuilder().setComponents(getScoutObject().getCalendar().getComponents());
updateSvgDocument();
}
else if (ICalendar.PROP_DISPLAY_MODE.equals(name)) {
initDocBuilder(getDocBuilder());
updateSvgDocument();
}
}
@Override
protected void hyperlinkActivatedFromUi(String url) {
if (url == null || url.length() < 1) {
return;
}
// forward the hyperlink event to the document
getDocBuilder().hyperlinkActivated(url);
}
private AbstractCalendarDocumentBuilder getDocBuilder() {
int mode = getScoutObject().getCalendar().getDisplayMode();
int index = mode - 1;
AbstractCalendarDocumentBuilder ret = m_documentBuilders[index];
if (ret == null) {
ret = AbstractCalendarDocumentBuilder.createInstance(mode);
initDocBuilder(ret);
ret.addCalendarDocumentListener(new P_CalendarDocumentListener());
m_documentBuilders[index] = ret; // save instance
}
return ret;
}
private void initDocBuilder(AbstractCalendarDocumentBuilder builder) {
Date selDate = getScoutObject().getCalendar().getSelectedDate();
CalendarComponent selComp = getScoutObject().getCalendar().getSelectedComponent();
builder.setShownDate(selDate);
builder.setSelection(selDate, selComp);
builder.setNumContextMenus(getContextMenusFromScout().length);
builder.setComponents(getScoutObject().getCalendar().getComponents());
setViewRangeFromUi(builder.getStartDate(), builder.getEndDate());
}
private void showPopupMenu() {
Rectangle calBounds = getAbsoluteCalendarBounds();
Point contextMenuPos = new Point(calBounds.x + calBounds.width - CONTEXT_MENU_POS_INSET, calBounds.y + calBounds.height - CONTEXT_MENU_POS_INSET);
createAndShowMenu(contextMenuPos);
}
private Menu createMenu() {
if (getUiField().getMenu() != null) {
getUiField().getMenu().dispose();
getUiField().setMenu(null);
}
Menu contextMenu = new Menu(getUiField().getShell(), SWT.POP_UP);
contextMenu.addMenuListener(new P_ContextMenuListener());
getUiField().setMenu(contextMenu);
return contextMenu;
}
private void createAndShowMenu(Point location) {
Menu menu = createMenu();
showMenu(menu, location);
}
private void showMenu(Menu menu, Point location) {
menu.setLocation(location);
menu.setVisible(true);
}
private Rectangle getAbsoluteCalendarBounds() {
Rectangle ret = new Rectangle(0, 0, 0, 0);
Rectangle browserBounds = getAbsoluteBrowserBounds();
float calRatio = AbstractCalendarDocumentBuilder.ORIG_CALENDAR_WIDTH / AbstractCalendarDocumentBuilder.ORIG_CALENDAR_HEIGHT;
if (calRatio < browserBounds.width / (double) browserBounds.height) {
// browser is wider than the calendar -> height limits the size of the calendar
ret.width = (int) Math.round(browserBounds.height * calRatio);
ret.height = browserBounds.height;
}
else {
ret.width = browserBounds.width;
ret.height = (int) Math.round(browserBounds.width / calRatio);
}
ret.x = browserBounds.x + (int) Math.round((browserBounds.width - ret.width) / 2.0);
ret.y = browserBounds.y + (int) Math.round((browserBounds.height - ret.height) / 2.0);
return ret;
}
private void setViewRangeFromUi(final Date start, final Date end) {
if (getScoutObject() != null && getScoutObject().getCalendar() != null) {
getUiEnvironment().invokeScoutLater(new Runnable() {
@Override
public void run() {
getScoutObject().getCalendar().getUIFacade().setVisibleRangeFromUI(start, end);
}
}, 0);
}
}
private void setDisplayModeFromUi(final int mode) {
getUiEnvironment().invokeScoutLater(new Runnable() {
@Override
public void run() {
getScoutObject().getCalendar().setDisplayMode(mode);
}
}, 0);
}
private void setSelectionFromUi(final Date d, final CalendarComponent c) {
if (getScoutObject() != null && getScoutObject().getCalendar() != null) {
getUiEnvironment().invokeScoutLater(new Runnable() {
@Override
public void run() {
getScoutObject().getCalendar().getUIFacade().setSelectionFromUI(d, c);
}
}, 0);
}
}
private IMenu[] getContextMenusFromScout() {
return RwtMenuUtility.collectMenus(getScoutObject().getCalendar(), true, true, getUiEnvironment());
}
private final class P_InnerCalendarPropertyChangeListener implements PropertyChangeListener {
@Override
public void propertyChange(final PropertyChangeEvent e) {
debugHandlePropertyChanged(e);
if (isIgnoredScoutEvent(PropertyChangeEvent.class, e.getPropertyName())) {
return;
}
if (isHandleScoutPropertyChange(e.getPropertyName(), e.getNewValue()) &&
getUiEnvironment().getDisplay() != null &&
!getUiEnvironment().getDisplay().isDisposed()) {
Runnable t = new Runnable() {
@Override
public void run() {
if (!isUiDisposed()) {
try {
getUpdateUiFromScoutLock().acquire();
handleScoutPropertyChange(e.getPropertyName(), e.getNewValue());
}
finally {
getUpdateUiFromScoutLock().release();
}
}
}
};
getUiEnvironment().invokeUiLater(t);
}
}
} // end class P_InnerCalendarPropertyChangeListener
private final class P_ContextMenuListener extends MenuAdapterEx {
private static final long serialVersionUID = 1L;
public P_ContextMenuListener() {
super(getUiField(), getUiContainer());
}
@Override
public void menuShown(MenuEvent e) {
super.menuShown(e);
Menu menu = ((Menu) e.getSource());
RwtMenuUtility.fillContextMenu(getContextMenusFromScout(), RwtScoutCalendarField.this.getUiEnvironment(), menu);
}
} // end class P_ContextMenuListener
private final class P_CalendarDocumentListener implements ICalendarDocumentListener {
@Override
public void visibleRangeChanged(Date start, Date end) {
setViewRangeFromUi(start, end);
}
@Override
public void displayModeMenuActivated(int displayMode) {
setDisplayModeFromUi(displayMode);
}
@Override
public void popupMenuActivated() {
showPopupMenu();
}
@Override
public void selectionChanged(Date selectedDate, CalendarComponent selectedComponent) {
setSelectionFromUi(selectedDate, selectedComponent);
}
} // end class P_CalendarDocumentListener
}
|