Skip to content

Commit

Permalink
The Plugin Help invocation was moved to Mediator as a static method. …
Browse files Browse the repository at this point in the history
…This allows us to use the help functionality in the plug-in dialogs.

See the ZTF observation source as an example.
  • Loading branch information
mpyat2 committed Nov 7, 2023
1 parent d8410e8 commit e96b6da
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@
import org.aavso.tools.vstar.ui.dialog.MessageBox;
import org.aavso.tools.vstar.ui.dialog.TextArea;
import org.aavso.tools.vstar.ui.dialog.TextField;
import org.aavso.tools.vstar.ui.mediator.Mediator;
import org.aavso.tools.vstar.ui.mediator.StarInfo;
import org.aavso.tools.vstar.util.Pair;
import org.aavso.tools.vstar.util.locale.LocaleProps;
import org.aavso.tools.vstar.util.locale.NumberParser;

/**
Expand Down Expand Up @@ -195,7 +197,37 @@ public void stateChanged(ChangeEvent e) {

this.pack();
}


@Override
protected JPanel createButtonPane() {
JPanel panel = new JPanel();

// to-do: localize
JButton helpButton = new JButton("Help");
helpButton.addActionListener(createHelpButtonListener());
panel.add(helpButton);

JButton cancelButton = new JButton(LocaleProps.get("CANCEL_BUTTON"));
cancelButton.addActionListener(createCancelButtonListener());
panel.add(cancelButton);

okButton = new JButton(LocaleProps.get("OK_BUTTON"));
okButton.addActionListener(createOKButtonListener());
panel.add(okButton);

this.getRootPane().setDefaultButton(okButton);

return panel;
}

private ActionListener createHelpButtonListener() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
Mediator.openPluginHelp(getDocName());
}
};
}

private void searchParamPaneUpdateFocus() {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,47 +475,17 @@ public void run() {
private ActionListener createHelpButtonListener() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
String urlStr = "https://github.com/AAVSO/VStar/tree/master/plugin/doc/";
String plugin_doc_name = null;
if (!allCheckBox.isSelected()) {
int index = pluginList.getSelectedIndex();
String description = (String)(pluginListModel.get(index));
String plugin_doc_name = manager.getPluginDocName(description);
if (plugin_doc_name != null) {
// plugin_doc_name can be a file name (without path) resided in the base plug-in doc directory.
// In this case it may contain spaces and other special characters.
// Or it can be a document URL, in this case, spaces and special characters must be properly encoded (i.e. %20 instead of space).
// First, we try to convert the string into URI.
// If the conversion was successful, the string is presumably a full document path.
// If not, consider it as a file name.
URI uri = getURIfromStringSafe(plugin_doc_name);
if (uri != null) {
urlStr = uri.toString();
}
else {
try {
plugin_doc_name = URLEncoder.encode(plugin_doc_name, "UTF-8").replace("+", "%20");
} catch (UnsupportedEncodingException ex) {
plugin_doc_name = "";
}
urlStr = urlStr += plugin_doc_name;
}
}
plugin_doc_name = manager.getPluginDocName(description);
}
Mediator.openHelpURLInWebBrowser(urlStr);
Mediator.openPluginHelp(plugin_doc_name);
}
};
}

private URI getURIfromStringSafe(String s) {
try {
return (new URL(s)).toURI();
} catch (MalformedURLException ex) {
return null;
} catch (URISyntaxException ex) {
return null;
}
}

// Return a listener for the "Close Program" button.
private ActionListener createCloseProgramButtonListener() {
return new ActionListener() {
Expand Down
40 changes: 40 additions & 0 deletions src/org/aavso/tools/vstar/ui/mediator/Mediator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -2385,5 +2388,42 @@ public void run() {
}
});
}

/**
* Plug-in help
*/
public static void openPluginHelp(String plugin_doc_name) {
String urlStr = "https://github.com/AAVSO/VStar/tree/master/plugin/doc/";
if (plugin_doc_name != null) {
// plugin_doc_name can be a file name (without path) resided in the base plug-in doc directory.
// In this case it may contain spaces and other special characters.
// Or it can be a document URL, in this case, spaces and special characters must be properly encoded (i.e. %20 instead of space).
// First, we try to convert the string into URI.
// If the conversion was successful, the string is presumably a full document path.
// If not, consider it as a file name.
URI uri = getURIfromStringSafe(plugin_doc_name);
if (uri != null) {
urlStr = uri.toString();
}
else {
try {
plugin_doc_name = URLEncoder.encode(plugin_doc_name, "UTF-8").replace("+", "%20");
} catch (UnsupportedEncodingException ex) {
plugin_doc_name = "";
}
urlStr = urlStr += plugin_doc_name;
}
}
Mediator.openHelpURLInWebBrowser(urlStr);
}

private static URI getURIfromStringSafe(String s) {
try {
return (new URL(s)).toURI();
} catch (MalformedURLException ex) {
return null;
} catch (URISyntaxException ex) {
return null;
}
}
}

0 comments on commit e96b6da

Please sign in to comment.