1 7 8 package org.jdesktop.swing.data; 9 10 import org.jdesktop.swing.data.ConversionException; 11 12 import org.jdesktop.swing.event.MessageSourceSupport; 13 import org.jdesktop.swing.event.MessageEvent; 14 import org.jdesktop.swing.event.MessageListener; 15 import org.jdesktop.swing.event.MessageSource; 16 import org.jdesktop.swing.event.ProgressEvent; 17 import org.jdesktop.swing.event.ProgressListener; 18 import org.jdesktop.swing.event.ProgressSource; 19 20 import java.io.InputStream ; 21 import java.io.IOException ; 22 23 import java.util.ArrayList ; 24 25 import javax.swing.SwingUtilities ; 26 27 28 82 public abstract class DataLoader implements ProgressSource, MessageSource { 83 84 public static final String READER_PRIORITY_KEY = "swingx.readpriority"; 85 private LoadNotifier loadNotifier; 86 private MessageSourceSupport mss; 87 88 protected DataLoader() { 89 mss = new MessageSourceSupport(this); 90 } 91 92 96 public void addProgressListener(ProgressListener l) { 97 mss.addProgressListener(l); 98 } 99 100 104 public void removeProgressListener(ProgressListener l) { 105 mss.removeProgressListener(l); 106 } 107 108 112 public ProgressListener[] getProgressListeners() { 113 return mss.getProgressListeners(); 114 } 115 116 120 public void addMessageListener(MessageListener l) { 121 mss.addMessageListener(l); 122 } 123 124 128 public void removeMessageListener(MessageListener l) { 129 mss.removeMessageListener(l); 130 } 131 132 136 public MessageListener[] getMessageListeners() { 137 return mss.getMessageListeners(); 138 } 139 140 148 public abstract void loadMetaData(Object model, InputStream is) 149 throws IOException ; 150 151 165 public void startLoading(final Object model, final InputStream is) { 166 loadNotifier = new LoadNotifier(this, model); 167 Runnable task = new Runnable () { 168 public void run() { 169 try { 170 readData(is); 171 } 172 catch (Exception e) { 173 final Throwable error = e; 174 SwingUtilities.invokeLater(new Runnable () { 175 public void run() { 176 fireException(error); 177 } 178 }); 179 } 180 } 181 }; 182 Thread readerThread = new Thread (task); 183 readerThread.setPriority(getReaderThreadPriority()); 184 fireProgressStarted(1,1); readerThread.start(); 186 } 187 188 protected int getReaderThreadPriority() { 189 String priority = System.getProperty(READER_PRIORITY_KEY); 190 if (priority != null) { 191 try { 192 int prio = Integer.parseInt(priority); 193 return Math.max(Thread.MIN_PRIORITY, prio); 195 } catch (Exception ex) { 196 } 198 199 } 200 return Thread.MIN_PRIORITY; 201 } 202 203 219 protected abstract void readData(InputStream is) throws IOException , ConversionException; 220 221 248 protected abstract void loadData(Object model); 249 250 261 protected void scheduleLoad() { 262 synchronized (loadNotifier) { 263 if (!loadNotifier.isPending()) { 264 loadNotifier.setPending(true); 265 SwingUtilities.invokeLater(loadNotifier); 266 } 267 } 268 } 269 270 278 protected void fireProgressStarted(int minimum, int maximum) { 279 mss.fireProgressStarted(minimum, maximum); 280 } 281 282 287 protected void fireProgressIncremented(int progress) { 288 mss.fireProgressIncremented(progress); 289 } 290 291 294 protected void fireProgressEnded() { 295 mss.fireProgressEnded(); 296 } 297 298 protected void fireException(Throwable t) { 299 mss.fireException(t); 300 } 301 302 protected void fireMessage(String message) { 303 mss.fireMessage(message); 304 } 305 306 private class LoadNotifier 307 implements Runnable { 308 private DataLoader loader; 309 private Object model; 310 private boolean pending = false; 311 312 LoadNotifier(DataLoader loader, Object model) { 313 this.loader = loader; 314 this.model = model; 315 } 316 317 public synchronized void setPending(boolean pending) { 318 this.pending = pending; 319 } 320 321 public synchronized boolean isPending() { 322 return pending; 323 } 324 325 public void run() { 326 synchronized (this) { 327 loader.loadData(model); 328 setPending(false); 329 } 330 } 331 } 332 333 } 334 | Popular Tags |