FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Uni__LibraryManagementJavaGUI/src/code/AddBookForm.java at master · bestmahdi2/Uni__LibraryManagementJavaGUI · GitHub
bestmahdi2
/
Uni__LibraryManagementJavaGUI
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Uni__LibraryManagementJavaGUI
/
src
/
code
/
AddBookForm.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
234 lines (199 loc) · 8.75 KB
Breadcrumbs
Uni__LibraryManagementJavaGUI
/
src
/
code
/
AddBookForm.java
Copy path
File metadata and controls
234 lines (199 loc) · 8.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
code
;
import
javax
.
swing
.*;
import
javax
.
swing
.
filechooser
.
FileNameExtensionFilter
;
import
java
.
awt
.*;
import
java
.
awt
.
event
.*;
import
java
.
io
.
File
;
import
java
.
io
.
IOException
;
import
java
.
nio
.
file
.
Files
;
import
java
.
nio
.
file
.
StandardCopyOption
;
import
java
.
util
.
Objects
;
public
class
AddBookForm
extends
JFrame
{
// Ui variables
private
JPanel
adminFrame
;
private
JPanel
labelUsername
;
private
JLabel
labelLogo
;
private
JLabel
labelName
;
private
JTextField
fieldBookName
;
private
JButton
buttonOpenPhoto
;
private
JButton
buttonAddBook
;
private
JTextField
fieldBookPhotoAddress
;
private
JLabel
labelAuthor
;
private
JTextField
fieldBookAuthor
;
private
JLabel
labelErrorAddBook
;
private
JLabel
labelAbout
;
// keeper for name, author and photo address of the new book
private
String
bookName
;
private
String
authorName
;
private
String
photoAddress
;
/**
* Constructor method,
*/
public
AddBookForm
() {
setResizable
(
false
);
// forbidden user to resize the frame
setLocationRelativeTo
(
null
);
// to be opened in center
try
{
// change its look to Microsoft Windows
UIManager
.
setLookAndFeel
(
UIManager
.
getSystemLookAndFeelClassName
());
}
catch
(
Exception
e
) {
e
.
printStackTrace
();
}
setContentPane
(
adminFrame
);
// set main frame
// Title and size of frame
setTitle
(
"Add Book Form"
);
setSize
(
500
,
700
);
// set Icon for JFrame
setIconImage
(
new
ImageIcon
(
"src/images/mOMIDs.png"
).
getImage
());
mouseClickActions
();
// mouse click actions to object in frame
mouseMoveActions
();
// mouse move actions to object in frame
}
/**
* cleanup method for clean text fields
*/
private
void
cleanup
() {
fieldBookName
.
setText
(
""
);
fieldBookAuthor
.
setText
(
""
);
fieldBookPhotoAddress
.
setText
(
""
);
}
/**
* cleanup method for clean text fields
*
* @param sourceFile The source file of the photo
* @param destLoc String value of destination location with folder and file
* @param destFolder String value of destination location with folder
* @param destType String value of file type
*/
private
void
copyPhoto
(
File
sourceFile
,
String
destLoc
,
String
destFolder
,
String
destType
) {
try
{
// try to copy photo to database location of photos
Files
.
copy
(
sourceFile
.
toPath
(),
(
new
File
(
destLoc
)).
toPath
(),
StandardCopyOption
.
REPLACE_EXISTING
);
}
catch
(
IOException
ex
) {
ex
.
printStackTrace
();
}
// rename the file to name of the book for ease of access
File
destFile
=
new
File
(
destLoc
);
File
newFile
=
new
File
(
destFolder
+
fieldBookName
.
getText
() +
destType
);
destFile
.
renameTo
(
newFile
);
}
/**
* mouseMoveActions method for setting move actions of mouse for any object on frame
*/
private
void
mouseMoveActions
() {
fieldBookName
.
addMouseMotionListener
(
new
MouseMotionAdapter
() {
@
Override
public
void
mouseMoved
(
MouseEvent
e
) {
super
.
mouseMoved
(
e
);
buttonAddBook
.
setBackground
(
new
Color
(
234
,
35
,
123
));
buttonOpenPhoto
.
setBackground
(
new
Color
(
234
,
35
,
123
));
}
});
fieldBookPhotoAddress
.
addMouseMotionListener
(
new
MouseMotionAdapter
() {
@
Override
public
void
mouseMoved
(
MouseEvent
e
) {
super
.
mouseMoved
(
e
);
buttonAddBook
.
setBackground
(
new
Color
(
234
,
35
,
123
));
buttonOpenPhoto
.
setBackground
(
new
Color
(
234
,
35
,
123
));
}
});
buttonOpenPhoto
.
addMouseMotionListener
(
new
MouseMotionAdapter
() {
@
Override
public
void
mouseMoved
(
MouseEvent
e
) {
super
.
mouseMoved
(
e
);
buttonOpenPhoto
.
setCursor
(
Cursor
.
getPredefinedCursor
(
Cursor
.
HAND_CURSOR
));
buttonAddBook
.
setBackground
(
new
Color
(
234
,
35
,
123
));
buttonOpenPhoto
.
setBackground
(
new
Color
(
135
,
69
,
211
));
}
});
buttonAddBook
.
addMouseMotionListener
(
new
MouseMotionAdapter
() {
@
Override
public
void
mouseMoved
(
MouseEvent
e
) {
super
.
mouseMoved
(
e
);
buttonAddBook
.
setCursor
(
Cursor
.
getPredefinedCursor
(
Cursor
.
HAND_CURSOR
));
buttonOpenPhoto
.
setBackground
(
new
Color
(
234
,
35
,
123
));
buttonAddBook
.
setBackground
(
new
Color
(
135
,
69
,
211
));
}
});
labelAbout
.
addMouseMotionListener
(
new
MouseMotionAdapter
() {
@
Override
public
void
mouseMoved
(
MouseEvent
e
) {
super
.
mouseMoved
(
e
);
labelAbout
.
setCursor
(
Cursor
.
getPredefinedCursor
(
Cursor
.
HAND_CURSOR
));
}
});
}
/**
* mouseMoveActions method for setting click actions of mouse for any object on frame
*/
private
void
mouseClickActions
() {
buttonAddBook
.
addActionListener
(
new
ActionListener
() {
@
Override
public
void
actionPerformed
(
ActionEvent
e
) {
// get values
bookName
=
fieldBookName
.
getText
();
authorName
=
fieldBookAuthor
.
getText
();
photoAddress
=
fieldBookPhotoAddress
.
getText
();
// set error label to none
labelErrorAddBook
.
setText
(
" "
);
labelErrorAddBook
.
setForeground
(
Color
.
RED
);
boolean
continues
=
true
;
// check if a book is already in program or not
for
(
String
[][]
user
:
Manager
.
getBooks
()) {
if
(
Objects
.
equals
(
user
[
0
][
0
],
bookName
)) {
labelErrorAddBook
.
setText
(
"⚠️ Book name is taken !"
);
continues
=
false
;
}
}
if
(
continues
) {
if
(
Objects
.
equals
(
bookName
,
""
))
labelErrorAddBook
.
setText
(
"⚠️ Book name can't be empty !"
);
else
if
(
Objects
.
equals
(
authorName
,
""
))
labelErrorAddBook
.
setText
(
"⚠️ Author Name can't be empty !"
);
else
if
(!
Manager
.
isAlpha
(
authorName
))
labelErrorAddBook
.
setText
(
"⚠️ Author Name can only be alphabetical !"
);
else
if
(
Objects
.
equals
(
photoAddress
,
""
))
labelErrorAddBook
.
setText
(
"⚠️ Photo must be chosen !"
);
else
if
(!
new
File
(
photoAddress
).
isFile
())
labelErrorAddBook
.
setText
(
"⚠️ Photo does not exist !"
);
else
{
// if all conditions were fine:
labelErrorAddBook
.
setText
(
" "
);
// create new file, choose destination
File
sourceFile
=
new
File
(
photoAddress
);
String
destFolder
=
"src/photos/"
;
String
destLoc
=
destFolder
+
sourceFile
.
getName
();
String
destType
=
destLoc
.
substring
(
destLoc
.
lastIndexOf
(
'.'
));
copyPhoto
(
sourceFile
,
destLoc
,
destFolder
,
destType
);
// copy file to photos folder
// add book to database
Manager
.
addBook
(
bookName
,
authorName
,
sourceFile
.
getPath
(),
destFolder
+
fieldBookName
.
getText
() +
destType
,
"0"
);
// show a message
JOptionPane
.
showMessageDialog
(
null
,
String
.
format
(
"'%s' added to books successfully !"
,
bookName
),
"Book Adding Success !"
,
JOptionPane
.
INFORMATION_MESSAGE
);
cleanup
();
// clean text fields
}
}
}
});
buttonOpenPhoto
.
addActionListener
(
new
ActionListener
() {
@
Override
public
void
actionPerformed
(
ActionEvent
e
) {
// open file chooser
JFileChooser
fileChooser
=
new
JFileChooser
();
FileNameExtensionFilter
filter
=
new
FileNameExtensionFilter
(
"Images"
,
"jpg"
,
"png"
,
"gif"
,
"bmp"
);
fileChooser
.
setFileFilter
(
filter
);
if
(
fileChooser
.
showOpenDialog
(
adminFrame
) ==
JFileChooser
.
APPROVE_OPTION
) {
fieldBookPhotoAddress
.
setText
(
String
.
valueOf
(
fileChooser
.
getSelectedFile
()));
}
}
});
MouseListener
ml2
;
ml2
=
new
MouseAdapter
() {
public
void
mousePressed
(
MouseEvent
e
) {
AboutForm
aboutForm
=
new
AboutForm
();
}
};
labelAbout
.
addMouseListener
(
ml2
);
}
/**
* main method for testing the frame, it's useless in general
*/
public
static
void
main
(
String
[]
args
) {
AddBookForm
com
=
new
AddBookForm
();
}
}
Back
|
FazBrowse Home
|
New Git URL