-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryMainFrame.java
More file actions
232 lines (193 loc) Β· 7.17 KB
/
Copy pathLibraryMainFrame.java
File metadata and controls
232 lines (193 loc) Β· 7.17 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
229
230
231
232
package ui;
import dao.BookDAO;
import dao.IssuedBookDAO;
import models.Book;
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.logging.Logger;
/**
* LibraryMainFrame.java - Main application window
*
* MVC Pattern: This is the main View component
* Uses JTabbedPane to organize different operations:
* 1. Add Book
* 2. Search Book
* 3. Issue/Return Book
* 4. View All Books
*
* Best Practices:
* - Modular design (each operation in separate panel)
* - Consistent look and feel
* - Clear navigation
*/
public class LibraryMainFrame extends JFrame {
private static final Logger logger = Logger.getLogger(LibraryMainFrame.class.getName());
private static final long serialVersionUID = 1L;
// UI Components
private JTabbedPane tabbedPane;
private AddBookPanel addBookPanel;
private SearchBookPanel searchBookPanel;
private IssueReturnPanel issueReturnPanel;
private ViewBooksPanel viewBooksPanel;
// Data Access Objects
private BookDAO bookDAO;
private IssuedBookDAO issuedBookDAO;
/**
* Constructor - Initializes the main frame
*/
public LibraryMainFrame() {
// Initialize DAOs
this.bookDAO = new BookDAO();
this.issuedBookDAO = new IssuedBookDAO();
// Frame settings
setTitle("π Library Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1000, 700);
setLocationRelativeTo(null);
setResizable(true);
setIconImage(createIconImage());
// Initialize UI
initializeUI();
// Display statistics
displayStatistics();
logger.info("β Application started successfully");
}
/**
* Initializes all UI components
*/
private void initializeUI() {
// Create tabbed pane
tabbedPane = new JTabbedPane();
tabbedPane.setTabPlacement(JTabbedPane.TOP);
tabbedPane.setFont(new Font("Arial", Font.BOLD, 12));
// Create panels for each operation
addBookPanel = new AddBookPanel(bookDAO);
searchBookPanel = new SearchBookPanel(bookDAO);
issueReturnPanel = new IssueReturnPanel(bookDAO, issuedBookDAO);
viewBooksPanel = new ViewBooksPanel(bookDAO);
// Add tabs to tabbed pane
tabbedPane.addTab("β Add Book", addBookPanel);
tabbedPane.addTab("π Search Book", searchBookPanel);
tabbedPane.addTab("π€ Issue/Return", issueReturnPanel);
tabbedPane.addTab("π View All Books", viewBooksPanel);
// Create menu bar
createMenuBar();
// Create status bar
JPanel statusBar = createStatusBar();
// Add components to frame
add(tabbedPane, BorderLayout.CENTER);
add(statusBar, BorderLayout.SOUTH);
}
/**
* Creates the menu bar
*/
private void createMenuBar() {
JMenuBar menuBar = new JMenuBar();
// File Menu
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem refreshItem = new JMenuItem("Refresh");
refreshItem.addActionListener(e -> refreshAllTabs());
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(e -> System.exit(0));
fileMenu.add(refreshItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
// Help Menu
JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic('H');
JMenuItem aboutItem = new JMenuItem("About");
aboutItem.addActionListener(e -> showAboutDialog());
helpMenu.add(aboutItem);
menuBar.add(fileMenu);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
}
/**
* Creates status bar with statistics
*/
private JPanel createStatusBar() {
JPanel statusPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
statusPanel.setBorder(BorderFactory.createLoweredBevelBorder());
JLabel statusLabel = new JLabel("Ready");
statusPanel.add(statusLabel);
return statusPanel;
}
/**
* Displays statistics on startup
*/
private void displayStatistics() {
try {
int totalBooks = bookDAO.getAllBooks().size();
int availableBooks = bookDAO.getAvailableBooksCount();
int issuedBooks = issuedBookDAO.getTotalIssuedCount();
int overdueBooks = issuedBookDAO.getOverdueBooksCount();
String stats = String.format(
"Total Books: %d | Available: %d | Issued: %d | Overdue: %d",
totalBooks, availableBooks, issuedBooks, overdueBooks
);
logger.info("π " + stats);
} catch (Exception e) {
logger.warning("Could not fetch statistics: " + e.getMessage());
}
}
/**
* Refreshes all tabs with latest data
*/
private void refreshAllTabs() {
addBookPanel.refresh();
searchBookPanel.refresh();
issueReturnPanel.refresh();
viewBooksPanel.refresh();
displayStatistics();
JOptionPane.showMessageDialog(this, "β All data refreshed",
"Success", JOptionPane.INFORMATION_MESSAGE);
}
/**
* Shows about dialog
*/
private void showAboutDialog() {
String aboutText = "Library Management System v1.0\n\n" +
"A Java Swing application for managing library books\n\n" +
"Features:\n" +
"β Add and manage books\n" +
"β Search books by ID or title\n" +
"β Issue and return books\n" +
"β Track overdue books\n" +
"β View comprehensive statistics\n\n" +
"Database: MySQL with JDBC\n" +
"UI Framework: Java Swing";
JOptionPane.showMessageDialog(this, aboutText, "About",
JOptionPane.INFORMATION_MESSAGE);
}
/**
* Creates a simple icon image
*/
private Image createIconImage() {
// Create a simple 16x16 image
java.awt.image.BufferedImage image =
new java.awt.image.BufferedImage(16, 16, java.awt.image.BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.BLUE);
g2d.fillRect(0, 0, 16, 16);
g2d.setColor(Color.WHITE);
g2d.drawString("π", 2, 14);
g2d.dispose();
return image;
}
/**
* Main entry point
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
logger.warning("Could not set system look and feel");
}
LibraryMainFrame frame = new LibraryMainFrame();
frame.setVisible(true);
});
}
}