PathMustExists and FileMustExists in SWT FileDialog - java

To date I only used Swing to build graphical user interfaces but now I also want to make myself familiar with the Standard Widget Toolkit.
I already read the documentation and built a simple app.
My problem is now to use the FileDialog component.
I did the following code:
FileDialog openFileDialog = new FileDialog(shell, SWT.OPEN);
openFileDialog.setFilterExtensions(new String[] { "*.txt" });
openFileDialog.setFilterNames(new String[] { "Text files (*.txt)" });
openFileDialog.setText("Open file");
openFileDialog.open();
But I found no methods to set flags like "PathMustExists" or "FileMustExists".
Is this not possible with FileDialog?
Do I have to extend the class to implement that functionality? If so, how I have to proceed?
Or this there a better OpenFileDialog component (maybe in JFace) from which I don't know?

There are no options for this.
Since you are specifying SWT.OPEN you will get a file dialog specialized for opening existing files. Depending on which platform you are running on this dialog may not allow non-existent files to be selected at all (certainly true on Mac OS X). Still you should check the file after the dialog returns.

Related

Create java applications with MacBook Pro touchbar support?

as the tile describe, I would love to have the ability to add some cool touchbar buttons to my java application for MacBook Pro 2016 users.. I've not seen yet if there is a way to implement it in java yet.
Anyone got some knowledge on that?
There is a new Java library for interacting with the NSTouchBar API called JTouchBar.
For example using SWT
Shell shell = ...
JTouchBar jTouchBar = new JTouchBar();
jTouchBar.setCustomizationIdentifier("MySWTJavaTouchBar");
// flexible space
jTouchBar.addItem(new TouchBarItem(TouchBarItem.NSTouchBarItemIdentifierFlexibleSpace));
// button
TouchBarButton touchBarButtonImg = new TouchBarButton();
touchBarButtonImg.setTitle("Button 1");
touchBarButtonImg.setAction(new TouchBarViewAction() {
#Override
public void onCall( TouchBarView view ) {
System.out.println("Clicked Button_1.");
}
});
Image image = new Image();
img.setName(ImageName.NSImageNameTouchBarColorPickerFill);
touchBarButtonImg.setImage(image);
jTouchBar.addItem(new TouchBarItem("Button_1", touchBarButtonImg, true));
// label
TouchBarTextField touchBarTextField = new TouchBarTextField();
touchBarTextField.setStringValue("TextField 1");
jTouchBar.addItem(new TouchBarItem("TextField_1", touchBarTextField, true));
// enable touchbar
jTouchBar.enableForShell(shell);
You can find the library on Github:
https://github.com/Thizzer/JTouchBar
By the looks of it, apple doesn't provide support for adding items to the touch bar, never mind doing it in java.
While going through some documentation for the touch bar, it would appear that you will also need an instance of the NSTouchBarItem class. Java doesn't have that, nor provide a way to get that. I doubt that using native methods would work as well, seeing how the instance is app specific and is passed to the application through apple.
Accessing the bar is possible, but only natively.

Control name in dialog box of class SunAwtDialog

Usually, my AHK program reads the content of the "Filename:" text zone in file dialog boxes (Open, Save As, etc.) using the following info: read the content of control "Edit1" in dialog box of class "#32770". It can also read the list of controls using this piece of code:
; in Notepad, open the "Open" dialog box
WinGet, strControlsList1, ControlList, ahk_class #32770
WinGetTitle, strTitle1, ahk_class #32770
MsgBox, , %strTitle1% controls, %strControlsList1%
I'm trying to do the same in Java programs implemented for Windows (PDF Split and Merge and Geogebra) using Java Access Bridge, I guess. I know that these programs use the class name "SunAwtDialog" instead of the usual "#32770" for their dialog boxes. But I can't get access to the "Filename" control name. "Edit1" does not work. And I can't get the list of controls in this dialog box using this code:
; in a Java app like PDF Split and Merge, open the "Save As" dialog box
WinGet, strControlsList2, ControlList, ahk_class SunAwtDialog
WinGetTitle, strTitle2, ahk_class SunAwtDialog
MsgBox, , %strTitle2% controls, %strControlsList2%
Any idea how an AHK script could get info from these Java apps dialog boxes?

