001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/sourcecontrol/NamePool.java,v 1.1 2005/04/20 22:21:21 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is the Java 3D(tm) Scene Graph Editor.
011: * The Initial Developer of the Original Code is Jan Becicka.
012: * Portions created by Jan Becicka are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Jan Becicka.
016: *
017: **/
018: package org.jdesktop.j3dedit.scenegrapheditor.sourcecontrol;
019:
020: import org.jdesktop.j3dfly.namecontrol.NameControl;
021: import java.util.HashMap;
022: import java.lang.reflect.Array;
023:
024: /** Class is used for creating names
025: * @author Jan Becicka
026: * @version
027: */
028: public class NamePool {
029:
030: /** Holds value of property nameControl. */
031: private NameControl nameControl;
032:
033: /** Each object has it's identifier
034: */
035: private HashMap namedObjectPool;
036:
037: /** How many times was used name
038: */
039: private HashMap nameUsage;
040:
041: /** Creates new NamePool
042: * @param n */
043: public NamePool(NameControl n) {
044: nameControl = n;
045: namedObjectPool = new HashMap();
046: nameUsage = new HashMap();
047: }
048:
049: /**
050: */
051: public NamePool() {
052: this (null);
053: }
054:
055: /** Getter for property nameControl.
056: * @return Value of property nameControl.
057: */
058: public NameControl getNameControl() {
059: return nameControl;
060: }
061:
062: /** returns name of given Object
063: * @param o
064: * @return
065: */
066: public String getObjectName(Object o) {
067: String objectName = (String) namedObjectPool.get(o);
068: if (objectName == null) {
069: objectName = createName(o);
070: namedObjectPool.put(o, objectName);
071: }
072: return objectName;
073: }
074:
075: /** Put name
076: */
077: public void put(Object o, String name) {
078: namedObjectPool.put(o, name);
079: }
080:
081: /** e.g. if a is Array of int, it returns int.class
082: * @param a
083: * @return
084: */
085: public static Class getPrimitiveClassFromArray(Object a) {
086: Class clazz = Array.get(a, 0).getClass();
087: if (clazz == Boolean.class)
088: return Boolean.TYPE;
089: if (clazz == Byte.class)
090: return Byte.TYPE;
091: if (clazz == Double.class)
092: return Double.TYPE;
093: if (clazz == Float.class)
094: return Float.TYPE;
095: if (clazz == Character.class)
096: return Character.TYPE;
097: if (clazz == Integer.class)
098: return Integer.TYPE;
099: if (clazz == Long.class)
100: return Long.TYPE;
101: if (clazz == Short.class)
102: return Short.TYPE;
103: return null;
104: }
105:
106: /** creates name for given Class
107: */
108: public String createName(Class o) {
109: String generalName = o.getName();
110: if (o.isArray()) {
111: generalName = generalName.substring(generalName
112: .lastIndexOf('.') + 1, generalName.length() - 1);
113: } else {
114: generalName = generalName.substring(generalName
115: .lastIndexOf('.') + 1);
116: }
117:
118: generalName = firstLower(generalName);
119: Integer in = (Integer) nameUsage.get(generalName);
120: int i;
121: if (in == null) {
122: i = 0;
123: } else {
124: i = in.intValue();
125: }
126: i++;
127: nameUsage.put(generalName, new Integer(i));
128: return generalName + i;
129: }
130:
131: /** creates name for given Object
132: */
133: public String createName(Object o) {
134: if (o.getClass().isArray())
135: if (o.getClass().getName().length() == 2)
136: return createName(getPrimitiveClassFromArray(o));
137: return createName(o.getClass());
138: }
139:
140: /** from TestName creates testName
141: * @param s
142: * @return
143: */
144: public static String firstLower(String s) {
145: return Character.toLowerCase(s.charAt(0)) + s.substring(1);
146: }
147:
148: /** from testName creates TestName
149: * @param s
150: * @return
151: */
152: public static String firstUpper(String s) {
153: return Character.toUpperCase(s.charAt(0)) + s.substring(1);
154: }
155: }
|