Now you can open the documentation from the help menu.

This commit is contained in:
hneemann 2017-08-18 23:48:04 +02:00
parent 13c89894e0
commit 55d67b3bec
5 changed files with 104 additions and 1 deletions

View File

@ -0,0 +1,96 @@
package de.neemann.digital.gui;
import de.neemann.digital.draw.library.ElementLibrary;
import de.neemann.digital.lang.Lang;
import de.neemann.gui.ErrorMessage;
import de.neemann.gui.ToolTipAction;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
/**
* Used to open documentation
*/
public class DocumentationLocator {
private final File folder;
/**
* Creates a new instance
*/
public DocumentationLocator() {
File folder = null;
try {
String path = ElementLibrary.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath().replace('\\', '/');
if (path.endsWith("/target/classes/"))
folder = new File(path.substring(0, path.length() - 9) + "/xslt");
else if (path.endsWith("/target/Digital.jar"))
folder = new File(path.substring(0, path.length() - 11) + "/xslt");
else if (path.endsWith("Digital.jar"))
folder = new File(path.substring(0, path.length() - 12) + "/docu");
} catch (URISyntaxException e) {
// do nothing on error
}
if (folder != null && folder.exists())
this.folder = folder;
else
this.folder = null;
}
/**
* Adds the documentation to the given menu
*
* @param help the menu to add the documentation to
*/
public void addMenuTo(JMenu help) {
if (folder != null) {
File[] files = folder.listFiles((file, name) -> name.endsWith(".pdf"));
if (files != null && files.length > 0) {
String language = "_" + Lang.currentLanguage().getName() + ".pdf";
File found = null;
for (File f : files)
if (f.getName().endsWith(language))
found = f;
if (found == null) {
JMenu docu = new JMenu(Lang.get("menu_pdfDocumentation"));
help.add(docu);
for (File f : files)
docu.add(new OpenPDFAction(f).createJMenuItem());
} else
help.add(new OpenPDFAction(found, Lang.get("menu_pdfDocumentation")));
}
}
}
private static final class OpenPDFAction extends ToolTipAction {
private final File f;
private OpenPDFAction(File f) {
this(f, Lang.get("menu_openPdfDocumentation", f.getName()));
}
private OpenPDFAction(File f, String name) {
super(name);
this.f = f;
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
try {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.OPEN))
desktop.open(f);
else
throw new IOException("could not open pdf document");
} catch (IOException e) {
new ErrorMessage(Lang.get("msg_errorOpeningDocumentation")).addCause(e).show();
}
}
}
}

View File

@ -249,6 +249,7 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS
}
}
}.setToolTip(Lang.get("menu_help_elements_tt")).createJMenuItem());
new DocumentationLocator().addMenuTo(help);
new ToolTipAction("insertLast") {
@Override

View File

@ -918,6 +918,9 @@ Sind evtl. die Namen der Variablen nicht eindeutig?</string>
<string name="menu_addPowerSupply_tt">Erweitert die Schaltung um eine Spannungsversorung.</string>
<string name="menu_exportVHDL">Export zu VHDL</string>
<string name="menu_exportVHDL_tt">Exportiert die Schaltung zu VHDL</string>
<string name="menu_pdfDocumentation">Dokumentation</string>
<string name="menu_openPdfDocumentation">Öffne {0}</string>
<string name="msg_errorOpeningDocumentation">Fehler beim Öffnen einer PDF-Datei!</string>
<string name="message">Digital

View File

@ -904,6 +904,9 @@ The names of the variables may not be unique.</string>
<string name="menu_addPowerSupply_tt">Adds a power supply to the circuit.</string>
<string name="menu_exportVHDL">Export to VHDL</string>
<string name="menu_exportVHDL_tt">Exports the circuit to VHDL</string>
<string name="menu_pdfDocumentation">Documentation</string>
<string name="menu_openPdfDocumentation">Open {0}</string>
<string name="msg_errorOpeningDocumentation">Error opening a PDF file!</string>
<string name="message">Digital

View File

@ -209,7 +209,7 @@ public class DocuTest extends TestCase {
for (Language l : Lang.getBundle().getSupportedLanguages()) {
// set language
Lang.setActualRuntimeLanguage(l);
final String basename = "docu_" + l.getName();
final String basename = "Documentation_" + l.getName();
// write xml
File xml = new File(target, basename + ".xml");
try (Writer w = new OutputStreamWriter(new FileOutputStream(xml), "UTF-8")) {