Render and Export FusionCharts on the Server

I am trying to render and export FusionCharts completely on the server. I am aware of solutions such as FCimg and FusionCharts .NET Solution. I have also implemented a Java solution that uses the Process class to run wkhtmltoimage.
However, I am trying to find a pure Java solution of doing this. I have an html file that includes FusionCharts JS Libraries and code to generate the fusion chart. I found JxBrowser that properly renders the chart but it requires X-Server for it to work on Linux. I also have tried Cobra/Lobo Browser but it does not fully support JavaScript. Are there any other ways to render and export fusion charts on the server or atleast render an html file that includes JavaScript completely in Java (and that does not require xserver)?
Thanks in advance for all the help!
Update: Solution that does not require xserver: WebRenderer. The Swing Edition is the only edition that supports HTML5 as of July 9th, 2012. You can use the swing edition to capture the image without a GUI.
I found a way that uses Eclipse's SWT Browser. However this cannot be run in an headless mode. You will have to use xserver to implement this. See this question.
Since this requires xserver and cannot be run in an headless mode, I would suggest using JxBrowser. It is a lot simpler and all you need is to generate an html file with all the fusion charts scripts. See #1, #2, #3
You have to create a template.html file that contains the header
(<html><head>), jquery.min.js, FusionCharts.js,
FusionCharts.HC.js, FusionCharts.HC.Charts.js. Make sure each of
these scripts are in their own script tags (<script type="text/javascript"> [js code] </script>)
Now add another JavaScript function with its own script tags containing the steps to render the chart. For example:
function load() { FusionCharts.setCurrentRenderer('javascript'); var chart = new FusionCharts("swf", 'chart0', "width", "height", "0", "1"); chart.setXMLData("XML DATA HERE"); chart.render("divNAMEHere"); }
Now you need to call the load() function onload, create a div to render the chart in, and end the html file. For example:
`
test
`
Create a new class that imports the eclipse swt browser libraries. Instantiate Display, Shell, and Browser (use this as a guideline to help understand what is happening: http://www.roseindia.net/tutorials/swt/swt-browser.shtml).
Set the text of the browser (browser.setText("htmlcode")) to the html code from template.html. The best way to do this would be to read the file using BufferedReader.
Lastly, the image takes some time to render. Now there is probably a better way to do this but if you want to just get it working, I set up a count and it captures the image after a certain number. This is what you need to add to the end:
int i = 0;
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
i++;
// System.out.println(i);
if(i==100)
{
GC source = new GC (shell);
Image image = new Image(display, browser.getClientArea());
source.copyArea(image, 0, 0);
ImageLoader io = new ImageLoader ();
io.data = new ImageData[] { image.getImageData() };
File f = new File (currentDir+"/workpng.png");
io.save (f.getAbsolutePath(), SWT.IMAGE_PNG);
}
}
}

JFileChooser: Cannot select Desktop when Selection Mode is File and Directories

