Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 KB | None | 0 0
  1. ObservableList<String> stringList = EasyBind.map(myBaseList, myConverter::toString);
  2.  
  3. import org.fxmisc.easybind.EasyBind;
  4.  
  5. import javafx.beans.property.SimpleStringProperty;
  6. import javafx.beans.property.StringProperty;
  7. import javafx.collections.FXCollections;
  8. import javafx.collections.ListChangeListener.Change;
  9. import javafx.collections.ObservableList;
  10. import javafx.util.StringConverter;
  11.  
  12.  
  13. public class MappedAndTransformedListExample {
  14.  
  15. public static void main(String[] ags) {
  16. ObservableList<Person> baseList = FXCollections.observableArrayList(
  17. new Person("Jacob", "Smith", "jacob.smith@example.com"),
  18. new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
  19. new Person("Ethan", "Williams", "ethan.williams@example.com"),
  20. new Person("Emma", "Jones", "emma.jones@example.com")
  21. );
  22.  
  23. StringConverter<Person> converter = new StringConverter<Person>() {
  24.  
  25. @Override
  26. public String toString(Person person) {
  27. return person.getFirstName() + " " + person.getLastName();
  28. }
  29.  
  30. @Override
  31. public Person fromString(String string) {
  32. int indexOfDelimiter = string.indexOf(' ');
  33. return new Person(string.substring(0, indexOfDelimiter),
  34. string.substring(indexOfDelimiter+1),
  35. "");
  36. }
  37.  
  38. };
  39.  
  40. ObservableList<String> namesList = EasyBind.map(baseList, converter::toString);
  41.  
  42. namesList.forEach(System.out::println);
  43.  
  44. namesList.addListener((Change<? extends String> c) -> {
  45. while (c.next()) {
  46. if (c.wasAdded()) {
  47. System.out.println("Added "+c.getAddedSubList());
  48. }
  49. }
  50. });
  51.  
  52. System.out.println("nAdding Michael to base list...n");
  53. baseList.add(new Person("Michael", "Brown", "michael.brown@example.com"));
  54.  
  55. namesList.forEach(System.out::println);
  56. }
  57.  
  58.  
  59.  
  60. public static class Person {
  61. private final StringProperty firstName = new SimpleStringProperty(this, "firstName");
  62. private final StringProperty lastName = new SimpleStringProperty(this, "lastName");
  63. private final StringProperty email = new SimpleStringProperty(this, "email");
  64.  
  65. public Person(String firstName, String lastName, String email) {
  66. this.firstName.set(firstName);
  67. this.lastName.set(lastName);
  68. this.email.set(email);
  69. }
  70.  
  71. public final StringProperty firstNameProperty() {
  72. return this.firstName;
  73. }
  74.  
  75. public final String getFirstName() {
  76. return this.firstNameProperty().get();
  77. }
  78.  
  79. public final void setFirstName(final String firstName) {
  80. this.firstNameProperty().set(firstName);
  81. }
  82.  
  83. public final StringProperty lastNameProperty() {
  84. return this.lastName;
  85. }
  86.  
  87. public final java.lang.String getLastName() {
  88. return this.lastNameProperty().get();
  89. }
  90.  
  91. public final void setLastName(final java.lang.String lastName) {
  92. this.lastNameProperty().set(lastName);
  93. }
  94.  
  95. public final StringProperty emailProperty() {
  96. return this.email;
  97. }
  98.  
  99. public final java.lang.String getEmail() {
  100. return this.emailProperty().get();
  101. }
  102.  
  103. public final void setEmail(final java.lang.String email) {
  104. this.emailProperty().set(email);
  105. }
  106.  
  107. }
  108. }
  109.  
  110. ObservableList<String> namesList = EasyBind.map(baseList, converter::toString);
  111.  
  112. ObservableList<String> namesList = new TransformationList<String, Person>(baseList) {
  113.  
  114. @Override
  115. public int getSourceIndex(int index) {
  116. return index ;
  117. }
  118.  
  119. @Override
  120. public String get(int index) {
  121. return converter.toString(getSource().get(index));
  122. }
  123.  
  124. @Override
  125. public int size() {
  126. return getSource().size();
  127. }
  128.  
  129. @Override
  130. protected void sourceChanged(Change<? extends Person> c) {
  131. fireChange(new Change<String>(this) {
  132. @Override
  133. public boolean wasAdded() {
  134. return c.wasAdded();
  135. }
  136.  
  137. @Override
  138. public boolean wasRemoved() {
  139. return c.wasRemoved();
  140. }
  141.  
  142. @Override
  143. public boolean wasReplaced() {
  144. return c.wasReplaced();
  145. }
  146.  
  147. @Override
  148. public boolean wasUpdated() {
  149. return c.wasUpdated();
  150. }
  151.  
  152. @Override
  153. public boolean wasPermutated() {
  154. return c.wasPermutated();
  155. }
  156.  
  157. @Override
  158. public int getPermutation(int i) {
  159. return c.getPermutation(i);
  160. }
  161.  
  162. @Override
  163. protected int[] getPermutation() {
  164. // This method is only called by the superclass methods
  165. // wasPermutated() and getPermutation(int), which are
  166. // both overriden by this class. There is no other way
  167. // this method can be called.
  168. throw new AssertionError("Unreachable code");
  169. }
  170.  
  171. @Override
  172. public List<String> getRemoved() {
  173. ArrayList<String> res = new ArrayList<>(c.getRemovedSize());
  174. for(Person removedPerson: c.getRemoved()) {
  175. res.add(converter.toString(removedPerson));
  176. }
  177. return res;
  178. }
  179.  
  180. @Override
  181. public int getFrom() {
  182. return c.getFrom();
  183. }
  184.  
  185. @Override
  186. public int getTo() {
  187. return c.getTo();
  188. }
  189.  
  190. @Override
  191. public boolean next() {
  192. return c.next();
  193. }
  194.  
  195. @Override
  196. public void reset() {
  197. c.reset();
  198. }
  199. });
  200. }
  201.  
  202.  
  203. };
  204.  
  205. public static void main(String[] ags) {
  206. ObservableList<Person> baseList = FXCollections.observableArrayList(
  207. new Person("Jacob", "Smith", "jacob.smith@example.com"),
  208. new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
  209. new Person("Ethan", "Williams", "ethan.williams@example.com"),
  210. new Person("Emma", "Jones", "emma.jones@example.com")
  211. );
  212.  
  213. StringConverter<Person> converter = new StringConverter<Person>() {
  214.  
  215. @Override
  216. public String toString(Person person) {
  217. return person.getFirstName() + " " + person.getLastName();
  218. }
  219.  
  220. @Override
  221. public Person fromString(String string) {
  222. int indexOfDelimiter = string.indexOf(' ');
  223. return new Person(string.substring(0, indexOfDelimiter),
  224. string.substring(indexOfDelimiter+1),
  225. "");
  226. }
  227.  
  228. };
  229.  
  230. ObservableList<String> namesList = EasyBind.map(baseList, converter::toString);
  231.  
  232. ObservableList<String> namesListWithHeader = new TransformationList<String, String>(namesList) {
  233.  
  234. @Override
  235. public int getSourceIndex(int index) {
  236. return index - 1 ;
  237. }
  238.  
  239. @Override
  240. public String get(int index) {
  241. if (index == 0) {
  242. return "Contacts";
  243. } else {
  244. return getSource().get(index - 1);
  245. }
  246. }
  247.  
  248. @Override
  249. public int size() {
  250. return getSource().size() + 1 ;
  251. }
  252.  
  253. @Override
  254. protected void sourceChanged(Change<? extends String> c) {
  255. fireChange(new Change<String>(this) {
  256. @Override
  257. public boolean wasAdded() {
  258. return c.wasAdded();
  259. }
  260.  
  261. @Override
  262. public boolean wasRemoved() {
  263. return c.wasRemoved();
  264. }
  265.  
  266. @Override
  267. public boolean wasReplaced() {
  268. return c.wasReplaced();
  269. }
  270.  
  271. @Override
  272. public boolean wasUpdated() {
  273. return c.wasUpdated();
  274. }
  275.  
  276. @Override
  277. public boolean wasPermutated() {
  278. return c.wasPermutated();
  279. }
  280.  
  281. @Override
  282. public int getPermutation(int i) {
  283. return c.getPermutation(i - 1);
  284. }
  285.  
  286. @Override
  287. protected int[] getPermutation() {
  288. // This method is only called by the superclass methods
  289. // wasPermutated() and getPermutation(int), which are
  290. // both overriden by this class. There is no other way
  291. // this method can be called.
  292. throw new AssertionError("Unreachable code");
  293. }
  294.  
  295. @Override
  296. public List<String> getRemoved() {
  297. ArrayList<String> res = new ArrayList<>(c.getRemovedSize());
  298. for(String removed: c.getRemoved()) {
  299. res.add(removed);
  300. }
  301. return res;
  302. }
  303.  
  304. @Override
  305. public int getFrom() {
  306. return c.getFrom() + 1;
  307. }
  308.  
  309. @Override
  310. public int getTo() {
  311. return c.getTo() + 1;
  312. }
  313.  
  314. @Override
  315. public boolean next() {
  316. return c.next();
  317. }
  318.  
  319. @Override
  320. public void reset() {
  321. c.reset();
  322. }
  323. });
  324. }
  325.  
  326. };
  327.  
  328. namesListWithHeader.forEach(System.out::println);
  329.  
  330. namesListWithHeader.addListener((Change<? extends String> c) -> {
  331. while (c.next()) {
  332. if (c.wasAdded()) {
  333. System.out.println("Added "+c.getAddedSubList());
  334. System.out.println("From: "+c.getFrom()+", To: "+c.getTo());
  335. }
  336. }
  337. });
  338.  
  339. System.out.println("nAdding Michael to base list...n");
  340. baseList.add(new Person("Michael", "Brown", "michael.brown@example.com"));
  341.  
  342. namesListWithHeader.forEach(System.out::println);
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement