001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * Portions Copyrighted 2007 Sun Microsystems, Inc.
016: */
017: package org.netbeans.modules.visualweb.project.jsf.ui;
018:
019: import java.awt.Image;
020: import java.awt.Toolkit;
021: import java.io.IOException;
022: import java.net.URL;
023: import java.util.zip.ZipEntry;
024: import java.util.zip.ZipInputStream;
025: import org.openide.ErrorManager;
026: import org.openide.filesystems.FileLock;
027: import org.openide.filesystems.FileObject;
028: import org.openide.filesystems.FileUtil;
029: import org.openide.util.NbBundle;
030:
031: /**
032: *
033: * @author winstonp
034: */
035: public class PageLayoutData {
036:
037: FileObject templateFileObject;
038:
039: public PageLayoutData(FileObject fileObject) {
040: templateFileObject = fileObject;
041: }
042:
043: public String getName() {
044: String bundleName = (String) templateFileObject
045: .getAttribute("SystemFileSystem.localizingBundle");
046: String nameKey = (String) templateFileObject
047: .getAttribute("name");
048: if (nameKey != null) {
049: try {
050: return NbBundle.getBundle(bundleName)
051: .getString(nameKey);
052: } catch (Exception exc) {
053: // OK no bundle key found, ignore the exception and just use key as name value
054: // Exception not logged because it is not mandatory to specify value in the bundle
055: return nameKey;
056: }
057: } else {
058: return templateFileObject.getName();
059: }
060: }
061:
062: public int getPosition() {
063: return (Integer) templateFileObject.getAttribute("position");
064: }
065:
066: public FileObject getFileObject() {
067: return templateFileObject;
068: }
069:
070: public Image getIcon() {
071: return loadImage("icon");
072: }
073:
074: public Image getPreviewImage() {
075: return loadImage("previewImage");
076: }
077:
078: public String getDescription() {
079: String bundleName = (String) templateFileObject
080: .getAttribute("SystemFileSystem.localizingBundle");
081: String descriptionKey = (String) templateFileObject
082: .getAttribute("description");
083: if (descriptionKey != null) {
084: try {
085: return NbBundle.getBundle(bundleName).getString(
086: descriptionKey);
087: } catch (Exception exc) {
088: // OK no bundle key found, ignore the exception and just use key as description value
089: // Exception not logged because it is not mandatory to specify value in the bundle
090: return descriptionKey;
091: }
092: } else {
093: return NbBundle.getMessage(PageLayoutData.class,
094: "NO_PREVIEW_TEXT");
095: }
096: }
097:
098: public boolean isPageLayoutTemplate() {
099: if (templateFileObject.getAttribute("pageLayoutTemplate") != null) {
100: return (Boolean) templateFileObject
101: .getAttribute("pageLayoutTemplate");
102: } else {
103: return false;
104: }
105: }
106:
107: private Image loadImage(String attributeName) {
108: Object value = templateFileObject.getAttribute(attributeName);
109: if (value instanceof Image) {
110: return (Image) value;
111: }
112: if (value instanceof URL) {
113: try {
114: return Toolkit.getDefaultToolkit()
115: .getImage((URL) value);
116: } catch (Exception e) {
117: ErrorManager.getDefault().notify(e);
118: }
119: }
120: return null;
121: }
122:
123: public void copyResources(FileObject resourceRoot)
124: throws IOException {
125: if (templateFileObject.getAttribute("resources") != null) {
126: URL resourceZipUrl = (URL) templateFileObject
127: .getAttribute("resources");
128: ZipInputStream zipInputStream = null;
129: try {
130: zipInputStream = new ZipInputStream(resourceZipUrl
131: .openConnection().getInputStream());
132: ZipEntry zipEntry;
133: while ((zipEntry = zipInputStream.getNextEntry()) != null) {
134: String entryName = zipEntry.getName();
135: if (zipEntry.isDirectory()) {
136: FileUtil.createFolder(resourceRoot, entryName);
137: } else {
138: FileObject file = FileUtil.createData(
139: resourceRoot, entryName);
140: FileLock lock = file.lock();
141: FileUtil.copy(zipInputStream, file
142: .getOutputStream(lock));
143: lock.releaseLock();
144: }
145: }
146: } finally {
147: if (zipInputStream != null) {
148: zipInputStream.close();
149: }
150: }
151: }
152: }
153: }
|