How to open a generated PDF in vaadin? - java

In my vaadin application I have a Table with an additional column containing a print Button. The Button calls the following util method to create a pdf and open it in a new window (ui parameter is the button):
public static void printPDF(Offer offer, AbstractComponent ui) throws IOException, DocumentException, TemplateException {
// ... create PDF
FileResource resource = new FileResource(pdfFile);
BrowserWindowOpener opener = new BrowserWindowOpener(resource);
opener.setFeatures("");
opener.extend(ui);
}
Now clicking the button the first time does not work. Clicking it the second time works. Clicking it the third time, opens two windows. This increases on every further click.
I also want to open the pdf using the context menu e.g.
table.addActionHandler(new Handler()...
There I don't even have a button to extend. I would prefer to, not use the .extend() part and just open a new window. How can I do that?
EDIT: This blocks the button from opening mulitple instances, still not a nice solution and the first click does not work.
Collection<Extension> extensions = ui.getExtensions();
for (Extension e : extensions) {
if (e instanceof BrowserWindowOpener) {
((BrowserWindowOpener) e).setResource(resource);
return;
}
}
I guess I would need to create a BrowserWindowOpener for every print Button in my Table.
Not a very clean solution, the table may contain lots of rows which would create a lot of BrowserWindowOpener instances which will never be used. The context menu problem would not be solved as well.
EDIT2: This is the other solution I tried:
ResourceReference rr = ResourceReference.create(resource, ui, "print");
Page.getCurrent().open(rr.getURL(), "blank_");
Here I get the following error:
Button (175) did not handle connector request for
print/2016_9090_R_1634500091131558445.pdf

You can use the FileDownloader to achieve what you want.
FileResource resource = new FileResource(pdfFile);
FileDownloader downloader = new FileDownloader(resource);
Button pdf= new Button("Download PDF");
downloader.extend(pdf);

Use this code
Window window = new Window();
((VerticalLayout) window.getContent()).setSizeFull();
window.setResizable(true);
window.setCaption("Exemplo PDF");
window.setWidth("800");
window.setHeight("600");
window.center();
StreamSource s = new StreamResource.StreamSource() {
#Override
public InputStream getStream() {
try {
File f = new File("C:/themes/repy.pdf");
FileInputStream fis = new FileInputStream(f);
return fis;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
};
StreamResource r = new StreamResource(s, "repy.pdf", mainLayout.getApplication());
Embedded e = new Embedded();
e.setSizeFull();
e.setType(Embedded.TYPE_BROWSER);
r.setMIMEType("application/pdf");
e.setSource(r);
window.addComponent(e);
getMainWindow().addWindow(window);

Related

Vaadin FileDownload requires the button be clicked twice to download

I'm following the Vaadin sample code but when I do I require a second click to start the file download. Below is my code:
final StreamResource streamResource = new StreamResource(
() -> {
return new ByteArrayInputStream("hello world".getBytes());
}, document.getName() + ".txt");
FileDownloader fileDownloader = new FileDownloader(streamResource);
fileDownloader.extend(getDownloadButton());
There's nothing special about the code to create the button but as requested in the comments here it is:
Button downloadButton = new Button("Download");
To me, I inited FileDownloader in constructor with dummy resource:
if (fileDownloader == null) {
fileDownloader = new FileDownloader(streamResourceDummy);
fileDownloader.extend(button);
}
And set actual resource in button event listener:
buttonClick(ClickEvent event){
if (fileDownloader != null) {
// close previous stream
((StreamResource) fileDownloader.getFileDownloadResource()).getStreamSource().getStream().close();
fileDownloader.setFileDownloadResource(streamResource);
}
}
And It works :)). I am using Vaadin 8.
Although I don't like this solution it works. It has to do with how the download works and some restrictions by the browser. I'm sure there's a better solution but right now I simulate the first click with Javascript. If someone can find the correct answer then please post it and I'll change the selected answer, otherwise this is the only solution I found (posted in the Vaadin forums).
streamResource = createStreamResource();
downloadButton createDownloadButton();
downloadButton.setId("DownloadButtonID");
if(fileDownloader == null)
{
fileDownloader = new FileDownloader(streamResource);
fileDownloader.extend(downloadButton);
// Javascript click so that it works without a second click
Page.getCurrent().getJavaScript().execute(
"document.getElementById('DownloadButtonID').click();");
} else {
fileDownloader.setFileDownloadResource(streamResource);
}
The solution to the second click requirement for downloading the file is the button's listener. Like in my case, i have the click listener of button in which FileDownloader extends button. But it should be without listener only as FileDownloader has its own mechanism to handle the listener actions.
Here, the first button click gets handled by the clickListener and only in that, fileDownloader extends download button which holds all the functionality of downloading the file and this functionality occurs only when click event goes through the FileDownloader. so for the next time click goes through the FileDownloader as now its extending the button.
public static Button getDownloadButton(String fileName, String fileAsString, String caption) {
// caption
Button dwnButton = new Button(caption, VaadinIcons.DOWNLOAD);
dwnButton.addClickListener(listener -> {
StreamResource resource = createResource(fileName, fileAsString);
FileDownloader fileDownloader = new FileDownloader(resource);
fileDownloader.extend(dwnButton);
});
return dwnButton;
}
Here, fileDownloader already extending the button and it has all the stream resources. So on the first click only downloading gets invoked.
public static Button getDownloadButton(String fileName, String fileAsString, String caption) {
// caption
Button dwnButton = new Button(caption, VaadinIcons.DOWNLOAD);
StreamResource resource = createResource(fileName, fileAsString);
FileDownloader fileDownloader = new FileDownloader(resource);
fileDownloader.extend(dwnButton);
return dwnButton;
}

dynamically adding hyperlinks to rtf file in JEditorPane

I am currently building a word processor for use in a multi-window media annotation tool, written in Java. It is for film students to write essays and embed them with links to multimedia clips.
I want the user to be able to highlight text in an rtf document and create a link to a media file in the project. When clicked the link will display the media in its associated window.
I would like to know if it is possible to dynamically create hyperlinks in rtf documents in Java? As is possible in Word, for example.
At the moment I am using a JEditorPane with the Advanced RTF Editor Kit (http://java-sl.com/advanced_rtf_editor_kit.html). I am struggling to find any sort of a solution.
Any help or pointers greatly appreciated.
Thanks
Edit:
code, with parts 1 & 3 from # Eric's answer added
`item3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent e) {
//use FX thread to open FileChooser
Platform.runLater(new Runnable() {
#Override
public void run() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("create link");
String startDirectory = System.getProperty("user.home") + File.separator + "Pictures";
fileChooser.setInitialDirectory(new File(startDirectory));
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("JPEG files (*.jpg)", "*.jpg");
FileChooser.ExtensionFilter extFilter2 = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.png");
FileChooser.ExtensionFilter extFilter3 = new FileChooser.ExtensionFilter("JPG files (*.jpeg)", "*.jpeg");
fileChooser.getExtensionFilters().addAll(extFilter,extFilter2, extFilter3);
File imageFile = fileChooser.showOpenDialog(stage);
if(imageFile != null){
Image image = ImageViewerController.getImage();
try {
image = new Image(imageFile.toURI().toURL().toExternalForm().toString());
int start = textArea.getSelectionStart();
int end = textArea.getSelectionEnd();
textArea.getDocument().remove(start, end);
String newString = "{\field{\*\fldinst HYPERLINK 'http://www.google.com/'}{\fldrslt http://www.google.com}}";
textArea.getDocument().insertString(start, newString , null);
textArea.addHyperlinkListener(new HyperlinkListener() {
#Override
public void hyperlinkUpdate(HyperlinkEvent hle) {
if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
System.out.println(hle.getURL());
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(hle.getURL().toURI());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});`
I think there are various parts in your question:
1. Replace selected text in a Document:
Get the selected range with:
int start = editorpane.getSelectionStart();
int end = editorpane.getSelectionEnd();
Replace the text with:
editorpane.getDocument().remove(start,end);
editorpane.getDocument().insertString(start, newString, null);
Note: replace null with actual attribute set if needed.
2. Create a RTF-formatted hyperlink. I think this post has everything.
3. React to hyperlink clicks: As explained in the docs, you must add a HyperlinkListener to the editor pane to open the corresponding media. However a condition for this to work is that the editor kit generates HyperlinkEvents when hyperlinks are clicked. This is definitely the case for HTML documents, but since you are using a 3rd party library, I cannot confirm it will work the same way...

How to access jtextPane in a different form?

I am developing an application where, when I select a value(file) from list it should be opened in jTextPane of a different form. I am using two panels one is mainpanel where my list is shown and one is ExcelSheet, when i click on a list value then mainpanel is closed and new form ExcelSheet is displayed but not the content of doc file in jTextPane.
XWPFWordExtractor extractor=null;
File file=null;
String str=(String) list.getSelectedValue();
mainPanel.setVisible(false);
new ExcelSheet().setVisible(true);
ExcelSheet obj=new ExcelSheet();
try {
file=new File("C:\\Users\\Siddique Ansari\\Documents\\CV Parser\\"+str);
FileInputStream fis=new FileInputStream(file.getAbsolutePath());
XWPFDocument document=new XWPFDocument(fis);
extractor = new XWPFWordExtractor(document);
String fileData = extractor.getText();
Document doc = obj.jTextPane1.getDocument();
System.out.println(fileData);
doc.insertString(doc.getLength(), fileData, null);
}
catch(Exception exep){exep.printStackTrace();}
Use Action to encapsulate the code that updates the text pane in order to display a given file. You can invoke the action from a ListSelectionListener added to your JList. You can also use the action in a menu item or a toolbar button, as shown here. ImageApp is a related example.
For example, each instance of your action will need the target text pane and file:
class FileAction extends AbstractAction {
JTextPane target;
File file;
public FileAction(JTextPane target, File file) {
this.target = target;
this.file = file;
}
#Override
public void actionPerformed(ActionEvent e) {
// render file in target
}
}

JFileChooser not showing up

I have a method that takes a txt file as an input. I used to use string by typing the direct path to the file.
But it became burdensome whenever I tried to use different file for an input. I try implementing JFileChooser but with no luck.
This is the code, but nothing happening.
public static JFileChooser choose;
File directory = new File("B:\\");
choose = new JFileChooser(directory);
choose.setVisible(true);
File openFile = choose.getSelectedFile();
FileReader fR = new FileReader(openFile);
BufferedReader br = new BufferedReader(fR);
As per Java tutorial on How to Use File Choosers:
Bringing up a standard open dialog requires only two lines of code:
//Create a file chooser
final JFileChooser fc = new JFileChooser();
...
//In response to a button click:
int returnVal = fc.showOpenDialog(aComponent);
The argument to the showOpenDialog method specifies the parent
component for the dialog. The parent component affects the position of
the dialog and the frame that the dialog depends on.
Note as per docs it can also be:
int returnVal = fc.showOpenDialog(null);
If the parent is null, then the dialog depends on no visible window,
and it's placed in a look-and-feel-dependent position such as the
center of the screen.
 
Also have a read on Concurrency in Swing if you haven't already.
No blocking code (as David Kroukamp suggest). It solves "not showing up" problem.
Runnable r = new Runnable() {
#Override
public void run() {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if( jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION ){
selected = jfc.getSelectedFile();
}
}
}
SwingUtilities.invokeLater(r);
I personally found that the first dialog would show, but subsequent dialogs wouldn't show. I fixed it by reusing the same JFileChooser with this code.
JFileChooser jfc = new JFileChooser();
File jar = selectFile(jfc, "Select jar to append to");
File append = selectFile(jfc, "Select file to append");
//When done, remove the window
jfc.setVisible(false);
public static File selectFile(JFileChooser jfc, String msg) {
if (!jfc.isVisible()) {
jfc.setVisible(true);
jfc.requestFocus();
}
int returncode = jfc.showDialog(null, msg);
if (returncode == JFileChooser.APPROVE_OPTION) return jfc.getSelectedFile();
return null;
}
For JFileChoosers, you're supposed to call objectName.showOpenDialog(Component parent) or objectName.showOpenDialog(Component parent). These methods will return an integer, which you can use to compare to the static constants set in JFileChooser to determine whether the user clicked cancel or open/save. You then use getSelectedFile() to retrieve the file that the user has selected.
Ex (There might be small errors):
class Example {
public static void main(String[] args) {
JFileChooser jfc = new JFileChooser();
File selected;
if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
selected = jfc.getSelectedFile();
}
}
}
The Java API is a great resource for figuring out what objects can do what, and how. Here's the page for JFileChoosers
The API pages are usually found when you Google the object name. They're usually the first ones that come up as a result as well.

How to fetch path of a file from preference page and print the Output on console via Button on Workbench?

I have made one preference page whose programming is:
public class SAML
extends FieldEditorPreferencePage
implements IWorkbenchPreferencePage {
public SAML() {
super(GRID);
setPreferenceStore(RmpPlugin.getDefault().getPreferenceStore());
setDescription("Browse Appropriate files");
}
public FileFieldEditor f;
public FileFieldEditor f1;
public void createFieldEditors() {
f=new FileFieldEditor(PreferenceConstants.P_PATH,
"&Prism.bat File:", getFieldEditorParent());
addField(f);
f1=new FileFieldEditor(PreferenceConstants.P_PATH1,
"&NuSMV Application File:", getFieldEditorParent());
addField(f1);
}
I want to get path of FileFieldEditor f and want this path to run on a button which is embedded on workbench (but programming of that button is in different project on the same workspace).
The button programming which has hard coded path of "prism.bat" file is:
try {
//to clear the console on every click of button
IViewPart view = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(IConsoleConstants.ID_CONSOLE_VIEW);
if (view != null) {
(myConsole).clearConsole();
}
ProcessBuilder pb=new ProcessBuilder("C:\\Program Files\\prism-4.0\\bin\\prism.bat");
pb.directory(new File("C:\\Program Files\\prism-4.0\\bin"));
Process p=pb.start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String in;
while((in = input.readLine()) != null) {
out.println(in);
}
int exitVal=p.waitFor();
if(exitVal==0)
{
out.println("Process Successful");
out.println("Printing on console with Exitvalue =0");
}
else
{out.println("Process failed");
out.println("Exitvalue = 1");
}
}
catch (Exception e)
{
out.println(e.toString());
e.printStackTrace();
}
But I want to fetch file from my preference page FileFieldEditor f and want this path to embed in button programming so that when button is pressed, result is shown.
You need two parts:
Code that initialize the default for the preference
Code that use the current value
To set the default, you use the following code in the Activator:
public class Activator extends AbstractUIPlugin {
#Override
public void start(BundleContext context) throws Exception {
super.start(context);
IPreferenceStore ps = getPreferenceStore();
ps.setDefault(SHOW_IMAGE, true);
}
public static final String SHOW_IMAGE = "showImage";
}
Alternatively, you can use the org.eclipse.core.runtime.preferences extension point...
Note that the code above assume that the type of the preference is Boolean - there are other methods for numbers, strings, etc... A file name is a string.
To use the current value, just use
if (Activator.getDefault().getPreferenceStore().getBoolean(Activator.SHOW_IMAGE)) {
…
}
The following slides contains a little more information...

Categories

Resources