Browse Source

refactors some PopUp Code

Andreas T. Meyer-Berg 6 years ago
parent
commit
ea2c54c4b6
2 changed files with 88 additions and 70 deletions
  1. 75 70
      src/ui/view/AddElementPopUp.java
  2. 13 0
      src/ui/view/CreateTemplatePopUp.java

+ 75 - 70
src/ui/view/AddElementPopUp.java

@@ -28,29 +28,29 @@ public class AddElementPopUp extends JDialog {
 	private HolonElement hl;
 	private AbstractCpsObject tempCps;
 
-//	/**
-//	 * Launch the application.
-//	 *
-//	 * @param args
-//	 *            standard
-//	 */
-//	public static void main(String[] args) {
-//		try {
-//
-//			AddElementPopUp dialog = new AddElementPopUp();
-//			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
-//			dialog.setVisible(true);
-//		} catch (Exception e) {
-//			e.printStackTrace();
-//		}
-//	}
+	// /**
+	// * Launch the application.
+	// *
+	// * @param args
+	// * standard
+	// */
+	// public static void main(String[] args) {
+	// try {
+	//
+	// AddElementPopUp dialog = new AddElementPopUp();
+	// dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+	// dialog.setVisible(true);
+	// } catch (Exception e) {
+	// e.printStackTrace();
+	// }
+	// }
 
 	/**
 	 * Create the dialog.
 	 */
 	AddElementPopUp(JFrame parentFrame) {
 		super((java.awt.Frame) null, true);
-		this.setIconImage(Util.loadImage(this,"/Images/Dummy_House.png",30,30));
+		this.setIconImage(Util.loadImage(this, "/Images/Dummy_House.png", 30, 30));
 		setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
 		setBounds(100, 100, 400, 190);
 		setLocationRelativeTo(parentFrame);
@@ -102,57 +102,21 @@ public class AddElementPopUp extends JDialog {
 		contentPanel.add(amount);
 		amount.setColumns(10);
 		amount.setText("1");
-		{
-			JPanel buttonPane = new JPanel();
-			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
-			getContentPane().add(buttonPane, BorderLayout.SOUTH);
-			{
-				JButton okButton = new JButton("OK");
-                okButton.addActionListener(arg0 -> {
-                    boolean repeated = false;
-                    for (HolonElement e : ((HolonObject) tempCps).getElements()) {
-                        if (elementName.getText().equals(e.getEleName())) {
-                            repeated = true;
-                            break;
-                        }
-                    }
-                    if (elementName.getText().length() != 0 && !repeated) {
-                        try {
-                            float energy = Float.parseFloat(providedEnergy.getText());
-                            int elementAmount = Integer.parseInt(amount.getText());
-                            hl = new HolonElement(elementName.getText(), elementAmount, energy);
-
-                            dispose();
-                        } catch (NumberFormatException e) {
-                            JOptionPane.showMessageDialog(new JFrame(), Languages.getLanguage()[68]);
-                        }
-                    } else {
-                        // JOptionPane.showMessageDialog(new JFrame(),
-                        // "Please enter a Name");
-
-                        if (elementName.getText().length() == 0) {
-                            JLabel errorString = new JLabel(Languages.getLanguage()[69]);
-                            errorString.setBounds(240, 8, 100, 20);
-                            contentPanel.add(errorString);
-                        } else if (repeated) {
-                            JLabel errorString = new JLabel(Languages.getLanguage()[70]);
-                            errorString.setBounds(250, 8, 100, 20);
-                            contentPanel.add(errorString);
-                        }
-                        elementName.setBackground(new Color(255, 50, 50));
-                    }
-                });
-                okButton.setActionCommand("OK");
-				buttonPane.add(okButton);
-				getRootPane().setDefaultButton(okButton);
-			}
-			{
-				JButton cancelButton = new JButton(Languages.getLanguage()[71]);
-				cancelButton.setActionCommand("Cancel");
-				buttonPane.add(cancelButton);
-                cancelButton.addActionListener(e -> dispose());
-            }
-		}
+
+		JPanel buttonPane = new JPanel();
+		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
+		getContentPane().add(buttonPane, BorderLayout.SOUTH);
+
+		JButton okButton = new JButton("OK");
+		okButton.addActionListener(arg0 -> okAction());
+		okButton.setActionCommand("OK");
+		buttonPane.add(okButton);
+		getRootPane().setDefaultButton(okButton);
+
+		JButton cancelButton = new JButton(Languages.getLanguage()[71]);
+		cancelButton.setActionCommand("Cancel");
+		buttonPane.add(cancelButton);
+		cancelButton.addActionListener(e -> dispose());
 
 	}
 
@@ -162,8 +126,8 @@ public class AddElementPopUp extends JDialog {
 	 * @param cps
 	 *            actual Cps
 	 */
-    void setActualCps(AbstractCpsObject cps) {
-        this.tempCps = cps;
+	void setActualCps(AbstractCpsObject cps) {
+		this.tempCps = cps;
 	}
 
 	/**
@@ -174,4 +138,45 @@ public class AddElementPopUp extends JDialog {
 	public HolonElement getElement() {
 		return hl;
 	}
+
+	public void setElement(HolonElement holonElement) {
+		hl = new HolonElement(hl);
+
+	}
+
+	private void okAction() {
+		boolean repeated = false;
+		for (HolonElement e : ((HolonObject) tempCps).getElements()) {
+			if (elementName.getText().equals(e.getEleName())) {
+				repeated = true;
+				break;
+			}
+		}
+		if (elementName.getText().length() != 0 && !repeated) {
+			try {
+				float energy = Float.parseFloat(providedEnergy.getText());
+				int elementAmount = Integer.parseInt(amount.getText());
+				hl = new HolonElement(elementName.getText(), elementAmount, energy);
+
+				dispose();
+			} catch (NumberFormatException e) {
+				JOptionPane.showMessageDialog(new JFrame(), Languages.getLanguage()[68]);
+			}
+		} else {
+			// JOptionPane.showMessageDialog(new JFrame(),
+			// "Please enter a Name");
+
+			if (elementName.getText().length() == 0) {
+				JLabel errorString = new JLabel(Languages.getLanguage()[69]);
+				errorString.setBounds(240, 8, 100, 20);
+				contentPanel.add(errorString);
+			} else if (repeated) {
+				JLabel errorString = new JLabel(Languages.getLanguage()[70]);
+				errorString.setBounds(250, 8, 100, 20);
+				contentPanel.add(errorString);
+			}
+			elementName.setBackground(new Color(255, 50, 50));
+		}
+
+	}
 }

+ 13 - 0
src/ui/view/CreateTemplatePopUp.java

@@ -342,6 +342,19 @@ public class CreateTemplatePopUp extends JDialog {
 		int index = list.getSelectedIndex();
 		if (index == -1)
 			return;
+		
+		AddElementPopUp popUp = new AddElementPopUp(parent);
+		popUp.setActualCps(template);
+		popUp.setElement(template.getElements().get(index));
+		popUp.setVisible(true);
+		HolonElement he = popUp.getElement();
+		if (he != null) {
+			listModel.addElement(he.getAmount() + " * " + he.getEleName()
+					+ ": " + he.getOverallEnergy() + "U");
+			template.addElement(he);
+			he.setSaving(new Pair<>(category, textField_name.getText()));
+		}
+		
 		System.out.println("Edit "
 				+ template.getElements().get(index).toString());
 	}