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: package com.sun.rave.propertyeditors.domains;
042:
043: import com.sun.rave.designtime.DesignBean;
044: import com.sun.rave.designtime.DesignContext;
045: import com.sun.rave.designtime.DesignProject;
046: import com.sun.rave.designtime.DesignProperty;
047: import java.net.URI;
048: import java.net.URISyntaxException;
049: import java.util.Iterator;
050: import java.util.Set;
051: import java.util.TreeSet;
052:
053: /**
054: * Domain of resource bundles available within the current project. The domain
055: * elements are constructed dynamically, by exploring the project source directory
056: * for Java properties files.
057: */
058: public class ResourceBundlesDomain extends AttachedDomain {
059:
060: // Root URI of the project's source directory
061: private static final String BLUE_PRINT_SOURCE_ROOT = "src/java/"; //NOI18N
062: private static final String JAKARTA_SOURCE_ROOT = "src/";
063: private static final URI SOURCE_URI = URI.create("src/");
064:
065: // File name suffix that identifiers a Java property bundle file
066: private static final String BUNDLE_SUFFIX = ".properties"; // NOI18N
067:
068: public Element[] getElements() {
069:
070: DesignProperty designProperty = this .getDesignProperty();
071:
072: if (designProperty == null)
073: return Element.EMPTY_ARRAY;
074:
075: DesignBean bean = getDesignProperty().getDesignBean();
076: DesignProject project = bean.getDesignContext().getProject();
077:
078: URI resources[] = project.getResources(SOURCE_URI, true);
079: if (resources == null)
080: return Element.EMPTY_ARRAY;
081:
082: Set set = new TreeSet();
083: for (int i = 0; i < resources.length; i++) {
084: String resource = resources[i].toString();
085: String sourceRoot;
086: if (resource.startsWith(BLUE_PRINT_SOURCE_ROOT)) {
087: sourceRoot = BLUE_PRINT_SOURCE_ROOT;
088: } else {
089: sourceRoot = JAKARTA_SOURCE_ROOT;
090: }
091: if (isBasePropertiesResource(resource)) {
092: resource = resource.substring(0, resource.length()
093: - BUNDLE_SUFFIX.length());
094: resource = resource.substring(sourceRoot.length());
095: String baseName = resource.replace('/', '.');
096: set.add(new Element(baseName)); // NOI18N
097: }
098: }
099:
100: return (Element[]) set.toArray(new Element[set.size()]);
101:
102: }
103:
104: private static boolean isBasePropertiesResource(String resource) {
105: if (resource.endsWith(BUNDLE_SUFFIX)) {
106: int i = resource.lastIndexOf('/');
107: int j = resource.lastIndexOf('_');
108: if (j <= i)
109: return true;
110: }
111: return false;
112: }
113:
114: }
|