001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.wizard.components.actions;
038:
039: import java.util.List;
040: import org.netbeans.installer.product.components.Product;
041: import org.netbeans.installer.product.Registry;
042: import org.netbeans.installer.product.RegistryType;
043: import org.netbeans.installer.utils.helper.Status;
044: import org.netbeans.installer.utils.ErrorManager;
045: import org.netbeans.installer.utils.helper.ErrorLevel;
046: import org.netbeans.installer.utils.ResourceUtils;
047: import org.netbeans.installer.utils.StringUtils;
048: import org.netbeans.installer.utils.SystemUtils;
049: import org.netbeans.installer.utils.exceptions.DownloadException;
050: import org.netbeans.installer.utils.exceptions.InstallationException;
051: import org.netbeans.installer.utils.progress.CompositeProgress;
052: import org.netbeans.installer.utils.progress.Progress;
053: import org.netbeans.installer.wizard.components.WizardAction;
054:
055: public class DownloadConfigurationLogicAction extends WizardAction {
056: /////////////////////////////////////////////////////////////////////////////////
057: // Constants
058: public static final String DEFAULT_TITLE = ResourceUtils.getString(
059: DownloadConfigurationLogicAction.class, "DCLA.title"); // NOI18N
060: public static final String DEFAULT_DESCRIPTION = ResourceUtils
061: .getString(DownloadConfigurationLogicAction.class,
062: "DCLA.description"); // NOI18N
063:
064: public static final String DEFAULT_PROGRESS_TITLE_LOCAL = ResourceUtils
065: .getString(DownloadConfigurationLogicAction.class,
066: "DCLA.progress.local.title"); //NOI18N
067: public static final String PROGRESS_TITLE_LOCAL_PROPERTY = "progress.title.local";//NOI18N
068:
069: public static final String DEFAULT_PROGRESS_TITLE_REMOTE = ResourceUtils
070: .getString(DownloadConfigurationLogicAction.class,
071: "DCLA.progress.remote.title"); //NOI18N
072: public static final String PROGRESS_TITLE_REMOTE_PROPERTY = "progress.title.remote";//NOI18N
073:
074: public static final String DEFAULT_DOWNLOAD_FAILED_EXCEPTION = ResourceUtils
075: .getString(DownloadConfigurationLogicAction.class,
076: "DCLA.failed"); //NOI18N
077: public static final String DOWNLOAD_FAILED_EXCEPTION_PROPERTY = "download.failed";//NOI18N
078:
079: public static final String DEFAULT_DEPENDENT_FAILED_EXCEPTION = ResourceUtils
080: .getString(DownloadConfigurationLogicAction.class,
081: "DCLA.dependent.failed"); //NOI18N
082:
083: public static final String DEPENDENT_FAILED_EXCEPTION_PROPERTY = "download.dependent.failed"; //NOI18N
084:
085: /////////////////////////////////////////////////////////////////////////////////
086: // Instance
087: private CompositeProgress overallProgress;
088: private Progress currentProgress;
089:
090: public DownloadConfigurationLogicAction() {
091: setProperty(TITLE_PROPERTY, DEFAULT_TITLE);
092: setProperty(DESCRIPTION_PROPERTY, DEFAULT_DESCRIPTION);
093: setProperty(PROGRESS_TITLE_LOCAL_PROPERTY,
094: DEFAULT_PROGRESS_TITLE_LOCAL);
095: setProperty(PROGRESS_TITLE_REMOTE_PROPERTY,
096: DEFAULT_PROGRESS_TITLE_REMOTE);
097: setProperty(DOWNLOAD_FAILED_EXCEPTION_PROPERTY,
098: DEFAULT_DOWNLOAD_FAILED_EXCEPTION);
099: setProperty(DEPENDENT_FAILED_EXCEPTION_PROPERTY,
100: DEFAULT_DEPENDENT_FAILED_EXCEPTION);
101: }
102:
103: public boolean canExecuteForward() {
104: for (Product product : Registry.getInstance()
105: .getProductsToInstall()) {
106: if (!product.isLogicDownloaded()) {
107: return true;
108: }
109: }
110:
111: return false;
112: }
113:
114: public void execute() {
115: final Registry registry = Registry.getInstance();
116: final List<Product> products = registry.getProductsToInstall();
117: final int percentageChunk = Progress.COMPLETE / products.size();
118: final int percentageLeak = Progress.COMPLETE % products.size();
119:
120: overallProgress = new CompositeProgress();
121: overallProgress.setPercentage(percentageLeak);
122: overallProgress.synchronizeDetails(true);
123:
124: getWizardUi().setProgress(overallProgress);
125: for (int i = 0; i < products.size(); i++) {
126: // get the handle of the current item
127: final Product product = products.get(i);
128:
129: // initiate the progress for the current element
130: currentProgress = new Progress();
131:
132: overallProgress.addChild(currentProgress, percentageChunk);
133: try {
134: String prop = product.getRegistryType() == RegistryType.REMOTE ? PROGRESS_TITLE_REMOTE_PROPERTY
135: : PROGRESS_TITLE_LOCAL_PROPERTY;
136: String overallProgressTitle = StringUtils.format(
137: getProperty(prop), product.getDisplayName());
138:
139: overallProgress.setTitle(overallProgressTitle);
140:
141: product.downloadLogic(currentProgress);
142:
143: // ensure that the current progress has reached the complete state
144: // (sometimes it just does not happen and we're left over with 99%)
145: currentProgress.setPercentage(Progress.COMPLETE);
146:
147: // check for cancel status
148: if (isCanceled())
149: return;
150:
151: // sleep a little so that the user can perceive that something
152: // is happening
153: SystemUtils.sleep(200);
154: } catch (DownloadException e) {
155: // wrap the download exception with a more user-friendly one
156: final InstallationException error = new InstallationException(
157: StringUtils
158: .format(
159: getProperty(DOWNLOAD_FAILED_EXCEPTION_PROPERTY),
160: product.getDisplayName()), e);
161:
162: // adjust the product's status and save this error - it will
163: // be reused later at the PostInstallSummary
164: product.setStatus(Status.NOT_INSTALLED);
165: product.setInstallationError(error);
166:
167: // since the configuration logic for the current product failed to
168: // be downloaded, we should cancel the installation of the products
169: // that may require this one
170: for (Product dependent : registry.getProducts()) {
171: if ((dependent.getStatus() == Status.TO_BE_INSTALLED)
172: && registry.satisfiesRequirement(product,
173: dependent)) {
174: String exString = StringUtils
175: .format(
176: getProperty(DEPENDENT_FAILED_EXCEPTION_PROPERTY),
177: dependent.getDisplayName(),
178: product.getDisplayName());
179:
180: final InstallationException dependentError = new InstallationException(
181: exString, error);
182:
183: dependent.setStatus(Status.NOT_INSTALLED);
184: dependent.setInstallationError(dependentError);
185:
186: products.remove(dependent);
187: }
188: }
189:
190: // finally notify the user of what has happened
191: ErrorManager.notify(ErrorLevel.ERROR, error);
192: }
193: }
194: }
195:
196: public void cancel() {
197: if (currentProgress != null) {
198: currentProgress.setCanceled(true);
199: }
200:
201: if (overallProgress != null) {
202: overallProgress.setCanceled(true);
203: }
204:
205: super.cancel();
206: }
207: }
|