001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.graphic.configuration.shape;
051:
052: import java.awt.geom.GeneralPath;
053: import java.util.HashMap;
054:
055: import com.projity.util.ArrayUtils;
056:
057: /**
058: *
059: */
060: public class PredefinedShape {
061: private String name;
062: private double[][] points = null;
063: private double[][][] pointGrid = null;
064:
065: public GeneralPath toGeneralPath(double hScale, double vScale,
066: double hShift, double vShift) {
067: GeneralPath path;
068: if (points != null) {
069: path = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
070: points.length);
071: int x, y;
072: for (int i = 0; i < points.length; i++) {
073: x = r(hScale * points[i][0] + hShift);
074: y = r(vScale * points[i][1] + vShift);
075: if (i == 0)
076: path.moveTo(x, y);
077: else
078: path.lineTo(x, y);
079: }
080: } else {
081: path = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
082: pointGrid.length);
083: int x, y;
084: for (int i = 0; i < pointGrid.length; i++) {
085: x = r(hScale * pointGrid[i][0][0] + vScale
086: * pointGrid[i][0][1] + hShift
087: * pointGrid[i][0][2] + vShift
088: * pointGrid[i][0][3] + hShift);
089: y = r(hScale * pointGrid[i][1][0] + vScale
090: * pointGrid[i][1][1] + hShift
091: * pointGrid[i][1][2] + vShift
092: * pointGrid[i][1][3] + vShift);
093: if (i == 0)
094: path.moveTo(x, y);
095: else
096: path.lineTo(x, y);
097: }
098: }
099: path.closePath();
100: return path;
101: }
102:
103: protected PredefinedShape(String name, double[][] points,
104: double hScale, double vScale, double hShift, double vShift) {
105: this .name = name;
106: this .points = ArrayUtils.clone(points);
107: scale(hScale, vScale);
108: translate(hShift, vShift);
109:
110: }
111:
112: protected PredefinedShape(String name, double[][] points) {
113: this (name, points, 1.0, 1.0, 0.0, 0.0);
114: }
115:
116: protected PredefinedShape(String name, double[][][] matrixPoints) {
117: this .name = name;
118: this .pointGrid = ArrayUtils.clone(matrixPoints);
119: }
120:
121: private void scale(double hScale, double vScale) {
122: for (int i = 0; i < points.length; i++) {
123: points[i][0] *= hScale;
124: points[i][1] *= vScale;
125: }
126: }
127:
128: private void translate(double hShift, double vShift) {
129: for (int i = 0; i < points.length; i++) {
130: points[i][0] += hShift;
131: points[i][1] += vShift;
132: }
133: }
134:
135: private static int r(double d) {
136: return (int) Math.round(d);
137: }
138:
139: private static void add(PredefinedShape predefinedShape) {
140: predefinedShapeMap.put(predefinedShape.name, predefinedShape);
141: }
142:
143: private static final double rectPoints[][] = new double[][] {
144: { 1, .5 }, { 0, .5 }, { 0, -.5 }, { 1, -.5 } };
145:
146: public static final PredefinedShape FULL_HEIGHT = new PredefinedShape(
147: "FULL_HEIGHT", rectPoints);
148:
149: public static final PredefinedShape HALF_HEIGHT_TOP = new PredefinedShape(
150: "HALF_HEIGHT_TOP", rectPoints, 1, .5, 0, -.25);
151:
152: public static final PredefinedShape HALF_HEIGHT_BOTTOM = new PredefinedShape(
153: "HALF_HEIGHT_BOTTOM", rectPoints, 1, .5, 0, .25);
154:
155: public static final PredefinedShape HALF_HEIGHT_CENTER = new PredefinedShape(
156: "HALF_HEIGHT_CENTER", rectPoints, 1, .5, 0, 0);
157:
158: public static final PredefinedShape QUARTER_HEIGHT_CENTER = new PredefinedShape(
159: "QUARTER_HEIGHT_CENTER", rectPoints, 1, .25, 0, 0);
160:
161: public static final PredefinedShape SQUARE = new PredefinedShape(
162: "SQUARE", new double[][] { { .5, .5 }, { -.5, .5 },
163: { -.5, -.5 }, { .5, -.5 }
164:
165: });
166:
167: public static final PredefinedShape DIAMOND = new PredefinedShape(
168: "DIAMOND", new double[][] { { 0, -.5 }, { -.5, 0 },
169: { 0, .5 }, { .5, 0 } });
170:
171: public static final PredefinedShape PENTAGON_UP = new PredefinedShape(
172: "PENTAGON_UP", new double[][] { { 0, -.5 }, { -.5, 0 },
173: { -.5, .5 }, { .5, .5 }, { .5, 0 } });
174:
175: public static final PredefinedShape PENTAGON_DOWN = new PredefinedShape(
176: "PENTAGON_DOWN", new double[][] { { 0, .5 }, { -.5, 0 },
177: { -.5, -.5 }, { .5, -.5 }, { .5, 0 } });
178:
179: public static final PredefinedShape TRIANGLE_UP = new PredefinedShape(
180: "TRIANGLE_UP", new double[][] { { -.5, .5 }, { 0, -.5 },
181: { .5, .5 }, });
182:
183: public static final PredefinedShape TRIANGLE_DOWN = new PredefinedShape(
184: "TRIANGLE_DOWN", new double[][] { { -.5, -.5 }, { 0, .5 },
185: { .5, -.5 }, });
186:
187: public static final PredefinedShape TRIANGLE_RIGHT = new PredefinedShape(
188: "TRIANGLE_RIGHT", new double[][] { { -.5, -.5 },
189: { -.5, .5 }, { .5, 0 }, });
190:
191: public static final PredefinedShape TRIANGLE_LEFT = new PredefinedShape(
192: "TRIANGLE_LEFT", new double[][] { { .5, -.5 }, { .5, .5 },
193: { -.5, 0 }, });
194:
195: public static final PredefinedShape ARROW_UP = new PredefinedShape(
196: "ARROW_UP", new double[][] { { -.2, .5 }, { -.2, 0 },
197: { -.5, 0 }, { 0, -.5 }, { .5, 0 }, { .2, 0 },
198: { .2, .5 }, });
199:
200: public static final PredefinedShape ARROW_DOWN = new PredefinedShape(
201: "ARROW_DOWN", new double[][] { { -.2, -.5 }, { -.2, 0 },
202: { -.5, 0 }, { 0, .5 }, { .5, 0 }, { .2, 0 },
203: { .2, -.5 }, });
204:
205: public static final PredefinedShape LINK_ARROW1 = new PredefinedShape(
206: "LINK_ARROW1", new double[][] { { 0, 0 }, { 1, 1 },
207: { .7, 0 }, { 1, -1 }, { 0, 0 }, });
208:
209: // pert shapes
210: public static final PredefinedShape HEXAGON = new PredefinedShape(
211: "HEXAGON", new double[][][] {
212: { { 1, -.25, 0, 0 }, { 0, .5, 0, 0 } },
213: { { 0, .25, 0, 0 }, { 0, .5, 0, 0 } },
214: { { 0, 0, 0, 0 }, { 0, 0, 0, 0 } },
215: { { 0, .25, 0, 0 }, { 0, -.5, 0, 0 } },
216: { { 1, -.25, 0, 0 }, { 0, -.5, 0, 0 } },
217: { { 1, 0, 0, 0 }, { 0, 0, 0, 0 } }, });
218:
219: public static final PredefinedShape PARALLELOGRAM = new PredefinedShape(
220: "PARALLELOGRAM", new double[][][] {
221: { { 1, 0, 0, 0 }, { 0, -.5, 0, 0 } },
222: { { 0, .25, 0, 0 }, { 0, -.5, 0, 0 } },
223: { { 0, 0, 0, 0 }, { 0, .5, 0, 0 } },
224: { { 1, -.25, 0, 0 }, { 0, .5, 0, 0 } }, });
225:
226: public static final PredefinedShape[] MIDDLE_LIST = { FULL_HEIGHT,
227: HALF_HEIGHT_TOP, HALF_HEIGHT_BOTTOM, HALF_HEIGHT_CENTER,
228: QUARTER_HEIGHT_CENTER };
229:
230: public static final PredefinedShape[] END_LIST = { SQUARE, DIAMOND,
231: PENTAGON_UP, PENTAGON_DOWN, TRIANGLE_UP, TRIANGLE_DOWN,
232: TRIANGLE_RIGHT, TRIANGLE_LEFT, ARROW_UP, ARROW_DOWN,
233: LINK_ARROW1 };
234:
235: public static final PredefinedShape[] NETWORK_LIST = { FULL_HEIGHT,
236: HEXAGON, PARALLELOGRAM };
237:
238: private static void initialize() {
239:
240: add(FULL_HEIGHT);
241: add(HALF_HEIGHT_TOP);
242: add(HALF_HEIGHT_BOTTOM);
243: add(HALF_HEIGHT_CENTER);
244: add(QUARTER_HEIGHT_CENTER);
245:
246: add(SQUARE);
247: add(DIAMOND);
248: add(PENTAGON_UP);
249: add(PENTAGON_DOWN);
250: add(TRIANGLE_UP);
251: add(TRIANGLE_DOWN);
252: add(TRIANGLE_RIGHT);
253: add(TRIANGLE_LEFT);
254: add(ARROW_UP);
255: add(ARROW_DOWN);
256:
257: // pert shapes
258: add(HEXAGON);
259: add(PARALLELOGRAM);
260:
261: // links
262: add(LINK_ARROW1);
263:
264: }
265:
266: private static HashMap predefinedShapeMap = null;
267:
268: private static HashMap getPredefinedShapeMap() {
269: if (predefinedShapeMap == null) {
270: predefinedShapeMap = new HashMap();
271: initialize();
272: }
273: return predefinedShapeMap;
274: }
275:
276: public static PredefinedShape find(String key) {
277: PredefinedShape found = (PredefinedShape) getPredefinedShapeMap()
278: .get(key);
279: return found;
280: }
281: }
|