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.Installer;
041: import org.netbeans.installer.product.components.Product;
042: import org.netbeans.installer.product.Registry;
043: import org.netbeans.installer.utils.helper.Status;
044: import org.netbeans.installer.utils.helper.ErrorLevel;
045: import org.netbeans.installer.utils.ErrorManager;
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.UninstallationException;
050: import org.netbeans.installer.utils.progress.CompositeProgress;
051: import org.netbeans.installer.utils.progress.Progress;
052: import org.netbeans.installer.wizard.components.WizardAction;
053:
054: public class UninstallAction extends WizardAction {
055: /////////////////////////////////////////////////////////////////////////////////
056: // Constants
057: public static final String DEFAULT_TITLE = ResourceUtils.getString(
058: UninstallAction.class, "UA.title"); // NOI18N
059: public static final String DEFAULT_DESCRIPTION = ResourceUtils
060: .getString(UninstallAction.class, "UA.description"); // NOI18N
061: public static final String DEFAULT_PROGRESS_UNINSTALL_TITLE_MESSAGE = ResourceUtils
062: .getString(UninstallAction.class,
063: "UA.progress.uninstall.title");//NOI18N
064: public static final String DEFAULT_UNINSTALL_DEPENDENT_FAILED_MESSAGE = ResourceUtils
065: .getString(UninstallAction.class,
066: "UA.uninstall.dependent.failed");//NOI18N
067:
068: public static final String PROGRESS_UNINSTALL_TITLE_PROPERTY = "progress.uninstall.title";
069:
070: public static final String UNINSTALL_DEPENDENT_FAILED_PROPERTY = "uninstall.dependent.failed";
071: public static final int UNINSTALLATION_ERROR_CODE = 126;//NOMAGI
072:
073: /////////////////////////////////////////////////////////////////////////////////
074: // Instance
075: private CompositeProgress overallProgress;
076: private Progress currentProgress;
077:
078: public UninstallAction() {
079: setProperty(TITLE_PROPERTY, DEFAULT_TITLE);
080: setProperty(DESCRIPTION_PROPERTY, DEFAULT_DESCRIPTION);
081: setProperty(PROGRESS_UNINSTALL_TITLE_PROPERTY,
082: DEFAULT_PROGRESS_UNINSTALL_TITLE_MESSAGE);
083: setProperty(UNINSTALL_DEPENDENT_FAILED_PROPERTY,
084: DEFAULT_UNINSTALL_DEPENDENT_FAILED_MESSAGE);
085: }
086:
087: public void execute() {
088: final Registry registry = Registry.getInstance();
089: final List<Product> products = registry
090: .getProductsToUninstall();
091: final int percentageChunk = Progress.COMPLETE / products.size();
092: final int percentageLeak = Progress.COMPLETE % products.size();
093:
094: overallProgress = new CompositeProgress();
095: overallProgress.setPercentage(percentageLeak);
096: overallProgress.synchronizeDetails(true);
097:
098: getWizardUi().setProgress(overallProgress);
099: for (int i = 0; i < products.size(); i++) {
100: // get the handle of the current item
101: final Product product = products.get(i);
102:
103: // initiate the progress for the current element
104: currentProgress = new Progress();
105:
106: overallProgress.addChild(currentProgress, percentageChunk);
107: overallProgress.setTitle(StringUtils.format(
108: getProperty(PROGRESS_UNINSTALL_TITLE_PROPERTY),
109: product.getDisplayName()));
110: try {
111: product.uninstall(currentProgress);
112:
113: // sleep a little so that the user can perceive that something
114: // is happening
115: SystemUtils.sleep(200);
116: } catch (UninstallationException e) {
117: // do not override already set exit code
118: if (System.getProperties().get(
119: Installer.EXIT_CODE_PROPERTY) == null) {
120: System.getProperties().put(
121: Installer.EXIT_CODE_PROPERTY,
122: new Integer(UNINSTALLATION_ERROR_CODE));
123: }
124: // adjust the component's status and save this error - it will
125: // be reused later at the PostInstallSummary
126: product.setStatus(Status.INSTALLED);
127: product.setUninstallationError(e);
128:
129: // since the product failed to uninstall - we should remove
130: // the components it depends on from our plans to uninstall
131: for (Product requirement : registry.getProducts()) {
132: if ((requirement.getStatus() == Status.TO_BE_UNINSTALLED)
133: && registry.satisfiesRequirement(
134: requirement, product)) {
135: UninstallationException requirementError = new UninstallationException(
136: StringUtils
137: .format(
138: getProperty(PROGRESS_UNINSTALL_TITLE_PROPERTY),
139: requirement
140: .getDisplayName(),
141: product
142: .getDisplayName()),
143: e);
144:
145: requirement.setStatus(Status.INSTALLED);
146: requirement
147: .setUninstallationError(requirementError);
148:
149: products.remove(requirement);
150: }
151: }
152:
153: // finally notify the user of what has happened
154: ErrorManager.notify(ErrorLevel.ERROR, e);
155: }
156: }
157: }
158:
159: @Override
160: public boolean canExecuteForward() {
161: return Registry.getInstance().getProductsToUninstall().size() > 0;
162: }
163:
164: @Override
165: public boolean isPointOfNoReturn() {
166: return true;
167: }
168:
169: @Override
170: public boolean isCancelable() {
171: return false;
172: }
173: }
|