-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductDataPanel.java
More file actions
183 lines (160 loc) · 8.12 KB
/
ProductDataPanel.java
File metadata and controls
183 lines (160 loc) · 8.12 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
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
public class ProductDataPanel extends AppPanel {
private Object[][] productDetails = {{1,"Tomato-carton.jpg","Recycle",getContents("tomato-carton.txt","Recycle")},
{2,"Charger-broken.png","Recycle",getContents("charger-broken.txt","Recycle"),
"Donate",getContents("charger-broken.txt", "Donate")},
{2,"plasticBottle.jpg","Recycle",getContents("plasticBottle.txt","Recycle"),
"DIY",getContents("plasticBottle.txt", "DIY")},
{2,"Potato-peel.jpg","Recycle",getContents("potato-peel.txt","Recycle"),
"Recipe",getContents("potato-peel.txt", "Recipe")},
{3,"Trousers.jpg","Recycle",getContents("jeans.txt","Recycle"),
"Donate",getContents("jeans.txt", "Donate"),
"Fix",getContents("jeans.txt", "Fix")}};
private Object[] currentDetails;
private ArrayList<Boolean> dropDownStates = new ArrayList<>();
private String[] productDetailsString = {"tomato carton","charger broken","plastic bottle","potato peel","ripped jeans"};
private JPanel dropDownPanelMaster = new JPanel();
private boolean hasHyperlink = false;
private String hyperlink;
public ProductDataPanel(String product){
int index = indexOf(product,productDetailsString);
currentDetails = productDetails[index];
GridBagLayout gridBag = new GridBagLayout();
setLayout(gridBag);
GridBagConstraints constraint = new GridBagConstraints();
GridBagLayout gridBag2 = new GridBagLayout();
dropDownPanelMaster.setLayout(gridBag2);
GridBagConstraints constraint2 = new GridBagConstraints();
changeConstraintPadding(constraint, 2,2 );
changeConstraintPadding(constraint2, 2,2 );
JPanel topPanel = new JPanel();
topPanel.setPreferredSize(new Dimension(500,100));
topPanel.setBackground(appColour);
topPanel.setLayout(new BorderLayout());
JButton backButton = new JButton(getImage("leftBackButton.png"));
backButton.setPreferredSize(new Dimension(100,100));
backButton.setFont(textFont);
backButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MasterFrame.changePanel("StartingPanel");
SwingUtilities.updateComponentTreeUI(MasterFrame.masterFrame);
}
});
topPanel.add(backButton,BorderLayout.WEST);
JLabel logoLabel = new JLabel(getImage("LazyRecycleLogo.png"));
logoLabel.setPreferredSize(new Dimension(400,50));
logoLabel.setFont(headingTextFont);
topPanel.add(logoLabel,BorderLayout.CENTER);
changeConstraintPosition(constraint, 0, 0);
addToPane(topPanel, constraint, gridBag, this);
JPanel productPanel = new JPanel();
productPanel.setLayout(new BorderLayout());
productPanel.setPreferredSize(new Dimension(500, 50));
JLabel productLabel = new JLabel(product);
productLabel.setFont(headingTextFont);
JLabel productIdentifierLabel = new JLabel("Product: ");
productIdentifierLabel.setFont(headingTextFont);
productPanel.add(productIdentifierLabel,BorderLayout.WEST);
productPanel.add(productLabel,BorderLayout.EAST);
changeConstraintPosition(constraint, 0, 1);
addToPane(productPanel, constraint, gridBag, this);
JLabel image = new JLabel(getImage((String)currentDetails[1]));
changeConstraintPosition(constraint, 0, 2);
addToPane(image, constraint, gridBag, this);
for(int i=0;i<(int) currentDetails[0];i++){
dropDownStates.add(false);
}
for(int i=0;i<(int) productDetails[index][0];i++){
createDropDown((String) productDetails[index][2*(i+1)], (String) productDetails[index][2*(i+1)+1], i,constraint2,gridBag2);
}
changeConstraintPosition(constraint, 0, 3);
addToPane(dropDownPanelMaster, constraint, gridBag, this);
}
private void createDropDown(String name, String contents, int index,GridBagConstraints constraint2, GridBagLayout gridBag2){
JPanel dropDownPanel = new JPanel();
GridBagLayout gridBag = new GridBagLayout();
dropDownPanel.setLayout(gridBag);
GridBagConstraints constraint = new GridBagConstraints();
JLabel dropDownName = new JLabel(name);
dropDownName.setFont(textFont);
dropDownName.setBackground(appColour);
dropDownName.setOpaque(true);
dropDownName.setPreferredSize(new Dimension(500,30));
dropDownName.setBorder(border);
DefaultStyledDocument currentContents = new DefaultStyledDocument();
JTextPane dropDownContents = new JTextPane();
dropDownContents.setBorder(border);
dropDownContents.setText(contents);
dropDownContents.setEditable(false);
for(String line : contents.split("\n")){
if(line.startsWith("http")){
hasHyperlink = true;
hyperlink = line;
}
}
if(hasHyperlink){
JLabel jamieOliver = new JLabel(getImage("JamieOliver.png"));
changeConstraintPosition(constraint, 0, 2);
addToPane(jamieOliver, constraint, gridBag, dropDownPanel);
dropDownContents.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent event) {
try {
Desktop.getDesktop().browse(new URI(hyperlink));
}
catch (URISyntaxException e){}
catch(IOException e2){}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
});
}
dropDownName.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
if((boolean) dropDownStates.get(index)){
System.out.println("close");
dropDownPanel.remove(dropDownContents);
dropDownStates.set(index,false);
}
else{
System.out.println("open");
changeConstraintPosition(constraint, 0, 1);
addToPane(dropDownContents, constraint, gridBag, dropDownPanel);
dropDownStates.set(index,true);
}
SwingUtilities.updateComponentTreeUI(MasterFrame.masterFrame);
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
changeConstraintPosition(constraint, 0, 0);
addToPane(dropDownName, constraint, gridBag, dropDownPanel);
changeConstraintPosition(constraint2, 0, index);
addToPane(dropDownPanel, constraint2, gridBag2, dropDownPanelMaster);
}
}