001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/warp/WarpSet.java,v 1.1 2005/04/20 21:05:19 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 Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.warp;
019:
020: import java.io.Serializable;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import javax.swing.JDialog;
024: import javax.swing.JOptionPane;
025: import javax.swing.JComboBox;
026:
027: import javax.media.j3d.TransformGroup;
028: import javax.media.j3d.Transform3D;
029:
030: /**
031: * Encapsulates a set of WarpPositions
032: *
033: * @author Paul Byrne
034: * @version 1.6, 12/19/00
035: */
036: public class WarpSet extends Object implements Serializable {
037:
038: private static final long serialVersionUID = 4278610821202427778L;
039:
040: private HashMap warps; // Map of warps indexed by name
041: private String defaultWarp;
042: private transient JComboBox comboBox;
043:
044: /** Creates new WarpSet */
045: public WarpSet(JComboBox comboBox) {
046: warps = new HashMap();
047: this .comboBox = comboBox;
048: }
049:
050: public WarpSet() {
051: warps = new HashMap();
052: comboBox = null;
053: }
054:
055: /**
056: * Add a new Warp to the set.
057: * @param name Name of the warp
058: * @param description A description of the Warp (can be null)
059: * @param position The position of the warp
060: */
061: public void addWarpPosition(String name, String description,
062: Transform3D position) {
063: WarpPosition w = new WarpPosition(name, description, position);
064: warps.put(name, w);
065: if (comboBox != null)
066: comboBox.addItem(name);
067: }
068:
069: /**
070: * Remove the named warp
071: */
072: public void deleteWarpPosition(String name) {
073: warps.remove(name);
074: if (comboBox != null)
075: comboBox.removeItem(name);
076: }
077:
078: /**
079: * Change the position of the named warp, if the warp does not exist it
080: * will be created.
081: *
082: * @param name Name of the warp
083: * @param newPosition The new position for the warp
084: */
085: public void changeWarpPosition(String name, Transform3D newPosition) {
086: WarpPosition w = (WarpPosition) warps.get(name);
087: if (w == null)
088: addWarpPosition(name, null, newPosition);
089: else
090: w.setPosition(newPosition);
091: }
092:
093: /**
094: * Returns true if name exists in the set
095: */
096: public boolean nameExists(String name) {
097: return warps.containsKey(name);
098: }
099:
100: /**
101: * Clear the warp set and update the combobox
102: */
103: public void clear() {
104: warps.clear();
105: if (comboBox != null) {
106: comboBox.removeAllItems();
107: comboBox.addItem("");
108: }
109: }
110:
111: /**
112: * Set the viewTG to the position of the named warp.
113: *
114: * If the named warp does not exist the transformGroup will not
115: * be modified.
116: *
117: * @param name Name of the warp
118: * @param viewTG The View TransformGroup
119: */
120: public void warpTo(String name, TransformGroup viewTG) {
121: WarpPosition w = (WarpPosition) warps.get(name);
122: if (w != null) {
123: viewTG.setTransform(w.getPosition());
124: }
125: }
126:
127: /**
128: * Create a warp for the supplied position.
129: * Present the user with a dialog to obtain the
130: * name and description of the warp.
131: *
132: * @param position The position of the warp
133: * @param parent The component which should be the parent of the dialog
134: * @return Returns the name of the new warp, or null if user cancelled creation
135: */
136: public String createWarp(Transform3D position, JDialog parent) {
137: CreateWarpDialog dialog = new CreateWarpDialog(parent, true,
138: this );
139: dialog.setVisible(true);
140:
141: if (dialog.isValid()) {
142: addWarpPosition(dialog.getName(), dialog.getDescription(),
143: position);
144: return dialog.getName();
145: }
146:
147: return null;
148:
149: }
150:
151: public void setDefaultWarp(String name) {
152: defaultWarp = name;
153: }
154:
155: public String getDefaultWarp() {
156: return defaultWarp;
157: }
158:
159: /**
160: * Returns an array containing all the warp names
161: */
162: public String[] getNames() {
163: return (String[]) warps.keySet().toArray(
164: new String[warps.size()]);
165: }
166:
167: /**
168: * Return all the warps.
169: *
170: * This was added to support the transition to the new
171: * WarpPlugin
172: */
173: public WarpPosition[] getWarps() {
174: return (WarpPosition[]) warps.values().toArray(
175: new WarpPosition[warps.size()]);
176: }
177:
178: java.util.Collection getValues() {
179: return warps.values();
180: }
181:
182: /**
183: * Merge set with this warp set
184: * If this defaultWarp==null then set it to set.defaultWarp
185: *
186: * TODO - Handle duplicate names correctly
187: */
188: public void merge(WarpSet set) {
189: java.util.Collection collection = set.getValues();
190:
191: Iterator it = collection.iterator();
192: while (it.hasNext()) {
193: WarpPosition w = (WarpPosition) it.next();
194: addWarpPosition(w.getName(), w.getDescription(), w
195: .getPosition());
196: }
197:
198: if (defaultWarp == null)
199: defaultWarp = set.getDefaultWarp();
200: }
201:
202: }
|