1 11 package org.eclipse.help.internal.search; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.OperationCanceledException; 21 import org.eclipse.help.internal.base.BaseHelpSystem; 22 import org.eclipse.help.internal.base.HelpBasePlugin; 23 24 29 public class SearchProgressMonitor implements IProgressMonitor { 30 31 protected static Map progressMonitors = new HashMap (); 33 34 protected static ISearchHitCollector dummy_collector; 36 37 private boolean started, done, canceled; 38 39 private int totalWork = IProgressMonitor.UNKNOWN; 40 41 private double currWork; 42 43 static { 44 dummy_collector = new ISearchHitCollector() { 45 public void addHits(List hits, String s) { 46 } 47 }; 48 } 49 50 53 public SearchProgressMonitor() { 54 started = done = canceled = false; 55 } 56 57 public void beginTask(String name, int totalWork) { 58 this.totalWork = totalWork; 59 this.started = true; 60 } 61 62 public void done() { 63 currWork = totalWork; 64 this.done = true; 65 this.started = true; 66 } 67 68 public void setTaskName(String name) { 69 } 70 71 public void subTask(String name) { 72 } 73 74 public void worked(int work) { 75 internalWorked(work); 76 } 77 78 public void internalWorked(double work) { 79 currWork += work; 80 if (currWork > totalWork) 81 currWork = totalWork; 82 else if (currWork < 0) 83 currWork = 0; 84 } 85 86 public int getPercentage() { 87 if (done) { 88 return 100; 89 } 90 if (totalWork == IProgressMonitor.UNKNOWN) 91 return 0; 92 if (currWork >= totalWork) 93 return 100; 94 return (int)(100 * currWork / totalWork); 95 } 96 97 102 public boolean isCanceled() { 103 return canceled; 104 } 105 106 109 public void started() { 110 this.started = true; 111 } 112 113 118 public boolean isStarted() { 119 return started; 120 } 121 122 127 public boolean isDone() { 128 return done; 129 } 130 131 137 public void setCanceled(boolean canceled) { 138 this.canceled = canceled; 139 } 140 141 144 public static synchronized SearchProgressMonitor getProgressMonitor( 145 final String locale) { 146 147 if (progressMonitors.get(locale) != null) 149 return (SearchProgressMonitor) progressMonitors.get(locale); 150 151 final SearchProgressMonitor pm = new SearchProgressMonitor(); 152 progressMonitors.put(locale, pm); 153 154 Thread indexer = new Thread (new Runnable () { 156 public void run() { 157 try { 158 BaseHelpSystem.getSearchManager().search( 159 new DummySearchQuery(locale), dummy_collector, pm); 160 } catch (OperationCanceledException oce) { 161 progressMonitors.remove(locale); 164 } catch (Exception e) { 165 progressMonitors.remove(locale); 166 if (HelpBasePlugin.getDefault() != null) { 167 HelpBasePlugin 168 .logError( 169 "Problem occurred during indexing of documentation.", e); 171 } else { 172 } 174 } 175 } 176 }); 177 indexer.setName("HelpSearchIndexer"); indexer.start(); 179 while (!pm.isStarted()) { 182 try { 183 Thread.sleep(50); 184 } catch (InterruptedException ie) { 185 } 186 if (progressMonitors.get(locale) == null) 187 break; 189 } 190 191 return pm; 192 } 193 194 static class DummySearchQuery implements ISearchQuery { 195 private String l; 196 197 DummySearchQuery(String loc) { 198 l = loc; 199 } 200 201 204 public Collection getFieldNames() { 205 return new ArrayList (); 206 } 207 208 211 public String getSearchWord() { 212 return "dummy"; } 214 215 219 public boolean isFieldSearch() { 220 return false; 221 } 222 223 226 public String getLocale() { 227 return l; 228 } 229 } 230 231 public synchronized static void reinit(String locale) { 232 progressMonitors.remove(locale); 233 } 234 235 } 236 | Popular Tags |