Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Diff view to use a HTMLView to display diff in colorful format (Issue #73) #77

Merged
merged 7 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 65 additions & 3 deletions src/main/java/io/github/gitfx/controller/GitFxController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.github.gitfx.data.GitRepoMetaData;
import io.github.gitfx.data.ProjectData;
import io.github.gitfx.data.RepositoryData;
import io.github.gitfx.util.GitCreateHtmlPage;
import io.github.gitfx.util.GitFXGsonUtil;
import io.github.gitfx.util.WorkbenchUtil;
import java.io.File;
Expand All @@ -41,13 +42,24 @@
//import javafx.event.EventHandler;
//import javafx.event.Event;
import javafx.fxml.Initializable;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
//import javafx.scene.effect.BlendMode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
//import javafx.scene.layout.Background;
//import javafx.scene.layout.Region;
//import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.util.Pair;
import org.eclipse.jgit.api.errors.GitAPIException;

Expand All @@ -56,6 +68,11 @@
//[LOG] import org.slf4j.Logger;
//[LOG] import org.slf4j.LoggerFactory;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

@Loggable
public class GitFxController implements Initializable {
Expand Down Expand Up @@ -89,7 +106,11 @@ public class GitFxController implements Initializable {
@FXML
private AnchorPane diffContainer;
@FXML
private TextArea diffArea;
private Group webViewGroup;





//[DOUBT] Shouldn't we either remove this or the duplicate declaration in the addProgressIndiactor()
private ProgressIndicator pi;
Expand Down Expand Up @@ -222,8 +243,9 @@ public void changed(ObservableValue<? extends TitledPane> observable, TitledPane
if(newValue!=null) {
try {
//[LOG] logger.debug("Accordion expanded with oldvalue" + newValue.getId());
diffArea.setWrapText(true);
diffArea.setText(metaData.getDiffBetweenCommits(Integer.parseInt(newValue.getId())));
String diffData = metaData.getDiffBetweenCommits(Integer.parseInt(newValue.getId()));
GitCreateHtmlPage.parseDiffData(diffData);
webViewGroup.getChildren().addAll(new Browser());
}catch(GitAPIException | IOException ex){
//[LOG] logger.debug("Something went wrong in getting commit history");
}
Expand Down Expand Up @@ -381,3 +403,43 @@ public void onGitParticularRepositoryClicked(ActionEvent event) {
}

}

class Browser extends Region {

final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();

public Browser() {
//apply the styles
getStyleClass().add("browser");
// load the web page
String projectPath = System.getProperty("user.dir");
String tempFile = projectPath + File.separator+ "src\\main\\resources\\diffWebViewHtmlPage.html";
System.out.println(tempFile);
webEngine.load("file:///" + tempFile );
//add the web view to the scene
ScrollPane scrollPane = new ScrollPane();
// scrollPane.setContent(browser);
getChildren().addAll(browser,scrollPane);

}
private Node createSpacer() {
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
return spacer;
}

@Override protected void layoutChildren() {
double w = getWidth();
double h = getHeight();
layoutInArea(browser,0,0,w,h,0, HPos.CENTER, VPos.CENTER);
}

@Override protected double computePrefWidth(double height) {
return 750;
}

@Override protected double computePrefHeight(double width) {
return 500;
}
}
3 changes: 3 additions & 0 deletions src/main/java/io/github/gitfx/data/GitRepoMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;


import com.jcabi.aspects.Loggable;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -156,6 +157,8 @@ public String getDiffBetweenCommits(int commitIndex) throws IOException,GitAPIEx
formatter.setRepository(repository);
formatter.format(entry);
}
// System.out.println(byteStream.toString());
String diffContent = byteStream.toString();
return byteStream.toString();
}

Expand Down
89 changes: 66 additions & 23 deletions src/main/java/io/github/gitfx/dialog/GitFxDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,25 @@
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.stage.DirectoryChooser;
import javafx.util.Duration;
import javafx.util.Pair;
Expand Down Expand Up @@ -153,6 +161,7 @@ public void gitExceptionDialog(String title, String header, String content, Exce
alert.getDialogPane().setExpandableContent(expContent);
alert.showAndWait();
}

/*
* Implementation of Git Open Dialog.
*/
Expand All @@ -162,34 +171,54 @@ public String gitOpenDialog(String title, String header, String content) {
String repo;
DirectoryChooser chooser = new DirectoryChooser();
Dialog<Pair<String, GitFxDialogResponse>> dialog = new Dialog<>();
//Dialog<Pair<String, GitFxDialogResponse>> dialog = getCostumeDialog();
dialog.setTitle(title);
dialog.setHeaderText(header);
dialog.setContentText(content);

ButtonType chooseButtonType = new ButtonType("Choose");
ButtonType okButton = new ButtonType("Ok");
ButtonType cancelButton = new ButtonType("Cancel");

dialog.getDialogPane().getButtonTypes().addAll(chooseButtonType, okButton, cancelButton);

dialog.getDialogPane().getButtonTypes().addAll(okButton, cancelButton);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));

TextField repository = new TextField();
repository.setPrefWidth(250.0);
repository.setPromptText("Repo");
grid.add(new Label("Repository:"), 0, 0);
grid.add(repository, 1, 0);

dialog.getDialogPane().setContent(grid);

dialog.setResultConverter(dialogButton -> {
if (dialogButton == chooseButtonType) {
File path = chooser.showDialog(dialog.getOwner());

/////////////////////Modification of choose Button////////////////////////
Button chooseButtonType = new Button("Choose");
grid.add(chooseButtonType, 2, 0);
// Button btnCancel1 = new Button("Cancel");
// btnCancel1.setPrefWidth(90.0);
// Button btnOk1 = new Button("Ok");
// btnOk1.setPrefWidth(90.0);
// HBox hbox = new HBox(4);
// hbox.getChildren().addAll(btnOk1,btnCancel1);
// hbox.setPadding(new Insets(2));
// grid.add(hbox, 1, 1);
chooseButtonType.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
// label.setText("Accepted");
File path = chooser.showDialog(dialog.getOwner());
getFileAndSeText(repository,path);
chooser.setTitle(resourceBundle.getString("repo"));
}
});
// btnCancel1.setOnAction(new EventHandler<ActionEvent>(){
// @Override public void handle(ActionEvent e) {
// System.out.println("Shyam : Testing");
// setResponse(GitFxDialogResponse.CANCEL);
// }
// });
//////////////////////////////////////////////////////////////////////
dialog.getDialogPane().setContent(grid);

dialog.setResultConverter(dialogButton -> {
if (dialogButton == okButton) {
String filePath = repository.getText();
//filePath = filePath.concat("/.git");
Expand Down Expand Up @@ -246,22 +275,29 @@ public Pair<String, String> gitInitDialog(String title, String header, String co
grid.setPadding(new Insets(20, 150, 10, 10));
TextField localPath = new TextField();
localPath.setPromptText("Local Path");
localPath.setPrefWidth(250.0);
grid.add(new Label("Local Path:"), 0, 0);
grid.add(localPath, 1,0);
ButtonType chooseButtonType = new ButtonType("Choose");
ButtonType initRepo = new ButtonType("Initialize Repository");
ButtonType cancelButtonType = new ButtonType("Cancel");
dialog.getDialogPane().setContent(grid);
dialog.getDialogPane().getButtonTypes().addAll(chooseButtonType, initRepo, cancelButtonType);

dialog.setResultConverter(dialogButton -> {
if (dialogButton == chooseButtonType) {
dialog.getDialogPane().getButtonTypes().addAll( initRepo, cancelButtonType);

/////////////////////Modification of choose Button////////////////////////
Button chooseButtonType = new Button("Choose");
grid.add(chooseButtonType, 2, 0);
chooseButtonType.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
DirectoryChooser chooser = new DirectoryChooser();
chooser.setTitle(resourceBundle.getString("selectRepo"));
File initialRepo = chooser.showDialog(dialog.getOwner());
getFileAndSeText(localPath, initialRepo);
dialog.getDialogPane();
}
});
//////////////////////////////////////////////////////////////////////

dialog.setResultConverter(dialogButton -> {
if (dialogButton == initRepo) {
setResponse(GitFxDialogResponse.INITIALIZE);
String path = localPath.getText();
Expand Down Expand Up @@ -303,26 +339,33 @@ public Pair<String, String> gitCloneDialog(String title, String header,
grid.setPadding(new Insets(20, 150, 10, 10));
TextField remoteRepo = new TextField();
remoteRepo.setPromptText("Remote Repo URL");
remoteRepo.setPrefWidth(250.0);
TextField localPath = new TextField();
localPath.setPromptText("Local Path");
localPath.setPrefWidth(250.0);
grid.add(new Label("Remote Repo URL:"), 0, 0);
grid.add(remoteRepo, 1, 0);
grid.add(new Label("Local Path:"), 0, 1);
grid.add(localPath, 1, 1);
ButtonType chooseButtonType = new ButtonType("Choose");
//ButtonType chooseButtonType = new ButtonType("Choose");
ButtonType cloneButtonType = new ButtonType("Clone");
ButtonType cancelButtonType = new ButtonType("Cancel");
/////////////////////Modification of choose Button////////////////////////
Button chooseButtonType = new Button("Choose");
grid.add(chooseButtonType, 2, 1);
chooseButtonType.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
chooser.setTitle(resourceBundle.getString("cloneRepo"));
File cloneRepo = chooser.showDialog(dialog.getOwner());
getFileAndSeText(localPath, cloneRepo);
}
});
//////////////////////////////////////////////////////////////////////
dialog.getDialogPane().setContent(grid);
dialog.getDialogPane().getButtonTypes().addAll(chooseButtonType,
dialog.getDialogPane().getButtonTypes().addAll(
cloneButtonType, cancelButtonType);

dialog.setResultConverter(dialogButton -> {
if (dialogButton == chooseButtonType) {
//[LOG] logger.debug("Choose clicked");
chooser.setTitle(resourceBundle.getString("cloneRepo"));
File cloneRepo = chooser.showDialog(dialog.getOwner());
getFileAndSeText(localPath, cloneRepo);
}
if (dialogButton == cloneButtonType) {
setResponse(GitFxDialogResponse.CLONE);
String remoteRepoURL = remoteRepo.getText();
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/io/github/gitfx/util/GitCreateHtmlPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.github.gitfx.util;

import java.io.*;

public class GitCreateHtmlPage {

public static void parseDiffData(String diffData)
{
try {
//define a HTML String Builder
diffData = diffData.replaceAll("<", "&lt;");
diffData = diffData.replaceAll(">", "&gt;");
String textStr[] = diffData.split("\\r?\\n");
System.out.println("Number of lines: " + textStr.length);
StringBuilder htmlStringBuilder=new StringBuilder();
//append html header and title
htmlStringBuilder.append("<html><head><title>Selenium Test </title></head>");
//append body
htmlStringBuilder.append("<body style=\"font-size:70%;font-family:courier;\">");
//append table
for(String line : textStr) {
if(line.length()>0) {
char start = line.charAt(0) ;
if(start=='-') {
htmlStringBuilder.append("<div style=\"background-color:rgb(255, 163, 163);\"> " + line + "</div>");
}
else if(start=='+') {
htmlStringBuilder.append("<div style=\"background-color:rgb(66, 244, 158);\"> " + line + "</div>");
}
else {
htmlStringBuilder.append("<div> " + line + "</div>");
}
}

}
htmlStringBuilder.append("</html>");
//write html string content to a file
WriteToFile(htmlStringBuilder.toString(),"diffWebViewHtmlPage.html");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void WriteToFile(String fileContent, String fileName) throws IOException {
String projectPath = System.getProperty("user.dir");
String tempFile = projectPath + File.separator+ "src\\main\\resources\\" + fileName;
System.out.println(tempFile);
File file = new File(tempFile);
// if file does exists, then delete and create a new file
if (file.exists()) {
try {
File newFileName = new File(projectPath + File.separator+ "src\\main\\resources\\" + "backup_"+fileName);
file.renameTo(newFileName);
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//write to file with OutputStreamWriter
OutputStream outputStream = new FileOutputStream(file.getAbsoluteFile());
Writer writer=new OutputStreamWriter(outputStream);
writer.write(fileContent);
writer.close();

}
}
1 change: 1 addition & 0 deletions src/main/resources/backup_diffWebViewHtmlPage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><head><title>Selenium Test </title></head><body style="font-size:70%;font-family:courier;"><div> diff --git a/build.gradle b/build.gradle</div><div> index 31f7c8a..a9c0501 100644</div><div style="background-color:rgb(255, 163, 163);"> --- a/build.gradle</div><div style="background-color:rgb(66, 244, 158);"> +++ b/build.gradle</div><div> @@ -22,4 +22,7 @@</div><div> compile 'org.eclipse.jgit:org.eclipse.jgit.http.server:3.7.0.201502260915-r'</div><div> compile 'org.eclipse.jgit:org.eclipse.jgit.ui:3.7.0.201502260915-r'</div><div> compile 'org.eclipse.jgit:org.eclipse.jgit.junit:3.7.0.201502260915-r'</div><div style="background-color:rgb(66, 244, 158);"> + compile 'com.jcabi:jcabi-aspects:0.22.5'</div><div style="background-color:rgb(66, 244, 158);"> + compile 'org.aspectj:aspectjtools:1.8.6'</div><div style="background-color:rgb(66, 244, 158);"> + compile 'org.aspectj:aspectjrt:1.6.12'</div><div> }</div><div> \ No newline at end of file</div></html>
1 change: 1 addition & 0 deletions src/main/resources/diffWebViewHtmlPage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><head><title>Selenium Test </title></head><body style="font-size:70%;font-family:courier;"></html>
Loading