Last active
April 26, 2016 11:37
-
-
Save inaz2/82f486fef259e9e9603d946875e3df2e to your computer and use it in GitHub Desktop.
SHA-1 Calculator FXML (JavaFX+FXML) / http://inaz2.hatenablog.com/entry/2016/04/26/203503
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package application; | |
import javafx.beans.property.SimpleStringProperty; | |
public class FileInfo { | |
private final SimpleStringProperty path = new SimpleStringProperty(); | |
private final SimpleStringProperty sha1sum = new SimpleStringProperty(); | |
public FileInfo(String path, String sha1sum) { | |
setPath(path); | |
setSha1sum(sha1sum); | |
} | |
public String getPath() { | |
return path.get(); | |
} | |
public void setPath(String newPath) { | |
path.set(newPath); | |
} | |
public String getSha1sum() { | |
return sha1sum.get(); | |
} | |
public void setSha1sum(String newSha1sum) { | |
sha1sum.set(newSha1sum); | |
} | |
public SimpleStringProperty sha1sumProperty() { | |
return sha1sum; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package application; | |
import javafx.application.Application; | |
import javafx.stage.Stage; | |
import javafx.scene.Scene; | |
import javafx.scene.layout.BorderPane; | |
import javafx.fxml.FXMLLoader; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) { | |
try { | |
primaryStage.setTitle("SHA-1 Calculator FXML"); | |
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("MainWindow.fxml")); | |
Scene scene = new Scene(root,600,400); // expanded default size | |
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.collections.FXCollections?> | |
<?import javafx.scene.control.TableColumn?> | |
<?import javafx.scene.control.TableView?> | |
<?import javafx.scene.control.cell.PropertyValueFactory?> | |
<?import javafx.scene.layout.BorderPane?> | |
<BorderPane xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainWindowController"> | |
<center> | |
<TableView fx:id="tableView" editable="true" onDragDropped="#handleDragDropped" onDragOver="#handleDragOver"> | |
<columnResizePolicy> | |
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /> | |
</columnResizePolicy> | |
<columns> | |
<TableColumn fx:id="filePathCol" text="File Path"> | |
<cellValueFactory><PropertyValueFactory property="path" /></cellValueFactory> | |
</TableColumn> | |
<TableColumn fx:id="sha1sumCol" text="sha1sum"> | |
<cellValueFactory><PropertyValueFactory property="sha1sum" /></cellValueFactory> | |
</TableColumn> | |
</columns> | |
<items> | |
<FXCollections fx:factory="observableArrayList" /> | |
</items> | |
</TableView> | |
</center> | |
</BorderPane> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package application; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.Arrays; | |
import javafx.collections.ObservableList; | |
import javafx.concurrent.Task; | |
import javafx.fxml.FXML; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.cell.TextFieldTableCell; | |
import javafx.scene.input.DragEvent; | |
import javafx.scene.input.Dragboard; | |
import javafx.scene.input.TransferMode; | |
public class MainWindowController { | |
@FXML private TableView<FileInfo> tableView; | |
@FXML private TableColumn<FileInfo, String> filePathCol; | |
@FXML private TableColumn<FileInfo, String> sha1sumCol; | |
@FXML private void initialize() { | |
filePathCol.setCellFactory(TextFieldTableCell.forTableColumn()); | |
sha1sumCol.setCellFactory(TextFieldTableCell.forTableColumn()); | |
} | |
public void handleDragOver(DragEvent event) { | |
Dragboard db = event.getDragboard(); | |
if (db.hasFiles()) { | |
event.acceptTransferModes(TransferMode.COPY); | |
} else { | |
event.consume(); | |
} | |
} | |
public void handleDragDropped(DragEvent event) { | |
ObservableList<FileInfo> data = tableView.getItems(); | |
Dragboard db = event.getDragboard(); | |
boolean success = false; | |
if (db.hasFiles()) { | |
success = true; | |
for (File file : db.getFiles()) { | |
FileInfo fileinfo = new FileInfo(file.getAbsolutePath(), ""); | |
data.add(fileinfo); | |
SHA1CalculationTask task = new SHA1CalculationTask(fileinfo); | |
fileinfo.sha1sumProperty().bind(task.messageProperty()); | |
new Thread(task).start(); | |
} | |
} | |
event.setDropCompleted(success); | |
event.consume(); | |
} | |
class SHA1CalculationTask extends Task<Void> { | |
private final FileInfo fileinfo; | |
public SHA1CalculationTask(FileInfo fileinfo) { | |
this.fileinfo = fileinfo; | |
} | |
@Override | |
protected Void call() throws Exception { | |
MessageDigest md = null; | |
try { | |
md = MessageDigest.getInstance("SHA-1"); | |
} catch (NoSuchAlgorithmException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
// update digest with each 8MB chunk | |
String path = fileinfo.getPath(); | |
long totalBytes = (new File(path)).length(); | |
long currentBytes = 0; | |
try (FileInputStream istream = new FileInputStream(path)) { | |
byte[] buf = new byte[8*1024*1024]; | |
int n; | |
while ((n = istream.read(buf, 0, buf.length)) != -1) { | |
md.update(Arrays.copyOf(buf, n)); | |
currentBytes += n; | |
String message = String.format("calculating %d%%...", 100 * currentBytes / totalBytes); | |
updateMessage(message); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
byte[] digest = md.digest(); | |
StringBuilder sb = new StringBuilder(); | |
for (byte b : digest) { | |
sb.append(String.format("%02x", b)); | |
} | |
updateMessage(sb.toString()); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment