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);
65 filesList.add(new WikiFile(file, wikiTitle));
67 wikiNames.add(wikiTitle);
72 if (filesList.size() > 0) {
73 Collections.sort(filesList);
74 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
75 .getActiveWorkbenchWindow().getShell(), filesList, new ListContentProvider(), new LabelProvider(),
76 "Select the links for file creation:");
77 listSelectionDialog.setTitle("Links found in this article");
78 if (listSelectionDialog.open() == Window.OK) {
79 Object[] locations = listSelectionDialog.getResult();
80 if (locations.length > 0) {
81 IFile[] files = new IFile[locations.length];
82 String[] wikiTitles = new String[locations.length];
83 for (int i = 0; i < files.length; i++) {
84 files[i] = ((WikiFile) locations[i]).file;
85 wikiTitles[i] = ((WikiFile) locations[i]).wikiTitle;
88 Job job = new CreateFilesJob(files, wikiTitles);
89 // job.setRule(createRule(files));
103 private ArrayList getLinksStartingPosition(IDocument doc) {
104 ArrayList startPositionList = new ArrayList();
105 char[] text = doc.get().toCharArray();
121 if (ch == ']' && startPos != (-1)) {
122 startPositionList.add(new Integer(startPos));
132 } catch (IndexOutOfBoundsException e) {
135 return startPositionList;