Skip to content

28 14_ModifyFile_Medium

Wil Simpson edited this page Jul 30, 2021 · 5 revisions

Portfolio Home Page

Problem Statement

Modify Exercise 28.13 to allow the client to modify the contents of the file and send the file back to the server for storage. The user can edit the file in a JTextArea, then click a save changes button to send the file back to the server.

User Documentation

To start the file editing application compile and run ModifyFileClient. To start the server compile and run ServerTest for an example file server. Start typing what you would like to save in the file editor. If you would like to open an existing file select *File -> Open..." and select a file you would like to open. To start a new file without saving select File -> New. To save the file to the server select File -> Save and the file will be stored locally in memory on the server.

Developer Documentation

ModifyFileClient is built in JavaFX while ServerTest is built in java swing. The JavaFX GUI is built from ModifyFileFX.fxml using SceneBuilder and controlled by ModifyFileController.

The controller creates a DatagramSocket and connects locally on an open port. When File -> Save is pressed in the GUI, saveFile(ActionEvent) is called and and attempts to send a packet to the local file server on port 5000 on the event thread. The packet contains the raw string data contained in the textArea which is the TextArea the client manipulate files. When File -> New is selected, openNewFile(ActionEvent) is clears the textArea. When File -> Open... is selected, openFile(ActionEvent) is called and creates a new FileChooser to select a file from storage. Once a file is selected the data is read with a BufferedReader and sets the textArea with the lines read.

Server creates a DatagramSocket using port 5000 and when waitForPackets() is called it runs on a new thread using SwingUtilities#invokeLater() and continuously attempts to read data from port 5000. Once a packet is received it is stored locally in memory in the files ArrayList<String> and lists the packet and files information to the displayArea. Server uses java swing to create a GUI by extending JFrame and adds a JScrollPane to the frame where all packet data information that is received will be appended to.

UML Diagram: UML Diagram

JavaDocs

The java documents are served from a local web server on this machine. To start the web server, navigate to the directory immediately above where the source code is checked out (i.e. ~/git ) and then use "python -m SimpleHTTPServer" in that directory.

cd ~/git
python -m SimpleHTTPServer&

Note: if you are running python 3 (which you can check via opening a terminal and typing: python --version), then the command is:

python3 -m http.server

Click Here to View JavaDocs

Source Code

Link to the source code