-
Notifications
You must be signed in to change notification settings - Fork 1
/
GUI.java
411 lines (354 loc) · 17.1 KB
/
GUI.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
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
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.*;
import java.io.File;
public class GUI implements ActionListener
{
private static final int TAM = 1000;
//Storage
int editorasI = 0;
Editora[] editoras = new Editora[TAM];
int publicacoesI = 0;
Publicacao[] publicacoes = new Publicacao[TAM];
JFrame window;
JTextField differentField;
JLabel differentFieldLabel;
JFileChooser fileChooser;
//Insert Publicacao
JTextField nome;
JTextField assunto;
JTextField edicao;
JList<String> editorasJList;
ButtonGroup option;
JRadioButton revista;
JRadioButton livro;
//Center Panel
JPanel centerPanel = new JPanel();
//Editora
JTextField nomeEditoraTexto;
JTextField websiteEditoraTexto;
JList<String> editorasVisiveis;
DefaultListModel<String> modelEditoras = new DefaultListModel<String>(); //Allows us to update list visually and directly
JLabel infoEditora;
//Saving
JTextField savePath;
//Publicacoes
JList<String> publicacoesVisiveis;
DefaultListModel<String> modelPublicacoes = new DefaultListModel<String>(); //Allows us to update list visually and directly
JLabel infoPublicacoes;
//Error
JLabel error;
//GUI Setup
public GUI()
{
//Setting frame
window = new JFrame("Gerenciador de Publicações");
window.pack();
window.setSize(new Dimension(900, 600));
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//Setting Main Panel
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
//Setting Title
JLabel title = new JLabel("Gerenciador de Publicações");
title.setHorizontalAlignment(SwingConstants.CENTER);
mainPanel.add(title, BorderLayout.NORTH);
//Setting Forms ----------------------------------------------------------------
Color defaultColor = new Color(220, 220, 220);
JPanel formsBox = new JPanel();
formsBox.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel forms = new JPanel();
Dimension minimumFormsSize = new Dimension(500, 0);
forms.setLayout(new GridBagLayout());
formsBox.setBackground(defaultColor);
formsBox.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, new Color(30, 30, 30)));
JLabel insertLabel = new JLabel("Criar Nova Publicação");
insertLabel.setHorizontalAlignment(SwingConstants.CENTER);
option = new ButtonGroup();
revista = new JRadioButton("Revista", true);
livro = new JRadioButton("Livro", false);
revista.addActionListener(this);
revista.setActionCommand("SRevista");
livro.addActionListener(this);
livro.setActionCommand("SLivro");
option.add(revista);
option.add(livro);
nome = new JTextField("Um nome");
assunto = new JTextField("Um assunto");
assunto.setMinimumSize(minimumFormsSize);
edicao = new JTextField("Uma edição");
editorasJList = new JList<String>(modelEditoras);
JButton submit = new JButton("Insert");
GridBagConstraints insertGrid = new GridBagConstraints();
insertGrid.gridy = 0; insertGrid.gridwidth = 2;
forms.add(insertLabel, insertGrid);
GridBagConstraints textFieldGrid = new GridBagConstraints();
textFieldGrid.fill = GridBagConstraints.HORIZONTAL;
GridBagConstraints optionConstraints = new GridBagConstraints();
optionConstraints.gridx = 0; optionConstraints.gridy = 2;
forms.add(revista, optionConstraints);
optionConstraints.gridx = 1;
forms.add(livro, optionConstraints);
textFieldGrid.gridx = 0; textFieldGrid.gridy = 1;
forms.add(new JLabel("Nome: "), textFieldGrid, 0);
textFieldGrid.gridx = 1;
forms.add(nome, textFieldGrid);
textFieldGrid.gridx = 0;
textFieldGrid.gridy = 3;
forms.add(new JLabel("Assunto: "), textFieldGrid, 0);
textFieldGrid.gridx = 1;
forms.add(assunto, textFieldGrid);
textFieldGrid.gridx = 0;
textFieldGrid.gridy = 4;
forms.add(new JLabel("Edição: "), textFieldGrid, 0);
textFieldGrid.gridx = 1;
forms.add(edicao, textFieldGrid);
textFieldGrid.gridx = 0;
textFieldGrid.gridy = 5;
forms.add(new JLabel("Editoras: "), textFieldGrid, 0);
textFieldGrid.gridx = 1;
forms.add(editorasJList, textFieldGrid);
textFieldGrid.gridx = 0;
textFieldGrid.gridy = 6;
differentFieldLabel= new JLabel("Peridiocidade: ");
forms.add(differentFieldLabel, textFieldGrid, 0);
differentField = new JTextField();
textFieldGrid.gridx = 1;
forms.add(differentField, textFieldGrid);
JPanel salvarPanel = new JPanel();
salvarPanel.setLayout(new BoxLayout(salvarPanel, 1));
JSeparator separatorS = new JSeparator(JSeparator.HORIZONTAL);
JLabel tituloSalvar = new JLabel("Salvar Em Arquivo");
tituloSalvar.setHorizontalAlignment(SwingConstants.CENTER);
JButton saveButton = new JButton("Salvar");
saveButton.addActionListener(this);
saveButton.setActionCommand("salvarPub");
JLabel caminhoSalvar = new JLabel("Caminho (sem aspas): ");
savePath = new JTextField();
salvarPanel.add(separatorS);
salvarPanel.add(tituloSalvar);
salvarPanel.add(caminhoSalvar);
salvarPanel.add(savePath);
salvarPanel.add(saveButton);
salvarPanel.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));
textFieldGrid.gridx = 0;
textFieldGrid.gridy = 7;
textFieldGrid.gridwidth = 2;
submit.setHorizontalAlignment(SwingConstants.CENTER);
submit.addActionListener(this);
submit.setActionCommand("Submit");
forms.add(submit, textFieldGrid);
textFieldGrid.gridx = 0;
textFieldGrid.gridy = 8;
forms.add(salvarPanel, textFieldGrid);
//Left Panel ---------------------------------------------------------
JPanel leftPanelBox = new JPanel();
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, 1));
leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
leftPanelBox.setBackground(defaultColor);
leftPanelBox.add(leftPanel);
//Setting Import (Panel) ----------------------------------------------
JPanel importPanel = new JPanel();
importPanel.setLayout(new BoxLayout(importPanel, 1));
JLabel importLabel = new JLabel("Importar Publicação");
importPanel.add(importLabel);
JLabel chooseFileLabel = new JLabel("Escolha um Arquivo");
JButton chooseFileButton = new JButton("Escolher");
JButton importFileButton = new JButton("Importar Publicação");
chooseFileButton.addActionListener(this);
chooseFileButton.setActionCommand("Import");
fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
importPanel.add(chooseFileLabel);
importPanel.add(chooseFileButton);
importFileButton.addActionListener(this);
importFileButton.setActionCommand("importPub");
importPanel.add(importFileButton);
importPanel.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, new Color(0, 0, 0)));
leftPanel.add(importPanel);
//importPanel.add(fileChooser);
//Creating Publisher --------------------------------------------------
JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);
JLabel tituloEditora = new JLabel("Criar Editora");
tituloEditora.setHorizontalAlignment(SwingConstants.CENTER);
tituloEditora.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 10));
importPanel.add(separator);
leftPanel.add(tituloEditora);
importPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 10));
JPanel editoraPanel = new JPanel();
editoraPanel.setLayout(new BoxLayout(editoraPanel, 1));
//nome, website
JLabel nomeEditoraLabel = new JLabel("Nome: ");
nomeEditoraTexto = new JTextField();
editoraPanel.add(nomeEditoraLabel); editoraPanel.add(nomeEditoraTexto);
JLabel websiteEditoraLabel = new JLabel("Website: ");
websiteEditoraTexto = new JTextField();
editoraPanel.add(websiteEditoraLabel); editoraPanel.add(websiteEditoraTexto);
JButton submitEditora = new JButton("Submit");
submitEditora.addActionListener(this);
submitEditora.setActionCommand("SEditora");
editoraPanel.add(submitEditora);
leftPanel.add(editoraPanel);
JPanel errorMsgs = new JPanel();
errorMsgs.setLayout(new BoxLayout(errorMsgs, 1));
JSeparator separator3 = new JSeparator(0);
JLabel errorTitle = new JLabel("Erros");
errorTitle.setHorizontalAlignment(SwingConstants.CENTER);
error = new JLabel("<html>Nenhum erro encontrado até o momento.</html>");
errorMsgs.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));
errorMsgs.add(separator3);
errorMsgs.add(errorTitle);
errorMsgs.add(error);
leftPanel.add(errorMsgs);
//Center Panels ------------------------------------------------------
JPanel centerPanelBox = new JPanel();
centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, 1));
//Editora
JPanel editorasCenterPanel = new JPanel();
editorasCenterPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 1));
JLabel editorasVisiveisLabel = new JLabel("Editoras");
editorasVisiveis = new JList<String>(modelEditoras);
infoEditora = new JLabel("Selecione uma Editora Para Obter Informações sobre Ela.");
editorasVisiveis.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event)
{
int editorasIndex = editorasVisiveis.getSelectedIndex();
if(editorasIndex != -1){
Editora valueLabel = editoras[editorasIndex];
infoEditora.setText("<html>Nome: " + valueLabel.nome + "<br/>Website: \n" + valueLabel.website + "<br/>Num. de Pub.: " + valueLabel.numPublicacoes + "</html>");}
}
});
editorasCenterPanel.add(editorasVisiveisLabel);
editorasCenterPanel.add(editorasVisiveis);
editorasCenterPanel.add(infoEditora);
editorasCenterPanel.setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, new Color(0, 0, 0)));
centerPanel.setBackground(defaultColor);
centerPanel.add(editorasCenterPanel);
centerPanelBox.add(centerPanel);
//Publicacoes
JPanel publicacoesCenterPanel = new JPanel();
publicacoesCenterPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 1));
JLabel publicacoesVisiveisLabel = new JLabel("Publicações");
publicacoesVisiveis = new JList<String>(modelPublicacoes);
infoPublicacoes = new JLabel("Selecione uma Publicação Para Obter Informações sobre Ela.");
publicacoesCenterPanel.add(publicacoesVisiveisLabel);
publicacoesCenterPanel.add(publicacoesVisiveis);
publicacoesCenterPanel.add(infoPublicacoes);
publicacoesVisiveis.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event)
{
try{
int pubIndex = publicacoesVisiveis.getSelectedIndex();
if(pubIndex!= -1 && publicacoes[pubIndex].getClass().equals(Livro.class)){
Livro valueLabel = (Livro) publicacoes[pubIndex];
String autoresLabel = "";
for(String umaString : valueLabel.autores)
{
autoresLabel += "<br/> - " + umaString;
}
infoPublicacoes.setText("<html>Nome: " + valueLabel.nome + "<br/>Edição: \n" + valueLabel.edicao + "<br/>Assunto: " + valueLabel.assunto + "<br/>Editora: " + valueLabel.editora.nome +"<br/>Autores: " + autoresLabel + "</html>");
}
else if(pubIndex != -1)
{
Revista valueLabel = (Revista) publicacoes[pubIndex];
infoPublicacoes.setText("<html>Nome: " + valueLabel.nome + "<br/>Edição: \n" + valueLabel.edicao + "<br/>Assunto: " + valueLabel.assunto + "<br/>Editora: " + valueLabel.editora.nome +"<br/>Peridiocidade: " + valueLabel.periodicidade + "</html>");
}
}catch(Exception o)
{
System.out.println("<!> Error displaying Publicacoes: " + o);
}
}
});
publicacoesCenterPanel.setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, new Color(0, 0, 0)));
centerPanel.add(publicacoesCenterPanel);
//Integrating Panels with Frame ---------------------------------------
formsBox.add(forms);
mainPanel.add(formsBox, BorderLayout.EAST);
mainPanel.add(leftPanelBox,BorderLayout.WEST);
mainPanel.add(centerPanel, BorderLayout.CENTER);
window.add(mainPanel);
//Visibility
window.setVisible(true);
}
//Events
public void actionPerformed(ActionEvent event)
{
switch(event.getActionCommand())
{
case "SRevista":
differentFieldLabel.setText("Peridiocidade: ");
break;
case "SLivro":
differentFieldLabel.setText("Autores: ");
break;
case "Submit":
Publicacao novo;
try{
int indexEditora = editorasJList.getSelectedIndex();
if(indexEditora != -1){
if(option.isSelected(revista.getModel())) novo = new Revista(nome.getText(), assunto.getText(), edicao.getText(), editoras[indexEditora], differentField.getText());
else{
String[] novaString = differentField.getText().split(",");
novo = new Livro(nome.getText(), assunto.getText(), edicao.getText(), editoras[editorasJList.getSelectedIndex()], novaString);
}
publicacoes[publicacoesI] = novo;
modelPublicacoes.add(publicacoesI, novo.nome);
publicacoesI++;
}}
catch(Exception o)
{
error.setText("<html><p>Error ao inserir nova publicação.</p> <p>Certifique-se que todos os campos estão preenchidos.</p></html>");
System.out.println("<!> Error with submission: " + o);
}
break;
case "Import":
fileChooser.showOpenDialog(window);
break;
case "importPub":
try{
File importMe = fileChooser.getSelectedFile();
if(importMe.exists()){
Publicacao pubImportada = Publicacao.importarPub(importMe.getAbsolutePath());
publicacoes[publicacoesI] = pubImportada;
modelPublicacoes.add(publicacoesI, pubImportada.nome);
publicacoesI++;
}
}
catch(Exception o)
{
error.setText("<html><p>Error ao importar publicação.</p> <p>Certifique-se que o arquivo escolhido é o correto.</p></html>");
System.out.println("<!> Error with importing file: " + o);
}
break;
case "salvarPub":
Publicacao novaPub;
if(editorasJList.getSelectedIndex() != -1){
if(option.isSelected(revista.getModel()))
{
novaPub = new Revista(nome.getText(), assunto.getText(), edicao.getText(), editoras[editorasJList.getSelectedIndex()], differentField.getText());
}
else
{
novaPub = new Livro(nome.getText(), assunto.getText(), edicao.getText(), editoras[editorasJList.getSelectedIndex()], differentField.getText().split(","));
}
novaPub.salvar(savePath.getText());
}
break;
case "SEditora":
String nomeEditora = nomeEditoraTexto.getText();
String websiteEditora = websiteEditoraTexto.getText();
if(nomeEditora.length() >= 1 && websiteEditora.length() >= 1)
{
editoras[editorasI] = new Editora(nomeEditora, websiteEditora);
modelEditoras.add(editorasI, nomeEditora);
editorasI++;
}
break;
}
}
}