Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - Observer Pattern in regards to some code. Mimic syncronized text update
    primarykey
    data
    text
    <p>The purpose of my code is to mimic the functionality of Google Docs - when a user types on one machine, the letters they type appear on another machine. For simplistic purposes, each machine types text in a gui, and a main class handles all changes.</p> <p><img src="https://i.stack.imgur.com/LYiCY.png" alt=""> Every machine has an "Editor" and is linked up to a total of 1 "File Content Subject". The "File Content Subject" is supposed to make the changes a user makes and send the updated code to all "Editors"</p> <p><img src="https://i.stack.imgur.com/atgx5.png" alt="enter image description here"></p> <p>This begins with a simple <strong>driver</strong> where I make a File Content Subject, create two Editors and connect these together</p> <pre><code>public class Driver { public static void main(String[] args) { FileContentSubject filecontentsubject = new FileContentSubject(); Editor e1 = new Editor(filecontentsubject); Editor e2 = new Editor(filecontentsubject); filecontentsubject.attach(e1); filecontentsubject.attach(e2); } } </code></pre> <p>The <strong>Editor</strong> looks like this (the two pop up windows, not the rest): <img src="https://i.stack.imgur.com/tq0PM.png" alt="enter image description here"></p> <p>The code that makes the Editor is here:</p> <pre><code>import javax.swing.*; import java.util.*; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import javax.swing.text.AbstractDocument; import javax.swing.text.BadLocationException; import javax.swing.text.Document; public class Editor extends JFrame implements DocumentListener, Observer { private FileContentSubject reference; private Document doc; private JScrollPane textAreaScrollPane; private JTextArea textArea; public Editor(FileContentSubject filecontentsubject) { super("Editor"); initComponents(); this.reference = filecontentsubject; textArea.getDocument().addDocumentListener(reference); } private void initComponents(){ textArea = new JTextArea(); textArea.setColumns(5); textArea.setLineWrap(true); textArea.setRows(50); textArea.setWrapStyleWord(true); textAreaScrollPane = new JScrollPane(textArea); setLocation(600,100); setSize(500,400); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(new BorderLayout()); getContentPane().add(textArea, BorderLayout.CENTER); } @Override public void changedUpdate(DocumentEvent arg0) { } @Override public void insertUpdate(DocumentEvent arg0) { reference.insertUpdate(arg0); } @Override public void removeUpdate(DocumentEvent arg0) { reference.removeUpdate(arg0); } @Override public void update() { //textArea.setText(reference.getJTextArea()); //textArea.setText(reference.temp); } } </code></pre> <p>The only <strong>Problem</strong> right now with my code is within the <strong>File Content Subject</strong>, when I try to change the code that I get passed by through the Editor. I am getting many "cannot mutate within notification" and somethings a "null pointer exception" error. The code that generates this is below and is the part I have <strong>commented out.</strong></p> <pre><code>import java.util.ArrayList; import java.util.List; import javax.swing.JTextArea; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.Position; public class FileContentSubject implements Subject, DocumentListener { private JTextArea textArea; private Document doc; private SubjectImpl reference; @Override public void attach(Observer o) { reference.attach(o); } @Override public void detach(Observer o) { reference.detach(o); } @Override public void notifyAllObservers() { reference.notifyAllObservers(); } public FileContentSubject(){ reference = new SubjectImpl(); textArea = new JTextArea(); textArea.setTabSize(5); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); textArea.getDocument().addDocumentListener(this); } @Override public void changedUpdate(DocumentEvent arg0) {} @Override public void insertUpdate(DocumentEvent arg0) { doc = (Document)arg0.getDocument(); // try { // //this.textArea.setText(doc.getText(0, doc.getLength()-1)); // } catch (BadLocationException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } notifyAllObservers(); } @Override public void removeUpdate(DocumentEvent arg0) { doc = (Document)arg0.getDocument(); // try { // this.textArea.setText(doc.getText(0, doc.getLength()-1)); // } catch (BadLocationException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } notifyAllObservers(); } public String getJTextArea(){ return textArea.getText(); } } </code></pre> <p>So my question is, how can I pass text in (by passing in a DocumentEvent through an Editor) into the File Content Subject, have it change the File Content Subject, and notify all Editors?</p> <hr> <p>My other classes that make this all happen (not important but shown for clarity):</p> <p><strong>Subject Interface</strong></p> <pre><code>/** * Interface */ public interface Subject { public void attach(Observer o); public void detach(Observer o); public void notifyAllObservers(); } </code></pre> <p><strong>Observer Interface</strong></p> <pre><code>public interface Observer { public void update(); } </code></pre> <p><strong>SubjectImpl class</strong></p> <pre><code>import java.util.ArrayList; import java.util.List; public class SubjectImpl implements Subject { private List &lt;Observer&gt; observers; public SubjectImpl(){ observers = new ArrayList&lt;Observer&gt;(); } @Override public void attach(Observer o) { observers.add(o); } @Override public void detach(Observer o) { observers.remove(o); } @Override public void notifyAllObservers() { for(Observer o: observers){ o.update(); } } } </code></pre> <hr> <hr> <hr> <h1><strong>ANSWER</strong></h1> <p>Needed to prevent the Editor that was editing itself from being updated. This was done with document properties and maintaining everything in a string rather than JTextArea. BIG thanks to acdcjunior for helping out and code answer are on the very last three code blocks in selected answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload