Skip to content

Commit e0cbe93

Browse files
author
SeTSeR
committed
Fixed Makefile
2 parents c54d3d3 + afa0860 commit e0cbe93

13 files changed

Lines changed: 307 additions & 0 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ install:
4545
mkdir -p $(DESTDIR)$(PREFIX)/share/NoticEditor/examples
4646
install -m 0755 $(DIST)/$(OUTPUT) $(DESTDIR)$(PREFIX)/bin/$(OUTPUT)
4747
cp -rf $(EXAMPLES) $(DESTDIR)$(PREFIX)/share/NoticEditor/examples
48+
chmod -R 0755 $(DESTDIR)$(PREFIX)/share/NoticEditor/examples

src/com/temporaryteam/noticeditor/controller/NoticeController.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@
2727
import com.temporaryteam.noticeditor.view.Chooser;
2828
import com.temporaryteam.noticeditor.view.EditNoticeTreeCell;
2929
import com.temporaryteam.noticeditor.view.Notification;
30+
import java.util.Locale;
3031
import java.util.ResourceBundle;
3132
import java.util.logging.Level;
3233
import java.util.logging.Logger;
3334
import javafx.beans.binding.Bindings;
3435

3536
import javafx.beans.value.ChangeListener;
3637
import javafx.beans.value.ObservableValue;
38+
import javafx.fxml.FXMLLoader;
39+
import javafx.scene.Scene;
3740
import javafx.scene.layout.VBox;
41+
import javafx.stage.Modality;
42+
import javafx.stage.Stage;
3843

