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.vmd.api.io.javame;
043:
044: import org.netbeans.api.java.platform.JavaPlatform;
045: import org.netbeans.api.java.platform.JavaPlatformManager;
046: import org.netbeans.api.java.platform.Specification;
047: import org.netbeans.api.project.Project;
048: import org.netbeans.modules.mobility.cldcplatform.J2MEPlatform;
049: import org.netbeans.modules.mobility.project.DefaultPropertiesDescriptor;
050: import org.netbeans.modules.mobility.project.J2MEProject;
051: import org.netbeans.modules.mobility.project.ProjectConfigurationsHelper;
052: import org.netbeans.modules.vmd.api.io.DataObjectContext;
053: import org.netbeans.modules.vmd.api.io.ProjectUtils;
054: import org.netbeans.modules.vmd.api.model.Debug;
055: import org.netbeans.spi.project.support.ant.AntProjectEvent;
056: import org.netbeans.spi.project.support.ant.AntProjectHelper;
057: import org.netbeans.spi.project.support.ant.AntProjectListener;
058: import org.netbeans.spi.project.support.ant.EditableProperties;
059:
060: import java.awt.*;
061: import java.util.HashMap;
062:
063: /**
064: * @author David Kaspar
065: */
066: public class MidpProjectPropertiesSupportImpl {
067:
068: private static final HashMap<DeviceListener, AntProjectListener> deviceListeners = new HashMap<DeviceListener, AntProjectListener>();
069:
070: static Dimension getDeviceScreenSizeFromProject(
071: DataObjectContext context) {
072: return getDeviceScreenSizeFromProject((J2MEProject) ProjectUtils
073: .getProject(context));
074: }
075:
076: private static Dimension getDeviceScreenSizeFromProject(
077: J2MEProject project) {
078: AntProjectHelper helper = project.getLookup().lookup(
079: AntProjectHelper.class);
080: EditableProperties ep = helper
081: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
082: ProjectConfigurationsHelper confs = project
083: .getConfigurationHelper();
084: String activeConfiguration = confs.getActiveConfiguration() != confs
085: .getDefaultConfiguration() ? confs
086: .getActiveConfiguration().getDisplayName() : null;
087:
088: String platformActive = evaluateProperty(ep,
089: DefaultPropertiesDescriptor.PLATFORM_ACTIVE,
090: activeConfiguration);
091: String deviceActive = evaluateProperty(ep,
092: DefaultPropertiesDescriptor.PLATFORM_DEVICE,
093: activeConfiguration);
094: if (platformActive != null && deviceActive != null) {
095: JavaPlatform[] platforms = JavaPlatformManager.getDefault()
096: .getPlatforms(
097: null,
098: new Specification(
099: J2MEPlatform.SPECIFICATION_NAME,
100: null));
101: J2MEPlatform platform = null;
102:
103: if (platforms != null)
104: for (JavaPlatform javaPlatform : platforms) {
105: if (javaPlatform instanceof J2MEPlatform) {
106: if (platformActive
107: .equals((((J2MEPlatform) javaPlatform)
108: .getName()))) {
109: platform = (J2MEPlatform) javaPlatform;
110: break;
111: }
112: }
113: }
114:
115: if (platform != null) {
116: J2MEPlatform.Device[] devices = platform.getDevices();
117: if (devices != null)
118: for (J2MEPlatform.Device device : devices) {
119: if (deviceActive.equals(device.getName())) {
120: J2MEPlatform.Screen screen = device
121: .getScreen();
122: if (screen != null) {
123: Integer height = screen.getHeight();
124: Integer width = screen.getWidth();
125: if (height != null && width != null)
126: return new Dimension(width, height);
127: }
128: }
129:
130: }
131: }
132: }
133: return null;
134: }
135:
136: public static String evaluateProperty(EditableProperties ep,
137: String propertyName, String configuration) {
138: if (configuration == null)
139: return ep.getProperty(propertyName);
140: String value = ep.getProperty("configs." + configuration + "."
141: + propertyName); // NOI18N
142: return value != null ? value : evaluateProperty(ep,
143: propertyName, null);
144: }
145:
146: public static void addDeviceListener(DataObjectContext context,
147: DeviceListener listener) {
148: Project project = ProjectUtils.getProject(context);
149: if (project == null)
150: return;
151: AntProjectHelper helper = project.getLookup().lookup(
152: AntProjectHelper.class);
153: if (helper == null)
154: return;
155:
156: if (deviceListeners.containsKey(listener))
157: Debug
158: .warning("DeviceListener already registered",
159: listener); // NOI18N
160: DeviceAntProjectListener antListener = new DeviceAntProjectListener(
161: listener);
162: helper.addAntProjectListener(antListener);
163: deviceListeners.put(listener, antListener);
164: }
165:
166: public static void removeDeviceListener(DataObjectContext context,
167: DeviceListener listener) {
168: Project project = ProjectUtils.getProject(context);
169: if (project == null)
170: return;
171: AntProjectHelper helper = project.getLookup().lookup(
172: AntProjectHelper.class);
173: if (helper == null)
174: return;
175:
176: AntProjectListener antListener = deviceListeners
177: .remove(listener);
178: if (antListener != null)
179: helper.removeAntProjectListener(antListener);
180: else
181: Debug.warning("DeviceListener not registered", listener); // NOI18N
182: }
183:
184: public static boolean isMobileProject(Project project) {
185: return project instanceof J2MEProject; // "J2MEProject".equals (prj.getClass ().getSimpleName ()); // NOI18N
186: }
187:
188: public static String getActiveConfiguration(Project project) {
189: ProjectConfigurationsHelper confs = project.getLookup().lookup(
190: ProjectConfigurationsHelper.class);
191: return confs.getActiveConfiguration() != confs
192: .getDefaultConfiguration() ? confs
193: .getActiveConfiguration().getDisplayName() : null;
194: }
195:
196: public static void setProperty(EditableProperties ep,
197: String propertyName, String configuration,
198: String propertyValue) {
199: if (configuration == null)
200: ep.put(propertyName, propertyValue);
201: else
202: ep.put("configs." + configuration + "." + propertyName,
203: propertyValue); // NOI18N
204: }
205:
206: private static class DeviceAntProjectListener implements
207: AntProjectListener {
208:
209: private DeviceListener listener;
210:
211: public DeviceAntProjectListener(DeviceListener listener) {
212: this .listener = listener;
213: }
214:
215: public void configurationXmlChanged(AntProjectEvent ev) {
216: listener.deviceChanged();
217: }
218:
219: public void propertiesChanged(AntProjectEvent ev) {
220: listener.deviceChanged();
221: }
222:
223: }
224:
225: }
|