-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.java
619 lines (590 loc) · 22.9 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
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
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import java.util.*;
/*! GUI CLASS
* All the creation and the initialization (Components and the table )
* is being done here.
*/
public class GUI extends JFrame
{
private static final long serialVersionUID = 1L;
protected List<Publication> pubList;
protected Map<Integer, List<Publication> > pubMap;
protected Set<Author> authors;
protected Main control;
protected JComboBox queryChoice,searchBy;
protected JCheckBox sortByRelevance,sortByYear;
protected JScrollPane pane;
protected JTextArea queryResults,noPubl,nameTitleTags,sinceYear,customRangeTo,customRangeFrom;
protected JButton next,reset,search;
protected JTable jt;
protected JLabel nameTitleTagsLabel,PubLabel, rangeLabel, sinceYearLabel, searchLabel, queryLabel, errorLabel, resultNumberLabel;
protected int currentIndex;
protected Iterator<Author> authorIterator;
String data[][] = { {"","","","","","","",""},{"","","","","","","",""},{"","","","","","","",""},{"","","","","","","",""},{"","","","","","","",""},
{"","","","","","","",""},{"","","","","","","",""},{"","","","","","","",""},{"","","","","","","",""},{"","","","","","","",""},
{"","","","","","","",""},{"","","","","","","",""},{"","","","","","","",""},{"","","","","","","",""},{"","","","","","","",""},
{"","","","","","","",""},{"","","","","","","",""},{"","","","","","","",""},{"","","","","","","",""},{"","","","","","","",""}};
protected void resetTable()
{
for(int i=0; i<20; i++)
{
for(int j=0; j<8; j++)
{
data[i][j] = "";
}
}
}
/*!
* Creates all the components required for the GUI.
*/
protected void makeComponents()
{
queryLabel = new JLabel("Query: ");
queryChoice = new JComboBox(new String[] {"query" ,"Query 1", "Query 2", "Query 3"});
PubLabel = new JLabel("No.of Publication");
searchLabel = new JLabel("Search By: ");
searchBy = new JComboBox(new String[] { "search by","By Author","By Title Tags"});
nameTitleTagsLabel = new JLabel("Name/Title Tags: ");
sinceYearLabel = new JLabel("Since Year: ");
rangeLabel = new JLabel("Custom Range: ");
customRangeFrom = new JTextArea();
customRangeTo = new JTextArea();
customRangeTo.setEditable(true);
customRangeTo.setText("YYYY");
customRangeFrom.setText("YYYY");
customRangeFrom.setEditable(true);
sinceYear = new JTextArea();
sinceYear.setEditable(true);
sinceYear.setText("YYYY");
noPubl = new JTextArea();
sortByRelevance = new JCheckBox("Sort By Relevance");
sortByYear = new JCheckBox("Sort By Year");
errorLabel = new JLabel();
resultNumberLabel = new JLabel();
nameTitleTags = new JTextArea();
makeTable();
queryResults = new JTextArea();
pane = new JScrollPane(jt);
pane.setBorder(BorderFactory.createEtchedBorder());
search = new JButton("Search");
reset = new JButton("reset");
next = new JButton("next");
}
/*!
* Creates and initializes the Table for Query Results.
*/
protected void makeTable()
{
String column[]={"S.No","Author","Title","Pages","Year","Volume","Journal/BookTitle","URL"};
jt=new JTable(data,column);
jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jt.setRowHeight(22);
jt.getColumnModel().getColumn(0).setPreferredWidth(40);
jt.getColumnModel().getColumn(1).setPreferredWidth(150);
jt.getColumnModel().getColumn(2).setPreferredWidth(170);
jt.getColumnModel().getColumn(3).setPreferredWidth(90);
jt.getColumnModel().getColumn(4).setPreferredWidth(90);
jt.getColumnModel().getColumn(5).setPreferredWidth(70);
jt.getColumnModel().getColumn(6).setPreferredWidth(150);
jt.getColumnModel().getColumn(7).setPreferredWidth(180);
}
/*!
* creates the horizontal Group for the Group Layout of the GUI
*/
protected void makeHorizontalGroup(GroupLayout layout)
{
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(queryLabel)
.addComponent(searchLabel)
.addComponent(PubLabel)
.addComponent(nameTitleTagsLabel)
.addComponent(sinceYearLabel)
.addComponent(rangeLabel)
.addComponent(search)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(queryChoice)
.addComponent(searchBy)
.addComponent(noPubl)
.addComponent(nameTitleTags)
.addComponent(sinceYear)
.addGroup(layout.createSequentialGroup().addContainerGap()
.addComponent(customRangeTo).addGap(8)
.addComponent(customRangeFrom))
.addComponent(reset)
).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
)
.addComponent(sortByYear)
.addComponent(sortByRelevance)
.addGap(30)
.addComponent(errorLabel)
.addGap(20)
.addComponent(resultNumberLabel)
).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pane)
.addComponent(next)
.addContainerGap()
)))
);
}
/*!
* creates the Vertical Group for the Group Layout of the GUI
*/
protected void makeVerticalGroup(GroupLayout layout)
{
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(pane, GroupLayout.Alignment.TRAILING)
.addComponent(next, GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(queryChoice)
.addComponent(queryLabel)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(searchBy)
.addComponent(searchLabel)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(nameTitleTagsLabel)
.addComponent(nameTitleTags)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(PubLabel)
.addComponent(noPubl)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(sinceYearLabel)
.addComponent(sinceYear)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(rangeLabel)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(customRangeFrom)
.addComponent(customRangeTo))).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sortByYear, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sortByRelevance, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(Alignment.BASELINE)
.addComponent(search)
.addComponent(reset)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGap(80).addComponent(resultNumberLabel).addGap(40).addComponent(errorLabel).addGap(120))).addContainerGap()
));
}
}
/*!
* the main GUI class where the GUI is being created using the
* group layout and the action listeners are added to it.
*/
class DBLP_GUI extends GUI
{
private static final long serialVersionUID = 1L;
private void integrateListeners()
{
queryChoice.addActionListener(new SelectQuery());
searchListener listenSearch = new searchListener();
search.addActionListener(listenSearch);
nextData nextListener = new nextData();
next.addActionListener(nextListener);
reset.addActionListener(new resetListener());
}
/*! CONSTRUCTOR
* Calls the Group Layout Functions and integrates listeners to it.
*/
public DBLP_GUI()
{
control = new Main();
this.currentIndex = 0;
setTitle("DBLP QUERY ENGINE");
setSize(900, 510);
makeComponents();
integrateListeners();
makeInvisible(searchBy, sortByRelevance, sortByYear, sinceYear, customRangeFrom,
customRangeTo, search, reset, searchLabel, sinceYearLabel, rangeLabel,
nameTitleTags, nameTitleTagsLabel, queryLabel, noPubl, PubLabel);
makeVisible(errorLabel, resultNumberLabel);
errorLabel.setText("");
resultNumberLabel.setText("");
GroupLayout layout = new GroupLayout(getContentPane());setLayout(layout);
makeHorizontalGroup(layout);
layout.linkSize(SwingConstants.HORIZONTAL, new java.awt.Component[] { queryChoice ,searchBy });
makeVerticalGroup(layout);
}
/*!
* Stores the data to the corresponding rows.
*/
private void fillRow(Publication p, int i){
data[i][0] = String.valueOf(i + this.currentIndex);
data[i][1] = p.getAuthors().get(0).getName();
data[i][2] = p.getTitle();
data[i][3] = p.getPages();
data[i][4] = String.valueOf(p.getYear());
data[i][5] = p.getVolume();
data[i][6] = p.getJournal();
data[i][7] = p.getURL();
}
/*!
* Displays the next 20 rows of the data from the List
*/
public void displayNextData(List<Publication> listPub){
if(listPub == null)
{
System.out.print("The list is null");
}
else
{
resetTable();
for(int i=this.currentIndex; i<this.currentIndex+20 && i<listPub.size(); i++){
fillRow(listPub.get(i), i-this.currentIndex);
}
this.currentIndex += 20;
}
}
/*!
* Displays the next 20 rows of the data from the Set of authors
*/
public void displayNextData(Set<Author> authors){
int count = 0;
resetTable();
while(count < 20 && authorIterator.hasNext()){
this.currentIndex += 1;
data[count][0] = String.valueOf(currentIndex);
data[count][1] = authorIterator.next().toString();
count += 1;
}
}
/*!
* A function for Setting the visibility of the variable number of arguments to false.
*/
private void makeInvisible(Component... components){
for(int i=0; i<components.length; i++)
components[i].setVisible(false);
}
/*!
* A function for Setting the visibility of the variable number of arguments to true.
*/
private void makeVisible(Component... components)
{
for(int i=0; i<components.length; i++)
components[i].setVisible(true);
}
/*!
* A function for prediction of the number of publications based on the
* the data provided till the specified year, considering only the non-zero values.
*/
private Map<Integer, Integer> tillYear(List<Publication> p, int toYear)
{
Map<Integer, Integer> predMap = new HashMap<Integer, Integer>();
for(Publication i : p)
{
if(i.getYear() <= toYear)
{
if(!predMap.containsKey(i.getYear()))
predMap.put(i.getYear(), 0);
int a = predMap.get(i.getYear());
predMap.put(i.getYear(), a+1);
}
}
return predMap;
}
/*!
* A function for Custom range text box where we specify the range of years
* for the publications to be printed.
*/
private List<Publication> checkRange(List<Publication> p){
List<Publication> linkedP = new ArrayList<Publication>();
if(customRangeFrom.getText().equals("YYYY") && customRangeTo.getText().equals("YYYY")){
return p;
}
else if(customRangeFrom.getText().equals("YYYY")){
try {
int to = Integer.valueOf(customRangeTo.getText());
for(Publication i : p){
if(i.getYear() <= to)
linkedP.add(i);
}
} catch (Exception e) {
errorLabel.setText("Please provide a valid year!");
return p;
}
}
else if(customRangeTo.getText().equals("YYYY"))
{
try {
int from = Integer.valueOf(customRangeTo.getText());
for(Publication i : p){
if(i.getYear() >= from)
linkedP.add(i);
}
} catch (Exception e) {
errorLabel.setText("Please provide a valid year!");
return p;
}
}
else{
try {
int to = Integer.valueOf(customRangeFrom.getText());
int from = Integer.valueOf(customRangeFrom.getText());
for(Publication i : p){
if(i.getYear() >= from && i.getYear() <= to)
linkedP.add(i);
}
} catch (Exception e) {
errorLabel.setText("Please provide a valid year!");
return p;
}
}
return linkedP;
}
/*!
* A function for Since year functionality where we specify the year from
* which we want the author's publications to be printed/returned.
*/
private List<Publication> checkSinceYear(List<Publication> p)
{
List<Publication> linkedP = new ArrayList<Publication>();
if(!sinceYear.getText().equals("YYYY"))
{
try
{
int since = Integer.valueOf(sinceYear.getText());
System.out.println(since);
for(Publication i : p){
if(i.getYear() >= since)
linkedP.add(i);
}
}
catch(Exception e){
errorLabel.setText("Please provide a valid year");
return p;
}
}
else{
return p;
}
return linkedP;
}
/*!
* Action Listener class for the query selection.
*/
class SelectQuery implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JComboBox box = (JComboBox)e.getSource();
String selectedQuery = box.getSelectedItem().toString();
switch (selectedQuery)
{
case "query" :
makeInvisible(searchBy, sortByRelevance, sortByYear, sinceYear, customRangeFrom,
customRangeTo, search, reset, searchLabel, sinceYearLabel, rangeLabel,
nameTitleTags, nameTitleTagsLabel, queryLabel, noPubl, PubLabel);
makeVisible(errorLabel, resultNumberLabel);
errorLabel.setText("");
resultNumberLabel.setText("");
break;
case "Query 1" :
makeVisible(searchBy, sortByRelevance, sortByYear, sinceYear, customRangeFrom,
customRangeTo, search, reset, searchLabel, sinceYearLabel, rangeLabel,
nameTitleTags, nameTitleTagsLabel, queryLabel, errorLabel, resultNumberLabel);
makeInvisible(noPubl, PubLabel);
errorLabel.setText("");
resultNumberLabel.setText("");
break;
case "Query 2" :
makeInvisible(searchBy, sortByRelevance, sortByYear, sinceYear, customRangeFrom,
customRangeTo, searchLabel, sinceYearLabel,
rangeLabel, nameTitleTags, nameTitleTagsLabel, queryLabel);
makeVisible(noPubl, search, reset, PubLabel, errorLabel, resultNumberLabel);
errorLabel.setText("");
resultNumberLabel.setText("");
break;
case "Query 3" :
makeInvisible(searchBy, sortByRelevance, sortByYear, customRangeFrom,
customRangeTo, searchLabel, rangeLabel, queryLabel, noPubl, PubLabel);
makeVisible(sinceYearLabel, nameTitleTags, nameTitleTagsLabel, search, reset, errorLabel, resultNumberLabel, sinceYear);
sinceYearLabel.setText("Up To Year");
errorLabel.setText("");
resultNumberLabel.setText("");
}
}
}
/*!
* ActionListener class for the SEARCH button
* This implements the action listener for the search button for
* all the queries separately and returns the required error if some
* required field is not selected
*/
class searchListener implements ActionListener {
private void queryOne()
{
String selectedSearch = searchBy.getSelectedItem().toString();
if(selectedSearch.equals("search by"))
errorLabel.setText("Please select the search by option.");
else if(selectedSearch.equals("By Author"))
{
if(nameTitleTags.getText() == null || nameTitleTags.getText().equals(""))
errorLabel.setText("Please specify Name/Title query string.");
else
{
System.out.println("Started working on query one.");
currentIndex = 0;
errorLabel.setText("");
String queryString = nameTitleTags.getText();
if(pubList != null)
pubList.clear();
pubList = control.getPublicationByAuthor(queryString);
System.out.println("Query one processed");
control.sortPublicationByDate(pubList);
pubList = checkRange(pubList);
pubList = checkSinceYear(pubList);
displayNextData(pubList);
if(pubList.size() > 0)
resultNumberLabel.setText(String.valueOf(pubList.size()));
else
resultNumberLabel.setText("No result!");
}
}
else if(selectedSearch.equals("By Title Tags"))
{
if(nameTitleTags.getText() == null || nameTitleTags.getText().equals(""))
errorLabel.setText("Please specify title tag.");
else
{
errorLabel.setText("");
if(pubMap != null)
pubMap.clear();
pubMap = control.getPublicatonByTitle(nameTitleTags.getText());
if(pubList != null)
pubList.clear();
for(Map.Entry<Integer,List<Publication> > entry : pubMap.entrySet())
pubList.addAll(entry.getValue());
pubList = checkRange(pubList);
pubList = checkSinceYear(pubList);
if(sortByRelevance.isSelected() && sortByYear.isSelected())
errorLabel.setText("Please select only one of the two sorting parameters.");
else if(sortByRelevance.isSelected()){
displayNextData(pubList);
if(pubList.size() > 0)
resultNumberLabel.setText(String.valueOf(pubList.size()));
else
resultNumberLabel.setText("No result!");
}
else if(sortByYear.isSelected()){
errorLabel.setText("");
control.sortPublicationByDate(pubList);
displayNextData(pubList);
if(pubList.size() > 0)
resultNumberLabel.setText(String.valueOf(pubList.size()));
else
resultNumberLabel.setText("No result!");
}
else
errorLabel.setText("Please select sorting parameter!");
}
}
}
private void queryTwo(){
String num1 = noPubl.getText().toString();
currentIndex = 0;
try {
if(num1 == null)
errorLabel.setText("Please specify the number of publications.");
int numP = Integer.valueOf(num1);
if(numP < 0)
errorLabel.setText("Specify number of publications as a valid positive Integer.");
else
{
errorLabel.setText("");
authors = control.getMoreThanK(numP);
if(authors != null)
authorIterator = authors.iterator();
displayNextData(authors);
if(authors.size() > 0)
resultNumberLabel.setText(String.valueOf(authors.size()));
else
resultNumberLabel.setText("No result!");
}
}
catch(Exception exc){
errorLabel.setText("Specify number of publications as a valid positive Integer.");
}
}
private void queryThree(){
String authorName = nameTitleTags.getText();
int year = 0;
try
{
year = Integer.valueOf(sinceYear.getText());
if(pubList != null)
pubList.clear();
pubList = control.getPublicationByAuthor(authorName);
Map<Integer, Integer> predictionMap = tillYear(pubList, year);
errorLabel.setText("");
Integer predictionPub = control.prediction(predictionMap, year+1);
resultNumberLabel.setText(String.valueOf(predictionPub));
}
catch(Exception e)
{
errorLabel.setText("Please provide a valid year!");
}
}
public void actionPerformed(ActionEvent e)
{
String selectedQuery = queryChoice.getSelectedItem().toString();
currentIndex = 0;
switch(selectedQuery)
{
case "Query 1" :
queryOne();
break;
case "Query 2" :
queryTwo();
break;
case "Query 3":
queryThree();
break;
}
}
}
/*!
* ActionListener class for the NEXT button
*/
class nextData implements ActionListener {
public void actionPerformed(ActionEvent e)
{
String selectedQuery = queryChoice.getSelectedItem().toString();
if(selectedQuery.equals("Query 1"))
{
errorLabel.setText("");
displayNextData(pubList);
}
else if(selectedQuery.equals("Query 2"))
{
errorLabel.setText("");
displayNextData(authors);
}
}
}
/*! ActionListener class for RESET button
* Resets all the texts and the selected items and clears the table.
*/
class resetListener implements ActionListener {
public void actionPerformed(ActionEvent e)
{
resetTable();
nameTitleTags.setText("");
customRangeFrom.setText("YYYY");
customRangeTo.setText("YYYY");
sinceYear.setText("YYYY");
noPubl.setText("");
errorLabel.setText("");
resultNumberLabel.setText("");
searchBy.setSelectedItem("search by");
}
}
}