Enable word wrapping with preference key editor.wrap.words (false by default)
[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           if (!file.exists()) {
65             filesList.add(new WikiFile(file, wikiTitle));
66           }
67           wikiNames.add(wikiTitle);
68         }
69       }
70     }
71
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;
86           }
87           
88           Job job = new CreateFilesJob(files, wikiTitles);
89           //        job.setRule(createRule(files));
90           job.setRule(null);
91           job.setUser(true);
92           job.schedule();
93         }
94       }
95
96     }
97   }
98
99   /**
100    * @param doc
101    * @return
102    */
103   private ArrayList getLinksStartingPosition(IDocument doc) {
104     ArrayList startPositionList = new ArrayList();
105     char[] text = doc.get().toCharArray();
106     try {
107       char ch = ' ';
108       int i = 0;
109       int startPos = -1;
110       while (true) {
111         ch = text[i++];
112         switch (ch) {
113         case '[':
114           ch = text[i++];
115           if (ch == '[') {
116             startPos = i;
117           }
118           break;
119         case ']':
120           ch = text[i++];
121           if (ch == ']' && startPos != (-1)) {
122             startPositionList.add(new Integer(startPos));
123             startPos = -1;
124           }
125           break;
126         case '\r':
127         case '\n':
128           startPos = -1;
129           break;
130         }
131       }
132     } catch (IndexOutOfBoundsException e) {
133       // ignore it
134     }
135     return startPositionList;
136   }
137 }