I need to print anchorPane in scenebuilder javafx - javafx-8

I'm creating customized TableViews and then i need to print them the way they look exactly, so i thought about taking like a screenshot for the AnchorPane itself then printing it, so is that possible anyways ? or what should i do exactly ?

You can place all the structures you need to print in a node for example anchorpane and then pass the anchorPane to this code,
FileChooser exportFileChooser = new FileChooser(); //The folder selector to save the pdf
exportFileChooser.setInitialDirectory(FileSystemView.getFileSystemView().getDefaultDirectory());
exportFileChooser.setInitialFileName(fileName);
File file = new File("temp/"+fileName+".png");
exportFileChooser.showSaveDialog(owner);
file.getParentFile().mkdirs();//crete temp directory if not available
final SnapshotParameters spa = new SnapshotParameters();
spa.setTransform(javafx.scene.transform.Transform.scale(0.71, 1.1));//Scaling only if required i.e., the screenshot is unable to fit in the page
WritableImage image = node.snapshot(spa, null);//This part takes the snapshot, here node is the anchorpane you sent
BufferedImage buffImage = SwingFXUtils.fromFXImage(image, null);
try {
ImageIO.write(buffImage, "png", file);//Writes the snapshot to file
} catch (Exception e) {
e.printStackTrace();
}

Related

How to show more than one image using active reports, Which control I have to use. I will bind images dynamically

I need to display multiple images dynamically, Please help me which control I have to use for this, and how to bind the images dnamically
You can use Picture Box control. for displaying Images dynamically you can write the following code in code behind
public Image GetImageObject(string imagename)
{
try
{
string _imagepath = D:\reports\Source\xyz\Images\+ imagename;
if (File.Exists( _imagepath)
{
return System.Drawing.Image.FromFile(_imagepath);
}
else
{
return null;
}
}
catch (Exception ex)
{
throw ex;
}
}
and then just create a object to your report, for Example your report Name is rptExample, suppose from BLL method,
rptExample orptExample=new rptExample();
orptExample.Logo=GetImageObject("Example.png");
orptExample.Run();
Hope this helps you.

Save JavaFX ScrollPane content to PDF file

I'm using the below code to save the content of a ScrollPane in my JavaFx Application to a PDF file.
button.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
File pdfFile = fileChooser.showSaveDialog(primaryStage);
try {
BufferedImage bufImage = SwingFXUtils.fromFXImage(scrollPane.snapshot(new SnapshotParameters(), null), null);
FileOutputStream out = new FileOutputStream(new File("../temp.jpg"));
javax.imageio.ImageIO.write(bufImage, "jpg", out);
out.flush();
out.close();
com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance("../temp.jpg");
Document doc = new Document(new com.itextpdf.text.Rectangle(image.getScaledWidth(), image.getScaledHeight()));
FileOutputStream fos = new FileOutputStream(pdfFile);
PdfWriter.getInstance(doc, fos);
doc.open();
doc.newPage();
image.setAbsolutePosition(0, 0);
doc.add(image);
fos.flush();
doc.close();
fos.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
In the scrollpane, I have a very long VBox which contains almost 40-50 labels. So, this code initially saves it to a jpg file and then adds it to a pdf file.
When the temp.jpg is created initially, due to its length, the jpg file looks very thin. It should be zoomed to see the actual content.
When the pdf file is written, it was blank except that it was lengthy as it would have been when the jpg is really converted to a PDF file.
Can anyone help me fix this ? to take the snapshot of the ScrollPane to PDF with its actual size/scale ?
I have first scaled the image and then created document with scaled PageSize. This fixed the issue
com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance("../temp.jpg");
image.scalePercent(1);
Document doc = new Document(new com.itextpdf.text.Rectangle(image.getScaledWidth(), image.getScaledHeight()));
doc.open();
doc.add(image);

Issue while opening Marker in an editor programatically

I am trying to open a marker, while double-clicking on an entry from a TableViewer, inside an eclipse plug-in. I am able to get the associated resource from the marker, however nothing is happening while the openEditor method is executed.
The code is as below:
viewer.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
try {
IStructuredSelection sel = (IStructuredSelection) event.getSelection();
ReviewIssue reviewIssue = (ReviewIssue) sel.getFirstElement();
if(reviewIssue != null){
MessageDialog.openError(window.getShell(), "Insta Review", reviewIssue.getMarker().getResource());
try {
IDE.openEditor(window.getActivePage(), reviewIssue.getMarker(), true);
} catch (PartInitException e) {
MessageDialog.openError(window.getShell(), "Insta Review", e.getMessage());
}
}
} catch (Exception e) {
MessageDialog.openError(window.getShell(), "Insta Review", e.getMessage());
}
}
});
Please let me know, if am missing something here. Thanks in advance.
Also ignore the message dialogs, as I plan to implement the logging functionality later.
UPDATE:
Even though I created the marker on IFile, I was getting the same behaviour. I was finally able to open the editor by using the IFile, instead of the marker.
IFile iFile = markerProject.getFile(path);
//IMarker marker = iFile.createMarker("id.myMarker");
.....
IDE.openEditor(window.getActivePage(), reviewIssue.getiFile(), true);
//IDE.openEditor(window.getActivePage(), reviewIssue.getMarker()), true);
For this to work the IMarker.getResource() method must return an IFile. The code in IDE.openEditor is:
// get the marker resource file
if (!(marker.getResource() instanceof IFile)) {
IDEWorkbenchPlugin
.log("Open editor on marker failed; marker resource not an IFile"); //$NON-NLS-1$
return null;
}
so look in the .log file in the workspace .metadata directory to see if you are getting that log message.
Normally you would create a marker for a file using the IFile.createMarker method (createMarker is actually an IResource method).

