123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- package ui.view.dialog;
- import java.awt.BorderLayout;
- import java.awt.CardLayout;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import java.io.IOException;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JDialog;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import model.HolonObject;
- import ui.controller.Control;
- public class CreateNewDialog extends JDialog{
- //DefaultConstructor
- String[] optionStrings = { "","Category", "Object", "Battery", "Switch"};
- public static enum Option {
- None, Category, Object, Switch;
- public static Option getEnumAtIndex(int desired){
- if(desired>=0 && desired<=4)
- return values()[desired];
- else
- return None;
- }
-
- };
- Option actualOption = Option.None;
- Control actualController;
-
- //important JPanelItems
- JComboBox<String> optionList = new JComboBox<String>(optionStrings);
- JTextField inputName = new JTextField();
- JButton saveButton = new JButton("Save");
- JTextField inputNameSwitch = new JTextField();
- JComboBox<String> selectedCategorySwitch = new JComboBox<String>();
-
-
-
- public CreateNewDialog(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(), Option.None.name());
- cards.add(makeCategoryPanel(), Option.Category.name());
- cards.add(makeObjectPanel(), Option.Object.name());
- cards.add(makeSwitchPanel(), Option.Switch.name());
- contentPanel.add(cards, BorderLayout.CENTER);
-
- optionList.addActionListener(actionEvent -> {
- CardLayout cl = (CardLayout)(cards.getLayout());
- actualOption = Option.getEnumAtIndex(optionList.getSelectedIndex());
- cl.show(cards, actualOption.name());
- saveButton.setEnabled(actualOption != Option.None);
- saveButton.setVisible(actualOption != Option.Object);
- });
-
-
- contentPanel.add(makeBottemPanel(), BorderLayout.PAGE_END);
- addSaveButtonLogik();
- add(contentPanel);
- saveButton.setEnabled(false);
- setMinimumSize(new Dimension(400,50));
- pack();
- setLocationRelativeTo(parentFrame);
- }
- public CreateNewDialog(Control controller, Option aOption, JFrame parentFrame){
- super((JFrame)parentFrame, "Create a " + aOption.name());
- actualController = controller;
- if(aOption == Option.None)
- dispose();
- setVisible(true);
- JPanel contentPanel = new JPanel();
- contentPanel.setLayout(new BorderLayout());
- JPanel content;
- switch(aOption)
- {
- case Category:
- content = makeCategoryPanel();
- break;
- case Object:
- content = makeObjectPanel();
- break;
- case Switch:
- content = makeSwitchPanel();
- break;
- default:
- content = makeNothingPanel();
- break;
- }
- actualOption = aOption;
- contentPanel.add(content, BorderLayout.CENTER);
- contentPanel.add(makeBottemPanel(), BorderLayout.PAGE_END);
- addSaveButtonLogik();
- //TODO(Tom2021-12-1) delete and make own iterface
- if(aOption == Option.Object) saveButton.setVisible(false);
- add(contentPanel);
- setMinimumSize(new Dimension(400,50));
- pack();
- setLocationRelativeTo(parentFrame);
- }
-
-
- private JPanel makeTopPanel() {
- JPanel topPanel = new JPanel(new GridLayout(1,2));
- JLabel text = new JLabel("Choose..");
- topPanel.add(text);
- topPanel.add(optionList);
- return topPanel;
- }
- private JPanel makeBottemPanel() {
- JPanel bottomPanel = new JPanel();
- bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
-
- bottomPanel.add(saveButton);
- JButton cancelButton = new JButton("Cancel");
- cancelButton.addActionListener(actionEvent -> dispose());
- bottomPanel.add(cancelButton);
- return bottomPanel;
- }
- private JPanel makeObjectPanel() {
- JPanel objectPanel = new JPanel();
- JLabel categoryText = new JLabel("In Category:");
- objectPanel.add(categoryText);
- JComboBox<String> selectedCategory = new JComboBox<String>(actualController.getCategoriesStrings());
- objectPanel.add(selectedCategory);
-
-
- JButton nextButton = new JButton("Next");
- objectPanel.add(nextButton);
- nextButton.addActionListener(actionEvent -> {
- makeOldObjectPopUp(selectedCategory.getSelectedItem().toString());
- });
- return objectPanel;
- }
- private void makeOldObjectPopUp(String categoryName) {
- //TODO(Tom2021-12-1): del and make own on
-
- HolonObject hO = new HolonObject("");
-
- AddObjectPopUp addObjectPopUP = new AddObjectPopUp(false,hO , "hei", null);
- addObjectPopUP.setVisible(true);
- addObjectPopUP.setController(actualController);
- addObjectPopUP.setCategory(categoryName);
- dispose();
- }
- private JPanel makeNothingPanel() {
- return new JPanel();
- }
-
- private JPanel makeCategoryPanel()
- {
- JPanel newCategory = new JPanel(new FlowLayout());
- JLabel categoryName = new JLabel("Name:");
- 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.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);
- newCategory.add(inputName);
- return newCategory;
- }
- private JPanel makeSwitchPanel()
- {
- JPanel objectPanel = new JPanel();
- JLabel categoryText = new JLabel("In Category:");
- objectPanel.add(categoryText);
- selectedCategorySwitch = new JComboBox<String>(actualController.getCategoriesStrings());
- objectPanel.add(selectedCategorySwitch);
-
-
- JLabel switchName = new JLabel("Name:");
- String initialText = "The name of the new switch";
-
- inputNameSwitch.setText(initialText);
- inputNameSwitch.setBackground(java.awt.Color.LIGHT_GRAY);
- inputNameSwitch.addFocusListener(new java.awt.event.FocusAdapter() {
- public void focusGained(java.awt.event.FocusEvent evt) {
- if (inputNameSwitch.getText().equals(initialText)) {
- inputNameSwitch.setText("");
- inputNameSwitch.setBackground(java.awt.Color.WHITE);
- }
- }
- public void focusLost (java.awt.event.FocusEvent evt) {
- if (inputNameSwitch.getText().equals("")) {
- inputNameSwitch.setText(initialText);
- inputNameSwitch.setBackground(java.awt.Color.LIGHT_GRAY);
- }
- }
- });
- inputNameSwitch.setColumns(20);
- objectPanel.add(switchName);
- objectPanel.add(inputNameSwitch);
- return objectPanel;
- }
- private void addSaveButtonLogik() {
- saveButton.addActionListener(actionEvent -> {
- if(actualOption == Option.Category)
- {
- try {
- actualController.addCategory(inputName.getText());
- } catch (IOException e) {
- System.err.println("IOException addCategory");
- }
- dispose();
- }
- else if (actualOption == Option.Switch)
- {
- try {
- actualController.addSwitch(actualController.searchCategory(selectedCategorySwitch.getSelectedItem().toString()), inputNameSwitch.getText());
- } catch (IOException e) {
- System.err.println("IOException addSwitch");
- }
- dispose();
- }
- });
- }
- }
|