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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.php.project.ui;
029:
030: import java.io.File;
031: import java.text.MessageFormat;
032: import org.netbeans.api.project.FileOwnerQuery;
033: import org.netbeans.api.project.Project;
034: import org.netbeans.api.project.ProjectInformation;
035: import org.netbeans.api.project.ProjectUtils;
036: import org.netbeans.api.project.SourceGroup;
037: import org.netbeans.api.project.Sources;
038: import org.netbeans.modules.php.project.PhpProject;
039: import org.openide.DialogDisplayer;
040: import org.openide.NotifyDescriptor;
041: import org.openide.filesystems.FileObject;
042: import org.openide.filesystems.FileUtil;
043: import org.openide.util.NbBundle;
044:
045: /**
046: *
047: * @author avk
048: */
049: public final class SourceRootsUi {
050:
051: public static final String INVALID_SOURCE_ROOT_MSG = "MSG_InvalidSourceRoot"; // NOI18N
052: public static final String INVALID_SOURCE_ROOT_TTL = "TTL_InvalidSourceRoot"; // NOI18N
053:
054: /**
055: * checks if suggested source directory root is not occupied by another projects
056: *
057: * @param file File object of source root to verify
058: * @param projectFolder project wich will contain this source root
059: */
060: public static boolean isRootNotOccupied(File file,
061: File projectFolder) {
062: Project p = FileOwnerQuery.getOwner(file.toURI());
063: boolean isInsideProject = file.getAbsolutePath().startsWith(
064: projectFolder.getAbsolutePath() + File.separatorChar);
065: boolean isEqualToProject = file.getAbsolutePath()
066: .equalsIgnoreCase(projectFolder.getAbsolutePath());
067:
068: // actions inside this IF are copied from ruby project and adopted
069: if (p != null && !isInsideProject && !isEqualToProject) {
070: final Sources sources = (Sources) p.getLookup().lookup(
071: Sources.class);
072: if (sources == null) {
073: return false;
074: }
075: final SourceGroup[] sourceGroups = sources
076: .getSourceGroups(Sources.TYPE_GENERIC);
077: final SourceGroup[] sourceGroupsPhp = sources
078: .getSourceGroups(PhpProject.SOURCES_TYPE_PHP);
079: final SourceGroup[] groups = new SourceGroup[sourceGroups.length
080: + sourceGroupsPhp.length];
081: System.arraycopy(sourceGroups, 0, groups, 0,
082: sourceGroups.length);
083: System.arraycopy(sourceGroupsPhp, 0, groups,
084: sourceGroups.length, sourceGroupsPhp.length);
085: final FileObject projectDirectory = p.getProjectDirectory();
086: final FileObject fileObject = FileUtil.toFileObject(file);
087: if (projectDirectory == null || fileObject == null) {
088: return false;
089: }
090: for (int i = 0; i < groups.length; i++) {
091: final FileObject sgRoot = groups[i].getRootFolder();
092: if (fileObject.equals(sgRoot)) {
093: return false;
094: }
095: if (!projectDirectory.equals(sgRoot)
096: && FileUtil.isParentOf(sgRoot, fileObject)) {
097: return false;
098: }
099: }
100: return true;
101: }
102: return true;
103: }
104:
105: public static void showSourceUsedDialog(File srcRoot) {
106: String srcRootPath = srcRoot.getPath();
107:
108: Project p = FileOwnerQuery.getOwner(srcRoot.toURI());
109: ProjectInformation pi = ProjectUtils.getInformation(p);
110: String projectName = pi.getDisplayName();
111:
112: String msg = MessageFormat.format(NbBundle.getMessage(
113: SourceRootsUi.class, INVALID_SOURCE_ROOT_MSG),
114: srcRootPath, projectName);
115: String title = NbBundle.getMessage(SourceRootsUi.class,
116: INVALID_SOURCE_ROOT_TTL);
117:
118: NotifyDescriptor d = new NotifyDescriptor(msg, title,
119: NotifyDescriptor.OK_CANCEL_OPTION,
120: NotifyDescriptor.WARNING_MESSAGE,
121: new Object[] { NotifyDescriptor.OK_OPTION },
122: NotifyDescriptor.OK_OPTION);
123: DialogDisplayer.getDefault().notify(d);
124: }
125: }
|