Browse files

added bbcode editor

  • Loading branch information...
1 parent cd869aa commit 4af8cbdae75247d2e2812fbf069a0e95f7414e3c unknown committed Jan 3, 2012
View
5 .settings/org.eclipse.m2e.core.prefs
@@ -0,0 +1,5 @@
+#Thu Aug 25 10:24:06 CEST 2011
+activeProfiles=
+eclipse.preferences.version=1
+resolveWorkspaceProjects=true
+version=1
View
12 src/main/java/burrito/annotations/BBCode.java
@@ -0,0 +1,12 @@
+package burrito.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target( { ElementType.FIELD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface BBCode {
+ //no value
+}
View
13 src/main/java/burrito/client/crud/generic/fields/BBCodeField.java
@@ -0,0 +1,13 @@
+package burrito.client.crud.generic.fields;
+
+@SuppressWarnings("serial")
+public class BBCodeField extends StringField {
+
+ public BBCodeField(String string) {
+ super(string);
+ }
+
+ public BBCodeField() {
+ // TODO Auto-generated constructor stub
+ }
+}
View
40 src/main/java/burrito/client/crud/input/BBCodeInputField.java
@@ -0,0 +1,40 @@
+package burrito.client.crud.input;
+
+import burrito.client.crud.generic.CrudField;
+import burrito.client.crud.generic.fields.BBCodeField;
+import burrito.client.crud.widgets.BBCodeEditor;
+
+import com.google.gwt.user.client.ui.Widget;
+
+public class BBCodeInputField implements CrudInputField<String> {
+
+ private final BBCodeField field;
+ private BBCodeEditor codeEditor;
+
+ public BBCodeInputField(BBCodeField field) {
+ this.field = field;
+ this.codeEditor = new BBCodeEditor((String) field.getValue());
+ }
+
+ @Override
+ public Widget getDisplayWidget() {
+ return codeEditor;
+ }
+
+ @Override
+ public void load(String value) {
+ codeEditor.setText(value);
+ }
+
+ @Override
+ public String getValue() {
+ return codeEditor.getText();
+ }
+
+ @Override
+ public CrudField getCrudField() {
+ field.setValue(codeEditor.getText());
+ return field;
+ }
+
+}
View
145 src/main/java/burrito/client/crud/widgets/BBCodeEditor.java
@@ -0,0 +1,145 @@
+package burrito.client.crud.widgets;
+
+
+import burrito.client.crud.widgets.SelectableTextArea.SelectedText;
+import burrito.client.widgets.services.BBCodeService;
+import burrito.client.widgets.services.BBCodeServiceAsync;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.event.logical.shared.BeforeSelectionEvent;
+import com.google.gwt.event.logical.shared.BeforeSelectionHandler;
+import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.HTML;
+import com.google.gwt.user.client.ui.HorizontalPanel;
+import com.google.gwt.user.client.ui.TabPanel;
+import com.google.gwt.user.client.ui.VerticalPanel;
+
+public class BBCodeEditor extends VerticalPanel {
+
+ private TabPanel tabPanel = new TabPanel();
+
+ private VerticalPanel rawPanel = new VerticalPanel();
+ private HTML preview = new HTML();
+
+ private HorizontalPanel buttonPanel = new HorizontalPanel();
+ private SelectableTextArea rawEditor = new SelectableTextArea();
+
+ private BBCodeServiceAsync bbCodeService = GWT.create(BBCodeService.class);
+
+ public BBCodeEditor(String value) {
+ initButtons();
+ this.rawEditor.setText(value);
+ }
+
+ public BBCodeEditor() {
+ initButtons();
+ }
+
+ private void initButtons() {
+ Button buttonBold = new Button("Bold");
+ buttonBold.addClickHandler(new ClickHandler() {
+ @Override
+ public void onClick(ClickEvent event) {
+ SelectedText selectedText = rawEditor.getSelectedTextObj();
+ selectedText.text = "[b]" + selectedText.text + "[/b]";
+ rawEditor.setSelectedText(selectedText);
+ }
+ });
+ buttonPanel.add(buttonBold);
+
+ Button buttonItalic = new Button("Italic");
+ buttonItalic.addClickHandler(new ClickHandler() {
+ @Override
+ public void onClick(ClickEvent event) {
+ SelectedText selectedText = rawEditor.getSelectedTextObj();
+ selectedText.text = "[i]" + selectedText.text + "[/i]";
+ rawEditor.setSelectedText(selectedText);
+ }
+ });
+ buttonPanel.add(buttonItalic);
+
+ Button buttonImg = new Button("Bild");
+ buttonImg.addClickHandler(new ClickHandler() {
+ @Override
+ public void onClick(ClickEvent event) {
+ ImagePickerPopup imagePicker = new ImagePickerPopup(800, 800, false);
+ imagePicker.center();
+ imagePicker.show();
+ imagePicker.addSaveHandler(new ImagePickerPopup.SaveHandler() {
+
+ public void saved(String value) {
+ SelectedText selectedText = rawEditor.getSelectedTextObj();
+ selectedText.text = "[img]" + value + "[/img]";
+ rawEditor.setSelectedText(selectedText);
+ }
+ });
+ }
+ });
+ buttonPanel.add(buttonImg);
+
+ Button buttonUrl = new Button("Url");
+ buttonUrl.addClickHandler(new ClickHandler() {
+
+ @Override
+ public void onClick(ClickEvent event) {
+ BBUrlPanel urlPanel = new BBUrlPanel(rawEditor);
+ urlPanel.show();
+ }
+ });
+ buttonPanel.add(buttonUrl);
+
+ Button buttonYoutube = new Button("Youtube");
+ buttonYoutube.addClickHandler(new ClickHandler() {
+ @Override
+ public void onClick(ClickEvent event) {
+ BBYoutubePanel youtubePanel = new BBYoutubePanel(rawEditor);
+ youtubePanel.show();
+ }
+ });
+ buttonPanel.add(buttonYoutube);
+
+
+ rawPanel.add(buttonPanel);
+ rawPanel.add(rawEditor);
+
+ preview.setSize("960px", "700px");
+ rawEditor.setSize("960px", "700px");
+ tabPanel.setSize("960px", "700px");
+
+ tabPanel.add(rawPanel, "RAW");
+ tabPanel.add(preview, "Preview");
+ tabPanel.selectTab(0);
+
+ tabPanel.addBeforeSelectionHandler(new BeforeSelectionHandler<Integer>() {
+ @Override
+ public void onBeforeSelection(BeforeSelectionEvent<Integer> event) {
+
+ bbCodeService.generateBBCodePreview(rawEditor.getText(), new AsyncCallback<String>() {
+ @Override
+ public void onSuccess(String html) {
+ preview.setHTML(html);
+ }
+
+ @Override
+ public void onFailure(Throwable caught) {
+ Window.alert("Could not create preview! " + caught.getMessage());
+ }
+ });
+ }
+ });
+
+ add(tabPanel);
+ }
+
+ public void setText(String value) {
+ this.rawEditor.setText(value);
+ }
+
+ public String getText() {
+ return this.rawEditor.getText();
+ }
+}
View
77 src/main/java/burrito/client/crud/widgets/BBPopupPanel.java
@@ -0,0 +1,77 @@
+package burrito.client.crud.widgets;
+
+
+import burrito.client.crud.widgets.SelectableTextArea.SelectedText;
+
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.DialogBox;
+import com.google.gwt.user.client.ui.HorizontalPanel;
+import com.google.gwt.user.client.ui.VerticalPanel;
+import com.google.gwt.user.client.ui.Widget;
+
+public abstract class BBPopupPanel extends DialogBox {
+
+ private VerticalPanel panel = new VerticalPanel();
+
+ private final SelectableTextArea rawEditor;
+ private SelectedText selectedText;
+
+ public BBPopupPanel(String caption, SelectableTextArea rawEditor2) {
+ super();
+ setText(caption);
+ this.rawEditor = rawEditor2;
+ this.selectedText = rawEditor2.getSelectedTextObj();
+ }
+
+ protected String getSelectedText() {
+ return this.selectedText.text;
+ }
+
+ protected void setSelectedText(String text) {
+ this.selectedText.text = text;
+ this.rawEditor.setSelectedText(this.selectedText);
+ }
+
+ protected void addPanel(Widget w) {
+ panel.add(w);
+ }
+
+ abstract protected void onShow();
+
+ abstract protected boolean onClose();
+
+ @Override
+ public void show() {
+ add(panel);
+
+ onShow();
+
+ Button okButton = new Button("Ok");
+ okButton.addClickHandler(new ClickHandler() {
+ @Override
+ public void onClick(ClickEvent event) {
+ if (onClose()) {
+ hide();
+ }
+ }
+ });
+
+ Button cancelButton = new Button("Avbryt");
+ cancelButton.addClickHandler(new ClickHandler() {
+ @Override
+ public void onClick(ClickEvent event) {
+ hide();
+ }
+ });
+
+ HorizontalPanel horizontalPanel = new HorizontalPanel();
+ horizontalPanel.add(okButton);
+ horizontalPanel.add(cancelButton);
+ addPanel(horizontalPanel);
+
+ super.show();
+ center();
+ }
+}
View
59 src/main/java/burrito/client/crud/widgets/BBUrlPanel.java
@@ -0,0 +1,59 @@
+package burrito.client.crud.widgets;
+
+import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.ui.HorizontalPanel;
+import com.google.gwt.user.client.ui.Label;
+import com.google.gwt.user.client.ui.TextBox;
+
+public class BBUrlPanel extends BBPopupPanel {
+
+ private TextBox srcTextBox;
+ private TextBox titleTextBox;
+
+ public BBUrlPanel(SelectableTextArea rawEditor) {
+ super("Länk", rawEditor);
+ }
+
+ @Override
+ protected void onShow() {
+ Label titleLabel = new Label("title");
+ titleTextBox = new TextBox();
+
+ Label srcLabel = new Label("src");
+ srcTextBox = new TextBox();
+
+ HorizontalPanel titlePanel = new HorizontalPanel();
+ titlePanel.add(titleLabel);
+ titlePanel.add(titleTextBox);
+ addPanel(titlePanel);
+
+ HorizontalPanel srcPanel = new HorizontalPanel();
+ srcPanel.add(srcLabel);
+ srcPanel.add(srcTextBox);
+ addPanel(srcPanel);
+
+ srcTextBox.setText(getSelectedText());
+ }
+
+ private boolean validate(String url) {
+ return url.matches("^(http(s?)://){1}.*$");
+ }
+
+ @Override
+ protected boolean onClose() {
+ String url = srcTextBox.getText();
+ if (!validate(url)) {
+ Window.alert("Not valid image url!");
+ return false;
+ }
+
+ String text = titleTextBox.getText();
+ if (!"".equals(text)) {
+ setSelectedText("[url=" + url + "]" + text + "[/url]");
+ } else {
+ setSelectedText("[url]" + url + "[/url]");
+ }
+
+ return true;
+ }
+}
View
48 src/main/java/burrito/client/crud/widgets/BBYoutubePanel.java
@@ -0,0 +1,48 @@
+package burrito.client.crud.widgets;
+
+import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.ui.HorizontalPanel;
+import com.google.gwt.user.client.ui.Label;
+import com.google.gwt.user.client.ui.TextBox;
+
+public class BBYoutubePanel extends BBPopupPanel {
+
+ public BBYoutubePanel(SelectableTextArea rawEditor) {
+ super("Youtube-video", rawEditor);
+ }
+
+ private TextBox textBox;
+
+ @Override
+ protected void onShow() {
+ Label label = new Label("src");
+ textBox = new TextBox();
+
+ HorizontalPanel horizontalPanel = new HorizontalPanel();
+ horizontalPanel.add(label);
+ horizontalPanel.add(textBox);
+ addPanel(horizontalPanel);
+
+ textBox.setText(getSelectedText());
+ }
+
+ private boolean validate(String url) {
+ //http://youtu.be/R7J8FSBitWs är 11 tecken
+ return url.matches("^http://youtu.be/.{8,15}$");
+ }
+
+
+ @Override
+ protected boolean onClose() {
+ String url = textBox.getText();
+
+ if (!validate(url)) {
+ Window.alert("Not valid youtube url!");
+ return false;
+ }
+
+ setSelectedText("[video]" + url + "[/video]");
+
+ return true;
+ }
+}
View
53 src/main/java/burrito/client/crud/widgets/SelectableTextArea.java
@@ -0,0 +1,53 @@
+package burrito.client.crud.widgets;
+
+import com.google.gwt.core.client.JsArrayString;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.user.client.ui.TextArea;
+
+public class SelectableTextArea extends TextArea {
+
+ public class SelectedText {
+ protected String originalText = "";
+ public String text = "";
+ public Integer startpos;
+
+ public SelectedText(JsArrayString tx) {
+
+ originalText = tx.get(0);
+ text = originalText;
+ try {
+ startpos = Integer.parseInt(tx.get(1));
+ } catch (NumberFormatException e) {
+ startpos = getCursorPos();
+ }
+ }
+ }
+
+ public void setSelectedText(SelectedText selectedText) {
+ Integer startpos = selectedText.startpos;
+
+ String text = getText();
+ String beforeText = text.substring(0, startpos);
+ String afterText = text.substring(startpos + selectedText.originalText.length());
+
+ setText(beforeText + selectedText.text + afterText);
+ }
+
+ public SelectedText getSelectedTextObj() {
+ Element element = getElement();
+ JsArrayString tx = getSelectedText(element);
+ return new SelectedText(tx);
+ }
+
+ private static native JsArrayString getSelectedText(Element elem) /*-{
+ if (isNaN(elem.selectionStart)) {
+ return ["", ""];
+ }
+
+ var start = elem.selectionStart;
+ var end = elem.selectionEnd;
+ var sel = elem.value.substring(start, end);
+
+ return [""+sel, ""+start];
+ }-*/;
+}
View
11 src/main/java/burrito/client/widgets/services/BBCodeService.java
@@ -0,0 +1,11 @@
+package burrito.client.widgets.services;
+
+import com.google.gwt.user.client.rpc.RemoteService;
+import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
+
+@RemoteServiceRelativePath("bbCodeService")
+public interface BBCodeService extends RemoteService {
+
+ String generateBBCodePreview(String bbcode);
+
+}
View
9 src/main/java/burrito/client/widgets/services/BBCodeServiceAsync.java
@@ -0,0 +1,9 @@
+package burrito.client.widgets.services;
+
+import com.google.gwt.user.client.rpc.AsyncCallback;
+
+public interface BBCodeServiceAsync {
+
+ void generateBBCodePreview(String html, AsyncCallback<String> asyncCallback);
+
+}
View
17 src/main/java/burrito/services/BBCodeServiceImpl.java
@@ -0,0 +1,17 @@
+package burrito.services;
+
+import burrito.client.widgets.services.BBCodeService;
+import burrito.util.BBCodeUtil;
+
+import com.google.gwt.user.server.rpc.RemoteServiceServlet;
+
+@SuppressWarnings("serial")
+public class BBCodeServiceImpl extends RemoteServiceServlet implements BBCodeService {
+
+ @Override
+ public String generateBBCodePreview(String bbcode) {
+ String html = BBCodeUtil.generateHTML(bbcode);
+ return html;
+ }
+
+}
View
38 src/main/java/burrito/util/BBCodeUtil.java
@@ -0,0 +1,38 @@
+package burrito.util;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class BBCodeUtil {
+
+ public static String generateHTML(String bbcode) {
+ String html = StringUtils.escapeHtml(bbcode);
+
+ Map<String, String> bbMap = new HashMap<String, String>();
+
+ bbMap.put("(\r\n|\r|\n|\n\r)", "<br/>");
+ bbMap.put("\\[b\\](.+?)\\[/b\\]", "<span style=\"font-weight:bold;\">$1</span>");
+ bbMap.put("\\[i\\](.+?)\\[/i\\]", "<span style=\"font-weight:italic;\">$1</span>");
+ bbMap.put("\\[url\\](.+?)\\[/url\\]", "<a href=\"$1\">$1</a>");
+ bbMap.put("\\[img\\](.+?)\\[/img\\]", "<img src=\"/blobstore/image?key=$1\" />");
+ bbMap.put("\\[url=(.+?)\\](.+?)\\[/url\\]", "<a href=\"$1\">$2</a>");
+
+ for (@SuppressWarnings("rawtypes") Map.Entry entry : bbMap.entrySet()) {
+ html = html.replaceAll(entry.getKey().toString(), entry.getValue().toString());
+ }
+
+ html = generateYouTube(html);
+
+ return html;
+ }
+
+ private static String generateYouTube(String bbcode) {
+ bbcode = bbcode.replaceAll("\\[video\\]http://youtu.be/(.+)\\[/video\\]", youtubeEmbed());
+ bbcode = bbcode.replaceAll("\\[video\\]http://www.youtube.com/watch\\?v=([A-Za-z0-9]+).*\\[/video\\]", youtubeEmbed());
+ return bbcode;
+ }
+
+ private static String youtubeEmbed() {
+ return "<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen></iframe>";
+ }
+}

0 comments on commit 4af8cbd

Please sign in to comment.