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-2006 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.projectimport.jbuilder.ui;
043:
044: import java.io.File;
045: import java.util.Collection;
046: import java.util.HashSet;
047: import java.util.Iterator;
048: import java.util.LinkedList;
049: import org.netbeans.modules.projectimport.j2seimport.AbstractProject;
050: import org.netbeans.modules.projectimport.j2seimport.WarningContainer;
051: import org.netbeans.modules.projectimport.j2seimport.ui.WarningMessage;
052: import org.openide.util.NbBundle;
053:
054: /**
055: *
056: * @author Radek Matous
057: */
058: final class Utils {
059:
060: /** Creates a new instance of Utils */
061: private Utils() {
062: }
063:
064: static boolean checkNotFoundUserLibraries(Collection prjDefs) {
065: boolean notfound = notFoundUserLibraries(prjDefs).size() > 0;
066: if (!notfound && prjDefs != null) {
067: for (Iterator it = prjDefs.iterator(); it.hasNext();) {
068: AbstractProject prj = (AbstractProject) it.next();
069: if (prj.getJdkId() != null
070: && prj.getJDKDirectory() == null)
071: return true;
072: }
073: }
074: return notfound;
075: }
076:
077: private static Collection notFoundUserLibraries(
078: Collection allPrjDefs) {
079: Collection notFoundUserLibraries = new HashSet();
080: if (allPrjDefs != null) {
081: Iterator prjsIt = allPrjDefs.iterator();
082: while (prjsIt.hasNext()) {
083: AbstractProject ap = (AbstractProject) prjsIt.next();
084: Iterator it = ap.getUserLibraries().iterator();
085: while (it.hasNext()) {
086: AbstractProject.UserLibrary uLib = (AbstractProject.UserLibrary) it
087: .next();
088: if (uLib.fileNotFound()) {
089: notFoundUserLibraries.add(uLib);
090: }
091: }
092: }
093: }
094:
095: return notFoundUserLibraries;
096: }
097:
098: static Collection getInvalidUserLibraries(Collection allPrjDefs) {
099: Collection invalidUserLibraries = new HashSet();
100: if (allPrjDefs != null) {
101: Iterator prjsIt = allPrjDefs.iterator();
102: while (prjsIt.hasNext()) {
103: AbstractProject ap = (AbstractProject) prjsIt.next();
104: if (ap.getJDKDirectory() == null
105: && ap.getJdkId() != null) {
106: invalidUserLibraries
107: .add(new AbstractProject.UserLibrary(ap
108: .getJdkId(), false));
109: }
110: Iterator it = ap.getUserLibraries().iterator();
111: while (it.hasNext()) {
112: AbstractProject.UserLibrary uLib = (AbstractProject.UserLibrary) it
113: .next();
114: if (!uLib.isValid()) {
115: invalidUserLibraries.add(uLib);
116: }
117: }
118: }
119: }
120:
121: return invalidUserLibraries;
122: }
123:
124: static StringBuffer getDependencyErrors(Collection prjDefs,
125: File destinationDir) {
126: return getProjectsErrors(collectAllDependencies(prjDefs),
127: destinationDir);
128: }
129:
130: static StringBuffer getProjectsErrors(Collection prjDefs,
131: File destinationDir) {
132: StringBuffer errBuf = null;
133: if (prjDefs != null) {
134: for (Iterator it = prjDefs.iterator(); it.hasNext();) {
135: AbstractProject ap = (AbstractProject) it.next();
136: File importLocation = new File(destinationDir, ap
137: .getName());
138:
139: if (ap.getErrors().size() > 0
140: || importLocation.exists()) {
141: errBuf = (errBuf == null) ? new StringBuffer()
142: : errBuf;
143: for (Iterator errIt = ap.getErrors().iterator(); errIt
144: .hasNext();) {
145: errBuf.append((String) errIt.next()).append(
146: " ");//NOI18N
147: }
148: if (importLocation.exists()) {
149: String errMsg = NbBundle.getMessage(
150: JBWizardPanel.class,
151: "MSG_ProjectAlreadyExists",
152: importLocation.getAbsolutePath()); // NOI18N
153: errBuf.append(errMsg).append(" ");//NOI18N
154: }
155: }
156: }
157: }
158: return errBuf;
159: }
160:
161: static String getHtmlWarnings(Collection prjDefs) {
162: Collection all = new LinkedList();
163: if (prjDefs != null) {
164: for (Iterator it = prjDefs.iterator(); it.hasNext();) {
165: AbstractProject ap = (AbstractProject) it.next();
166: all.addAll(ap.getWarnings().getAllWarnings());
167: }
168: }
169:
170: return (all.isEmpty()) ? null : WarningMessage
171: .createHtmlString("", all.iterator(), true, 4);//NOI18N
172: }
173:
174: private static Collection collectAllDependencies(Collection prjDefs) {
175: Collection result = new HashSet();
176: if (prjDefs != null && prjDefs.size() > 0) {
177: for (Iterator it = prjDefs.iterator(); it.hasNext();) {
178: AbstractProject ap = (AbstractProject) it.next();
179: for (Iterator depsIt = ap.getDependencies().iterator(); depsIt
180: .hasNext();) {
181: AbstractProject dep = (AbstractProject) depsIt
182: .next();
183: result.add(dep);
184: }
185:
186: }
187: if (result.size() > 0) {
188: result.addAll(collectAllDependencies(result));
189: }
190: }
191:
192: return result;
193: }
194: }
|