initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / NewPostBlogEditorAction.java
1 package net.sourceforge.phpeclipse.wiki.actions;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
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;
13
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;
24
25 public final class NewPostBlogEditorAction implements IEditorActionDelegate {
26   //  public static String APPKEY =
27   // "1c0c75ffffffb512ffffff9575ffffff97ffffffd2ffffff87ffffff91ffffffe41dffffffc5320cffffffab544effffffc0546459ffffff83";
28
29   private IWorkbenchWindow window;
30
31   private AbstractTextEditor fEditor;
32
33   public void dispose() {
34   }
35
36   public void init(IWorkbenchWindow window) {
37     this.window = window;
38   }
39
40   public void selectionChanged(IAction action, ISelection selection) {
41     if (selection.isEmpty()) {
42       return;
43     }
44     if (selection instanceof TextSelection) {
45       action.setEnabled(true);
46       return;
47     }
48     if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
49       action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
50     }
51   }
52
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;
58       }
59     }
60     if (fEditor != null) {
61       try {
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);
67
68         ArrayList images = new ArrayList();
69         getImages(htmlBuffer, images);
70
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) {
74           return;
75         }
76         if (result[0].equals("") || result[1].equals("")) {
77           return;
78         }
79
80         String title = Util.getWikiTitle(file);
81         if (title != null) {
82           MetaWeblog metaWebLog = new MetaWeblog(config);
83           String guid = metaWebLog.newPost(file, title, htmlBuffer, true);
84           System.out.println(guid);
85
86           if (images.size() > 0) {
87             String fullImagePath;
88             String filePath = file.getLocation().toString();
89             int index = filePath.lastIndexOf('/');
90             if (index>=0) {
91               filePath = filePath.substring(0,index+1);
92             }
93             for (int i = 0; i < images.size(); i++) {
94               fullImagePath = filePath+"Image/"+images.get(i).toString();
95               metaWebLog.publishAttachement(guid, fullImagePath, false);
96             }
97           }
98         } else {
99           MessageDialog.openError(null, "Undefined Blog Title: ", "Blog file name must end with *.wp");
100         }
101       } catch (Exception e) {
102         MessageDialog.openError(null, "Exception: ", e.toString());
103         e.printStackTrace();
104       }
105     }
106   }
107
108   /**
109    * @param content the wikitext
110    * @param images result List of image names
111    */
112   private void getImages(StringBuffer content, List images) {
113     int startIndex = 0;
114     int endIndex = 0;
115     String imageName;
116     while (startIndex >= 0) {
117       startIndex = content.indexOf("[[Image:", startIndex);
118       if (startIndex >= 0) {
119         endIndex = content.indexOf("]]", startIndex + 8);
120         if (endIndex < 0) {
121           return;
122         }
123         imageName = content.substring(startIndex + 8, endIndex);
124         images.add(imageName);
125         startIndex += 8;
126       }
127     }
128   }
129
130   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
131     if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
132       fEditor = (AbstractTextEditor) targetEditor;
133     }
134   }
135
136 }