![]() |
|
#4
|
||||
|
||||
|
Pfeil, thanks for taking an interest! The relevant code is as follows:
Server path chooser; this is a javafx.stage.FileChooser instance, which returns a java.io.File object. The UI objects are retrieved from a map of controls by key and "file" is bound to the results of showing the file chooser dialog using the .showOpenDialog method (this is all that is in ui/show-chooser). This is just to keep the UI and the application logic separate. The file is then converted to an absolute path String using File.getCanonicalPath. Code:
(defn server-choose-command
"### server-choose-command
This zero argument function displays the server chooser dialog and uses
the provided file to set the server path in the UI."
[]
(let [{:keys [server-chooser
server-path-lbl]} @state/controls
file (ui/show-chooser server-chooser)]
(when file
(ui/set-label server-path-lbl (.getCanonicalPath file)))))
Code:
(defn server-path-select
[]
(let [{:keys [server-path-lbl]} @state/controls
server-path (ui/get-text server-path-lbl)]
(if (= server-path "...")
(reset! state/server-path nil)
(reset! state/server-path server-path))))
Code:
(defn get-text "### get-text This one argument function returns the text from the supplied control." ^String [control] (.getText control)) Code:
(defn set-single-remote
[]
(let [{:keys [single-path-fld single-path-lbl]} @state/controls
mission-path (ui/get-text single-path-fld)]
(when (not (string/blank? mission-path))
(ui/set-label single-path-lbl mission-path))))
Code:
(defn set-label "### set-label This two argument function sets the text content of the supplied control to the value of the supplied text argument." [^Label label text] (.setText label text)) Code:
(defn single-choose-command
[]
(let [{:keys [mis-chooser
single-path-lbl]} @state/controls
file (ui/show-chooser mis-chooser)]
(when file
(ui/set-label single-path-lbl
(get-resolved-path @state/server-path (.getCanonicalPath file))))))
Then the path is relativized against the missions directory, normalised to remove any redundant elements and converted to a String object, before replacing all backward slash characters with forward slash characters. The Clojure function str calls the .toString method for x when it is given the single object x. The methods used for canonisation and relativisation can be found in java.nio.file.Path. Code:
(defn get-resolved-path
[root-path in-path]
(let [path (Paths/get in-path (into-array [""]))
server-path (Paths/get root-path (into-array [""]))
server-dir (.getParent server-path)
mis-dir (.resolve server-dir "Missions")]
(string/replace (->> path (.relativize mis-dir) .normalize str) "\\" "/")))
This is the same global state atom update logic as with the server chooser, triggered by an InvalidationListener attached to the Label object: Code:
(defn single-path-select
[]
(let [{:keys [single-path-lbl]} @state/controls
single-path (ui/get-text single-path-lbl)]
(if (= single-path "...")
(reset! state/mission-path nil)
(reset! state/mission-path single-path))))
Code:
(defn load-unload-command
"### load-unload-command
This is a zero argument function which unloads the currently loaded mission if
it is loaded."
[]
(if @state/loaded
(server/unload-mission)
(when (= @state/mode "single")
(ui/toggle-prog-ind @state/controls true)
(server/load-mission @state/mission-path))))
Code:
(defn unload-mission "### unload-mission This is a zero argument function which sends the command to the server console which unloads the current mission. Because the unload command doesn't return any output on completion, we also request the mission state so that the state parsers can register the change in mission status." [] (write-socket "mission DESTROY") (get-mission-state)) Code:
(defn load-mission "### load-mission This is a one argument function which sends the command to the server console which loads a mission using the path described by the argument." [path-to-mission] (write-socket (str "mission LOAD " path-to-mission))) Code:
(defn write-socket "### write-socket This is a single argument function that simply calls the println method of the object that is stored in the socket-out atom using the argument that we provide. This atom should contain the instance of the PrintWriter object that is instantiated when we successfully connect to the server." [text] (.println ^PrintWriter @socket-out text)) Code:
(.print @socket-out (str text "\\n")) (.flush @socket-out) Output stream definition here: Code:
(reset! socket-out (PrintWriter.
(BufferedWriter.
(OutputStreamWriter.
(.getOutputStream ^Socket @socket)
(Charset/forName "UTF-8")))
true))
Hope this is a decent enough outline of what goes on (I'm not cagey about the code as I intend to open the repo to all once the basic feature set is complete). One of the methods that I used to try and detect any differences in the path was to set a comparator function on the single-choose-command function using clojure.data/diff, which compared the text content of the single mission path TextField to the return value of the get-resolved-path function. I loaded the mission using the TextField, loaded a different mission using the chooser (to trigger the bug), then loaded the original mission from the TextField using the FileChooser. In every instance it indicated that the value in the TextField (which worked every time) was the same as that returned by the FileChooser (which didn't), i.e. it returned (nil nil "Net/dogfight/DCG/dcgmission.mis"). The_WOZ, I will give your suggestion a try once I get home, thanks! Last edited by TheGrunch; 02-17-2014 at 10:55 PM. Reason: corrections |
|
|