How can I build a Java App with GUI and CLI? - java

I have to develop a small app to compare automatically generated folders. It must compare the folders, sub-folders and file contents. The problem is that this app needs to be launched either from a user on his computer to manually check for changes, or automatically along with the ANT nightlies. In the first case the results are displayed as a table in the Swing GUI. But in the other case, it must create a file to put the results in (format doesn't matter, XML, CSV, ...).
Anyone got some tips, or a link to a tutorial ?

You might want to add some command line option that switches between ui and file export, e.g. --gui or --export=[filename]. You could use Apache Commons CLI for that.

The other method is to create a set of classes that performs the task, and returns a set of values, which can then be either written to disk, or displayed in a GUI. I.e., an engine, and two front-ends (the GUI and the CLI).
for example:
public interface DirectoryComparer {
CompareResult performCompare(Directory dir1, Directory dir2);
public static interface CompareResult {
//...things here that you need, such as, file or dir difference, etc
Iterable<File> getFileDiff();
Iterable<Directory> getDirectoryDiff();
}
}
then, the GUI clients will just use DirectoryComparer to display the results, and the CLI client will write these results to a file or three. But those two clients are completely separate, and can be maintained separately.

Related

JavaFx Do something on first load [duplicate]

This question already has answers here:
Detect first time user in java app
(3 answers)
Closed 6 years ago.
I would like to pop up a window to select a file location when the user launch the software for the first time. I'm new to javafx and I looked for an answer on the web but no success...
Thanks in advance
I think I found it.
primaryStage.setOnShowing(event -> {
//Code here
});
It does action on first startup. I don't know if it's the best way to do it but that's how I did it. I already have a file with about 3 lines. I just added a 4th one with a random word and when the user launch the app it check if the word exist in the file. If so, it does nothing. If not, it ask the user to select the folder and if the selection is successful, it write the word.
Firstly, I would want to point out that your phrasing probably isn't clear enough for most people to understand exactly what you need. I am going to assume you have some kind of settings (like default application storage directory) which you need the user to specify at the first time the JAR is run. If the JAR file is run subsequently, it should not prompt for that again and use the settings previously specified.
Typically, when the user runs the JAR file, all the data would be isolated within that session. If the user closes the application and opens the application again, it would behave just like the previous run.
If you need to persist these data or settings, you can use Properties. This will save data in a separate file. The normal convention is to save it in the same folder as the JAR file, and named as config.properties.
At the start of the application, you should check if this file exists, if it does not exist, it means that this is the first run. Subsequently, when the user set the data (e.g. file folder), you would save it to the file.
You can find an example here.
Background
In order to implement a file selector, we can make use of JavaFX’s FileChooser. This will open a window giving us the opportunity to select a file.
What you’re asking for is for a FileChooser to open prior to entering the actual application. Let’s have a look at the implementation for something like that!
Implementation
At first, we’re going to need a JavaFX Application class that will open a window if we were to create a new instance out of it:
public class App extends Application
{
private final File file;
public App(File file)
{
this.file = file;
// Optionally provide ‘launch’ with some arguments
Application.launch();
}
#Override
public void start(final Stage stage)
{
// ...
stage.setScene(new Scene(insertNodeHere));
}
}
As I’m certain you’re already aware of—a class like this will open up a new window. This is the separate application class we’ll be calling once we’ve retrieved a File using the FileChooser in our main class.
In our main class, we’ll put this:
File file = fileChooser.showOpenDialog(stage);
if (file != null)
new App(file);
This will launch your application if the selected file didn’t turn out as null.
Moreover
With the implementation above in mind, you can complicate things as much as you feel like. Perhaps you’d like the application to start even if the file were null? In that case, there’s no need for the if statement.

Netbeans Ant build task to take multiple values in one input dialog

Upon building a jar/war etc I'd like to show the user the current version (stored in a properties file in the project) and already read into ns.MAJORVERSION and ns.MINORVERSION and allow them to update before writing to the manifest.
Ideally I'd like to take two inputs at once like so...
<input
message="Current Version is ${ns.MAJORVERSION} :${ns.MINORVERSION} update?"
addproperty="new.majorversion"
addproperty="new.minorversion"
/>
However this is not allowed and fails with an error saying 'Attribute "addproperty" was already specified'
Is there anyway of doing this in one dialog (I presume netbeans is setting the default input handler as a pop up dialog) or do I just need to have two inputs?
have single input as version in a format like MAJORVERSION.MINORVERSION and then split them in two separate properties.
PropertyRegex from ant contrib may come handy (http://ant-contrib.sourceforge.net/tasks/tasks/propertyregex.html)
Alternatively, yes, separate in two distinct inputs.

Auto Diff and Merge for Java Strings

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.

How to manipulate paths in a dynamic way

Currently my application using this path for taking images:
D:\Workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\webapps\patternImages
So inside of this folder might be n number of folders may contains n number folders and inside of the folders it may contain n number of images,
I need to get all the image names.
In this case there are 2 scenario we go for usually getting image path
Using the static string path to get the image names
Using system.getproperty()
But I need more dynamic way of approach:
By this "patternImages" folder can be placed anywhere in the web and I should have to get all the folders name and image names without any issue and also should know which folder contains which image? any way?
It's hard to say what would be a perfect fit for your application since we don't know everything you're doing, but since it's a GUI application, I imagine presenting the user with a GUI to pick the file path is your best option. What class you use for that depends on what GUI library you're using, but here's a tutorial for JFileChooser: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html. The basic code you need is this:
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
} else {
// Do nothing or log it
}
where fc is an instance of JFileChooser. Just make sure you configure the object for picking directories and not files. You would probably wire this up to a button. Whatever library you use should provide some kind of dialog element to allow the user to pick a directory.
If a graphical directory picker is not an option, then I think you're stuck with a configuration file. Even if you can use a directory picker, you might want to consider a configuration file to save the user's last choice.

Building Web Based SQL like client

We are storing our data in an internal key-value store in few different data formats, To view or update data in the store I have built a command line tool which accepts a commands in a custom query language (similar to Object Query Language) parses, interprets and displays it on the console.
I am thinking of building a web based GUI for this. Since I have limited experience with this, wanted to know what would be the easiest way to do it (in java).
Currently the UI just needs to have two elements a text area for accepting commands and a table for displaying the output. The output table should be simple enough to implement but I am stuck on the input element part, since the console application provides history of the commands by default but I am not sure what GUI element would be good for this. An alternative would be to have a big text box in which the user can type multiple commands and run any of them by simply highlighting and executing it like most standard gui sql clients do (
http://mywebsql.net/screenshots/?cat=Sql+Editors#/images/screens/02.Sql%20Editors/Multiple%20Editors.png)
I went through the Google Web Tool kit widget gallery ( https://developers.google.com/web-toolkit/doc/latest/RefWidgetGallery ) but none of them seem to fit my need.
Any suggestions on this would be very helpful.
Thanks
I don't think you'll find any component providing a history of previously executed commands for you. But nobody forbids you to implement your own solution.
Just provide a text field or text area to type a command, and display a list of previous commands next to the text field/area. When a previous command is clicked, replace the text of the text area by the text of the clicked command. And when a command is executed, add it to the history list.
Any web framework, from basic servlet/JSP to component-based framewoks, should allow you doing that.

Categories

Resources