I have 4 jButtons that execute each specific program (Browsers for the time being), I need to make a conditional that tells a user that if a path to that specific program doesn't exist it will open a webpage with that specific browser in mind.
This piece of code repeats itself and I need to optimise it. I tried making another method where I would use if statement but instead it just gave me a lot of issues while executing each button.
Warning_MSG(); is just another method with jOptionPane to confirm if a user wants to open an external application, etc etc.
private void chromeMouseClicked(java.awt.event.MouseEvent evt) {
File file = new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
if(!file.exists()){
try {
URI url = new URI("https://google.com/chrome");
java.awt.Desktop.getDesktop().browse(url);
System.err.println("File Error, Google Chrome is not found!\n");
} catch (IOException | URISyntaxException e) {
System.err.println("Unknown Error, Exception found\n" e);
}
}else {
Warning_MSG(file);
}
}
private void firefoxMouseClicked(java.awt.event.MouseEvent evt) {
File file = new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
if(!file.exists()){
try {
System.err.println("File Error, Mozilla Firefox is not found!\n");
URI url = new URI("https://www.mozilla.org/en-GB/firefox/download/thanks/");
java.awt.Desktop.getDesktop().browse(url);
} catch (IOException | URISyntaxException e) {
System.err.println("Unknown Error, Exception found\n" e);
}
}else {
Warning_MSG(file);
}
}
private void ieMouseClicked(java.awt.event.MouseEvent evt) {
File file = new File("C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe");
if(!file.exists()){
System.err.println("File Error, iExplorer is not found! \n");
}else {
Warning_MSG(file);
}
}
private void edgeMouseClicked(java.awt.event.MouseEvent evt) {
File file = new File("C:\\Windows\\SystemApps\\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\\MicrosoftEdge.exe");
if(!file.exists()){
System.err.println("File Error, Microsoft Edge is not Found! \n");
}else {
Warning_MSG(file);
}
}
The question is, how can I optimise these pieces of code so that I don't have to repeat the same thing all over again with other programs but instead run certain things only when conditions are met?
CodePudding user response:
You can create an enum with 2 parameters (exe path, download page)
enum Browser
{
Chrome("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "https://google.com/chrome"),
Firefox("C:\\Program Files\\Mozilla Firefox\\firefox.exe", "https://www.mozilla.org/en-GB/firefox/download/thanks/"),
IExplorer("C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe", "https://google.com/internet-explorer"),
Edge("C:\\Windows\\SystemApps\\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\\MicrosoftEdge.exe", "https://google.com/download-edge");
private final String _filePath, _downloadPageURL;
Browser(String filePath, String downloadPage)
{
_filePath = filePath;
_downloadPageURL = downloadPage;
}
private String getDownloadPageURL()
{
return _downloadPageURL;
}
private String getFilePath()
{
return _filePath;
}
public boolean exists()
{
return new File(getFilePath()).exists();
}
public void openDownloadPage()
{
try
{
Desktop.getDesktop().browse(new URI(getDownloadPageURL()));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Then, all you have to do is a simple check at your methods.
private void chromeMouseClicked(java.awt.event.MouseEvent evt)
{
final Browser chrome = Browser.Chrome;
if (!chrome.exists())
{
chrome.openDownloadPage();
System.err.println("File Error, Google Chrome is not found!\n");
return;
}
// The browser exists at this line. So create your code
// ...
}