瀏覽代碼

Added new CategoryComboBox

tolatesry 7 年之前
父節點
當前提交
4730c0be59
共有 4 個文件被更改,包括 85 次插入37 次删除
  1. 12 1
      src/ui/controller/CategoryController.java
  2. 9 0
      src/ui/controller/Control.java
  3. 5 5
      src/ui/view/GUI.java
  4. 59 31
      src/ui/view/NewPopUp.java

+ 12 - 1
src/ui/controller/CategoryController.java

@@ -100,7 +100,18 @@ public class CategoryController {
 
 		notifyCatListeners();
 	}
-
+	
+	/**
+	 * get All Categories
+	 * 
+	 * @return the ArrayList of all Categories
+	 */
+	public ArrayList<Category> getCategories()
+	{
+		return model.getCategories();
+	}
+	
+	
 	/**
 	 * delete a given Category.
 	 * 

+ 9 - 0
src/ui/controller/Control.java

@@ -21,6 +21,7 @@ import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Hashtable;
+import java.util.stream.Collectors;
 
 import javax.swing.JFrame;
 
@@ -197,6 +198,14 @@ public class Control {
         saveCategory();
     }
 
+    /**
+     * Gives all Category as String
+     * @return a array of strings from all Categorys
+     */
+    public String[] getCategoriesStrings()
+    {
+		return ((ArrayList<String>) categoryController.getCategories().stream().map(c -> c.getName()).collect(Collectors.toList())).toArray(new String[categoryController.getCategories().size()]);
+    }
     /**
      * Add new Holon Object to a Category.
      *

+ 5 - 5
src/ui/view/GUI.java

@@ -14,7 +14,7 @@ import ui.controller.Control;
 import ui.controller.SimulationManager;
 import ui.controller.UpdateController;
 import ui.model.Model;
-import ui.view.NewPopUp.NewItem;
+import ui.view.NewPopUp.Option;
 
 import javax.swing.*;
 import javax.swing.border.LineBorder;
@@ -1981,19 +1981,19 @@ public class GUI implements CategoryListener {
 		btnAddPopUp.addSeparator();
 		btnAddPopUp.add(mItemCategory);
 		mItemCategory.addActionListener(actionEvent -> {
-			new NewPopUp(controller,NewItem.Category, frmCyberPhysical);
+			new NewPopUp(controller,Option.Category, frmCyberPhysical);
 		});
 		btnAddPopUp.add(mItemObject);
 		mItemObject.addActionListener(actionEvent -> {
-			new NewPopUp(controller,NewItem.Object, frmCyberPhysical);
+			new NewPopUp(controller,Option.Object, frmCyberPhysical);
 		});
 		btnAddPopUp.add(mItemSwitch);
 		mItemSwitch.addActionListener(actionEvent -> {
-			new NewPopUp(controller,NewItem.Switch, frmCyberPhysical);
+			new NewPopUp(controller,Option.Switch, frmCyberPhysical);
 		});
 		btnAddPopUp.add(mItemBattery);
 		mItemBattery.addActionListener(actionEvent -> {
-			new NewPopUp(controller,NewItem.Battery, frmCyberPhysical);
+			new NewPopUp(controller,Option.Battery, frmCyberPhysical);
 		});
 		btnAdd.addActionListener(actionEvent -> {
 			btnAddPopUp.show(btnAdd, 0, 0);

+ 59 - 31
src/ui/view/NewPopUp.java

@@ -2,10 +2,12 @@ package ui.view;
 
 import java.awt.BorderLayout;
 import java.awt.CardLayout;
+import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.FlowLayout;
 import java.awt.GridBagLayout;
 import java.awt.GridLayout;
+import java.io.IOException;
 
 import javax.swing.JButton;
 import javax.swing.JComboBox;
@@ -15,6 +17,7 @@ import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
 
+import javafx.scene.paint.Color;
 import ui.controller.Control;
 
 
@@ -23,9 +26,9 @@ public class NewPopUp extends JDialog{
 	//DefaultConstructor
 	
 	String[] optionStrings = { "","Category", "Object", "Battery", "Switch"};
-	public static enum NewItem {
+	public static enum Option {
 		None, Category, Object, Battery, Switch;
-		public static NewItem getEnumAtIndex(int desired){
+		public static Option getEnumAtIndex(int desired){
 			if(desired>=0 && desired<=4)
 				return values()[desired];
 			else 
@@ -33,11 +36,11 @@ public class NewPopUp extends JDialog{
 		}
 		
 	};
-	NewItem choosenOption = NewItem.None;
-	
+	Option actualOption = Option.None;
+	Control actualController;
 	
 	//important JPanelItems
-	JComboBox optionList = new JComboBox(optionStrings);
+	JComboBox<String> optionList = new JComboBox<String>(optionStrings);
 	JTextField inputName = new JTextField();
 	JButton saveButton = new JButton("Save");
 	
@@ -50,45 +53,40 @@ public class NewPopUp extends JDialog{
 	
 	NewPopUp(Control controller, JFrame parentFrame){
 		super((JFrame)parentFrame, "Create a..");
+		actualController = controller;
 		setVisible(true);
 		JPanel contentPanel = new JPanel();
 		contentPanel.setLayout(new BorderLayout());
 		contentPanel.add(makeTopPanel(), BorderLayout.PAGE_START);
 		JPanel cards = new JPanel(new CardLayout());
-		cards.add(makeNothingPanel(), NewItem.None.name());
-		cards.add(makeCategoryPanel(), NewItem.Category.name());
-		cards.add(makeObjectPanel(), NewItem.Object.name());
-		cards.add(makeBatteryPanel(), NewItem.Battery.name());
-		cards.add(makeSwitchPanel(), NewItem.Switch.name());
+		cards.add(makeNothingPanel(), Option.None.name());
+		cards.add(makeCategoryPanel(), Option.Category.name());
+		cards.add(makeObjectPanel(), Option.Object.name());
+		cards.add(makeBatteryPanel(), Option.Battery.name());
+		cards.add(makeSwitchPanel(), Option.Switch.name());
 		contentPanel.add(cards, BorderLayout.CENTER);
 		
 		optionList.addActionListener(actionEvent -> {
 			CardLayout cl = (CardLayout)(cards.getLayout());
-			choosenOption = NewItem.getEnumAtIndex(optionList.getSelectedIndex());
-		    cl.show(cards, choosenOption.name());
-		    saveButton.setEnabled(choosenOption != NewItem.None);  
-		});
-		saveButton.addActionListener(actionEvent -> {
-			System.out.println(choosenOption);
-			if(choosenOption == NewItem.Category)
-			{
-				try {
-					controller.addCategory(inputName.getText());
-				} catch (Exception e) {
-				}
-			}
+			actualOption = Option.getEnumAtIndex(optionList.getSelectedIndex());
+		    cl.show(cards, actualOption.name());
+		    saveButton.setEnabled(actualOption != Option.None);  
 		});
 		
+		
         contentPanel.add(makeBottemPanel(), BorderLayout.PAGE_END);
-        
+        addSaveButtonLogik();
         add(contentPanel);	
 		setMinimumSize(new Dimension(400,350));
 		pack();
 		setLocationRelativeTo(parentFrame);
 	}
-	NewPopUp(Control controller, NewItem aOption, JFrame parentFrame){
+
+
+	NewPopUp(Control controller, Option aOption, JFrame parentFrame){
 		super((JFrame)parentFrame, "Create a " + aOption.name());
-		if(aOption == NewItem.None)
+		actualController = controller;
+		if(aOption == Option.None)
 			dispose();
 		setVisible(true);
 		JPanel contentPanel = new JPanel();
@@ -112,15 +110,17 @@ public class NewPopUp extends JDialog{
 			content = new JPanel();
 			break;
 		}
-		choosenOption = aOption;
+		actualOption = aOption;
 		contentPanel.add(content, BorderLayout.CENTER);
 		contentPanel.add(makeBottemPanel(), BorderLayout.PAGE_END);
+		addSaveButtonLogik();
 		add(contentPanel);	
 		setMinimumSize(new Dimension(400,350));
 		pack();
 		setLocationRelativeTo(parentFrame);
 	}
-
+	
+	
 	private JPanel makeTopPanel() {
 		JPanel topPanel = new JPanel(new GridLayout(1,2));
 		JLabel text = new JLabel("Choose..");
@@ -140,7 +140,13 @@ public class NewPopUp extends JDialog{
 	}
 
 	private JPanel makeObjectPanel() {
-		return new JPanel();
+		JPanel objectPanel = new JPanel();
+		JLabel categoryText = new JLabel("In Category:");
+		objectPanel.add(categoryText);
+		JComboBox<String> selectedCategory = new JComboBox<String>(actualController.getCategoriesStrings());
+		objectPanel.add(selectedCategory);
+		
+		return objectPanel;
 	}
 
 	private JPanel makeBatteryPanel() {
@@ -158,14 +164,22 @@ public class NewPopUp extends JDialog{
 	{
 		JPanel newCategory = new JPanel(new FlowLayout());
 		JLabel categoryName = new JLabel("Name:");
-		String initialText = "The name of the new Category";
+		String initialText = "The name of the new category";
 		inputName.setText(initialText);
+		inputName.setBackground(java.awt.Color.LIGHT_GRAY);
 		inputName.addFocusListener(new java.awt.event.FocusAdapter() {
 		    public void focusGained(java.awt.event.FocusEvent evt) {
 		       if (inputName.getText().equals(initialText)) {
-		    	   inputName.selectAll();
+		    	   inputName.setText("");
+		    	   inputName.setBackground(java.awt.Color.WHITE);
 		       }
 		    }
+		    public void focusLost (java.awt.event.FocusEvent evt) {
+			       if (inputName.getText().equals("")) {
+			    	   inputName.setText(initialText);
+			    	   inputName.setBackground(java.awt.Color.LIGHT_GRAY);
+			       }
+			    }
 		});
 		inputName.setColumns(20);
 		newCategory.add(categoryName);
@@ -176,7 +190,21 @@ public class NewPopUp extends JDialog{
 	{
 		JPanel newCategory = new JPanel();
 		JLabel categoryName = new JLabel("Name:Switch");
+		
 		newCategory.add(categoryName);
 		return newCategory;
+	}	
+	private void addSaveButtonLogik() {
+		saveButton.addActionListener(actionEvent -> {
+			System.out.println(actualOption);
+			if(actualOption == Option.Category)
+			{
+				try {
+					actualController.addCategory(inputName.getText());
+				} catch (IOException e) {
+					System.err.println("IOException addCategory");
+				}
+			}
+		});
 	}
 }