PageRenderTime 69ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/JacpFXTwitterSSEClient/src/main/java/org/jacp/twitter/components/TweetView.java

https://bitbucket.org/amoncsek/jeemax
Java | 184 lines | 117 code | 18 blank | 49 comment | 8 complexity | 43160946e391b7bcfd571658b0d9eff2 MD5 | raw file
  1. /************************************************************************
  2. *
  3. * Copyright (C) 2010 - 2012
  4. *
  5. * [TweetView.java]
  6. * AHCP Project (http://jacp.googlecode.com)
  7. * All rights reserved.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an "AS IS"
  17. * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  18. * express or implied. See the License for the specific language
  19. * governing permissions and limitations under the License.
  20. *
  21. *
  22. ************************************************************************/
  23. package org.jacp.twitter.components;
  24. import javafx.collections.FXCollections;
  25. import javafx.collections.ObservableList;
  26. import javafx.event.Event;
  27. import javafx.scene.Node;
  28. import javafx.scene.control.TableCell;
  29. import javafx.scene.control.TableColumn;
  30. import javafx.scene.control.TableView;
  31. import javafx.scene.control.cell.PropertyValueFactory;
  32. import javafx.scene.image.Image;
  33. import javafx.scene.image.ImageView;
  34. import javafx.scene.layout.*;
  35. import javafx.util.Callback;
  36. import org.jacp.api.action.IAction;
  37. import org.jacp.api.annotations.Component;
  38. import org.jacp.api.annotations.OnStart;
  39. import org.jacp.api.annotations.OnTearDown;
  40. import org.jacp.javafx.rcp.component.AFXComponent;
  41. import org.jacp.javafx.rcp.componentLayout.FXComponentLayout;
  42. import org.jacp.javafx.rcp.util.FXUtil.MessageUtil;
  43. import org.jacp.jee.entity.Tweet;
  44. import org.jacp.jee.entity.TwitterResult;
  45. import org.jacp.twitter.entities.Clear;
  46. import java.util.Arrays;
  47. import java.util.Collections;
  48. import java.util.List;
  49. import java.util.ResourceBundle;
  50. import java.util.logging.Logger;
  51. /**
  52. * A simple JacpFX UI component
  53. *
  54. * @author <a href="mailto:amo.ahcp@gmail.com"> Andy Moncsek</a>
  55. */
  56. @Component(defaultExecutionTarget = "PMain", id = "id001", name = "componentLeft", active = true, resourceBundleLocation = "bundles.languageBundle", localeID = "en_US")
  57. public class TweetView extends AFXComponent {
  58. private AnchorPane pane;
  59. private ObservableList<Tweet> tweets = FXCollections.observableArrayList();
  60. private final Logger log = Logger.getLogger(TweetView.class.getName());
  61. @Override
  62. /**
  63. * The handleAction method always runs outside the main application thread. You can create new nodes, execute long running tasks but you are not allowed to manipulate existing nodes here.
  64. */
  65. public Node handleAction(final IAction<Event, Object> action) {
  66. // runs in worker thread
  67. if (action.getLastMessage().equals(MessageUtil.INIT)) {
  68. return this.createUI();
  69. }
  70. return null;
  71. }
  72. @Override
  73. /**
  74. * The postHandleAction method runs always in the main application thread.
  75. */
  76. public Node postHandleAction(final Node arg0,
  77. final IAction<Event, Object> action) {
  78. // runs in FX application thread
  79. if (action.getLastMessage().equals(MessageUtil.INIT)) {
  80. this.pane = (AnchorPane) arg0;
  81. } else if (action.getLastMessage() instanceof TwitterResult) {
  82. TwitterResult result = (TwitterResult) action.getLastMessage();
  83. if (!result.getResults().isEmpty()) {
  84. tweets.addAll(result.getResults());
  85. Collections.sort(tweets);
  86. }
  87. } else if(action.getLastMessage() instanceof Clear){
  88. tweets.clear();
  89. }
  90. return this.pane;
  91. }
  92. @OnStart
  93. /**
  94. * The @OnStart annotation labels methods executed when the component switch from inactive to active state
  95. * @param arg0
  96. */
  97. public void onStartComponent(final FXComponentLayout arg0,
  98. final ResourceBundle resourceBundle) {
  99. this.log.info("run on start of TweetView ");
  100. }
  101. @OnTearDown
  102. /**
  103. * The @OnTearDown annotations labels methods executed when the component is set to inactive
  104. * @param arg0
  105. */
  106. public void onTearDownComponent(final FXComponentLayout arg0) {
  107. this.log.info("run on tear down of TweetView ");
  108. }
  109. /**
  110. * create the UI on first call
  111. *
  112. * @return
  113. */
  114. private Node createUI() {
  115. final AnchorPane anchor = AnchorPaneBuilder.create()
  116. .styleClass("roundedAnchorPaneFX").build();
  117. TableView table = new TableView();
  118. table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
  119. table.getColumns().addAll(createColumns());
  120. table.setItems(tweets);
  121. AnchorPane.setTopAnchor(table, 25.0);
  122. AnchorPane.setRightAnchor(table, 25.0);
  123. AnchorPane.setLeftAnchor(table, 25.0);
  124. anchor.getChildren().addAll(table);
  125. GridPane.setHgrow(anchor, Priority.ALWAYS);
  126. GridPane.setVgrow(anchor, Priority.ALWAYS);
  127. return anchor;
  128. }
  129. private List<TableColumn> createColumns() {
  130. TableColumn imageView = new TableColumn("image");
  131. imageView.setMinWidth(60);
  132. imageView.setCellValueFactory(
  133. new PropertyValueFactory<Tweet, String>("profile_image_url"));
  134. imageView.setCellFactory(new Callback<TableColumn<Tweet, String>, TableCell<Tweet, String>>() {
  135. @Override
  136. public TableCell<Tweet, String> call(TableColumn<Tweet, String> tweetStringTableColumn) {
  137. return new TableCell<Tweet, String>() {
  138. @Override
  139. public void updateItem(String item, boolean empty) {
  140. if (item!=null) {
  141. HBox box = new HBox();
  142. box.setSpacing(10);
  143. VBox vbox = new VBox();
  144. ImageView imageview = new ImageView();
  145. imageview.setFitHeight(50);
  146. imageview.setFitWidth(50);
  147. imageview.setImage(new Image(item, true));
  148. box.getChildren().addAll(imageview, vbox);
  149. setGraphic(box);
  150. }
  151. }
  152. };
  153. //To change body of implemented methods use File | Settings | File Templates.
  154. }
  155. });
  156. TableColumn nameView = new TableColumn("name");
  157. nameView.setMinWidth(100);
  158. nameView.setCellValueFactory(
  159. new PropertyValueFactory<Tweet, String>("from_user"));
  160. TableColumn messageView = new TableColumn("message");
  161. messageView.setMinWidth(500);
  162. messageView.setCellValueFactory(
  163. new PropertyValueFactory<Tweet, String>("text"));
  164. return Arrays.asList(imageView, nameView, messageView);
  165. }
  166. }