NewPopUp.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 NewPopUp extends JDialog{
  19. //DefaultConstructor
  20. String[] optionStrings = { "","Category", "Object", "Battery", "Switch", "WildCard"};
  21. public static enum Option {
  22. None, Category, Object, Battery, Switch, WildCard;
  23. public static Option getEnumAtIndex(int desired){
  24. if(desired>=0 && desired<=5)
  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. NewPopUp(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. NewPopUp(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. case WildCard:
  93. content = makeWildCardPanel();
  94. break;
  95. default:
  96. content = makeNothingPanel();
  97. break;
  98. }
  99. actualOption = aOption;
  100. contentPanel.add(content, BorderLayout.CENTER);
  101. contentPanel.add(makeBottemPanel(), BorderLayout.PAGE_END);
  102. addSaveButtonLogik();
  103. //TODO delete and make own iterface
  104. if(aOption == Option.Object || aOption == Option.Battery) saveButton.setVisible(false);
  105. add(contentPanel);
  106. setMinimumSize(new Dimension(400,50));
  107. pack();
  108. setLocationRelativeTo(parentFrame);
  109. }
  110. private JPanel makeTopPanel() {
  111. JPanel topPanel = new JPanel(new GridLayout(1,2));
  112. JLabel text = new JLabel("Choose..");
  113. topPanel.add(text);
  114. topPanel.add(optionList);
  115. return topPanel;
  116. }
  117. private JPanel makeBottemPanel() {
  118. JPanel bottomPanel = new JPanel();
  119. bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  120. bottomPanel.add(saveButton);
  121. JButton cancelButton = new JButton("Cancel");
  122. cancelButton.addActionListener(actionEvent -> dispose());
  123. bottomPanel.add(cancelButton);
  124. return bottomPanel;
  125. }
  126. private JPanel makeObjectPanel() {
  127. JPanel objectPanel = new JPanel();
  128. JLabel categoryText = new JLabel("In Category:");
  129. objectPanel.add(categoryText);
  130. JComboBox<String> selectedCategory = new JComboBox<String>(actualController.getCategoriesStrings());
  131. objectPanel.add(selectedCategory);
  132. JButton nextButton = new JButton("Next");
  133. objectPanel.add(nextButton);
  134. nextButton.addActionListener(actionEvent -> {
  135. makeOldObjectPopUp(selectedCategory.getSelectedItem().toString());
  136. });
  137. return objectPanel;
  138. }
  139. private void makeOldObjectPopUp(String categoryName) {
  140. //TODO: del and make own on
  141. HolonObject hO = new HolonObject("");
  142. AddObjectPopUp addObjectPopUP = new AddObjectPopUp(false,hO , "hei", null);
  143. addObjectPopUP.setVisible(true);
  144. addObjectPopUP.setController(actualController);
  145. addObjectPopUP.setCategory(categoryName);
  146. dispose();
  147. }
  148. private JPanel makeBatteryPanel() {
  149. JPanel objectPanel = new JPanel();
  150. JLabel categoryText = new JLabel("In Category:");
  151. objectPanel.add(categoryText);
  152. JComboBox<String> selectedCategory = new JComboBox<String>(actualController.getCategoriesStrings());
  153. objectPanel.add(selectedCategory);
  154. JButton nextButton = new JButton("Next");
  155. objectPanel.add(nextButton);
  156. nextButton.addActionListener(actionEvent -> {
  157. makeOldBatteryPopUp(selectedCategory.getSelectedItem().toString());
  158. });
  159. return objectPanel;
  160. }
  161. private void makeOldBatteryPopUp(String category) {
  162. HolonBattery holonbat = new HolonBattery("newBattery");
  163. try {
  164. actualController.addBattery(actualController.searchCategory(category), holonbat);
  165. AddObjectPopUp addObjectPopUP = new AddObjectPopUp(false,holonbat , "hei", null);
  166. addObjectPopUP.setVisible(true);
  167. addObjectPopUP.setController(actualController);
  168. dispose();
  169. } catch (IOException e) {
  170. }
  171. }
  172. private JPanel makeNothingPanel() {
  173. return new JPanel();
  174. }
  175. private JPanel makeCategoryPanel()
  176. {
  177. JPanel newCategory = new JPanel(new FlowLayout());
  178. JLabel categoryName = new JLabel("Name:");
  179. String initialText = "The name of the new category";
  180. inputName.setText(initialText);
  181. inputName.setBackground(java.awt.Color.LIGHT_GRAY);
  182. inputName.addFocusListener(new java.awt.event.FocusAdapter() {
  183. public void focusGained(java.awt.event.FocusEvent evt) {
  184. if (inputName.getText().equals(initialText)) {
  185. inputName.setText("");
  186. inputName.setBackground(java.awt.Color.WHITE);
  187. }
  188. }
  189. public void focusLost (java.awt.event.FocusEvent evt) {
  190. if (inputName.getText().equals("")) {
  191. inputName.setText(initialText);
  192. inputName.setBackground(java.awt.Color.LIGHT_GRAY);
  193. }
  194. }
  195. });
  196. inputName.setColumns(20);
  197. newCategory.add(categoryName);
  198. newCategory.add(inputName);
  199. return newCategory;
  200. }
  201. private JPanel makeSwitchPanel()
  202. {
  203. JPanel objectPanel = new JPanel();
  204. JLabel categoryText = new JLabel("In Category:");
  205. objectPanel.add(categoryText);
  206. selectedCategorySwitch = new JComboBox<String>(actualController.getCategoriesStrings());
  207. objectPanel.add(selectedCategorySwitch);
  208. JLabel switchName = new JLabel("Name:");
  209. String initialText = "The name of the new switch";
  210. inputNameSwitch.setText(initialText);
  211. inputNameSwitch.setBackground(java.awt.Color.LIGHT_GRAY);
  212. inputNameSwitch.addFocusListener(new java.awt.event.FocusAdapter() {
  213. public void focusGained(java.awt.event.FocusEvent evt) {
  214. if (inputNameSwitch.getText().equals(initialText)) {
  215. inputNameSwitch.setText("");
  216. inputNameSwitch.setBackground(java.awt.Color.WHITE);
  217. }
  218. }
  219. public void focusLost (java.awt.event.FocusEvent evt) {
  220. if (inputNameSwitch.getText().equals("")) {
  221. inputNameSwitch.setText(initialText);
  222. inputNameSwitch.setBackground(java.awt.Color.LIGHT_GRAY);
  223. }
  224. }
  225. });
  226. inputNameSwitch.setColumns(20);
  227. objectPanel.add(switchName);
  228. objectPanel.add(inputNameSwitch);
  229. return objectPanel;
  230. }
  231. private JPanel makeWildCardPanel()
  232. {
  233. JPanel objectPanel = new JPanel();
  234. JLabel categoryText = new JLabel("In Category:");
  235. objectPanel.add(categoryText);
  236. selectedCategorySwitch = new JComboBox<String>(actualController.getCategoriesStrings());
  237. objectPanel.add(selectedCategorySwitch);
  238. JLabel switchName = new JLabel("Name:");
  239. String initialText = "The name of the new Wild Card";
  240. inputNameSwitch.setText(initialText);
  241. inputNameSwitch.setBackground(java.awt.Color.LIGHT_GRAY);
  242. inputNameSwitch.addFocusListener(new java.awt.event.FocusAdapter() {
  243. public void focusGained(java.awt.event.FocusEvent evt) {
  244. if (inputNameSwitch.getText().equals(initialText)) {
  245. inputNameSwitch.setText("");
  246. inputNameSwitch.setBackground(java.awt.Color.WHITE);
  247. }
  248. }
  249. public void focusLost (java.awt.event.FocusEvent evt) {
  250. if (inputNameSwitch.getText().equals("")) {
  251. inputNameSwitch.setText(initialText);
  252. inputNameSwitch.setBackground(java.awt.Color.LIGHT_GRAY);
  253. }
  254. }
  255. });
  256. inputNameSwitch.setColumns(20);
  257. objectPanel.add(switchName);
  258. objectPanel.add(inputNameSwitch);
  259. return objectPanel;
  260. }
  261. private void addSaveButtonLogik() {
  262. saveButton.addActionListener(actionEvent -> {
  263. if(actualOption == Option.Category)
  264. {
  265. try {
  266. actualController.addCategory(inputName.getText());
  267. } catch (IOException e) {
  268. System.err.println("IOException addCategory");
  269. }
  270. dispose();
  271. }
  272. else if (actualOption == Option.Switch)
  273. {//TODO
  274. try {
  275. actualController.addSwitch(actualController.searchCategory(selectedCategorySwitch.getSelectedItem().toString()), inputNameSwitch.getText());
  276. } catch (IOException e) {
  277. System.err.println("IOException addSwitch");
  278. }
  279. dispose();
  280. }
  281. else if (actualOption == Option.WildCard)
  282. {//TODO
  283. try {
  284. actualController.addWildCard(actualController.searchCategory(selectedCategorySwitch.getSelectedItem().toString()), inputNameSwitch.getText());
  285. } catch (IOException e) {
  286. System.err.println("IOException addWildcard");
  287. }
  288. dispose();
  289. }
  290. });
  291. }
  292. }