1 package net.sourceforge.phpeclipse.wiki.actions;
3 import java.util.ArrayList;
6 import net.sourceforge.phpeclipse.wiki.blog.Configuration;
7 import net.sourceforge.phpeclipse.wiki.blog.MetaWeblog;
8 import net.sourceforge.phpeclipse.wiki.builder.CreatePageAction;
9 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
10 import net.sourceforge.phpeclipse.wiki.preferences.Util;
11 import net.sourceforge.phpeclipse.wiki.renderer.IContentRenderer;
12 import net.sourceforge.phpeclipse.wiki.renderer.RendererFactory;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.text.TextSelection;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.ui.IEditorActionDelegate;
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.IFileEditorInput;
22 import org.eclipse.ui.IWorkbenchWindow;
23 import org.eclipse.ui.texteditor.AbstractTextEditor;
25 public final class NewPostBlogEditorAction implements IEditorActionDelegate {
26 // public static String APPKEY =
27 // "1c0c75ffffffb512ffffff9575ffffff97ffffffd2ffffff87ffffff91ffffffe41dffffffc5320cffffffab544effffffc0546459ffffff83";
29 private IWorkbenchWindow window;
31 private AbstractTextEditor fEditor;
33 public void dispose() {
36 public void init(IWorkbenchWindow window) {
40 public void selectionChanged(IAction action, ISelection selection) {
41 if (selection.isEmpty()) {
44 if (selection instanceof TextSelection) {
45 action.setEnabled(true);
48 if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
49 action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
53 public void run(IAction action) {
54 if (fEditor == null) {
55 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
56 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
57 fEditor = (AbstractTextEditor) targetEditor;
60 if (fEditor != null) {
62 Configuration config = new Configuration("http://localhost:8080/snip/RPC2", "1", "admin", "admin");
63 IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
64 IFile file = ei.getFile();
65 StringBuffer htmlBuffer = new StringBuffer();
66 CreatePageAction.getWikiBuffer(htmlBuffer,file);
68 ArrayList images = new ArrayList();
69 getImages(htmlBuffer, images);
71 String[] result = new String[2];
72 boolean cache = config.promptForPassword(config.getUser(), "Insert Config", true, result);
73 if (result[0] == null || result[1] == null) {
76 if (result[0].equals("") || result[1].equals("")) {
80 String title = Util.getWikiTitle(file);
82 MetaWeblog metaWebLog = new MetaWeblog(config);
83 String guid = metaWebLog.newPost(file, title, htmlBuffer, true);
84 System.out.println(guid);
86 if (images.size() > 0) {
88 String filePath = file.getLocation().toString();
89 int index = filePath.lastIndexOf('/');
91 filePath = filePath.substring(0,index+1);
93 for (int i = 0; i < images.size(); i++) {
94 fullImagePath = filePath+"Image/"+images.get(i).toString();
95 metaWebLog.publishAttachement(guid, fullImagePath, false);
99 MessageDialog.openError(null, "Undefined Blog Title: ", "Blog file name must end with *.wp");
101 } catch (Exception e) {
102 MessageDialog.openError(null, "Exception: ", e.toString());
109 * @param content the wikitext
110 * @param images result List of image names
112 private void getImages(StringBuffer content, List images) {
116 while (startIndex >= 0) {
117 startIndex = content.indexOf("[[Image:", startIndex);
118 if (startIndex >= 0) {
119 endIndex = content.indexOf("]]", startIndex + 8);
123 imageName = content.substring(startIndex + 8, endIndex);
124 images.add(imageName);
130 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
131 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
132 fEditor = (AbstractTextEditor) targetEditor;