CreateNewDialog.java 7.8 KB

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