-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductCreateView.java
More file actions
228 lines (190 loc) · 6.87 KB
/
Copy pathProductCreateView.java
File metadata and controls
228 lines (190 loc) · 6.87 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
package view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.math.BigDecimal;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import Utils.CharacterLimitFilter;
import Utils.ItemCategory;
import daoLibrary.ProductDAO;
import modelsLibrary.Product;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.text.AbstractDocument;
public class ProductCreateView extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private Point fixar;
private JLabel lblNewLabel;
private JLabel jlblNome;
private JLabel jlblDescricao;
private JLabel jlblPreco;
private JLabel jlblCategoria;
private JTextField jtxtNome;
private JTextField jtxtPreco;
private JTextArea jtxtAreaDescri;
private JComboBox<ItemCategory> jcmbCategoria;
private JButton jbtnExit;
private JButton btnCadatrarProduto;
private JButton jbtnListarProdu;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ProductCreateView frame = new ProductCreateView();
frame.setVisible(true);
frame.fixar = frame.getLocation();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ProductCreateView() {
InitComponents();
actionsButtons();
}
public void InitComponents() {
setType(Type.UTILITY);
setUndecorated(true);
getRootPane().setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.DARK_GRAY));
setSize(new Dimension(1300, 700));
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
jbtnExit = new JButton("Voltar");
jbtnExit.setBackground(new Color(192, 192, 192));
jbtnExit.setFocusPainted(false);
jbtnExit.setBorder(null);
jbtnExit.setBounds(1228, 666, 62, 23);
contentPane.add(jbtnExit);
lblNewLabel = new JLabel("Cadastro de Produtos");
lblNewLabel.setFont(new Font("Arial Black", Font.PLAIN, 30));
lblNewLabel.setBounds(463, 58, 368, 32);
lblNewLabel.setBackground(new Color(240, 240, 240));
contentPane.add(lblNewLabel);
jtxtNome = new JTextField();
jtxtNome.setFont(new Font("Arial", Font.PLAIN, 17));
jtxtNome.setBounds(547, 139, 267, 30);
contentPane.add(jtxtNome);
jtxtNome.setColumns(10);
jtxtPreco = new JTextField();
jtxtPreco.setFont(new Font("Arial", Font.PLAIN, 17));
jtxtPreco.setBounds(584, 369, 98, 30);
contentPane.add(jtxtPreco);
jtxtPreco.setColumns(10);
jtxtAreaDescri = new JTextArea();
jtxtAreaDescri.setFont(new Font("Arial", Font.PLAIN, 17));
jtxtAreaDescri.setBounds(478, 220, 336, 138);
jtxtAreaDescri.setLineWrap(true);
jtxtAreaDescri.setWrapStyleWord(true);
//Limitando o TextArea para 200 caracters
((AbstractDocument) jtxtAreaDescri.getDocument()).setDocumentFilter(new CharacterLimitFilter(200));
contentPane.add(jtxtAreaDescri);
//Trazendo os nomes das caegorias junto com o id para o combobox
jcmbCategoria = new JComboBox<>();
jcmbCategoria.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 17));
jcmbCategoria.setBounds(478, 441, 267, 30);
contentPane.add(jcmbCategoria);
ItemCategory.comboCategorias(jcmbCategoria);
jlblNome = new JLabel("Nome:");
jlblNome.setFont(new Font("Arial", Font.BOLD, 18));
jlblNome.setBounds(478, 145, 59, 14);
contentPane.add(jlblNome);
jlblDescricao = new JLabel("Descrição:");
jlblDescricao.setFont(new Font("Arial", Font.BOLD, 18));
jlblDescricao.setBounds(478, 195, 99, 14);
contentPane.add(jlblDescricao);
jlblPreco = new JLabel("Preço R$:");
jlblPreco.setFont(new Font("Arial", Font.BOLD, 18));
jlblPreco.setBounds(478, 376, 96, 14);
contentPane.add(jlblPreco);
jlblCategoria = new JLabel("Selecione a categoria:");
jlblCategoria.setFont(new Font("Arial", Font.BOLD, 18));
jlblCategoria.setBounds(475, 416, 207, 14);
contentPane.add(jlblCategoria);
btnCadatrarProduto = new JButton("Cadastrar Produto");
btnCadatrarProduto.setFont(new Font("Arial Black", Font.PLAIN, 25));
btnCadatrarProduto.setBackground(new Color(192, 192, 192));
btnCadatrarProduto.setBounds(478, 502, 279, 80);
btnCadatrarProduto.setFocusPainted(false);
btnCadatrarProduto.setBorder(null);
contentPane.add(btnCadatrarProduto);
jbtnListarProdu = new JButton("Listar Produtos");
jbtnListarProdu.setFont(new Font("Arial Black", Font.PLAIN, 25));
jbtnListarProdu.setBounds(811, 502, 279, 80);
contentPane.add(jbtnListarProdu);
addComponentListener(new ComponentAdapter() {
@Override
public void componentMoved(ComponentEvent e) {
if (fixar != null) {
setLocation(fixar);
}
}
});
}
public void actionsButtons() {
jbtnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainView tprincipal = new MainView();
tprincipal.setVisible(true);
dispose();
}
});
jbtnListarProdu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ProductListView tlProduto = new ProductListView();
tlProduto.setVisible(true);
dispose();
}
});
btnCadatrarProduto.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Product produto = new Product();
ProductDAO ddproduto = new ProductDAO();
produto.setNome(jtxtNome.getText());
produto.setDescricao(jtxtAreaDescri.getText());
if(jtxtNome.getText().trim().isEmpty()) {
JOptionPane.showMessageDialog(null, "Nome do Produto vazio");
return;
}
try {
BigDecimal preco = new BigDecimal(jtxtPreco.getText().trim());
produto.setPreco(preco);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Valor do Produto incorreto! Erro: "+ex.getMessage());
return;
}
ItemCategory categoriaSelecionada = (ItemCategory) jcmbCategoria.getSelectedItem();
if (categoriaSelecionada == null) {
JOptionPane.showMessageDialog(null, "Categoria não selecionada. Selecione uma categoria!");
return;
}
int idCategoria = categoriaSelecionada.getId();
produto.setIdCategoria(idCategoria);
ddproduto.insertProduto(produto);
JOptionPane.showMessageDialog(null, "Produto cadastrado com sucesso!");
jtxtNome.setText("");
jtxtAreaDescri.setText("");
jtxtPreco.setText("");
}
});
}
}