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;
12 import org.eclipse.core.resources.IFile;
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.jface.text.TextSelection;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.ui.IEditorActionDelegate;
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.IFileEditorInput;
20 import org.eclipse.ui.IWorkbenchWindow;
21 import org.eclipse.ui.texteditor.AbstractTextEditor;
23 public final class NewPostBlogEditorAction implements IEditorActionDelegate {
24 // public static String APPKEY =
25 // "1c0c75ffffffb512ffffff9575ffffff97ffffffd2ffffff87ffffff91ffffffe41dffffffc5320cffffffab544effffffc0546459ffffff83";
27 private IWorkbenchWindow window;
29 private AbstractTextEditor fEditor;
31 public void dispose() {
34 public void init(IWorkbenchWindow window) {
38 public void selectionChanged(IAction action, ISelection selection) {
39 if (selection.isEmpty()) {
42 if (selection instanceof TextSelection) {
43 action.setEnabled(true);
46 if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
47 action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
51 public void run(IAction action) {
52 if (fEditor == null) {
53 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
54 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
55 fEditor = (AbstractTextEditor) targetEditor;
58 if (fEditor != null) {
60 Configuration config = new Configuration("http://localhost:8080/snip/RPC2", "1", "admin", "admin");
61 IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
62 IFile file = ei.getFile();
63 StringBuffer htmlBuffer = new StringBuffer();
64 CreatePageAction.getWikiBuffer(htmlBuffer,file);
66 ArrayList images = new ArrayList();
67 getImages(htmlBuffer, images);
69 String[] result = new String[2];
70 boolean cache = config.promptForPassword(config.getUser(), "Insert Config", true, result);
71 if (result[0] == null || result[1] == null) {
74 if (result[0].equals("") || result[1].equals("")) {
78 String title = Util.getWikiTitle(file);
80 MetaWeblog metaWebLog = new MetaWeblog(config);
81 String guid = metaWebLog.newPost(file, title, htmlBuffer, true);
82 System.out.println(guid);
84 if (images.size() > 0) {
86 String filePath = file.getLocation().toString();
87 int index = filePath.lastIndexOf('/');
89 filePath = filePath.substring(0,index+1);
91 for (int i = 0; i < images.size(); i++) {
92 fullImagePath = filePath+"Image/"+images.get(i).toString();
93 metaWebLog.publishAttachement(guid, fullImagePath, false);
97 MessageDialog.openError(null, "Undefined Blog Title: ", "Blog file name must end with *.wp");
99 } catch (Exception e) {
100 MessageDialog.openError(null, "Exception: ", e.toString());
107 * @param content the wikitext
108 * @param images result List of image names
110 private void getImages(StringBuffer content, List images) {
114 while (startIndex >= 0) {
115 startIndex = content.indexOf("[[Image:", startIndex);
116 if (startIndex >= 0) {
117 endIndex = content.indexOf("]]", startIndex + 8);
121 imageName = content.substring(startIndex + 8, endIndex);
122 images.add(imageName);
128 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
129 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
130 fEditor = (AbstractTextEditor) targetEditor;