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: * ScreenSizeHelper.java
043: *
044: * Created on July 3, 2006, 3:29 PM
045: *
046: * To change this template, choose Tools | Template Manager
047: * and open the template in the editor.
048: */
049:
050: package org.netbeans.modules.mobility.svgcore.export;
051:
052: import java.awt.Dimension;
053: import org.netbeans.api.java.platform.JavaPlatform;
054: import org.netbeans.api.java.platform.JavaPlatformManager;
055: import org.netbeans.api.java.platform.Specification;
056: import org.netbeans.api.project.FileOwnerQuery;
057: import org.netbeans.api.project.Project;
058: import org.netbeans.modules.mobility.cldcplatform.J2MEPlatform;
059: import org.netbeans.modules.mobility.project.J2MEProject;
060: import org.netbeans.modules.mobility.project.ProjectConfigurationsHelper; //import org.netbeans.modules.mobility.project.ui.customizer.J2MEProjectProperties;
061: import org.netbeans.modules.mobility.project.DefaultPropertiesDescriptor;
062: import org.netbeans.spi.project.support.ant.AntProjectHelper;
063: import org.netbeans.spi.project.support.ant.EditableProperties;
064: import org.openide.filesystems.FileObject;
065:
066: /**
067: *
068: * @author suchys
069: */
070: public class ScreenSizeHelper {
071:
072: /**
073: * Gets current device screen size (the device which is currently used
074: * in the project)
075: *
076: */
077: public static Dimension getCurrentDeviceScreenSize(
078: FileObject primaryFile, String configuration) {
079: Project p = FileOwnerQuery.getOwner(primaryFile);
080: if (p == null || !(p instanceof J2MEProject)) {
081: return new Dimension(320, 240);
082: }
083: Dimension dim = getDeviceScreenSizeFromProject((J2MEProject) p,
084: configuration);
085: if (dim == null || dim.getHeight() < 10.0
086: || dim.getWidth() < 10.0) {
087: return new Dimension(320, 240);
088: }
089: return dim;
090: }
091:
092: /**
093: * Gets currently used device screen size from J2ME project
094: * @param project
095: * @return
096: */
097: private static Dimension getDeviceScreenSizeFromProject(
098: J2MEProject project, String configuration) {
099: AntProjectHelper helper = (AntProjectHelper) project
100: .getLookup().lookup(AntProjectHelper.class);
101: EditableProperties ep = helper
102: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
103: ProjectConfigurationsHelper confs = project
104: .getConfigurationHelper();
105: String activeConfiguration = configuration;
106:
107: String platformActive = evaluateProperty(ep,
108: DefaultPropertiesDescriptor.PLATFORM_ACTIVE,
109: activeConfiguration);
110: String deviceActive = evaluateProperty(ep,
111: DefaultPropertiesDescriptor.PLATFORM_DEVICE,
112: activeConfiguration);
113:
114: if (platformActive != null && deviceActive != null) {
115: JavaPlatform[] platforms = JavaPlatformManager.getDefault()
116: .getPlatforms(
117: null,
118: new Specification(
119: J2MEPlatform.SPECIFICATION_NAME,
120: null));
121: J2MEPlatform platform = null;
122:
123: if (platforms != null)
124: for (int i = 0; i < platforms.length; i++) {
125: JavaPlatform javaPlatform = platforms[i];
126: if (javaPlatform instanceof J2MEPlatform) {
127: if (platformActive
128: .equals((((J2MEPlatform) javaPlatform)
129: .getName()))) {
130: platform = (J2MEPlatform) javaPlatform;
131: break;
132: }
133: }
134: }
135:
136: if (platform != null) {
137: J2MEPlatform.Device[] devices = platform.getDevices();
138: if (devices != null)
139: for (int i = 0; i < devices.length; i++) {
140: J2MEPlatform.Device device = devices[i];
141: if (deviceActive.equals(device.getName())) {
142: J2MEPlatform.Screen screen = device
143: .getScreen();
144: if (screen != null) {
145: Integer height = screen.getHeight();
146: Integer width = screen.getWidth();
147: if (height != null && width != null)
148: return new Dimension(width
149: .intValue(), height
150: .intValue());
151: }
152: }
153:
154: }
155: }
156: }
157: return null;
158: }
159:
160: private static String evaluateProperty(EditableProperties ep,
161: String propertyName, String configuration) {
162: if (configuration == null)
163: return ep.getProperty(propertyName);
164: String value = ep.getProperty("configs." + configuration + "."
165: + propertyName); // NOI18N
166: return value != null ? value : evaluateProperty(ep,
167: propertyName, null);
168: }
169:
170: }
|