Skip to content

Commit

Permalink
- code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cbell504 committed Nov 24, 2024
1 parent 4884539 commit 8d9cd35
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void attemptSaveAndReloadWindow(String website, String username, String p
&& !password.isEmpty()) {

frame.setVisible(false);
BuddyPasswordManagerService.saveAccount2(Constants.USERNAME, website, username, password);
BuddyPasswordManagerService.saveAccount(Constants.USERNAME, website, username, password);
BuddyPasswordManagerService.getInstance().scheduleTableReload();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.appbuddy.buddypasswordmanager.configs;

public final class Constants {

public final static String OPERATING_SYSTEM = System.getProperty("os.name");
public final static String USERNAME_PROPERTY = "user.name";
public final static String USERNAME = System.getProperty(Constants.USERNAME_PROPERTY);
public final static String MAC_OS_X = "Mac OS X";
public final static String WINDOWS_7 = "Windows 7";
public final static String WINDOWS_8 = "Windows 8";
public final static String WINDOWS_XP = "Windows XP";
public final static String SAVE_FILE_NAME = "Buddy_Information.txt";
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.appbuddy.buddypasswordmanager.services;

import com.appbuddy.buddypasswordmanager.configs.Constants;
import com.appbuddy.buddypasswordmanager.models.OSInfo;
import com.appbuddy.buddypasswordmanager.models.OSType;
import com.appbuddy.buddypasswordmanager.view.EntryListTable;
import com.appbuddy.buddypasswordmanager.view.ViewService;
import com.appbuddy.buddypasswordmanager.view.windows.MainWindow;

Expand All @@ -18,14 +16,8 @@
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.table.DefaultTableModel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;
import java.util.Scanner;
import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -35,15 +27,13 @@
public class BuddyPasswordManagerService {

private static final BuddyPasswordManagerService INSTANCE = new BuddyPasswordManagerService();
public ViewService viewService;
public static MainWindow mainWindow;
private OSInfo osInfo;
public static MainWindow mainWindow;
public ViewService viewService;
private static final String BUDDY_INFO_FILENAME = "BuddyInfo.txt";
public static final String ENCRYPTION_KEY = "YourSecretKey123";

public BuddyPasswordManagerService() {

}
private BuddyPasswordManagerService() {}

public void initService() {
initializeBuddyInfoFile();
Expand All @@ -55,67 +45,6 @@ public static BuddyPasswordManagerService getInstance() {
return INSTANCE;
}

public static void populateTable() {

log.info("Updating table data...");

var row = 0;
var col = 0;
var filePath = new File("/Users/" + Constants.USERNAME + "/Documents/BuddyInfo.txt");
try {
var input = new Scanner(filePath);
while (Objects.requireNonNull(input).hasNextLine()) {
var entry = input.nextLine();

var entryListTable = Objects.requireNonNullElse(mainWindow.getEntryListTable(), new EntryListTable());
var modelTable = Objects.requireNonNullElse(entryListTable.getModelTable(), new DefaultTableModel());
modelTable.setValueAt(entry, row, col);

col++;
if (col > 2) {
col = 0;
}
if (col == 0) {
row++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

public static void saveAccount(String username, String website, String name, String password) {
String fileName;
switch (Constants.OPERATING_SYSTEM) {
case Constants.WINDOWS_7:
case Constants.WINDOWS_8:
fileName = "C:/Users/" + username + "/Documents/" + Constants.SAVE_FILE_NAME;
printLoginData(fileName, website, name, password);
break;
case Constants.WINDOWS_XP:
fileName = "C:/Documents and Settings/" + username + "/My Documents/" + Constants.SAVE_FILE_NAME;
printLoginData(fileName, website, name, password);
break;
case Constants.MAC_OS_X:
fileName = "/Users/" + username + "/Documents/" + Constants.SAVE_FILE_NAME;
printLoginData(fileName, website, name, password);
break;
}
}

private static void printLoginData(String fileName, String website, String name, String password) {
FileWriter outFile;
try {
outFile = new FileWriter(fileName, true);
PrintWriter printOutFile = new PrintWriter(Objects.requireNonNull(outFile));
printOutFile.printf(website + "\n" + name + "\n" + password + "\n");
printOutFile.close();
} catch (IOException e) {

log.error("Error while attempt to print login data", e);
}
}

/**
* Checks for BuddyInfo.txt in user's home directory and creates it if it doesn't exist
*
Expand Down Expand Up @@ -170,7 +99,7 @@ public void scheduleTableReload() {
viewService.populateTable3();
}

public static boolean saveAccount2(String username, String website, String name, String password) {
public static void saveAccount(String username, String website, String name, String password) {
Objects.requireNonNull(username, "Username cannot be null");
Objects.requireNonNull(website, "Website cannot be null");
Objects.requireNonNull(name, "Name cannot be null");
Expand Down Expand Up @@ -198,11 +127,9 @@ public static boolean saveAccount2(String username, String website, String name,
);

log.info("Successfully saved account data for website: {}", website);
return true;

} catch (IOException e) {
log.error("Error saving account data: ", e);
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class EntryListTable extends JPanel {
private DefaultTableModel modelTable;
private final JTable table;

private static final String[] COLUMN_TITLES = {"Websites", "Usernames", "Passwords"};
public static final String[] COLUMN_TITLES = {"Websites", "Usernames", "Passwords"};
private static final int TABLE_WIDTH = 600;
private static final int TABLE_HEIGHT = 553;
private static final int PANEL_HEIGHT = 600;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
public class ViewService {

public MainWindow mainWindow;
private static final int COLUMN_COUNT = 3; // Assuming 3 columns based on original code
private static final String[] COLUMN_NAMES = {"Column 1", "Column 2", "Column 3"}; // Customize as needed


public ViewService() {
mainWindow = new MainWindow();
Expand All @@ -48,18 +45,17 @@ public void populateTable() {

// Verify the file exists
if (!Files.exists(filePath)) {
log.error("BuddyInfo.txt not found at: {}", filePath);
throw new IllegalStateException("BuddyInfo.txt not found");
}

// Read all lines from file
List<String> lines = Files.readAllLines(filePath);

// Calculate required rows based on data
int requiredRows = (int) Math.ceil(lines.size() / (double) COLUMN_COUNT);
int requiredRows = (int) Math.ceil(lines.size() / (double) EntryListTable.COLUMN_TITLES.length);

// Create data matrix for the table
Object[][] tableData = new Object[requiredRows][COLUMN_COUNT];
Object[][] tableData = new Object[requiredRows][EntryListTable.COLUMN_TITLES.length];

// Populate the data matrix
int currentRow = 0;
Expand All @@ -70,7 +66,7 @@ public void populateTable() {
tableData[currentRow][currentCol] = line.trim();

currentCol++;
if (currentCol >= COLUMN_COUNT) {
if (currentCol >= EntryListTable.COLUMN_TITLES.length) {
currentCol = 0;
currentRow++;
}
Expand All @@ -84,7 +80,7 @@ public void populateTable() {
);

// Create and set new table model
DefaultTableModel newModel = new DefaultTableModel(tableData, COLUMN_NAMES) {
DefaultTableModel newModel = new DefaultTableModel(tableData, EntryListTable.COLUMN_TITLES) {
@Override
public boolean isCellEditable(int row, int column) {
return false; // Make table read-only
Expand All @@ -107,7 +103,6 @@ public boolean isCellEditable(int row, int column) {

/**
* Populates the table with decrypted data from BuddyInfo.txt
* @param mainWindow the main window containing the table
* @throws IllegalStateException if the table or model is null
*/
public void populateTable3() {
Expand Down Expand Up @@ -143,7 +138,7 @@ public void populateTable3() {
}

// Create data matrix for the table
Object[][] tableData = new Object[decryptedEntries.size()][COLUMN_COUNT];
Object[][] tableData = new Object[decryptedEntries.size()][EntryListTable.COLUMN_TITLES.length];

// Populate the data matrix
for (int i = 0; i < decryptedEntries.size(); i++) {
Expand All @@ -160,7 +155,7 @@ public void populateTable3() {
);

// Create and set new table model with custom cell editing rules
DefaultTableModel newModel = new DefaultTableModel(tableData, COLUMN_NAMES) {
DefaultTableModel newModel = new DefaultTableModel(tableData, EntryListTable.COLUMN_TITLES) {
@Override
public boolean isCellEditable(int row, int column) {
return false; // Make table read-only
Expand Down Expand Up @@ -220,7 +215,7 @@ private static String maskPassword(String password) {
public static void clearTable(MainWindow mainWindow) {
try {
EntryListTable entryListTable = Objects.requireNonNull(mainWindow.getEntryListTable());
DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, 0);
DefaultTableModel model = new DefaultTableModel(EntryListTable.COLUMN_TITLES, 0);

javax.swing.SwingUtilities.invokeLater(() -> {
entryListTable.setModelTable(model);
Expand Down

0 comments on commit 8d9cd35

Please sign in to comment.