diff --git a/Lab3/src/main/java/org/lumijiez/Main.java b/Lab3/src/main/java/org/lumijiez/Main.java index aa6e1f9..b7c22c9 100644 --- a/Lab3/src/main/java/org/lumijiez/Main.java +++ b/Lab3/src/main/java/org/lumijiez/Main.java @@ -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)); } } \ No newline at end of file diff --git a/Lab3/src/main/java/org/lumijiez/base/ArbitraryFile.java b/Lab3/src/main/java/org/lumijiez/base/ArbitraryFile.java new file mode 100644 index 0000000..e784d03 --- /dev/null +++ b/Lab3/src/main/java/org/lumijiez/base/ArbitraryFile.java @@ -0,0 +1,20 @@ +package org.lumijiez.base; + +import java.nio.file.Path; + +public class ArbitraryFile extends Document{ + public ArbitraryFile(Path path) { + super(path); + } + + @Override + public String getInfo() { + StringBuilder info = new StringBuilder(); + info.append("Type: ").append(getFileType().getTypeName()).append("
"); + info.append("Extension: ").append(getExtension().toUpperCase()).append("
"); + info.append("File size: ").append(getFilesizeKB()).append(" KB").append("
"); + info.append("Created at: ").append(getCreatedTime()).append("
"); + info.append("Modified at: ").append(getModificationTime()).append("
"); + return info.toString(); + } +} diff --git a/Lab3/src/main/java/org/lumijiez/base/CodeFile.java b/Lab3/src/main/java/org/lumijiez/base/CodeFile.java new file mode 100644 index 0000000..db20eec --- /dev/null +++ b/Lab3/src/main/java/org/lumijiez/base/CodeFile.java @@ -0,0 +1,25 @@ +package org.lumijiez.base; + +import org.lumijiez.util.Utils; + +import java.nio.file.Path; + +public class CodeFile extends Document{ + public CodeFile(Path path) { + super(path); + } + + @Override + public String getInfo() { + StringBuilder info = new StringBuilder(); + info.append("Type: ").append(getFileType().getTypeName()).append("
"); + info.append("Extension: ").append(getExtension().toUpperCase()).append("
"); + info.append("File size: ").append(getFilesizeKB()).append(" KB").append("
"); + info.append("Lines: ").append(Utils.countLines(this)).append("
"); + info.append("Classes: ").append(Utils.countClasses(this)).append("
"); + info.append("Methods: ").append(Utils.countMethods(this)).append("
"); + info.append("Created at: ").append(getCreatedTime()).append("
"); + info.append("Modified at: ").append(getModificationTime()).append("
"); + return info.toString(); + } +} diff --git a/Lab3/src/main/java/org/lumijiez/base/Document.java b/Lab3/src/main/java/org/lumijiez/base/Document.java new file mode 100644 index 0000000..2de230a --- /dev/null +++ b/Lab3/src/main/java/org/lumijiez/base/Document.java @@ -0,0 +1,92 @@ +package org.lumijiez.base; + +import org.lumijiez.enums.FileType; +import org.lumijiez.interfaces.IDocument; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class Document extends File implements IDocument { + private String extension; + private FileType fileType; + + public Document(Path path) { + super(path.toString()); + init(); + } + + private void init() { + int lastDotIndex = getName().lastIndexOf('.'); + extension = (lastDotIndex > 0) ? getName().substring(lastDotIndex + 1) : ""; + fileType = FileType.getFileType(extension); + } + + @Override + public String getExtension() { + return extension; + } + + @Override + public String getCreatedTime() { + try { + BasicFileAttributes fileAttributes = Files.readAttributes(Path.of(this.getPath()), BasicFileAttributes.class); + SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yy"); + SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); + + Date date = new Date(fileAttributes.creationTime().toMillis()); + + String formattedDate = dateFormat.format(date); + String formattedTime = timeFormat.format(date); + + return formattedTime + " " + formattedDate; + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public String getModificationTime() { + try { + BasicFileAttributes fileAttributes = Files.readAttributes(Path.of(this.getPath()), BasicFileAttributes.class); + SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yy"); + SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); + + Date date = new Date(fileAttributes.lastModifiedTime().toMillis()); + + String formattedDate = dateFormat.format(date); + String formattedTime = timeFormat.format(date); + + return formattedTime + " " + formattedDate; + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public FileType getFileType() { + return fileType; + } + + @Override + public long getFilesizeKB() { + try { + BasicFileAttributes fileAttributes = Files.readAttributes(Path.of(this.getPath()), BasicFileAttributes.class); + return fileAttributes.size() / 1024; + } catch (IOException e) { + e.printStackTrace(); + } + return 0; + } + + @Override + public String getInfo() { + return "Name: " + getName() + " Size: " + getFilesizeKB() + "Type: " + getFileType().getTypeName(); + } +} diff --git a/Lab3/src/main/java/org/lumijiez/base/ImageFile.java b/Lab3/src/main/java/org/lumijiez/base/ImageFile.java new file mode 100644 index 0000000..b11887e --- /dev/null +++ b/Lab3/src/main/java/org/lumijiez/base/ImageFile.java @@ -0,0 +1,29 @@ +package org.lumijiez.base; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.nio.file.Path; + +public class ImageFile extends Document { + public ImageFile(Path path) { + super(path); + } + @Override + public String getInfo() { + try { + StringBuilder info = new StringBuilder(); + info.append("Type: ").append(getFileType().getTypeName()).append("
"); + info.append("Extension: ").append(getExtension().toUpperCase()).append("
"); + BufferedImage image = ImageIO.read(this); + info.append("Dimensions: ").append(image.getWidth()).append("x").append(image.getHeight()).append("
"); + info.append("File size: ").append(getFilesizeKB()).append(" KB").append("
"); + info.append("Created at: ").append(getCreatedTime()).append("
"); + info.append("Modified at: ").append(getModificationTime()).append("
"); + return info.toString(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/Lab3/src/main/java/org/lumijiez/base/TextFile.java b/Lab3/src/main/java/org/lumijiez/base/TextFile.java new file mode 100644 index 0000000..2784817 --- /dev/null +++ b/Lab3/src/main/java/org/lumijiez/base/TextFile.java @@ -0,0 +1,25 @@ +package org.lumijiez.base; + +import org.lumijiez.util.Utils; + +import java.nio.file.Path; + +public class TextFile extends Document { + public TextFile(Path path) { + super(path); + } + + @Override + public String getInfo() { + StringBuilder info = new StringBuilder(); + info.append("Type: ").append(getFileType().getTypeName()).append("
"); + info.append("Extension: ").append(getExtension().toUpperCase()).append("
"); + info.append("File size: ").append(getFilesizeKB()).append(" KB").append("
"); + info.append("Words: ").append(Utils.countWords(this)).append("
"); + info.append("Lines: ").append(Utils.countLines(this)).append("
"); + info.append("Characters: ").append(Utils.countChars(this)).append("
"); + info.append("Created at: ").append(getCreatedTime()).append("
"); + info.append("Modified at: ").append(getModificationTime()).append("
"); + return info.toString(); + } +} diff --git a/Lab3/src/main/java/org/lumijiez/enums/DiffType.java b/Lab3/src/main/java/org/lumijiez/enums/DiffType.java new file mode 100644 index 0000000..55e060e --- /dev/null +++ b/Lab3/src/main/java/org/lumijiez/enums/DiffType.java @@ -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; + } +} diff --git a/Lab3/src/main/java/org/lumijiez/enums/FileType.java b/Lab3/src/main/java/org/lumijiez/enums/FileType.java new file mode 100644 index 0000000..9dc612b --- /dev/null +++ b/Lab3/src/main/java/org/lumijiez/enums/FileType.java @@ -0,0 +1,35 @@ +package org.lumijiez.enums; + +import java.util.ArrayList; +import java.util.List; + +public enum FileType { + IMAGE("Image", new ArrayList<>(List.of("jpg", "png"))), + PLAINTEXT("Plaintext", new ArrayList<>(List.of("txt", "csv"))), + FILE("File", new ArrayList<>(List.of("doc", "pdf", "zip"))), + CODE("Code", new ArrayList<>(List.of("java", "cpp", "py"))), + NONE("None", new ArrayList<>()); + + private final List typeExtensions; + private final String typeName; + + FileType(String typeName, ArrayList list) { + this.typeName = typeName; + this.typeExtensions = list; + } + + public static FileType getFileType(String extension) { + for (FileType fileType : values()) { + if (fileType.typeExtensions.contains(extension.toLowerCase())) { + return fileType; + } + } + return NONE; + } + + public String getTypeName() { + return typeName; + } + + +} diff --git a/Lab3/src/main/java/org/lumijiez/util/StateType.java b/Lab3/src/main/java/org/lumijiez/enums/StateType.java similarity index 50% rename from Lab3/src/main/java/org/lumijiez/util/StateType.java rename to Lab3/src/main/java/org/lumijiez/enums/StateType.java index 66483ab..ee0a836 100644 --- a/Lab3/src/main/java/org/lumijiez/util/StateType.java +++ b/Lab3/src/main/java/org/lumijiez/enums/StateType.java @@ -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; } } diff --git a/Lab3/src/main/java/org/lumijiez/gui/MainFrame.java b/Lab3/src/main/java/org/lumijiez/gui/MainFrame.java index 0d31c42..ec83d41 100644 --- a/Lab3/src/main/java/org/lumijiez/gui/MainFrame.java +++ b/Lab3/src/main/java/org/lumijiez/gui/MainFrame.java @@ -1,81 +1,81 @@ package org.lumijiez.gui; +import org.lumijiez.base.Document; 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 JList fileList = new JList<>(); + private final JScrollPane fileInfoScrollPane = new JScrollPane(); 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(); - private final JMenuBar MainMenubar = new JMenuBar(); + 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 fileContents = new HashMap<>(); + private final JList fileList = new JList<>(); + private final Map fileContents = new HashMap<>(); private TrackerThread tracker; - public MainFrame() throws IOException { + public MainFrame() { initComponents(); } private void initComponents() { + setResizable(false); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + mainTextPane.setContentType("text/html"); + fileInfoTextPane.setContentType("text/html"); + + mainTextPane.setEditable(false); + fileInfoTextPane.setEditable(false); 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); + if (folderChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { + FOLDER_PATH = Path.of(folderChooser.getSelectedFile().getAbsolutePath()); + tracker = new TrackerThread(mainTextPane, fileContents, fileList, fileInfoTextPane); tracker.start(); } + snapshotLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18)); + pathLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18)); + 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); + fileInfoScrollPane.setViewportView(fileInfoTextPane); - 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(fileMenu); + mainMenubar.add(settingsMenu); - MainMenubar.add(settingsMenu); + StatusButton.setText("Status"); + pathLabel.setText("Currently tracking: " + FOLDER_PATH.toString()); + CommitButton.setText("Commit"); + snapshotLabel.setText("Last snapshot: " + new Date()); + fileMenu.setText("File"); + pickFolder.setText("Pick another folder"); + settingsMenu.setText("Edit"); + settings.setText("Settings"); - setJMenuBar(MainMenubar); + setJMenuBar(mainMenubar); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); @@ -84,35 +84,31 @@ 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)))) - .addContainerGap()) - ); + .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(); } @@ -122,7 +118,4 @@ public class MainFrame extends JFrame { mainTextPane.setText(""); snapshotLabel.setText("Last snapshot: " + new Date()); } - - private void StatusButtonActionPerformed(java.awt.event.ActionEvent evt) { - } } diff --git a/Lab3/src/main/java/org/lumijiez/interfaces/IDocument.java b/Lab3/src/main/java/org/lumijiez/interfaces/IDocument.java new file mode 100644 index 0000000..05f2f54 --- /dev/null +++ b/Lab3/src/main/java/org/lumijiez/interfaces/IDocument.java @@ -0,0 +1,12 @@ +package org.lumijiez.interfaces; + +import org.lumijiez.enums.FileType; + +public interface IDocument { + String getExtension(); + String getCreatedTime(); + String getModificationTime(); + FileType getFileType(); + long getFilesizeKB(); + String getInfo(); +} diff --git a/Lab3/src/main/java/org/lumijiez/tracker/TrackerThread.java b/Lab3/src/main/java/org/lumijiez/tracker/TrackerThread.java index b50045b..c281f9a 100644 --- a/Lab3/src/main/java/org/lumijiez/tracker/TrackerThread.java +++ b/Lab3/src/main/java/org/lumijiez/tracker/TrackerThread.java @@ -1,46 +1,59 @@ package org.lumijiez.tracker; -import org.lumijiez.util.DiffType; +import org.lumijiez.base.Document; +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 org.lumijiez.util.FileFactory; +import org.lumijiez.util.NotificationHandler; import javax.swing.*; +import java.awt.*; 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 fileContents; - private final JList fileList; + private final JTextPane fileInfoTextPane; + private final JList fileList; + private Map fileContents; private final Map fileStates = new HashMap<>(); - public TrackerThread(JTextPane textPane, Path path, Map files, JList fileList) { + public TrackerThread(JTextPane textPane, Map files, JList fileList, JTextPane fileInfoTextPane) { this.textPane = textPane; - this.path = path; this.fileContents = files; this.fileList = fileList; + this.fileInfoTextPane = fileInfoTextPane; init(); } public void init() { System.out.println("Init called"); - fileContents = FileDiffer.crawlDirectory(path); - - ArrayList fileNames = new ArrayList<>(); + fileContents = FileDiffer.crawlDirectory(MainFrame.FOLDER_PATH); + DefaultListModel listModel = new DefaultListModel<>(); for (File file : fileContents.keySet()) { - fileNames.add(file.getName()); + Document doc = FileFactory.getDocument(file.toPath()); + listModel.addElement(doc); } - fileList.setModel(new AbstractListModel<>() { - public int getSize() { - return fileNames.size(); + + fileList.setModel(listModel); + fileList.setCellRenderer(new DefaultListCellRenderer() { + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + if (value instanceof Document) { + value = ((Document) value).getName(); + } + return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); } - public String getElementAt(int i) { - return fileNames.get(i); + }); + + fileList.addListSelectionListener(e -> { + if (!e.getValueIsAdjusting()) { + refreshFileInfo(); } }); } @@ -49,41 +62,47 @@ public class TrackerThread extends Thread { fileStates.clear(); } + public void refreshFileInfo() { + Document selectedDocument = fileList.getSelectedValue(); + if (selectedDocument != null) { + fileInfoTextPane.setText(selectedDocument.getInfo()); + } else { + fileInfoTextPane.setText(""); + } + } + + public void checkDirectory() { - Map> result = FileDiffer.diff(fileContents, FileDiffer.crawlDirectory(path)); + Map> 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(); + refreshFileInfo(); for (File file : fileStates.keySet()) { if (fileStates.get(file) != StateType.NONE) { - toShow.append(file.getName()).append(" has been ").append(fileStates.get(file).getName()).append("
"); + if (fileStates.get(file) == StateType.NEW) { + toShow.append(""); + } + if (fileStates.get(file) == StateType.DELETED) { + toShow.append(""); + } + if (fileStates.get(file) == StateType.MODIFIED) { + toShow.append(""); + } + toShow.append(file.getName()).append(" has been ").append(fileStates.get(file).getAction()).append("
"); + NotificationHandler.showNotification(file.getName(), fileStates.get(file)); } } textPane.setText(toShow.toString()); @@ -94,7 +113,6 @@ public class TrackerThread extends Thread { public void run() { while(this.isAlive()) { checkDirectory(); - //Thread.sleep(200); } } } diff --git a/Lab3/src/main/java/org/lumijiez/util/DiffType.java b/Lab3/src/main/java/org/lumijiez/util/DiffType.java deleted file mode 100644 index 2be9dfd..0000000 --- a/Lab3/src/main/java/org/lumijiez/util/DiffType.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.lumijiez.util; - -public enum DiffType { - CREATE, DELETE, MODIFY, NONE -} diff --git a/Lab3/src/main/java/org/lumijiez/util/FileDiffer.java b/Lab3/src/main/java/org/lumijiez/util/FileDiffer.java index 5316fe5..60e3844 100644 --- a/Lab3/src/main/java/org/lumijiez/util/FileDiffer.java +++ b/Lab3/src/main/java/org/lumijiez/util/FileDiffer.java @@ -1,6 +1,8 @@ package org.lumijiez.util; -import java.io.File; +import org.lumijiez.base.Document; +import org.lumijiez.enums.DiffType; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -11,43 +13,39 @@ import java.util.Map; import java.util.stream.Stream; public class FileDiffer { - public static Map> diff(Map oldFiles, Map newFiles) { - Map> result = new HashMap<>(); + public static Map> diff(Map oldFiles, Map newFiles) { + Map> result = Map.of( + DiffType.CREATE, new ArrayList<>(), + DiffType.DELETE, new ArrayList<>(), + DiffType.MODIFY, new ArrayList<>()); - ArrayList newFileList = new ArrayList<>(); - ArrayList deletedFileList = new ArrayList<>(); - ArrayList modifiedFileList = new ArrayList<>(); - - for (File file : oldFiles.keySet()) { + for (Document 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()) { + for (Document 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; } - public static Map crawlDirectory(Path path) { - Map newFileContents = new HashMap<>(); + public static Map crawlDirectory(Path path) { + Map newFileContents = new HashMap<>(); try (Stream paths = Files.walk(path)) { paths.forEach(p -> { - if (Files.isRegularFile(p)) { + Document doc = new Document(p); + if (Files.isRegularFile(doc.toPath())) { try { - newFileContents.put(p.toFile(), Files.readAllBytes(p)); + newFileContents.put(doc, Files.readAllBytes(doc.toPath())); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/Lab3/src/main/java/org/lumijiez/util/FileFactory.java b/Lab3/src/main/java/org/lumijiez/util/FileFactory.java new file mode 100644 index 0000000..a0bcf29 --- /dev/null +++ b/Lab3/src/main/java/org/lumijiez/util/FileFactory.java @@ -0,0 +1,26 @@ +package org.lumijiez.util; + +import org.lumijiez.base.*; + +import java.nio.file.Path; + +public class FileFactory { + public static Document getDocument(Path path) { + Document doc = new Document(path); + switch (doc.getFileType()) { + case PLAINTEXT -> { + return new TextFile(path); + } + case IMAGE -> { + return new ImageFile(path); + } + case FILE -> { + return new ArbitraryFile(path); + } + case CODE -> { + return new CodeFile(path); + } + } + return doc; + } +} diff --git a/Lab3/src/main/java/org/lumijiez/util/NotificationHandler.java b/Lab3/src/main/java/org/lumijiez/util/NotificationHandler.java new file mode 100644 index 0000000..f0806e1 --- /dev/null +++ b/Lab3/src/main/java/org/lumijiez/util/NotificationHandler.java @@ -0,0 +1,23 @@ +package org.lumijiez.util; + +import org.lumijiez.enums.StateType; + +import java.awt.*; +import java.awt.TrayIcon.MessageType; + +public class NotificationHandler { + public static void showNotification(String filename, StateType stateType) { + try { + SystemTray tray = SystemTray.getSystemTray(); + Image image = Toolkit.getDefaultToolkit().createImage("some-icon.png"); + + TrayIcon trayIcon = new TrayIcon(image, "Java AWT Tray Demo"); + trayIcon.setImageAutoSize(true); + trayIcon.setToolTip("File Tracker"); + tray.add(trayIcon); + trayIcon.displayMessage("File Tracker", filename + " has been " + stateType.getAction(), MessageType.INFO); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/Lab3/src/main/java/org/lumijiez/util/Utils.java b/Lab3/src/main/java/org/lumijiez/util/Utils.java new file mode 100644 index 0000000..2a6f6bc --- /dev/null +++ b/Lab3/src/main/java/org/lumijiez/util/Utils.java @@ -0,0 +1,92 @@ +package org.lumijiez.util; + +import org.lumijiez.base.Document; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class Utils { + public static int countWords(Document doc) { + try { + BufferedReader reader = new BufferedReader(new FileReader(doc)); + int wordCount = 0; + String line; + while ((line = reader.readLine()) != null) { + String[] words = line.split("\\s+"); + wordCount += words.length; + } + reader.close(); + return wordCount; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static int countLines(Document doc) { + try { + BufferedReader reader = new BufferedReader(new FileReader(doc)); + int lineCount = 0; + while (reader.readLine() != null) lineCount++; + reader.close(); + return lineCount; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static long countChars(Document doc) { + try { + BufferedReader reader = new BufferedReader(new FileReader(doc)); + long characterCount = 0; + while (reader.read() != -1) characterCount++; + reader.close(); + return characterCount; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static int countClasses(Document doc) { + try { + int classCount = 0; + + BufferedReader reader = new BufferedReader(new FileReader(doc)); + String line; + + while ((line = reader.readLine()) != null) { + if (line.contains("class ")) { + classCount++; + } + } + reader.close(); + return classCount; + } catch (IOException e) { + e.printStackTrace(); + } + return 0; + } + + public static int countMethods(Document doc) { + try { + int methodCount = 0; + + BufferedReader reader = new BufferedReader(new FileReader(doc)); + String line; + + while ((line = reader.readLine()) != null) { + if (line.matches(".*\\b\\w+\\s+\\w+\\(.*\\)\\s*\\{.*")) { + methodCount++; + } else if (line.contains("def ")) { + methodCount++; + } + } + + reader.close(); + return methodCount; + } catch (IOException e) { + e.printStackTrace(); + } + return 0; + } +}