I have a desktop bassed eclipse plugin application. In which, for a case I am opening html report file in browser.
for which I am using this:
try {
browser = new Browser(shell, SWT.NONE);
browser
.setUrl(path);
shell.open();
}
catch (SWTError e) {
e.printStacktrace();
}
But in one case I am having html file of 90 MB which is not able to get open by it. It got hanged.
So request you for any action around it need to be done.
On any other optimized way to open it.
Instead of loading the whole file, load it on demand.
Window onscroll event is your friend.
Related
I need to know what is the best way to export excel file into my .jar code.
I used GetDesktop open file statement, and that works good when I run it inside of Eclipse IDE. But when I export my code into a .JAR, the excel sheet will not open, it will not do anything.
public void actionPerformed(ActionEvent e) {
try { Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
desktop.open(new File("Resources/pics/chart 2.xls"));
} catch (IOException ioe) {
ioe.printStackTrace();
}}
I have a picture image in a jframe, and I have 2 buttons, inside of the picture, for the user to click on. Depends on which button they click on, I need that certain excel file to open. Just for viewing purposes, no editing is involved. I tried using apache POI, but the excel file did not open at all, but when I use the code above at least it opens in eclipse. I see that there is others with the same problem, but mine I need it to just open excel not edit.
thanks
You must copy it out of the jar. You can get an inputstream using InputStream in = ClassYouAreIn.class.getResource("someFile.xls"). Here is an example:
try (InputStream in = ClassYouAreIn.class.getResource("someFile.xls")) {
// copy it here, for example using:
Files.copy(in, Paths.get("somefile.xls"), StandardCopyOption.REPLACE_EXISTING);
}
NB: Your catch code is really bad: exceptions contain 4 interesting bits of information (type, message, stacktrace, causal chain) and you're chucking 3 of the 4 things, AND you are causing a cascade of errors by not aborting. You should always just throw them onwards (put throws IOException on this method) unless you have an ACTUAL strategy to deal with it (and logging / printing it is not dealing with it!).
There is an already opened EXcel file, but is minimized. I want to maximize that Excel sheet. I have been using open() method of Desktop Class, but sheet is not getting maximized.
It opens file in maximized mode by default XLS reading software (office).
Desktop dt = Desktop.getDesktop();
try {
dt.open(new File("c:\\fileName.xls"));
} catch (IOException e) {
e.printStackTrace();
}
First you maximize your XLS reading software (for e.g. office) window manually and close again and then run your program. It will open in maximized mode only.
I am working with .pdf files that are available on my companies' website only. I am not aware of any way to download them and store in one folder.
The link that I click to get the .pdf file has the following source code:
<a href="javascript:propertiesView('documentName')">
As I click on the link, a .pdf file pops up in a new browser window with no url and no source code. I presume that there is no way to manipulate that .pdf directly, then how can I save it then in order to manipulate the .pdfs from a folder?
Thank You
You may have luck by simply telling your browser to always save PDF files to disk (credits to Dirk):
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
If that doesn't work, you are probably able to iterate through all open windows/tabs by using the switchTo() methods. Try something like this to get some insight about your opened windows (credits to Prashant Shukla):
public void getWindows() {
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
driver.switchTo().window(window);
System.out.println(driver.getTitle());
}
}
A non-selenium solution to download the file would be to use the apache-commons library (creadits to Shengyuan Lu):
org.apache.commons.io.FileUtils.copyURLToFile(URL, File)
But this would require that you know the URL of the window, which you probably are able to fetch with the second approach i mentioned (driver.switchTo()) and driver.getCurrentUrl().
I presume that there is no way to manipulate that .pdf directly
That's correct. With Selenium, you cannot.
how can I save it then in order to manipulate the .pdfs from a folder?
I've actually implemented this exact thing in our regression system where I work.
My first step, was to construct a Url based on what the propertiesView(). method did.
in your case, propertiesView() does some sort of window.open is my guess. So your goal is, to extract that Url that it opens, and use concatenation to construct the url.
Once you've found your Url, the rest is a cakewalk. Just download the url to a folder named /pdfs. See this question for how to do that.
It may even require calling that method to figure it out.. Due to my ignorance of your System Under Test, it's difficult for me to give you a code answer, unless you posted it.
A hint that i'll tell you, is if you are using Selenium 1, use
String url =selenium.getEval("var url = something; url;");
to fetch the url and get it into a java object.
(If using selenium 2, use the JavaScriptExecutor#executeScript)
If you want to save a PDF to your hard drive in IE with selenium, you need to use pywinauto with selenium. I just used this code for PDF files that open up in the browser.
//selenium imports
from pywinauto import application //pywinauto import
//write selenium code to open up pdf in the browser
driver = webdriver.Ie("IEDriverServer.exe", capabilities = caps)
//this could be a get or driver.execute_script() to click a link
driver.get("link to pdf")
//save pdf
app = application.Application()
//get the ie window by the title of the application (assuming only one window is open here)
ie = app.window_(title_re = ".*Internet Explorer.*")
//this line focuses on the pdf that is open in the browser
static = ie.Static
//focus on the pdf so we can access the internal controls
static.SetFocus()
//control + h shows the pdf bar, but you don't really need this step
//for it to work. i just used it as a debug
static.TypeKeys("^H")
//open save file dialog
static.TypeKeys("+^S")
//tricky here because the save file dialog opens up as another app instance
//which is how pywinauto sees it
app2 = application.Application()
//bind to the window by title - name of the dialog
save = app2.window_(title_re = ".*Save As.*")
//this is the name of the property where you type in the filename
//way to be undescriptive microsoft
file_name = save[u'FloatNotifySink']
//type in the file name
save.TypeKeys("hello")
//pause for a second - you don't have to do this
time.sleep(4)
//find and bind the save button
button = save[u'&SaveButton']
//click the save button
button.Click()
I have Java code to open the pdf file in Java and it is running perfectly.
Code is
try {
File pdfFile = new File("/Users/alkandari/Desktop/SMART/Fahim/test.pdf");
if (pdfFile.exists()) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(pdfFile);
} else {
System.out.println("Awt Desktop is not supported!");
}
} else {
System.out.println("File is not exists!");
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
However when I put this in some method say showMyPDFFile() and try to call that on commandLink, pdf doesn't get open. It says Awt Desktop is not supported!.
<h:commandLink value="View PDF/ DOC"
action="#{PersonalInformationDataBean.showMyPDFFile()}" />
Any idea why pdf is not getting opened?
So just to check, you're writing a web application, right?
The Desktop class refers to the local desktop - so it's for local GUI client applications. Even if it were supported, you'd be opening the PDF on the server's desktop - not the client's desktop, which I assume is what you're after.
It sounds like you should basically just be linking straight to the PDF file (as URL). You'll need to make the PDF available via your web server, of course.
I'm writing an applet in eclipse and under the eclipse environment it works well.
while creating a jar file from this project, the problems start.
After testing the jar with several options, I think the problem is with loading an image from a web page.
Any Other features from the applet seems to work ok in the jar.
The code of loading image in my project looks like that:
MediaTracker mt = new MediaTracker(this);
String photo = imagePath
URL base = null;
try {
base = getDocumentBase();
}
catch (Exception e) {
}
if(base == null){
System.out.println("ERROR LOADING IMAGE");
}
Image imageBase = getImage(base,photo);
// Some code that works on the image (not relevant)
// The rest of the code
icon.setImage(image);
imageLabel.setIcon(icon);
But the jar can not load the imgae and it doesn't disply it in while running and the applet is stuck because of that. (unlike in the eclipse, which loads the image and shows it)
What could be the problem?
A second problem is that from the applet in the eclipse the loading take few seconds. Is there a way to speed things up?
Thanks for any help,
I have no idea how this could be working in Eclipse.
The problem is that getDocumentBase() returns location of a page, in which the applet is embedded (e.g. http://some.site.com/index.html), and you are trying to load a picture from that location. Obviously, there is no picture, just an html (or php) file, and the loading fails.
If your goal is to load an image from inside the jar, try:
Image img = null;
try {
img = ImageIO.read(getClass().getResource("/images/tree.png"));
} catch (IOException ex) {
System.err.println("Picture loading failed!");
}
where "/images/tree.png" is path to image file in your source tree.
EDIT: If you need just to load an image from URL, you can use:
Image img = null;
try {
img = ImageIO.read(new URL("http://some.site.com/images/tree.png"));
} catch (IOException ex) {
System.err.println("Picture loading failed!");
}
This method is a bit better than Applet.getImage(new URL(...)) - I had some problems when loading many images.