Auto Diff and Merge for Java Strings - java

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.

Related

How to force TextMergeViewer to flush content even if content is unchanged?

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).

How to add a none editable and dispalable original creator tag to a file

How to add a none editable and dispalable original creator tag to a file to prevent cheating in exams.
I looking for a way, as it says, to stop cheating in exams. A way that the originators name, student/employee no or the like is added to the file in a way that I or anyone else can read it BUT in such a way that regardless of how many times that file is either edited or copied that originators Id stay permanently visible, something like the copyright maker in a boo eg, File created by A.N. Other
Thanks in advance for all help & suggestion or pointers as to available software, if any, all appreciated
What you are asking is generally not possible for generic files.
If you want to apply this to a certain type of file, like a text document or an image, you may find editors for that kind of file that allow to set some fields only when a new document is created, fields that cannot be altered afterwards. This would require the editor to use some kind of encryption so that you cannot simply open the file in a different editor or in binary and change the fields.
Also, this would require to force everybody to use that editor. And still, if the editor supports copy- and pasting, anybody that gets a hold of a document can still copy it's contents into a new file and set the fields then.

How can I pretty-print multiple Java files to a single page?

I am a teacher and would like my students to have in front of them pretty printouts of the source code to 4 short Java files. I don't want to waste paper (or have them shuffling papers around), so I would like to have the four files on a single page. I don't want to print (from Eclipse) each to a separate PDF, then combine them 4-up, since that would make the text tiny. I tried concatenating the four files into a single .java file in Eclipse, but, despite reading this question, I found no way of suppressing the display of errors (namely defining multiple public classes in a single file).
Update: I don't just want to print the code as text. I would like it pretty-printed, i.e., with syntax highlighting.
You can copy them from eclipse to open/libreoffice or word to keep the formatting, especially the colors.
You could use a program like Highlight: http://www.andre-simon.de/ (Results can be copied to with highlighting)
You could use Latex to handle the formatting
You could put the source together in one file, you have to make the necessary changes to make sure there is only one public class:
public class a {}
class B {}
Copy them all, to a text editor, and print from there.

Eclipse - sorting members on save

I like eclipse save actions feature, but I can't get rid of one little annoying thing. I use sorting members and methods on save.
The good thing is, eclipse moves member/method to correct position, alphabetically.
The bad thing is, when I am writing method and save it, eclipse moves method but not current caret position. So basically, I press CTRL+S and end somehwere at completely other place then I was before, so I have to scroll up/down to find new location of my method.
Is there some kind of workaround?
I am using Eclipse 3.6 Helios atm.
It sounds like a bug, try submitting it to eclipse at this site.
As a workaround - but not the best one, you can use Ctrl-O to find the method.
2 more workaround options:
Set a //TODO return here in the line that you want to return to, then you can filter the task view by a specific todo.
Put breakpoint before saving.
Generally I prefer not to use options like this, since when working with other poeple in a large scale project, it can cause a lot of noise. I mean that if you make a change to an existing file, then save it and then the format happen. next step you will commit this file to the repository. when someone or even you try to look at the diff of the change, it will be cluttered with all the formatting and sorting. So I prefer doing it as an independent change.

How Netbeans Editors know that file is not modified after undo?

Netbeans uses standard UndoManager API for implementation of undo functionality. But neither standard javax.swing.undo.UndoManager nor org.openide.awt.UndoRedo.Manager doesn't have any method to mark states as saved and check modified status. Nonetheless if you change the file and press undo, the file is marked as unmodified. How do they do that? I need the same functionality for my (non-text) editors if the Netbeans RCP application.
I figured this out. The CloneableEditSupport adds UndoableEdit when someone saves the file. This edit signals when someone undos from the saved state or redos to the saved state. Also it wraps next edits (up to the first significant one) to intercept redos from the saved state and undos to the saved state.
This trick only works if all UndoableEdits you generate are significant.

Categories

Resources