Skip to content
This repository has been archived by the owner on May 20, 2021. It is now read-only.

Commit

Permalink
Merge pull request #86 from ShyykoSerhiy/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ShyykoSerhiy committed Sep 24, 2015
2 parents 0bef263 + b2ac631 commit 357d776
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 40 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/github/shyykoserhiy/gfm/browser/BrowserFx.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.concurrent.*;

public class BrowserFx implements IsBrowser {
private final JPanel jPanel;
Expand Down Expand Up @@ -153,6 +154,27 @@ public String getUrl() {
return webView.getEngine().getLocation();
}

@Override
public String getHtml() {
FutureTask<String> futureTask = new FutureTask<String>(new Callable<String>() {
@Override
public String call() throws Exception {
return (String) webView.getEngine().executeScript("document.documentElement.outerHTML");
}
});
String result = "";
try {
result = futureTask.get(1000, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
return result;
}

@Override
public boolean canGoForward() {
WebHistory history = webView.getEngine().getHistory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ public String getUrl() {
return webView.getBrowser().getURL();
}

@Override
public String getHtml() {
return webView.getBrowser().getHTML();
}

@Override
public boolean canGoForward() {
return webView.getBrowser().canGoForward();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public String getUrl() {
return null;//todo throw unsupported exception?
}

@Override
public String getHtml() {
return webView.getSourceCode();
}

@Override
public boolean canGoForward() {
return false;//todo throw unsupported exception?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public interface IsBrowser extends Disposable {

String getUrl();

String getHtml();

boolean canGoForward();

boolean canGoBack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import com.github.shyykoserhiy.gfm.markdown.offline.JnaMarkdownParser;
import com.github.shyykoserhiy.gfm.settings.GfmGlobalSettings;
import com.github.shyykoserhiy.gfm.settings.GfmGlobalSettingsChangedListener;
import com.github.shyykoserhiy.gfm.ui.Utils;
import com.intellij.codeHighlighting.BackgroundEditorHighlighter;
import com.intellij.ide.structureView.StructureViewBuilder;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.fileEditor.FileEditor;
Expand All @@ -24,6 +27,8 @@
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeListener;
Expand All @@ -39,6 +44,8 @@ public abstract class AbstractGfmPreview extends UserDataHolderBase implements D
protected final GfmGlobalSettings settings;
protected IsBrowser browser;

private PopClickListener popClickListener;

public AbstractGfmPreview(@NotNull VirtualFile markdownFile, @NotNull Document document) {
this.markdownFile = markdownFile;
this.document = document;
Expand Down Expand Up @@ -128,6 +135,9 @@ public StructureViewBuilder getStructureViewBuilder() {
public void dispose() {
Disposer.dispose(this);
browser.dispose();
if (popClickListener != null) {
getComponent().removeMouseListener(popClickListener);
}
}

/**
Expand All @@ -145,7 +155,8 @@ public boolean isPreviewIsSelected() {
protected abstract GfmRequestDoneListener getRequestDoneListener();

protected void addPopupListener() {
getComponent().addMouseListener(new PopClickListener());
popClickListener = new PopClickListener();
getComponent().addMouseListener(popClickListener);
}

private void updateMarkdownParser(GfmGlobalSettings gfmGlobalSettings) {
Expand Down Expand Up @@ -200,6 +211,31 @@ class PopUpDemo extends JPopupMenu {

public PopUpDemo() {
anItem = new JMenuItem("Get HTML");
anItem.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
final String html = browser.getHtml();
Window window = SwingUtilities.getWindowAncestor(AbstractGfmPreview.this.getComponent());
JDialog dialog = new JDialog(window);
dialog.setModal(true);
final Editor editor = Utils.createEditor("html");
dialog.setContentPane(editor.getComponent());
dialog.setSize(700, 500);
dialog.setLocationRelativeTo(null);
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
editor.getDocument().setText(html);
}
});
}
});
dialog.setVisible(true);
}
});
add(anItem);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,8 @@
import com.github.shyykoserhiy.gfm.GfmBundle;
import com.github.shyykoserhiy.gfm.editor.RenderingEngine;
import com.github.shyykoserhiy.gfm.markdown.offline.JnaMarkdownParser;
import com.intellij.ide.highlighter.HighlighterFactory;
import com.intellij.openapi.editor.Document;
import com.github.shyykoserhiy.gfm.ui.Utils;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.fileTypes.UnknownFileType;

import javax.swing.*;
import java.awt.*;
Expand Down Expand Up @@ -92,35 +83,7 @@ private void createUIComponents() {
connectionTimeoutSpinner = new JSpinner(new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 100));
socketTimeoutSpinner = new JSpinner(new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 100));
additionalCssPanel = new JPanel(new BorderLayout());
this.additionalCssTextArea = createEditor();
this.additionalCssTextArea = Utils.createEditor("css");
this.additionalCssPanel.add(this.additionalCssTextArea.getComponent(), "Center");
}

private static Editor createEditor() {
EditorFactory editorFactory = EditorFactory.getInstance();
Document editorDocument = editorFactory.createDocument("");
EditorEx editor = (EditorEx) editorFactory.createEditor(editorDocument);
fillEditorSettings(editor.getSettings());
setHighlighting(editor);
return editor;
}

private static void setHighlighting(EditorEx editor) {
FileType cssFileType = FileTypeManager.getInstance().getFileTypeByExtension("css");
if (cssFileType != UnknownFileType.INSTANCE) {
EditorHighlighter editorHighlighter = HighlighterFactory.createHighlighter(cssFileType, EditorColorsManager.getInstance().getGlobalScheme(), null);
editor.setHighlighter(editorHighlighter);
}
}

private static void fillEditorSettings(EditorSettings editorSettings) {
editorSettings.setWhitespacesShown(false);
editorSettings.setLineMarkerAreaShown(false);
editorSettings.setIndentGuidesShown(false);
editorSettings.setLineNumbersShown(true);
editorSettings.setFoldingOutlineShown(false);
editorSettings.setAdditionalColumnsCount(1);
editorSettings.setAdditionalLinesCount(1);
editorSettings.setUseSoftWraps(false);
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/github/shyykoserhiy/gfm/ui/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.shyykoserhiy.gfm.ui;

import com.intellij.ide.highlighter.HighlighterFactory;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.fileTypes.UnknownFileType;

public class Utils {
public static Editor createEditor(String fileExtension) {
EditorFactory editorFactory = EditorFactory.getInstance();
Document editorDocument = editorFactory.createDocument("");
EditorEx editor = (EditorEx) editorFactory.createEditor(editorDocument);
fillEditorSettings(editor.getSettings());
setHighlighting(editor, fileExtension);
return editor;
}

private static void setHighlighting(EditorEx editor, String fileExtension) {
FileType cssFileType = FileTypeManager.getInstance().getFileTypeByExtension(fileExtension);
if (cssFileType != UnknownFileType.INSTANCE) {
EditorHighlighter editorHighlighter = HighlighterFactory.createHighlighter(cssFileType, EditorColorsManager.getInstance().getGlobalScheme(), null);
editor.setHighlighter(editorHighlighter);
}
}

private static void fillEditorSettings(EditorSettings editorSettings) {
editorSettings.setWhitespacesShown(false);
editorSettings.setLineMarkerAreaShown(false);
editorSettings.setIndentGuidesShown(false);
editorSettings.setLineNumbersShown(true);
editorSettings.setFoldingOutlineShown(false);
editorSettings.setAdditionalColumnsCount(1);
editorSettings.setAdditionalLinesCount(1);
editorSettings.setUseSoftWraps(false);
}
}

0 comments on commit 357d776

Please sign in to comment.