Big code clean-up, added interfaces, and colored text
This commit is contained in:
@@ -3,16 +3,9 @@ package org.lumijiez;
|
||||
import org.lumijiez.gui.MainFrame;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
try {
|
||||
new MainFrame().setVisible(true);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));
|
||||
}
|
||||
}
|
||||
33
Lab3/src/main/java/org/lumijiez/base/Document.java
Normal file
33
Lab3/src/main/java/org/lumijiez/base/Document.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package org.lumijiez.base;
|
||||
|
||||
import org.lumijiez.enums.FileType;
|
||||
import org.lumijiez.interfaces.IDocument;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Document implements IDocument {
|
||||
private String fullFilename = "";
|
||||
private FileType fileType;
|
||||
private Date createdAt;
|
||||
private Date modifiedAt;
|
||||
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExtension() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getCreatedTime() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getModificationTime() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
15
Lab3/src/main/java/org/lumijiez/enums/DiffType.java
Normal file
15
Lab3/src/main/java/org/lumijiez/enums/DiffType.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package org.lumijiez.enums;
|
||||
|
||||
public enum DiffType {
|
||||
CREATE(StateType.NEW), DELETE(StateType.DELETED), MODIFY(StateType.MODIFIED), NONE(StateType.NONE);
|
||||
|
||||
private final StateType type;
|
||||
|
||||
DiffType(StateType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public StateType getState() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
16
Lab3/src/main/java/org/lumijiez/enums/FileType.java
Normal file
16
Lab3/src/main/java/org/lumijiez/enums/FileType.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package org.lumijiez.enums;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public enum FileType {
|
||||
IMAGE, PLAINTEXT, FILE, CODE;
|
||||
public List<String> typeExtensions;
|
||||
|
||||
static {
|
||||
IMAGE.typeExtensions = new ArrayList<>(List.of("jpg", "png"));
|
||||
PLAINTEXT.typeExtensions = new ArrayList<>(List.of("txt", "csv"));
|
||||
FILE.typeExtensions = new ArrayList<>(List.of("doc", "pdf", "zip"));
|
||||
CODE.typeExtensions = new ArrayList<>(List.of("java", "cpp", "py"));
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,14 @@
|
||||
package org.lumijiez.util;
|
||||
package org.lumijiez.enums;
|
||||
|
||||
public enum StateType {
|
||||
NEW("Created"), MODIFIED("Modified"), DELETED("Deleted"), NONE("Nothing");
|
||||
|
||||
private final String name;
|
||||
private final String action;
|
||||
StateType(String name) {
|
||||
this.name = name;
|
||||
this.action = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
public String getAction() {
|
||||
return this.action;
|
||||
}
|
||||
}
|
||||
@@ -4,16 +4,18 @@ import org.lumijiez.tracker.TrackerThread;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
|
||||
public class MainFrame extends JFrame {
|
||||
public static Path FOLDER_PATH;
|
||||
private final JScrollPane fileListScrollPane = new JScrollPane();
|
||||
private final JScrollPane fileInfoScrollPane = new JScrollPane();
|
||||
private final JList<String> fileList = new JList<>();
|
||||
private final JLabel pathLabel = new JLabel();
|
||||
private final JScrollPane mainScrollPane = new JScrollPane();
|
||||
private final JTextPane mainTextPane = new JTextPane();
|
||||
private final JTextPane fileInfoTextPane = new JTextPane();
|
||||
private final JLabel snapshotLabel = new JLabel();
|
||||
private final JButton CommitButton = new JButton();
|
||||
private final JButton StatusButton = new JButton();
|
||||
@@ -25,7 +27,7 @@ public class MainFrame extends JFrame {
|
||||
private final Map<File, byte[]> fileContents = new HashMap<>();
|
||||
private TrackerThread tracker;
|
||||
|
||||
public MainFrame() throws IOException {
|
||||
public MainFrame() {
|
||||
initComponents();
|
||||
}
|
||||
|
||||
@@ -40,20 +42,22 @@ public class MainFrame extends JFrame {
|
||||
|
||||
int returnVal = folderChooser.showOpenDialog(this);
|
||||
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||
tracker = new TrackerThread(mainTextPane, Path.of(folderChooser.getSelectedFile().getAbsolutePath()), fileContents, fileList);
|
||||
FOLDER_PATH = Path.of(folderChooser.getSelectedFile().getAbsolutePath());
|
||||
tracker = new TrackerThread(mainTextPane, fileContents, fileList);
|
||||
tracker.start();
|
||||
}
|
||||
|
||||
fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
fileListScrollPane.setViewportView(fileList);
|
||||
|
||||
snapshotLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18)); // NOI18N
|
||||
snapshotLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18));
|
||||
snapshotLabel.setText("Last snapshot: " + new Date());
|
||||
|
||||
mainScrollPane.setViewportView(mainTextPane);
|
||||
fileInfoScrollPane.setViewportView(fileInfoTextPane);
|
||||
|
||||
pathLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18)); // NOI18N
|
||||
pathLabel.setText("Currently tracking: " + folderChooser.getSelectedFile().getAbsolutePath());
|
||||
pathLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18));
|
||||
pathLabel.setText("Currently tracking: " + FOLDER_PATH.toString());
|
||||
|
||||
CommitButton.setText("Commit");
|
||||
CommitButton.addActionListener(this::CommitButtonActionPerformed);
|
||||
@@ -84,35 +88,32 @@ public class MainFrame extends JFrame {
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addContainerGap()
|
||||
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
|
||||
.addComponent(pathLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
||||
.addComponent(snapshotLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
||||
.addComponent(pathLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addComponent(fileListScrollPane, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE)
|
||||
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addComponent(mainScrollPane, GroupLayout.PREFERRED_SIZE, 599, GroupLayout.PREFERRED_SIZE)
|
||||
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addComponent(CommitButton)
|
||||
.addGap(0, 1, Short.MAX_VALUE))
|
||||
.addComponent(StatusButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
|
||||
.addComponent(CommitButton, GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
|
||||
.addComponent(fileInfoScrollPane))))
|
||||
.addContainerGap())
|
||||
);
|
||||
layout.setVerticalGroup(
|
||||
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
|
||||
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
|
||||
.addComponent(snapshotLabel, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
|
||||
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
||||
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addComponent(pathLabel, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
|
||||
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
|
||||
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
|
||||
.addComponent(fileListScrollPane, GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
|
||||
.addComponent(mainScrollPane))
|
||||
.addComponent(fileListScrollPane, GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
|
||||
.addComponent(mainScrollPane)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addComponent(CommitButton)
|
||||
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addComponent(StatusButton)))
|
||||
.addComponent(fileInfoScrollPane)))
|
||||
.addContainerGap()));
|
||||
pack();
|
||||
}
|
||||
|
||||
10
Lab3/src/main/java/org/lumijiez/interfaces/IDocument.java
Normal file
10
Lab3/src/main/java/org/lumijiez/interfaces/IDocument.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package org.lumijiez.interfaces;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public interface IDocument {
|
||||
public String getFilename();
|
||||
public String getExtension();
|
||||
public Date getCreatedTime();
|
||||
public Date getModificationTime();
|
||||
}
|
||||
@@ -1,26 +1,24 @@
|
||||
package org.lumijiez.tracker;
|
||||
|
||||
import org.lumijiez.util.DiffType;
|
||||
import org.lumijiez.gui.MainFrame;
|
||||
import org.lumijiez.enums.DiffType;
|
||||
import org.lumijiez.util.FileDiffer;
|
||||
import org.lumijiez.util.StateType;
|
||||
import org.lumijiez.enums.StateType;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.File;
|
||||
import java.nio.file.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TrackerThread extends Thread {
|
||||
private final JTextPane textPane;
|
||||
private final Path path;
|
||||
private Map<File, byte[]> fileContents;
|
||||
private final JList<String> fileList;
|
||||
private final Map<File, StateType> fileStates = new HashMap<>();
|
||||
private final JList<String> fileList;
|
||||
|
||||
public TrackerThread(JTextPane textPane, Path path, Map<File, byte[]> files, JList<String> fileList) {
|
||||
public TrackerThread(JTextPane textPane, Map<File, byte[]> files, JList<String> fileList) {
|
||||
this.textPane = textPane;
|
||||
this.path = path;
|
||||
this.fileContents = files;
|
||||
this.fileList = fileList;
|
||||
init();
|
||||
@@ -28,19 +26,14 @@ public class TrackerThread extends Thread {
|
||||
|
||||
public void init() {
|
||||
System.out.println("Init called");
|
||||
fileContents = FileDiffer.crawlDirectory(path);
|
||||
|
||||
ArrayList<String> fileNames = new ArrayList<>();
|
||||
|
||||
for (File file : fileContents.keySet()) {
|
||||
fileNames.add(file.getName());
|
||||
}
|
||||
fileContents = FileDiffer.crawlDirectory(MainFrame.FOLDER_PATH);
|
||||
ArrayList<File> fList = new ArrayList<>(fileContents.keySet());
|
||||
fileList.setModel(new AbstractListModel<>() {
|
||||
public int getSize() {
|
||||
return fileNames.size();
|
||||
return fList.size();
|
||||
}
|
||||
public String getElementAt(int i) {
|
||||
return fileNames.get(i);
|
||||
return fList.get(i).getName();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -50,42 +43,37 @@ public class TrackerThread extends Thread {
|
||||
}
|
||||
|
||||
public void checkDirectory() {
|
||||
Map<DiffType, ArrayList<File>> result = FileDiffer.diff(fileContents, FileDiffer.crawlDirectory(path));
|
||||
Map<DiffType, ArrayList<File>> result = FileDiffer.diff(fileContents, FileDiffer.crawlDirectory(MainFrame.FOLDER_PATH));
|
||||
|
||||
StringBuilder toShow = new StringBuilder();
|
||||
|
||||
boolean created = false, deleted = false, modified = false;
|
||||
if (!result.get(DiffType.CREATE).isEmpty()) {
|
||||
created = true;
|
||||
for (File file : result.get(DiffType.CREATE)) {
|
||||
fileStates.put(file, StateType.NEW);
|
||||
boolean somethingNew = false;
|
||||
|
||||
for (DiffType type : result.keySet()) {
|
||||
for (File file : result.get(type)) {
|
||||
somethingNew = true;
|
||||
System.out.println("File changed " + file.getName() + " " + type.getState().getAction());
|
||||
fileStates.put(file, type.getState());
|
||||
}
|
||||
System.out.println("Created");
|
||||
}
|
||||
|
||||
if (!result.get(DiffType.DELETE).isEmpty()) {
|
||||
deleted = true;
|
||||
for (File file : result.get(DiffType.DELETE)) {
|
||||
fileStates.put(file, StateType.DELETED);
|
||||
}
|
||||
System.out.println("Deleted");
|
||||
}
|
||||
|
||||
if (!result.get(DiffType.MODIFY).isEmpty()) {
|
||||
modified = true;
|
||||
for (File file : result.get(DiffType.MODIFY)) {
|
||||
fileStates.put(file, StateType.MODIFIED);
|
||||
}
|
||||
System.out.println("Modified");
|
||||
}
|
||||
|
||||
if (created || deleted || modified) {
|
||||
if (somethingNew) {
|
||||
init();
|
||||
for (File file : fileStates.keySet()) {
|
||||
if (fileStates.get(file) != StateType.NONE) {
|
||||
toShow.append(file.getName()).append(" has been ").append(fileStates.get(file).getName()).append("<br>");
|
||||
if (fileStates.get(file) == StateType.NEW) {
|
||||
toShow.append("<span color=\"green\">");
|
||||
}
|
||||
if (fileStates.get(file) == StateType.DELETED) {
|
||||
toShow.append("<span color=\"red\">");
|
||||
}
|
||||
if (fileStates.get(file) == StateType.MODIFIED) {
|
||||
toShow.append("<span color=\"orange\">");
|
||||
}
|
||||
toShow.append(file.getName()).append(" has been ").append(fileStates.get(file).getAction()).append("</span><br>");
|
||||
}
|
||||
}
|
||||
// System.out.println(toShow.toString());
|
||||
textPane.setText(toShow.toString());
|
||||
}
|
||||
}
|
||||
@@ -94,7 +82,6 @@ public class TrackerThread extends Thread {
|
||||
public void run() {
|
||||
while(this.isAlive()) {
|
||||
checkDirectory();
|
||||
//Thread.sleep(200);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package org.lumijiez.util;
|
||||
|
||||
public enum DiffType {
|
||||
CREATE, DELETE, MODIFY, NONE
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.lumijiez.util;
|
||||
|
||||
import org.lumijiez.enums.DiffType;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@@ -12,32 +14,27 @@ import java.util.stream.Stream;
|
||||
|
||||
public class FileDiffer {
|
||||
public static Map<DiffType, ArrayList<File>> diff(Map<File, byte[]> oldFiles, Map<File, byte[]> newFiles) {
|
||||
Map<DiffType, ArrayList<File>> result = new HashMap<>();
|
||||
|
||||
ArrayList<File> newFileList = new ArrayList<>();
|
||||
ArrayList<File> deletedFileList = new ArrayList<>();
|
||||
ArrayList<File> modifiedFileList = new ArrayList<>();
|
||||
Map<DiffType, ArrayList<File>> result = Map.of(
|
||||
DiffType.CREATE, new ArrayList<>(),
|
||||
DiffType.DELETE, new ArrayList<>(),
|
||||
DiffType.MODIFY, new ArrayList<>());
|
||||
|
||||
for (File file : oldFiles.keySet()) {
|
||||
if (newFiles.get(file) != null) {
|
||||
if (Arrays.compare(oldFiles.get(file), newFiles.get(file)) != 0) {
|
||||
modifiedFileList.add(file);
|
||||
result.get(DiffType.MODIFY).add(file);
|
||||
}
|
||||
} else {
|
||||
deletedFileList.add(file);
|
||||
result.get(DiffType.DELETE).add(file);
|
||||
}
|
||||
}
|
||||
|
||||
for (File file : newFiles.keySet()) {
|
||||
if (oldFiles.get(file) == null) {
|
||||
newFileList.add(file);
|
||||
result.get(DiffType.CREATE).add(file);
|
||||
}
|
||||
}
|
||||
|
||||
result.put(DiffType.CREATE, newFileList);
|
||||
result.put(DiffType.DELETE, deletedFileList);
|
||||
result.put(DiffType.MODIFY, modifiedFileList);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user