How to add help file to frame? - java

I'm making plugin for eclipse which opens frame with some table's when plugin command is activated. Now I want to add help file to plugin's frame, so that when clicked on help file's link in frame, file opens (executes). File is suppose to be part of plugin. My problems are:
Don't know how to make link and add it to frame.
Don't know how to locate that file in plugin from run time application.
JLabel lblFileLink = new JLabel("Help");
lblFileLink.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
lblFileLink.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
try {
/* Add code for opening file from plugin.*/
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
Found this code somewhere, now I need to implement link, any thoughts?

If i understand you question correct, something like this should work:
JLabel lblFileLink = new JLabel("Help");
lblFileLink.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
lblFileLink.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
try {
java.awt.Desktop.getDesktop().edit(INSERTYOURFILEHERE);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
This will open the standard text editor and show your file. Just replace INSERTYOURFILEHERE with your own text file.
Edit: If you want to open it in Eclipse maybe look at this
Edit2: The gist of the link above:
File fileToOpen = new File("externalfile.xml");
if (fileToOpen.exists() && fileToOpen.isFile()) {
IFileStore fileStore = EFS.getLocalFileSystem().getStore(fileToOpen.toURI());
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
IDE.openEditorOnFileStore( page, fileStore );
} catch ( PartInitException e ) {
//Put your exception handler here if you wish to
}
} else {
//Do something if the file does not exist
}

Related

Java not opening PDF

Attempting to have a button in my Java GUI open a PDF. My PDF is in the src folder, the same folder where my code is but I get a 'file not found' error and am unsure what is wrong with my pathing.
aboutButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (Desktop.isDesktopSupported()) {
try {
File aboutPDF = new File("./aboutGUI.pdf");
Desktop.getDesktop().open(aboutPDF);
} catch (Exception ex) {
System.out.println(ex);
}
}
}
});
You'll have to put it in your resources folder as that is where your IDE looks by default for files unless specified otherwise. Then do "filename.pdf" or "src/main/resources/filename.pdf"

Check and open downloaded file in Selenium?

I must check downloaded PDF and open it using Selenium. For that, I am using Robot class. This is not the permanent or we can say general solution of this.
Question : Can anyone please help and provide more reliable solution for the same ?
Please find below code:
public boolean CommonEvents(WebDriver driver) throws InterruptedException {
try {
Thread.sleep(2000);
Robot robot = new Robot();
robot.mouseMove(100, 700);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(10000);
} catch(Exception e) {
BaseTest.reportPass(driver, null, "Should click on PDF to open", "Failed to click on PDF to open");
}
}
Just in case you really need to open every downloaded PDF then I would simply add this line to your preferences (e.g. for Firefox):
ffprofile.setPreference("browser.helperApps.neverAsk.openFile", "application/pdf");
Then it will automatically open the downloaded file after finishing the download.
You can use something similar to this if you have a fixed directory where you are saving the downloaded pdf files .
public static void main(String[] args) {
try {
File pdfFile = new File("c:\\Hello.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 doesn't exists.");
}
System.out.println("File opened.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
Reference : https://docs.oracle.com/javase/6/docs/api/java/awt/Desktop.html
We use below logic:
First set preference for a download location, so that the file will be downloaded to your desired location.
chromePrefs.put("download.default_directory", downloadFilepath);
Set Preference so that it won't ask the pop to download.
chromePrefs.put("profile.default_content_settings.popups", 0);
As #Mudit_ has answered check for the *.pdf with regex pattern, if you want to make it dynamic.

Java how do I save a background color once my application has been launched again

I'm trying to create a button in game where the background color will go from light_gray to dark_gray. However when the application relaunches I have to re select the button to get the color back to dark_gray.
How would I have it so that it saves the color when the application is relaunched?
My code is very simple and is just an action listener on the button which then changes the bg color of selected items.
Ok, I have now had the chance to allow it to create the properties file but one doesn't know how one could store the data. I've seen people have stuff such as 'properties.setProperty("Favorite sport", "Football");'
But how could one have this so that it stores the bg color?
windowDark.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
try {
Properties properties = new Properties();
properties.setProperty();
File file = new File("DarkTheme.properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "Dark theme background colour");
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
The java.util.prefs Preferences API is well suited for storing persistent preference data for user applications running on the desktop.
Here's an example how you can use it to store and retrieve persistent background color settings:
import java.awt.Color;
import java.util.prefs.Preferences;
public class MyPrefs {
private static Preferences preferences =
Preferences.userRoot().node("myappname.ColorPreferences");
public static void storeBackground(Color background) {
preferences.putInt("background", background.getRGB());
}
public static Color retrieveBackground() {
// Second argument is the default when the setting has not been stored yet
int color = preferences.getInt("background", Color.WHITE.getRGB());
return new Color(color);
}
}
To call it, use something like:
public static void main(String[] args) {
System.out.println("Background: " + retrieveBackground());
storeBackground(Color.RED);
}
You can store the color as an int value in the properties file, as follows:
windowDark.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
getProperties().setProperty("color", Integer.toString(getColor().getRGB()));
}
});
Have the properties as a member of the window this button is in, or even better, in some general location of the application (the class with the main() perhaps ?), and access it with getProperties().
When you need to use the color, parse the string:
Color color = new Color(Integer.parseInt(getProperties().getProperty("color")));
Don't save the properties file on each button click, instead, do so when the application is about to exit:
mainWindow.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
mainWindow.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
File file = new File("DarkTheme.properties");
FileOutputStream fileOut = new FileOutputStream(file);
getProperties().store(fileOut, "Dark theme background colour");
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
mainWindow.dispose();
}
}
});
The change done in memory will be disposed once the application is terminated. If you want to persist some data (in this case, the background color), then you need to store is somewhere, e.g. file, database, etc.
For a simple application, storing your data in a file will be practical.
To do this, you will need to:
- when application starts, read the file, and apply the color specified in the file
- while the application is running and user changes the color, save the color to the same file
To deal with file, you will need to use File, FileReader, and FileWriter classes (all are in java.io package).

