Skip to content

Commit 6088221

Browse files
authored
Merge 'version_3_0_0' into 'main'
version 3.0.0
2 parents 26bb2f4 + 4b72894 commit 6088221

14 files changed

+135
-72
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>home</groupId>
88
<artifactId>vehicle</artifactId>
9-
<version>2.0.0</version>
9+
<version>3.0.0</version>
1010

1111
<packaging>jar</packaging>
1212

src/main/java/home/Main.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static void main(String[] args) {
2323
initDb();
2424
} else {
2525
try {
26-
new CustomJFileChooser(null).showCreateOrOpen();
26+
new CustomJFileChooser(null, CustomJFileChooser.CREATE_OR_OPEN).showChooser();
2727
initDb();
2828
Gui.getInstance().setDbLabel(Settings.DB_FILE_PATH);
2929
} catch (IOException e) {

src/main/java/home/Storage.java

+29
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package home;
22

33
import java.util.ArrayList;
4+
import java.util.HashSet;
45
import java.util.List;
6+
import java.util.Set;
57

68
import home.gui.Gui;
79
import home.models.AbstractVehicle;
810

911
public class Storage {
1012

13+
public static final int NO_ROW_IS_SELECTED = -1;
14+
1115
private static final List<AbstractVehicle> DATA_OBJS = new ArrayList<>();
16+
private static final Set<Long> DATA_OBJ_IDS_FOR_DELETE = new HashSet<>();
1217

1318
private static Storage instance;
1419

@@ -23,6 +28,7 @@ public static Storage getInstance() {
2328
}
2429

2530
public void refresh(List<AbstractVehicle> dataObjs) {
31+
DATA_OBJ_IDS_FOR_DELETE.clear();
2632
DATA_OBJS.clear();
2733
DATA_OBJS.addAll(dataObjs);
2834
Gui.getInstance().refreshTable();
@@ -35,4 +41,27 @@ public List<AbstractVehicle> getAll() {
3541
public AbstractVehicle get(int row) {
3642
return DATA_OBJS.get(row);
3743
}
44+
45+
public Long[] getIdsForDelete() {
46+
return DATA_OBJ_IDS_FOR_DELETE.stream().map(id -> Long.valueOf(id))
47+
.toArray(Long[]::new);
48+
}
49+
50+
public void updateStorage(AbstractVehicle dataObj, int tblRowOfSelectedDataObj) {
51+
if (NO_ROW_IS_SELECTED == tblRowOfSelectedDataObj) {
52+
DATA_OBJS.add(dataObj);
53+
} else {
54+
DATA_OBJS.set(tblRowOfSelectedDataObj, dataObj);
55+
}
56+
}
57+
58+
public void deleteObjects(List<AbstractVehicle> obsMarkedForDelete) {
59+
for (AbstractVehicle objForDel : obsMarkedForDelete) {
60+
long idObjForDel = objForDel.getId();
61+
if (idObjForDel > 0) {
62+
DATA_OBJ_IDS_FOR_DELETE.add(idObjForDel);
63+
}
64+
}
65+
DATA_OBJS.removeAll(obsMarkedForDelete);
66+
}
3867
}

src/main/java/home/db/dao/Dao.java

+2
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ public interface Dao {
1616
void update(AbstractVehicle dataObj) throws SQLException;
1717

1818
void delete(Long[] ids) throws SQLException;
19+
20+
void saveAllChanges() throws SQLException;
1921
}

src/main/java/home/db/dao/DaoSQLite.java

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.sql.SQLException;
66
import java.util.ArrayList;
77

8+
import home.Storage;
89
import home.db.Connector;
910
import home.db.DbConsts;
1011
import home.models.AbstractVehicle;
@@ -216,4 +217,21 @@ public void delete(Long[] ids) throws SQLException {
216217
Connector.closeConnection();
217218
}
218219
}
220+
221+
@Override
222+
public void saveAllChanges() throws SQLException {
223+
Long[] idsForDel = Storage.getInstance().getIdsForDelete();
224+
if (idsForDel.length > 0) {
225+
delete(Storage.getInstance().getIdsForDelete());
226+
}
227+
228+
for (AbstractVehicle dataObj : Storage.getInstance().getAll()) {
229+
long id = dataObj.getId();
230+
if (id > 0) {
231+
update(dataObj);
232+
} else {
233+
create(dataObj);
234+
}
235+
}
236+
}
219237
}

src/main/java/home/gui/DialogCaller.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.apache.log4j.Logger;
88

9+
import home.Storage;
910
import home.gui.component.dialog.AbstractDialog;
1011
import home.gui.component.dialog.DialogCar;
1112
import home.gui.component.dialog.DialogMotorcycle;
@@ -22,12 +23,13 @@ public class DialogCaller {
2223
private static final int OBJ_DIALOG_HEIGHT = 350;
2324

2425
public static <T extends AbstractDialog> void showObjDialog(JFrame frame,
25-
Class<T> dialogClass, AbstractVehicle dataObj) {
26+
Class<T> dialogClass, AbstractVehicle dataObj, int tblRowOfSelectedDataObj) {
2627
Constructor<T> constructor;
2728
try {
2829
constructor = dialogClass.getConstructor(
29-
new Class[] { int.class, int.class, AbstractVehicle.class });
30-
T dialog = constructor.newInstance(OBJ_DIALOG_WIDTH, OBJ_DIALOG_HEIGHT, dataObj);
30+
new Class[] { int.class, int.class, AbstractVehicle.class, int.class });
31+
T dialog = constructor.newInstance(OBJ_DIALOG_WIDTH, OBJ_DIALOG_HEIGHT,
32+
dataObj, tblRowOfSelectedDataObj);
3133
dialog.setVisible(true);
3234
} catch (Exception e) {
3335
Utils.logAndShowError(LOG, frame,
@@ -39,12 +41,12 @@ public static <T extends AbstractDialog> void showObjDialog(JFrame frame,
3941

4042
public static <T extends AbstractDialog> void showObjDialog(JFrame frame,
4143
Class<T> dialogClass) {
42-
showObjDialog(frame, dialogClass, null);
44+
showObjDialog(frame, dialogClass, null, Storage.NO_ROW_IS_SELECTED);
4345
}
4446

4547
@SuppressWarnings("unchecked")
4648
public static <T extends AbstractDialog> void showObjDialog(JFrame frame,
47-
AbstractVehicle dataObj) {
49+
AbstractVehicle dataObj, int tblRowOfSelectedDataObj) {
4850
Class<T> dialogClass = null;
4951
VehicleType objType = dataObj.getType();
5052
switch (objType) {
@@ -66,7 +68,7 @@ public static <T extends AbstractDialog> void showObjDialog(JFrame frame,
6668
"Object type error", new IllegalArgumentException("DataObj type error"));
6769
return;
6870
}
69-
showObjDialog(frame, dialogClass, dataObj);
71+
showObjDialog(frame, dialogClass, dataObj, tblRowOfSelectedDataObj);
7072
}
7173

7274
private DialogCaller() {

src/main/java/home/gui/Gui.java

+46-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import java.awt.event.MouseEvent;
99
import java.io.IOException;
1010
import java.sql.SQLException;
11+
import java.util.List;
1112
import java.util.Locale;
13+
import java.util.stream.Collectors;
1214

1315
import javax.swing.JCheckBoxMenuItem;
1416
import javax.swing.JLabel;
@@ -37,6 +39,7 @@
3739
import home.gui.component.dialog.DialogCar;
3840
import home.gui.component.dialog.DialogMotorcycle;
3941
import home.gui.component.dialog.DialogTruck;
42+
import home.models.AbstractVehicle;
4043
import home.utils.Utils;
4144

4245
public class Gui {
@@ -118,8 +121,9 @@ private void createTable() {
118121
public void mousePressed(MouseEvent mouseEvent) {
119122
JTable table = (JTable) mouseEvent.getSource();
120123
if (CLICK_COUNT == mouseEvent.getClickCount()) {
124+
int selectedTableRow = table.getSelectedRow();
121125
DialogCaller.showObjDialog(frame,
122-
Storage.getInstance().get(table.getSelectedRow()));
126+
Storage.getInstance().get(selectedTableRow), selectedTableRow);
123127
}
124128
}
125129
});
@@ -145,19 +149,13 @@ private void createButtons() {
145149
btnDelete = new CustomJButton(GuiConsts.DELETE);
146150
btnDelete.addActionListener(actionEvent -> {
147151
Utils.runInThread(() -> {
148-
try {
149-
Long[] idsMarkedForDelete = Storage.getInstance().getAll().stream()
150-
.filter(dataObj -> dataObj.isMarkedForDelete())
151-
.map(dataObj -> Long.valueOf(dataObj.getId()))
152-
.toArray(Long[]::new);
153-
if (idsMarkedForDelete.length > 0) {
154-
DaoSQLite.getInstance().delete(idsMarkedForDelete);
155-
Storage.getInstance().refresh(DaoSQLite.getInstance().readAll());
156-
}
157-
} catch (SQLException e) {
158-
Utils.logAndShowError(LOG, frame,
159-
"Error while delete:\n" + e.getLocalizedMessage(),
160-
"Deletion error", e);
152+
List<AbstractVehicle> obsMarkedForDelete = Storage.getInstance()
153+
.getAll().stream()
154+
.filter(dataObj -> dataObj.isMarkedForDelete())
155+
.collect(Collectors.toList());
156+
if (!obsMarkedForDelete.isEmpty()) {
157+
Storage.getInstance().deleteObjects(obsMarkedForDelete);
158+
Gui.getInstance().refreshTable();
161159
}
162160
});
163161
});
@@ -178,8 +176,11 @@ private void createPanels() {
178176
private void createMenu() {
179177
var createOfOpenItem = new JMenuItem(GuiConsts.CREATE_OR_OPEN);
180178
createOfOpenItem.addActionListener(new CreateOrOpenActionListener(frame, dbLabel));
179+
var saveItem = new JMenuItem(GuiConsts.SAVE);
180+
saveItem.addActionListener(new SaveActionListener(frame));
181181
var fileMenu = new JMenu(GuiConsts.FILE);
182182
fileMenu.add(createOfOpenItem);
183+
fileMenu.add(saveItem);
183184

184185
var defaultItem = new JCheckBoxMenuItem(GuiConsts.DEFAULT);
185186
defaultItem.setSelected(Settings.STYLE.equalsIgnoreCase(GuiConsts.DEFAULT));
@@ -252,7 +253,7 @@ public CreateOrOpenActionListener(Component parent, JLabel dbLabel) {
252253
public void actionPerformed(ActionEvent event) {
253254
Utils.runInThread(() -> {
254255
try {
255-
new CustomJFileChooser(parent).showCreateOrOpen();
256+
new CustomJFileChooser(parent, CustomJFileChooser.CREATE_OR_OPEN).showChooser();
256257
DbInitializer.createTableIfNotExists();
257258
Storage.getInstance().refresh(DaoSQLite.getInstance().readAll());
258259
dbLabel.setText(Settings.DB_FILE_PATH);
@@ -269,4 +270,34 @@ public void actionPerformed(ActionEvent event) {
269270
});
270271
}
271272
}
273+
274+
private static class SaveActionListener implements ActionListener {
275+
276+
private final Component parent;
277+
278+
public SaveActionListener(Component parent) {
279+
this.parent = parent;
280+
}
281+
282+
@Override
283+
public void actionPerformed(ActionEvent event) {
284+
Utils.runInThread(() -> {
285+
try {
286+
new CustomJFileChooser(parent, CustomJFileChooser.SAVE).showChooser();
287+
DbInitializer.createTableIfNotExists();
288+
DaoSQLite.getInstance().saveAllChanges();
289+
Storage.getInstance().refresh(DaoSQLite.getInstance().readAll());
290+
} catch (IOException e) {
291+
Utils.logAndShowError(LOG, parent, "Error while create/open DB file.",
292+
"Create/Open file error.", e);
293+
System.exit(1);
294+
} catch (SQLException e) {
295+
Utils.logAndShowError(LOG, parent,
296+
"Error while read selected DB file.\n" + e.getLocalizedMessage(),
297+
"Read selected DB error", e);
298+
System.exit(1);
299+
}
300+
});
301+
}
302+
}
272303
}

src/main/java/home/gui/GuiConsts.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public interface GuiConsts {
1818
String TRUCK = "Truck";
1919
String MOTORCYCLE = "Motorcycle";
2020
String DELETE = "Delete";
21-
String SAVE = "Save";
21+
String OK = "Ok";
2222
String CANCEL = "Cancel";
2323
String HAS_TRAILER = "has trailer";
2424
String TRANSPORTS_PASSENGERS = "transports passengers";
@@ -28,6 +28,7 @@ public interface GuiConsts {
2828
// Menu names
2929
String FILE = "File";
3030
String CREATE_OR_OPEN = "Create or Open";
31+
String SAVE = "Save";
3132
String STYLE = "Style";
3233
String DEFAULT = "Default";
3334
String SYSTEM = "System";

src/main/java/home/gui/component/CustomJFileChooser.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
@SuppressWarnings("serial")
1515
public class CustomJFileChooser extends JFileChooser {
1616

17+
public static final String CREATE_OR_OPEN = "Create or Open";
18+
public static final String SAVE = "Save";
19+
1720
private static final File APPLICATION_DIR = new File(".");
1821
private static final String EXTENSION_DESCRIPTIONS = "SQLite DB (*.db, *.sqlite, *.sqlite3)";
1922
private static final String[] EXTENSIONS = { "db", "sqlite", "sqlite3" };
2023

21-
private static final String CREATE_OR_OPEN = "Create or Open";
2224
private static final String CHOOSE_STORAGE = "Choose storage";
2325
private static final String TYPE_NAME_OR_CHOOSE_DB_FILE = "Type a new file"
2426
+ " name or choose already existed SQLite DB file.";
@@ -27,16 +29,18 @@ public class CustomJFileChooser extends JFileChooser {
2729
private int selectionRejectionCounter;
2830

2931
private final Component parent;
32+
private final String operation;
3033

31-
public CustomJFileChooser(Component parent) {
34+
public CustomJFileChooser(Component parent, String operation) {
3235
super(APPLICATION_DIR);
3336
this.parent = parent;
37+
this.operation = operation;
3438
setFileFilter(new FileNameExtensionFilter(
3539
EXTENSION_DESCRIPTIONS, EXTENSIONS));
3640
}
3741

38-
public void showCreateOrOpen() throws IOException {
39-
if (JFileChooser.APPROVE_OPTION == showDialog(parent, CREATE_OR_OPEN)) {
42+
public void showChooser() throws IOException {
43+
if (JFileChooser.APPROVE_OPTION == showDialog(parent, operation)) {
4044
selectionRejectionCounter = 0;
4145
File file = getSelectedFile();
4246
checkDbFileExtension(file);
@@ -48,7 +52,7 @@ public void showCreateOrOpen() throws IOException {
4852
if (MAX_REJECTIONS_COUNT == selectionRejectionCounter) {
4953
System.exit(1);
5054
}
51-
showCreateOrOpen();
55+
showChooser();
5256
}
5357
}
5458

0 commit comments

Comments
 (0)