]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/client/console/ConsoleClient.java
Initial commit
[addressbook-swing.git] / Addressbook / src / org / mxchange / addressbook / client / console / ConsoleClient.java
1 /*\r
2  * Copyright (C) 2015 Roland Haeder\r
3  *\r
4  * This program is free software: you can redistribute it and/or modify\r
5  * it under the terms of the GNU General Public License as published by\r
6  * the Free Software Foundation, either version 3 of the License, or\r
7  * (at your option) any later version.\r
8  *\r
9  * This program is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
16  */\r
17 package org.mxchange.addressbook.client.console;\r
18 \r
19 import java.util.Arrays;\r
20 import java.util.HashMap;\r
21 import java.util.Map;\r
22 import java.util.Scanner;\r
23 import org.mxchange.addressbook.application.AddressbookApplication;\r
24 import org.mxchange.addressbook.application.Application;\r
25 import org.mxchange.addressbook.client.BaseClient;\r
26 import org.mxchange.addressbook.client.Client;\r
27 import org.mxchange.addressbook.menu.Menu;\r
28 import org.mxchange.addressbook.menu.MenuTools;\r
29 import org.mxchange.addressbook.menu.console.ConsoleMenu;\r
30 import org.mxchange.addressbook.menu.item.SelectableMenuItem;\r
31 import org.mxchange.addressbook.menu.item.console.ConsoleMenuItem;\r
32 \r
33 /**\r
34  * A client for the console\r
35  *\r
36  * @author Roland Haeder\r
37  */\r
38 public class ConsoleClient extends BaseClient implements Client {\r
39     /**\r
40      * Menu system\r
41      */\r
42     private final Map<String, Menu> menus;\r
43 \r
44     /**\r
45      * Scanner instance for reading data from console input\r
46      */\r
47     private final Scanner scanner;\r
48 \r
49     /**\r
50      * Parameterless constructor\r
51      * @param application An instance of an Application class\r
52      */\r
53     public ConsoleClient (final Application application) {\r
54         super();\r
55 \r
56         // Set application instance\r
57         this.setApplication(application);\r
58 \r
59         // Init contact manager here\r
60         this.initContactManager(this);\r
61 \r
62         // Init scanner instance\r
63         this.scanner = new Scanner(System.in);\r
64 \r
65         // Init menu map\r
66         this.menus = new HashMap<>(10);\r
67 \r
68         // Fill menu map\r
69         this.fillConsoleMenuMap();\r
70     }\r
71 \r
72     /**\r
73      * Displays textural message to the user\r
74      * @param message \r
75      */\r
76     @Override\r
77     public void displayMessage (final String message) {\r
78         System.out.println(message);\r
79     }\r
80 \r
81     @Override\r
82     public void doUserChoice () throws Exception {\r
83         // Get all access keys from menu\r
84         char[] accessKeys = MenuTools.getAccessKeysFromMenuMap(this.menus, this.getCurrentMenu());\r
85 \r
86         // Output textural message and ask for a char as input\r
87         char choice = this.enterChar(accessKeys, "Bitte Auswahl eingeben (0=Beenden/Zurück in's vorherhige Menü): ");\r
88 \r
89         // @TODO Rewrite this ugly switch() block\r
90         switch (choice) {\r
91             case '1': // Enter/add own data\r
92                 this.getContactManager().enterOwnData();\r
93                 break;\r
94         \r
95             case '2': // Change own data\r
96                 this.getContactManager().changeOwnData();\r
97                 break;\r
98         \r
99             case '3': // Add new addess\r
100                 this.getContactManager().addOtherAddress();\r
101                 break;\r
102         \r
103             case '4': // Change other addess\r
104                 this.getContactManager().changeOtherAddress();\r
105                 break;\r
106         \r
107             case '5': // Delete other address\r
108                 this.getContactManager().deleteOtherAddress();\r
109                 break;\r
110 \r
111             case '0': // Program exit\r
112                 this.disableIsRunning();\r
113                 break;\r
114         \r
115             default:\r
116                 // @TODO throw on exception\r
117                 throw new Exception("choice " + choice + " invalid");\r
118         }\r
119     }\r
120 \r
121     /**\r
122      * Asks the the user to enter a single character which must match validChars\r
123      * @param   validChars  Valid chars that are accepted\r
124      * @param   message     Message to user\r
125      * @return  Allowed character\r
126      */\r
127     @Override\r
128     public char enterChar (final char[] validChars, final String message) {\r
129         char input = 0;\r
130 \r
131         // Sort array, else binarySearch() won't work\r
132         Arrays.sort(validChars);\r
133 \r
134         // Keep asking until valid char has been entered\r
135         while (Arrays.binarySearch(validChars, input) < 0) {\r
136             // Output message\r
137             System.out.print(message);\r
138 \r
139             // Read char\r
140             input = this.readChar();\r
141         }\r
142 \r
143         // Return read char\r
144         return input;\r
145     }\r
146 \r
147     /**\r
148      * Reads a string of minimum and maximum length from the user\r
149      * @param minLength Minimum length of the string to read\r
150      * @param maxLength Maximum length of the string to read\r
151      * @param message   Message to user\r
152      * @return \r
153      */\r
154     @Override\r
155     public String enterString (final int minLength, final int maxLength, final String message) {\r
156         // Init input\r
157         String input = null;\r
158 \r
159         // Check if it is to short or to long\r
160         while ((input == null) || (input.length() < minLength) || (input.length() > maxLength)) {\r
161             // Output message\r
162             System.out.print(message);\r
163 \r
164             // Read line\r
165             input = this.readString();\r
166         }\r
167 \r
168         // Return it\r
169         return input;\r
170     }\r
171 \r
172     /**\r
173      * Returns a console menu item\r
174      * \r
175      * @param accessKey Key to access the menu\r
176      * @param text Text to show to user\r
177      * @return A SelectableMenuItem\r
178      * @todo Make sure the access key is unique\r
179      */\r
180     @Override\r
181     public SelectableMenuItem getMenuItem (final char accessKey, final String text) {\r
182         // Return a new console menu item\r
183         return new ConsoleMenuItem(accessKey,text);\r
184     }\r
185 \r
186     /**\r
187      * Shows textural menu on console\r
188      */\r
189     @Override\r
190     public void showCurrentMenu () {\r
191         this.showMenu(this.getCurrentMenu());\r
192     }\r
193 \r
194     @Override\r
195     public void showEntry (final SelectableMenuItem item) {\r
196         // Access key then text\r
197         this.displayMessage("[" + item.getAccessKey() + "] " + item.getText());\r
198     }\r
199 \r
200     /**\r
201      * Shows a textural message to the user\r
202      */\r
203     @Override\r
204     public void showWelcome () {\r
205          this.displayMessage("Welcome to " + AddressbookApplication.APP_TITLE + " v" + AddressbookApplication.APP_VERSION);\r
206          this.displayMessage("");\r
207          this.displayMessage("Copyright(c) 2015 by Roland Haeder, this is free software");\r
208          \r
209          // Debug message\r
210          this.getLogger().debug("Intro shown to user");\r
211      }\r
212 \r
213     /**\r
214      * Fills menu map with menu entries\r
215      */\r
216     private void fillConsoleMenuMap () {\r
217         // Initialize first (main) menu\r
218         Menu menu = new ConsoleMenu("main", this);\r
219 \r
220         // Add it\r
221         this.menus.put("main", menu);\r
222     }\r
223 \r
224     /**\r
225      * "Getter" for given menu type\r
226      * \r
227      * @param menuType Menu type instance to return\r
228      * @return Menu or null if not found\r
229      */\r
230     private Menu getMenu (final String menuType) {\r
231         // Default is not found\r
232         Menu menu = null;\r
233 \r
234         // Check array\r
235         if (this.menus.containsKey(menuType)) {\r
236             // Found!\r
237             menu = this.menus.get(menuType);\r
238         }\r
239         \r
240         // Return it\r
241         return menu;\r
242     }\r
243 \r
244     /**\r
245      * Reads one character\r
246      * @return \r
247      */\r
248     private char readChar () {\r
249         // Read line\r
250         String input = this.scanner.nextLine();\r
251 \r
252         // This must be only one character\r
253         if (input.length() != 1) {\r
254             // Return zero\r
255             return 0;\r
256         }\r
257 \r
258         // Get char from first (and only) position\r
259         return input.charAt(0);\r
260     }\r
261 \r
262     /**\r
263      * Reads a string from a scanner until RETURN is pressed\r
264      * \r
265      * @return Read string from scanner\r
266      */\r
267     private String readString () {\r
268         return this.scanner.nextLine();\r
269     }\r
270 \r
271     /**\r
272      * Shows given menu\r
273      *\r
274      * @param menuType Given menu to show\r
275      */\r
276     private void showMenu (final String menuType) {\r
277         Menu menu = this.getMenu(menuType);\r
278         \r
279         // Is the menu set?\r
280         if (!(menu instanceof Menu)) {\r
281             // Not found\r
282             // @todo Own exception?\r
283             throw new NullPointerException("Menu '" + menuType + "' not found.");\r
284         }\r
285         \r
286         // Show menu\r
287         menu.show(this);\r
288     }\r
289 }\r