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
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.visualweb.project.jsf;
043:
044: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
045:
046: import java.io.IOException;
047: import java.util.Arrays;
048: import java.util.List;
049: import java.util.Set;
050: import java.util.LinkedHashSet;
051: import java.util.logging.Level;
052: import java.util.logging.Logger;
053: import java.beans.PropertyChangeEvent;
054: import java.beans.PropertyChangeListener;
055: import javax.swing.SwingUtilities;
056:
057: import org.openide.modules.ModuleInstall;
058: import org.openide.DialogDisplayer;
059: import org.openide.NotifyDescriptor;
060: import org.openide.util.NbBundle;
061:
062: import org.netbeans.api.project.Project;
063: import org.netbeans.api.project.ProjectUtils;
064: import org.netbeans.api.project.ProjectInformation;
065: import org.netbeans.api.project.ui.OpenProjects;
066: import org.netbeans.api.project.libraries.Library;
067: import org.netbeans.api.project.libraries.LibraryManager;
068: import org.netbeans.api.java.classpath.ClassPath;
069:
070: /**
071: *
072: * @author Po-Ting Wu
073: */
074: public class Install extends ModuleInstall {
075: private static final Logger LOGGER = Logger.getLogger(Install.class
076: .getName());
077:
078: public void restored() {
079: OpenProjects.getDefault().addPropertyChangeListener(
080: new OpenProjectsListener());
081: }
082:
083: public static class OpenProjectsListener implements
084: PropertyChangeListener {
085: public OpenProjectsListener() {
086: // Don't do anything during IDE startup.
087: }
088:
089: public void propertyChange(PropertyChangeEvent event) {
090: // The list of open projects has changed.
091: if (OpenProjects.PROPERTY_OPEN_PROJECTS.equals(event
092: .getPropertyName())) {
093: List<Project> oldOpenProjectsList = Arrays
094: .asList((Project[]) event.getOldValue());
095: List<Project> newOpenProjectsList = Arrays
096: .asList((Project[]) event.getNewValue());
097:
098: Set<Project> jsfProjectsSet = new LinkedHashSet<Project>();
099: Set<Project> openedProjectsSet = new LinkedHashSet<Project>(
100: newOpenProjectsList);
101: openedProjectsSet.removeAll(oldOpenProjectsList);
102: for (Project project : openedProjectsSet) {
103: if (JsfProjectUtils.isJsfProject(project)) {
104: jsfProjectsSet.add(project);
105: }
106: }
107:
108: if (jsfProjectsSet.size() == 0) {
109: return;
110: }
111:
112: // Visual Web project found in the just opened project list
113: LibraryManager libManager = LibraryManager.getDefault();
114: Library libJSF = libManager.getLibrary("jsf1102"); // NOI18N
115: Library libJAXRPC = libManager.getLibrary("jaxrpc16"); // NOI18N
116: Library libRowset = libManager.getLibrary("rowset-ri"); // NOI18N
117: String projs = ""; // NOI18N
118: boolean needJSF = false;
119: boolean needJAXRPC = false;
120: boolean needRowset = false;
121:
122: for (Project project : jsfProjectsSet) {
123: boolean badProj = false;
124: boolean JavaEE5Project = JsfProjectUtils
125: .isJavaEE5Project(project);
126: ClassPath cp = ClassPath.getClassPath(
127: JsfProjectUtils.getDocumentRoot(project),
128: ClassPath.COMPILE);
129:
130: // It's a VisualWeb/Creator J2EE 1.4 project
131: if (!JavaEE5Project) {
132: if ((libJSF == null)
133: && (cp
134: .findResource("javax/faces/FacesException.class") == null)
135: && //NOI18N
136: (cp
137: .findResource("org/apache/myfaces/webapp/StartupServletContextListener.class") == null)) { //NOI18N
138: // Both the IDE and Server do not have the JSF 1.1 RI support
139: projs = appendProject(project, projs);
140: badProj = true;
141: needJSF = true;
142: }
143:
144: if ((libJAXRPC == null)
145: && (cp
146: .findResource("javax/xml/rpc/Service.class") == null)) { //NOI18N
147: // Both the IDE and Server do not have the JAXRPC support
148: if (!badProj) {
149: projs = appendProject(project, projs);
150: }
151: badProj = true;
152: needJAXRPC = true;
153: }
154: }
155:
156: // It's a VisualWeb/Creator J2SE 1.3/1.4 project
157: String srcLevel = JsfProjectUtils
158: .getSourceLevel(project);
159: if ("1.3".equals(srcLevel)
160: || "1.4".equals(srcLevel)) { // NOI18N
161: if (cp
162: .findResource("javax/sql/rowset/BaseRowSet.class") == null) { //NOI18N
163: // IDE doesn't have the Rowset RI support
164: if (libRowset != null) {
165: try {
166: JsfProjectUtils
167: .addLibraryReferences(
168: project,
169: new Library[] { libRowset });
170: } catch (IOException ex) {
171: LOGGER
172: .log(
173: Level.WARNING,
174: "Exception during adding Rowset RI library",
175: ex); //NOI18N
176: }
177: } else {
178: if (!badProj) {
179: projs = appendProject(project,
180: projs);
181: }
182: needRowset = true;
183: }
184: }
185: }
186: }
187:
188: if (projs.length() > 0) {
189: int count = 0;
190: String nbms = "";
191: if (needJSF) {
192: count++;
193: nbms = NbBundle.getMessage(Install.class,
194: "LBL_MissingJSF");
195: }
196: if (needJAXRPC) {
197: count++;
198: nbms += NbBundle.getMessage(Install.class,
199: "LBL_MissingJAXRPC");
200: }
201: if (needRowset) {
202: count++;
203: nbms += NbBundle.getMessage(Install.class,
204: "LBL_MissingRowset");
205: }
206:
207: String RI = NbBundle.getMessage(Install.class,
208: (count > 1) ? "LBL_MissingMany"
209: : "LBL_MissingOne");
210:
211: final String mesg = NbBundle.getMessage(
212: Install.class, "LBL_MissingNBM", projs, RI,
213: nbms);
214: SwingUtilities.invokeLater(new Runnable() {
215: public void run() {
216: NotifyDescriptor d = new NotifyDescriptor.Message(
217: mesg,
218: NotifyDescriptor.WARNING_MESSAGE);
219: DialogDisplayer.getDefault().notify(d);
220: }
221: });
222: }
223: }
224: }
225:
226: public String appendProject(Project project, String projs) {
227: ProjectInformation info = ProjectUtils
228: .getInformation(project);
229: if (projs.length() > 0) {
230: projs += ", "; // NOI18N
231: }
232: projs += info.getDisplayName();
233:
234: return projs;
235: }
236: }
237: }
|