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 javax.swing.JComponent;
034: import javax.swing.JFrame;
035: import javax.swing.UIManager;
036: import javax.swing.WindowConstants;
037:
038: import com.jgoodies.forms.builder.PanelBuilder;
039: import com.jgoodies.forms.layout.CellConstraints;
040: import com.jgoodies.forms.layout.FormLayout;
041: import com.jgoodies.validation.tutorial.util.TutorialUtils;
042:
043: /**
044: * Demonstrates and compares approaches to improving validation performance.
045: *
046: * @author Karsten Lentzsch
047: * @version $Revision: 1.8 $
048: */
049: public final class PerformanceExample {
050:
051: private final PerformanceSubExample immediateSetup;
052: private final PerformanceSubExample delayedSetup;
053: private final PerformanceSubExample backgroundSetup;
054: private final PerformanceSubExample preValidationSetup;
055: private final PerformanceSubExample delayedPreValidationSetup;
056: private final PerformanceSubExample cachingSetup;
057: private final PerformanceSubExample allTogetherSetup;
058:
059: public static void main(String[] args) {
060: try {
061: UIManager
062: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
063: } catch (Exception e) {
064: // Likely Plastic is not in the classpath; ignore it.
065: }
066: JFrame frame = new JFrame();
067: frame.setTitle("Performance");
068: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
069: JComponent panel = new PerformanceExample().buildPanel();
070: frame.getContentPane().add(panel);
071: frame.pack();
072: TutorialUtils.locateOnOpticalScreenCenter(frame);
073: frame.setVisible(true);
074: }
075:
076: // Instance Creation ******************************************************
077:
078: public PerformanceExample() {
079: int delay = 250; // ms
080: boolean coalesce = true;
081:
082: immediateSetup = new PerformanceSubExample(
083: "Immediate Validation", new ContainerValidator(
084: "immediate"));
085: immediateSetup
086: .addContainerChangeListener(new ValidationHandler(
087: immediateSetup));
088:
089: delayedSetup = new PerformanceSubExample("Delayed Validation",
090: new ContainerValidator("delayed"));
091: delayedSetup
092: .addContainerChangeListener(new ValidationHandlerDelayed(
093: delayedSetup, delay, coalesce));
094:
095: backgroundSetup = new PerformanceSubExample(
096: "Delay && Background Validation",
097: new ContainerValidator("background"));
098: backgroundSetup
099: .addContainerChangeListener(new ValidationHandlerInBackground(
100: backgroundSetup, delay, coalesce));
101:
102: preValidationSetup = new PerformanceSubExample(
103: "Pre-Validation (on the client)",
104: new ContainerValidatorWithPreValidation("preValidation"));
105: preValidationSetup
106: .addContainerChangeListener(new ValidationHandler(
107: preValidationSetup));
108:
109: delayedPreValidationSetup = new PerformanceSubExample(
110: "Delay && Pre-Validation",
111: new ContainerValidatorWithPreValidation(
112: "delayedPreValidation"));
113: delayedPreValidationSetup
114: .addContainerChangeListener(new ValidationHandlerDelayed(
115: delayedPreValidationSetup, delay, coalesce));
116:
117: cachingSetup = new PerformanceSubExample(
118: "Caching && Pre-Validation",
119: new ContainerValidatorWithCache("cache"));
120: cachingSetup.addContainerChangeListener(new ValidationHandler(
121: cachingSetup));
122:
123: allTogetherSetup = new PerformanceSubExample(
124: "Delay, Background, Pre-Validation, Cache",
125: new ContainerValidatorWithCache("allTogether"));
126: allTogetherSetup
127: .addContainerChangeListener(new ValidationHandlerInBackground(
128: allTogetherSetup, delay, coalesce));
129: }
130:
131: // Building ***************************************************************
132:
133: /**
134: * Builds and returns a panel that combines all setups.
135: */
136: public JComponent buildPanel() {
137: FormLayout layout = new FormLayout("fill:pref:grow",
138: "p, 9dlu, p, 9dlu, p, 9dlu, p, 9dlu, p, 9dlu, p, 9dlu, p");
139:
140: PanelBuilder builder = new PanelBuilder(layout);
141: builder.setDefaultDialogBorder();
142: CellConstraints cc = new CellConstraints();
143:
144: builder.add(immediateSetup.buildPanel(), cc.xy(1, 1));
145: builder.add(delayedSetup.buildPanel(), cc.xy(1, 3));
146: builder.add(backgroundSetup.buildPanel(), cc.xy(1, 5));
147: builder.add(preValidationSetup.buildPanel(), cc.xy(1, 7));
148: builder
149: .add(delayedPreValidationSetup.buildPanel(), cc
150: .xy(1, 9));
151: builder.add(cachingSetup.buildPanel(), cc.xy(1, 11));
152: builder.add(allTogetherSetup.buildPanel(), cc.xy(1, 13));
153:
154: return builder.getPanel();
155: }
156:
157: }
|