1 package net.sourceforge.phpeclipse.wiki.actions.blogwiki;
3 import java.util.ArrayList;
4 import java.util.Collections;
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.ConfigurationWorkingCopy;
14 import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
15 import net.sourceforge.phpeclipse.wiki.preferences.Util;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.text.TextSelection;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.LabelProvider;
23 import org.eclipse.jface.window.Window;
24 import org.eclipse.ui.IEditorActionDelegate;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IFileEditorInput;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.dialogs.ListSelectionDialog;
29 import org.eclipse.ui.internal.dialogs.ListContentProvider;
30 import org.eclipse.ui.texteditor.AbstractTextEditor;
32 public final class NewPostBlogWikiAction implements IEditorActionDelegate {
33 // public static String APPKEY =
34 // "1c0c75ffffffb512ffffff9575ffffff97ffffffd2ffffff87ffffff91ffffffe41dffffffc5320cffffffab544effffffc0546459ffffff83";
36 private IWorkbenchWindow window;
38 private AbstractTextEditor fEditor;
40 public void dispose() {
43 public void init(IWorkbenchWindow window) {
47 public void selectionChanged(IAction action, ISelection selection) {
48 if (selection.isEmpty()) {
51 if (selection instanceof TextSelection) {
52 action.setEnabled(true);
55 if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
56 action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
60 protected ConfigurationWorkingCopy getConfiguration() {
61 List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
62 ArrayList configsList = new ArrayList();
63 for (int i = 0; i < allConfigsList.size(); i++) {
64 IConfiguration temp = (IConfiguration) allConfigsList.get(i);
65 if (temp.getType().equals(WikiEditorPlugin.BLOG_A_WIKI)) {
66 configsList.add(temp);
69 Collections.sort(configsList);
70 ConfigurationWorkingCopy configuration = null;
71 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
72 .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
73 "Select blog configuration for your wiki text.");
74 listSelectionDialog.setTitle("Multiple active configuration found");
75 if (listSelectionDialog.open() == Window.OK) {
76 Object[] locations = listSelectionDialog.getResult();
77 if (locations != null) {
78 for (int i = 0; i < locations.length; i++) {
79 configuration = (ConfigurationWorkingCopy) locations[i];
87 public void run(IAction action) {
88 if (fEditor == null) {
89 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
90 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
91 fEditor = (AbstractTextEditor) targetEditor;
94 if (fEditor != null) {
96 ConfigurationWorkingCopy config = getConfiguration(); //new Configuration("http://localhost:8080/snip/RPC2", "1", "admin",
99 IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
100 IFile file = ei.getFile();
101 StringBuffer htmlBuffer = new StringBuffer();
102 CreatePageAction.getWikiBuffer(htmlBuffer, file);
104 ArrayList images = new ArrayList();
105 getImages(htmlBuffer, images);
107 String[] result = new String[2];
108 cache = config.promptForPassword(config.getUser(), "Insert Config", true, result);
109 if (result[0] == null || result[1] == null) {
112 if (result[0].equals("") || result[1].equals("")) {
116 String title = Util.getWikiTitle(file);
118 config.setName(result[0]);
119 config.setPassword(result[1]);
120 MetaWeblog metaWebLog = new MetaWeblog(config);
121 String guid = metaWebLog.newPost(file, title, htmlBuffer, true);
122 System.out.println(guid);
124 if (images.size() > 0) {
125 String fullImagePath;
126 String filePath = file.getLocation().toString();
127 int index = filePath.lastIndexOf('/');
129 filePath = filePath.substring(0, index + 1);
131 for (int i = 0; i < images.size(); i++) {
132 fullImagePath = filePath + "Image/" + images.get(i).toString();
133 metaWebLog.publishAttachement(guid, fullImagePath, false);
137 MessageDialog.openError(null, "Undefined Blog Title: ", "Blog file name must end with *.wp");
139 } catch (Exception e) {
140 MessageDialog.openError(null, "Exception: ", e.toString());
143 if (!cache && config != null) {
145 config.setPassword("");
155 * result List of image names
157 private void getImages(StringBuffer content, List images) {
161 while (startIndex >= 0) {
162 startIndex = content.indexOf("[[Image:", startIndex);
163 if (startIndex >= 0) {
164 endIndex = content.indexOf("]]", startIndex + 8);
168 imageName = content.substring(startIndex + 8, endIndex);
169 images.add(imageName);
175 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
176 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
177 fEditor = (AbstractTextEditor) targetEditor;