CreateNewDialog.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package ui.view.additional.popup;
  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.io.IOException;
  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 classes.HolonObject;
  16. import ui.controller.Control;
  17. public class CreateNewDialog extends JDialog{
  18. //DefaultConstructor
  19. String[] optionStrings = { "","Category", "Object", "Battery", "Switch"};
  20. public static enum Option {
  21. None, Category, Object, Switch;
  22. public static Option getEnumAtIndex(int desired){
  23. if(desired>=0 && desired<=4)
  24. return values()[desired];
  25. else
  26. return None;
  27. }
  28. };
  29. Option actualOption = Option.None;
  30. Control actualController;
  31. //important JPanelItems
  32. JComboBox<String> optionList = new JComboBox<String>(optionStrings);
  33. JTextField inputName = new JTextField();
  34. JButton saveButton = new JButton("Save");
  35. //TODO
  36. JTextField inputNameSwitch = new JTextField();
  37. JComboBox<String> selectedCategorySwitch = new JComboBox<String>();
  38. public CreateNewDialog(Control controller, JFrame parentFrame){
  39. super((JFrame)parentFrame, "Create a..");
  40. actualController = controller;
  41. setVisible(true);
  42. JPanel contentPanel = new JPanel();
  43. contentPanel.setLayout(new BorderLayout());
  44. contentPanel.add(makeTopPanel(), BorderLayout.PAGE_START);
  45. JPanel cards = new JPanel(new CardLayout());
  46. cards.add(makeNothingPanel(), Option.None.name());
  47. cards.add(makeCategoryPanel(), Option.Category.name());
  48. cards.add(makeObjectPanel(), Option.Object.name());
  49. cards.add(makeSwitchPanel(), Option.Switch.name());
  50. contentPanel.add(cards, BorderLayout.CENTER);
  51. optionList.addActionListener(actionEvent -> {
  52. CardLayout cl = (CardLayout)(cards.getLayout());
  53. actualOption = Option.getEnumAtIndex(optionList.getSelectedIndex());
  54. cl.show(cards, actualOption.name());
  55. saveButton.setEnabled(actualOption != Option.None);
  56. //TODO delete and make own iterface
  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 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: del and make own on
  133. HolonObject hO = new HolonObject("");
  134. AddObjectPopUp addObjectPopUP = new AddObjectPopUp(false,hO , "hei", null);
  135. addObjectPopUP.setVisible(true);
  136. addObjectPopUP.setController(actualController);
  137. addObjectPopUP.setCategory(categoryName);
  138. dispose();
  139. }
  140. private JPanel makeNothingPanel() {
  141. return new JPanel();
  142. }
  143. private JPanel makeCategoryPanel()
  144. {
  145. JPanel newCategory = new JPanel(new FlowLayout());
  146. JLabel categoryName = new JLabel("Name:");
  147. String initialText = "The name of the new category";
  148. inputName.setText(initialText);
  149. inputName.setBackground(java.awt.Color.LIGHT_GRAY);
  150. inputName.addFocusListener(new java.awt.event.FocusAdapter() {
  151. public void focusGained(java.awt.event.FocusEvent evt) {
  152. if (inputName.getText().equals(initialText)) {
  153. inputName.setText("");
  154. inputName.setBackground(java.awt.Color.WHITE);
  155. }
  156. }
  157. public void focusLost (java.awt.event.FocusEvent evt) {
  158. if (inputName.getText().equals("")) {
  159. inputName.setText(initialText);
  160. inputName.setBackground(java.awt.Color.LIGHT_GRAY);
  161. }
  162. }
  163. });
  164. inputName.setColumns(20);
  165. newCategory.add(categoryName);
  166. newCategory.add(inputName);
  167. return newCategory;
  168. }
  169. private JPanel makeSwitchPanel()
  170. {
  171. JPanel objectPanel = new JPanel();
  172. JLabel categoryText = new JLabel("In Category:");
  173. objectPanel.add(categoryText);
  174. selectedCategorySwitch = new JComboBox<String>(actualController.getCategoriesStrings());
  175. objectPanel.add(selectedCategorySwitch);
  176. JLabel switchName = new JLabel("Name:");
  177. String initialText = "The name of the new switch";
  178. inputNameSwitch.setText(initialText);
  179. inputNameSwitch.setBackground(java.awt.Color.LIGHT_GRAY);
  180. inputNameSwitch.addFocusListener(new java.awt.event.FocusAdapter() {
  181. public void focusGained(java.awt.event.FocusEvent evt) {
  182. if (inputNameSwitch.getText().equals(initialText)) {
  183. inputNameSwitch.setText("");
  184. inputNameSwitch.setBackground(java.awt.Color.WHITE);
  185. }
  186. }
  187. public void focusLost (java.awt.event.FocusEvent evt) {
  188. if (inputNameSwitch.getText().equals("")) {
  189. inputNameSwitch.setText(initialText);
  190. inputNameSwitch.setBackground(java.awt.Color.LIGHT_GRAY);
  191. }
  192. }
  193. });
  194. inputNameSwitch.setColumns(20);
  195. objectPanel.add(switchName);
  196. objectPanel.add(inputNameSwitch);
  197. return objectPanel;
  198. }
  199. private void addSaveButtonLogik() {
  200. saveButton.addActionListener(actionEvent -> {
  201. if(actualOption == Option.Category)
  202. {
  203. try {
  204. actualController.addCategory(inputName.getText());
  205. } catch (IOException e) {
  206. System.err.println("IOException addCategory");
  207. }
  208. dispose();
  209. }
  210. else if (actualOption == Option.Switch)
  211. {//TODO
  212. try {
  213. actualController.addSwitch(actualController.searchCategory(selectedCategorySwitch.getSelectedItem().toString()), inputNameSwitch.getText());
  214. } catch (IOException e) {
  215. System.err.println("IOException addSwitch");
  216. }
  217. dispose();
  218. }
  219. });
  220. }
  221. }