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.midp.screen.display;
043:
044: import org.netbeans.modules.vmd.api.model.Debug;
045: import org.netbeans.modules.vmd.api.model.DesignComponent;
046: import org.netbeans.modules.vmd.api.model.DesignDocument;
047: import org.netbeans.modules.vmd.api.screen.display.ScreenDeviceInfo;
048: import org.netbeans.modules.vmd.api.screen.display.DeviceTheme.FontFace;
049: import org.netbeans.modules.vmd.api.screen.display.DeviceTheme.FontSize;
050: import org.netbeans.modules.vmd.api.screen.display.DeviceTheme.FontStyle;
051: import org.netbeans.modules.vmd.api.screen.display.DeviceTheme.FontType;
052: import org.netbeans.modules.vmd.api.screen.display.ScreenDeviceInfoPresenter;
053: import org.netbeans.modules.vmd.midp.components.MidpProjectSupport;
054: import org.netbeans.modules.vmd.midp.components.MidpTypes;
055: import org.netbeans.modules.vmd.midp.components.resources.FontCD;
056: import org.netbeans.modules.vmd.midp.components.resources.ImageCD;
057: import org.openide.filesystems.FileObject;
058: import javax.imageio.ImageIO;
059: import javax.swing.*;
060: import java.awt.*;
061: import java.awt.image.BufferedImage;
062: import java.io.IOException;
063: import java.util.EnumSet;
064: import java.util.Map;
065: import org.netbeans.modules.vmd.api.model.PropertyValue;
066:
067: /**
068: *
069: * @author Anton Chechel
070: * @version 1.0
071: */
072: public final class ScreenSupport {
073:
074: private ScreenSupport() {
075: }
076:
077: public static Font getFont(DesignComponent fontComponent) {
078: if (fontComponent == null) {
079: return null;
080: }
081: return getFont(getDeviceInfo(fontComponent.getDocument()),
082: fontComponent);
083: }
084:
085: public static Font getFont(DesignDocument document, int kindCode,
086: int faceCode, int styleCode, int sizeCode) {
087: ScreenDeviceInfo deviceInfo = getDeviceInfo(document);
088:
089: if (kindCode == FontCD.VALUE_KIND_DEFAULT) {
090: return deviceInfo.getDeviceTheme()
091: .getFont(FontType.DEFAULT);
092: } else if (kindCode == FontCD.VALUE_KIND_STATIC) {
093: return deviceInfo.getDeviceTheme().getFont(
094: FontType.STATIC_TEXT);
095: } else if (kindCode == FontCD.VALUE_KIND_INPUT) {
096: return deviceInfo.getDeviceTheme().getFont(
097: FontType.INPUT_TEXT);
098: }
099:
100: FontFace face = FontFace.SYSTEM;
101: if (faceCode == FontCD.VALUE_FACE_MONOSPACE) {
102: face = FontFace.MONOSPACE;
103: } else if (faceCode == FontCD.VALUE_FACE_PROPORTIONAL) {
104: face = FontFace.PROPORTIONAL;
105: }
106:
107: EnumSet<FontStyle> style = EnumSet.of(FontStyle.PLAIN);
108: if ((styleCode & FontCD.VALUE_STYLE_BOLD) != 0) {
109: style.add(FontStyle.BOLD);
110: }
111: if ((styleCode & FontCD.VALUE_STYLE_ITALIC) != 0) {
112: style.add(FontStyle.ITALIC);
113: }
114: if ((styleCode & FontCD.VALUE_STYLE_UNDERLINED) != 0) {
115: style.add(FontStyle.UNDERLINED);
116: }
117:
118: FontSize size = FontSize.MEDIUM;
119: if (sizeCode == FontCD.VALUE_SIZE_SMALL) {
120: size = FontSize.SMALL;
121: } else if (sizeCode == FontCD.VALUE_SIZE_LARGE) {
122: size = FontSize.LARGE;
123: }
124:
125: return deviceInfo.getDeviceTheme().getFont(face, style, size);
126: }
127:
128: // TODO Should this method be in VMD Screen Designer module?
129: private static ScreenDeviceInfo getDeviceInfo(
130: final DesignDocument document) {
131: final ScreenDeviceInfo[] screenDevice = new ScreenDeviceInfo[1];
132: if (document == null) {
133: return null;
134: }
135: document.getTransactionManager().readAccess(new Runnable() {
136:
137: public void run() {
138: DesignComponent rootComponent = document
139: .getRootComponent();
140: ScreenDeviceInfoPresenter presenter = rootComponent
141: .getPresenter(ScreenDeviceInfoPresenter.class);
142: if (presenter == null) {
143: throw Debug
144: .error("No ScreenDevice attached to the root component"); //NOI18N
145: }
146: screenDevice[0] = presenter.getScreenDeviceInfo();
147: }
148: });
149: return screenDevice[0];
150: }
151:
152: /**
153: * Returns AWT font according to kind, face, style and size
154: *
155: * @param deviceInfo
156: * @param fontComponent
157: * @return font
158: */
159: public static Font getFont(ScreenDeviceInfo deviceInfo,
160: DesignComponent fontComponent) {
161: if (fontComponent == null) {
162: return deviceInfo.getDeviceTheme()
163: .getFont(FontType.DEFAULT);
164: }
165:
166: PropertyValue value = fontComponent
167: .readProperty(FontCD.PROP_FONT_KIND);
168: int kindCode;
169: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
170: kindCode = MidpTypes.getInteger(value);
171: } else {
172: kindCode = FontCD.VALUE_KIND_DEFAULT;
173: }
174:
175: value = fontComponent.readProperty(FontCD.PROP_FACE);
176: int faceCode;
177: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
178: faceCode = MidpTypes.getInteger(value);
179: } else {
180: faceCode = FontCD.VALUE_FACE_SYSTEM;
181: }
182:
183: value = fontComponent.readProperty(FontCD.PROP_STYLE);
184: int styleCode;
185: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
186: styleCode = MidpTypes.getInteger(value);
187: } else {
188: styleCode = FontCD.VALUE_STYLE_PLAIN;
189: }
190:
191: value = fontComponent.readProperty(FontCD.PROP_SIZE);
192: int sizeCode;
193: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
194: sizeCode = MidpTypes.getInteger(value);
195: } else {
196: sizeCode = FontCD.VALUE_SIZE_MEDIUM;
197: }
198:
199: return getFont(fontComponent.getDocument(), kindCode, faceCode,
200: styleCode, sizeCode);
201: }
202:
203: /**
204: * Loads icon using resourcePath property from given image design component
205: *
206: * @param imageComponent image design component
207: * @return icon
208: */
209: public static Icon getIconFromImageComponent(
210: DesignComponent imageComponent) {
211: if (imageComponent == null) {
212: return null;
213: }
214: PropertyValue value = imageComponent
215: .readProperty(ImageCD.PROP_RESOURCE_PATH);
216: String imagePath = null;
217: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
218: imagePath = MidpTypes.getString(imageComponent
219: .readProperty(ImageCD.PROP_RESOURCE_PATH));
220: }
221:
222: if (imagePath == null) {
223: return null;
224: }
225: DesignDocument document = imageComponent.getDocument();
226:
227: Map<FileObject, FileObject> fileMap = MidpProjectSupport
228: .getFileObjectsForRelativeResourcePath(document,
229: imagePath);
230: if (fileMap == null
231: || fileMap.keySet().iterator().hasNext() == false) {
232: return null;
233: }
234: FileObject imageFileObject = fileMap.keySet().iterator().next();
235: if (imageFileObject != null) {
236: return resolveImageForRoot(imageFileObject, imagePath);
237: }
238: Debug.warning("Resource path property in", imageComponent,
239: "contains incorrect value"); // NOI18N
240: return null;
241: }
242:
243: public static FileObject getFileObjectFromImageComponent(
244: DesignComponent imageComponent) {
245: if (imageComponent == null) {
246: return null;
247: }
248: PropertyValue value = imageComponent
249: .readProperty(ImageCD.PROP_RESOURCE_PATH);
250: String imagePath = null;
251: if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
252: imagePath = MidpTypes.getString(imageComponent
253: .readProperty(ImageCD.PROP_RESOURCE_PATH));
254: }
255:
256: if (imagePath == null) {
257: return null;
258: }
259: DesignDocument document = imageComponent.getDocument();
260:
261: Map<FileObject, FileObject> fileMap = MidpProjectSupport
262: .getFileObjectsForRelativeResourcePath(document,
263: imagePath);
264: if (fileMap == null || !fileMap.keySet().iterator().hasNext()) {
265: return null;
266: }
267: FileObject imageFileObject = fileMap.keySet().iterator().next();
268: if (imageFileObject != null) {
269: return imageFileObject;
270: }
271: Debug.warning("Resource path property in", imageComponent,
272: "contains incorrect value"); // NOI18N
273: return null;
274: }
275:
276: public static int getFontHeight(Graphics g, Font f) {
277: assert (g != null) && (f != null);
278: FontMetrics fm = g.getFontMetrics(f);
279: return fm.getHeight();
280: }
281:
282: private static Icon resolveImageForRoot(FileObject file,
283: String relPath) {
284: try {
285: BufferedImage img = ImageIO.read(file.getInputStream());
286: if (img != null) {
287: return new ImageIcon(img);
288: }
289: } catch (IOException e) {
290: e.printStackTrace();
291: }
292: return null;
293: }
294: }
|