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-2007 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.uml.resources.images;
042:
043: import java.io.File;
044: import java.io.FileInputStream;
045: import java.io.FileNotFoundException;
046: import java.net.URL;
047: import javax.swing.Icon;
048: import javax.swing.ImageIcon;
049:
050: import org.netbeans.modules.uml.core.metamodel.diagrams.IDiagramKind;
051: import org.openide.modules.InstalledFileLocator;
052:
053: import org.openide.util.Exceptions;
054:
055: /**
056: *
057: * @author Craig Conover, craig.conover@sun.com
058: */
059: public class ImageUtil {
060: public final static String EXT_PNG = ".png"; // NOI18N
061: public final static String EXT_GIF = ".gif"; // NOI18N
062:
063: public final static String IMAGE_FOLDER_FOR_OS = "org"
064: + File.separatorChar + "netbeans"
065: + File.separatorChar
066: + // NOI18N
067: "modules" + File.separatorChar + "uml"
068: + File.separatorChar
069: + // NOI18N
070: "resources" + File.separatorChar + "images"
071: + File.separatorChar; // NOI18N
072:
073: public final static String IMAGE_FOLDER = "org/netbeans/modules/uml/resources/images/"; // NOI18N
074:
075: public final static String IMAGE_PACKAGE = "org.netbeans.modules.uml.resources.images"; // NOI18N
076:
077: public static final String DIAGRAM_ICON_DEFAULT = "diagrams-root-node.png"; // NOI18N
078:
079: public static final String DIAGRAM_ICON_ACTIVITY = "activity-diagram.png"; // NOI18N
080:
081: public static final String DIAGRAM_ICON_CLASS = "class-diagram.png"; // NOI18N
082:
083: public static final String DIAGRAM_ICON_COLLABORATION = "collaboration-diagram.png"; // NOI18N
084:
085: public static final String DIAGRAM_ICON_COMPONENT = "component-diagram.png"; // NOI18N
086:
087: public static final String DIAGRAM_ICON_DEPLOYMENT = "deployment-diagram.png"; // NOI18N
088:
089: public static final String DIAGRAM_ICON_SEQUENCE = "sequence-diagram.png"; // NOI18N
090:
091: public static final String DIAGRAM_ICON_STATE = "state-diagram.png"; // NOI18N
092:
093: public static final String DIAGRAM_ICON_USECASE = "use-case-diagram.png"; // NOI18N
094:
095: private ImageUtil() {
096: }
097:
098: private static ImageUtil self = null;
099:
100: public static ImageUtil instance() {
101: if (self == null)
102: self = new ImageUtil();
103:
104: return self;
105: }
106:
107: public FileInputStream getImageFileInputStream(String imageName) {
108: try {
109: java.net.URL url = getClass().getResource(imageName);
110:
111: if (url == null) {
112: throw new ImageUtil.ImageFileNotFoundException(
113: "Image file \"" + imageName
114: + // NOI18N
115: "\" could not be found in the images folder: "
116: + // NOI18N
117: IMAGE_FOLDER);
118: }
119:
120: return new java.io.FileInputStream(url.getPath());
121: }
122:
123: catch (FileNotFoundException ex) {
124: Exceptions.printStackTrace(ex);
125: return null;
126: }
127: }
128:
129: public File getImageFile(String imageName) {
130: return InstalledFileLocator.getDefault().locate(imageName,
131: "org.netbeans.modules.uml", false); // NOI18N
132: }
133:
134: public Icon getIcon(String imageName) {
135: URL url = getClass().getResource(imageName);
136: if (url == null) {
137: throw new ImageFileNotFoundException("Image file \""
138: + imageName
139: + // NOI18N
140: "\" could not be found in the images folder: "
141: + IMAGE_FOLDER); // NOI18N
142: }
143:
144: return new ImageIcon(url);
145: }
146:
147: public Icon getIcon(String imageName, boolean isExtSpecified) {
148: if (isExtSpecified)
149: return getIcon(imageName);
150:
151: else {
152: Icon icon = getIcon(imageName + EXT_PNG);
153:
154: if (icon == null)
155: icon = getIcon(imageName + EXT_GIF);
156:
157: return icon;
158: }
159: }
160:
161: public String getDiagramTypeImageName(int diagramKind) {
162: switch (diagramKind) {
163: case IDiagramKind.DK_ACTIVITY_DIAGRAM:
164: return DIAGRAM_ICON_ACTIVITY;
165:
166: case IDiagramKind.DK_CLASS_DIAGRAM:
167: return DIAGRAM_ICON_CLASS;
168:
169: case IDiagramKind.DK_COLLABORATION_DIAGRAM:
170: return DIAGRAM_ICON_COLLABORATION;
171:
172: case IDiagramKind.DK_COMPONENT_DIAGRAM:
173: return DIAGRAM_ICON_COMPONENT;
174:
175: case IDiagramKind.DK_DEPLOYMENT_DIAGRAM:
176: return DIAGRAM_ICON_DEPLOYMENT;
177:
178: case IDiagramKind.DK_SEQUENCE_DIAGRAM:
179: return DIAGRAM_ICON_SEQUENCE;
180:
181: case IDiagramKind.DK_STATE_DIAGRAM:
182: return DIAGRAM_ICON_STATE;
183:
184: case IDiagramKind.DK_USECASE_DIAGRAM:
185: return DIAGRAM_ICON_USECASE;
186:
187: default: // IDiagramKind.DK_DIAGRAM
188: return DIAGRAM_ICON_DEFAULT;
189: }
190: }
191:
192: public Icon getDiagramTypeIcon(int diagramKind) {
193: return getIcon(getDiagramTypeImageName(diagramKind));
194: }
195:
196: class ImageFileNotFoundException extends RuntimeException {
197: public ImageFileNotFoundException() {
198: super ();
199: }
200:
201: public ImageFileNotFoundException(String message) {
202: super(message);
203: }
204:
205: }
206: }
|