I ran into an issue with JFileChooser and wanted to see if there is a workaround.
If the JFileChooser is created and the setFileSelectionMode is FILES_AND_DIRECTORIES, when a user clicks a shortcut button on the left (in XP) such as Desktop or My Documents or drop down to Desktop, the field is not placed in the File Name JTextPane. And when clicking the "Select/Accept" button, nothing happens (because isDirectorySelected() returns false for some reason).
Overriding the approveSeletion does not work because the Event Handler function in BasicFileChooser does not call it.
How would I make it so the Desktop can be selected without having to navigate to it manually, but by clicking the shortcut on the left?
Thanks
In Windows, the desktop is not backed by any file in the file system - it's a shell namespace. So there really isn't anything that JFileChooser could return to you. Yes, I know that there is a folder that contains the desktop for the user - but remember that the desktop actually displays as a composite of the user's desktop and the All Users desktop folder - plus other things that are added by the shell but not part of any folder (like the trash bin). So returning a File object that represents the 'desktop' is pretty much a non-starter.
Long and short: Ask yourself why you need to do this - chances are that you are going to wind up deep into native code territory, dealing with namespace PIDLs and all sorts of nastiness that you may not want to get into (for the life of me, I cannot understand why M$ had to make this stuff so amazingly difficult to use)...
Here's an intro to Windows shell namespaces so you'll have a feel for what's involved:
http://msdn.microsoft.com/en-us/library/cc144090%28v=vs.85%29.aspx
Found the following code in the BasicFileChooserUI:
if (fc.getFileSelectionMode() == JFileChooser.FILES_AND_DIRECTORIES
&& fc.getFileSystemView().isFileSystem(dir)) {
setFileName(dir.getAbsolutePath());
}
So it looks like "special folders" are purposely ignored. The code is in a private method so it would be hard to create you own UI.
As a hack you might be able to add a PropertyChangeListener to the file chooser:
public void propertyChange(final PropertyChangeEvent e)
{
String prop = e.getPropertyName();
if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop))
{
JFileChooser fileChooser = (JFileChooser)e.getSource();
File currentDirectory = (File)e.getNewValue();
String directory = currentDirectory.toString();
if (directory.endsWith("Desktop")
|| directory.endsWith("My Documents"))
{
File selectedFile = fileChooser.getSelectedFile();
if (selectedFile == null || ! selectedFile.equals(currentDirectory))
{
fileChooser.removePropertyChangeListener( this );
fileChooser.setSelectedFile( currentDirectory );
fileChooser.addPropertyChangeListener( this );
}
}
}
}

How do I setup up JFileChooser for single click behavior in java Swing?

How do I change the JFileChooser behavior from double-click selection to single-click selection mode?
I'm developing an application to run with either a single-click interface (nothing requires a double-click, just like the KDE interface mode) or a double-click interface (the default Windows interface mode or the regular GNOME interface mode). I want the Java application to behave just like the rest of the system to respect the user current configuration and environment.
The ideal solution should be to set up a configuration value somewhere in the JFileChooser class to have it work either under single-click ordouble-click mode.
Since it seems there is no such a configuration, here is an approximate solution based on Richie_W's idea. I had to extend it a bit in order to allow the user to navigate in many directories and also to avoid reentrant events that get fired when setting the selection. However, as Oscar pointed out, it is not possible to navigate using the keyboard (it always chooses whatever is under focus). If you don't use the keyboard, it works.
JFileChooser _fileChooser=new JFileChooser();
if (ConfigurationManager.isSingleClickDesired()) {
//We will be interested in files only, but we need to allow it to choose both
_fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
_fileChooser.addPropertyChangeListener(new PropertyChangeListener() {
//To prevent reentry
private boolean handlingEvent=false;
public void propertyChange(PropertyChangeEvent e) {
//Prevent reentry
if (handlingEvent)
return;
else
//Mark it as handling the event
handlingEvent=true;
String propertyName = e.getPropertyName();
//We are interested in both event types
if(propertyName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) ||
propertyName.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)){
File selectedFile = (File) e.getNewValue();
if (selectedFile!=null) {
if (selectedFile.isDirectory()) {
//Allow the user to navigate directories with single click
_fileChooser.setCurrentDirectory(selectedFile);
} else {
_fileChooser.setSelectedFile(selectedFile);
if (_fileChooser.getSelectedFile()!=null)
//Accept it
_fileChooser.approveSelection();
}
}
}
//Allow new events to be processed now
handlingEvent=false;
}
});
}
Ps->Sorry for not great looking code format, but StackoverFlow has broken Firefox and Iceweasel code format support under KDE and Gnome.

Categories