CreateNewDialog.java 8.9 KB

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