I develop a JavaFX GUI application and I have a problem with a node called "TreeView". My tree should represent a file structure inside a specific directory. The user is supposed to conduct some typical operations on those files like copying, removing or renaming them.
After that happens, I'd like the tree to be refreshed (it means that it is supposed to load once again the structure from the file system). The issue is that, once it is done, the selection on one of the tree items is lost and every directory is collapsed. I would like the selection to be persistent, and all the parent directories to be expanded, even after the content of the tree has been refreshed.
I've tried numerous solutions so far to address this issue, but none of them let me select the same tree item (apart from the deleting operation of course) and expand all needed roots as before. My question is, how can this be achieved? I guess the main problem here is the fact, that I load all the data once again and therefore my new tree items do not match with the "old" ones.
I would appreciate all hints that you can give me in that matter.
I tried to save the item with getSelectionModel() and then set it, it doesn't select the same item as before (it ignores that directories are not expanded).
Related
I am implementing an editor that works on a distant document. The document is first downloaded, copied in a local cached file, displayed to the user who edits and saves it. On the 'save' action, the document is uploaded back to the server. Classic.
But sometimes, if the distant document has been modified by another user during the download/edit/upload cycle, a conflict is detected and a compare editor is presented to the user : the local version is displayed on the left pane (and editable) and the distant on the right pane (and read-only). The user does the "merge" operation manually (I don't consider automatic merge yet) and when the save action is launched, the comparison result overwrites the localy cached file that is afterwards uploaded onto the server. This is still a classic issue.
The problem is: if the user wants to discard the distant modifications, and thus wants to save the document as is without modifying the left pane, the compare editor refuses to flush the content of the pane in the local file and I can not detect that the user launched the save action to init the upload operation. Why the compare editor refuses to overwrite the local version is quite simple: since no modification has been made, it considers that there is nothing to flush.
I am trying to find a way to make it believe something has changed, but It seems that the TextMergeViewer that handles the comparison is not listening to any model change, and is so far from my code, that I don't know how to reach it (and it seems that I could not set the left pane as dirty anyway).
In my system the user can make changes to JBoss Drools code either through a code editor or a GUI. My problem is if the user makes changes in the code editor and then subsequently in the GUI then the changes from the code editor are lost. Because the GUI tool works by taking a template of the code and plugging in changes made through parameters (FreeMarkerTemplateUtils.processTemplateIntoString).
Does anyone have any suggestions for this problem in general?
One thought was to use Diff Match Patch to somehow merge the changes from GUI with those in the code editor. If this is the way could you please share some code (most of what I found online was just for comparing files not for generating the acutal diff from Strings).
Thanks.
First assumption, the change pattern in code editor would always match with the one GUI uses?
e.g., if the file body pattern is something like
<some text .........>___ReplaceThisString__<some more text ..............>
If it is like that, you could write a method on GUI save, which works something like most of the code repository, like SVN's diff and merge do.
Make a line by line comparison between file saved by code editor (say left) and GUI (say right), you will have 3 conditions,
Only left (changes in a line by code editor content)
Only in right (changes in a line by GUI)
Conflict (a line changed in both code content and GUI)
Ideally, you could merge only left/right without any difficulty, with logic that you take the either change.
For 3rd condition, you must let user decide what to take to maintain correctness of the file functionally, otherwise the file may fail to behave correctly by the next program.
For letting the user decide, you may with to create a compare window showing the user, the difference in two files, and let user simply click on that which one needs to be taken.
Ideally, in all the cases, it is better to show diff and let user confirm that the merge is not breaking the content integrity of the file.
2 things are important here:
A) The merge functionality should be there for code editor as well as in GUI; whenever someone try to save something from code editor. If it is not possible to have this (say you are using a code editor from a 3rd party, e.g. notepad), you should go for 2-step save-promote solution.
In this case, only saving will not change the actual file, rather the file is written in a different location, as a temporary file.
In step-2, 'promote step' - You should have a tool (build one with above strategy) to compare the temp file and original file. Then the user could visually merge the changes.
Same applies for GUI editor.
B) Whenever you do a compare and merge, the original file must be locked for modification, so that another user do not change the file, while somebody is merging.
I call a webservice and get the following data from it:
Name of the folder
Id of the folder
Id of the parent-folder (null if it is root)
I create ArrayLists (List<String>) for the names, the ids and the parent-ids. So the folder with the name on position "0" has the id and the parent-id on position "0" in these lists.
Now I need to recreate the same structure on my local file system. The user enters a root-directory ("C:\test" for example) that I need to use.
I guess that a recursive method would be the best thing to do, but I have no idea how to implement it.
Any ideas / hints?
I don't see how recursion helps you. I assume you get multiple sets of the data you present, implied by your explanation though you don't say so. You also don't say what order you get them in. I'd create a hashmap, using full path to each parent as a key, and an object representing the directory as a value. The directory object would contain pointers to all its child directories. I'd create that entire hashmap, then walk it top-down. If you don't get the data in the correct order to build it top-down, then you'll have to put them all in a list and search the list to create top-down order, or trust that you can build the list without the IDs and fill them in later
I've been trying to get my head around this so maybe some of you can help me. I have a list of files with their full paths (these are just strings the files are on another machine), e.g:
C:\a\b\c\file1.txt
C:\a\b\c\file2.txt
C:\a\d\file3.txt
C:\e\file4.txt
I want to create a Jtree to show the directory structure like this:
C:
a
b
c
file1.txt
file2.txt
d
file3.tct
e
file4.txt
I've been spliting the string on the seperator so I end up with a list of arrays like:
"C:","a","b","c","file1.txt"
"C:","a","b","c","file2.txt"
"C:","a","d","file3.txt"
"C:","e","file4.txt"
Now I want to add them an index at a time but if the value already exists at that level then to skip to the next index. i.e it would add the first array then on the second array it would go on level 0 of the tree there already exists a "C:" so move onto level 1 of the tree and index 1 of the array. The issues that I have is that Im not sure how to navigate the tree in such a way.
Any suggestions and or alternative implementations?
Let File do the work of parsing and maintaining paths. As you want to display the files in a JTree, you might as well create a corresponding TreeModel such as FileTreeModel, cited here. Because it implements TreeModel, it can "be set as a JTree's model and then you'd have a plain old standard JTree." You can use any File in any mounted file system as the root, for example:
TreeModel model = new FileTreeModel(new File(System.getProperty("user.dir")));
JTree tree = new JTree(model);
I'm not sure if FileTreeModel is the best way - it scans entire directories. From what you wrote, I guess you only want to display paths from your list. You can achieve it by using TreePathsTreeModel described here: How I Show Windows Registry in jTree?
You just have to to convert filepaths from strings into TreePath objects.
First, sort the Strings (before splitting them).
How to process the first line is obvious and I won't comment on it. In the second line, search the already built tree and check if the nodes already exist. After you find one that does not exist, follow the procedure done in the first line.
I need to create multiple Child Nodes in one element node in XML, do I just append as many times as required to create these nodes? Like this:
rootElement.appendChild(creator);
creator.appendChild(name);
creator.appendChild(email);
creator.appendChild(name);
creator.appendChild(email);
Or does java automatically create the extra child nodes whenever I do this:
name.appendChild(doc.createTextNode("Bob"));
email.appendChild(doc.createTextNode("bob#email.com"));
name.appendChild(doc.createTextNode("Smith"));
email.appendChild(doc.createTextNode("smith#email.com"));
I'm not too sure how it works, any advice or help would be appreciated!
Behavior varies across different implementations, but in general you want to go with the second approach.
When appending or adding a child to a parent the previous parent is replaced. This means that the first approach does nothing but shuffle the same to children. The second approach is correct because you create new children as you go and the previously added children remain untouched by later API calls.