|
@@ -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");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
}
|