1 package net.sourceforge.phpeclipse.wiki.actions;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
7 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
9 import org.eclipse.core.resources.IFile;
10 import org.eclipse.core.runtime.jobs.Job;
11 import org.eclipse.jface.text.IDocument;
12 import org.eclipse.jface.viewers.LabelProvider;
13 import org.eclipse.jface.window.Window;
14 import org.eclipse.ui.IFileEditorInput;
15 import org.eclipse.ui.dialogs.ListSelectionDialog;
16 import org.eclipse.ui.internal.dialogs.ListContentProvider;
18 public final class CreateFilesFromLinksEditorAction extends OpenWikiLinkEditorAction {
20 class WikiFile implements Comparable {
25 public WikiFile(IFile f, String title) {
33 * @see java.lang.Object#toString()
35 public String toString() {
36 return wikiTitle + " - " + file.getProjectRelativePath().toString();
39 * @see java.lang.Comparable#compareTo(java.lang.Object)
41 public int compareTo(Object o) {
42 return wikiTitle.compareTo(((WikiFile)o).wikiTitle);
46 public void openWikiLinkOnSelection() {
47 IDocument doc = getDocument();
48 ArrayList startPositionList = getLinksStartingPosition(doc);
50 HashSet wikiNames = new HashSet();
51 ArrayList filesList = new ArrayList();
52 ArrayList wikiList = new ArrayList();
55 IFile currentFile = ((IFileEditorInput) editor.getEditorInput()).getFile();
57 for (int i = 0; i < startPositionList.size(); i++) {
58 posInteger = (Integer) startPositionList.get(i);
59 wikiTitle = getWikiTitle(editor, doc, posInteger.intValue());
61 if (wikiTitle != null && !wikiTitle.equals("")) {
62 if (!wikiNames.contains(wikiTitle)) {
63 IFile file = getWikiFile(currentFile, wikiTitle);
64 filesList.add(new WikiFile(file, wikiTitle));
65 // wikiList.add(wikiTitle);
66 wikiNames.add(wikiTitle);
71 if (filesList.size() > 0) {
72 Collections.sort(filesList);
73 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
74 .getActiveWorkbenchWindow().getShell(), filesList, new ListContentProvider(), new LabelProvider(),
75 "Select the links for file creation:");
76 listSelectionDialog.setTitle("Links found in this article");
77 if (listSelectionDialog.open() == Window.OK) {
78 Object[] locations = listSelectionDialog.getResult();
79 if (locations.length > 0) {
80 IFile[] files = new IFile[locations.length];
81 String[] wikiTitles = new String[locations.length];
82 for (int i = 0; i < files.length; i++) {
83 files[i] = ((WikiFile) locations[i]).file;
84 wikiTitles[i] = ((WikiFile) locations[i]).wikiTitle;
87 Job job = new CreateFilesJob(files, wikiTitles);
88 // job.setRule(createRule(files));
102 private ArrayList getLinksStartingPosition(IDocument doc) {
103 ArrayList startPositionList = new ArrayList();
104 char[] text = doc.get().toCharArray();
120 if (ch == ']' && startPos != (-1)) {
121 startPositionList.add(new Integer(startPos));
131 } catch (IndexOutOfBoundsException e) {
134 return startPositionList;