Skip to content

The Unified and Autonomous Development Platform

Continue to enhance editsavepanel

robin shinef4263ab2a859
diff --git a/commons.git/src/main/java/com/pmease/commons/git/FileEdit.java b/commons.git/src/main/java/com/pmease/commons/git/FileEdit.java
index d32a348ea4..665c3dde7a 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/FileEdit.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/FileEdit.java
@@ -36,22 +36,29 @@ public class FileEdit implements Serializable {
private final String oldPath;
- private final File newFile;
+ private final String newPath;
- public FileEdit(@Nullable String oldPath, @Nullable File newFile) {
+ private final byte[] content;
+
+ public FileEdit(@Nullable String oldPath, @Nullable String newPath, byte[] content) {
this.oldPath = GitUtils.normalizePath(oldPath);
- this.newFile = newFile;
+ this.newPath= GitUtils.normalizePath(newPath);
+ this.content = content;
- Preconditions.checkArgument(this.oldPath != null || this.newFile != null,
- "Either oldPath or newFile should be defined");
+ Preconditions.checkArgument(this.oldPath != null || this.newPath != null,
+ "Either oldPath or newPath should be defined");
}
public String getOldPath() {
return oldPath;
}
- public File getNewFile() {
- return newFile;
+ public String getNewPath() {
+ return newPath;
+ }
+
+ public byte[] getContent() {
+ return content;
}
private ObjectId insertTree(RevTree revTree, TreeWalk treeWalk, ObjectInserter inserter,
@@ -69,7 +76,7 @@ public class FileEdit implements Serializable {
oldPathFound = true;
if (name.equals(currentNewPath)) {
newPathFound = true;
- ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, newFile.getContent());
+ ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, content);
formatter.append(name, FileMode.REGULAR_FILE, blobId);
appended = true;
}
@@ -127,7 +134,7 @@ public class FileEdit implements Serializable {
for (int i=splitted.size()-1; i>=0; i--) {
if (childId == null) {
childName = splitted.get(i);
- childId = inserter.insert(Constants.OBJ_BLOB, newFile.getContent());
+ childId = inserter.insert(Constants.OBJ_BLOB, content);
childMode = FileMode.REGULAR_FILE;
} else {
TreeFormatter childFormatter = new TreeFormatter();
@@ -190,11 +197,6 @@ public class FileEdit implements Serializable {
treeWalk.addTree(revTree);
CommitBuilder commit = new CommitBuilder();
- String newPath;
- if (newFile != null)
- newPath = newFile.getPath();
- else
- newPath = null;
commit.setTreeId(insertTree(revTree, treeWalk, inserter, oldPath, newPath));
commit.setAuthor(authorAndCommitter);
commit.setCommitter(authorAndCommitter);
diff --git a/commons.git/src/test/java/com/pmease/commons/git/FileEditTest.java b/commons.git/src/test/java/com/pmease/commons/git/FileEditTest.java
index 62d318c4b6..7b0de6a487 100644
--- a/commons.git/src/test/java/com/pmease/commons/git/FileEditTest.java
+++ b/commons.git/src/test/java/com/pmease/commons/git/FileEditTest.java
@@ -40,7 +40,7 @@ public class FileEditTest extends AbstractGitTest {
ObjectId oldCommitId = repo.resolve(refName);
PersonIdent person = new PersonIdent(repo);
- FileEdit edit = new FileEdit("/server/src/com/example/a//a.java", null);
+ FileEdit edit = new FileEdit("/server/src/com/example/a//a.java", null, null);
ObjectId newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test delete");
try (RevWalk revWalk = new RevWalk(repo)) {
@@ -69,7 +69,7 @@ public class FileEditTest extends AbstractGitTest {
ObjectId oldCommitId = repo.resolve(refName);
PersonIdent person = new PersonIdent(repo);
- FileEdit edit = new FileEdit("/server/src/com/example/c//c.java", null);
+ FileEdit edit = new FileEdit("/server/src/com/example/c//c.java", null, null);
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test delete");
assertTrue("An ObjectNotExistException should be thrown", false);
@@ -95,7 +95,7 @@ public class FileEditTest extends AbstractGitTest {
PersonIdent person = new PersonIdent(repo);
FileEdit edit = new FileEdit("server/src/com/example/a/a.java",
- new FileEdit.File("client/c.java", "a".getBytes()));
+ "client/c.java", "a".getBytes());
ObjectId newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename");
try (RevWalk revWalk = new RevWalk(repo)) {
RevTree revTree = revWalk.parseCommit(newCommitId).getTree();
@@ -125,7 +125,7 @@ public class FileEditTest extends AbstractGitTest {
PersonIdent person = new PersonIdent(repo);
FileEdit edit = new FileEdit("server/src/com/example/a",
- new FileEdit.File("client/c.java", "a".getBytes()));
+ "client/c.java", "a".getBytes());
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");
assertTrue("A NotFileException should be thrown", false);
@@ -151,7 +151,7 @@ public class FileEditTest extends AbstractGitTest {
PersonIdent person = new PersonIdent(repo);
FileEdit edit = new FileEdit("server/src/com/example/a/a.java",
- new FileEdit.File("client/a.java/a.java", "a".getBytes()));
+ "client/a.java/a.java", "a".getBytes());
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");
assertTrue("A NotTreeException should be thrown", false);
@@ -176,7 +176,7 @@ public class FileEditTest extends AbstractGitTest {
ObjectId oldCommitId = repo.resolve(refName);
PersonIdent person = new PersonIdent(repo);
- FileEdit edit = new FileEdit(null, new FileEdit.File("/server/src/com/example/c/c.java", "c".getBytes()));
+ FileEdit edit = new FileEdit(null, "/server/src/com/example/c/c.java", "c".getBytes());
ObjectId newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test add");
try (RevWalk revWalk = new RevWalk(repo)) {
@@ -189,7 +189,7 @@ public class FileEditTest extends AbstractGitTest {
}
oldCommitId = newCommitId;
- edit = new FileEdit(null, new FileEdit.File("/common/common.java", "common".getBytes()));
+ edit = new FileEdit(null, "/common/common.java", "common".getBytes());
newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test add");
try (RevWalk revWalk = new RevWalk(repo)) {
@@ -220,7 +220,7 @@ public class FileEditTest extends AbstractGitTest {
ObjectId oldCommitId = repo.resolve(refName);
PersonIdent person = new PersonIdent(repo);
- FileEdit edit = new FileEdit(null, new FileEdit.File("client/a.java", "a".getBytes()));
+ FileEdit edit = new FileEdit(null, "client/a.java", "a".getBytes());
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");
assertTrue("An ObjectAlreadyExistException should be thrown", false);
@@ -242,7 +242,7 @@ public class FileEditTest extends AbstractGitTest {
ObjectId newCommitId = repo.resolve(refName);
- FileEdit edit = new FileEdit(null, new FileEdit.File("/server/src/com/example/c/c.java", "c".getBytes()));
+ FileEdit edit = new FileEdit(null, "/server/src/com/example/c/c.java", "c".getBytes());
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test add");
assertTrue("An ObsoleteCommitException should be thrown", false);
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
index 325e44ebe4..87c131c71b 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
@@ -18,7 +18,6 @@ import org.apache.wicket.markup.html.link.ResourceLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
-import org.apache.wicket.model.Model;
import org.apache.wicket.request.resource.CssResourceReference;
import org.apache.wicket.request.resource.JavaScriptResourceReference;
import org.eclipse.jgit.internal.storage.file.FileRepository;
@@ -30,10 +29,10 @@ import org.eclipse.jgit.treewalk.TreeWalk;
import com.google.common.base.Preconditions;
import com.pmease.commons.git.BlobIdent;
-import com.pmease.commons.git.FileEdit;
import com.pmease.commons.git.Git;
import com.pmease.commons.wicket.assets.closestdescendant.ClosestDescendantResourceReference;
import com.pmease.gitplex.core.model.Repository;
+import com.pmease.gitplex.web.component.editsave.CancelListener;
import com.pmease.gitplex.web.component.editsave.EditSavePanel;
import com.pmease.gitplex.web.page.repository.file.HistoryState;
import com.pmease.gitplex.web.resource.BlobResource;
@@ -207,8 +206,29 @@ public abstract class BlobViewPanel extends Panel {
final BlobIdent file = context.getState().file;
String refName = Git.REFS_HEADS + file.revision;
+ String parentPath;
+ String oldName;
+ if (file.path.contains("/")) {
+ parentPath = StringUtils.substringBeforeLast(file.path, "/");
+ oldName = StringUtils.substringAfterLast(file.path, "/");
+ } else {
+ parentPath = null;
+ oldName = file.path;
+ }
+
+ CancelListener cancelListener = new CancelListener() {
+
+ @Override
+ public void onCancel(AjaxRequestTarget target) {
+ BlobViewPanel blobViewPanel = context.render(getId());
+ replaceWith(blobViewPanel);
+ target.add(blobViewPanel);
+ target.appendJavaScript("$(window).resize();");
+ }
+
+ };
EditSavePanel saveChangePanel = new EditSavePanel(panelId, repoModel, refName,
- file.path, Model.of((FileEdit.File)null), commitId) {
+ parentPath, oldName, null, commitId, cancelListener) {
@Override
protected void onCommitted(AjaxRequestTarget target, ObjectId newCommitId) {
@@ -232,14 +252,6 @@ public abstract class BlobViewPanel extends Panel {
throw new RuntimeException(e);
}
}
-
- @Override
- protected void onCancel(AjaxRequestTarget target) {
- BlobViewPanel blobViewPanel = context.render(getId());
- replaceWith(blobViewPanel);
- target.add(blobViewPanel);
- target.appendJavaScript("$(window).resize();");
- }
};
BlobViewPanel.this.replaceWith(saveChangePanel);
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/CancelListener.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/CancelListener.java
new file mode 100644
index 0000000000..fc2bb241ea
--- /dev/null
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/CancelListener.java
@@ -0,0 +1,7 @@
+package com.pmease.gitplex.web.component.editsave;
+
+import org.apache.wicket.ajax.AjaxRequestTarget;
+
+public interface CancelListener {
+ void onCancel(AjaxRequestTarget target);
+}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.java
index bff94b8c12..95defd3817 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.java
@@ -2,6 +2,8 @@ package com.pmease.gitplex.web.component.editsave;
import java.io.IOException;
+import javax.annotation.Nullable;
+
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
@@ -43,40 +45,55 @@ public abstract class EditSavePanel extends Panel {
private final String refName;
- private final String oldPath;
+ private final String parentPath;
+
+ private final String oldName;
+
+ private final IModel<byte[]> contentModel;
- private final IModel<FileEdit.File> newFileModel;
+ private final CancelListener cancelListener;
private ObjectId prevCommitId;
private ObjectId currentCommitId;
+ private String newName;
+
private String summaryCommitMessage;
private String detailCommitMessage;
public EditSavePanel(String id, IModel<Repository> repoModel, String refName,
- String oldPath, IModel<FileEdit.File> newFileModel, ObjectId prevCommitId) {
+ @Nullable String parentPath, @Nullable String oldName,
+ @Nullable IModel<byte[]> contentModel, ObjectId prevCommitId,
+ @Nullable CancelListener cancelListener) {
super(id);
this.repoModel = repoModel;
this.refName = refName;
- this.oldPath = oldPath;
- this.newFileModel = newFileModel;
+ this.parentPath = parentPath!=null?parentPath:"";
+ this.oldName = oldName;
+ this.contentModel = contentModel;
+ this.cancelListener = cancelListener;
this.prevCommitId = prevCommitId;
+
+ newName = oldName;
}
private String getDefaultCommitMessage() {
- FileEdit.File newFile = newFileModel.getObject();
- if (newFile != null) {
- if (newFile.getPath().equals(oldPath))
- return "Edit " + oldPath;
- else if (oldPath != null)
- return "Rename " + oldPath;
- else
- return "Add " + newFile.getPath();
+ if (contentModel != null) {
+ if (oldName != null) {
+ if (oldName.equals(newName))
+ return "Edit " + oldName;
+ else
+ return "Rename " + oldName;
+ } else if (newName != null) {
+ return "Add " + newName;
+ } else {
+ return "Add new file";
+ }
} else {
- return "Delete " + oldPath;
+ return "Delete " + oldName;
}
}
@@ -162,7 +179,23 @@ public abstract class EditSavePanel extends Panel {
commitMessage += "\n\n" + detailCommitMessage;
User user = Preconditions.checkNotNull(GitPlex.getInstance(UserManager.class).getCurrent());
- FileEdit edit = new FileEdit(oldPath, newFileModel.getObject());
+ String oldPath;
+ if (oldName != null)
+ oldPath = parentPath + "/" + oldName;
+ else
+ oldPath = null;
+
+ String newPath;
+ byte[] content;
+ if (contentModel != null) {
+ newPath = parentPath + "/" + newName;
+ content = contentModel.getObject();
+ } else {
+ newPath = null;
+ content = null;
+ }
+
+ FileEdit edit = new FileEdit(oldPath, newPath, content);
ObjectId newCommitId = null;
while(newCommitId == null) {
try {
@@ -184,8 +217,8 @@ public abstract class EditSavePanel extends Panel {
if (!treeWalk.getObjectId(0).equals(treeWalk.getObjectId(1))
|| !treeWalk.getFileMode(0).equals(treeWalk.getFileMode(1))) {
if (treeWalk.getObjectId(1).equals(ObjectId.zeroId())) {
- if (edit.getNewFile() != null) {
- edit = new FileEdit(null, edit.getNewFile());
+ if (edit.getNewPath() != null) {
+ edit = new FileEdit(null, edit.getNewPath(), edit.getContent());
hasChangesContainer.setVisibilityAllowed(true);
target.add(hasChangesContainer);
break;
@@ -201,8 +234,8 @@ public abstract class EditSavePanel extends Panel {
}
}
}
- if (edit.getNewFile() != null && !edit.getNewFile().getPath().equals(edit.getOldPath())) {
- TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, edit.getNewFile().getPath(),
+ if (edit.getNewPath() != null && !edit.getNewPath().equals(edit.getOldPath())) {
+ TreeWalk treeWalk = TreeWalk.forPath(jgitRepo, edit.getNewPath(),
prevCommit.getTree().getId(), currentCommit.getTree().getId());
if (treeWalk != null) {
if (!treeWalk.getObjectId(0).equals(treeWalk.getObjectId(1))
@@ -234,9 +267,15 @@ public abstract class EditSavePanel extends Panel {
form.add(new AjaxLink<Void>("cancel") {
+ @Override
+ protected void onConfigure() {
+ super.onConfigure();
+ setVisible(cancelListener != null);
+ }
+
@Override
public void onClick(AjaxRequestTarget target) {
- onCancel(target);
+ cancelListener.onCancel(target);
}
});
@@ -257,12 +296,11 @@ public abstract class EditSavePanel extends Panel {
protected abstract void onCommitted(AjaxRequestTarget target, ObjectId newCommitId);
- protected abstract void onCancel(AjaxRequestTarget target);
-
@Override
protected void onDetach() {
repoModel.detach();
- newFileModel.detach();
+ if (contentModel != null)
+ contentModel.detach();
super.onDetach();
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.html
new file mode 100644
index 0000000000..abf8f8b4c5
--- /dev/null
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.html
@@ -0,0 +1,14 @@
+<wicket:panel>
+ <div class="file-edit">
+ <div class="head">
+ <a wicket:id="editLink"></a>
+ <a wicket:id="previewLink"></a>
+ <a wicket:id="saveLink"></a>
+ </div>
+ <div class="body">
+ <div wicket:id="edit" class="edit"></div>
+ <div wicket:id="preview" class="preview"></div>
+ <div wicket:id="save" class="save"></div>
+ </div>
+ </div>
+</wicket:panel>
\ No newline at end of file
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.java
new file mode 100644
index 0000000000..c6c8e2496e
--- /dev/null
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.java
@@ -0,0 +1,34 @@
+package com.pmease.gitplex.web.component.fileedit;
+
+import javax.annotation.Nullable;
+
+import org.apache.wicket.markup.html.panel.Panel;
+import org.apache.wicket.model.IModel;
+import org.eclipse.jgit.lib.ObjectId;
+
+import com.pmease.gitplex.core.model.Repository;
+
+@SuppressWarnings("serial")
+public class FileEditPanel extends Panel {
+
+ private final IModel<Repository> repoModel;
+
+ public FileEditPanel(String id, IModel<Repository> repoModel, String refName,
+ @Nullable String oldPath, ObjectId prevCommitId) {
+ super(id);
+ this.repoModel = repoModel;
+ }
+
+ @Override
+ protected void onInitialize() {
+ super.onInitialize();
+ }
+
+ @Override
+ protected void onDetach() {
+ repoModel.detach();
+
+ super.onDetach();
+ }
+
+}