Listview display wrong image at wrong place when loading thumbnail image from web

I am using List view for displaying android files i.e. showing Folder and files from server.
When i start my activity, i can see the list on the screen, just as i want it.
and I am getting data from server.
I want to show structure like
when there is image file need to show image thumbnail (I have thumbnail URL which is getting from server by default imageloader displaying default image icon until thumbnail is not available).
when there is other file type like audio,video..... or file need to show that icon.
and when I am showing list it shows proper icon for related files.
But the moment i try to start scrolling the and in background my thumbnail is loading from server and when from server I am getting image from server and showing that bitmap thumbnail it will displaying thumbnail bitmap in other file type like folder or music file icon due to view reusing of listview.
this misplace is happen when getting image from server.
any one can please suggest me to how to overcome this problem.
below is my getview() method of my listview.
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder = new ViewHolder();
if(convertView == null){
convertView = _inflate.inflate(R.layout.layout_list, null);
viewHolder.text = (TextView) convertView.findViewById(R.id.title);
viewHolder.owner = (TextView) convertView.findViewById(R.id.owner);
viewHolder.image = (ImageView) convertView.findViewById(R.id.thumb);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> item = (HashMap<String, String>) getItem(position);
viewHolder.text.setText( item.get("poiName").toString() );
viewHolder.owner.setText( item.get("owner").toString() );
ImageView imageView = viewHolder.image;
imageView.setTag(item.get("thumbs"));
//Is type of file is picture then display thumbnail by using imagloader class
if (genericDAO.sub_content_type.equalsIgnoreCase(Constants.TYPE_PICTURE))
{
String url = getTumbnalURL();
viewHolder.image.setTag(genericDAO.id);
viewHolder.image.setScaleType(ScaleType.CENTER_CROP);
mImageLoader.DisplayImage(id, url, mActivity,viewHolder.image);
}
else
{
//Display related file icon
viewHolder.image.setImageResource(FileUtil.getImageRelatedToFileType(mcontext, sub_ext));
}
return convertView;
}
although this code is insufficient. What I understood from this code is, you are using lazy loading for images, and mImageLoader must be running thread, which is setting image to viewHolder.
you can set imageInfo as tag in else part also.
....
else
{
//Display related file icon
viewHolder.image.setImageResource(FileUtil.getImageRelatedToFileType(mcontext, sub_ext));
viewHolder.image.setTag(genericDAO.id);
}
Inside ImageLoader, when new image is downloaded, just before you set new image to viewHolder please check for correct tag.

Eclipse RCP ListProvider tweaking

I have problem with changing this image list provider in to thumbnail provider. In case of need I will post View for it too.
public Object[] getElements(Object inputElement) {
if (iDirname == null)
return null;
File dir = new File(iDirname);
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File directory, String filename) {
if (filename.endsWith("jpg") || (filename.endsWith("bmp")) || (filename.endsWith("png") || (filename.endsWith("JPG") || (filename.endsWith("BMP")) || (filename.endsWith("PNG")))))
return true;
else
return false;
}
};
String[] dirList = null;
if (dir.isDirectory()) {
dirList = dir.list(filter);
for (int i=0; i<dirList.length;++i){
//dirList2[i] = new Image(device, dirList2[i]); added this to try passing array of Images - failed.
dirList[i] = iDirname + File.separatorChar + dirList[i];
}
}
return dirList;
}
And the view
public void createPartControl(Composite parent) {
iViewer = new ListViewer(parent);
iViewer.setContentProvider(new DirListProvider());
getSite().setSelectionProvider(iViewer);
makeActions();
hookContextMenu();
contributeToActionBars();
}
I don't know how to change provided path lists to the thumbnail displaying. Should I get the provided content in to Array and iterate through it creating Images? If so how?
Thanks in advance for your help.
EDIT:
I added
ImageDescriptor[] dirList = null;
if (dir.isDirectory()) {
String[] dirList2 = dir.list(filter);
for (int i=0; i<dirList2.length;++i){
dirList[i] = ImageDescriptor.createFromImageData(new ImageData(iDirname + File.separatorChar + dirList2[i]));
//dirList[i] = iDirname + File.separatorChar + dirList[i];
}
}
return dirList;
but this is not showing anything at all.
When you are telling me to use Composite, is it my parent variable? I still don't know how to display the images from paths passed by ListProvider. I am really green in this :/
What you are missing here is a LabelProvider. You can use a LabelProvider to provide an image for each element in your viewer's input.
However, Francis Upton is right, I don't think ListViewer will really suit your needs as you will end up with a single column of images. Although you won't be able to add the images directly to your Composite, you will need to set them as the background image of a label.
There are a couple of other things to consider:
You need to dispose() of your Images once you're done with them as they use up System handles. Therefore you need to keep track of the Images you create in your getElements(Object) method.
If the directories you are reading the images from do not already contain thumbnails, you will need to scale the images before presenting them on your UI.
Remember, the array type you return from your ContentProvider's getElements(Object) method defines the type that will get passed into your LabelProvider's methods. So you started off returning an array of strings representing paths to the images. Your LabelProvider would need to load these into images to be returned from the provider's getImage method - but bear in mind what I said about disposing of these images! Then you switched to returning an Array of image descriptors, in this case you would need to cast your incoming Object to an ImageDescriptor and use that to create the Image in the getImage method. Maybe once you have this working you can think about whether this meets your needs, and then possibly look at doing a different implementation, such as the composite/gridlayout/label approach.
I would not use a ListViewer for this. I would just create a Composite and then using GridLayout set up the number of columns you want and margins and so forth, and then just add the images directly to the composite. As far as I know you cannot put arbitrary things like imagines in an SWT List, so the ListViewer is not going to help you. You can do all of this in the createPartControl method.