Ejemplo n.º 1
0
  public DistributedTextEditor() {
    area1.setFont(new Font("Monospaced", Font.PLAIN, 12));

    area2.setFont(new Font("Monospaced", Font.PLAIN, 12));
    ((AbstractDocument) area1.getDocument()).setDocumentFilter(dec);
    area2.setEditable(false);

    Container content = getContentPane();
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

    JScrollPane scroll1 =
        new JScrollPane(
            area1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    content.add(scroll1, BorderLayout.CENTER);

    JScrollPane scroll2 =
        new JScrollPane(
            area2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    content.add(scroll2, BorderLayout.CENTER);

    content.add(ipaddress, BorderLayout.CENTER);
    content.add(portNumber, BorderLayout.CENTER);

    JMenuBar JMB = new JMenuBar();
    setJMenuBar(JMB);
    JMenu file = new JMenu("File");
    JMenu edit = new JMenu("Edit");
    JMB.add(file);
    JMB.add(edit);

    file.add(Listen);
    file.add(Connect);
    file.add(Disconnect);
    file.addSeparator();
    file.add(Save);
    file.add(SaveAs);
    file.add(Quit);

    edit.add(Copy);
    edit.add(Paste);
    edit.getItem(0).setText("Copy");
    edit.getItem(1).setText("Paste");

    Save.setEnabled(false);
    SaveAs.setEnabled(false);
    Disconnect.setEnabled(false);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    area1.addKeyListener(k1);
    area1.addMouseListener(m1);
    setTitle("Disconnected");
    setVisible(true);
    area1.insert("Welcome to Hjortehandlerne's distributed text editor. \n", 0);

    this.addWindowListener(w1);
  }
 public void actionPerformed(ActionEvent e) {
   saveOld();
   area.setText("");
   currentFile = "Untitled";
   setTitle(currentFile + " - CoreyTextEditor");
   changed = false;
   Save.setEnabled(false);
   SaveAs.setEnabled(false);
 }
Ejemplo n.º 3
0
 /**
  * The disconnect method will stop the eventreplayer, and send null to the other peers, to stop
  * their reading from their streams. The GUI-menu is updated appropriately.
  */
 public void disconnect() {
   setTitle("Disconnected");
   active = false;
   if (connected == true) {
     er.stopStreamToQueue();
     ert.interrupt();
     setDocumentFilter(null);
     connected = false;
     setLocked(false);
   }
   deregisterOnPort();
   Disconnect.setEnabled(false);
   Connect.setEnabled(true);
   Listen.setEnabled(true);
   Save.setEnabled(true);
   SaveAs.setEnabled(true);
 }
Ejemplo n.º 4
0
        public void actionPerformed(ActionEvent e) {
          saveOld();
          area1.setText("");
          resetArea2();
          try {
            clientSocket = new Socket(ipaddress.getText(), Integer.parseInt(portNumber.getText()));
            Random r = new Random();
            serverport = 10000 + r.nextInt(8999); // random port :D

            serverSocket = new ServerSocket(serverport);
            active = true;
            editor.setTitleToListen();

            connected = true;

            ObjectOutputStream output = new ObjectOutputStream(clientSocket.getOutputStream());
            ObjectInputStream input = new ObjectInputStream(clientSocket.getInputStream());
            output.writeObject(new JoinNetworkRequest(serverport));

            ConnectionData data = getConnectionData(clientSocket, input);

            lc = new LamportClock(data.getId());
            lc.setMaxTime(data.getTs());
            dec = new DocumentEventCapturer(lc, editor);
            er = new EventReplayer(editor, dec, lc);
            ert = new Thread(er);
            ert.start();

            Peer peer =
                new Peer(
                    editor,
                    er,
                    data.getHostId(),
                    clientSocket,
                    output,
                    input,
                    lc,
                    clientSocket.getInetAddress().getHostAddress(),
                    data.getPort());
            dec.addPeer(peer);
            Thread thread = new Thread(peer);
            thread.start();

            er.setAcknowledgements(data.getAcknowledgements());
            er.setEventHistory(data.getEventHistory());
            er.setCarets(data.getCarets());

            er.addCaretPos(lc.getID(), 0);

            for (PeerWrapper p : data.getPeers()) {
              Socket socket;
              try {
                socket = connectToPeer(p.getIP(), p.getPort());
                ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
                outputStream.writeObject(
                    new NewPeerDataRequest(lc.getID(), serverSocket.getLocalPort(), 0));
                Peer newPeer =
                    new Peer(
                        editor,
                        er,
                        p.getId(),
                        socket,
                        outputStream,
                        inputStream,
                        lc,
                        p.getIP(),
                        p.getPort());
                dec.addPeer(newPeer);
                Thread t = new Thread(newPeer);
                t.start();
              } catch (IOException ex) {
                continue;
              }
            }

            Thread t1 =
                new Thread(
                    new Runnable() {

                      @Override
                      public void run() {
                        waitForConnection();
                      }
                    });
            t1.start();
            area1.setText(data.getTextField());
            area1.setCaretPosition(0);
            setDocumentFilter(dec);

            dec.sendObjectToAllPeers(new UnlockRequest(lc.getTimeStamp()));

            changed = false;
            Connect.setEnabled(false);
            Disconnect.setEnabled(true);
            Listen.setEnabled(false);
            Save.setEnabled(false);
            SaveAs.setEnabled(false);
          } catch (NumberFormatException | IOException e1) {
            setTitle("Unable to connect");
          }
        }
Ejemplo n.º 5
0
        public void actionPerformed(ActionEvent e) {
          saveOld();
          final InetAddress local;
          active = true;
          try {
            local = InetAddress.getLocalHost();
            Runnable server =
                new Runnable() {
                  public void run() {
                    serverport = Integer.parseInt(portNumber.getText());
                    int myID = getNewId();
                    registerOnPort();
                    editor.setTitleToListen();
                    clientSocket = waitForConnectionFromClient();
                    lc = new LamportClock(myID);
                    area1.setText("");
                    resetArea2();
                    if (clientSocket != null) {
                      listen = true;
                      connected = true;
                      dec = new DocumentEventCapturer(lc, editor);
                      setDocumentFilter(dec);
                      er = new EventReplayer(editor, dec, lc);
                      er.updateCaretPos(myID, 0);
                      ert = new Thread(er);
                      ert.start();

                      try {
                        ObjectOutputStream output =
                            new ObjectOutputStream(clientSocket.getOutputStream());
                        ObjectInputStream input =
                            new ObjectInputStream(clientSocket.getInputStream());
                        JoinNetworkRequest request = (JoinNetworkRequest) input.readObject();
                        int id = getNewId();
                        Peer peer =
                            new Peer(
                                editor,
                                er,
                                id,
                                clientSocket,
                                output,
                                input,
                                lc,
                                clientSocket.getInetAddress().getHostAddress(),
                                request.getPort());
                        ConnectionData cd =
                            new ConnectionData(
                                er.getEventHistory(),
                                er.getAcknowledgements(),
                                er.getCarets(),
                                id,
                                area1.getText(),
                                lc.getTimeStamp(),
                                lc.getID(),
                                dec.getPeers(),
                                serverSocket.getLocalPort());
                        dec.addPeer(peer);
                        er.addCaretPos(id, 0);
                        Thread t = new Thread(peer);
                        t.start();
                        peer.writeObjectToStream(cd);
                      } catch (IOException | ClassNotFoundException e) {
                        e.printStackTrace();
                      }
                    }
                    waitForConnection();
                  }
                };
            Thread serverThread = new Thread(server);
            serverThread.start();
          } catch (UnknownHostException e1) {
            e1.printStackTrace();
          }

          changed = false;
          Disconnect.setEnabled(true);
          Listen.setEnabled(false);
          Connect.setEnabled(false);
          Save.setEnabled(false);
          SaveAs.setEnabled(false);
        }
Ejemplo n.º 6
0
 public void keyPressed(KeyEvent e) {
   changed = true;
   Save.setEnabled(true);
   SaveAs.setEnabled(true);
 }
  /** Constructor to create the frame and its components */
  public CoreyTextEditor() {
    // Create a scroll pane
    area.setFont(new Font("Monospaced", Font.PLAIN, 12));
    JScrollPane scroll =
        new JScrollPane(
            area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    add(scroll, BorderLayout.CENTER);

    // Adds the system default look and feel

    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException
        | InstantiationException
        | UnsupportedLookAndFeelException
        | IllegalAccessException e) {
      e.printStackTrace();
    }

    // Create a menu bar
    JMenuBar JMB = new JMenuBar();
    setJMenuBar(JMB);
    JMenu file = new JMenu("File");
    JMenu edit = new JMenu("Edit");
    JMB.add(file);
    JMB.add(edit);

    // Finishing our menu bar
    file.add(New);
    file.add(Open);
    file.add(Save);
    file.add(SaveAs);
    file.addSeparator();
    file.add(Quit);

    edit.add(Cut);
    edit.add(Copy);
    edit.add(Paste);

    edit.getItem(0).setText("Cut");
    edit.getItem(0).setIcon(new ImageIcon("cut.gif"));
    edit.getItem(1).setText("Copy");
    edit.getItem(1).setIcon(new ImageIcon("copy.gif"));
    edit.getItem(2).setText("Paste");
    edit.getItem(2).setIcon(new ImageIcon("paste.gif"));

    // Time to make a toolbar!
    JToolBar tool = new JToolBar();
    add(tool, BorderLayout.NORTH);
    tool.add(New);
    tool.add(Open);
    tool.add(Save);
    tool.addSeparator();

    JButton cut = tool.add(Cut);
    JButton cop = tool.add(Copy);
    JButton pas = tool.add(Paste);

    cut.setText(null);
    cut.setIcon(new ImageIcon("cut.gif"));
    cop.setText(null);
    cop.setIcon(new ImageIcon("copy.gif"));
    pas.setText(null);
    pas.setIcon(new ImageIcon("paste.gif"));

    Save.setEnabled(false);
    SaveAs.setEnabled(false);

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    pack();

    /*
     KeyListener to change Save and SaveAs
    */
    KeyListener k1 =
        new KeyAdapter() {
          public void keyPressed(KeyEvent e) {
            changed = true;
            Save.setEnabled(true);
            SaveAs.setEnabled(true);
          }
        };
    area.addKeyListener(k1);
    setTitle(currentFile + " - CoreyTextEditor");
    setVisible(true);
  }