initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / NewPostBlogEditorAction.java
diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/NewPostBlogEditorAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/NewPostBlogEditorAction.java
new file mode 100644 (file)
index 0000000..c402bed
--- /dev/null
@@ -0,0 +1,136 @@
+package net.sourceforge.phpeclipse.wiki.actions;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import net.sourceforge.phpeclipse.wiki.blog.Configuration;
+import net.sourceforge.phpeclipse.wiki.blog.MetaWeblog;
+import net.sourceforge.phpeclipse.wiki.builder.CreatePageAction;
+import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
+import net.sourceforge.phpeclipse.wiki.preferences.Util;
+import net.sourceforge.phpeclipse.wiki.renderer.IContentRenderer;
+import net.sourceforge.phpeclipse.wiki.renderer.RendererFactory;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.text.TextSelection;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.ui.IEditorActionDelegate;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IFileEditorInput;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.texteditor.AbstractTextEditor;
+
+public final class NewPostBlogEditorAction implements IEditorActionDelegate {
+  //  public static String APPKEY =
+  // "1c0c75ffffffb512ffffff9575ffffff97ffffffd2ffffff87ffffff91ffffffe41dffffffc5320cffffffab544effffffc0546459ffffff83";
+
+  private IWorkbenchWindow window;
+
+  private AbstractTextEditor fEditor;
+
+  public void dispose() {
+  }
+
+  public void init(IWorkbenchWindow window) {
+    this.window = window;
+  }
+
+  public void selectionChanged(IAction action, ISelection selection) {
+    if (selection.isEmpty()) {
+      return;
+    }
+    if (selection instanceof TextSelection) {
+      action.setEnabled(true);
+      return;
+    }
+    if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
+      action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
+    }
+  }
+
+  public void run(IAction action) {
+    if (fEditor == null) {
+      IEditorPart targetEditor = window.getActivePage().getActiveEditor();
+      if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
+        fEditor = (AbstractTextEditor) targetEditor;
+      }
+    }
+    if (fEditor != null) {
+      try {
+        Configuration config = new Configuration("http://localhost:8080/snip/RPC2", "1", "admin", "admin");
+        IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
+        IFile file = ei.getFile();
+        StringBuffer htmlBuffer = new StringBuffer();
+        CreatePageAction.getWikiBuffer(htmlBuffer,file);
+
+        ArrayList images = new ArrayList();
+        getImages(htmlBuffer, images);
+
+        String[] result = new String[2];
+        boolean cache = config.promptForPassword(config.getUser(), "Insert Config", true, result);
+        if (result[0] == null || result[1] == null) {
+          return;
+        }
+        if (result[0].equals("") || result[1].equals("")) {
+          return;
+        }
+
+        String title = Util.getWikiTitle(file);
+        if (title != null) {
+          MetaWeblog metaWebLog = new MetaWeblog(config);
+          String guid = metaWebLog.newPost(file, title, htmlBuffer, true);
+          System.out.println(guid);
+
+          if (images.size() > 0) {
+            String fullImagePath;
+            String filePath = file.getLocation().toString();
+            int index = filePath.lastIndexOf('/');
+            if (index>=0) {
+              filePath = filePath.substring(0,index+1);
+            }
+            for (int i = 0; i < images.size(); i++) {
+              fullImagePath = filePath+"Image/"+images.get(i).toString();
+              metaWebLog.publishAttachement(guid, fullImagePath, false);
+            }
+          }
+        } else {
+          MessageDialog.openError(null, "Undefined Blog Title: ", "Blog file name must end with *.wp");
+        }
+      } catch (Exception e) {
+        MessageDialog.openError(null, "Exception: ", e.toString());
+        e.printStackTrace();
+      }
+    }
+  }
+
+  /**
+   * @param content the wikitext
+   * @param images result List of image names
+   */
+  private void getImages(StringBuffer content, List images) {
+    int startIndex = 0;
+    int endIndex = 0;
+    String imageName;
+    while (startIndex >= 0) {
+      startIndex = content.indexOf("[[Image:", startIndex);
+      if (startIndex >= 0) {
+        endIndex = content.indexOf("]]", startIndex + 8);
+        if (endIndex < 0) {
+          return;
+        }
+        imageName = content.substring(startIndex + 8, endIndex);
+        images.add(imageName);
+        startIndex += 8;
+      }
+    }
+  }
+
+  public void setActiveEditor(IAction action, IEditorPart targetEditor) {
+    if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
+      fEditor = (AbstractTextEditor) targetEditor;
+    }
+  }
+
+}
\ No newline at end of file