GWT's class similar to AWT Desktop? - java

Here is a little snippet of what I need to do:
if (Desktop.isDesktopSupported())
{
final Desktop desktop = Desktop.getDesktop();
try{
desktop.open(new File(path));
}
catch (final IOException e)
{
MessageBox.alert("Log View", "Error finding file", null);
}
}
I need to open a file in the logged in users default desktop application for that file type. Maven is giving me issues on build saying it can't find java.io.File and java.awt.Desktop.
Kind of a 2 part question, first, does GWT have some sort of similar functionality to java.awt.Desktop and how do I fix the project so maven finds the io and awt files?

To get the same effect as a JButton calling Desktop in HTML, you would provide an HTML button that links to a download of the file and ensure it has the correct content-type provided by the server.

Specifically to the GWT part of the question.
Yes you can do this. You would likely use either use a Grid, FlexTable, HTMLTable or event just HTML to display. You would embed that widget into a panel, and display that panel on the root panel.
The trick will be to get the file data from the server to the client. You can do that using either GWT RPC, or some kind of REST server. If you look at the default app that is produced when you start a GWT application, it will send back string data based on a string. You can use that as a pattern to bring down your model object. The default app uses GWT-RPC, and is pretty easy to follow.
The reason why I was vague on the type of widget is it will depend on how you want to present this data to your user. You can use an HTML to just render HTML, but I would think you would want to use a better widget to truly render this data.

No, you can't do this with GWT.
Browsers typically have no access to the file system. The best you can do is have the users install some plugin that gives you file access (will require a permission check) or have them upload a file to your server (your web app can download the file from there to inspect it).

Related

Drag-and-Drop file from filesystem to Java web application for IE9

I need to implement drag and drop functionality like in Google Drive. That is possibility to drag files from file system and drop it to browser window and start file uploading. Is there a way to implement it using Java?
In what way is it implemented in Google Drive and other similar services?
UPD:
There are several jQuery plugins for this but a haven't found no one that works correct with IE9.
I can't say about Java, but you can use Javascript/jQuery.
Check out this. Of course there are other options, but if you want to use it in your browser, I can't think of an easier way.
In Java, you can use this class named FileDrop in your Java Applet.
You can attach a FileDrop listener to a JComponent of your applet, such as a JPanel, and when users will drag and drop files onto it, the filesDropped method will be invoked, making available to you the array of files dropped.
JPanel myPanel = new JPanel();
new FileDrop( myPanel, new FileDrop.Listener()
{ public void filesDropped( java.io.File[] files )
{
// handle file drop
...
} // end filesDropped
}); // end FileDrop.Listener
Then, you can upload files via ftp, e.g. using the FTPClient class of the Apache commons-net library, to your ftp server.

Force Java to open XML in web browser

I have a program (GUI = JavaFX), which scans my file system for specific XML report files and lists them in a table. These XML reports are rendered in a web browser by XSLT. Now I want to be able to click on such a report in my Java application and have it displayed in the browser. I already wrote the handler and the correct URL is determined. On my Windows system this is
file://localhost/C:/report.xml
The XML is not the problem. If I open it manually in my browser everything works fine. However if I use Google and ask how to open files in a browser it always gives me this:
java.awt.Desktop.getDesktop().
browse(new java.net.URI("file://localhost/C:/report.xml"));
As this is a good solution for http URLs (web sites), it always opens my XML file in my default text editor (e.g. Notepad++). So the browse method of the Desktop doesn't really force the browse, but merely falls back to a default open operation.
So the question is: How can I force Java to open the XML in the browser similar to the Windows function "Open with >"?
Here an sscce (which should try to access the file in a browser, even though it doesn't exist):
public class XMLOpener {
public static void main(String[] args)
{
String fileURL = "file://localhost/C:/report.xml";
try {
java.awt.Desktop.getDesktop().browse(new java.net.URI(fileURL));
} catch (Exception e) {}
}
}
The JavaFX replacement for the awt Desktop.browse method would be HostServices.showDocument. You could try that, but it will likely have the same the same effect as Desktop.browse.
Another alternative is to load the XML and perform the XSL transform in Java, then display the resulting document in a JavaFX WebView using webview.getEngine.loadContent(contentString, contentType) or just display the resultant document in a Label or custom JavaFX control. Note that, as of JavaFX 2.2, the JavaFX WebView does not yet have a viewer for pretty printing an xml content type, so to get pretty printed xml in the webview you may need to parse and format the xml as an html document using javascript/css/html, similar to the method demonstrated in this post for displaying formatted java source in a WebView.
For me, although it's more development work, this alternate approach of handling the display with JavaFX is nice because the display of the resultant document can be encapsulated and controlled in the JavaFX application itself and you don't have a reliance on whatever browser and configuration may or may not be installed in the host environment.
If the browser can't handle the URI, browse launches "the application registered for handling URIs of the specified type is invoked".
Check your favourite browser exists (Chrome, Fx, O, IE, etc) or get it by some other method, and then execute a custom command. If you know the OS you're running on (windows) then you only need to consider the exec line for that.

How can I convert one slide of a PowerPoint file to flash in jsf app (using javaSE/javaEE)?

I need to show the first slide of a PowerPoint presentation in my jsf app. It must look something like that:
user upload the .ppt file to jsf app
the app take the first slide and converts it to Flash
user sees the converted slide in the separate (not as part of the another page)
I suppose that I couldn't use any external converters because the user of the system will not have them when he uses our system.
Also I'm thinkinig about Flash, not HTML, because the presentation can be dynamic.
Any ideas? Can I do this task or it's impossible and I need to think in another way (maybe restriction for end users - to save presentation as png, but I think that my boss won't like this decision).
Maybe I need to look at .xslt format, maybe it would help?
suppose that I couldn't use any external converters because the user of the system will not have them when he uses our system.
You can truly use external tools since your Java/JSF code runs at the webserver, not at the webclient (webbrowser). All the webbrowser get is just the Java/JSF-generated HTML/CSS/JS code. Open a page in webbrowser, rightclick and View Source and see it yourself. You just install the external tool at the webserver and execute it there.
To upload a file in JSF, you'll need to grab a 3rd party component library since the standard implementation doesn't have an upload component, for example Tomahawk's t:inputFileUpload or the one of whatever component library you're currently already using.
To convert PPT to Flash, execute the appropriate action using external tool in the managed bean action method and store the Flash file somewhere in the local disk file system of the webserver. You can store it in the public webcontent so that it's directly accessible by URL, but those files will be lost whenever you redeploy the webapp. If this shouldn't happen, then store it outside the public webcontent.
I don't have hands on experience with PPT-Flash converters, so I can't recommend a specific one, but Google learns me that there's pretty a lot of choice. There seems to be a Java solution of iSpring.
To display the Flash file, use the HTML <object> element which points to the URL of the Flash file. If the Flash file is stored outside the public webcontent, then you'll need to create a Servlet which gets an InputStream of the file from the local disk file system and writes it to the OutputStream of the response along a correct set of HTTP headers and then let the URL of the <object> element point to that instead.

