~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to scripting/php/webproject/src/org/netbeans/modules/php/project/ui/SourceRootsUi.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * Portions Copyrighted 2007 Sun Microsystems, Inc.
 
27
 */
 
28
package org.netbeans.modules.php.project.ui;
 
29
 
 
30
import java.io.File;
 
31
import java.text.MessageFormat;
 
32
import org.netbeans.api.project.FileOwnerQuery;
 
33
import org.netbeans.api.project.Project;
 
34
import org.netbeans.api.project.ProjectInformation;
 
35
import org.netbeans.api.project.ProjectUtils;
 
36
import org.netbeans.api.project.SourceGroup;
 
37
import org.netbeans.api.project.Sources;
 
38
import org.netbeans.modules.php.project.PhpProject;
 
39
import org.openide.DialogDisplayer;
 
40
import org.openide.NotifyDescriptor;
 
41
import org.openide.filesystems.FileObject;
 
42
import org.openide.filesystems.FileUtil;
 
43
import org.openide.util.NbBundle;
 
44
 
 
45
/**
 
46
 *
 
47
 * @author avk
 
48
 */
 
49
public final class SourceRootsUi {
 
50
 
 
51
    public static final String INVALID_SOURCE_ROOT_MSG = "MSG_InvalidSourceRoot"; // NOI18N
 
52
    public static final String INVALID_SOURCE_ROOT_TTL = "TTL_InvalidSourceRoot"; // NOI18N
 
53
 
 
54
    /**
 
55
     * checks if suggested source directory root is not occupied by another projects
 
56
     *
 
57
     * @param file File object of source root to verify
 
58
     * @param projectFolder project wich will contain this source root
 
59
     */
 
60
    public static boolean isRootNotOccupied(File file, File projectFolder) {
 
61
        Project p = FileOwnerQuery.getOwner(file.toURI());
 
62
        boolean isInsideProject = file.getAbsolutePath().startsWith(projectFolder.getAbsolutePath() + File.separatorChar);
 
63
        boolean isEqualToProject = file.getAbsolutePath().equalsIgnoreCase(projectFolder.getAbsolutePath());
 
64
 
 
65
        // actions inside this IF are copied from ruby project and adopted
 
66
        if (p != null && !isInsideProject && !isEqualToProject) {
 
67
            final Sources sources = (Sources) p.getLookup().lookup(Sources.class);
 
68
            if (sources == null) {
 
69
                return false;
 
70
            }
 
71
            final SourceGroup[] sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC);
 
72
            final SourceGroup[] sourceGroupsPhp = sources.getSourceGroups(PhpProject.SOURCES_TYPE_PHP);
 
73
            final SourceGroup[] groups = new SourceGroup[sourceGroups.length + sourceGroupsPhp.length];
 
74
            System.arraycopy(sourceGroups, 0, groups, 0, sourceGroups.length);
 
75
            System.arraycopy(sourceGroupsPhp, 0, groups, sourceGroups.length, sourceGroupsPhp.length);
 
76
            final FileObject projectDirectory = p.getProjectDirectory();
 
77
            final FileObject fileObject = FileUtil.toFileObject(file);
 
78
            if (projectDirectory == null || fileObject == null) {
 
79
                return false;
 
80
            }
 
81
            for (int i = 0; i < groups.length; i++) {
 
82
                final FileObject sgRoot = groups[i].getRootFolder();
 
83
                if (fileObject.equals(sgRoot)) {
 
84
                    return false;
 
85
                }
 
86
                if (!projectDirectory.equals(sgRoot) && FileUtil.isParentOf(sgRoot, fileObject)) {
 
87
                    return false;
 
88
                }
 
89
            }
 
90
            return true;
 
91
        }
 
92
        return true;
 
93
    }
 
94
 
 
95
    public static void showSourceUsedDialog(File srcRoot) {
 
96
        String srcRootPath = srcRoot.getPath();
 
97
        
 
98
        Project p = FileOwnerQuery.getOwner(srcRoot.toURI());
 
99
        ProjectInformation pi = ProjectUtils.getInformation(p);
 
100
        String projectName = pi.getDisplayName();
 
101
 
 
102
        String msg = MessageFormat.format(NbBundle.getMessage(SourceRootsUi.class, INVALID_SOURCE_ROOT_MSG), srcRootPath, projectName);
 
103
        String title = NbBundle.getMessage(SourceRootsUi.class, INVALID_SOURCE_ROOT_TTL);
 
104
 
 
105
        NotifyDescriptor d = new NotifyDescriptor(msg, title, NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.WARNING_MESSAGE, new Object[]{NotifyDescriptor.OK_OPTION}, NotifyDescriptor.OK_OPTION);
 
106
        DialogDisplayer.getDefault().notify(d);
 
107
    }
 
108
}