How to add a MultiPageEditor into a MultiPageEditor as a sub editor?

When I tried to use addPage() to put a customized multipage editor into the Multipage editor, I found that the customized multipage editor won't display. Is there anyone who encountered the same problem and if there is any solution?
the codes are as follow
private void createMultiPage() {
try {
multiPageEditor = new CustomizedMultiPageEditor();
int index = addPage(multiPageEditor, getEditorInput());
setPageText(index, "Customized MultiPage");
} catch (Exception e) {
System.out.print("opps!!!");
}
}

How to refresh XML in Jtree

I read here , but if the xml file changes the jtree does not reload /refreshes
how to create a function for refresh / reload Jtree
I try to write code :
refreshAction = new AbstractAction("Refresh", IconFactory.getIcon("delete", IconFactory.IconSize.SIZE_16X16)) {
public void actionPerformed(ActionEvent e) {
XMLTree xmlClass = null;
((DefaultTreeModel) xmlClass.getModel()).reload();
System.out.println("Refresh");
}};
but i got the error : java.lang.NullPointerException
I added a new Action to popup in getJPopupForExplorerTree(). You'll probably want to re-factor xmlFile out of the XMLTree constructor; I've hard coded it for expedience below:
popup.add(new AbstractAction("Reload") {
public void actionPerformed(ActionEvent e) {
System.out.println("Reload");
try {
root = getRoot("xml.xml");
setModel(new XMLTreeModel(root));
} catch (Exception ex) {
ex.printStackTrace(System.err);
}
}
});
this is most complex code, probably
read tutorial about JTables DefaultTableModel (good described concept and logics for DefaultXxxModel is similair / the same)
read tutorial about JTree
read tutorial about Concurency in Swing,
especially description about SwingWorker
in your case better (sorry for that) would be create an new instance for DefaultTreeModel, fills data by using SwingWorker, add new model to the visible JTree,
by replacing model you'll lost all changes in the current JTree
I dont know the spesific code but you can try this
refreshAction = new AbstractAction("Refresh", IconFactory.getIcon("delete", IconFactory.IconSize.SIZE_16X16)) {
public void actionPerformed(ActionEvent e) {
DefaultTreeModel myTreeModel = (DefaultTreeModel) xmlClass.getModel();
myTreeModel.reload();
revalidate();
repaint();
}};

Categories

Resources