Skip to content

Commit 5905e0d

Browse files
author
Open Lowcode SAS
committed
Close #65
1 parent 8a9a74c commit 5905e0d

File tree

1 file changed

+84
-18
lines changed

1 file changed

+84
-18
lines changed

src/org/openlowcode/tools/richtext/RichTextArea.java

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.util.ArrayList;
1717
import java.util.logging.Logger;
1818
import org.openlowcode.client.runtime.PageActionManager;
19-
import javafx.scene.control.Button;
2019
import javafx.scene.control.ToggleButton;
2120
import javafx.scene.input.Clipboard;
2221
import javafx.scene.input.ClipboardContent;
@@ -360,8 +359,8 @@ public void resetOtherSelections(Paragraph paragraph) {
360359
}
361360

362361
/**
363-
* a control that allows to display and edit a text, potentially with
364-
* specific rich-text file. It always displays the full size of the text
362+
* a control that allows to display and edit a text, potentially with specific
363+
* rich-text file. It always displays the full size of the text
365364
*
366365
* @param richtext true if rich text
367366
* @param editable true if editable
@@ -543,9 +542,6 @@ public void handle(ActionEvent event) {
543542

544543
toolbar.getChildren().add(titlebutton);
545544

546-
547-
548-
549545
contextmenu = new ContextMenu();
550546
contextmenu.focusedProperty().addListener(new ChangeListener<Boolean>() {
551547

@@ -562,11 +558,11 @@ public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldvalue, B
562558
@Override
563559
public void handle(MouseEvent event) {
564560
contextmenu.show(toolbar, event.getScreenX(), event.getScreenY());
565-
561+
566562
}
567-
563+
568564
});
569-
565+
570566
MenuItem clear = new MenuItem("Clear");
571567
clear.setOnAction(new EventHandler<ActionEvent>() {
572568
@Override
@@ -587,7 +583,7 @@ public void handle(ActionEvent event) {
587583

588584
});
589585
contextmenu.getItems().add(clear);
590-
586+
591587
MenuItem exportsource = new MenuItem("Export Source");
592588
exportsource.setOnAction(new EventHandler<ActionEvent>() {
593589

@@ -599,7 +595,7 @@ public void handle(ActionEvent event) {
599595
content.putString(source);
600596
Clipboard.getSystemClipboard().setContent(content);
601597
pageactionmanager.getClientSession().getActiveClientDisplay()
602-
.updateStatusBar("text source copied to clipboard, size = "+source.length()+"ch");
598+
.updateStatusBar("text source copied to clipboard, size = " + source.length() + "ch");
603599
} catch (Exception e) {
604600
logger.warning("Error while executing export source " + e.getMessage());
605601
for (int i = 0; i < e.getStackTrace().length; i++)
@@ -608,16 +604,39 @@ public void handle(ActionEvent event) {
608604
pageactionmanager.getClientSession().getActiveClientDisplay()
609605
.updateStatusBar("Error while executing export source " + e.getMessage(), true);
610606
}
611-
607+
612608
}
613-
609+
614610
});
615611
contextmenu.getItems().add(exportsource);
616-
617-
618-
619-
620-
612+
613+
MenuItem exportsourceescape = new MenuItem("Export Source (Dev)");
614+
exportsourceescape.setOnAction(new EventHandler<ActionEvent>() {
615+
616+
@Override
617+
public void handle(ActionEvent event) {
618+
try {
619+
String source = generateText();
620+
source = RichTextArea.escapeforjavasource(source);
621+
final ClipboardContent content = new ClipboardContent();
622+
content.putString(source);
623+
Clipboard.getSystemClipboard().setContent(content);
624+
pageactionmanager.getClientSession().getActiveClientDisplay()
625+
.updateStatusBar("text source copied to clipboard, size = " + source.length() + "ch");
626+
} catch (Exception e) {
627+
logger.warning("Error while executing export source " + e.getMessage());
628+
for (int i = 0; i < e.getStackTrace().length; i++)
629+
logger.warning(" " + e.getStackTrace()[i]);
630+
;
631+
pageactionmanager.getClientSession().getActiveClientDisplay()
632+
.updateStatusBar("Error while executing export source " + e.getMessage(), true);
633+
}
634+
635+
}
636+
637+
});
638+
contextmenu.getItems().add(exportsourceescape);
639+
621640
area.setTop(toolbar);
622641

623642
}
@@ -795,4 +814,51 @@ public void deleteActiveParagraphIfNotLast() {
795814

796815
}
797816

817+
/**
818+
* a utility method to escape a string
819+
*
820+
* @param source
821+
* @return
822+
*/
823+
public static String escapeforjavasource(String source) {
824+
StringBuffer returnstring = new StringBuffer();
825+
returnstring.append('"');
826+
if (source != null)
827+
for (int i = 0; i < source.length(); i++) {
828+
char currentchar = source.charAt(i);
829+
if (currentchar == '\n') {
830+
returnstring.append("\\n");
831+
continue;
832+
}
833+
if (currentchar == '\\') {
834+
returnstring.append("\\\\");
835+
continue;
836+
}
837+
if (currentchar == '\r') {
838+
returnstring.append("\\r");
839+
continue;
840+
}
841+
if (currentchar == '"') {
842+
returnstring.append("\\\"");
843+
continue;
844+
}
845+
if (currentchar == '\u2022') {
846+
returnstring.append("\\u2022");
847+
continue;
848+
}
849+
if (currentchar == '\u0009') {
850+
returnstring.append("\\u0009");
851+
continue;
852+
}
853+
if (currentchar == '\u0003') {
854+
returnstring.append("\\u0003");
855+
continue;
856+
}
857+
returnstring.appendCodePoint(currentchar);
858+
859+
}
860+
returnstring.append('"');
861+
return returnstring.toString();
862+
}
863+
798864
}

0 commit comments

Comments
 (0)