Skip to content

Commit

Permalink
Allow adding items using context menu
Browse files Browse the repository at this point in the history
Resolves #8
  • Loading branch information
anton-johansson committed Feb 23, 2016
1 parent 84d045c commit 1a72a4a
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import com.google.inject.Inject;
Expand Down Expand Up @@ -83,7 +84,7 @@ class CommitController extends AbstractController<LoadingView>
@Override
public void initialize()
{
commitView.initialize(contextMenuProvider);
commitView.initialize(contextMenuProvider, this::refresh);
initializeHandlers();

loadingView.setContent(commitView);
Expand Down Expand Up @@ -162,7 +163,10 @@ private void compare()

private void changeDoCommit()
{
boolean allMarked = commitView.selectedItems().allMatch(i -> i.isDoCommit());
boolean allMarked = commitView.selectedItems()
.filter(Objects::nonNull)
.allMatch(i -> i.isDoCommit());

boolean doCommit = !allMarked;
commitView.selectedItems()
.filter(s -> s.getStatus().isCommitable())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ public void setOnMouseDoubleClicked(Runnable runnable)
/**
* Initializes the view.
*/
public void initialize(Provider<CommitContextMenu> contextMenuProvider)
public void initialize(Provider<CommitContextMenu> contextMenuProvider, Runnable refreshCommand)
{
initializeTableView();
initializeTableViewContextMenu(contextMenuProvider);
initializeTableViewContextMenu(contextMenuProvider, refreshCommand);
}

private void initializeTableView()
Expand Down Expand Up @@ -211,28 +211,25 @@ protected void updateItem(FileStatus item, boolean empty)
tableView.getColumns().add(replicationColumn);
}

private void initializeTableViewContextMenu(Provider<CommitContextMenu> provider)
private void initializeTableViewContextMenu(Provider<CommitContextMenu> provider, Runnable refreshCommand)
{
tableView.setRowFactory(tableView ->
{
CommitContextMenu contextMenu = provider.get();
TableRow<ModifiedItem> row = new TableRow<>();
row.setContextMenu(contextMenu.getContextMenu());
row.itemProperty().addListener(e ->
{
setMenuItemStatuses(row, contextMenu);
});
row.itemProperty().addListener(e -> configureMenuItem(row, contextMenu, refreshCommand));
return row;
});
}

private void setMenuItemStatuses(TableRow<ModifiedItem> row, CommitContextMenu contextMenu)
private void configureMenuItem(TableRow<ModifiedItem> row, CommitContextMenu contextMenu, Runnable refreshCommand)
{
ModifiedItem item = row.getItem();
contextMenu.getMenuItems()
.stream()
.filter(menuItem -> item != null)
.forEach(menuItem -> menuItem.enable(item));
.forEach(menuItem -> menuItem.configure(item, refreshCommand));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
package com.antonjohansson.svncommit.application.commit.context;

import com.antonjohansson.svncommit.core.domain.ModifiedItem;
import com.antonjohansson.svncommit.core.utils.Subversion;
import com.antonjohansson.svncommit.core.view.AbstractRowContextMenuItem;

import static com.antonjohansson.svncommit.core.domain.FileStatus.UNVERSIONED;

import com.google.common.base.Predicate;
import com.google.inject.Inject;

/**
* Context menu item for running an {@code svn add} command.
Expand All @@ -29,18 +31,28 @@
*/
class AddMenuItem extends AbstractRowContextMenuItem<ModifiedItem>
{
/**
* Constructs a new {@link AddMenuItem}.
*/
AddMenuItem()
private final Subversion subversion;

@Inject
AddMenuItem(Subversion subversion)
{
super("SVN Add");
this.subversion = subversion;
}

/** {@inheritDoc} */
@Override
public Predicate<ModifiedItem> predicate()
protected Predicate<ModifiedItem> predicate()
{
return modiifedItem -> UNVERSIONED.equals(modiifedItem.getStatus());
}

/** {@inheritDoc} */
@Override
protected void action(ModifiedItem item, Runnable refreshCommand)
{
subversion.add(item.getFileName());
item.setDoCommit(true);
refreshCommand.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CommitContextMenuImpl extends ContextMenu implements CommitContextMenu
@Inject
CommitContextMenuImpl(Set<AbstractRowContextMenuItem<ModifiedItem>> menuItems)
{
super(menuItems.stream().toArray(size -> new MenuItem[size]));
super(menuItems.stream().toArray(MenuItem[]::new));
this.menuItems = menuItems;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@ public interface Subversion
* @param onComplete The task to run when the update is complete.
*/
void commit(String message, Collection<String> filePaths, Consumer<String> onData, Consumer<Boolean> onComplete);

/**
* Adds the given file.
*
* @param fileName The file name of the file to add.
*/
void add(String fileName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,11 @@ public void commit(String message, Collection<String> filePaths, Consumer<String

shell.executeAndPipeOutput(onData, onData, onComplete, command.toString());
}

/** {@inheritDoc} */
@Override
public void add(String fileName)
{
shell.execute("svn add '" + fileName + "'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ protected AbstractRowContextMenuItem(String title)
}

/**
* Enables or disables this menu item depending on the predicate and the given item.
* Sets the actual item for this menu item.
*
* @param item The item of the row.
* @param item The item.
* @param refreshCommand The command used to refresh the commit view.
*/
public void enable(T item)
public final void configure(T item, Runnable refreshCommand)
{
boolean enabled = predicate().apply(item);
setDisable(!enabled);
setOnAction(e -> action(item, refreshCommand));
}

/**
Expand All @@ -56,4 +58,12 @@ public void enable(T item)
* @return Returns the predicate.
*/
protected abstract Predicate<T> predicate();

/**
* Performs the action when clicking on the item.
*
* @param item The item that is clicked.
* @param refreshCommand The command that is used to refresh the commit view.
*/
protected abstract void action(T item, Runnable refreshCommand);
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void test_that_initialize_triggers_a_refresh() throws Exception
{
sleep(100);
assertThat(loadingView.isLoading(), is(false));
verify(commitView).initialize(any());
verify(commitView).initialize(any(), any());
verify(commitView).setCommitHandler(any());
verify(commitView).setOnKeyPressed(any());
verify(commitView).setOnMouseDoubleClicked(any());
Expand Down

0 comments on commit 1a72a4a

Please sign in to comment.