Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Presenter implements Observer {
- private ViewInterface ui;
- private Actions model;
- public Presenter(ViewInterface ui, Actions model)
- {
- System.out.println("presenter constructor");
- this.model=model;
- this.ui=ui;
- }
- @Override
- public void update(Observable arg0, Object arg1) {
- // TODO Auto-generated method stub
- if(arg0 == ui )
- {
- System.out.println("do action");
- this.doAction(ui.getUserCommand());
- }
- if(arg0 == model)
- {
- }
- }
- public void doAction(int act)
- {
- if(act==0)
- {
- model.loadgetsTest(ui.getUrl());
- System.out.println("Load....");
- }
- if(act==1)
- {
- model.setIsSchdualerOn(0);
- System.out.println("stoped");
- }
- if(act==2)
- //model.
- if(act==3)
- //model.
- if(act==6){}
- //model.
- }
- public class ActionsModel extends Observable implements Actions {
- public ActionsModel() {
- super();
- }
- private final String USER_AGENT = "Mozilla/45.0";
- private URL url;
- private InputStream is = null;
- private int isSchdualerOn = 0;
- @Override
- public void loadgetsTest(String url) {
- isSchdualerOn = 1;
- // TODO Auto-generated method stub
- try {
- this.url = new URL(url);
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- System.out.println("INVALID URL");
- e.printStackTrace();
- }
- while(isSchdualerOn == 1)
- {
- try {
- sendGet(url);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- System.out.println("GET FAILED");
- e.printStackTrace();
- }
- }
- }
- public class View extends Observable implements ViewInterface,Runnable {
- private JFrame frame;
- JLabel requestsCountLabel;
- int userCommand;
- String url;
- private JTextField urlTextField;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- View window = new View();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public View() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 450, 300);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- GridBagLayout gridBagLayout = new GridBagLayout();
- gridBagLayout.columnWidths = new int[]{0, 0};
- gridBagLayout.rowHeights = new int[]{0};
- gridBagLayout.columnWeights = new double[]{Double.MIN_VALUE, 1.0};
- gridBagLayout.rowWeights = new double[]{Double.MIN_VALUE};
- frame.getContentPane().setLayout(gridBagLayout);
- JButton loadURLXSSButton = new JButton("Load gets");
- requestsCountLabel = new JLabel("Request Count: ");
- frame.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
- GridBagConstraints gbc_loadURLXSSButton = new GridBagConstraints();
- gbc_loadURLXSSButton.insets = new Insets(0, 0, 0, 5);
- gbc_loadURLXSSButton.gridx = 0;
- gbc_loadURLXSSButton.gridy = 0;
- frame.getContentPane().add(loadURLXSSButton, gbc_loadURLXSSButton);
- urlTextField = new JTextField("Insert URL here");
- GridBagConstraints gbc_textField = new GridBagConstraints();
- gbc_textField.insets = new Insets(0, 0, 0, 5);
- gbc_textField.fill = GridBagConstraints.HORIZONTAL;
- gbc_textField.gridx = 1;
- gbc_textField.gridy = 0;
- frame.getContentPane().add(urlTextField, gbc_textField);
- urlTextField.setColumns(10);
- GridBagConstraints gbc_requestsCountLabel = new GridBagConstraints();
- gbc_requestsCountLabel.gridx = 2;
- gbc_requestsCountLabel.gridy = 0;
- frame.getContentPane().add(requestsCountLabel, gbc_requestsCountLabel);
- loadURLXSSButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- setUrl(urlTextField.getText());
- setUserCommand(0);
- System.out.println("load gets performed");
- notifyObservers();
- }
- });
- JButton stopGetsButton = new JButton("Stop gets");
- stopGetsButton.setHorizontalAlignment(SwingConstants.LEFT);
- GridBagConstraints gbc_stopGetsButton = new GridBagConstraints();
- gbc_stopGetsButton.anchor = GridBagConstraints.SOUTH;
- gbc_stopGetsButton.insets = new Insets(0, 0, 0, 5);
- gbc_stopGetsButton.gridx = 0;
- gbc_stopGetsButton.gridy = 0;
- frame.getContentPane().add(stopGetsButton, gbc_stopGetsButton);
- stopGetsButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- setUserCommand(0);
- System.out.println("Stop performed");
- notifyObservers();
- }
- });
- }
- @Override
- public void displayData(int[][] data) {
- this.requestsCountLabel.setText("Request Count: " + data[0][0]); //data[0][0] = requestCount
- notifyObservers();
- }
- // Setters and Getters
- public int getUserCommand() {
- return userCommand;
- }
- public void setUserCommand(int userCommand) {
- this.userCommand = userCommand;
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- initialize();
- }
- void displayData(int[][] data);
- int getUserCommand();
- public String getUrl();
- public void setUrl(String url);
- public class Main {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- ActionsModel am = new ActionsModel();
- View ui = new View();
- Presenter p = new Presenter(ui, am);
- ui.addObserver(p);
- am.addObserver(p);
- System.out.println("start");
- //new Thread(ui).start();
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement