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: package org.netbeans.modules.apisupport.project.metainf;
042:
043: import java.awt.Component;
044: import java.awt.Dimension;
045: import java.awt.Toolkit;
046: import java.io.File;
047: import java.io.IOException;
048: import java.util.Collections;
049: import java.util.Set;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052: import org.netbeans.api.project.Project;
053: import org.netbeans.modules.apisupport.project.Util;
054: import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
055: import org.openide.filesystems.FileObject;
056:
057: /**
058: * Wrapper for testing purpose. I am too lazy to initialize NbModuleProject project in unittests
059: * so in order to I created special usecase when the project is null
060: * @author pzajac
061: */
062: class SUtil {
063: static final String LOG_SET_KEYS = "setting keys";
064: static final String LOG_COMPUTE_KEYS = "computing services keys";
065: static final String LOG_END_COMPUTE_KEYS = "compute keys finished";
066: static final String LOG_SERVICE_NODE_HANDLER_ADD_NOTIFY = "ServiceNodeHandler.addNotify()";
067: static Logger logger;
068:
069: static FileObject projectDir;
070: static Set<File> platformJars;
071:
072: /** Creates a new instance of SUtil */
073: public SUtil() {
074: }
075:
076: static Logger getLogger() {
077: if (logger == null) {
078: logger = Logger.getLogger("apisupport.meta-inf"); // NOI18N
079: logger.setLevel(Level.OFF);
080: }
081: return logger;
082: }
083:
084: static void log(String message) {
085: getLogger().log(Level.INFO, message);
086: }
087:
088: // static String getCodeNameBase(NbModuleProject nbModuleProject) {
089: // return (nbModuleProject == null) ?
090: // codeBaseName :
091: // nbModuleProject.getCodeNameBase();
092: // }
093:
094: // static FileObject getServicesFolder(NbModuleProject project) {
095: // FileObject dir = (project != null) ? project.getProjectDirectory() : projectDir;
096: // return (dir != null) ?
097: // dir.getFileObject("src/" + Service.META_INF_SERVICES):
098: // null;
099: // }
100:
101: static FileObject getServicesFolder(Project project, boolean create)
102: throws IOException {
103: NbModuleProvider info = project.getLookup().lookup(
104: NbModuleProvider.class);
105: FileObject srcDir = project.getProjectDirectory()
106: .getFileObject(info.getResourceDirectoryPath(false));
107: if (srcDir != null) {
108: FileObject services = srcDir
109: .getFileObject("META-INF/services"); //NOI18N
110: if (services == null && create) {
111: FileObject fo = srcDir.getFileObject("META-INF"); //NOI18N
112: if (fo == null) {
113: fo = srcDir.createFolder("META-INF"); //NOI18N
114: }
115: services = fo.createFolder("services"); //NOI18N
116: }
117: return services;
118: } else {
119: Util.err.log(project.getProjectDirectory().getPath()
120: + " doesn't contain source directory.");
121: return null;
122: }
123: }
124:
125: static Set<File> getPlatformJars() {
126: if (platformJars == null) {
127: return Collections.emptySet();
128: } else {
129: return platformJars;
130: }
131: }
132:
133: /**
134: * Centers the component <CODE>c</CODE> on the screen.
135: *
136: * @param c the component to center
137: * @see #centerComponent(Component, Component)
138: */
139: public static void centerComponent(Component c) {
140: centerComponent(c, null);
141: }
142:
143: /**
144: * Centers the component <CODE>c</CODE> on component <CODE>p</CODE>.
145: * If <CODE>p</CODE> is <CODE>null</CODE>, the component <CODE>c</CODE>
146: * will be centered on the screen.
147: *
148: * @param c the component to center
149: * @param p the parent component to center on or null for screen
150: * @see #centerComponent(Component)
151: */
152: public static void centerComponent(Component c, Component p) {
153: if (c == null) {
154: return;
155: }
156: Dimension d = (p != null ? p.getSize() : Toolkit
157: .getDefaultToolkit().getScreenSize());
158: c.setLocation(Math.max(0, (d.getSize().width / 2)
159: - (c.getSize().width / 2)), Math.max(0,
160: (d.getSize().height / 2) - (c.getSize().height / 2)));
161: }
162:
163: // public static Project getProject(Node[] nodes) {
164: // ArrayList dobjs = new ArrayList();
165: // Project prj = null;
166: // if (nodes.length == 1) {
167: // prj = (Project) nodes[0].getLookup().lookup(Project.class);
168: // }
169: // if (prj == null) {
170: // for(int n = 0 ; n < nodes.length ; n++) {
171: // DataObject dobj = (DataObject) nodes[n].getCookie(DataObject.class);
172: // if (dobj == null) {
173: // return null;
174: // }
175: // dobjs.add(dobj);
176: // }
177: // prj = getProject(dobjs);
178: // }
179: // return prj;
180: // }
181:
182: }
|