001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of 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,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.springframework.richclient.application.setup;
017    
018    import java.io.BufferedReader;
019    import java.io.IOException;
020    import java.io.InputStreamReader;
021    
022    import javax.swing.JComponent;
023    import javax.swing.JScrollPane;
024    
025    import org.springframework.core.io.Resource;
026    import org.springframework.richclient.command.CommandGroup;
027    import org.springframework.richclient.command.ToggleCommand;
028    import org.springframework.richclient.layout.GridBagLayoutBuilder;
029    import org.springframework.richclient.text.HtmlPane;
030    import org.springframework.richclient.util.LabelUtils;
031    import org.springframework.richclient.wizard.AbstractWizardPage;
032    import org.springframework.richclient.wizard.WizardPage;
033    import org.springframework.util.Assert;
034    import org.springframework.util.FileCopyUtils;
035    
036    /**
037     * A {@link WizardPage} which shows a license text and confirmation radio
038     * buttons at the bottom. The license text can be set by
039     * {@link #setLicenseTextLocation(Resource)}.
040     *
041     * @author Claudio Romano
042     */
043    public class SetupLicenseWizardPage extends AbstractWizardPage {
044            /** Pane holding the license text. */
045            private HtmlPane licenseTextPane;
046    
047            /** The license text. */
048            private Resource licenseTextLocation;
049    
050            /**
051             * Default constructor. License Resource can be added later.
052             */
053            public SetupLicenseWizardPage() {
054                    this(null);
055            }
056    
057            /**
058             * Convenience constructor which sets the license resource.
059             *
060             * @see #setLicenseTextLocation(Resource)
061             */
062            public SetupLicenseWizardPage(Resource licenseTextLocation) {
063                    super("license");
064                    setLicenseTextLocation(licenseTextLocation);
065            }
066    
067            /**
068             * Set the {@link Resource} to use as license text.
069             */
070            public final void setLicenseTextLocation(Resource location) {
071                    this.licenseTextLocation = location;
072                    if (licenseTextPane != null) {
073                            updateLicenseTextPane();
074                    }
075            }
076    
077            /**
078             * {@inheritDoc}
079             */
080            protected JComponent createControl() {
081                    initLicenseTextPane();
082    
083                    ToggleCommand acceptCommand = new ToggleCommand("acceptLicenseCommand") {
084                            protected void onSelection() {
085                                    SetupLicenseWizardPage.this.setEnabled(true);
086                            }
087                    };
088    
089                    ToggleCommand doNotAcceptCommand = new ToggleCommand("doNotAcceptLicenseCommand") {
090                            protected void onSelection() {
091                                    SetupLicenseWizardPage.this.setEnabled(false);
092                            }
093                    };
094                    doNotAcceptCommand.setSelected(true);
095    
096                    CommandGroup.createExclusiveCommandGroup(new ToggleCommand[] { acceptCommand, doNotAcceptCommand });
097    
098                    GridBagLayoutBuilder formBuilder = new GridBagLayoutBuilder();
099                    formBuilder.append(new JScrollPane(licenseTextPane), 1, 1, true, true);
100                    formBuilder.nextLine();
101                    formBuilder.append(acceptCommand.createRadioButton());
102                    formBuilder.nextLine();
103                    formBuilder.append(doNotAcceptCommand.createRadioButton());
104                    return formBuilder.getPanel();
105            }
106    
107            /**
108             * Create the html pane and update its contents.
109             */
110            protected void initLicenseTextPane() {
111                    this.licenseTextPane = new HtmlPane();
112                    updateLicenseTextPane();
113            }
114    
115            /**
116             * Updates the text in the html pane.
117             */
118            private void updateLicenseTextPane() {
119                    try {
120                            Assert.state(licenseTextLocation != null, "License text location is not set");
121                            String text = FileCopyUtils.copyToString(new BufferedReader(new InputStreamReader(licenseTextLocation
122                                            .getInputStream())));
123                            licenseTextPane.setText(LabelUtils.htmlBlock(text));
124                    }
125                    catch (IOException e) {
126                            final IllegalStateException exp = new IllegalStateException("License text not accessible: "
127                                            + e.getMessage());
128                            exp.setStackTrace(e.getStackTrace());
129                            throw exp;
130                    }
131            }
132    
133    }