001: // WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package net.ar.webonswing.managers.names;
019:
020: import java.text.*;
021:
022: import net.ar.webonswing.helpers.*;
023: import net.ar.webonswing.wrapping.*;
024:
025: import org.apache.commons.lang.*;
026:
027: /**
028: *
029: * Nombra los componentes de la siguiente forma: si el numero de rama es menor
030: * o igual a 52 entonces usa una letra para identificarla, y si es mayor usar
031: * un numero con el siguiente formato "XXX". Y luego le concatena un "_"
032: * seguido del nombre del componente con el numero de rama.
033: *
034: * ej: aabac_067eb.JButton02
035: *
036: * @author Fernando Damian Petrola
037: */
038: public class CharacterComponentNameManager extends
039: ComponentVisitorAdapter implements ComponentNameManager {
040: public static final String UNIQUE_NAME_PROPERTY = "theUniqueName";
041: public static final String USER_UNIQUE_NAME_PROPERTY = "theUserUniqueName";
042:
043: int theCurrentChildNumber;
044: protected static final char SEPARATOR = '_';
045: protected DecimalFormat digitsFormat = new DecimalFormat("#000");
046:
047: public VisualComponent findComponentWithNameIn(
048: String aComponentName, VisualComponent aRootComponent) {
049: try {
050: VisualComponent theContainer = null;
051:
052: theContainer = aRootComponent;
053: String[] theParts = StringUtils.split(aComponentName,
054: SEPARATOR + "");
055:
056: if (theParts.length < 2)
057: return null;
058:
059: boolean isValidName = true;
060:
061: for (int i = 1; i < theParts[0].length() && isValidName; i++) {
062: int theComponentNumber;
063: char theChar = theParts[0].charAt(i);
064:
065: if (Character.isDigit(theChar)) {
066: theComponentNumber = digitsFormat.parse(
067: theParts[0].substring(i, i + 4)).intValue();
068: i += 3;
069: } else
070: theComponentNumber = CharacterConverter.charToPosition[theChar];
071:
072: isValidName = theComponentNumber < theContainer
073: .getChildCount();
074: if (isValidName)
075: theContainer = theContainer
076: .getChildAt(theComponentNumber);
077: }
078:
079: return theContainer;
080: } catch (ParseException e) {
081: throw new WebOnSwingException(e);
082: }
083: }
084:
085: public void assignNamesFrom(VisualComponent aComponent) {
086: theCurrentChildNumber = 0;
087: aComponent.accept(this );
088: }
089:
090: public void visitComponentBegin(VisualComponent aComponent) {
091: String theHead = pathToString(aComponent.getParent() != null
092: && !(aComponent.getParent() instanceof VisualWindow) ? aComponent
093: .getParent().getName().substring(
094: 0,
095: aComponent.getParent().getName().indexOf(
096: SEPARATOR))
097: : "");
098:
099: if (aComponent.getName() == null
100: || aComponent.getName().length() == 0)
101: aComponent.setName(theHead + SEPARATOR
102: + getTail(aComponent));
103:
104: theCurrentChildNumber = 0;
105: }
106:
107: public void visitComponentEnd(VisualComponent aComponent) {
108: try {
109: int separatorIndex = aComponent.getName()
110: .indexOf(SEPARATOR);
111: char lastChar = aComponent.getName().charAt(
112: separatorIndex - 1);
113:
114: if (Character.isDigit(lastChar))
115: theCurrentChildNumber = digitsFormat.parse(
116: aComponent.getName().substring(
117: separatorIndex - 3, separatorIndex))
118: .intValue() + 1;
119: else
120: theCurrentChildNumber = CharacterConverter.charToPosition[lastChar] + 1;
121: } catch (ParseException e) {
122: throw new WebOnSwingException(e);
123: }
124: }
125:
126: public String getTail(VisualComponent aComponent) {
127: String theTail = (String) aComponent
128: .getClientProperty(USER_UNIQUE_NAME_PROPERTY);
129:
130: if (theTail == null)
131: theTail = aComponent.getTypeId().substring(
132: aComponent.getTypeId().lastIndexOf('.') + 1)
133: + theCurrentChildNumber;
134:
135: return theTail;
136: }
137:
138: protected String pathToString(String aParentPath) {
139: String theResult = "";
140:
141: if (theCurrentChildNumber < CharacterConverter.positionToChar.length)
142: theResult = aParentPath
143: + CharacterConverter.positionToChar[theCurrentChildNumber];
144: else
145: theResult = aParentPath
146: + digitsFormat.format(theCurrentChildNumber);
147:
148: return theResult;
149: }
150:
151: public static class CharacterConverter {
152: static public char[] positionToChar = { 'a', 'b', 'c', 'd',
153: 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
154: 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
155: 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
156: 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
157: 'W', 'X', 'Y', 'Z' };
158: static public int[] charToPosition = new CharacterConverter()
159: .getIntMapping();
160:
161: public int[] getIntMapping() {
162: int[] theIntMap = new int[300];
163:
164: for (int i = 0; i < positionToChar.length; i++)
165: theIntMap[positionToChar[i]] = i;
166:
167: return theIntMap;
168: }
169: }
170: }
|