Skip to content

Commit

Permalink
Merge 'version_3_0_0' into 'main'
Browse files Browse the repository at this point in the history
version 3.0.0
  • Loading branch information
m1ra9e authored Jan 27, 2024
2 parents 26bb2f4 + 4b72894 commit 6088221
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 72 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>home</groupId>
<artifactId>vehicle</artifactId>
<version>2.0.0</version>
<version>3.0.0</version>

<packaging>jar</packaging>

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/home/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void main(String[] args) {
initDb();
} else {
try {
new CustomJFileChooser(null).showCreateOrOpen();
new CustomJFileChooser(null, CustomJFileChooser.CREATE_OR_OPEN).showChooser();
initDb();
Gui.getInstance().setDbLabel(Settings.DB_FILE_PATH);
} catch (IOException e) {
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/home/Storage.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package home;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import home.gui.Gui;
import home.models.AbstractVehicle;

public class Storage {

public static final int NO_ROW_IS_SELECTED = -1;

private static final List<AbstractVehicle> DATA_OBJS = new ArrayList<>();
private static final Set<Long> DATA_OBJ_IDS_FOR_DELETE = new HashSet<>();

private static Storage instance;

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

public void refresh(List<AbstractVehicle> dataObjs) {
DATA_OBJ_IDS_FOR_DELETE.clear();
DATA_OBJS.clear();
DATA_OBJS.addAll(dataObjs);
Gui.getInstance().refreshTable();
Expand All @@ -35,4 +41,27 @@ public List<AbstractVehicle> getAll() {
public AbstractVehicle get(int row) {
return DATA_OBJS.get(row);
}

public Long[] getIdsForDelete() {
return DATA_OBJ_IDS_FOR_DELETE.stream().map(id -> Long.valueOf(id))
.toArray(Long[]::new);
}

public void updateStorage(AbstractVehicle dataObj, int tblRowOfSelectedDataObj) {
if (NO_ROW_IS_SELECTED == tblRowOfSelectedDataObj) {
DATA_OBJS.add(dataObj);
} else {
DATA_OBJS.set(tblRowOfSelectedDataObj, dataObj);
}
}

public void deleteObjects(List<AbstractVehicle> obsMarkedForDelete) {
for (AbstractVehicle objForDel : obsMarkedForDelete) {
long idObjForDel = objForDel.getId();
if (idObjForDel > 0) {
DATA_OBJ_IDS_FOR_DELETE.add(idObjForDel);
}
}
DATA_OBJS.removeAll(obsMarkedForDelete);
}
}
2 changes: 2 additions & 0 deletions src/main/java/home/db/dao/Dao.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface Dao {
void update(AbstractVehicle dataObj) throws SQLException;

void delete(Long[] ids) throws SQLException;

void saveAllChanges() throws SQLException;
}
18 changes: 18 additions & 0 deletions src/main/java/home/db/dao/DaoSQLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.sql.SQLException;
import java.util.ArrayList;

import home.Storage;
import home.db.Connector;
import home.db.DbConsts;
import home.models.AbstractVehicle;
Expand Down Expand Up @@ -216,4 +217,21 @@ public void delete(Long[] ids) throws SQLException {
Connector.closeConnection();
}
}

@Override
public void saveAllChanges() throws SQLException {
Long[] idsForDel = Storage.getInstance().getIdsForDelete();
if (idsForDel.length > 0) {
delete(Storage.getInstance().getIdsForDelete());
}

for (AbstractVehicle dataObj : Storage.getInstance().getAll()) {
long id = dataObj.getId();
if (id > 0) {
update(dataObj);
} else {
create(dataObj);
}
}
}
}
14 changes: 8 additions & 6 deletions src/main/java/home/gui/DialogCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.apache.log4j.Logger;

import home.Storage;
import home.gui.component.dialog.AbstractDialog;
import home.gui.component.dialog.DialogCar;
import home.gui.component.dialog.DialogMotorcycle;
Expand All @@ -22,12 +23,13 @@ public class DialogCaller {
private static final int OBJ_DIALOG_HEIGHT = 350;

public static <T extends AbstractDialog> void showObjDialog(JFrame frame,
Class<T> dialogClass, AbstractVehicle dataObj) {
Class<T> dialogClass, AbstractVehicle dataObj, int tblRowOfSelectedDataObj) {
Constructor<T> constructor;
try {
constructor = dialogClass.getConstructor(
new Class[] { int.class, int.class, AbstractVehicle.class });
T dialog = constructor.newInstance(OBJ_DIALOG_WIDTH, OBJ_DIALOG_HEIGHT, dataObj);
new Class[] { int.class, int.class, AbstractVehicle.class, int.class });
T dialog = constructor.newInstance(OBJ_DIALOG_WIDTH, OBJ_DIALOG_HEIGHT,
dataObj, tblRowOfSelectedDataObj);
dialog.setVisible(true);
} catch (Exception e) {
Utils.logAndShowError(LOG, frame,
Expand All @@ -39,12 +41,12 @@ public static <T extends AbstractDialog> void showObjDialog(JFrame frame,

public static <T extends AbstractDialog> void showObjDialog(JFrame frame,
Class<T> dialogClass) {
showObjDialog(frame, dialogClass, null);
showObjDialog(frame, dialogClass, null, Storage.NO_ROW_IS_SELECTED);
}

@SuppressWarnings("unchecked")
public static <T extends AbstractDialog> void showObjDialog(JFrame frame,
AbstractVehicle dataObj) {
AbstractVehicle dataObj, int tblRowOfSelectedDataObj) {
Class<T> dialogClass = null;
VehicleType objType = dataObj.getType();
switch (objType) {
Expand All @@ -66,7 +68,7 @@ public static <T extends AbstractDialog> void showObjDialog(JFrame frame,
"Object type error", new IllegalArgumentException("DataObj type error"));
return;
}
showObjDialog(frame, dialogClass, dataObj);
showObjDialog(frame, dialogClass, dataObj, tblRowOfSelectedDataObj);
}

private DialogCaller() {
Expand Down
61 changes: 46 additions & 15 deletions src/main/java/home/gui/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JLabel;
Expand Down Expand Up @@ -37,6 +39,7 @@
import home.gui.component.dialog.DialogCar;
import home.gui.component.dialog.DialogMotorcycle;
import home.gui.component.dialog.DialogTruck;
import home.models.AbstractVehicle;
import home.utils.Utils;

public class Gui {
Expand Down Expand Up @@ -118,8 +121,9 @@ private void createTable() {
public void mousePressed(MouseEvent mouseEvent) {
JTable table = (JTable) mouseEvent.getSource();
if (CLICK_COUNT == mouseEvent.getClickCount()) {
int selectedTableRow = table.getSelectedRow();
DialogCaller.showObjDialog(frame,
Storage.getInstance().get(table.getSelectedRow()));
Storage.getInstance().get(selectedTableRow), selectedTableRow);
}
}
});
Expand All @@ -145,19 +149,13 @@ private void createButtons() {
btnDelete = new CustomJButton(GuiConsts.DELETE);
btnDelete.addActionListener(actionEvent -> {
Utils.runInThread(() -> {
try {
Long[] idsMarkedForDelete = Storage.getInstance().getAll().stream()
.filter(dataObj -> dataObj.isMarkedForDelete())
.map(dataObj -> Long.valueOf(dataObj.getId()))
.toArray(Long[]::new);
if (idsMarkedForDelete.length > 0) {
DaoSQLite.getInstance().delete(idsMarkedForDelete);
Storage.getInstance().refresh(DaoSQLite.getInstance().readAll());
}
} catch (SQLException e) {
Utils.logAndShowError(LOG, frame,
"Error while delete:\n" + e.getLocalizedMessage(),
"Deletion error", e);
List<AbstractVehicle> obsMarkedForDelete = Storage.getInstance()
.getAll().stream()
.filter(dataObj -> dataObj.isMarkedForDelete())
.collect(Collectors.toList());
if (!obsMarkedForDelete.isEmpty()) {
Storage.getInstance().deleteObjects(obsMarkedForDelete);
Gui.getInstance().refreshTable();
}
});
});
Expand All @@ -178,8 +176,11 @@ private void createPanels() {
private void createMenu() {
var createOfOpenItem = new JMenuItem(GuiConsts.CREATE_OR_OPEN);
createOfOpenItem.addActionListener(new CreateOrOpenActionListener(frame, dbLabel));
var saveItem = new JMenuItem(GuiConsts.SAVE);
saveItem.addActionListener(new SaveActionListener(frame));
var fileMenu = new JMenu(GuiConsts.FILE);
fileMenu.add(createOfOpenItem);
fileMenu.add(saveItem);

var defaultItem = new JCheckBoxMenuItem(GuiConsts.DEFAULT);
defaultItem.setSelected(Settings.STYLE.equalsIgnoreCase(GuiConsts.DEFAULT));
Expand Down Expand Up @@ -252,7 +253,7 @@ public CreateOrOpenActionListener(Component parent, JLabel dbLabel) {
public void actionPerformed(ActionEvent event) {
Utils.runInThread(() -> {
try {
new CustomJFileChooser(parent).showCreateOrOpen();
new CustomJFileChooser(parent, CustomJFileChooser.CREATE_OR_OPEN).showChooser();
DbInitializer.createTableIfNotExists();
Storage.getInstance().refresh(DaoSQLite.getInstance().readAll());
dbLabel.setText(Settings.DB_FILE_PATH);
Expand All @@ -269,4 +270,34 @@ public void actionPerformed(ActionEvent event) {
});
}
}

private static class SaveActionListener implements ActionListener {

private final Component parent;

public SaveActionListener(Component parent) {
this.parent = parent;
}

@Override
public void actionPerformed(ActionEvent event) {
Utils.runInThread(() -> {
try {
new CustomJFileChooser(parent, CustomJFileChooser.SAVE).showChooser();
DbInitializer.createTableIfNotExists();
DaoSQLite.getInstance().saveAllChanges();
Storage.getInstance().refresh(DaoSQLite.getInstance().readAll());
} catch (IOException e) {
Utils.logAndShowError(LOG, parent, "Error while create/open DB file.",
"Create/Open file error.", e);
System.exit(1);
} catch (SQLException e) {
Utils.logAndShowError(LOG, parent,
"Error while read selected DB file.\n" + e.getLocalizedMessage(),
"Read selected DB error", e);
System.exit(1);
}
});
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/home/gui/GuiConsts.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface GuiConsts {
String TRUCK = "Truck";
String MOTORCYCLE = "Motorcycle";
String DELETE = "Delete";
String SAVE = "Save";
String OK = "Ok";
String CANCEL = "Cancel";
String HAS_TRAILER = "has trailer";
String TRANSPORTS_PASSENGERS = "transports passengers";
Expand All @@ -28,6 +28,7 @@ public interface GuiConsts {
// Menu names
String FILE = "File";
String CREATE_OR_OPEN = "Create or Open";
String SAVE = "Save";
String STYLE = "Style";
String DEFAULT = "Default";
String SYSTEM = "System";
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/home/gui/component/CustomJFileChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
@SuppressWarnings("serial")
public class CustomJFileChooser extends JFileChooser {

public static final String CREATE_OR_OPEN = "Create or Open";
public static final String SAVE = "Save";

private static final File APPLICATION_DIR = new File(".");
private static final String EXTENSION_DESCRIPTIONS = "SQLite DB (*.db, *.sqlite, *.sqlite3)";
private static final String[] EXTENSIONS = { "db", "sqlite", "sqlite3" };

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

private final Component parent;
private final String operation;

public CustomJFileChooser(Component parent) {
public CustomJFileChooser(Component parent, String operation) {
super(APPLICATION_DIR);
this.parent = parent;
this.operation = operation;
setFileFilter(new FileNameExtensionFilter(
EXTENSION_DESCRIPTIONS, EXTENSIONS));
}

public void showCreateOrOpen() throws IOException {
if (JFileChooser.APPROVE_OPTION == showDialog(parent, CREATE_OR_OPEN)) {
public void showChooser() throws IOException {
if (JFileChooser.APPROVE_OPTION == showDialog(parent, operation)) {
selectionRejectionCounter = 0;
File file = getSelectedFile();
checkDbFileExtension(file);
Expand All @@ -48,7 +52,7 @@ public void showCreateOrOpen() throws IOException {
if (MAX_REJECTIONS_COUNT == selectionRejectionCounter) {
System.exit(1);
}
showCreateOrOpen();
showChooser();
}
}

Expand Down
Loading

0 comments on commit 6088221

Please sign in to comment.