summaryrefslogtreecommitdiff
path: root/src/tesseract/newmenu/NewChainLinkMenuItem.java
blob: 65e7395aec7c219fad0a26d94035d9588c63e564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package tesseract.newmenu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.vecmath.Vector3f;

import tesseract.World;
import tesseract.objects.ChainLink2;
import tesseract.objects.Icosahedron;

/**
 * NewIcosahedronMenutItem
 * 
 * Defines a menu item to add an ChainLink to the world.
 * Code recycled from TesseractMenuItem by Steve Bradshaw and Jessie Morgan
 * 
 * @author Phillip Cardon
 */
public class NewChainLinkMenuItem extends MenuItem {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1936364496102891064L;
	//private static Map <String, Object> myParams;
	
	/**
	 * Constructor.
	 * @param theWorld to add objects to.
	 */
	public NewChainLinkMenuItem(final World theWorld) {
		super(theWorld, "ChainLink(NEW)");
		buildParams();
	}
	
	/**
	 * Adds Parameters for user input.
	 * Sets default text box text.
	 */
	private void buildParams() {
		myParameters.put("Diameter", new Float(0f));
		myParameters.put("Length", new Float(0f));
		myParameters.put("Width", new Float(0f));
		this.makePanel();
		myReadData.get("Diameter").setText(((Float) 
				ChainLink2.DEFAULT_DIAMETER_RATIO).toString());
		myReadData.get("Length").setText(((Float) 
				ChainLink2.DEFAULT_LENGTH).toString());
		myReadData.get("Width").setText(((Float) 
				ChainLink2.DEFAULT_WIDTH_RATIO).toString());
	}

	@Override
	public void actionPerformed(final ActionEvent e) {
		createParameterMenu();
		final JButton defaultButton = getDefaultButton();
		final JFrame params = getParamFrame();
		final JButton enterButton = getEnterButton();
		
		defaultButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				if (e.getSource() == defaultButton) {
					myWorld.addObject(new ChainLink2(MenuItem.DEFAULT_POSITION,
							MenuItem.DEFAULT_MASS));
					params.dispose();
				}
			}
		});
		enterButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				Set<String> itr = myParameters.keySet();	
				for (String s : itr) {
					Object o = myParameters.get(s);
					String p = myReadData.get(s).getText();
					if (o.getClass().equals(new Float(0f).getClass())) {
						myParameters.put(s, new Float(Float.parseFloat(p)));
					} else if (o.getClass().equals(new Vector3f().getClass())) {
						myParameters.put(s, parseVector(p));
					}
						
				}
				if (event.getSource() == enterButton) {
					myWorld.addObject(
					new ChainLink2((Vector3f) myParameters.get("Position"),
							((Float) myParameters.get("Mass")).floatValue(),
							((Float) myParameters.get("Diameter")).floatValue(),
							((Float) myParameters.get("Length")).floatValue(),
							((Float) myParameters.get("Width")).floatValue()));
					params.dispose();
				}
			}
		});

	}
}