CreateNewDialog.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package holeg.ui.view.dialog;
  2. import java.awt.BorderLayout;
  3. import java.awt.CardLayout;
  4. import java.awt.Dimension;
  5. import java.awt.FlowLayout;
  6. import java.awt.GridLayout;
  7. import javax.swing.JButton;
  8. import javax.swing.JComboBox;
  9. import javax.swing.JDialog;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPanel;
  13. import javax.swing.JTextField;
  14. import holeg.model.HolonObject;
  15. import holeg.ui.controller.Control;
  16. public class CreateNewDialog extends JDialog{
  17. //DefaultConstructor
  18. String[] optionStrings = { "","Category", "Object", "Battery", "Switch"};
  19. public static enum Option {
  20. None, Category, Object, Switch;
  21. public static Option getEnumAtIndex(int desired){
  22. if(desired>=0 && desired<=4)
  23. return values()[desired];
  24. else
  25. return None;
  26. }
  27. };
  28. Option actualOption = Option.None;
  29. Control actualController;
  30. //important JPanelItems
  31. JComboBox<String> optionList = new JComboBox<String>(optionStrings);
  32. JTextField inputName = new JTextField();
  33. JButton saveButton = new JButton("Save");
  34. JTextField inputNameSwitch = new JTextField();
  35. JComboBox<String> selectedCategorySwitch = new JComboBox<String>();
  36. public CreateNewDialog(Control controller, JFrame parentFrame){
  37. super((JFrame)parentFrame, "Create a..");
  38. actualController = controller;
  39. setVisible(true);
  40. JPanel contentPanel = new JPanel();
  41. contentPanel.setLayout(new BorderLayout());
  42. contentPanel.add(makeTopPanel(), BorderLayout.PAGE_START);
  43. JPanel cards = new JPanel(new CardLayout());
  44. cards.add(makeNothingPanel(), Option.None.name());
  45. cards.add(makeCategoryPanel(), Option.Category.name());
  46. cards.add(makeObjectPanel(), Option.Object.name());
  47. cards.add(makeSwitchPanel(), Option.Switch.name());
  48. contentPanel.add(cards, BorderLayout.CENTER);
  49. optionList.addActionListener(actionEvent -> {
  50. CardLayout cl = (CardLayout)(cards.getLayout());
  51. actualOption = Option.getEnumAtIndex(optionList.getSelectedIndex());
  52. cl.show(cards, actualOption.name());
  53. saveButton.setEnabled(actualOption != Option.None);
  54. saveButton.setVisible(actualOption != Option.Object);
  55. });
  56. contentPanel.add(makeBottemPanel(), BorderLayout.PAGE_END);
  57. addSaveButtonLogik();
  58. add(contentPanel);
  59. saveButton.setEnabled(false);
  60. setMinimumSize(new Dimension(400,50));
  61. pack();
  62. setLocationRelativeTo(parentFrame);
  63. }
  64. public CreateNewDialog(Control controller, Option aOption, JFrame parentFrame){
  65. super((JFrame)parentFrame, "Create a " + aOption.name());
  66. actualController = controller;
  67. if(aOption == Option.None)
  68. dispose();
  69. setVisible(true);
  70. JPanel contentPanel = new JPanel();
  71. contentPanel.setLayout(new BorderLayout());
  72. JPanel content;
  73. switch(aOption)
  74. {
  75. case Category:
  76. content = makeCategoryPanel();
  77. break;
  78. case Object:
  79. content = makeObjectPanel();
  80. break;
  81. case Switch:
  82. content = makeSwitchPanel();
  83. break;
  84. default:
  85. content = makeNothingPanel();
  86. break;
  87. }
  88. actualOption = aOption;
  89. contentPanel.add(content, BorderLayout.CENTER);
  90. contentPanel.add(makeBottemPanel(), BorderLayout.PAGE_END);
  91. addSaveButtonLogik();
  92. //TODO(Tom2021-12-1) delete and make own iterface
  93. if(aOption == Option.Object) saveButton.setVisible(false);
  94. add(contentPanel);
  95. setMinimumSize(new Dimension(400,50));
  96. pack();
  97. setLocationRelativeTo(parentFrame);
  98. }
  99. private JPanel makeTopPanel() {
  100. JPanel topPanel = new JPanel(new GridLayout(1,2));
  101. JLabel text = new JLabel("Choose..");
  102. topPanel.add(text);
  103. topPanel.add(optionList);
  104. return topPanel;
  105. }
  106. private JPanel makeBottemPanel() {
  107. JPanel bottomPanel = new JPanel();
  108. bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  109. bottomPanel.add(saveButton);
  110. JButton cancelButton = new JButton("Cancel");
  111. cancelButton.addActionListener(actionEvent -> dispose());
  112. bottomPanel.add(cancelButton);
  113. return bottomPanel;
  114. }
  115. private JPanel makeObjectPanel() {
  116. JPanel objectPanel = new JPanel();
  117. JLabel categoryText = new JLabel("In Category:");
  118. objectPanel.add(categoryText);
  119. JComboBox<String> selectedCategory = new JComboBox<String>(actualController.getCategoriesStrings());
  120. objectPanel.add(selectedCategory);
  121. JButton nextButton = new JButton("Next");
  122. objectPanel.add(nextButton);
  123. nextButton.addActionListener(actionEvent -> {
  124. makeOldObjectPopUp(selectedCategory.getSelectedItem().toString());
  125. });
  126. return objectPanel;
  127. }
  128. private void makeOldObjectPopUp(String categoryName) {
  129. //TODO(Tom2021-12-1): del and make own on
  130. HolonObject hO = new HolonObject("");
  131. AddObjectPopUp addObjectPopUP = new AddObjectPopUp(false,hO , "hei", null);
  132. addObjectPopUP.setVisible(true);
  133. addObjectPopUP.setController(actualController);
  134. addObjectPopUP.setCategory(categoryName);
  135. dispose();
  136. }
  137. private JPanel makeNothingPanel() {
  138. return new JPanel();
  139. }
  140. private JPanel makeCategoryPanel()
  141. {
  142. JPanel newCategory = new JPanel(new FlowLayout());
  143. JLabel categoryName = new JLabel("Name:");
  144. String initialText = "The name of the new category";
  145. inputName.setText(initialText);
  146. inputName.setBackground(java.awt.Color.LIGHT_GRAY);
  147. inputName.addFocusListener(new java.awt.event.FocusAdapter() {
  148. public void focusGained(java.awt.event.FocusEvent evt) {
  149. if (inputName.getText().equals(initialText)) {
  150. inputName.setText("");
  151. inputName.setBackground(java.awt.Color.WHITE);
  152. }
  153. }
  154. public void focusLost (java.awt.event.FocusEvent evt) {
  155. if (inputName.getText().equals("")) {
  156. inputName.setText(initialText);
  157. inputName.setBackground(java.awt.Color.LIGHT_GRAY);
  158. }
  159. }
  160. });
  161. inputName.setColumns(20);
  162. newCategory.add(categoryName);
  163. newCategory.add(inputName);
  164. return newCategory;
  165. }
  166. private JPanel makeSwitchPanel()
  167. {
  168. JPanel objectPanel = new JPanel();
  169. JLabel categoryText = new JLabel("In Category:");
  170. objectPanel.add(categoryText);
  171. selectedCategorySwitch = new JComboBox<String>(actualController.getCategoriesStrings());
  172. objectPanel.add(selectedCategorySwitch);
  173. JLabel switchName = new JLabel("Name:");
  174. String initialText = "The name of the new switch";
  175. inputNameSwitch.setText(initialText);
  176. inputNameSwitch.setBackground(java.awt.Color.LIGHT_GRAY);
  177. inputNameSwitch.addFocusListener(new java.awt.event.FocusAdapter() {
  178. public void focusGained(java.awt.event.FocusEvent evt) {
  179. if (inputNameSwitch.getText().equals(initialText)) {
  180. inputNameSwitch.setText("");
  181. inputNameSwitch.setBackground(java.awt.Color.WHITE);
  182. }
  183. }
  184. public void focusLost (java.awt.event.FocusEvent evt) {
  185. if (inputNameSwitch.getText().equals("")) {
  186. inputNameSwitch.setText(initialText);
  187. inputNameSwitch.setBackground(java.awt.Color.LIGHT_GRAY);
  188. }
  189. }
  190. });
  191. inputNameSwitch.setColumns(20);
  192. objectPanel.add(switchName);
  193. objectPanel.add(inputNameSwitch);
  194. return objectPanel;
  195. }
  196. private void addSaveButtonLogik() {
  197. saveButton.addActionListener(actionEvent -> {
  198. if(actualOption == Option.Category)
  199. {
  200. actualController.createCategoryWithName(inputName.getText());
  201. dispose();
  202. }
  203. else if (actualOption == Option.Switch)
  204. {
  205. actualController.findCategoryWithName(selectedCategorySwitch.getSelectedItem().toString()).ifPresent(cat -> {
  206. actualController.addSwitch(cat, inputNameSwitch.getText());
  207. });
  208. dispose();
  209. }
  210. });
  211. }
  212. }