-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainscreen.java
More file actions
891 lines (793 loc) · 39.1 KB
/
Copy pathmainscreen.java
File metadata and controls
891 lines (793 loc) · 39.1 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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
package com.cs2212;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.tree.TreeModel;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
/**
* The mainscreen class represents the home screen after the user logs in.
*/
public class mainscreen {
// Declare Instance Variables
private JFrame mainscreen;
private Main main;
static String searchText;
static JSONArray pois;
private Campus campus;
private Building currBuilding;
private Floor currFloor;
private HashMap<Integer, POI> poiMap;
boolean addPOI = false;
Component activeScrollComponent;
CheckboxTree POIList;
final int mainscreenWidth = 1200; // width of the JFrame
final int mainscreenHeight = 650; // height of the JFrame
JPanel panelTop;
JButton addPOIBtn = new JButton("Add POI");
final Color mediumGrey = new Color(202, 203, 204);
final Color lightGrey = new Color(232, 232, 232);
final Color darkGrey = new Color(88, 89, 89);
JLabel mapLbl;
JTabbedPane panelMap;
JScrollPane alumniScrollPane;
JScrollPane middlesexScrollPane;
JScrollPane healthScrollPane;
JComboBox floors;
JPanel panelSideBar;
JScrollPane resultScrollPane;
/**
* Returns the current floor.
* @return the Floor object of the current floor.
*/
public Floor getCurrFloor() {
return currFloor;
}
/**
* Set the current floor.
* @param floor the current floor to be set.
*/
public void setCurrFloor(Floor floor) {
currFloor = floor;
}
/**
* Create the UI for displaying the maps.
* @param building the name of the building.
* @param floor the integer representing a floor.
* @throws IOException
*/
public void createMap(String building, int floor) throws IOException {
try {
//Prepare map images
BufferedImage mapImage = ImageIO.read(getClass().getResource("images/" + building + "-" + floor + ".png"));
JLabel image = new JLabel(new ImageIcon(mapImage));
image.setBounds(0, 30, 970, 550); // Set size of the image
// Set up maps for each of the buildings
if (building.equals("Alumni Hall")) { // Alumni Hall
//Create a scroll pane to hold the image
alumniScrollPane = new JScrollPane(image);
//Set the scroll pane properties
alumniScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
alumniScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
alumniScrollPane.setPreferredSize(new Dimension(970, 550));
//Set scroll bar speed
alumniScrollPane.getVerticalScrollBar().setUnitIncrement(20);
alumniScrollPane.getHorizontalScrollBar().setUnitIncrement(20);
panelMap.add(building, alumniScrollPane);
} else if (building.equals("Middlesex College")) { // Middlesex College
middlesexScrollPane = new JScrollPane(image);
middlesexScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
middlesexScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
middlesexScrollPane.setPreferredSize(new Dimension(970, 550));
middlesexScrollPane.getVerticalScrollBar().setUnitIncrement(20);
middlesexScrollPane.getHorizontalScrollBar().setUnitIncrement(20);
panelMap.add(building, middlesexScrollPane);
} else if (building.equals("Health Sciences Building")) { // Health Sciences Building
healthScrollPane = new JScrollPane(image);
healthScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
healthScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
healthScrollPane.setPreferredSize(new Dimension(970, 550));
healthScrollPane.getVerticalScrollBar().setUnitIncrement(20);
healthScrollPane.getHorizontalScrollBar().setUnitIncrement(20);
panelMap.add(building, healthScrollPane);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Rebuild the side bar with new information.
* @param newTree TreeModel containing the POIs and layers for the specific map.
*/
private void repaintUI(TreeModel newTree) {
mainscreen.remove(panelSideBar);
generateSideBar(newTree);
floors.revalidate(); // Trigger a new layout pass
floors.repaint(); // Repaint the combobox
}
/**
* Change floor according to user selection.
* @param building Building object to obtain number of floors
*/
private void changeFloor(Building building) {
// Create dropdown to switch floors
floors = new JComboBox(building.getFloorsArray());
JLabel floorsLabel = new JLabel("Floor:");
floorsLabel.setBounds(870, 3, 50, 24);
floors.setBounds(915, 3, 125, 24);
panelTop.add(floorsLabel);
panelTop.add(floors); // add combo box to the panel
// Create a listener to grab user selection
floors.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
try {
if (event.getStateChange() == ItemEvent.SELECTED) {
activeScrollComponent = panelMap.getSelectedComponent(); //ensures the active panel for drawing
Integer floorNum = (int) event.getItem();
// Consider that HSB does not have a basement
if (currBuilding.getName().equals("Health Sciences Building")) {
floorNum -= 1;
}
Floor newFloor = building.getArray().get(floorNum);
// Change the floor image and set new current floor to reflect user selection
changeFloorImage(building.getName(), floorNum);
setCurrFloor(newFloor);
TreeModel newTree = main.makeTree(newFloor);
repaintUI(newTree);
drawPOIs();//drawing the poits
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
/**
* Change the map displayed according to floor.
* @param building name of the building to switch to
* @param floor integer representing the floor to switch to
* @throws IOException
*/
private void changeFloorImage(String building, int floor) throws IOException {
try {
BufferedImage mapImage = ImageIO.read(getClass().getResource("images/" + building + "-" + floor + ".png"));
mapLbl = new JLabel(new ImageIcon(mapImage));
mapLbl.setBounds(0, 30, 970, 550);
//Create a scroll pane to hold the image
if (building.equals("Alumni Hall")) {
alumniScrollPane.setViewportView(mapLbl);
} else if (building.equals("Middlesex College")) {
middlesexScrollPane.setViewportView(mapLbl);
} else if (building.equals("Health Sciences Building")) {
healthScrollPane.setViewportView(mapLbl);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Displays the POIs on the map according to list of POIs to draw.
*/
private void drawPOIs() {
try {
activeScrollComponent = panelMap.getSelectedComponent();
JScrollPane activeScrollPane = (JScrollPane) activeScrollComponent;
BufferedImage mapImage = ImageIO.read(getClass().getResource(currFloor.getImage()));
// Create a label to hold the Alumni Hall image and set its bounds
mapLbl = new JLabel(new ImageIcon(mapImage));
mapLbl.setBounds(0, 0, mapImage.getWidth(), mapImage.getHeight());
// Create a layered pane to layer the components
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(mapImage.getWidth(), mapImage.getHeight()));
layeredPane.add(mapLbl, JLayeredPane.DEFAULT_LAYER); // Add the Alumni Hall image label to the bottom layer
ArrayList<POI> poisToDraw = POIList.getPOIDraw();
//For every poi that needs be draw; draw it. if empty/no pois ignore
if (poisToDraw != null) {
for (POI poi : poisToDraw) {
poi.setActive();
layeredPane.add(poi.getLbl(), JLayeredPane.PALETTE_LAYER);
}
}
if (poisToDraw.size()> 0) {
POI lastPOI = poisToDraw.get(poisToDraw.size()-1);
JViewport viewport = activeScrollPane.getViewport();
viewport.setView(layeredPane);
viewport.setViewPosition(new Point((int)lastPOI.getXCoord()-50, (int)lastPOI.getYCoord()-50));
activeScrollPane.setViewport(viewport);
activeScrollPane.revalidate(); // Trigger a new layout pass
activeScrollPane.repaint(); // Repaint the JLayeredPane
} else {
// Get the current viewport
JViewport viewport = activeScrollPane.getViewport();
// Keep the same view position
Point viewPosition = viewport.getViewPosition();
// Update the viewport's view component with the new content
viewport.setView(layeredPane);
// Set the view position to the same location as before
viewport.setViewPosition(viewPosition);
activeScrollPane.setViewport(viewport);
activeScrollPane.revalidate(); // Trigger a new layout pass
activeScrollPane.repaint(); // Repaint the JLayeredPane
}
} catch (IOException e) {
}
}
/**
*
* @param main
* @param campus
* @param poiMap
* @throws IOException
*/
public mainscreen(Main main, Campus campus, HashMap<Integer, POI> poiMap) throws IOException {
this.main = main;
this.campus = campus;
panelMap = new JTabbedPane();
panelTop = new JPanel();
this.poiMap = poiMap;
for (Object poi : poiMap.values()) {
POI currPOI = (POI) poi;
currPOI.setMainframe(this);
}
main.setMainframe(this);
//Parse POI json
try {
//Parse and print out each of the different results
JSONParser parser = new JSONParser();
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("poi.json");
InputStreamReader poiReader = new InputStreamReader(inputStream);
JSONObject jsonObj = (JSONObject)parser.parse(poiReader);
pois = (JSONArray) jsonObj.get("pois");
} catch (Exception e) {
e.printStackTrace();
}
// Create Jframe for the entire window
mainscreen = new JFrame("Map of Western University");
mainscreen.setLayout(null); // ENABLES PANELS TO BE ADDED WITHIN ONE ANOTHER
mainscreen.setSize(mainscreenWidth, mainscreenHeight); // make JFrame full screen
mainscreen.setLocationRelativeTo(null); // center JFrame in the middle of the screen
mainscreen.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
mainscreen.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//popup that warns the user that unsaved progress will be deleted
int result = JOptionPane.showConfirmDialog(mainscreen, "Are you sure you want to exit? Any unsaved work will be lost!", "Exit Program", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
e.getWindow().dispose();
main.logOut();
}
}
});
// Create building maps
createMap("Alumni Hall", 0);
createMap("Middlesex College", 0);
createMap("Health Sciences Building", 1);
// Sets current building as Alumni Hall
currBuilding = (Building) campus.getBuildings().get(0);
changeFloor(currBuilding);
setCurrFloor(currBuilding.getArray().get(0));
final MouseListener mouseListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
};
// Get the currently selected component
activeScrollComponent = panelMap.getSelectedComponent();
panelMap.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
activeScrollComponent = panelMap.getSelectedComponent();
// Set building from selected pane
currBuilding = (Building) campus.getBuildings().get(tabbedPane.getSelectedIndex());
activeScrollComponent = tabbedPane.getSelectedComponent();
// Get the currently selected component
activeScrollComponent = tabbedPane.getSelectedComponent();
// Remove the MouseListener from the previously selected component
Component[] components = tabbedPane.getComponents();
for (Component component : components) {
if (component != activeScrollComponent && component instanceof JComponent) {
((JComponent) component).removeMouseListener(mouseListener);
}
}
panelTop.remove(floors);
changeFloor(currBuilding);
Floor newFloor = currBuilding.getArray().get(0);
setCurrFloor(newFloor);
TreeModel newTree = main.makeTree(newFloor);
repaintUI(newTree);
drawPOIs(); //drawing the poits
}
});
activeScrollComponent.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Get the mouse click location
drawPOIs();
if (addPOI == true) {
mapLbl.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent ex) {
// Get the mouse click location
if (addPOI == true) {
newPoiAdd(ex.getX()-13, ex.getY()-40, floors);
addPOIBtn.setText("Add POI");
addPOI = false; //Turn off the clicking
}
}
});
}
}
});
if (addPOI == true) {
mapLbl.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent ex) {
// Get the mouse click location
if (addPOI == true) {
newPoiAdd(ex.getX()-13, ex.getY()-40, floors);
addPOIBtn.setText("Add POI");
addPOI = false; //Turn off the clicking
}
}
});
}
// JPanel for the top bar, that includes Search
panelTop.setLayout(null);
panelTop.setBackground(mediumGrey);
panelTop.setBounds(0, 0, 1200, 30);
// JPanel for the map
panelMap.setBackground(Color.white);
panelMap.setBounds(0, 30, 970, 620);
// JPanel behind the tabbed panels
JPanel panelCenter = new JPanel(); // what was changed
panelCenter.setBackground(Color.white);
panelCenter.setBounds(0, 30, 970, 620);
panelCenter.add(panelMap); // what was changed
mainscreen.add(panelCenter); // add map
// Create search bar
JTextField searchField = new JTextField(20);
searchField.setBounds(10, 3, 650, 24);
// Create search button
JButton searchButton = new JButton("Search");
searchButton.setBounds(685, 3, 100, 25);
//add search bar components into top panel
panelTop.add(searchField);
panelTop.add(searchButton);
//panelTop.add(closeResults);
//event listener that takes in text the user searches
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchText = searchField.getText();
//create String list to contain search results
DefaultListModel<POI> searchResultsList = main.search(searchText);
JList resultJList = new JList<>(searchResultsList);
if (searchResultsList.isEmpty()) {
JLabel emptyResult = new JLabel("No results were found. Try again!");
emptyResult.setHorizontalAlignment(JLabel.CENTER);
JOptionPane.showMessageDialog(null, emptyResult, "Search Results", JOptionPane.PLAIN_MESSAGE);
} else {
//add selection listener to the resultJList instance
resultJList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
//get the selected item
POI selectedItem = (POI) resultJList.getSelectedValue();
String layerId = selectedItem.getLayerId();
char building = layerId.toLowerCase().charAt(0);
Integer floor = Character.getNumericValue(layerId.charAt(1));
// Determine which building the selected item is from
boolean changedBuilding;
if (building != currBuilding.getName().charAt(0)) {
if (building == 'a') {
currBuilding = (Building) campus.getBuildings().get(0);
panelMap.setSelectedComponent(alumniScrollPane);
} else if (building == 'm') {
currBuilding = (Building) campus.getBuildings().get(1);
panelMap.setSelectedComponent(middlesexScrollPane);
} else if (building == 'h') {
currBuilding = (Building) campus.getBuildings().get(2);
floor -= 1;
panelMap.setSelectedComponent(healthScrollPane);
}
} else {
changedBuilding = false;
}
// Change the floor image and set new current floor to reflect user selection
try {
panelTop.remove(floors);
changeFloor(currBuilding);
Floor newFloor = currBuilding.getArray().get(floor);
setCurrFloor(newFloor);
TreeModel newTree = main.makeTree(newFloor);
repaintUI(newTree);
ArrayList<POI> draw = new ArrayList<>();
draw.add(selectedItem);
POIList.setPOIDraw(draw);
// Set POI to active
selectedItem.isActive();
drawPOIs(); // Draw the seearched POI
throw new IOException();
} catch (IOException error) {
error.printStackTrace();
}
}
}
});
// Create scroll panes for results
resultScrollPane = new JScrollPane(resultJList);
resultScrollPane.setPreferredSize(new Dimension(350,200));
JPanel resultsPane = new JPanel();
resultsPane.add(resultScrollPane);
//add search results components into top panel
JOptionPane searchResultsPanel = new JOptionPane(resultsPane);
JOptionPane.showMessageDialog(null, resultsPane, "Search Results", JOptionPane.PLAIN_MESSAGE);
}
}
});
// Help icon for users to click
JButton helpIcon = new JButton("Help");
helpIcon.setBounds(1050, 3, 125, 24);
panelTop.add(helpIcon);
// Event listener so that when the help icon is clicked the PDF is opened
helpIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
URL pdfUrl = getClass().getClassLoader().getResource("resources/CS2212_Help_Document.pdf");
try {
File pdfFile = new File(pdfUrl.getFile());
if (pdfFile.exists()) {
Desktop.getDesktop().open(pdfFile);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
// add all JPanels to the JFrame
TreeModel defaultTree = main.makeTree(currFloor);
generateSideBar(defaultTree);
mainscreen.add(panelTop); // add top bar
mainscreen.setResizable(false);
mainscreen.setVisible(true);
}
/**
* Generate sidebar with POIs according to the map
* @param layers TreeModel with the different POI categories and the POIs themselves
*/
private void generateSideBar(TreeModel layers) {
// JPanel for the side bar
panelSideBar = new JPanel();
panelSideBar.setLayout(null);
panelSideBar.setBackground(Color.white);
panelSideBar.setBounds(970, 30, 230, 580);
// JPanel for the weather in the side bar
JPanel panelWeather = new JPanel();
panelWeather.setBackground(lightGrey);
panelWeather.setBounds(0, 0, 230, 50);
panelSideBar.add(panelWeather);
Weather newWeather = new Weather("London");
if (newWeather.getCurrCondition().equals("No internet.")){
JLabel weatherString = new JLabel ("No Internet Connection.");
panelWeather.add(weatherString);
} else {
JLabel weatherString = new JLabel(newWeather.getCity() + ": " + newWeather.getCurrWeather() + "°C " + newWeather.getCurrCondition());
panelWeather.add(weatherString);
}
// JPanel for the POI Title and Button
JPanel panelPOITitle = new JPanel();
panelPOITitle.setLayout(null);
panelPOITitle.setBackground(darkGrey);
panelPOITitle.setBounds(0, 50, 230, 30);
panelSideBar.add(panelPOITitle);
// Turn TreeModel into checkbox list of POIs
POIList = new CheckboxTree();
POIList.setShowsRootHandles(true);
POIList.setRootVisible(false);
POIList.setModel(layers);
JScrollPane panelPOIScroll = new JScrollPane(POIList); // add tree to scroll pane
panelPOIScroll.setBackground(Color.white);
panelPOIScroll.setBounds(0, 80, 230, 500);
panelPOIScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
panelPOIScroll.getVerticalScrollBar().setUnitIncrement(20);
panelSideBar.add(panelPOIScroll); // add scroll pane to side bar
// Label for sidebar
JLabel POITitle = new JLabel("Points of Interest");
POITitle.setBounds(5, 5, 200, 20);
POITitle.setFont(new Font("Arial", Font.BOLD, 12));
POITitle.setBackground(darkGrey);
POITitle.setForeground(Color.white);
panelPOITitle.add(POITitle);
// Add a button to Add POIs
addPOIBtn = new JButton("Add POI");
addPOIBtn.setBounds(120, 5, 90, 20);
panelPOITitle.add(addPOIBtn);
//Listener for when user selects checkbox to draw the POI marker
POIList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
drawPOIs();
}
});
// Button action listener to toggle on the poi adding mode
addPOIBtn.addActionListener(e -> {
addPOI = !addPOI; // Toggle the boolean variable
drawPOIs();
if (addPOI == true) {
addPOIBtn.setText("Click Map");
} else {
addPOIBtn.setText("Add POI");
}
if (mapLbl != null) {
mapLbl.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Get the mouse click location
if (addPOI == true) {
newPoiAdd(e.getX()-13, e.getY()-40, floors);
addPOIBtn.setText("Add POI");
addPOI = false; //Turn off the clicking
}
}
});
}
});
if (mapLbl != null) {
mapLbl.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Get the mouse click location
if (addPOI == true) {
newPoiAdd(e.getX()-13, e.getY()-40, floors);
addPOIBtn.setText("Add POI");
addPOI = false; //Turn off the clicking
}
}
});
}
mainscreen.add(panelSideBar); // add side bar
}
/**
* Create and display pop-up menu that shows when user clicks on POI.
* @param xCoord x coordinate of where the user clicked on.
* @param yCoord y coordinate of where the user clicked on.
* @param floorCB JComboBox containing the floors of the building.
*/
private void newPoiAdd(long xCoord, long yCoord, JComboBox floorCB) {
boolean isDeveloper = main.isDeveloper();
JTextField layerField = new JTextField();
char layerType = 'a';
// Create a panel with a grid layout for the input boxes
JPanel panel = new JPanel(new GridLayout(0, 2));
// Add labels and text fields for point name, room number, and description
panel.add(new JLabel("Point name:"));
JTextField pointNameField = new JTextField();
panel.add(pointNameField);
panel.add(new JLabel("Room number:"));
JTextField roomNumberField = new JTextField();
panel.add(roomNumberField);
panel.add(new JLabel("Description:"));
JTextField descriptionField = new JTextField();
panel.add(descriptionField);
//if dev, then add this component to panel
if (isDeveloper) {
panel.add(new JLabel("Layer:"));
panel.add(layerField);
}
// Show the input dialog with the panel as the message
int result = JOptionPane.showConfirmDialog(null, panel, "Enter point information", JOptionPane.OK_CANCEL_OPTION);
// Check if the user clicked OK and get the input values
if (result == JOptionPane.OK_OPTION) {
String name = pointNameField.getText();
String roomNum = roomNumberField.getText();
String description = descriptionField.getText();
// If the user is a developer, display layer
if (isDeveloper) {
String layer = (String)layerField.getText();
if (layer.equals("Washroom")) {
layerType = 'w';
} else if (layer.equals("Classroom")) {
layerType = 'c';
} else if (layer.equals("Gen Lab")) {
layerType = 'g';
} else if (layer.equals("CS Specific")) {
layerType = 's';
} else if (layer.equals("Resturaunts")) {
layerType = 'r';
} else if (layer.equals("Exit/Entry point")) {
layerType = 'e';
} else if (layer.equals("Navigation")) {
layerType = 'n';
}
}
Integer selectedFloor = (Integer) floorCB.getSelectedItem();
// Make all fields mandatory
if (result == JOptionPane.OK_OPTION && !pointNameField.getText().isEmpty() && !roomNumberField.getText().isEmpty() && !descriptionField.getText().isEmpty()) {
// Create POI
main.addPOI(currBuilding.getName().toLowerCase().charAt(0) + Integer.toString(selectedFloor) + "u", xCoord, yCoord, roomNum, name, description);
TreeModel newTree = main.makeTree(currFloor);
repaintUI(newTree);
drawPOIs();
// Display message
JOptionPane.showMessageDialog(null, "Successfully added");
} else {
JOptionPane.showMessageDialog(null, "Unsuccessful No POI Added");
}
} else {
JOptionPane.showMessageDialog(null, "Unsuccessful No POI Added");
}
}
/**
* Display the POI on the map
* @param poiID ID of the POI to be displayed
*/
public void displayPOI(int poiID) {
String favOption = ""; // Text variable to change between favourite and unfavourite
String[] buttons = {favOption, "Edit", "Delete"};
boolean isDev = main.isDeveloper(); // Changes to true if user is a developer
//Get the POI object
POI poiToDisplay = (POI) poiMap.get(poiID);
// Create pop up panel
JPanel POIPopUp = new JPanel(new GridLayout(6, 0));
// Display Name
POIPopUp.add(new JLabel("Name:"));
JLabel POIName = new JLabel(poiToDisplay.getName());
POIPopUp.add(POIName);
// Display Room Number
POIPopUp.add(new JLabel("Room Number:"));
JLabel POIRoom = new JLabel(poiToDisplay.getRoomNum());
POIPopUp.add(POIRoom);
// Display Description
POIPopUp.add(new JLabel("Description:"));
JLabel POIDescription = new JLabel(poiToDisplay.getDescription());
POIPopUp.add(POIDescription);
if (isDev) {
//Add two additional buttons
JButton devEdit = new JButton("Edit");
POIPopUp.add(devEdit);
JButton devDelete = new JButton("Delete");
POIPopUp.add(devDelete);
// Event listener for edit
devEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Create a panel with a grid layout for the input boxes
JPanel panel = new JPanel(new GridLayout(0, 2));
// Add labels and text fields for point name, room number, and description
panel.add(new JLabel("Point name:"));
JTextField pointNameField = new JTextField();
panel.add(pointNameField);
panel.add(new JLabel("Room number:"));
JTextField roomNumberField = new JTextField();
panel.add(roomNumberField);
panel.add(new JLabel("Description:"));
JTextField descriptionField = new JTextField();
panel.add(descriptionField);
// Show the input dialog with the panel as the message
int result = JOptionPane.showConfirmDialog(null, panel, "Enter point information", JOptionPane.OK_CANCEL_OPTION);
// Check if the user clicked OK and get the input values
if (result == JOptionPane.OK_OPTION) {
JOptionPane.showMessageDialog(null, "Successfully Edited");
String name = pointNameField.getText();
String roomNum = roomNumberField.getText();
String description = descriptionField.getText();
// Ensure that all fields are filled
if (result == JOptionPane.OK_OPTION && !pointNameField.getText().isEmpty() && !roomNumberField.getText().isEmpty() && !descriptionField.getText().isEmpty()) {
main.editPOIInfo(poiToDisplay, name, roomNum, description);
mainscreen.remove(POIPopUp);
mainscreen.invalidate();
mainscreen.validate();
mainscreen.repaint();
displayPOI(poiID);
}
} else {
JOptionPane.showMessageDialog(null, "Unsuccessful: No POI Edited");
}
}
});
// Event listener for deleting a POI
devDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Boolean success = main.deletePOI(poiToDisplay);
if (success) {
JOptionPane.showMessageDialog(null, "Successfully Deleted");
TreeModel newTree = main.makeTree(currFloor);
repaintUI(newTree);
} else {
JOptionPane.showMessageDialog(null, "Unsuccessful: No POI Deleted");
}
}
});
} else {
HashSet<POI> createdPOIs = main.getCreated();
if (createdPOIs.contains(poiMap.get(poiID))) {
//Add two additional buttons
JButton userEdit = new JButton("Edit");
POIPopUp.add(userEdit);
JButton userDelete = new JButton("Delete");
POIPopUp.add(userDelete);
//Event listener for edit
userEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Create a panel with a grid layout for the input boxes
JPanel panel = new JPanel(new GridLayout(0, 2));
// Add labels and text fields for point name, room number, and description
panel.add(new JLabel("Point name:"));
JTextField pointNameField = new JTextField();
panel.add(pointNameField);
panel.add(new JLabel("Room number:"));
JTextField roomNumberField = new JTextField();
panel.add(roomNumberField);
panel.add(new JLabel("Description:"));
JTextField descriptionField = new JTextField();
panel.add(descriptionField);
// Show the input dialog with the panel as the message
int result = JOptionPane.showConfirmDialog(null, panel, "Enter point information", JOptionPane.OK_CANCEL_OPTION);
// Check if the user clicked OK and get the input values
if (result == JOptionPane.OK_OPTION) {
JOptionPane.showMessageDialog(null, "Successfully Edited");
String name = pointNameField.getText();
String roomNum = roomNumberField.getText();
String description = descriptionField.getText();
// Ensure all fields are filled
if (result == JOptionPane.OK_OPTION && !pointNameField.getText().isEmpty() && !roomNumberField.getText().isEmpty() && !descriptionField.getText().isEmpty()) {
main.editPOIInfo(poiToDisplay, name, roomNum, description);
mainscreen.remove(POIPopUp);
mainscreen.invalidate();
mainscreen.validate();
mainscreen.repaint();
displayPOI(poiID);
}
} else{
JOptionPane.showMessageDialog(null, "Unsuccessful: No POI Edited");
}
}
});
// Event listener for delete
userDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Boolean success = main.deletePOI(poiToDisplay);
if (success) {
JOptionPane.showMessageDialog(null, "Successfully Deleted");
TreeModel newTree = main.makeTree(currFloor);
repaintUI(newTree);
drawPOIs();
} else {
JOptionPane.showMessageDialog(null, "Unsuccessful: No POI Deleted");
}
}
});
}
// Display checkbox for favourite
JCheckBox isFavourite = new JCheckBox("Favourite");
POIPopUp.add(isFavourite);
if (poiMap.get(poiID).getFavourite()) {
isFavourite.setSelected (true);
}
// Event listener for the favourite option
isFavourite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (poiMap.get(poiID).getFavourite()) {
main.removeFavourite(poiID);
JOptionPane.showMessageDialog(null, "Successfully removed from favourites.");
} else {
main.addFavourite(poiID);
JOptionPane.showMessageDialog(null, "Successfully added to favourites.");
}
TreeModel newTree = main.makeTree(currFloor);
repaintUI(newTree);
drawPOIs();
}
});
}
JOptionPane.showConfirmDialog(null, POIPopUp, "Information", JOptionPane.DEFAULT_OPTION);
}
}