T[] shallowCopy(final T[] array) {
89 | if (array == null) {
90 | return null;
91 | }
92 | return array.clone();
93 | }
94 |
95 | /**
96 | *
97 | * Adds all the elements of the given arrays into a new array.
98 | *
99 | *
100 | * The new array contains all of the element of array1 followed
101 | * by all of the elements array2. When an array is returned, it
102 | * is always a new array.
103 | *
104 | *
105 | * ArrayUtilities.addAll(null, null) = null
106 | * ArrayUtilities.addAll(array1, null) = cloned copy of array1
107 | * ArrayUtilities.addAll(null, array2) = cloned copy of array2
108 | * ArrayUtilities.addAll([], []) = []
109 | * ArrayUtilities.addAll([null], [null]) = [null, null]
110 | * ArrayUtilities.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
111 | *
112 | *
113 | * @param array1 the first array whose elements are added to the new array,
114 | * may be null
115 | * @param array2 the second array whose elements are added to the new array,
116 | * may be null
117 | * @param the array type
118 | * @return The new array, null if null array
119 | * inputs. The type of the new array is the type of the first array.
120 | */
121 | public static T[] addAll(final T[] array1, final T[] array2) {
122 | if (array1 == null) {
123 | return shallowCopy(array2);
124 | } else if (array2 == null) {
125 | return shallowCopy(array1);
126 | }
127 | final T[] newArray = (T[]) Array.newInstance(array1.getClass().getComponentType(), array1.length + array2.length);
128 | System.arraycopy(array1, 0, newArray, 0, array1.length);
129 | System.arraycopy(array2, 0, newArray, array1.length, array2.length);
130 | return newArray;
131 | }
132 |
133 | public static T[] removeItem(T[] array, int pos) {
134 | int length = Array.getLength(array);
135 | T[] dest = (T[]) Array.newInstance(array.getClass().getComponentType(), length - 1);
136 |
137 | System.arraycopy(array, 0, dest, 0, pos);
138 | System.arraycopy(array, pos + 1, dest, pos, length - pos - 1);
139 | return dest;
140 | }
141 |
142 | public static T[] getArraySubset(T[] array, int start, int end) {
143 | return Arrays.copyOfRange(array, start, end);
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.wordlibrary/src/main/java/com/wordlibrary/utils/ArrayUtilities.java:
--------------------------------------------------------------------------------
1 | package com.wordlibrary.utils;
2 |
3 | import java.lang.reflect.Array;
4 | import java.util.Arrays;
5 |
6 | /**
7 | * Handy utilities for working with Java arrays.
8 | *
9 | * @author Ken Partlow
10 | * @author John DeRegnaucourt (john@cedarsoftware.com)
11 | *
12 | * Copyright (c) Cedar Software LLC
13 | *
14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
15 | * use this file except in compliance with the License. You may obtain a copy of
16 | * the License at
17 | *
18 | * http://www.apache.org/licenses/LICENSE-2.0
19 | *
20 | * Unless required by applicable law or agreed to in writing, software
21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
23 | * License for the specific language governing permissions and limitations under
24 | * the License.
25 | */
26 | public final class ArrayUtilities {
27 |
28 | /**
29 | * Immutable common arrays.
30 | */
31 | public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
32 | public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
33 |
34 | /**
35 | * Private constructor to promote using as static class.
36 | */
37 | private ArrayUtilities() {
38 | super();
39 | }
40 |
41 | /**
42 | * This is a null-safe isEmpty check. It uses the Array static class for
43 | * doing a length check. This check is actually .0001 ms slower than the
44 | * following typed check:
45 | *
46 | * return array == null || array.length == 0;
47 | *
48 | * but gives you more flexibility, since it checks for all array types.
49 | *
50 | * @param array array to check
51 | * @return true if empty or null
52 | */
53 | public static boolean isEmpty(final Object array) {
54 | return array == null || Array.getLength(array) == 0;
55 | }
56 |
57 | /**
58 | * This is a null-safe size check. It uses the Array static class for doing
59 | * a length check. This check is actually .0001 ms slower than the following
60 | * typed check:
61 | *
62 | * return (array == null) ? 0 : array.length;
63 | *
64 | *
65 | * @param array array to check
66 | * @return true if empty or null
67 | */
68 | public static int size(final Object array) {
69 | return array == null ? 0 : Array.getLength(array);
70 | }
71 |
72 | /**
73 | *
74 | * Shallow copies an array of Objects
75 | *
76 | *
77 | * The objects in the array are not cloned, thus there is no special
78 | * handling for multi-dimensional arrays.
79 | *
80 | *
81 | * This method returns null if null array
82 | * input.
83 | *
84 | * @param array the array to shallow clone, may be null
85 | * @param the array type
86 | * @return the cloned array, null if null input
87 | */
88 | public static T[] shallowCopy(final T[] array) {
89 | if (array == null) {
90 | return null;
91 | }
92 | return array.clone();
93 | }
94 |
95 | /**
96 | *
97 | * Adds all the elements of the given arrays into a new array.
98 | *
99 | *
100 | * The new array contains all of the element of array1 followed
101 | * by all of the elements array2. When an array is returned, it
102 | * is always a new array.
103 | *
104 | *
105 | * ArrayUtilities.addAll(null, null) = null
106 | * ArrayUtilities.addAll(array1, null) = cloned copy of array1
107 | * ArrayUtilities.addAll(null, array2) = cloned copy of array2
108 | * ArrayUtilities.addAll([], []) = []
109 | * ArrayUtilities.addAll([null], [null]) = [null, null]
110 | * ArrayUtilities.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
111 | *
112 | *
113 | * @param array1 the first array whose elements are added to the new array,
114 | * may be null
115 | * @param array2 the second array whose elements are added to the new array,
116 | * may be null
117 | * @param the array type
118 | * @return The new array, null if null array
119 | * inputs. The type of the new array is the type of the first array.
120 | */
121 | public static T[] addAll(final T[] array1, final T[] array2) {
122 | if (array1 == null) {
123 | return shallowCopy(array2);
124 | } else if (array2 == null) {
125 | return shallowCopy(array1);
126 | }
127 | final T[] newArray = (T[]) Array.newInstance(array1.getClass().getComponentType(), array1.length + array2.length);
128 | System.arraycopy(array1, 0, newArray, 0, array1.length);
129 | System.arraycopy(array2, 0, newArray, array1.length, array2.length);
130 | return newArray;
131 | }
132 |
133 | public static T[] removeItem(T[] array, int pos) {
134 | int length = Array.getLength(array);
135 | T[] dest = (T[]) Array.newInstance(array.getClass().getComponentType(), length - 1);
136 |
137 | System.arraycopy(array, 0, dest, 0, pos);
138 | System.arraycopy(array, pos + 1, dest, pos, length - pos - 1);
139 | return dest;
140 | }
141 |
142 | public static T[] getArraySubset(T[] array, int start, int end) {
143 | return Arrays.copyOfRange(array, start, end);
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/AnagramGame/src/com.wordlibrary/classes/com/wordlibrary/utils/DateUtilities.java:
--------------------------------------------------------------------------------
1 | package com.wordlibrary.utils;
2 |
3 | import java.util.Calendar;
4 | import java.util.Date;
5 | import java.util.LinkedHashMap;
6 | import java.util.Map;
7 | import java.util.TimeZone;
8 | import java.util.regex.Matcher;
9 | import java.util.regex.Pattern;
10 |
11 | /**
12 | * Handy utilities for working with Java Dates.
13 | *
14 | * @author John DeRegnaucourt (john@cedarsoftware.com)
15 | *
16 | * Copyright (c) Cedar Software LLC
17 | *
18 | * Licensed under the Apache License, Version 2.0 (the "License");
19 | * you may not use this file except in compliance with the License.
20 | * You may obtain a copy of the License at
21 | *
22 | * http://www.apache.org/licenses/LICENSE-2.0
23 | *
24 | * Unless required by applicable law or agreed to in writing, software
25 | * distributed under the License is distributed on an "AS IS" BASIS,
26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 | * See the License for the specific language governing permissions and
28 | * limitations under the License.
29 | */
30 | public final class DateUtilities
31 | {
32 | private static final String days = "(monday|mon|tuesday|tues|tue|wednesday|wed|thursday|thur|thu|friday|fri|saturday|sat|sunday|sun)"; // longer before shorter matters
33 | private static final String mos = "(Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|Sept|September|Oct|October|Nov|November|Dec|December)";
34 | private static final Pattern datePattern1 = Pattern.compile("(\\d{4})[./-](\\d{1,2})[./-](\\d{1,2})");
35 | private static final Pattern datePattern2 = Pattern.compile("(\\d{1,2})[./-](\\d{1,2})[./-](\\d{4})");
36 | private static final Pattern datePattern3 = Pattern.compile(mos + "[ ]*[,]?[ ]*(\\d{1,2})(st|nd|rd|th|)[ ]*[,]?[ ]*(\\d{4})", Pattern.CASE_INSENSITIVE);
37 | private static final Pattern datePattern4 = Pattern.compile("(\\d{1,2})(st|nd|rd|th|)[ ]*[,]?[ ]*" + mos + "[ ]*[,]?[ ]*(\\d{4})", Pattern.CASE_INSENSITIVE);
38 | private static final Pattern datePattern5 = Pattern.compile("(\\d{4})[ ]*[,]?[ ]*" + mos + "[ ]*[,]?[ ]*(\\d{1,2})(st|nd|rd|th|)", Pattern.CASE_INSENSITIVE);
39 | private static final Pattern datePattern6 = Pattern.compile(days+"[ ]+" + mos + "[ ]+(\\d{1,2})[ ]+(\\d{2}:\\d{2}:\\d{2})[ ]+[A-Z]{1,3}\\s+(\\d{4})", Pattern.CASE_INSENSITIVE);
40 | private static final Pattern timePattern1 = Pattern.compile("(\\d{2})[:.](\\d{2})[:.](\\d{2})[.](\\d{1,10})([+-]\\d{2}[:]?\\d{2}|Z)?");
41 | private static final Pattern timePattern2 = Pattern.compile("(\\d{2})[:.](\\d{2})[:.](\\d{2})([+-]\\d{2}[:]?\\d{2}|Z)?");
42 | private static final Pattern timePattern3 = Pattern.compile("(\\d{2})[:.](\\d{2})([+-]\\d{2}[:]?\\d{2}|Z)?");
43 | private static final Pattern dayPattern = Pattern.compile(days, Pattern.CASE_INSENSITIVE);
44 | private static final Map months = new LinkedHashMap<>();
45 |
46 | static
47 | {
48 | // Month name to number map
49 | months.put("jan", "1");
50 | months.put("january", "1");
51 | months.put("feb", "2");
52 | months.put("february", "2");
53 | months.put("mar", "3");
54 | months.put("march", "3");
55 | months.put("apr", "4");
56 | months.put("april", "4");
57 | months.put("may", "5");
58 | months.put("jun", "6");
59 | months.put("june", "6");
60 | months.put("jul", "7");
61 | months.put("july", "7");
62 | months.put("aug", "8");
63 | months.put("august", "8");
64 | months.put("sep", "9");
65 | months.put("sept", "9");
66 | months.put("september", "9");
67 | months.put("oct", "10");
68 | months.put("october", "10");
69 | months.put("nov", "11");
70 | months.put("november", "11");
71 | months.put("dec", "12");
72 | months.put("december", "12");
73 | }
74 |
75 | private DateUtilities() {
76 | super();
77 | }
78 |
79 | public static Date parseDate(String dateStr)
80 | {
81 | if (dateStr == null)
82 | {
83 | return null;
84 | }
85 | dateStr = dateStr.trim();
86 | if ("".equals(dateStr))
87 | {
88 | return null;
89 | }
90 |
91 | // Determine which date pattern (Matcher) to use
92 | Matcher matcher = datePattern1.matcher(dateStr);
93 |
94 | String year, month = null, day, mon = null, remains;
95 |
96 | if (matcher.find())
97 | {
98 | year = matcher.group(1);
99 | month = matcher.group(2);
100 | day = matcher.group(3);
101 | remains = matcher.replaceFirst("");
102 | }
103 | else
104 | {
105 | matcher = datePattern2.matcher(dateStr);
106 | if (matcher.find())
107 | {
108 | month = matcher.group(1);
109 | day = matcher.group(2);
110 | year = matcher.group(3);
111 | remains = matcher.replaceFirst("");
112 | }
113 | else
114 | {
115 | matcher = datePattern3.matcher(dateStr);
116 | if (matcher.find())
117 | {
118 | mon = matcher.group(1);
119 | day = matcher.group(2);
120 | year = matcher.group(4);
121 | remains = matcher.replaceFirst("");
122 | }
123 | else
124 | {
125 | matcher = datePattern4.matcher(dateStr);
126 | if (matcher.find())
127 | {
128 | day = matcher.group(1);
129 | mon = matcher.group(3);
130 | year = matcher.group(4);
131 | remains = matcher.replaceFirst("");
132 | }
133 | else
134 | {
135 | matcher = datePattern5.matcher(dateStr);
136 | if (matcher.find())
137 | {
138 | year = matcher.group(1);
139 | mon = matcher.group(2);
140 | day = matcher.group(3);
141 | remains = matcher.replaceFirst("");
142 | }
143 | else
144 | {
145 | matcher = datePattern6.matcher(dateStr);
146 | if (!matcher.find())
147 | {
148 | error("Unable to parse: " + dateStr);
149 | }
150 | year = matcher.group(5);
151 | mon = matcher.group(2);
152 | day = matcher.group(3);
153 | remains = matcher.group(4);
154 | }
155 | }
156 | }
157 | }
158 | }
159 |
160 | if (mon != null)
161 | { // Month will always be in Map, because regex forces this.
162 | month = months.get(mon.trim().toLowerCase());
163 | }
164 |
165 | // Determine which date pattern (Matcher) to use
166 | String hour = null, min = null, sec = "00", milli = "0", tz = null;
167 | remains = remains.trim();
168 | matcher = timePattern1.matcher(remains);
169 | if (matcher.find())
170 | {
171 | hour = matcher.group(1);
172 | min = matcher.group(2);
173 | sec = matcher.group(3);
174 | milli = matcher.group(4);
175 | if (matcher.groupCount() > 4)
176 | {
177 | tz = matcher.group(5);
178 | }
179 | }
180 | else
181 | {
182 | matcher = timePattern2.matcher(remains);
183 | if (matcher.find())
184 | {
185 | hour = matcher.group(1);
186 | min = matcher.group(2);
187 | sec = matcher.group(3);
188 | if (matcher.groupCount() > 3)
189 | {
190 | tz = matcher.group(4);
191 | }
192 | }
193 | else
194 | {
195 | matcher = timePattern3.matcher(remains);
196 | if (matcher.find())
197 | {
198 | hour = matcher.group(1);
199 | min = matcher.group(2);
200 | if (matcher.groupCount() > 2)
201 | {
202 | tz = matcher.group(3);
203 | }
204 | }
205 | else
206 | {
207 | matcher = null;
208 | }
209 | }
210 | }
211 |
212 | if (matcher != null)
213 | {
214 | remains = matcher.replaceFirst("");
215 | }
216 |
217 | // Clear out day of week (mon, tue, wed, ...)
218 | // if (StringUtilities.length(remains) > 0)
219 | // {
220 | // Matcher dayMatcher = dayPattern.matcher(remains);
221 | // if (dayMatcher.find())
222 | // {
223 | // remains = dayMatcher.replaceFirst("").trim();
224 | // }
225 | // }
226 | // if (StringUtilities.length(remains) > 0)
227 | // {
228 | // remains = remains.trim();
229 | // if (!remains.equals(",") && (!remains.equals("T")))
230 | // {
231 | // error("Issue parsing data/time, other characters present: " + remains);
232 | // }
233 | // }
234 |
235 | Calendar c = Calendar.getInstance();
236 | c.clear();
237 | if (tz != null)
238 | {
239 | if ("z".equalsIgnoreCase(tz))
240 | {
241 | c.setTimeZone(TimeZone.getTimeZone("GMT"));
242 | }
243 | else
244 | {
245 | c.setTimeZone(TimeZone.getTimeZone("GMT" + tz));
246 | }
247 | }
248 |
249 | // Regex prevents these from ever failing to parse
250 | int y = Integer.parseInt(year);
251 | int m = Integer.parseInt(month) - 1; // months are 0-based
252 | int d = Integer.parseInt(day);
253 |
254 | if (m < 0 || m > 11)
255 | {
256 | error("Month must be between 1 and 12 inclusive, date: " + dateStr);
257 | }
258 | if (d < 1 || d > 31)
259 | {
260 | error("Day must be between 1 and 31 inclusive, date: " + dateStr);
261 | }
262 |
263 | if (matcher == null)
264 | { // no [valid] time portion
265 | c.set(y, m, d);
266 | }
267 | else
268 | {
269 | // Regex prevents these from ever failing to parse.
270 | int h = Integer.parseInt(hour);
271 | int mn = Integer.parseInt(min);
272 | int s = Integer.parseInt(sec);
273 | int ms = Integer.parseInt(milli);
274 |
275 | if (h > 23)
276 | {
277 | error("Hour must be between 0 and 23 inclusive, time: " + dateStr);
278 | }
279 | if (mn > 59)
280 | {
281 | error("Minute must be between 0 and 59 inclusive, time: " + dateStr);
282 | }
283 | if (s > 59)
284 | {
285 | error("Second must be between 0 and 59 inclusive, time: " + dateStr);
286 | }
287 |
288 | // regex enforces millis to number
289 | c.set(y, m, d, h, mn, s);
290 | c.set(Calendar.MILLISECOND, ms);
291 | }
292 | return c.getTime();
293 | }
294 |
295 | private static void error(String msg)
296 | {
297 | throw new IllegalArgumentException(msg);
298 | }
299 | }
--------------------------------------------------------------------------------
/AnagramGameMaven/com.wordlibrary/src/main/java/com/wordlibrary/utils/DateUtilities.java:
--------------------------------------------------------------------------------
1 | package com.wordlibrary.utils;
2 |
3 | import java.util.Calendar;
4 | import java.util.Date;
5 | import java.util.LinkedHashMap;
6 | import java.util.Map;
7 | import java.util.TimeZone;
8 | import java.util.regex.Matcher;
9 | import java.util.regex.Pattern;
10 |
11 | /**
12 | * Handy utilities for working with Java Dates.
13 | *
14 | * @author John DeRegnaucourt (john@cedarsoftware.com)
15 | *
16 | * Copyright (c) Cedar Software LLC
17 | *
18 | * Licensed under the Apache License, Version 2.0 (the "License");
19 | * you may not use this file except in compliance with the License.
20 | * You may obtain a copy of the License at
21 | *
22 | * http://www.apache.org/licenses/LICENSE-2.0
23 | *
24 | * Unless required by applicable law or agreed to in writing, software
25 | * distributed under the License is distributed on an "AS IS" BASIS,
26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 | * See the License for the specific language governing permissions and
28 | * limitations under the License.
29 | */
30 | public final class DateUtilities
31 | {
32 | private static final String days = "(monday|mon|tuesday|tues|tue|wednesday|wed|thursday|thur|thu|friday|fri|saturday|sat|sunday|sun)"; // longer before shorter matters
33 | private static final String mos = "(Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|Sept|September|Oct|October|Nov|November|Dec|December)";
34 | private static final Pattern datePattern1 = Pattern.compile("(\\d{4})[./-](\\d{1,2})[./-](\\d{1,2})");
35 | private static final Pattern datePattern2 = Pattern.compile("(\\d{1,2})[./-](\\d{1,2})[./-](\\d{4})");
36 | private static final Pattern datePattern3 = Pattern.compile(mos + "[ ]*[,]?[ ]*(\\d{1,2})(st|nd|rd|th|)[ ]*[,]?[ ]*(\\d{4})", Pattern.CASE_INSENSITIVE);
37 | private static final Pattern datePattern4 = Pattern.compile("(\\d{1,2})(st|nd|rd|th|)[ ]*[,]?[ ]*" + mos + "[ ]*[,]?[ ]*(\\d{4})", Pattern.CASE_INSENSITIVE);
38 | private static final Pattern datePattern5 = Pattern.compile("(\\d{4})[ ]*[,]?[ ]*" + mos + "[ ]*[,]?[ ]*(\\d{1,2})(st|nd|rd|th|)", Pattern.CASE_INSENSITIVE);
39 | private static final Pattern datePattern6 = Pattern.compile(days+"[ ]+" + mos + "[ ]+(\\d{1,2})[ ]+(\\d{2}:\\d{2}:\\d{2})[ ]+[A-Z]{1,3}\\s+(\\d{4})", Pattern.CASE_INSENSITIVE);
40 | private static final Pattern timePattern1 = Pattern.compile("(\\d{2})[:.](\\d{2})[:.](\\d{2})[.](\\d{1,10})([+-]\\d{2}[:]?\\d{2}|Z)?");
41 | private static final Pattern timePattern2 = Pattern.compile("(\\d{2})[:.](\\d{2})[:.](\\d{2})([+-]\\d{2}[:]?\\d{2}|Z)?");
42 | private static final Pattern timePattern3 = Pattern.compile("(\\d{2})[:.](\\d{2})([+-]\\d{2}[:]?\\d{2}|Z)?");
43 | private static final Pattern dayPattern = Pattern.compile(days, Pattern.CASE_INSENSITIVE);
44 | private static final Map months = new LinkedHashMap<>();
45 |
46 | static
47 | {
48 | // Month name to number map
49 | months.put("jan", "1");
50 | months.put("january", "1");
51 | months.put("feb", "2");
52 | months.put("february", "2");
53 | months.put("mar", "3");
54 | months.put("march", "3");
55 | months.put("apr", "4");
56 | months.put("april", "4");
57 | months.put("may", "5");
58 | months.put("jun", "6");
59 | months.put("june", "6");
60 | months.put("jul", "7");
61 | months.put("july", "7");
62 | months.put("aug", "8");
63 | months.put("august", "8");
64 | months.put("sep", "9");
65 | months.put("sept", "9");
66 | months.put("september", "9");
67 | months.put("oct", "10");
68 | months.put("october", "10");
69 | months.put("nov", "11");
70 | months.put("november", "11");
71 | months.put("dec", "12");
72 | months.put("december", "12");
73 | }
74 |
75 | private DateUtilities() {
76 | super();
77 | }
78 |
79 | public static Date parseDate(String dateStr)
80 | {
81 | if (dateStr == null)
82 | {
83 | return null;
84 | }
85 | dateStr = dateStr.trim();
86 | if ("".equals(dateStr))
87 | {
88 | return null;
89 | }
90 |
91 | // Determine which date pattern (Matcher) to use
92 | Matcher matcher = datePattern1.matcher(dateStr);
93 |
94 | String year, month = null, day, mon = null, remains;
95 |
96 | if (matcher.find())
97 | {
98 | year = matcher.group(1);
99 | month = matcher.group(2);
100 | day = matcher.group(3);
101 | remains = matcher.replaceFirst("");
102 | }
103 | else
104 | {
105 | matcher = datePattern2.matcher(dateStr);
106 | if (matcher.find())
107 | {
108 | month = matcher.group(1);
109 | day = matcher.group(2);
110 | year = matcher.group(3);
111 | remains = matcher.replaceFirst("");
112 | }
113 | else
114 | {
115 | matcher = datePattern3.matcher(dateStr);
116 | if (matcher.find())
117 | {
118 | mon = matcher.group(1);
119 | day = matcher.group(2);
120 | year = matcher.group(4);
121 | remains = matcher.replaceFirst("");
122 | }
123 | else
124 | {
125 | matcher = datePattern4.matcher(dateStr);
126 | if (matcher.find())
127 | {
128 | day = matcher.group(1);
129 | mon = matcher.group(3);
130 | year = matcher.group(4);
131 | remains = matcher.replaceFirst("");
132 | }
133 | else
134 | {
135 | matcher = datePattern5.matcher(dateStr);
136 | if (matcher.find())
137 | {
138 | year = matcher.group(1);
139 | mon = matcher.group(2);
140 | day = matcher.group(3);
141 | remains = matcher.replaceFirst("");
142 | }
143 | else
144 | {
145 | matcher = datePattern6.matcher(dateStr);
146 | if (!matcher.find())
147 | {
148 | error("Unable to parse: " + dateStr);
149 | }
150 | year = matcher.group(5);
151 | mon = matcher.group(2);
152 | day = matcher.group(3);
153 | remains = matcher.group(4);
154 | }
155 | }
156 | }
157 | }
158 | }
159 |
160 | if (mon != null)
161 | { // Month will always be in Map, because regex forces this.
162 | month = months.get(mon.trim().toLowerCase());
163 | }
164 |
165 | // Determine which date pattern (Matcher) to use
166 | String hour = null, min = null, sec = "00", milli = "0", tz = null;
167 | remains = remains.trim();
168 | matcher = timePattern1.matcher(remains);
169 | if (matcher.find())
170 | {
171 | hour = matcher.group(1);
172 | min = matcher.group(2);
173 | sec = matcher.group(3);
174 | milli = matcher.group(4);
175 | if (matcher.groupCount() > 4)
176 | {
177 | tz = matcher.group(5);
178 | }
179 | }
180 | else
181 | {
182 | matcher = timePattern2.matcher(remains);
183 | if (matcher.find())
184 | {
185 | hour = matcher.group(1);
186 | min = matcher.group(2);
187 | sec = matcher.group(3);
188 | if (matcher.groupCount() > 3)
189 | {
190 | tz = matcher.group(4);
191 | }
192 | }
193 | else
194 | {
195 | matcher = timePattern3.matcher(remains);
196 | if (matcher.find())
197 | {
198 | hour = matcher.group(1);
199 | min = matcher.group(2);
200 | if (matcher.groupCount() > 2)
201 | {
202 | tz = matcher.group(3);
203 | }
204 | }
205 | else
206 | {
207 | matcher = null;
208 | }
209 | }
210 | }
211 |
212 | if (matcher != null)
213 | {
214 | remains = matcher.replaceFirst("");
215 | }
216 |
217 | // Clear out day of week (mon, tue, wed, ...)
218 | // if (StringUtilities.length(remains) > 0)
219 | // {
220 | // Matcher dayMatcher = dayPattern.matcher(remains);
221 | // if (dayMatcher.find())
222 | // {
223 | // remains = dayMatcher.replaceFirst("").trim();
224 | // }
225 | // }
226 | // if (StringUtilities.length(remains) > 0)
227 | // {
228 | // remains = remains.trim();
229 | // if (!remains.equals(",") && (!remains.equals("T")))
230 | // {
231 | // error("Issue parsing data/time, other characters present: " + remains);
232 | // }
233 | // }
234 |
235 | Calendar c = Calendar.getInstance();
236 | c.clear();
237 | if (tz != null)
238 | {
239 | if ("z".equalsIgnoreCase(tz))
240 | {
241 | c.setTimeZone(TimeZone.getTimeZone("GMT"));
242 | }
243 | else
244 | {
245 | c.setTimeZone(TimeZone.getTimeZone("GMT" + tz));
246 | }
247 | }
248 |
249 | // Regex prevents these from ever failing to parse
250 | int y = Integer.parseInt(year);
251 | int m = Integer.parseInt(month) - 1; // months are 0-based
252 | int d = Integer.parseInt(day);
253 |
254 | if (m < 0 || m > 11)
255 | {
256 | error("Month must be between 1 and 12 inclusive, date: " + dateStr);
257 | }
258 | if (d < 1 || d > 31)
259 | {
260 | error("Day must be between 1 and 31 inclusive, date: " + dateStr);
261 | }
262 |
263 | if (matcher == null)
264 | { // no [valid] time portion
265 | c.set(y, m, d);
266 | }
267 | else
268 | {
269 | // Regex prevents these from ever failing to parse.
270 | int h = Integer.parseInt(hour);
271 | int mn = Integer.parseInt(min);
272 | int s = Integer.parseInt(sec);
273 | int ms = Integer.parseInt(milli);
274 |
275 | if (h > 23)
276 | {
277 | error("Hour must be between 0 and 23 inclusive, time: " + dateStr);
278 | }
279 | if (mn > 59)
280 | {
281 | error("Minute must be between 0 and 59 inclusive, time: " + dateStr);
282 | }
283 | if (s > 59)
284 | {
285 | error("Second must be between 0 and 59 inclusive, time: " + dateStr);
286 | }
287 |
288 | // regex enforces millis to number
289 | c.set(y, m, d, h, mn, s);
290 | c.set(Calendar.MILLISECOND, ms);
291 | }
292 | return c.getTime();
293 | }
294 |
295 | private static void error(String msg)
296 | {
297 | throw new IllegalArgumentException(msg);
298 | }
299 | }
--------------------------------------------------------------------------------
/AnagramGameMaven/com.toy.anagram/src/main/java/com/toy/anagram/ui/Anagrams.form:
--------------------------------------------------------------------------------
1 |
2 |
3 |
211 |
--------------------------------------------------------------------------------
/AnagramGame/src/com.toy.anagrams/classes/com/toy/anagrams/ui/Anagrams.form:
--------------------------------------------------------------------------------
1 |
2 |
3 |
214 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.toy.anagram/src/main/java/com/toy/anagram/ui/Anagrams.java:
--------------------------------------------------------------------------------
1 |
2 | /* Anagram Game Application */
3 |
4 | package com.toy.anagram.ui;
5 |
6 | import com.wordlibrary.WordLibrary;
7 | import java.awt.Dimension;
8 | import java.awt.Point;
9 | import java.awt.Toolkit;
10 | import java.util.List;
11 | import javax.swing.DefaultComboBoxModel;
12 | import javax.swing.JFrame;
13 | import javax.swing.SwingUtilities;
14 |
15 | /**
16 | * Main window of the Anagram Game application.
17 | */
18 | public class Anagrams extends JFrame {
19 |
20 | public static void main(String[] args) {
21 | /* Set the Nimbus look and feel */
22 | //
23 | /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
24 | * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
25 | */
26 | try {
27 | javax.swing.UIManager.LookAndFeelInfo[] installedLookAndFeels=javax.swing.UIManager.getInstalledLookAndFeels();
28 | for (int idx=0; idx
43 | //
44 | //
45 | //
46 |
47 | /* Create and display the form */
48 | SwingUtilities.invokeLater(new Runnable() {
49 | public void run() {
50 | new Anagrams().setVisible(true);
51 | }
52 | });
53 | }
54 |
55 | private int wordIdx = 0;
56 |
57 | private DefaultComboBoxModel dcbm = new DefaultComboBoxModel();
58 |
59 |
60 | // private WordLibrary wordLibrary;
61 |
62 | /** Creates new form Anagrams */
63 | public Anagrams() {
64 |
65 | List libraries = WordLibrary.openWordLibraries();
66 |
67 | initComponents();
68 |
69 | for (WordLibrary library : libraries) {
70 | dcbm.addElement(library);
71 | }
72 |
73 | jComboBox1.setModel(dcbm);
74 |
75 | getRootPane().setDefaultButton(guessButton);
76 | pack();
77 | guessedWord.requestFocusInWindow();
78 | // Center in the screen
79 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
80 | Dimension frameSize = getSize();
81 | setLocation(new Point((screenSize.width - frameSize.width) / 2,
82 | (screenSize.height - frameSize.width) / 2));
83 | }
84 |
85 | /** This method is called from within the constructor to
86 | * initialize the form.
87 | * WARNING: Do NOT modify this code. The content of this method is
88 | * always regenerated by the Form Editor.
89 | */
90 | // //GEN-BEGIN:initComponents
91 | private void initComponents() {
92 | java.awt.GridBagConstraints gridBagConstraints;
93 |
94 | mainPanel = new javax.swing.JPanel();
95 | scrambledLabel = new javax.swing.JLabel();
96 | scrambledWord = new javax.swing.JTextField();
97 | guessLabel = new javax.swing.JLabel();
98 | guessedWord = new javax.swing.JTextField();
99 | feedbackLabel = new javax.swing.JLabel();
100 | buttonsPanel = new javax.swing.JPanel();
101 | guessButton = new javax.swing.JButton();
102 | nextTrial = new javax.swing.JButton();
103 | jComboBox1 = new javax.swing.JComboBox<>();
104 | jLabel1 = new javax.swing.JLabel();
105 | mainMenu = new javax.swing.JMenuBar();
106 | fileMenu = new javax.swing.JMenu();
107 | aboutMenuItem = new javax.swing.JMenuItem();
108 | exitMenuItem = new javax.swing.JMenuItem();
109 |
110 | setTitle("Anagrams");
111 | addWindowListener(new java.awt.event.WindowAdapter() {
112 | public void windowClosing(java.awt.event.WindowEvent evt) {
113 | exitForm(evt);
114 | }
115 | });
116 |
117 | mainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12));
118 | mainPanel.setMinimumSize(new java.awt.Dimension(297, 200));
119 | mainPanel.setLayout(new java.awt.GridBagLayout());
120 |
121 | scrambledLabel.setText("Scrambled Word:");
122 | gridBagConstraints = new java.awt.GridBagConstraints();
123 | gridBagConstraints.gridx = 0;
124 | gridBagConstraints.gridy = 1;
125 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
126 | gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
127 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
128 | mainPanel.add(scrambledLabel, gridBagConstraints);
129 |
130 | scrambledWord.setEditable(false);
131 | scrambledWord.setColumns(20);
132 | gridBagConstraints = new java.awt.GridBagConstraints();
133 | gridBagConstraints.gridx = 1;
134 | gridBagConstraints.gridy = 1;
135 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
136 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
137 | gridBagConstraints.weightx = 1.0;
138 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
139 | mainPanel.add(scrambledWord, gridBagConstraints);
140 |
141 | guessLabel.setDisplayedMnemonic('Y');
142 | guessLabel.setLabelFor(guessedWord);
143 | guessLabel.setText("Your Guess:");
144 | gridBagConstraints = new java.awt.GridBagConstraints();
145 | gridBagConstraints.gridx = 0;
146 | gridBagConstraints.gridy = 2;
147 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
148 | gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
149 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 6);
150 | mainPanel.add(guessLabel, gridBagConstraints);
151 |
152 | guessedWord.setColumns(20);
153 | gridBagConstraints = new java.awt.GridBagConstraints();
154 | gridBagConstraints.gridx = 1;
155 | gridBagConstraints.gridy = 2;
156 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
157 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
158 | gridBagConstraints.weightx = 1.0;
159 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
160 | mainPanel.add(guessedWord, gridBagConstraints);
161 |
162 | feedbackLabel.setText(" ");
163 | gridBagConstraints = new java.awt.GridBagConstraints();
164 | gridBagConstraints.gridx = 1;
165 | gridBagConstraints.gridy = 3;
166 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
167 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
168 | gridBagConstraints.weightx = 1.0;
169 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
170 | mainPanel.add(feedbackLabel, gridBagConstraints);
171 |
172 | buttonsPanel.setLayout(new java.awt.GridBagLayout());
173 |
174 | guessButton.setMnemonic('G');
175 | guessButton.setText("Guess");
176 | guessButton.setToolTipText("Guess the scrambled word.");
177 | guessButton.addActionListener(new java.awt.event.ActionListener() {
178 | public void actionPerformed(java.awt.event.ActionEvent evt) {
179 | guessedWordActionPerformed(evt);
180 | }
181 | });
182 | gridBagConstraints = new java.awt.GridBagConstraints();
183 | gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
184 | gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
185 | gridBagConstraints.weightx = 1.0;
186 | gridBagConstraints.weighty = 1.0;
187 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
188 | buttonsPanel.add(guessButton, gridBagConstraints);
189 |
190 | nextTrial.setMnemonic('N');
191 | nextTrial.setText("New Word");
192 | nextTrial.setToolTipText("Fetch a new word.");
193 | nextTrial.addActionListener(new java.awt.event.ActionListener() {
194 | public void actionPerformed(java.awt.event.ActionEvent evt) {
195 | nextTrialActionPerformed(evt);
196 | }
197 | });
198 | gridBagConstraints = new java.awt.GridBagConstraints();
199 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
200 | gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
201 | gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
202 | gridBagConstraints.weighty = 1.0;
203 | buttonsPanel.add(nextTrial, gridBagConstraints);
204 |
205 | gridBagConstraints = new java.awt.GridBagConstraints();
206 | gridBagConstraints.gridx = 0;
207 | gridBagConstraints.gridy = 4;
208 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
209 | gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
210 | gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
211 | gridBagConstraints.weighty = 1.0;
212 | mainPanel.add(buttonsPanel, gridBagConstraints);
213 |
214 | jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
215 | gridBagConstraints = new java.awt.GridBagConstraints();
216 | gridBagConstraints.gridx = 1;
217 | gridBagConstraints.gridy = 0;
218 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
219 | gridBagConstraints.ipady = 6;
220 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
221 | mainPanel.add(jComboBox1, gridBagConstraints);
222 |
223 | jLabel1.setText("Category:");
224 | gridBagConstraints = new java.awt.GridBagConstraints();
225 | gridBagConstraints.gridx = 0;
226 | gridBagConstraints.gridy = 0;
227 | gridBagConstraints.ipady = 6;
228 | gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
229 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
230 | mainPanel.add(jLabel1, gridBagConstraints);
231 |
232 | getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
233 |
234 | fileMenu.setMnemonic('F');
235 | fileMenu.setText("File");
236 |
237 | aboutMenuItem.setMnemonic('A');
238 | aboutMenuItem.setText("About");
239 | aboutMenuItem.setToolTipText("About");
240 | aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
241 | public void actionPerformed(java.awt.event.ActionEvent evt) {
242 | aboutMenuItemActionPerformed(evt);
243 | }
244 | });
245 | fileMenu.add(aboutMenuItem);
246 |
247 | exitMenuItem.setMnemonic('E');
248 | exitMenuItem.setText("Exit");
249 | exitMenuItem.setToolTipText("Quit Team, Quit!");
250 | exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
251 | public void actionPerformed(java.awt.event.ActionEvent evt) {
252 | exitMenuItemActionPerformed(evt);
253 | }
254 | });
255 | fileMenu.add(exitMenuItem);
256 |
257 | mainMenu.add(fileMenu);
258 |
259 | setJMenuBar(mainMenu);
260 | }// //GEN-END:initComponents
261 |
262 | private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
263 | new About(this).setVisible(true);
264 | }//GEN-LAST:event_aboutMenuItemActionPerformed
265 |
266 | private void nextTrialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextTrialActionPerformed
267 |
268 | WordLibrary wordLibrary = (WordLibrary) dcbm.getSelectedItem();
269 |
270 | scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
271 |
272 | wordIdx = (wordIdx + 1) % wordLibrary.getSize();
273 |
274 | feedbackLabel.setText(" ");
275 | scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
276 | guessedWord.setText("");
277 | getRootPane().setDefaultButton(guessButton);
278 |
279 | guessedWord.requestFocusInWindow();
280 | }//GEN-LAST:event_nextTrialActionPerformed
281 |
282 | private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
283 | System.exit(0);
284 | }//GEN-LAST:event_exitMenuItemActionPerformed
285 |
286 | private void guessedWordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guessedWordActionPerformed
287 | WordLibrary wordLibrary = (WordLibrary) dcbm.getSelectedItem();
288 |
289 | if (wordLibrary.isCorrect(wordIdx, guessedWord.getText())){
290 | feedbackLabel.setText("Correct! Try a new word!");
291 | getRootPane().setDefaultButton(nextTrial);
292 | } else {
293 | feedbackLabel.setText("Incorrect! Try again!");
294 | guessedWord.setText("");
295 | }
296 |
297 | guessedWord.requestFocusInWindow();
298 | }//GEN-LAST:event_guessedWordActionPerformed
299 |
300 | private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
301 | System.exit(0);
302 | }//GEN-LAST:event_exitForm
303 |
304 | // Variables declaration - do not modify//GEN-BEGIN:variables
305 | private javax.swing.JMenuItem aboutMenuItem;
306 | private javax.swing.JPanel buttonsPanel;
307 | private javax.swing.JMenuItem exitMenuItem;
308 | private javax.swing.JLabel feedbackLabel;
309 | private javax.swing.JMenu fileMenu;
310 | private javax.swing.JButton guessButton;
311 | private javax.swing.JLabel guessLabel;
312 | private javax.swing.JTextField guessedWord;
313 | private javax.swing.JComboBox jComboBox1;
314 | private javax.swing.JLabel jLabel1;
315 | private javax.swing.JMenuBar mainMenu;
316 | private javax.swing.JPanel mainPanel;
317 | private javax.swing.JButton nextTrial;
318 | private javax.swing.JLabel scrambledLabel;
319 | private javax.swing.JTextField scrambledWord;
320 | // End of variables declaration//GEN-END:variables
321 |
322 | }
323 |
--------------------------------------------------------------------------------
/AnagramGame/src/com.toy.anagrams/classes/com/toy/anagrams/ui/Anagrams.java:
--------------------------------------------------------------------------------
1 |
2 | /* Anagram Game Application */
3 |
4 | package com.toy.anagrams.ui;
5 |
6 | import com.wordlibrary.WordLibrary;
7 | import java.awt.Dimension;
8 | import java.awt.Point;
9 | import java.awt.Toolkit;
10 | import java.util.List;
11 | import javax.swing.DefaultComboBoxModel;
12 | import javax.swing.JFrame;
13 | import javax.swing.SwingUtilities;
14 |
15 | /**
16 | * Main window of the Anagram Game application.
17 | */
18 | public class Anagrams extends JFrame {
19 |
20 | public static void main(String[] args) {
21 | /* Set the Nimbus look and feel */
22 | //
23 | /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
24 | * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
25 | */
26 | try {
27 | javax.swing.UIManager.LookAndFeelInfo[] installedLookAndFeels=javax.swing.UIManager.getInstalledLookAndFeels();
28 | for (int idx=0; idx
43 | //
44 |
45 | /* Create and display the form */
46 | SwingUtilities.invokeLater(new Runnable() {
47 | public void run() {
48 | new Anagrams().setVisible(true);
49 | }
50 | });
51 | }
52 |
53 | private int wordIdx = 0;
54 |
55 | private DefaultComboBoxModel dcbm = new DefaultComboBoxModel();
56 |
57 |
58 | // private WordLibrary wordLibrary;
59 |
60 | /** Creates new form Anagrams */
61 | public Anagrams() {
62 |
63 | List libraries = WordLibrary.openWordLibraries();
64 |
65 | initComponents();
66 |
67 | for (WordLibrary library : libraries) {
68 | dcbm.addElement(library);
69 | }
70 |
71 | category.setModel(dcbm);
72 |
73 | getRootPane().setDefaultButton(guessButton);
74 | pack();
75 | guessedWord.requestFocusInWindow();
76 | // Center in the screen
77 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
78 | Dimension frameSize = getSize();
79 | setLocation(new Point((screenSize.width - frameSize.width) / 2,
80 | (screenSize.height - frameSize.width) / 2));
81 |
82 | WordLibrary wordLibrary = (WordLibrary) dcbm.getSelectedItem();
83 | scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
84 | }
85 |
86 | /** This method is called from within the constructor to
87 | * initialize the form.
88 | * WARNING: Do NOT modify this code. The content of this method is
89 | * always regenerated by the Form Editor.
90 | */
91 | // //GEN-BEGIN:initComponents
92 | private void initComponents() {
93 | java.awt.GridBagConstraints gridBagConstraints;
94 |
95 | mainPanel = new javax.swing.JPanel();
96 | scrambledLabel = new javax.swing.JLabel();
97 | scrambledWord = new javax.swing.JTextField();
98 | guessLabel = new javax.swing.JLabel();
99 | guessedWord = new javax.swing.JTextField();
100 | feedbackLabel = new javax.swing.JLabel();
101 | buttonsPanel = new javax.swing.JPanel();
102 | guessButton = new javax.swing.JButton();
103 | nextTrial = new javax.swing.JButton();
104 | category = new javax.swing.JComboBox<>();
105 | jLabel1 = new javax.swing.JLabel();
106 | mainMenu = new javax.swing.JMenuBar();
107 | fileMenu = new javax.swing.JMenu();
108 | aboutMenuItem = new javax.swing.JMenuItem();
109 | exitMenuItem = new javax.swing.JMenuItem();
110 |
111 | setTitle("Anagrams");
112 | addWindowListener(new java.awt.event.WindowAdapter() {
113 | public void windowClosing(java.awt.event.WindowEvent evt) {
114 | exitForm(evt);
115 | }
116 | });
117 |
118 | mainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12));
119 | mainPanel.setMinimumSize(new java.awt.Dimension(297, 200));
120 | mainPanel.setLayout(new java.awt.GridBagLayout());
121 |
122 | scrambledLabel.setText("Scrambled Word:");
123 | gridBagConstraints = new java.awt.GridBagConstraints();
124 | gridBagConstraints.gridx = 0;
125 | gridBagConstraints.gridy = 1;
126 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
127 | gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
128 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
129 | mainPanel.add(scrambledLabel, gridBagConstraints);
130 |
131 | scrambledWord.setEditable(false);
132 | scrambledWord.setColumns(20);
133 | gridBagConstraints = new java.awt.GridBagConstraints();
134 | gridBagConstraints.gridx = 1;
135 | gridBagConstraints.gridy = 1;
136 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
137 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
138 | gridBagConstraints.weightx = 1.0;
139 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
140 | mainPanel.add(scrambledWord, gridBagConstraints);
141 |
142 | guessLabel.setDisplayedMnemonic('Y');
143 | guessLabel.setLabelFor(guessedWord);
144 | guessLabel.setText("Your Guess:");
145 | gridBagConstraints = new java.awt.GridBagConstraints();
146 | gridBagConstraints.gridx = 0;
147 | gridBagConstraints.gridy = 2;
148 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
149 | gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
150 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 6);
151 | mainPanel.add(guessLabel, gridBagConstraints);
152 |
153 | guessedWord.setColumns(20);
154 | gridBagConstraints = new java.awt.GridBagConstraints();
155 | gridBagConstraints.gridx = 1;
156 | gridBagConstraints.gridy = 2;
157 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
158 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
159 | gridBagConstraints.weightx = 1.0;
160 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
161 | mainPanel.add(guessedWord, gridBagConstraints);
162 |
163 | feedbackLabel.setText(" ");
164 | gridBagConstraints = new java.awt.GridBagConstraints();
165 | gridBagConstraints.gridx = 1;
166 | gridBagConstraints.gridy = 3;
167 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
168 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
169 | gridBagConstraints.weightx = 1.0;
170 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
171 | mainPanel.add(feedbackLabel, gridBagConstraints);
172 |
173 | buttonsPanel.setLayout(new java.awt.GridBagLayout());
174 |
175 | guessButton.setMnemonic('G');
176 | guessButton.setText("Guess");
177 | guessButton.setToolTipText("Guess the scrambled word.");
178 | guessButton.addActionListener(new java.awt.event.ActionListener() {
179 | public void actionPerformed(java.awt.event.ActionEvent evt) {
180 | guessedWordActionPerformed(evt);
181 | }
182 | });
183 | gridBagConstraints = new java.awt.GridBagConstraints();
184 | gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
185 | gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
186 | gridBagConstraints.weightx = 1.0;
187 | gridBagConstraints.weighty = 1.0;
188 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
189 | buttonsPanel.add(guessButton, gridBagConstraints);
190 |
191 | nextTrial.setMnemonic('N');
192 | nextTrial.setText("New Word");
193 | nextTrial.setToolTipText("Fetch a new word.");
194 | nextTrial.addActionListener(new java.awt.event.ActionListener() {
195 | public void actionPerformed(java.awt.event.ActionEvent evt) {
196 | nextTrialActionPerformed(evt);
197 | }
198 | });
199 | gridBagConstraints = new java.awt.GridBagConstraints();
200 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
201 | gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
202 | gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
203 | gridBagConstraints.weighty = 1.0;
204 | buttonsPanel.add(nextTrial, gridBagConstraints);
205 |
206 | gridBagConstraints = new java.awt.GridBagConstraints();
207 | gridBagConstraints.gridx = 0;
208 | gridBagConstraints.gridy = 4;
209 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
210 | gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
211 | gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
212 | gridBagConstraints.weighty = 1.0;
213 | mainPanel.add(buttonsPanel, gridBagConstraints);
214 |
215 | category.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
216 | category.addActionListener(new java.awt.event.ActionListener() {
217 | public void actionPerformed(java.awt.event.ActionEvent evt) {
218 | categoryActionPerformed(evt);
219 | }
220 | });
221 | gridBagConstraints = new java.awt.GridBagConstraints();
222 | gridBagConstraints.gridx = 1;
223 | gridBagConstraints.gridy = 0;
224 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
225 | gridBagConstraints.ipady = 6;
226 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
227 | mainPanel.add(category, gridBagConstraints);
228 |
229 | jLabel1.setText("Category:");
230 | gridBagConstraints = new java.awt.GridBagConstraints();
231 | gridBagConstraints.gridx = 0;
232 | gridBagConstraints.gridy = 0;
233 | gridBagConstraints.ipady = 6;
234 | gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
235 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
236 | mainPanel.add(jLabel1, gridBagConstraints);
237 |
238 | getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
239 |
240 | fileMenu.setMnemonic('F');
241 | fileMenu.setText("File");
242 |
243 | aboutMenuItem.setMnemonic('A');
244 | aboutMenuItem.setText("About");
245 | aboutMenuItem.setToolTipText("About");
246 | aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
247 | public void actionPerformed(java.awt.event.ActionEvent evt) {
248 | aboutMenuItemActionPerformed(evt);
249 | }
250 | });
251 | fileMenu.add(aboutMenuItem);
252 |
253 | exitMenuItem.setMnemonic('E');
254 | exitMenuItem.setText("Exit");
255 | exitMenuItem.setToolTipText("Quit Team, Quit!");
256 | exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
257 | public void actionPerformed(java.awt.event.ActionEvent evt) {
258 | exitMenuItemActionPerformed(evt);
259 | }
260 | });
261 | fileMenu.add(exitMenuItem);
262 |
263 | mainMenu.add(fileMenu);
264 |
265 | setJMenuBar(mainMenu);
266 | }// //GEN-END:initComponents
267 |
268 | private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
269 | new About(this).setVisible(true);
270 | }//GEN-LAST:event_aboutMenuItemActionPerformed
271 |
272 | private void nextTrialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextTrialActionPerformed
273 |
274 | WordLibrary wordLibrary = (WordLibrary) dcbm.getSelectedItem();
275 |
276 | scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
277 |
278 | wordIdx = (wordIdx + 1) % wordLibrary.getSize();
279 |
280 | feedbackLabel.setText(" ");
281 | scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
282 | guessedWord.setText("");
283 | getRootPane().setDefaultButton(guessButton);
284 |
285 | guessedWord.requestFocusInWindow();
286 | }//GEN-LAST:event_nextTrialActionPerformed
287 |
288 | private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
289 | System.exit(0);
290 | }//GEN-LAST:event_exitMenuItemActionPerformed
291 |
292 | private void guessedWordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guessedWordActionPerformed
293 | WordLibrary wordLibrary = (WordLibrary) dcbm.getSelectedItem();
294 |
295 | if (wordLibrary.isCorrect(wordIdx, guessedWord.getText())){
296 | feedbackLabel.setText("Correct! Try a new word!");
297 | getRootPane().setDefaultButton(nextTrial);
298 | } else {
299 | feedbackLabel.setText("Incorrect! Try again!");
300 | guessedWord.setText("");
301 | }
302 |
303 | guessedWord.requestFocusInWindow();
304 | }//GEN-LAST:event_guessedWordActionPerformed
305 |
306 | private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
307 | System.exit(0);
308 | }//GEN-LAST:event_exitForm
309 |
310 | private void categoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_categoryActionPerformed
311 | WordLibrary wordLibrary = (WordLibrary) dcbm.getSelectedItem();
312 | scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
313 | }//GEN-LAST:event_categoryActionPerformed
314 |
315 | // Variables declaration - do not modify//GEN-BEGIN:variables
316 | private javax.swing.JMenuItem aboutMenuItem;
317 | private javax.swing.JPanel buttonsPanel;
318 | private javax.swing.JComboBox category;
319 | private javax.swing.JMenuItem exitMenuItem;
320 | private javax.swing.JLabel feedbackLabel;
321 | private javax.swing.JMenu fileMenu;
322 | private javax.swing.JButton guessButton;
323 | private javax.swing.JLabel guessLabel;
324 | private javax.swing.JTextField guessedWord;
325 | private javax.swing.JLabel jLabel1;
326 | private javax.swing.JMenuBar mainMenu;
327 | private javax.swing.JPanel mainPanel;
328 | private javax.swing.JButton nextTrial;
329 | private javax.swing.JLabel scrambledLabel;
330 | private javax.swing.JTextField scrambledWord;
331 | // End of variables declaration//GEN-END:variables
332 |
333 | }
334 |
--------------------------------------------------------------------------------