-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- extract AboutView controller and FMXL - refactor AboutViewController to make better use of Optional and Stream - add Google Guava library to dependencies Closes #456.
- Loading branch information
1 parent
0986204
commit a48d817
Showing
5 changed files
with
164 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
src/main/java/org/terasology/launcher/gui/javafx/AboutViewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package org.terasology.launcher.gui.javafx; | ||
|
||
import com.github.rjeschke.txtmark.Configuration; | ||
import com.github.rjeschke.txtmark.Processor; | ||
import com.google.common.io.Files; | ||
import javafx.fxml.FXML; | ||
import javafx.scene.control.Accordion; | ||
import javafx.scene.control.TitledPane; | ||
import javafx.scene.layout.AnchorPane; | ||
import javafx.scene.web.WebView; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.terasology.launcher.util.BundleUtils; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.net.URL; | ||
import java.nio.charset.Charset; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Controller for the <b>About</b> section in the tab view. | ||
* | ||
* Presents static content which is compiled from Markdown and HTML documents. | ||
*/ | ||
public class AboutViewController { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(AboutViewController.class); | ||
|
||
/** Bundle key for the resources related to this view. */ | ||
private static final String ABOUT = "about"; | ||
|
||
private static final Charset UTF_8 = Charset.forName("UTF-8"); | ||
|
||
@FXML | ||
private Accordion aboutInfoAccordion; | ||
|
||
@FXML | ||
public void initialize() { | ||
update(); | ||
} | ||
|
||
/** | ||
* Update/reload the <b>About</b> view. | ||
* | ||
* This will reload and parse the files to display again! | ||
*/ | ||
public void update() { | ||
aboutInfoAccordion.getPanes().clear(); | ||
|
||
Stream.of("README.md", "CHANGELOG.md", "CONTRIBUTING.md", "LICENSE") | ||
.map(filename -> BundleUtils.getFXMLUrl(ABOUT, filename)) | ||
.filter(Objects::nonNull) | ||
.map(this::getPaneFor) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.forEach(aboutInfoAccordion.getPanes()::add); | ||
|
||
if (!aboutInfoAccordion.getPanes().isEmpty()) { | ||
aboutInfoAccordion.setExpandedPane(aboutInfoAccordion.getPanes().get(0)); | ||
} | ||
} | ||
|
||
private Optional<TitledPane> getPaneFor(URL url) { | ||
return getViewFor(url) | ||
.map(view -> { | ||
view.getStylesheets().add(BundleUtils.getFXMLUrl("css_webview").toExternalForm()); | ||
view.setContextMenuEnabled(false); | ||
return view; | ||
}) | ||
.map(view -> { | ||
final AnchorPane pane = new AnchorPane(); | ||
AnchorPane.setBottomAnchor(view, 0.0); | ||
AnchorPane.setTopAnchor(view, 0.0); | ||
pane.getChildren().add(view); | ||
return pane; | ||
}) | ||
.map(contentPane -> { | ||
String fname = Files.getNameWithoutExtension(url.getFile()); | ||
final TitledPane titledPane = new TitledPane(fname, contentPane); | ||
titledPane.setAnimated(false); | ||
return titledPane; | ||
}); | ||
} | ||
|
||
private Optional<WebView> getViewFor(URL url) { | ||
switch (Files.getFileExtension(url.getFile().toLowerCase())) { | ||
case "md": | ||
case "markdown": | ||
return renderMarkdown(url); | ||
case "htm": | ||
case "html": | ||
return renderHtml(url); | ||
default: | ||
return renderUnknown(url); | ||
} | ||
|
||
} | ||
|
||
private Optional<WebView> renderMarkdown(URL url) { | ||
WebView view = null; | ||
try (InputStream input = url.openStream()) { | ||
view = new WebView(); | ||
String content = new StringBuilder() | ||
.append("<body style='padding-left:24px;'>\n") | ||
.append(Processor.process(input, Configuration.DEFAULT)) | ||
.append("</body>") | ||
.toString(); | ||
view.getEngine().loadContent(content, "text/html"); | ||
} catch (IOException e) { | ||
logger.warn("Could not render markdown file: {}", url); | ||
} | ||
return Optional.ofNullable(view); | ||
} | ||
|
||
private Optional<WebView> renderHtml(URL url) { | ||
final WebView view = new WebView(); | ||
view.getEngine().load(url.toExternalForm()); | ||
return Optional.of(view); | ||
} | ||
|
||
private Optional<WebView> renderUnknown(URL url) { | ||
WebView view = null; | ||
try (Reader isr = new InputStreamReader(url.openStream(), UTF_8); | ||
BufferedReader br = new BufferedReader(isr)) { | ||
|
||
view = new WebView(); | ||
StringBuilder content = new StringBuilder(); | ||
String line = br.readLine(); | ||
|
||
while (line != null) { | ||
content.append(line); | ||
content.append(System.lineSeparator()); | ||
line = br.readLine(); | ||
} | ||
view.getEngine().loadContent(content.toString(), "text/plain"); | ||
} catch (IOException e) { | ||
logger.warn("Could not render file: {}", url); | ||
} | ||
return Optional.ofNullable(view); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/resources/org/terasology/launcher/views/about-view.fxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?import javafx.scene.layout.AnchorPane?> | ||
<?import javafx.scene.control.Accordion?> | ||
<AnchorPane fx:controller="org.terasology.launcher.gui.javafx.AboutViewController" | ||
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" | ||
minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> | ||
<children> | ||
<Accordion fx:id="aboutInfoAccordion" | ||
prefHeight="336.0" prefWidth="800.0" | ||
AnchorPane.bottomAnchor="8.0" AnchorPane.leftAnchor="8.0" AnchorPane.rightAnchor="8.0" | ||
AnchorPane.topAnchor="8.0"/> | ||
</children> | ||
</AnchorPane> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters