Added list selection box for file creation from wiki links
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / CreateFilesFromLinksEditorAction.java
1 package net.sourceforge.phpeclipse.wiki.actions;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
6
7 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
8
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;
17
18 public final class CreateFilesFromLinksEditorAction extends OpenWikiLinkEditorAction {
19
20   class WikiFile implements Comparable {
21     IFile file;
22
23     String wikiTitle;
24
25     public WikiFile(IFile f, String title) {
26       file = f;
27       wikiTitle = title;
28     }
29
30     /*
31      * (non-Javadoc)
32      * 
33      * @see java.lang.Object#toString()
34      */
35     public String toString() {
36       return wikiTitle + " - " + file.getProjectRelativePath().toString();
37     }
38     /* (non-Javadoc)
39      * @see java.lang.Comparable#compareTo(java.lang.Object)
40      */
41     public int compareTo(Object o) {
42       return wikiTitle.compareTo(((WikiFile)o).wikiTitle);
43     }
44   }
45
46   public void openWikiLinkOnSelection() {
47     IDocument doc = getDocument();
48     ArrayList startPositionList = getLinksStartingPosition(doc);
49
50     HashSet wikiNames = new HashSet();
51     ArrayList filesList = new ArrayList();
52     ArrayList wikiList = new ArrayList();
53     String wikiTitle;
54     Integer posInteger;
55     IFile currentFile = ((IFileEditorInput) editor.getEditorInput()).getFile();
56
57     for (int i = 0; i < startPositionList.size(); i++) {
58       posInteger = (Integer) startPositionList.get(i);
59       wikiTitle = getWikiTitle(editor, doc, posInteger.intValue());
60
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);
67         }
68       }
69     }
70
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;
85           }
86           
87           Job job = new CreateFilesJob(files, wikiTitles);
88           //        job.setRule(createRule(files));
89           job.setRule(null);
90           job.setUser(true);
91           job.schedule();
92         }
93       }
94
95     }
96   }
97
98   /**
99    * @param doc
100    * @return
101    */
102   private ArrayList getLinksStartingPosition(IDocument doc) {
103     ArrayList startPositionList = new ArrayList();
104     char[] text = doc.get().toCharArray();
105     try {
106       char ch = ' ';
107       int i = 0;
108       int startPos = -1;
109       while (true) {
110         ch = text[i++];
111         switch (ch) {
112         case '[':
113           ch = text[i++];
114           if (ch == '[') {
115             startPos = i;
116           }
117           break;
118         case ']':
119           ch = text[i++];
120           if (ch == ']' && startPos != (-1)) {
121             startPositionList.add(new Integer(startPos));
122             startPos = -1;
123           }
124           break;
125         case '\r':
126         case '\n':
127           startPos = -1;
128           break;
129         }
130       }
131     } catch (IndexOutOfBoundsException e) {
132       // ignore it
133     }
134     return startPositionList;
135   }
136 }