001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.installer.cluster;
021:
022: import com.installshield.product.service.product.ProductService;
023: import com.installshield.util.Log;
024: import com.installshield.wizard.WizardBeanEvent;
025: import com.installshield.wizard.WizardBuilderSupport;
026: import com.installshield.wizard.WizardPanel;
027: import com.installshield.wizard.platform.win32.Win32RegistryService;
028: import com.installshield.wizard.service.ServiceException;
029: import com.installshield.wizard.service.WizardServicesUI;
030: import com.installshield.wizard.service.file.FileService;
031:
032: import java.io.File;
033: import java.io.IOException;
034:
035: import org.netbeans.installer.Util;
036:
037: public class NetBeansDirChooserPanel extends WizardPanel {
038: private String nbdir = "";
039:
040: void setNbDir(String nbdir) {
041: this .nbdir = nbdir;
042: }
043:
044: String getNbDir() {
045: return nbdir;
046: }
047:
048: public boolean queryEnter(WizardBeanEvent event) {
049: setDescription(resolveString("$L(org.netbeans.installer.cluster.Bundle, NetBeansDirChooser.desc)"));
050:
051: if (Util.isWindowsOS()) {
052: nbdir = getNbDirFromWindowsRegistry();
053: } else {
054: nbdir = getDefaultNbDir();
055: }
056:
057: return true;
058: }
059:
060: public boolean queryExit(WizardBeanEvent event) {
061: return validateNbDir();
062: }
063:
064: public void exited(WizardBeanEvent event) {
065: try {
066: String installDir = getInstallDir();
067:
068: ProductService service = (ProductService) getService(ProductService.NAME);
069: service.setProductBeanProperty(
070: ProductService.DEFAULT_PRODUCT_SOURCE, null,
071: "installLocation", resolveString(getInstallDir()));
072: } catch (ServiceException e) {
073: logEvent(this , Log.ERROR, e);
074: }
075: }
076:
077: private String getInstallDir() {
078: return new File(
079: nbdir,
080: resolveString("$L(org.netbeans.installer.cluster.Bundle, Product.clusterDir)"))
081: .getPath();
082: }
083:
084: private boolean validateNbDir() {
085: try {
086: String nbClusterDir = resolveString("$L(org.netbeans.installer.cluster.Bundle,NetBeans.nbClusterDir)");
087: File f = new File(nbdir, nbClusterDir);
088: if (!f.exists() || !f.isDirectory()) {
089: getWizard()
090: .getServices()
091: .displayUserMessage(
092: resolveString("$L(org.netbeans.installer.cluster.Bundle, NetBeansDirChooser.dirChooserDialogTitle)"),
093: resolveString("$L(org.netbeans.installer.cluster.Bundle, NetBeansDirChooser.invalidNbDir)"),
094: WizardServicesUI.ERROR);
095: return false;
096: }
097: if (!f.canWrite()) {
098: getWizard()
099: .getServices()
100: .displayUserMessage(
101: resolveString("$L(org.netbeans.installer.cluster.Bundle, NetBeansDirChooser.dirChooserDialogTitle)"),
102: resolveString("$L(org.netbeans.installer.cluster.Bundle, NetBeansDirChooser.cannotWriteNbDir)"),
103: WizardServicesUI.ERROR);
104: return false;
105: }
106: } catch (ServiceException ex) {
107: logEvent(this , Log.ERROR, ex);
108: }
109:
110: return true;
111: }
112:
113: /** Validate NB dir. No UI. */
114: private boolean validateNbDirNoUI(String dir) {
115: String nbClusterDir = resolveString("$L(org.netbeans.installer.cluster.Bundle,NetBeans.nbClusterDir)");
116: File f = new File(dir, nbClusterDir);
117: if (!f.exists() || !f.isDirectory()) {
118: return false;
119: }
120: if (!f.canWrite()) {
121: return false;
122: }
123: return true;
124: }
125:
126: public void build(WizardBuilderSupport support) {
127: super .build(support);
128:
129: support.putRequiredService(ProductService.NAME);
130: support.putRequiredService(FileService.NAME);
131: support.putRequiredService(Win32RegistryService.NAME);
132: try {
133: support.putClass(Util.class.getName());
134: support
135: .putClass("org.netbeans.installer.cluster.NetBeansDirChooserPanelSwingImpl$1");
136: } catch (IOException ex) {
137: logEvent(this , Log.ERROR, ex);
138: }
139: }
140:
141: /** Get NB install location from Windows Registry.
142: * @return path to NB installation or empty string when not found
143: */
144: private String getNbDirFromWindowsRegistry() {
145: try {
146: //Product key of NB 4.0
147: String uidNB = resolveString("$L(org.netbeans.installer.cluster.Bundle, NetBeans.productKey)");
148: String nbInstallDir;
149:
150: Win32RegistryService regserv = (Win32RegistryService) getService(Win32RegistryService.NAME);
151:
152: int HKEY = Win32RegistryService.HKEY_LOCAL_MACHINE;
153: int HKCU = Win32RegistryService.HKEY_CURRENT_USER;
154:
155: String HKEY_uninstall = "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"
156: + uidNB;
157: logEvent(this , Log.DBG, "key -> " + HKEY_uninstall);
158:
159: if (regserv.keyExists(HKEY, HKEY_uninstall)) {
160: nbInstallDir = regserv.getStringValue(HKEY,
161: HKEY_uninstall, "InstallLocation", false);
162: logEvent(this , Log.DBG, "Retrieved InstallLocation: "
163: + nbInstallDir);
164: if (nbInstallDir == null) {
165: return "";
166: } else {
167: return nbInstallDir;
168: }
169: } else {
170: logEvent(this , Log.DBG, "Key does not exist");
171: return "";
172: }
173: } catch (ServiceException ex) {
174: System.out.println(ex.getLocalizedMessage());
175: ex.printStackTrace();
176: }
177: return "";
178: }
179:
180: /** Try to get NB default install location. Check if there is NB installation.
181: * @return path to NB installation or empty string if not found
182: */
183: private String getDefaultNbDir() {
184: String nbInstallDir;
185:
186: //Check home
187: nbInstallDir = resolveString("$L(org.netbeans.installer.cluster.Bundle, NetBeans.installLocationForNonRoot)");
188: logEvent(this , Log.DBG, "Check InstallLocation1: "
189: + nbInstallDir);
190: if (validateNbDirNoUI(nbInstallDir)) {
191: return nbInstallDir;
192: }
193:
194: //Check default install location
195: nbInstallDir = resolveString("$L(org.netbeans.installer.cluster.Bundle, NetBeans.installLocation)");
196: logEvent(this , Log.DBG, "Check InstallLocation2: "
197: + nbInstallDir);
198: if (validateNbDirNoUI(nbInstallDir)) {
199: return nbInstallDir;
200: }
201:
202: return "";
203: }
204: }
|