-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathSearchFrame.java
177 lines (150 loc) · 5.2 KB
/
SearchFrame.java
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
package comp421;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.*;
//sql operation in button submit to be implemented --done
public class SearchFrame extends JFrame{
JButton submitButton = new JButton("Submit");
JButton backButton = new JButton("Back");
JTextField nameTF = new JTextField();
JTextField typeTF = new JTextField();
JTextField ModuleNumTF = new JTextField();
JTextField BrandTF = new JTextField();
SearchFrame frame = this;
int userid;
SQL sql;
MainFrame mainFrame;
public SearchFrame(int id,SQL sqlo,MainFrame mainFrame)
{
userid = id;
sql = sqlo;
this.mainFrame = mainFrame;
Panel titlePanel = new Panel();
Panel searchContentPanel = new Panel();
Panel buttonPanel = new Panel();
// title panel
titlePanel.add(new JLabel("Search for the product you want!"));
titlePanel.setSize(400,70);
this.add(titlePanel,BorderLayout.NORTH);
// search content panel
searchContentPanel.setLayout(new GridLayout(4,2,25,25));
searchContentPanel.setSize(400,200);
searchContentPanel.add(new JLabel("Name :"));
searchContentPanel.add(nameTF);
searchContentPanel.add(new JLabel("Type :"));
searchContentPanel.add(typeTF);
searchContentPanel.add(new JLabel("Module Number :"));
searchContentPanel.add(ModuleNumTF);
searchContentPanel.add(new JLabel("Brand :"));
searchContentPanel.add(BrandTF);
this.add(searchContentPanel, BorderLayout.CENTER);
// button panel
buttonPanel.setLayout(new GridLayout(1,2,25,25));
SearchListener listener = new SearchListener();
submitButton.addActionListener(listener);
backButton.addActionListener(listener);
buttonPanel.add(submitButton);
buttonPanel.add(backButton);
this.add(buttonPanel, BorderLayout.SOUTH);
}
private void setUpNewUI(int id, SQL sql,MainFrame mainFrame)
{
SearchFrame frame = new SearchFrame(id,sql,mainFrame);
frame.setTitle("C2C Online Electronic Shop");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(500, 400);
frame.setVisible(true);
// frame.pack();
}
static public void invoke(int id, SQL sql, MainFrame mainFrame)
{
SearchFrame frame = new SearchFrame(id,sql,mainFrame);
frame.setUpNewUI(id,sql,mainFrame);
}
public class SearchListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
// System.out.println("Back Button clicked ");
if(event.getSource() == submitButton)
{
// to be implemented
String name = nameTF.getText().trim();
String type = typeTF.getText().trim();
String modelNum = ModuleNumTF.getText().trim();
String brand = BrandTF.getText().trim();
String sqlCode = "";
/* select *
from product
where name = name and type = type and modelNumber = modelNumber and brand = brand;
*/
sqlCode += "select * from product ";
int notBlank = 0;
if((name.length())!=0 || ( type.length() != 0) ||(modelNum.length()!= 0)|| (brand.length()!= 0))
sqlCode += "where ";
if(name.length()!=0)
{
notBlank = 1;
sqlCode += "name = \'"+name+"\'";
}
if(type.length() != 0)
{
if(notBlank == 1)
sqlCode += " And ";
else
notBlank = 1;
sqlCode += "type = "+"\'"+type+"\'";
}
if(modelNum.length()!= 0)
{
if(notBlank == 1)
sqlCode += " And ";
else
notBlank = 1;
sqlCode += "modelNumber ="+"\'"+ modelNum+"\'";
}
if(brand.length()!= 0)
{
if(notBlank ==1)
sqlCode += " And ";
sqlCode += "brand = "+"\'"+brand+"\'";
}
sqlCode+= ";";
System.out.println(sqlCode);
java.sql.ResultSet rs = sql.QueryExchte(sqlCode);
int rowCount = 0;
try {
rs.last();
rowCount = rs.getRow();
rs.first();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
if(rowCount == 0)
JOptionPane.showMessageDialog(null, "No Result is found", "NO Result",JOptionPane.OK_OPTION);
else
try {
SaveToCartFrame.invoke(userid, sql, rs, frame);
frame.setVisible(false);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//open SaveToCartFrame with rs, sql, and userid
}
else if (event.getSource() == backButton)
{
//System.out.println("Back Button clicked ");
mainFrame.setVisible(true);
frame.dispose();
//close and invoke original one
//to be implemented
}
}
}
}