Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. public class Presenter implements Observer {
  2. private ViewInterface ui;
  3. private Actions model;
  4.  
  5. public Presenter(ViewInterface ui, Actions model)
  6. {
  7. System.out.println("presenter constructor");
  8. this.model=model;
  9. this.ui=ui;
  10. }
  11.  
  12.  
  13.  
  14. @Override
  15. public void update(Observable arg0, Object arg1) {
  16. // TODO Auto-generated method stub
  17. if(arg0 == ui )
  18. {
  19. System.out.println("do action");
  20. this.doAction(ui.getUserCommand());
  21. }
  22. if(arg0 == model)
  23. {
  24.  
  25. }
  26.  
  27. }
  28.  
  29.  
  30. public void doAction(int act)
  31. {
  32. if(act==0)
  33. {
  34. model.loadgetsTest(ui.getUrl());
  35. System.out.println("Load....");
  36. }
  37. if(act==1)
  38. {
  39. model.setIsSchdualerOn(0);
  40. System.out.println("stoped");
  41. }
  42. if(act==2)
  43. //model.
  44. if(act==3)
  45. //model.
  46. if(act==6){}
  47. //model.
  48. }
  49.  
  50. public class ActionsModel extends Observable implements Actions {
  51.  
  52. public ActionsModel() {
  53. super();
  54. }
  55.  
  56.  
  57. private final String USER_AGENT = "Mozilla/45.0";
  58.  
  59. private URL url;
  60. private InputStream is = null;
  61. private int isSchdualerOn = 0;
  62.  
  63.  
  64.  
  65. @Override
  66. public void loadgetsTest(String url) {
  67. isSchdualerOn = 1;
  68. // TODO Auto-generated method stub
  69. try {
  70. this.url = new URL(url);
  71. } catch (MalformedURLException e) {
  72. // TODO Auto-generated catch block
  73. System.out.println("INVALID URL");
  74. e.printStackTrace();
  75. }
  76.  
  77.  
  78. while(isSchdualerOn == 1)
  79. {
  80. try {
  81. sendGet(url);
  82. } catch (Exception e) {
  83. // TODO Auto-generated catch block
  84.  
  85. System.out.println("GET FAILED");
  86. e.printStackTrace();
  87. }
  88.  
  89. }
  90.  
  91.  
  92.  
  93. }
  94.  
  95.  
  96.  
  97. public class View extends Observable implements ViewInterface,Runnable {
  98.  
  99. private JFrame frame;
  100. JLabel requestsCountLabel;
  101. int userCommand;
  102. String url;
  103. private JTextField urlTextField;
  104.  
  105. /**
  106. * Launch the application.
  107. */
  108. public static void main(String[] args) {
  109. EventQueue.invokeLater(new Runnable() {
  110. public void run() {
  111. try {
  112. View window = new View();
  113. window.frame.setVisible(true);
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. });
  119. }
  120.  
  121. /**
  122. * Create the application.
  123. */
  124. public View() {
  125. initialize();
  126. }
  127.  
  128. /**
  129. * Initialize the contents of the frame.
  130. */
  131. private void initialize() {
  132. frame = new JFrame();
  133. frame.setBounds(100, 100, 450, 300);
  134. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  135. GridBagLayout gridBagLayout = new GridBagLayout();
  136. gridBagLayout.columnWidths = new int[]{0, 0};
  137. gridBagLayout.rowHeights = new int[]{0};
  138. gridBagLayout.columnWeights = new double[]{Double.MIN_VALUE, 1.0};
  139. gridBagLayout.rowWeights = new double[]{Double.MIN_VALUE};
  140.  
  141. frame.getContentPane().setLayout(gridBagLayout);
  142. JButton loadURLXSSButton = new JButton("Load gets");
  143. requestsCountLabel = new JLabel("Request Count: ");
  144. frame.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
  145. GridBagConstraints gbc_loadURLXSSButton = new GridBagConstraints();
  146. gbc_loadURLXSSButton.insets = new Insets(0, 0, 0, 5);
  147. gbc_loadURLXSSButton.gridx = 0;
  148. gbc_loadURLXSSButton.gridy = 0;
  149. frame.getContentPane().add(loadURLXSSButton, gbc_loadURLXSSButton);
  150.  
  151. urlTextField = new JTextField("Insert URL here");
  152. GridBagConstraints gbc_textField = new GridBagConstraints();
  153. gbc_textField.insets = new Insets(0, 0, 0, 5);
  154. gbc_textField.fill = GridBagConstraints.HORIZONTAL;
  155. gbc_textField.gridx = 1;
  156. gbc_textField.gridy = 0;
  157. frame.getContentPane().add(urlTextField, gbc_textField);
  158. urlTextField.setColumns(10);
  159. GridBagConstraints gbc_requestsCountLabel = new GridBagConstraints();
  160. gbc_requestsCountLabel.gridx = 2;
  161. gbc_requestsCountLabel.gridy = 0;
  162. frame.getContentPane().add(requestsCountLabel, gbc_requestsCountLabel);
  163.  
  164.  
  165. loadURLXSSButton.addActionListener(new ActionListener() {
  166.  
  167. @Override
  168. public void actionPerformed(ActionEvent e) {
  169. // TODO Auto-generated method stub
  170. setUrl(urlTextField.getText());
  171. setUserCommand(0);
  172. System.out.println("load gets performed");
  173. notifyObservers();
  174.  
  175.  
  176. }
  177. });
  178.  
  179.  
  180. JButton stopGetsButton = new JButton("Stop gets");
  181. stopGetsButton.setHorizontalAlignment(SwingConstants.LEFT);
  182. GridBagConstraints gbc_stopGetsButton = new GridBagConstraints();
  183. gbc_stopGetsButton.anchor = GridBagConstraints.SOUTH;
  184. gbc_stopGetsButton.insets = new Insets(0, 0, 0, 5);
  185. gbc_stopGetsButton.gridx = 0;
  186. gbc_stopGetsButton.gridy = 0;
  187. frame.getContentPane().add(stopGetsButton, gbc_stopGetsButton);
  188.  
  189. stopGetsButton.addActionListener(new ActionListener() {
  190.  
  191. @Override
  192. public void actionPerformed(ActionEvent e) {
  193. // TODO Auto-generated method stub
  194. setUserCommand(0);
  195. System.out.println("Stop performed");
  196. notifyObservers();
  197. }
  198. });
  199. }
  200.  
  201. @Override
  202. public void displayData(int[][] data) {
  203. this.requestsCountLabel.setText("Request Count: " + data[0][0]); //data[0][0] = requestCount
  204. notifyObservers();
  205.  
  206. }
  207.  
  208.  
  209. // Setters and Getters
  210. public int getUserCommand() {
  211. return userCommand;
  212. }
  213.  
  214. public void setUserCommand(int userCommand) {
  215. this.userCommand = userCommand;
  216. }
  217.  
  218. public String getUrl() {
  219. return url;
  220. }
  221.  
  222. public void setUrl(String url) {
  223. this.url = url;
  224. }
  225.  
  226. @Override
  227. public void run() {
  228. // TODO Auto-generated method stub
  229. initialize();
  230. }
  231.  
  232. void displayData(int[][] data);
  233.  
  234. int getUserCommand();
  235.  
  236. public String getUrl();
  237.  
  238. public void setUrl(String url);
  239.  
  240. public class Main {
  241.  
  242. public static void main(String[] args) {
  243. // TODO Auto-generated method stub
  244.  
  245. SwingUtilities.invokeLater(new Runnable() {
  246.  
  247. @Override
  248. public void run() {
  249. // TODO Auto-generated method stub
  250. ActionsModel am = new ActionsModel();
  251. View ui = new View();
  252. Presenter p = new Presenter(ui, am);
  253. ui.addObserver(p);
  254. am.addObserver(p);
  255.  
  256. System.out.println("start");
  257. //new Thread(ui).start();
  258. }
  259. });
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement