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: package org.netbeans.modules.vmd.game.integration.components;
042:
043: import org.netbeans.modules.vmd.api.model.PrimitiveDescriptor;
044: import org.netbeans.modules.vmd.api.model.PrimitiveDescriptorFactory;
045: import org.netbeans.modules.vmd.game.GameController;
046: import org.netbeans.modules.vmd.midp.components.MidpPrimitiveDescriptor;
047:
048: import java.awt.*;
049: import java.util.StringTokenizer;
050:
051: /**
052: * @author David Kaspar, Karel Herink
053: */
054: // HINT - after making change, update GameCodeSupport too
055: public final class GamePrimitiveDescriptor implements
056: PrimitiveDescriptorFactory {
057:
058: static final String TYPEID_STRING_TILES = "#TiledLayerTiles"; // NOI18N
059: static final String TYPEID_STRING_FRAMES = "#SequenceFrames"; // NOI18N
060: static final String TYPEID_STRING_POINT = "#Point"; // NOI18N
061:
062: static final TilesPrimitiveDescriptor PRIMITIVE_DESCRIPTOR_TILES = new TilesPrimitiveDescriptor();
063: static final FramesPrimitiveDescriptor PRIMITIVE_DESCRIPTOR_FRAMES = new FramesPrimitiveDescriptor();
064: static final PointPrimitiveDescriptor PRIMITIVE_DESCRIPTOR_POINT = new PointPrimitiveDescriptor();
065:
066: private MidpPrimitiveDescriptor midp = new MidpPrimitiveDescriptor();
067:
068: public String getProjectType() {
069: return GameController.PROJECT_TYPE_GAME;
070: }
071:
072: public PrimitiveDescriptor getDescriptorForTypeIDString(
073: String string) {
074: if (TYPEID_STRING_TILES.equals(string))
075: return PRIMITIVE_DESCRIPTOR_TILES;
076: if (TYPEID_STRING_FRAMES.equals(string))
077: return PRIMITIVE_DESCRIPTOR_FRAMES;
078: if (TYPEID_STRING_POINT.equals(string))
079: return PRIMITIVE_DESCRIPTOR_POINT;
080: //TODO
081: return midp.getDescriptorForTypeIDString(string);
082: }
083:
084: private static class PointPrimitiveDescriptor implements
085: PrimitiveDescriptor {
086:
087: public String serialize(Object value) {
088: Point point = (Point) value;
089: StringBuilder sb = new StringBuilder();
090: sb.append(Integer.toString((int) point.getX()));
091: sb.append(",");
092: sb.append(Integer.toString((int) point.getY()));
093: return sb.toString();
094: }
095:
096: public Object deserialize(String serialized) {
097: String[] xAndY = serialized.split(",");
098: Point p = new Point(Integer.parseInt(xAndY[0]), Integer
099: .parseInt(xAndY[1]));
100: return p;
101: }
102:
103: public boolean isValidInstance(Object object) {
104: return (object instanceof Point);
105: }
106: }
107:
108: private static class FramesPrimitiveDescriptor implements
109: PrimitiveDescriptor {
110:
111: public String serialize(Object value) {
112: int[] array = (int[]) value;
113: StringBuilder serialized = new StringBuilder();
114: serialized.append(array.length).append(","); // NOI18N
115: for (int cell : array) {
116: serialized.append(cell).append(','); // NOI18N
117: }
118: return serialized.toString();
119: }
120:
121: public Object deserialize(String serialized) {
122: String[] tokens = serialized.split(",");
123: int length = Integer.parseInt(tokens[0]);
124: int[] array = new int[length];
125: for (int i = 0; i < length; i++) {
126: array[i] = Integer.parseInt(tokens[i + 1]);
127: }
128: return array;
129: }
130:
131: public boolean isValidInstance(Object object) {
132: return object instanceof int[];
133: }
134:
135: }
136:
137: // HINT - describes serialization of the TiledLayer.tiles property
138: private static class TilesPrimitiveDescriptor implements
139: PrimitiveDescriptor {
140:
141: public String serialize(Object value) {
142: int[][] array = (int[][]) value;
143: StringBuffer serialized = new StringBuffer();
144: int rows = array.length;
145: int cols = array.length > 0 ? array[0].length : 0;
146: serialized.append(rows).append(',').append(cols);
147: for (int[] row : array)
148: for (int cell : row)
149: serialized.append(',').append(cell);
150: return serialized.toString();
151: }
152:
153: public Object deserialize(String serialized) {
154: StringTokenizer tokenizer = new StringTokenizer(serialized,
155: ",");
156: int rows = Integer.parseInt(tokenizer.nextToken());
157: int cols = Integer.parseInt(tokenizer.nextToken());
158: int[][] array = new int[rows][cols];
159: for (int y = 0; y < rows; y++)
160: for (int x = 0; x < cols; x++)
161: array[y][x] = Integer.parseInt(tokenizer
162: .nextToken());
163: return array;
164: }
165:
166: public boolean isValidInstance(Object object) {
167: return object instanceof int[][];
168: }
169:
170: }
171: }
|