Improved Templates i.e.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / blogwiki / NewPostBlogHTMLAction.java
1 package net.sourceforge.phpeclipse.wiki.actions.blogwiki;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
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.editor.WikiEditorPlugin;
11 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
12 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
13 import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
14 import net.sourceforge.phpeclipse.wiki.preferences.Util;
15 import net.sourceforge.phpeclipse.wiki.renderer.IContentRenderer;
16 import net.sourceforge.phpeclipse.wiki.renderer.RendererFactory;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.text.TextSelection;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.jface.window.Window;
25 import org.eclipse.ui.IEditorActionDelegate;
26 import org.eclipse.ui.IEditorPart;
27 import org.eclipse.ui.IFileEditorInput;
28 import org.eclipse.ui.IWorkbenchWindow;
29 import org.eclipse.ui.dialogs.ListSelectionDialog;
30 import org.eclipse.ui.internal.dialogs.ListContentProvider;
31 import org.eclipse.ui.texteditor.AbstractTextEditor;
32
33 public final class NewPostBlogHTMLAction implements IEditorActionDelegate {
34   //  public static String APPKEY =
35   // "1c0c75ffffffb512ffffff9575ffffff97ffffffd2ffffff87ffffff91ffffffe41dffffffc5320cffffffab544effffffc0546459ffffff83";
36
37   private IWorkbenchWindow window;
38
39   private AbstractTextEditor fEditor;
40
41   public void dispose() {
42   }
43
44   public void init(IWorkbenchWindow window) {
45     this.window = window;
46   }
47
48   public void selectionChanged(IAction action, ISelection selection) {
49     if (selection.isEmpty()) {
50       return;
51     }
52     if (selection instanceof TextSelection) {
53       action.setEnabled(true);
54       return;
55     }
56     if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
57       action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
58     }
59   }
60
61   protected Configuration getConfiguration(){
62     List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
63     ArrayList configsList = new ArrayList();
64     for (int i = 0; i < allConfigsList.size(); i++) {
65       IConfiguration temp = (IConfiguration) allConfigsList.get(i);
66       if (temp.getType().equals(WikiEditorPlugin.BLOG_A_HTML)) {
67         configsList.add(temp);
68       }
69     }
70     Collections.sort(configsList);
71     Configuration configuration = null;
72     ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
73         .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
74         "Select blog configuration for your wiki HTMl text.");
75     listSelectionDialog.setTitle("Multiple active configuration found");
76     if (listSelectionDialog.open() == Window.OK) {
77       Object[] locations = listSelectionDialog.getResult();
78       if (locations != null) {
79         for (int i = 0; i < locations.length; i++) {
80           configuration = (Configuration) locations[i];
81           break;
82         }
83       }
84     }
85     return configuration;
86   }
87   
88   public void run(IAction action) {
89     if (fEditor == null) {
90       IEditorPart targetEditor = window.getActivePage().getActiveEditor();
91       if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
92         fEditor = (AbstractTextEditor) targetEditor;
93       }
94     }
95     if (fEditor != null) {
96       try {
97         Configuration config = getConfiguration();
98         IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
99         IFile file = ei.getFile();
100         IContentRenderer renderer = RendererFactory.createContentRenderer(file.getProject());
101         StringBuffer htmlBuffer = new StringBuffer();
102         CreatePageAction.convertWikiBuffer(null, htmlBuffer, file, renderer, false);
103
104         String[] result = new String[2];
105         boolean cache = config.promptForPassword(config.getUser(), "Insert Config", true, result);
106         if (result[0] == null || result[1] == null) {
107           return;
108         }
109         if (result[0].equals("") || result[1].equals("") ) {
110           return;
111         }
112         
113         String title = Util.getWikiTitle(file);
114         if (title != null) {
115           MetaWeblog metaWebLog = new MetaWeblog(config);
116           String guid = metaWebLog.newPost(file, title, htmlBuffer, true);
117           System.out.println(guid);
118         } else {
119           MessageDialog.openError(null, "Undefined Blog Title: ", "Blog file name must end with *.wp");
120         }
121       } catch (Exception e) {
122         MessageDialog.openError(null, "Exception: ", e.toString());
123         e.printStackTrace();
124       }
125       //      try {
126       //        IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
127       //        IFile file = ei.getFile();
128       //        IContentRenderer renderer = RendererFactory.createContentRenderer(file.getProject());
129       //        StringBuffer htmlBuffer = new StringBuffer();
130       //        CreatePageAction.convertWikiBuffer(htmlBuffer, file, renderer, false);
131       //        XmlRpcClientLite xmlrpc = new XmlRpcClientLite("http://www.plog4u.de/xmlrpc.php");
132       //        Vector rpcParams = new Vector();
133       //        rpcParams.add(APPKEY);
134       //        rpcParams.add("1"); // blog.getBlogId());
135       //        rpcParams.add("admin");
136       //        rpcParams.add("******"); // fPassword
137       //        rpcParams.add(htmlBuffer.toString()); //getContent());
138       //        rpcParams.add(Boolean.TRUE); // publish == yes
139       //
140       //        String postId = (String) xmlrpc.execute("blogger.newPost", rpcParams);
141       //
142       //        // return postId;
143       //      } catch (Exception e) {
144       //        e.printStackTrace();
145       //        // return null;
146       //      }
147     }
148   }
149
150   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
151     if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
152       fEditor = (AbstractTextEditor) targetEditor;
153     }
154   }
155
156 }