Merge pull request #1 from lumijiez/lab3
Lab3
This commit was merged in pull request #1.
This commit is contained in:
@@ -1,19 +1,18 @@
|
||||
package org.lumijiez;
|
||||
|
||||
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
|
||||
// then press Enter. You can now see whitespace characters in your code.
|
||||
import org.lumijiez.gui.MainFrame;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// Press Alt+Enter with your caret at the highlighted text to see how
|
||||
// IntelliJ IDEA suggests fixing it.
|
||||
System.out.print("Hello and welcome!");
|
||||
|
||||
// Press Shift+F10 or click the green arrow button in the gutter to run the code.
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
|
||||
// Press Shift+F9 to start debugging your code. We have set one breakpoint
|
||||
// for you, but you can always add more by pressing Ctrl+F8.
|
||||
System.out.println("i = " + i);
|
||||
}
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
try {
|
||||
new MainFrame().setVisible(true);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
128
Lab3/src/main/java/org/lumijiez/gui/MainFrame.java
Normal file
128
Lab3/src/main/java/org/lumijiez/gui/MainFrame.java
Normal file
@@ -0,0 +1,128 @@
|
||||
package org.lumijiez.gui;
|
||||
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 {
|
||||
private final JScrollPane fileListScrollPane = 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 JLabel snapshotLabel = new JLabel();
|
||||
private final JButton CommitButton = new JButton();
|
||||
private final JButton StatusButton = new JButton();
|
||||
private final JMenuBar MainMenubar = new JMenuBar();
|
||||
private final JMenu fileMenu = new JMenu();
|
||||
private final JMenuItem pickFolder = new JMenuItem();
|
||||
private final JMenu settingsMenu = new JMenu();
|
||||
private final JMenuItem settings = new JMenuItem();
|
||||
private final Map<File, byte[]> fileContents = new HashMap<>();
|
||||
private TrackerThread tracker;
|
||||
|
||||
public MainFrame() throws IOException {
|
||||
initComponents();
|
||||
}
|
||||
|
||||
private void initComponents() {
|
||||
|
||||
mainTextPane.setContentType("text/html");
|
||||
|
||||
JFileChooser folderChooser = new JFileChooser();
|
||||
folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
||||
|
||||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
|
||||
int returnVal = folderChooser.showOpenDialog(this);
|
||||
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||
tracker = new TrackerThread(mainTextPane, Path.of(folderChooser.getSelectedFile().getAbsolutePath()), 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.setText("Last snapshot: " + new Date());
|
||||
|
||||
mainScrollPane.setViewportView(mainTextPane);
|
||||
|
||||
pathLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18)); // NOI18N
|
||||
pathLabel.setText("Currently tracking: " + folderChooser.getSelectedFile().getAbsolutePath());
|
||||
|
||||
CommitButton.setText("Commit");
|
||||
CommitButton.addActionListener(this::CommitButtonActionPerformed);
|
||||
|
||||
StatusButton.setText("Status");
|
||||
StatusButton.addActionListener(this::StatusButtonActionPerformed);
|
||||
|
||||
fileMenu.setText("File");
|
||||
|
||||
pickFolder.setText("Pick another folder");
|
||||
fileMenu.add(pickFolder);
|
||||
|
||||
MainMenubar.add(fileMenu);
|
||||
|
||||
settingsMenu.setText("Edit");
|
||||
|
||||
settings.setText("Settings");
|
||||
settingsMenu.add(settings);
|
||||
|
||||
MainMenubar.add(settingsMenu);
|
||||
|
||||
setJMenuBar(MainMenubar);
|
||||
|
||||
GroupLayout layout = new GroupLayout(getContentPane());
|
||||
getContentPane().setLayout(layout);
|
||||
layout.setHorizontalGroup(
|
||||
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
|
||||
.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)
|
||||
.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))))
|
||||
.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)
|
||||
.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))
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addComponent(CommitButton)
|
||||
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addComponent(StatusButton)))
|
||||
.addContainerGap()));
|
||||
pack();
|
||||
}
|
||||
|
||||
private void CommitButtonActionPerformed(java.awt.event.ActionEvent evt) {
|
||||
tracker.reset();
|
||||
mainTextPane.setText("");
|
||||
snapshotLabel.setText("Last snapshot: " + new Date());
|
||||
}
|
||||
|
||||
private void StatusButtonActionPerformed(java.awt.event.ActionEvent evt) {
|
||||
}
|
||||
}
|
||||
100
Lab3/src/main/java/org/lumijiez/tracker/TrackerThread.java
Normal file
100
Lab3/src/main/java/org/lumijiez/tracker/TrackerThread.java
Normal file
@@ -0,0 +1,100 @@
|
||||
package org.lumijiez.tracker;
|
||||
|
||||
import org.lumijiez.util.DiffType;
|
||||
import org.lumijiez.util.FileDiffer;
|
||||
import org.lumijiez.util.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<>();
|
||||
|
||||
public TrackerThread(JTextPane textPane, Path path, Map<File, byte[]> files, JList<String> fileList) {
|
||||
this.textPane = textPane;
|
||||
this.path = path;
|
||||
this.fileContents = files;
|
||||
this.fileList = fileList;
|
||||
init();
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
fileList.setModel(new AbstractListModel<>() {
|
||||
public int getSize() {
|
||||
return fileNames.size();
|
||||
}
|
||||
public String getElementAt(int i) {
|
||||
return fileNames.get(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
fileStates.clear();
|
||||
}
|
||||
|
||||
public void checkDirectory() {
|
||||
Map<DiffType, ArrayList<File>> result = FileDiffer.diff(fileContents, FileDiffer.crawlDirectory(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);
|
||||
}
|
||||
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) {
|
||||
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>");
|
||||
}
|
||||
}
|
||||
textPane.setText(toShow.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(this.isAlive()) {
|
||||
checkDirectory();
|
||||
//Thread.sleep(200);
|
||||
}
|
||||
}
|
||||
}
|
||||
5
Lab3/src/main/java/org/lumijiez/util/DiffType.java
Normal file
5
Lab3/src/main/java/org/lumijiez/util/DiffType.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package org.lumijiez.util;
|
||||
|
||||
public enum DiffType {
|
||||
CREATE, DELETE, MODIFY, NONE
|
||||
}
|
||||
61
Lab3/src/main/java/org/lumijiez/util/FileDiffer.java
Normal file
61
Lab3/src/main/java/org/lumijiez/util/FileDiffer.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package org.lumijiez.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
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<>();
|
||||
|
||||
for (File file : oldFiles.keySet()) {
|
||||
if (newFiles.get(file) != null) {
|
||||
if (Arrays.compare(oldFiles.get(file), newFiles.get(file)) != 0) {
|
||||
modifiedFileList.add(file);
|
||||
}
|
||||
} else {
|
||||
deletedFileList.add(file);
|
||||
}
|
||||
}
|
||||
|
||||
for (File file : newFiles.keySet()) {
|
||||
if (oldFiles.get(file) == null) {
|
||||
newFileList.add(file);
|
||||
}
|
||||
}
|
||||
|
||||
result.put(DiffType.CREATE, newFileList);
|
||||
result.put(DiffType.DELETE, deletedFileList);
|
||||
result.put(DiffType.MODIFY, modifiedFileList);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Map<File, byte[]> crawlDirectory(Path path) {
|
||||
Map<File, byte[]> newFileContents = new HashMap<>();
|
||||
try (Stream<Path> paths = Files.walk(path)) {
|
||||
paths.forEach(p -> {
|
||||
if (Files.isRegularFile(p)) {
|
||||
try {
|
||||
newFileContents.put(p.toFile(), Files.readAllBytes(p));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return newFileContents;
|
||||
}
|
||||
}
|
||||
14
Lab3/src/main/java/org/lumijiez/util/StateType.java
Normal file
14
Lab3/src/main/java/org/lumijiez/util/StateType.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package org.lumijiez.util;
|
||||
|
||||
public enum StateType {
|
||||
NEW("Created"), MODIFIED("Modified"), DELETED("Deleted"), NONE("Nothing");
|
||||
|
||||
private final String name;
|
||||
StateType(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user