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.PropertyChangeListener;
034:
035: import javax.swing.JComponent;
036: import javax.swing.JFormattedTextField;
037: import javax.swing.JTextField;
038:
039: import com.jgoodies.binding.adapter.BasicComponentFactory;
040: import com.jgoodies.binding.beans.BeanAdapter;
041: import com.jgoodies.binding.value.ValueHolder;
042: import com.jgoodies.forms.builder.PanelBuilder;
043: import com.jgoodies.forms.layout.CellConstraints;
044: import com.jgoodies.forms.layout.FormLayout;
045: import com.jgoodies.validation.ValidationResultModel;
046: import com.jgoodies.validation.tutorial.util.IconFeedbackPanel;
047: import com.jgoodies.validation.util.DefaultValidationResultModel;
048: import com.jgoodies.validation.view.ValidationComponentUtils;
049:
050: /**
051: * Provides the validation result model and actions necessary to
052: * build a presentation.
053: *
054: * @author Karsten Lentzsch
055: * @version $Revision: 1.7 $
056: */
057: final class PerformanceSubExample {
058:
059: private final String title;
060: private final Container container;
061: private final ContainerValidator validator;
062: private final ValidationResultModel validationResultModel;
063:
064: private final ValueHolder timeModel;
065:
066: private JTextField containerNoField;
067: private JFormattedTextField timeField;
068:
069: // Instance Creation -------------------------------------
070:
071: PerformanceSubExample(String title, ContainerValidator validator) {
072: this .title = title;
073: this .container = new Container();
074: this .validator = validator;
075: this .validationResultModel = new DefaultValidationResultModel();
076: timeModel = new ValueHolder(0L);
077: initComponents();
078: }
079:
080: // Building ---------------------------------------------------
081:
082: /**
083: * Creates and binds the UI components.
084: */
085: private void initComponents() {
086: BeanAdapter<Container> adapter = new BeanAdapter<Container>(
087: container, true);
088: containerNoField = BasicComponentFactory
089: .createTextField(
090: adapter
091: .getValueModel(Container.PROPERTYNAME_CONTAINER_NO),
092: false);
093: String messageKey = validator.getPrefix() + ".Container No";
094: ValidationComponentUtils.setMessageKey(containerNoField,
095: messageKey);
096:
097: timeField = BasicComponentFactory.createLongField(timeModel);
098: timeField.setEditable(false);
099: timeField.setFocusable(false);
100: }
101:
102: /**
103: * Builds and returns the whole editor with 3 sections
104: * for the different validation times.
105: */
106: public JComponent buildPanel() {
107: FormLayout layout = new FormLayout(
108: "right:max(65dlu;pref), 4dlu, 40dlu, 14dlu, pref, 4dlu, 40dlu, 1dlu, pref, 0:grow",
109: "p, 3dlu, p, 4px");
110:
111: PanelBuilder builder = new PanelBuilder(layout);
112: CellConstraints cc = new CellConstraints();
113:
114: builder.addSeparator(title, cc.xyw(1, 1, 10));
115: builder.addLabel("Container No", cc.xy(1, 3));
116: builder.add(containerNoField, cc.xy(3, 3));
117: builder.addLabel("EDT time", cc.xy(5, 3));
118: builder.add(timeField, cc.xy(7, 3));
119: builder.addLabel("ms", cc.xy(9, 3));
120:
121: return IconFeedbackPanel.getWrappedComponentTree(
122: getValidationResultModel(), builder.getPanel());
123: }
124:
125: // Exposing Models -------------------------------------------
126:
127: JTextField getTextField() {
128: return containerNoField;
129: }
130:
131: Container getContainer() {
132: return container;
133: }
134:
135: ContainerValidator getContainerValidator() {
136: return validator;
137: }
138:
139: ValidationResultModel getValidationResultModel() {
140: return validationResultModel;
141: }
142:
143: // API
144:
145: void addTime(long milliseconds) {
146: long oldMilliseconds = timeModel.longValue();
147: timeModel.setValue(oldMilliseconds + milliseconds);
148: }
149:
150: // Event Handling --------------------------------------------
151:
152: void addContainerChangeListener(
153: PropertyChangeListener containerChangeListener) {
154: container.addPropertyChangeListener(containerChangeListener);
155: }
156:
157: }
|