001    /*
002     * Copyright 2002-2008 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.selection.dialog;
017    
018    import java.awt.Window;
019    
020    import javax.swing.JComponent;
021    
022    import org.springframework.rules.closure.Closure;
023    import org.springframework.richclient.dialog.ApplicationDialog;
024    import org.springframework.richclient.layout.TableLayoutBuilder;
025    import org.springframework.util.Assert;
026    import org.springframework.util.StringUtils;
027    
028    /**
029     * Base class for selection dialogs.
030     * 
031     * @author Peter De Bruycker
032     */
033    public abstract class AbstractSelectionDialog extends ApplicationDialog {
034            private String description;
035    
036            private Closure onSelectAction;
037            private Closure onAboutToShow;
038    
039            public AbstractSelectionDialog(String title, Window parent) {
040                    super(title, parent);
041            }
042    
043            protected JComponent createDialogContentPane() {
044                    TableLayoutBuilder builder = new TableLayoutBuilder();
045    
046                    JComponent selectionComponent = createSelectionComponent();
047                    Assert.state(selectionComponent != null, "createSelectionComponent cannot return null");
048    
049                    if (StringUtils.hasText(description)) {
050                            builder.cell(getComponentFactory().createLabelFor(description, selectionComponent));
051                            builder.relatedGapRow();
052                            builder.row();
053                    }
054    
055                    builder.cell(selectionComponent);
056    
057                    return builder.getPanel();
058            }
059    
060            protected abstract JComponent createSelectionComponent();
061    
062            protected boolean onFinish() {
063                    onSelect(getSelectedObject());
064                    return true;
065            }
066    
067            public void setDescription(String desc) {
068                    Assert.isTrue(!isControlCreated(), "Set the description before the control is created.");
069    
070                    description = desc;
071            }
072    
073            protected abstract Object getSelectedObject();
074    
075            protected void onSelect(Object selection) {
076                    if (onSelectAction != null) {
077                            onSelectAction.call(selection);
078                    }
079                    else {
080                            throw new UnsupportedOperationException("Either provide an onSelectAction or override the onSelect method");
081                    }
082            }
083    
084            public void setOnSelectAction(Closure onSelectAction) {
085                    this.onSelectAction = onSelectAction;
086            }
087            
088            public void setOnAboutToShow(Closure onAboutToShow) {
089            this.onAboutToShow = onAboutToShow;
090        }
091            
092            protected void onAboutToShow() {
093                if(onAboutToShow != null) {
094                    onAboutToShow.call(null);
095                }
096            }
097    }