Multiple file uploader with previews

I'm trying to find something that will let users upload multiple files to a website. The requirements are that it let them easily select multiple files (preferably with something like check boxes) and that it displays a preview of the images they select.
I'd prefer to only use Javascript or Flash if possible, but Java is also an option (this needs to work on platforms where Silverlight isn't available).
So far all I've been able to find are things that use the native file selector (which doesn't show previews on Windows, and makes it unclear that you can select multiple by holding ctrl).
I'm not sure if the preview requirement is even possible, but it's the most important.
This is a firefox solution:
It uses the FileReader javascript object to load, display and upload images.
http://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/
It still doesnt show previews in the FileSelection dialog but at least allows you to preview the images before uploading.
And here is a ready made java applet solution:
http://jumploader.com/doc_overview.html
To upload multiple files I use RichFaces rich:fileUpload component.
Concerning the preview, I've got the similar problem and the best I found after couple of days of googling is following.
Alfresco has the same problem and resolved it with :
An open office which runs in server mode (socket) and all the office documents are sent by alfresco to open office in order to convert them in PDF
Those PDF are converted to .swf viewer thanks to SWFTOOLS
This .swf is integrated in the HTML
For images, it uses ImageMagick to create small version of the file I suppose
Personnaly, I will try to implement it this way :
Converting office documents to PDF thanks to open office in socket mode
Transform the first page of the PDF into a PNG thanks to JPedal library
Diplay that PNG to the end user
For images I would perhaps use ImageMagick too ... but for now, I'm using Seam Image.scaleToFit API
I am assuming 2 things here:
1) Some kind of client/enduser will be doing the file upload
2) You get some kind of say on what the client installs on their computer to help make this happen.
If this is the case, my first suggestion would be:
Give them FTP or SFTP client software to upload files. The php page you make can have a link to Filezilla, along with instructions on how to use it. ftp and sftp are THE protocols to use for transferring files. HTTP is just not designed(well) for it, nor are browsers.
Once the user has the (S)FTP client software installed, you can give them URL's to upload files to that are specific to their user account, and you can have a backend script process and load/move files that they upload. It's pretty easy to create a local temporary directory using a server side script, have the client upload files via ftp, then go back to the web browser and click a button that says "Done uploading, please process my stuff".
The browser can even give back confirmations on everything that gets uploaded/processed.

Browser PDF File Previwer(Google Style)

I am working on an application which needs to preview privileged content in the browser. The preview should work in a way that its NOT possible to download the content. Only reading within the browser is allowed. I have looked at google docs preview but it needs the url of the docs to accessible online. I need to work with content in the intranet
The previewer should not mandate the installation of a pluggin as this would limit the access.
Any hints
Have a look at ImageMagick. If your're using, say, php, you could do something like
exec("convert -resize 500×800 {$file}.pdf {$file}.jpg");
and then display the resulting jpg-file on a webpage.
man convert should give you some further options.

Categories