Commit a6712988 authored by Tatyana's avatar Tatyana 👽

code reduces ::: AdminController splitted into specialized ones

parent 7954bfb2
package application.controller;
import application.dto.RecordMap;
import application.entity.Record;
import application.util.NotificationType;
import application.util.RecordLoader;
import com.jfoenix.controls.JFXTextArea;
import com.jfoenix.controls.JFXTextField;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import org.apache.commons.codec.digest.DigestUtils;
import java.io.IOException;
public class FxAddSignatureController extends FxAdminController {
@FXML
private JFXTextArea body;
@FXML
private JFXTextArea preview;
@FXML
private JFXTextField name;
private String encode(ActionEvent event) {
return DigestUtils.md5Hex(body.getText());
}
public void generateSignature(ActionEvent event) throws IOException {
String encodedBody = encode(event);
JFXTextField extField = (JFXTextField) scene.lookup("#extension");
JFXTextField offsetField = (JFXTextField) scene.lookup("#offset");
JFXTextField prefaceField = (JFXTextField) scene.lookup("#preface");
if (!offsetField.getText().matches("\\d+-\\d+")) {
setFocusColor(offsetField, true);
notificate("Malformed input!", NotificationType.ERROR);
return;
} else {
setFocusColor(offsetField, false);
}
String signature = prefaceField.getText()
+ ":" + encodedBody
+ ":" + 10
+ ":" + 20
+ ":" + 30
+ ":" + extField.getText()
+ ":" + name.getText();
preview.setText(signature);
notificate("Signature created", NotificationType.SUCCESS);
Record newRecord = Record.getInstance(signature);
RecordMap map = RecordMap.getInstance();
map.addRecord(newRecord.getPreface(), newRecord);
RecordLoader.unloadRecord();
}
}
package application.controller;
import application.dto.RecordMap;
import application.entity.Record;
import application.util.FilePreparer;
import application.util.FileSearcher;
import application.util.NotificationType;
import application.util.RecordLoader;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextArea;
import com.jfoenix.controls.JFXTextField;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
......@@ -26,25 +21,34 @@ import javafx.scene.control.ListView;
import javafx.scene.paint.Color;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import org.apache.commons.codec.digest.DigestUtils;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.stream.Collectors;
public class FxAdminController extends FxController {
private Parent parent;
private Scene scene;
private Stage window;
protected Parent parent;
protected Scene scene;
protected Stage window;
private boolean isPE = false;
private FileSearcher fileSearcher;
@FXML
private Label filesFound;
private final DirectoryChooser directoryChooser = new DirectoryChooser();
@FXML
private void updateFilesFoundLabel(int total) {
filesFound.setText(String.valueOf(total));
}
public void loadAddSignature(ActionEvent event) throws IOException {
deselectHyperLink(event);
loadUI(event, "templates/av_add_signature.fxml");
......@@ -82,19 +86,12 @@ public class FxAdminController extends FxController {
setStageAndScene(event);
}
private void deselectHyperLink(ActionEvent event) {
protected void deselectHyperLink(ActionEvent event) {
Hyperlink hyperlink = (Hyperlink) event.getSource();
hyperlink.setVisited(false);
}
private String encode(ActionEvent event) {
JFXButton button = (JFXButton) event.getSource();
scene = button.getScene();
JFXTextArea body = (JFXTextArea) scene.lookup("#body");
return DigestUtils.md5Hex(body.getText());
}
private void setFocusColor(Control control, boolean malformed) {
protected void setFocusColor(Control control, boolean malformed) {
if (control instanceof JFXTextField) {
JFXTextField textField = (JFXTextField) control;
if (malformed) {
......@@ -107,39 +104,6 @@ public class FxAdminController extends FxController {
}
}
public void generateSignature(ActionEvent event) throws IOException {
String encodedBody = encode(event);
JFXTextArea preview = (JFXTextArea) scene.lookup("#preview");
JFXTextField nameField = (JFXTextField) scene.lookup("#name");
JFXTextField extField = (JFXTextField) scene.lookup("#extension");
JFXTextField offsetField = (JFXTextField) scene.lookup("#offset");
JFXTextField prefaceField = (JFXTextField) scene.lookup("#preface");
if (!offsetField.getText().matches("\\d+-\\d+")) {
setFocusColor(offsetField, true);
notificate("Malformed input!", NotificationType.ERROR);
return;
} else {
setFocusColor(offsetField, false);
}
String signature = prefaceField.getText()
+ ":" + encodedBody
+ ":" + 10
+ ":" + 20
+ ":" + 30
+ ":" + extField.getText()
+ ":" + nameField.getText();
preview.setText(signature);
notificate("Signature created", NotificationType.SUCCESS);
Record newRecord = Record.getInstance(signature);
RecordMap map = RecordMap.getInstance();
map.addRecord(newRecord.getPreface(), newRecord);
RecordLoader.unloadRecord();
}
public void clear(ActionEvent event) {
Hyperlink button = (Hyperlink) event.getSource();
scene = button.getScene();
......@@ -147,22 +111,6 @@ public class FxAdminController extends FxController {
dirpathNameField.setText(null);
}
public void loadDatabase(ActionEvent event) throws IOException, URISyntaxException {
JFXButton button = (JFXButton) event.getSource();
scene = button.getScene();
ListView<String> list = (ListView<String>) scene.lookup("#list");
RecordMap map = RecordLoader.loadRecords();
List<String> signatures = new ArrayList<>();
for (Record s : map.getRecords().values()) {
signatures.add(s.toString());
}
ObservableList<String> items = FXCollections.observableArrayList(signatures);
list.setItems(items);
}
public void browse(ActionEvent event) {
deselectHyperLink(event);
Hyperlink button = (Hyperlink) event.getSource();
......@@ -195,9 +143,11 @@ public class FxAdminController extends FxController {
}
File root = new File(dirpathNameField.getText());
Timer timer = new Timer("Label Files Found Timer");
if (root.exists()) {
FileSearcher fileSearcher = FileSearcher.getInstance();
fileSearcher = FileSearcher.getInstance();
Thread fileSearcherThread = new Thread(() -> {
List<File> files = fileSearcher.prepareFiles(root);
List<File> peFiles;
......@@ -212,15 +162,28 @@ public class FxAdminController extends FxController {
files.addAll(peFiles);
}
List<String> filesItems = files.stream().map(file -> file.toString()).collect(Collectors.toList());
Platform.runLater(() -> {
List<String> filesItems = files.stream().map(File::toString).collect(Collectors.toList());
ObservableList<String> items = FXCollections.observableArrayList(filesItems);
preparedFilesList.setItems(items);
timer.cancel();
});
});
fileSearcherThread.setName("File Searcher");
fileSearcherThread.setDaemon(true);
fileSearcherThread.start();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Platform.runLater(() -> {
updateFilesFoundLabel(fileSearcher.getTotal());
});
}
}, 0, 250);
} else {
notificate("Invalid dirpath!", NotificationType.ERROR);
}
......
package application.controller;
import application.dto.RecordMap;
import application.entity.Record;
import application.util.RecordLoader;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
public class FxLoadDbController extends FxAdminController {
@FXML
private ListView database;
public void loadDatabase(ActionEvent event) throws IOException, URISyntaxException {
RecordMap map = RecordLoader.loadRecords();
List<String> signatures = new ArrayList<>();
for (Record s : map.getRecords().values()) {
signatures.add(s.toString());
}
ObservableList<String> items = FXCollections.observableArrayList(signatures);
database.setItems(items);
}
}
......@@ -10,7 +10,13 @@ public class FileSearcher {
private FileMonitor monitor;
private List<File> monitoredFiles = new ArrayList<>();
public List<File> prepareFiles(File root) {
//private List<File> foundedFiles = new ArrayList<>();
private int filesFound;
public List<File> prepareFiles(File root) {
filesFound = 0;
//foundedFiles.clear();
monitor = new FileMonitor();
monitor.start();
monitor.registerDir(root);
......@@ -30,6 +36,7 @@ public class FileSearcher {
files.addAll(scanDirectory(file));
} else {
files.add(file);
filesFound++;
}
}
}
......@@ -53,4 +60,17 @@ public class FileSearcher {
public void removeFile(File file) {
monitoredFiles.remove(file);
}
public int getTotal() {
return filesFound;
}
/*public List<File> getFoundedFiles() {
return foundedFiles;
}
public void setFoundedFiles(List<File> foundedFiles) {
this.foundedFiles = foundedFiles;
}*/
}
......@@ -14,7 +14,7 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="600.0" prefWidth="1024.0" styleClass="parent" stylesheets="@../styles/av.css" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.controller.FxAdminController">
<AnchorPane prefHeight="600.0" prefWidth="1024.0" styleClass="parent" stylesheets="@../styles/av.css" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.controller.FxAddSignatureController">
<VBox alignment="TOP_CENTER" prefHeight="600.0" prefWidth="300.0" styleClass="sidebar" stylesheets="@../styles/uv.css">
<Label prefHeight="75.0" prefWidth="300.0" style="-fx-background-color: #00A2D3;" text="Admin Panel" textFill="WHITE">
<padding>
......
......@@ -13,7 +13,7 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="600.0" prefWidth="1024.0" styleClass="parent" stylesheets="@../styles/av.css" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.controller.FxAdminController">
<AnchorPane prefHeight="600.0" prefWidth="1024.0" styleClass="parent" stylesheets="@../styles/av.css" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.controller.FxLoadDbController">
<VBox alignment="TOP_CENTER" prefHeight="600.0" prefWidth="300.0" styleClass="sidebar" stylesheets="@../styles/uv.css">
<Label prefHeight="75.0" prefWidth="300.0" style="-fx-background-color: #00A2D3;" text="Admin Panel" textFill="WHITE">
<padding>
......@@ -132,7 +132,7 @@
<Font name="Calibri Light" size="16.0" />
</font>
</JFXButton>
<JFXListView fx:id="list" layoutX="14.0" layoutY="14.0" prefHeight="212.0" prefWidth="518.0" />
<JFXListView fx:id="database" layoutX="14.0" layoutY="14.0" prefHeight="212.0" prefWidth="518.0" />
</Pane>
</VBox>
</AnchorPane>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment