001: /**
002: * Copyright 2007 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.example.main.release.demos;
016:
017: import java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Locale;
022: import java.util.Random;
023: import org.apache.commons.beanutils.BeanPropertyValueEqualsPredicate;
024: import org.apache.commons.collections.CollectionUtils;
025: import org.araneaframework.example.main.TemplateBaseWidget;
026: import org.araneaframework.example.main.release.features.ExampleData;
027: import org.araneaframework.example.main.release.features.ExampleData.Client;
028: import org.araneaframework.framework.LocalizationContext.LocaleChangeListener;
029: import org.araneaframework.http.UpdateRegionContext;
030: import org.araneaframework.uilib.form.formlist.BeanFormListWidget;
031: import org.araneaframework.uilib.list.BeanListWidget;
032: import org.araneaframework.uilib.list.dataprovider.MemoryBasedListDataProvider;
033: import org.araneaframework.uilib.menu.ContextMenuItem;
034: import org.araneaframework.uilib.menu.ContextMenuWidget;
035: import org.araneaframework.uilib.menu.ContextMenuItem.ContextMenuEventEntry;
036:
037: /**
038: * @author Taimo Peelo (taimo@araneaframework.org)
039: */
040: public class DemoContextMenuWidget extends TemplateBaseWidget implements
041: LocaleChangeListener {
042: protected List friends = new ArrayList();
043:
044: private MemoryBasedListDataProvider dataProvider = new DataProvider();
045:
046: //Plays the role of a sequence
047: private Long lastId = new Long(0);
048:
049: {
050: Random rn = new Random();
051: List allSuggestions = new ArrayList();
052:
053: for (Iterator i = Arrays.asList(Locale.getISOCountries())
054: .iterator(); i.hasNext();) {
055: allSuggestions.add(new Locale("en", (String) i.next())
056: .getDisplayCountry(Locale.ENGLISH));
057: }
058:
059: for (int i = 0; i < ExampleData.males.length; i++) {
060: ExampleData.Client friend = new ExampleData.Client();
061: friend.setForename(ExampleData.males[i]);
062: friend.setId(lastId);
063: friend.setSex("M");
064: friend.setSurname(ExampleData.fungi[rn
065: .nextInt(ExampleData.fungi.length)]);
066: friend.setCountry((String) allSuggestions.get(rn
067: .nextInt(allSuggestions.size())));
068: friends.add(friend);
069: lastId = new Long(lastId.longValue() + 1);
070: }
071:
072: for (int i = 0; i < ExampleData.females.length; i++) {
073: ExampleData.Client friend = new ExampleData.Client();
074: friend.setForename(ExampleData.females[i]);
075: friend.setSex("F");
076: friend.setSurname(ExampleData.fungi[rn
077: .nextInt(ExampleData.fungi.length)]);
078: friend.setCountry((String) allSuggestions.get(rn
079: .nextInt(allSuggestions.size())));
080: friends.add(friend);
081: lastId = new Long(lastId.longValue() + 1);
082: }
083: }
084:
085: /* Editable list. */
086: private BeanListWidget list;
087: /* Actual holder of editable list rows (resides inside EditableBeanListWidget).
088: Look inside init() method to see where it comes from. */
089: private BeanFormListWidget formList;
090:
091: protected void init() throws Exception {
092: setViewSelector("release/demos/contextMenuDemo");
093:
094: getL10nCtx().addLocaleChangeListener(this );
095:
096: createList();
097: attachContextMenu();
098: }
099:
100: private void createList() {
101: list = new BeanListWidget(ExampleData.Client.class);
102: addWidget("list", list);
103: list.setOrderableByDefault(true);
104: list.addField("sex", "sed.Sex").like();
105: list.addField("forename", "sed.Forename").like();
106: list.addField("surname", "sed.Surname").like();
107: list.addField("country", "common.Country").like();
108: list.addField("dummy", null, false);
109:
110: list.setDataProvider(dataProvider);
111: }
112:
113: private class DataProvider extends MemoryBasedListDataProvider {
114: private static final long serialVersionUID = 1L;
115:
116: protected DataProvider() {
117: super (ExampleData.Client.class);
118: }
119:
120: public List loadData() throws Exception {
121: return friends;
122: }
123: }
124:
125: private ContextMenuWidget createListContextMenu() {
126: ContextMenuItem menu = new ContextMenuItem();
127: menu.addMenuItem(new ContextMenuItem(getL10nCtx().localize(
128: "common.View"), new ContextMenuEventEntry("viewRecord",
129: this , "cMenuparameterSupplier")));
130: menu.addMenuItem(new ContextMenuItem(getL10nCtx().localize(
131: "context.menu.ChangeSex"), new ContextMenuEventEntry(
132: "changeSex", this , "cMenuparameterSupplier")));
133: menu.addMenuItem(new ContextMenuItem(getL10nCtx().localize(
134: "common.Remove"), new ContextMenuEventEntry(
135: "deleteRecord", this , "cMenuparameterSupplier")));
136:
137: ContextMenuWidget contextMenuWidget = new ContextMenuWidget(
138: menu);
139:
140: return contextMenuWidget;
141: }
142:
143: public void onLocaleChange(Locale oldLocale, Locale newLocale) {
144: attachContextMenu();
145: }
146:
147: private void attachContextMenu() {
148: list.addWidget("contextmenu", createListContextMenu());
149: }
150:
151: private void handleEventViewRecord(String param) {
152: Client c = (Client) list.getRowFromRequestId(param);
153: getFlowCtx().start(new ClientViewWidget(c));
154: // XXX: this is a hack to work around the shortcoming of partial rendering -- namely when flow is switched,
155: // the regions that are supposed to be updated are of course lost and old flow remains on end-user screen
156: ((UpdateRegionContext) getEnvironment().getEntry(
157: UpdateRegionContext.class)).disableOnce();
158: }
159:
160: private void handleEventChangeSex(String param) {
161: Client c = (Client) list.getRowFromRequestId(param);
162: if (c.getSex().equals("M"))
163: c.setSex("F");
164: else
165: c.setSex("M");
166: }
167:
168: private void handleEventDeleteRecord(String param) {
169: final Client c = (Client) list.getRowFromRequestId(param);
170: friends.remove(CollectionUtils.find(friends,
171: new BeanPropertyValueEqualsPredicate("id", c.getId())));
172: list.refresh();
173: }
174: }
|