-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunFunFoodFinder.java
More file actions
448 lines (378 loc) · 16.6 KB
/
RunFunFoodFinder.java
File metadata and controls
448 lines (378 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// imports necessary libraries for Java swing
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.jar.JarEntry;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.html.HTMLEditorKit;
import java.net.URL;
public class RunFunFoodFinder implements Runnable {
public void randMealPlanButton(JPanel results_panel, JPanel display_panel, JPanel top_bar, FunFoodFinder f, JFrame frame) {
JButton random = new JButton("Fun Random Meal Plan");
random.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/**
* remove everything on results and display first
*/
Component[] results = results_panel.getComponents();
for (int i = 0; i < results.length; i++) {
results_panel.remove(i);
}
Component[] display = display_panel.getComponents();
for (int i = display.length-1; i >= 0; i--) {
display_panel.remove(i);
}
results_panel.revalidate();
results_panel.repaint();
display_panel.revalidate();
display_panel.repaint();
System.out.println("finished removing");
/**
* make the random meal plan
*/
Main.makeRandomMealPlan(f);
System.out.println("called makeRandom");
createListScroller(results_panel, display_panel, f, frame);
System.out.println("created list scroller");
SwingUtilities.updateComponentTreeUI(frame);
}
});
top_bar.add(random);
}
/**
* https://stackoverflow.com/questions/1090098/newline-in-jlabel
* @param results_panel
* @param display_panel
* @param f
* @param frame
*/
public void createListScroller(JPanel results_panel, JPanel display_panel, FunFoodFinder f, JFrame frame) {
String[] recTitles = f.getRecipes();
String[] recUrls = f.getRecipeUrls();
DefaultListModel<String> listMod = new DefaultListModel<String>(); // added <String> ???
for (int i = 0; i < recTitles.length; i++) {
listMod.addElement(recTitles[i]);
System.out.println("recipe title: " + recTitles[i] + "recipe url: " + recUrls[i]);
}
final JList recList = new JList(listMod);
recList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
recList.setLayoutOrientation(JList.VERTICAL);
recList.setSelectedIndex(-1);
// label of recipe title
final JLabel label = new JLabel("");
display_panel.add(label);
// label of sorting info
final JLabel sortLab = new JLabel("");
display_panel.add(sortLab);
// label of ingredients
final JLabel ingrLab = new JLabel("");
display_panel.add(ingrLab);
// label for image
final JLabel imageLab = new JLabel("");
display_panel.add(imageLab);
// label for steps
final JLabel stepsLab = new JLabel("");
JScrollPane stepsSc = new JScrollPane(stepsLab);
stepsSc.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//stepsSc.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
display_panel.add(stepsSc);
// action listener to register when value in list was changed
recList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (recList.getSelectedIndex() != -1) {
System.out.println("entering the value changed");
int index = recList.getSelectedIndex();
String title = recTitles[index];
//System.out.println(index);
label.setText(title); // label for recipe title
System.out.println("checkpoint 1");
/**
* Setting Sorting Label info
*/
String sortingInfo = f.getRecipeDataCell(title, 0) + " stars; " +
f.getRecipeDataCell(title, 1) + " minutes; " +
f.getRecipeDataCell(title, 2) + "calories";
sortLab.setText(sortingInfo);
System.out.println("checkpoint 2");
// Creating a parser object to get ingreds, steps, image
// System.out.println(recUrls[index]);
AllRecipesParser recipe = new AllRecipesParser(recUrls[index]);
String ingr = recipe.getIngreds();
String[] ingrList = ingr.split(" ; ");
recipe.getImageUrl(title);
ArrayList<String> steps = recipe.getDirections();
/**
* Setting the Ingredients label
* https://stackoverflow.com/questions/1090098/newline-in-jlabel
*/
String ingrText = "<html><br/>Ingredients<br/><br/>";
for (String s : ingrList) {
ingrText += s + "<br/>";
//System.out.println(ingrText);
}
ingrText += "</html>";
ingrLab.setText(ingrText);
/**
* Displaying the image
*/
String imageUrl = recipe.getImageUrl(title);
//"https://imagesvc.meredithcorp.io/v3/mm/image?url=https%3A%2F%2Fimages.media-allrecipes.com%2Fuserphotos%2F5252699.jpg";
try {
BufferedImage buffImage = ImageIO.read(new URL(imageUrl));
Image newImg = buffImage.getScaledInstance(100, 100, Image.SCALE_AREA_AVERAGING);
imageLab.setIcon(new ImageIcon(newImg));
} catch (IOException ex) {
//ex.printStackTrace();
}
/**
* Displaying the steps
*/
String stepsText = "<html><br/>Directions<br/><br/>";
for (String s : steps) {
stepsText += s + "<br/>";
//System.out.println(stepsText);
}
stepsText += "</html>";
stepsLab.setText(stepsText);
System.out.println("checkpoint 6");
//ImageIcon image = new ImageIcon(imageUrl);
//imageLab.setIcon(image);
//System.out.println(imageLab);
//createDisplayPane(display_panel, f, title);
SwingUtilities.updateComponentTreeUI(frame);
}
}
}
});
JScrollPane listSc = new JScrollPane(recList);
//listSc.setPreferredSize(new Dimension(200, 300));
results_panel.add(listSc);
System.out.println("checkpoint 8");
}
/**
*
* https://docs.oracle.com/javase/tutorial/uiswing/components/splitpane.html
*
* @param display_panel
* @param f
*/
public void createDisplayPane(JPanel display_panel, FunFoodFinder f, String title) {
final JLabel recLab = new JLabel(title);
display_panel.add(recLab);
}
/**
* checkboxes for dietary restrictions
* @param panel
* @param f
*/
public void createDietaryRestrictions(JPanel panel, FunFoodFinder f) {
final JLabel dietLab = new JLabel("Dietary Restrictions");
panel.add(dietLab);
String[] choices = new String[] {"Gluten", "Lactose", "Vegan", "Vegetarian", "Peanuts"};
// Radio Buttons for Meal Types
for (int i = 0; i < choices.length; i++) {
String restr = choices[i];
JCheckBox chBox = new JCheckBox(restr);
chBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (chBox.isSelected()) {
f.setDietR(restr);
//System.out.println(f.getDietR());
} else {
f.removeDietR(restr);
//System.out.println(f.getDietR());
}
}
});
panel.add(chBox);
}
}
/**
* Meal Type Filters in the Filter Panel
* @param panel
* @param f
*/
public void createSearchButton(JPanel panel, FunFoodFinder f, JPanel results_panel, JPanel display_panel, JFrame frame) {
final JButton search = new JButton("Search");
panel.add(search);
search.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Main.doFiltering(f);
createListScroller(results_panel, display_panel, f, frame);
SwingUtilities.updateComponentTreeUI(frame);
}
});
panel.add(search);
}
/**
* Meal Type Filters in the Filter Panel
* @param panel
* @param f
*/
public void createMTypeFilter(JPanel panel, FunFoodFinder f) {
final JLabel mLab = new JLabel("Choose Meal Type");
panel.add(mLab);
String[] choices = new String[] {"Breakfast", "Lunch", "Dinner", "Snacks", "Desserts"};
ButtonGroup mtypes = new ButtonGroup();
// Radio Buttons for Meal Types
for (int i = 0; i < choices.length; i++) {
String type = choices[i];
JRadioButton mButton = new JRadioButton(type);
mButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.setMtype(type);
//System.out.println(f.getMtype());
}
});
mtypes.add(mButton);
panel.add(mButton);
}
}
/**
* Enter Fields Text fields, and Enter button
* @param panel
* @param f
* @param label
*/
public void createEnterText(JPanel panel, FunFoodFinder f, String label, String des, boolean include) {
final JLabel textLabel = new JLabel(label);
panel.add(textLabel);
final JLabel description = new JLabel(des);
panel.add(description);
JTextField textBox = new JTextField(1);
JButton enterBut = new JButton("Enter");
enterBut.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (include) {
f.setIngred(textBox.getText());
} else {
f.setExclude(textBox.getText());
}
}
});
panel.add(textBox);
panel.add(enterBut);
}
public void createSortSelection(JPanel panel, FunFoodFinder f) {
final JLabel sLab = new JLabel("Sort By");
panel.add(sLab);
String[] choices = new String[] {"Ratings", "Time", "Calories"};
ButtonGroup sortTypes = new ButtonGroup();
// Radio Buttons for Meal Types
for (int i = 0; i < choices.length; i++) {
String type = choices[i];
JRadioButton button = new JRadioButton(type);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.setSortBy(type);
//System.out.println(f.getSortBy());
}
});
sortTypes.add(button);
panel.add(button);
}
}
public void run() {
JFrame frame = new JFrame("Fun Food Finder");
frame.setLocation(300, 300);
Border margin = BorderFactory.createEmptyBorder(10, 10, 10, 10);
Border blackline = BorderFactory.createLineBorder(Color.BLACK);
Border outer = BorderFactory.createCompoundBorder(margin, blackline);
Border border = BorderFactory.createCompoundBorder(outer, margin);
//GridLayout layout = new GridLayout(1, 3);
//BorderLayout layout2 = new BorderLayout();
BorderLayout layout = new BorderLayout();
frame.setLayout(layout);
// frame.setSize(800, 800);
FunFoodFinder f = new FunFoodFinder();
/**
* Filters Panel
*/
JPanel filters_panel = new JPanel();
filters_panel.setBorder(border);
filters_panel.setLayout(new BoxLayout(filters_panel, BoxLayout.Y_AXIS));
filters_panel.setPreferredSize(new Dimension(400, 700));
frame.add(filters_panel, BorderLayout.WEST);
// Label
final JLabel fLab = new JLabel("Filters");
filters_panel.add(fLab);
// Create components in Filters column
createMTypeFilter(filters_panel, f); // Radio Buttons for Meal Types
String ingrDes = "<html>Use a comma to separate ingredients <br/> (ex: egg, milk, ...). </html>";
createEnterText(filters_panel, f, "Enter Ingredients", ingrDes, true); // Ingredients Text Field
String excDes = "<html>Use a comma to separate ingredients to exclude <br/> (ex: nuts, fish, ...). </html>";
createEnterText(filters_panel, f, "Enter Allergies", excDes, false);
createDietaryRestrictions(filters_panel, f);
// Results panel
JPanel results_panel = new JPanel();
results_panel.setBorder(border);
results_panel.setLayout(new BoxLayout(results_panel, BoxLayout.Y_AXIS));
results_panel.setPreferredSize(new Dimension(400, 700));
frame.add(results_panel, BorderLayout.EAST);
createSortSelection(filters_panel, f);
// Display panel
JPanel display_panel = new JPanel();
display_panel.setBorder(border);
display_panel.setLayout(new BoxLayout(display_panel, BoxLayout.Y_AXIS));
display_panel.setPreferredSize(new Dimension(600, 700));
frame.add(display_panel, BorderLayout.CENTER);
//createDisplayPane(display_panel, f, "https://www.allrecipes.com/recipe/263880/bacon-okonomiyaki/");
createSearchButton(filters_panel, f, results_panel, display_panel, frame);
// list and panel display
//createListScroller(results_panel, f);
JPanel top_bar = new JPanel();
//top_bar.setBorder(border);
//top_bar.setLayout(new BoxLayout(top_bar, BoxLayout.Y_AXIS));
JButton reset = new JButton("Reset");
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Component[] results = results_panel.getComponents();
for (int i = 0; i < results.length; i++) {
results_panel.remove(i);
}
Component[] display = display_panel.getComponents();
for (int i = display.length-1; i >= 0; i--) {
display_panel.remove(i);
}
results_panel.revalidate();
results_panel.repaint();
display_panel.revalidate();
display_panel.repaint();
}
});
top_bar.setPreferredSize(new Dimension(800, 50));
top_bar.add(reset);
frame.add(top_bar, BorderLayout.NORTH);
randMealPlanButton(results_panel, display_panel, top_bar, f, frame);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable foodFinder = new RunFunFoodFinder();
SwingUtilities.invokeLater(foodFinder);
}
}