001: /*
002: * Copyright (c) 2003-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.validation.tutorial.performance;
032:
033: import java.beans.PropertyChangeEvent;
034: import java.util.concurrent.ExecutionException;
035:
036: import org.jdesktop.swingworker.SwingWorker;
037:
038: import com.jgoodies.binding.beans.DelayedPropertyChangeHandler;
039: import com.jgoodies.validation.ValidationResult;
040:
041: /**
042: * Executes a given performance sub example in a background thread using a
043: * SwingWorker. Validates the container using the example's container validator,
044: * and adds the EDT time spent (should be close to 0).
045: *
046: * @author Karsten Lentzsch
047: * @version $Revision: 1.8 $
048: */
049: final class ValidationHandlerInBackground extends
050: DelayedPropertyChangeHandler {
051:
052: private final PerformanceSubExample subExample;
053:
054: /**
055: * Constructs a change handler that handles container no changes
056: * with a slight delay, saving unnecessary validation operations.
057: * The validation is performed in the background using a Swing Worker.
058: *
059: * @param subExample provides the container, validator, and result model
060: * @param delay the milliseconds to wait before the delayed property change
061: * will be performed
062: * @param coalesce <code>true</code> to coalesce all pending changes,
063: * <code>false</code> to fire changes with the delay when an update
064: * has been received
065: */
066: ValidationHandlerInBackground(PerformanceSubExample subExample,
067: int delay, boolean coalesce) {
068: super (delay, coalesce);
069: this .subExample = subExample;
070: }
071:
072: @Override
073: public void delayedPropertyChange(PropertyChangeEvent evt) {
074: long t0 = System.currentTimeMillis();
075: new ContainerNoWorker(subExample).execute();
076: long t1 = System.currentTimeMillis();
077: // The time spent will be close to 0.
078: subExample.addTime(t1 - t0);
079: }
080:
081: // Helper Class ***********************************************************
082:
083: static final class ContainerNoWorker extends
084: SwingWorker<ValidationResult, Object> {
085:
086: private final PerformanceSubExample subExample;
087:
088: ContainerNoWorker(PerformanceSubExample subExample) {
089: this .subExample = subExample;
090: }
091:
092: @Override
093: protected ValidationResult doInBackground() {
094: Container container = subExample.getContainer();
095: ContainerValidator validator = subExample
096: .getContainerValidator();
097: return validator.validate(container);
098: }
099:
100: @Override
101: protected void done() {
102: try {
103: subExample.getValidationResultModel().setResult(get());
104: } catch (InterruptedException e) {
105: e.printStackTrace();
106: } catch (ExecutionException e) {
107: e.getCause().printStackTrace();
108: }
109: }
110: }
111:
112: }
|