3944
public class NoticeController {
4045

@@ -331,6 +336,34 @@ private void handleAbout(ActionEvent event) {
331336
Notification.show("NoticEditor\n==========\n\nhttps://github.com/TemporaryTeam/NoticEditor");
332337
}
333338

339+
@FXML
340+
private void handleImportUrl(ActionEvent event) {
341+
try {
342+
final ResourceBundle resource = ResourceBundle.getBundle("resources.i18n.WebImport", Locale.getDefault());
343+
344+
Stage stage = new Stage();
345+
stage.setTitle(resource.getString("import"));
346+
stage.initOwner(main.getPrimaryStage());
347+
stage.initModality(Modality.WINDOW_MODAL);
348+
349+
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/WebImport.fxml"), resource);
350+
Scene scene = new Scene(loader.load());
351+
stage.setScene(scene);
352+
WebImportController controller = (WebImportController) loader.getController();
353+
controller.setImportCallback((html, ex) -> {
354+
if (ex != null) {
355+
Notification.error(ex.toString());
356+
} else if (html != null) {
357+
noticeArea.setText(html);
358+
}
359+
stage.close();
360+
});
361+
stage.show();
362+
} catch(Exception e) {
363+
e.printStackTrace();
364+
}
365+
}
366+
334367
public NoticeTreeItem getCurrentNotice() {
335368
return currentTreeItem;
336369
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.temporaryteam.noticeditor.controller;
2+
3+
import com.temporaryteam.noticeditor.io.importers.HtmlImportMode;
4+
import com.temporaryteam.noticeditor.io.importers.ImportCallback;
5+
import com.temporaryteam.noticeditor.io.importers.WebImporter;
6+
import java.net.URL;
7+
import java.util.ResourceBundle;
8+
import javafx.collections.ObservableList;
9+
import javafx.event.ActionEvent;
10+
import javafx.fxml.FXML;
11+
import javafx.fxml.Initializable;
12+
import javafx.scene.Node;
13+
import javafx.scene.control.RadioButton;
14+
import javafx.scene.control.TextField;
15+
import javafx.scene.control.ToggleGroup;
16+
import javafx.scene.layout.VBox;
17+
import javafx.scene.web.WebView;
18+
19+
/**
20+
*
21+
* @author aNNiMON
22+
*/
23+
public class WebImportController implements Initializable {
24+
25+
@FXML
26+
private VBox modesBox;
27+
28+
@FXML
29+
private WebView pagePreview;
30+
31+
@FXML
32+
private TextField urlField;
33+
34+
private WebImporter importer;
35+
private HtmlImportMode importMode;
36+
private ImportCallback<String, Exception> importCallback;
37+
38+
@Override
39+
public void initialize(URL location, ResourceBundle resources) {
40+
importer = new WebImporter();
41+
importMode = HtmlImportMode.ORIGINAL;
42+
43+
ObservableList<Node> nodes = modesBox.getChildren();
44+
nodes.clear();
45+
final ToggleGroup modesGroup = new ToggleGroup();
46+
for (HtmlImportMode value : HtmlImportMode.values()) {
47+
RadioButton radio = new RadioButton(resources.getString(value.getName()));
48+
if (value == importMode) radio.setSelected(true);
49+
radio.setOnAction(e -> onModeChanged(value));
50+
radio.setToggleGroup(modesGroup);
51+
nodes.add(radio);
52+
}
53+
54+
pagePreview.getEngine().loadContent(resources.getString("preview"), "text/html");
55+
}
56+
57+
public void setImportCallback(ImportCallback<String, Exception> importCallback) {
58+
this.importCallback = importCallback;
59+
}
60+
61+
private void onModeChanged(HtmlImportMode mode) {
62+
importMode = mode;
63+
handlePreview(null);
64+
}
65+
66+
@FXML
67+
private void handlePreview(ActionEvent event) {
68+
if (!isUrlValid()) return;
69+
70+
importer.importFrom(urlField.getText(), importMode, (html, ex) -> {
71+
if (html != null) {
72+
pagePreview.getEngine().loadContent(html, "text/html");
73+
}
74+
});
75+
}
76+
77+
@FXML
78+
private void handleImport(ActionEvent event) {
79+
if (!isUrlValid()) return;
80+
81+
importer.importFrom(urlField.getText(), importMode, importCallback);
82+
}
83+
84+
private boolean isUrlValid() {
85+
final String url = urlField.getText();
86+
return !url.isEmpty();
87+
}
88+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.temporaryteam.noticeditor.io.importers;
2+
3+
import org.jsoup.safety.Whitelist;
4+
5+
/**
6+
*
7+
* @author aNNiMON
8+
*/
9+
public enum HtmlImportMode {
10+
RELAXED("relaxed", Whitelist.relaxed()),
11+
ONLY_TEXT("only_text", Whitelist.none()),
12+
ORIGINAL("original", null),
13+
BASIC("basic", Whitelist.basic()),
14+
BASIC_WITH_IMAGES("basic_with_img", Whitelist.basicWithImages()),
15+
SIMPLE_TEXT("simple_text", Whitelist.simpleText());
16+
17+
private final String name;
18+
private final Whitelist whitelist;
19+
20+
private HtmlImportMode(String name, Whitelist whitelist) {
21+
this.name = name;
22+
this.whitelist = whitelist;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public Whitelist getWhitelist() {
30+
return whitelist;
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.temporaryteam.noticeditor.io.importers;
2+
3+
import javafx.application.Platform;
4+
import org.jsoup.Jsoup;
5+
import org.jsoup.safety.Whitelist;
6+
7+
/**
8+
* Clears html page. Inserts scripts, styles, images directly to html.
9+
*
10+
* @author Naik, aNNiMON
11+
*/
12+
public class HtmlImporter implements Importer<String, HtmlImportMode, String> {
13+
14+
@Override
15+
public void importFrom(final String html, final HtmlImportMode mode, final ImportCallback<String, Exception> callback) {
16+
new Thread(() -> {
17+
try {
18+
String result = cleanHtml(html, mode.getWhitelist());
19+
Platform.runLater(() -> callback.call(result, null));
20+
} catch (Exception ex) {
21+
Platform.runLater(() -> callback.call(null, ex));
22+
}
23+
}).start();
24+
}
25+
26+
protected String cleanHtml(String html, Whitelist whitelist) throws Exception {
27+
if (whitelist == null) return html;
28+
return Jsoup.clean(html, whitelist);
29+
}
30+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.temporaryteam.noticeditor.io.importers;
2+
3+
/**
4+
*
5+
* @author aNNiMON
6+
* @param <R> result
7+
* @param <O> optional
8+
*/
9+
@FunctionalInterface
10+
public interface ImportCallback<R, O> {
11+
12+
public void call(R result, O optional);
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.temporaryteam.noticeditor.io.importers;
2+
3+
/**
4+
*
5+
* @author aNNiMON
6+
* @param <D> input data
7+
* @param <O> options
8+
* @param <R> result
9+
*
10+
*/
11+
public interface Importer<D, O, R> {
12+
13+
public void importFrom(D data, O options, ImportCallback<R, Exception> callback);
14+
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.temporaryteam.noticeditor.io.importers;
2+
3+
import com.temporaryteam.noticeditor.io.IOUtil;
4+
import java.net.URL;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import org.jsoup.safety.Whitelist;
8+
9+
/**
10+
* Load page from Internet, insert scripts, styles, images directly to html.
11+
*
12+
* @author Naik
13+
*/
14+
public class WebImporter extends HtmlImporter {
15+
16+
private final Map<String, String> cache = new HashMap<>();
17+
18+
@Override
19+
protected String cleanHtml(String url, Whitelist whitelist) throws Exception {
20+
String html;
21+
if (cache.containsKey(url)) {
22+
html = cache.get(url);
23+
} else {
24+
html = IOUtil.stringFromStream(new URL(url).openStream());
25+
cache.put(url, html);
26+
}
27+
return super.cleanHtml(html, whitelist);
28+
}
29+
}

src/fxml/Main.fxml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
<MenuItem onAction="#handleSave" text="Save" />
2121
<MenuItem onAction="#handleSaveAs" text="Save As" />
2222
<MenuItem onAction="#handleExportHtml" text="Export to HTML" />
23+
<Menu mnemonicParsing="false" text="Import">
24+
<items>
25+
<MenuItem onAction="#handleImportUrl" text="From url" />
26+
</items>
27+
</Menu>
2328
<MenuItem onAction="#handleExit" text="Exit" />
2429
</items>
2530
</Menu>

src/fxml/WebImport.fxml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.geometry.*?>
4+
<?import javafx.scene.web.*?>
5+
<?import javafx.scene.*?>
6+
<?import javafx.scene.control.*?>
7+
<?import java.lang.*?>
8+
<?import javafx.scene.layout.*?>
9+
10+
<GridPane prefHeight="320.0" prefWidth="480.0" hgap="5.0" vgap="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.temporaryteam.noticeditor.controller.WebImportController">
11+
<columnConstraints>
12+
<ColumnConstraints hgrow="SOMETIMES" percentWidth="25.0" />
13+
<ColumnConstraints hgrow="SOMETIMES" />
14+
<ColumnConstraints halignment="CENTER" hgrow="NEVER" />
15+
<ColumnConstraints halignment="CENTER" hgrow="NEVER" />
16+
</columnConstraints>
17+
<rowConstraints>
18+
<RowConstraints vgrow="NEVER" />
19+
<RowConstraints vgrow="ALWAYS" />
20+
</rowConstraints>
21+
<children>
22+
<TextField fx:id="urlField" promptText="http://" GridPane.columnSpan="2" />
23+
<Button mnemonicParsing="false" onAction="#handlePreview" text="%preview" GridPane.columnIndex="2" />
24+
<Button mnemonicParsing="false" onAction="#handleImport" text="%import" GridPane.columnIndex="3" />
25+
<VBox fx:id="modesBox" spacing="5.0" GridPane.rowIndex="1" />
26+
<WebView fx:id="pagePreview" prefHeight="-1.0" prefWidth="-1.0" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="1" />
27+
</children>
28+
<padding>
29+
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
30+
</padding>
31+
</GridPane>

0 commit comments

Comments
 (0)