SampleData.java
上传用户:hengxinggs
上传日期:2008-01-15
资源大小:212k
文件大小:9k
源码类别:

PlugIns编程

开发平台:

Java

  1. /*******************************************************************************
  2.  * Copyright (c) 2005 IBM Corporation and others.
  3.  * All rights reserved. This program and the accompanying materials
  4.  * are made available under the terms of the Eclipse Public License v1.0
  5.  * which accompanies this distribution, and is available at
  6.  * http://www.eclipse.org/legal/epl-v10.html
  7.  * 
  8.  * Contributors:
  9.  *     IBM Corporation - initial API and implementation
  10.  *******************************************************************************/
  11. package org.eclipse.jface.examples.databinding.model;
  12. import java.util.ArrayList;
  13. import java.util.Arrays;
  14. import java.util.List;
  15. import org.eclipse.jface.databinding.BeanUpdatableFactory;
  16. import org.eclipse.jface.databinding.DataBinding;
  17. import org.eclipse.jface.databinding.IChangeListener;
  18. import org.eclipse.jface.databinding.IDataBindingContext;
  19. import org.eclipse.jface.databinding.ITree;
  20. import org.eclipse.jface.databinding.IUpdatableFactory;
  21. import org.eclipse.jface.databinding.swt.SWTUpdatableFactory;
  22. import org.eclipse.jface.databinding.viewers.ViewersUpdatableFactory;
  23. import org.eclipse.swt.widgets.Control;
  24. public class SampleData {
  25. public static Category WINTER_CATEGORY;
  26. public static Category SUMMER_CATEGORY;
  27. public static Adventure BEACH_HOLIDAY;
  28. public static Adventure RAFTING_HOLIDAY;
  29. public static Adventure WINTER_HOLIDAY;
  30. public static Adventure ICE_FISHING;
  31. public static Lodging FIVE_STAR_HOTEL;
  32. public static Lodging YOUTH_HOSTEL;
  33. public static Lodging CAMP_GROUND;
  34. public static Catalog CATALOG_2005;
  35. public static Transportation GREYHOUND_BUS;
  36. public static Transportation EXECUTIVE_JET;
  37. public static Account PRESIDENT;
  38. public static Account DENTIST;
  39. public static Account SANTA_CLAUS;
  40. public static Cart CART;
  41. public static AdventureFactory FACTORY;
  42. public static ITree CATALOG_TREE;
  43. public static ITree CATEGORY_TREE;
  44. public static Signon SIGNON_ADMINISTRATOR;
  45. public static Signon SIGNON_JOEBLOGGS;
  46. private static SWTUpdatableFactory swtUpdatableFactory = new SWTUpdatableFactory();
  47. private static ViewersUpdatableFactory viewersUpdatableFactory = new ViewersUpdatableFactory();
  48. static {
  49. initializeData();
  50. }
  51. public static void initializeData() {
  52. FACTORY = new AdventureFactory();
  53. CATALOG_2005 = FACTORY.createCatalog();
  54. // Categories
  55. WINTER_CATEGORY = FACTORY.createCategory();
  56. WINTER_CATEGORY.setName("Freeze Adventures");
  57. WINTER_CATEGORY.setId("100");
  58. CATALOG_2005.addCategory(WINTER_CATEGORY);
  59. SUMMER_CATEGORY = FACTORY.createCategory();
  60. SUMMER_CATEGORY.setName("Hot Adventures");
  61. SUMMER_CATEGORY.setId("200");
  62. CATALOG_2005.addCategory(SUMMER_CATEGORY);
  63. // Adventures
  64. WINTER_HOLIDAY = FACTORY.createAdventure();
  65. WINTER_HOLIDAY.setDescription("Winter holiday in France");
  66. WINTER_HOLIDAY.setName("Ski Alps");
  67. WINTER_HOLIDAY.setLocation("Chamonix");
  68. WINTER_HOLIDAY.setPrice(4000.52d);
  69. WINTER_HOLIDAY.setId("150");
  70. WINTER_HOLIDAY.setMaxNumberOfPeople(3);
  71. WINTER_CATEGORY.addAdventure(WINTER_HOLIDAY);
  72. ICE_FISHING = FACTORY.createAdventure();
  73. ICE_FISHING.setDescription("Ice Fishing in Helsinki");
  74. ICE_FISHING.setName("Ice Fishing");
  75. ICE_FISHING.setLocation("Finland");
  76. ICE_FISHING.setPrice(375.55d);
  77. WINTER_CATEGORY.addAdventure(ICE_FISHING);
  78. BEACH_HOLIDAY = FACTORY.createAdventure();
  79. BEACH_HOLIDAY.setDescription("Beach holiday in Spain");
  80. BEACH_HOLIDAY.setName("Playa");
  81. BEACH_HOLIDAY.setLocation("Lloret de Mar");
  82. BEACH_HOLIDAY.setPrice(2000.52d);
  83. BEACH_HOLIDAY.setId("250");
  84. SUMMER_CATEGORY.addAdventure(BEACH_HOLIDAY);
  85. RAFTING_HOLIDAY = FACTORY.createAdventure();
  86. RAFTING_HOLIDAY
  87. .setDescription("White water rafting on the Ottawa river");
  88. RAFTING_HOLIDAY.setName("Whitewater");
  89. RAFTING_HOLIDAY.setLocation("Ottawa");
  90. RAFTING_HOLIDAY.setPrice(8000.52d);
  91. RAFTING_HOLIDAY.setId("270");
  92. SUMMER_CATEGORY.addAdventure(RAFTING_HOLIDAY);
  93. // Lodgings
  94. FIVE_STAR_HOTEL = FACTORY.createLodging();
  95. FIVE_STAR_HOTEL.setDescription("Deluxe palace");
  96. FIVE_STAR_HOTEL.setName("Flashy");
  97. YOUTH_HOSTEL = FACTORY.createLodging();
  98. YOUTH_HOSTEL.setDescription("Youth Hostel");
  99. YOUTH_HOSTEL.setName("Basic");
  100. CAMP_GROUND = FACTORY.createLodging();
  101. CAMP_GROUND.setDescription("Camp ground");
  102. CAMP_GROUND.setName("WetAndCold");
  103. CATALOG_2005.addLodging(FIVE_STAR_HOTEL);
  104. CATALOG_2005.addLodging(YOUTH_HOSTEL);
  105. CATALOG_2005.addLodging(CAMP_GROUND);
  106. WINTER_HOLIDAY.setDefaultLodging(YOUTH_HOSTEL);
  107. // Transporation
  108. GREYHOUND_BUS = FACTORY.createTransportation();
  109. GREYHOUND_BUS.setArrivalTime("14:30");
  110. GREYHOUND_BUS.setPrice(25.50);
  111. CATALOG_2005.addTransportation(GREYHOUND_BUS);
  112. EXECUTIVE_JET = FACTORY.createTransportation();
  113. EXECUTIVE_JET.setArrivalTime("11:10");
  114. EXECUTIVE_JET.setPrice(1500.99);
  115. CATALOG_2005.addTransportation(EXECUTIVE_JET);
  116. // Accounts
  117. PRESIDENT = FACTORY.createAccount();
  118. PRESIDENT.setFirstName("George");
  119. PRESIDENT.setLastName("Bush");
  120. PRESIDENT.setState("TX");
  121. PRESIDENT.setPhone("1112223333");
  122. PRESIDENT.setCountry("U.S.A");
  123. DENTIST = FACTORY.createAccount();
  124. DENTIST.setFirstName("Tooth");
  125. DENTIST.setLastName("Fairy");
  126. DENTIST.setState("CA");
  127. DENTIST.setPhone("4543219876");
  128. DENTIST.setCountry("PainLand");
  129. SANTA_CLAUS = FACTORY.createAccount();
  130. SANTA_CLAUS.setFirstName("Chris");
  131. SANTA_CLAUS.setLastName("Chringle");
  132. SANTA_CLAUS.setState("WI");
  133. SANTA_CLAUS.setPhone("8617429856");
  134. SANTA_CLAUS.setCountry("NorthPole");
  135. CATALOG_2005.addAccount(PRESIDENT);
  136. CATALOG_2005.addAccount(DENTIST);
  137. CATALOG_2005.addAccount(SANTA_CLAUS);
  138. // Signons
  139. SIGNON_ADMINISTRATOR = new Signon("Administrator","Foo123Bar");
  140. SIGNON_JOEBLOGGS = new Signon("JoeBloggs","Harry5Potter");
  141. CATALOG_2005.addSignon(SIGNON_ADMINISTRATOR);
  142. CATALOG_2005.addSignon(SIGNON_JOEBLOGGS);
  143. CART = FACTORY.createCart();
  144. CATALOG_TREE = new ITree() {
  145. Catalog catalog = CATALOG_2005;
  146. public boolean hasChildren(Object element) {
  147. if (element instanceof Catalog) {
  148. return true;  
  149. }
  150. else if (element instanceof Category) {
  151. Adventure[] list = ((Category)element).getAdventures();
  152. return list==null?true:list.length>0;
  153. }
  154. else if (element instanceof Lodging) {
  155. }
  156. return false;
  157. }
  158. public void setChildren(Object parentElement, Object[] children) {
  159. // ReadOnly for Adding Elements
  160. }
  161. public Object[] getChildren(Object parentElement) {
  162. if (parentElement==null)
  163. return new Object[] { catalog };
  164. else if (parentElement instanceof Catalog) {
  165. List list = new ArrayList();
  166. list.addAll(Arrays.asList(((Catalog)parentElement).getCategories()));
  167. list.addAll(Arrays.asList(((Catalog)parentElement).getLodgings()));
  168. list.addAll(Arrays.asList(((Catalog)parentElement).getAccounts()));
  169. return list.toArray();
  170. }
  171. else if (parentElement instanceof Category)
  172.    return ((Category)parentElement).getAdventures();
  173. return null;
  174. }
  175. public Class[] getTypes() {
  176. return new Class[]  { Catalog.class, Category.class, Lodging.class, Account.class, Adventure.class } ;
  177. }
  178. public void addTreeChangeListener(IChangeListener listener) {
  179. }
  180. public void removeTreeChangeListener(IChangeListener listener) {
  181. }
  182. public void dispose() {
  183. }
  184. };
  185. CATEGORY_TREE = new ITree() {
  186. Catalog catalog = CATALOG_2005;
  187. public boolean hasChildren(Object element) {
  188. if (element instanceof Catalog) {
  189. return true;  
  190. }
  191. else if (element instanceof Category) {
  192. Adventure[] list = ((Category)element).getAdventures();
  193. return list==null?true:list.length>0;
  194. }
  195. return false;
  196. }
  197. public void setChildren(Object parentElement, Object[] children) {
  198. // ReadOnly for Adding Elements
  199. }
  200. public Object[] getChildren(Object parentElement) {
  201. if (parentElement==null)
  202. return catalog.getCategories();
  203. else if (parentElement instanceof Category)
  204.    return ((Category)parentElement).getAdventures();
  205. return null;
  206. }
  207. public Class[] getTypes() {
  208. return new Class[]  { Category.class, Account.class } ;
  209. }
  210. public void addTreeChangeListener(IChangeListener listener) {
  211. }
  212. public void removeTreeChangeListener(IChangeListener listener) {
  213. }
  214. public void dispose() {
  215. }
  216. };
  217. }
  218. public static IDataBindingContext getDatabindingContext(Control aControl) {
  219. IDataBindingContext result = DataBinding.createContext(aControl,
  220. new IUpdatableFactory[] { new BeanUpdatableFactory(),
  221. swtUpdatableFactory, viewersUpdatableFactory});
  222. return result;
  223. }
  224. public static SWTUpdatableFactory getSWTUpdatableFactory() {
  225. return swtUpdatableFactory;
  226. }
  227. public static ViewersUpdatableFactory getViewersUpdatableFactory(){
  228. return viewersUpdatableFactory;
  229. }
  230. }