CreateNewDialog.java 7.7 KB

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