diff options
| author | Simon Muschel | 2019-11-25 17:40:40 +0000 |
|---|---|---|
| committer | Thomas Wolf | 2019-12-11 10:07:38 +0000 |
| commit | 072baaf8d3a90a493732e544c5c804b883306b31 (patch) | |
| tree | 4187ab2ae4506f484275ba8345d7988b2371dfd5 | |
| parent | 9ee1c5174b874c212f33b67b3ec4572e793ef74a (diff) | |
| download | egit-072baaf8d3a90a493732e544c5c804b883306b31.tar.gz egit-072baaf8d3a90a493732e544c5c804b883306b31.tar.xz egit-072baaf8d3a90a493732e544c5c804b883306b31.zip | |
CommitSelectionDialog: Offer search functionality
Adds a search bar above the commit selection list to allow users to
search for individual commits in the list. To avoid duplication, the
SearchBar implementation of GitHistoryPage is extracted and reused.
Bug: 345466
Change-Id: Ie987eeaa897b0b8917edace5c39b636d34ba18a6
Signed-off-by: Simon Muschel <smuschel@gmx.de>
5 files changed, 402 insertions, 250 deletions
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitSelectionDialog.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitSelectionDialog.java index 4422e3346c..412131f763 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitSelectionDialog.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/CommitSelectionDialog.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (C) 2011, 2013 Mathias Kinzler <mathias.kinzler@sap.com> and others. + * Copyright (C) 2011, 2019 Mathias Kinzler <mathias.kinzler@sap.com> and others. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -7,6 +7,9 @@ * https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Simon Muschel <smuschel@gmx.de> - Bug 345466 *******************************************************************************/ package org.eclipse.egit.ui.internal.history; @@ -58,6 +61,7 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.PlatformUI; @@ -80,6 +84,10 @@ public class CommitSelectionDialog extends TitleAreaDialog { private ObjectId commitId; + private SearchBar searchBar; + + private Label statusLabel; + /** * @param parentShell * @param repository @@ -113,12 +121,18 @@ public class CommitSelectionDialog extends TitleAreaDialog { @Override protected Control createDialogArea(Composite parent) { + Composite searchBarParent = new Composite(parent, SWT.NONE); + searchBarParent.setLayout(new GridLayout(1, false)); + GridDataFactory.fillDefaults().grab(true, false) + .applyTo(searchBarParent); + Composite main = new Composite(parent, SWT.NONE); main.setLayout(new GridLayout(1, false)); GridDataFactory.fillDefaults().grab(true, true).applyTo(main); final ResourceManager resources = new LocalResourceManager( JFaceResources.getResources()); UIUtils.hookDisposal(main, resources); + // Table never shows e-mail addresses because it might get rather wide. table = new CommitGraphTable(main, null, resources, false); table.getTableView().addSelectionChangedListener( @@ -146,6 +160,26 @@ public class CommitSelectionDialog extends TitleAreaDialog { 400).applyTo(table.getControl()); allCommits = new SWTCommitList(resources); table.getControl().addDisposeListener(e -> allCommits.clear()); + + searchBar = new SearchBar( + CommitSelectionDialog.class.getName() + ".searchBar", table) {//$NON-NLS-1$ + + @Override + protected void showStatus(FindToolbar originator, String text) { + statusLabel.setText(text); + statusLabel.requestLayout(); + } + + }; + searchBar.fill(searchBarParent); + + Composite statusLine = new Composite(parent, SWT.NONE); + statusLine.setLayout(new GridLayout()); + GridDataFactory.fillDefaults().grab(true, true).applyTo(statusLine); + statusLabel = new Label(statusLine, SWT.NONE); + GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.CENTER) + .grab(false, false).applyTo(statusLabel); + return main; } @@ -229,8 +263,25 @@ public class CommitSelectionDialog extends TitleAreaDialog { Integer.valueOf(allCommits.size()), GitLabels.getPlainShortLabel(repository))); setMessage(UIText.CommitSelectionDialog_DialogMessage); - table.setInput(highlightFlag, allCommits, allCommits - .toArray(new SWTCommit[0]), null, true); + SWTCommit[] allCommitsArray = allCommits.toArray(new SWTCommit[0]); + table.setInput(highlightFlag, allCommits, allCommitsArray, null, true); + searchBar.setInput(new ICommitsProvider() { + + @Override + public Object getSearchContext() { + return null; + } + + @Override + public SWTCommit[] getCommits() { + return allCommitsArray; + } + + @Override + public RevFlag getHighlight() { + return highlightFlag; + } + }); } private void markStartAllRefs(RevWalk walk, Set<Ref> refs) diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/FindToolbar.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/FindToolbar.java index 8cb65db38d..283e94849b 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/FindToolbar.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/FindToolbar.java @@ -5,6 +5,7 @@ * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com> * Copyright (C) 2013, Robin Stocker <robin@nibor.org> * Copyright (C) 2016, Thomas Wolf <thomas.wolf@paranor.ch> + * Copyright (C) 2019, Simon Muschel <smuschel@gmx.de> * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -219,7 +220,8 @@ public class FindToolbar extends Composite { patternField = new Text(this, SWT.SEARCH | SWT.ICON_CANCEL | SWT.ICON_SEARCH); - GridData findTextData = new GridData(SWT.LEFT, SWT.TOP, true, false); + GridData findTextData = new GridData(SWT.FILL, SWT.TOP, true, + false); findTextData.minimumWidth = 150; patternField.setLayoutData(findTextData); patternField.setMessage(UIText.HistoryPage_findbar_find_msg); diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/GitHistoryPage.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/GitHistoryPage.java index 12eb010acf..e62be85e21 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/GitHistoryPage.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/GitHistoryPage.java @@ -11,6 +11,7 @@ * Copyright (C) 2015-2019 Thomas Wolf <thomas.wolf@paranor.ch> * Copyright (C) 2015-2017, Stefan Dirix <sdirix@eclipsesource.com> * Copyright (C) 2019, Tim Neumann <Tim.Neumann@advantest.com> + * Copyright (C) 2019, Simon Muschel <smuschel@gmx.de> * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -69,7 +70,6 @@ import org.eclipse.egit.ui.internal.commit.FocusTracker; import org.eclipse.egit.ui.internal.components.DropDownMenuAction; import org.eclipse.egit.ui.internal.components.RepositoryMenuUtil.RepositoryToolbarAction; import org.eclipse.egit.ui.internal.fetch.FetchHeadChangedEvent; -import org.eclipse.egit.ui.internal.history.FindToolbar.StatusListener; import org.eclipse.egit.ui.internal.history.RefFilterHelper.RefFilter; import org.eclipse.egit.ui.internal.repository.tree.AdditionalRefNode; import org.eclipse.egit.ui.internal.repository.tree.FileNode; @@ -84,12 +84,10 @@ import org.eclipse.egit.ui.internal.selection.SelectionUtils; import org.eclipse.egit.ui.internal.trace.GitTraceLocation; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.ActionContributionItem; -import org.eclipse.jface.action.ControlContribution; import org.eclipse.jface.action.IAction; import org.eclipse.jface.action.IContributionItem; import org.eclipse.jface.action.IContributionManager; import org.eclipse.jface.action.IMenuManager; -import org.eclipse.jface.action.IStatusLineManager; import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.action.Separator; @@ -1389,248 +1387,6 @@ public class GitHistoryPage extends HistoryPage implements RefsChangedListener, } } - private interface ICommitsProvider { - - Object getSearchContext(); - - SWTCommit[] getCommits(); - - RevFlag getHighlight(); - } - - private static class SearchBar extends ControlContribution { - - private IActionBars bars; - - private FindToolbar toolbar; - - private Object searchContext; - - private String lastText; - - private ObjectId lastObjectId; - - private Object lastSearchContext; - - private ICommitsProvider provider; - - private boolean wasVisible = false; - - private final CommitGraphTable graph; - - private final IAction openCloseToggle; - - /** - * "Go to next/previous" from the {@link FindToolbar} sends - * {@link SWT#Selection} events with the chosen {@link RevCommit} as - * data. - */ - private final Listener selectionListener = new Listener() { - - @Override - public void handleEvent(Event evt) { - final RevCommit commit = (RevCommit) evt.data; - lastObjectId = commit.getId(); - graph.selectCommit(commit); - } - }; - - /** - * Listener to close the search bar on ESC. (Ctrl/Cmd-F is already - * handled via global retarget action.) - */ - private final KeyListener keyListener = new KeyAdapter() { - - @Override - public void keyPressed(KeyEvent e) { - int key = SWTKeySupport.convertEventToUnmodifiedAccelerator(e); - if (key == SWT.ESC) { - setVisible(false); - e.doit = false; - } - } - }; - - /** - * Listener to display status messages from the asynchronous find. (Is - * called in the UI thread.) - */ - private final StatusListener statusListener = new StatusListener() { - - @Override - public void setMessage(FindToolbar originator, String text) { - IStatusLineManager status = bars.getStatusLineManager(); - if (status != null) { - status.setMessage(text); - } - } - }; - - /** - * Listener to ensure that the history view is fully activated when the - * user clicks into the search bar's text widget. This makes sure our - * status manager gets activated and thus shows the status messages. We - * don't get a focus event when the user clicks in the field; and - * fiddling with the focus in a FocusListener could get hairy anyway. - */ - private final Listener mouseListener = new Listener() { - - private boolean hasFocus; - - private boolean hadFocusOnMouseDown; - - @Override - public void handleEvent(Event e) { - switch (e.type) { - case SWT.FocusIn: - toolbar.getDisplay().asyncExec(new Runnable() { - @Override - public void run() { - hasFocus = true; - } - }); - - break; - case SWT.FocusOut: - hasFocus = false; - break; - case SWT.MouseDown: - hadFocusOnMouseDown = hasFocus; - break; - case SWT.MouseUp: - if (!hadFocusOnMouseDown) { - graph.getControl().setFocus(); - toolbar.setFocus(); - } - break; - default: - break; - } - } - }; - - public SearchBar(String id, CommitGraphTable graph, - IAction openCloseAction, IActionBars bars) { - super(id); - super.setVisible(false); - this.graph = graph; - this.openCloseToggle = openCloseAction; - this.bars = bars; - } - - private void beforeHide() { - lastText = toolbar.getText(); - lastSearchContext = searchContext; - statusListener.setMessage(toolbar, ""); //$NON-NLS-1$ - // It will be disposed by the IToolBarManager - toolbar = null; - openCloseToggle.setChecked(false); - wasVisible = false; - } - - private void workAroundBug551067(boolean visible) { - // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=551067 - IContributionManager parent = getParent(); - if (parent instanceof SubToolBarManager) { - SubToolBarManager subManager = (SubToolBarManager) parent; - IContributionItem item = subManager.getParent().find(getId()); - if (item instanceof SubContributionItem) { - item.setVisible(visible && subManager.isVisible()); - } - } - } - - @Override - public void setVisible(boolean visible) { - if (visible != isVisible()) { - if (!visible) { - beforeHide(); - } - super.setVisible(visible); - workAroundBug551067(visible); - // Update the toolbar. Will dispose our FindToolbar widget on - // hide, and will create a new one (through createControl()) - // on show. It'll also reposition the toolbar, if needed. - // Note: just doing bars.getToolBarManager().update(true); - // messes up big time (doesn't resize or re-position). - bars.updateActionBars(); - if (visible && toolbar != null) { - openCloseToggle.setChecked(true); - // If the toolbar was moved below the tabs, we now have - // the wrong background. It disappears when one clicks - // elsewhere. Looks like an inactive selection... No - // way found to fix this but this ugly focus juggling: - graph.getControl().setFocus(); - toolbar.setFocus(); - } else if (!visible && !graph.getControl().isDisposed()) { - graph.getControl().setFocus(); - } - } - } - - @Override - public boolean isDynamic() { - // We toggle our own visibility - return true; - } - - @Override - protected Control createControl(Composite parent) { - toolbar = new FindToolbar(parent); - toolbar.setBackground(null); - toolbar.addKeyListener(keyListener); - toolbar.addListener(SWT.FocusIn, mouseListener); - toolbar.addListener(SWT.FocusOut, mouseListener); - toolbar.addListener(SWT.MouseDown, mouseListener); - toolbar.addListener(SWT.MouseUp, mouseListener); - toolbar.addListener(SWT.Modify, - (e) -> lastText = toolbar.getText()); - toolbar.addStatusListener(statusListener); - toolbar.addSelectionListener(selectionListener); - boolean hasInput = provider != null; - if (hasInput) { - setInput(provider); - } - if (lastText != null) { - if (lastSearchContext != null - && lastSearchContext.equals(searchContext)) { - toolbar.setPreselect(lastObjectId); - } - toolbar.setText(lastText, hasInput); - } - lastSearchContext = null; - lastObjectId = null; - if (wasVisible) { - return toolbar; - } - wasVisible = true; - // This fixes the wrong background when Eclipse starts up with the - // search bar visible. - toolbar.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - if (toolbar != null && !toolbar.isDisposed()) { - // See setVisible() above. Somehow, we need this, too. - graph.getControl().setFocus(); - toolbar.setFocus(); - } - } - }); - return toolbar; - } - - public void setInput(ICommitsProvider provider) { - this.provider = provider; - if (toolbar != null) { - searchContext = provider.getSearchContext(); - toolbar.setInput(provider.getHighlight(), - graph.getTableView().getTable(), provider.getCommits()); - } - } - - } - @Override public void createControl(final Composite parent) { trace = GitTraceLocation.HISTORYVIEW.isActive(); @@ -1729,7 +1485,8 @@ public class GitHistoryPage extends HistoryPage implements RefsChangedListener, .addPreferenceChangeListener(prefListener); IToolBarManager manager = getSite().getActionBars().getToolBarManager(); - searchBar = new SearchBar(GitHistoryPage.class.getName() + ".searchBar", //$NON-NLS-1$ + searchBar = new HistorySearchBar( + GitHistoryPage.class.getName() + ".searchBar", //$NON-NLS-1$ graph, actions.findAction, getSite().getActionBars()); manager.prependToGroup("org.eclipse.team.ui.historyView", searchBar); //$NON-NLS-1$ getSite().getActionBars().updateActionBars(); @@ -3302,4 +3059,173 @@ public class GitHistoryPage extends HistoryPage implements RefsChangedListener, public String getRenamedPath(String path, ObjectId commit) { return renameTracker.getPath(commit, path); } + + private static final class HistorySearchBar extends SearchBar { + + private IActionBars bars; + + private final IAction openCloseToggle; + + private boolean wasVisible = false; + + /** + * Listener to close the search bar on ESC. (Ctrl/Cmd-F is already + * handled via global retarget action.) + */ + private final KeyListener keyListener = new KeyAdapter() { + + @Override + public void keyPressed(KeyEvent e) { + int key = SWTKeySupport.convertEventToUnmodifiedAccelerator(e); + if (key == SWT.ESC) { + setVisible(false); + e.doit = false; + } + } + }; + + /** + * Listener to ensure that the history view is fully activated when the + * user clicks into the search bar's text widget. This makes sure our + * status manager gets activated and thus shows the status messages. We + * don't get a focus event when the user clicks in the field; and + * fiddling with the focus in a FocusListener could get hairy anyway. + */ + private final Listener mouseListener = new Listener() { + + private boolean hasFocus; + + private boolean hadFocusOnMouseDown; + + @Override + public void handleEvent(Event e) { + switch (e.type) { + case SWT.FocusIn: + toolbar.getDisplay().asyncExec(new Runnable() { + @Override + public void run() { + hasFocus = true; + } + }); + + break; + case SWT.FocusOut: + hasFocus = false; + break; + case SWT.MouseDown: + hadFocusOnMouseDown = hasFocus; + break; + case SWT.MouseUp: + if (!hadFocusOnMouseDown) { + graph.getControl().setFocus(); + toolbar.setFocus(); + } + break; + default: + break; + } + } + }; + + public HistorySearchBar(String id, CommitGraphTable graph, + IAction openCloseAction, IActionBars bars) { + super(id, graph); + this.bars = bars; + this.openCloseToggle = openCloseAction; + super.setVisible(false); + } + + @Override + public boolean isDynamic() { + // We toggle our own visibility + return true; + } + + @Override + protected FindToolbar createControl(Composite parent) { + FindToolbar createdControl = super.createControl(parent); + toolbar.addKeyListener(keyListener); + toolbar.addListener(SWT.FocusIn, mouseListener); + toolbar.addListener(SWT.FocusOut, mouseListener); + toolbar.addListener(SWT.MouseDown, mouseListener); + toolbar.addListener(SWT.MouseUp, mouseListener); + + if (wasVisible) { + return toolbar; + } + wasVisible = true; + // This fixes the wrong background when Eclipse starts up with the + // search bar visible. + toolbar.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + if (toolbar != null && !toolbar.isDisposed()) { + // See setVisible() above. Somehow, we need this, too. + graph.getControl().setFocus(); + toolbar.setFocus(); + } + } + }); + return createdControl; + } + + private void beforeHide() { + lastText = toolbar.getText(); + lastSearchContext = searchContext; + showStatus(toolbar, ""); //$NON-NLS-1$ + // It will be disposed by the IToolBarManager + toolbar = null; + openCloseToggle.setChecked(false); + wasVisible = false; + } + + private void workAroundBug551067(boolean visible) { + // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=551067 + IContributionManager parent = getParent(); + if (parent instanceof SubToolBarManager) { + SubToolBarManager subManager = (SubToolBarManager) parent; + IContributionItem item = subManager.getParent().find(getId()); + if (item instanceof SubContributionItem) { + item.setVisible(visible && subManager.isVisible()); + } + } + } + + @Override + public void setVisible(boolean visible) { + if (visible != isVisible()) { + if (!visible) { + beforeHide(); + } + super.setVisible(visible); + workAroundBug551067(visible); + // Update the toolbar. Will dispose our FindToolbar widget on + // hide, and will create a new one (through createControl()) + // on show. It'll also reposition the toolbar, if needed. + // Note: just doing bars.getToolBarManager().update(true); + // messes up big time (doesn't resize or re-position). + if (bars != null) { + bars.updateActionBars(); + } + if (visible && toolbar != null) { + openCloseToggle.setChecked(true); + // If the toolbar was moved below the tabs, we now have + // the wrong background. It disappears when one clicks + // elsewhere. Looks like an inactive selection... No + // way found to fix this but this ugly focus juggling: + graph.getControl().setFocus(); + toolbar.setFocus(); + } else if (!visible && !graph.getControl().isDisposed()) { + graph.getControl().setFocus(); + } + } + } + + @Override + protected void showStatus(FindToolbar originator, String text) { + bars.getStatusLineManager().setMessage(text); + } + + } } diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/ICommitsProvider.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/ICommitsProvider.java new file mode 100644 index 0000000000..726557ae55 --- /dev/null +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/ICommitsProvider.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (C) 2019 Simon Muschel <smuschel@gmx.de> and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Simon Muschel <smuschel@gmx.de> - Bug 345466 + *******************************************************************************/ +package org.eclipse.egit.ui.internal.history; + +import org.eclipse.jgit.revwalk.RevFlag; + +/** + * Provides relevant information to {@link SearchBar} implementations. + */ +interface ICommitsProvider { + + /** + * Returns the search context the {@link SearchBar} is using. An example on + * how to use the context can be found in {@link GitHistoryPage}. + * + * @return the current search context + */ + Object getSearchContext(); + + /** + * Returns a list of commits to be searched. + * + * @return array of commits + */ + SWTCommit[] getCommits(); + + /** + * Returns the RevFlag to be used as highlight marker for matching commits. + * + * @return the highlight RevFlag + */ + RevFlag getHighlight(); +}
\ No newline at end of file diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/SearchBar.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/SearchBar.java new file mode 100644 index 0000000000..724765bf2c --- /dev/null +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/history/SearchBar.java @@ -0,0 +1,129 @@ +/******************************************************************************* + * Copyright (C) 2019 Simon Muschel <smuschel@gmx.de> and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Simon Muschel <smuschel@gmx.de> - Bug 345466 + *******************************************************************************/ +package org.eclipse.egit.ui.internal.history; + +import org.eclipse.jface.action.ControlContribution; +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevFlag; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; + +/** + * A reusable SearchBar component that may be used in combination with + * {@link CommitGraphTable} to search in a given list of commits. + * + * @see GitHistoryPage + * @see CommitSelectionDialog + */ +abstract class SearchBar extends ControlContribution { + + protected FindToolbar toolbar; + + protected Object searchContext; + + protected String lastText; + + private ObjectId lastObjectId; + + protected Object lastSearchContext; + + private ICommitsProvider provider; + + protected final CommitGraphTable graph; + + /** + * "Go to next/previous" from the {@link FindToolbar} sends + * {@link SWT#Selection} events with the chosen {@link RevCommit} as data. + */ + private final Listener selectionListener = new Listener() { + + @Override + public void handleEvent(Event evt) { + final RevCommit commit = (RevCommit) evt.data; + lastObjectId = commit.getId(); + graph.selectCommit(commit); + } + }; + + /** + * Creates the SearchBar. + * + * @param id + * the contribution item id + * @param graph + * the UI element displaying the commits + */ + public SearchBar(String id, CommitGraphTable graph) { + super(id); + this.graph = graph; + } + + @Override + protected FindToolbar createControl(Composite parent) { + toolbar = new FindToolbar(parent); + toolbar.setBackground(null); + toolbar.addListener(SWT.Modify, (e) -> lastText = toolbar.getText()); + toolbar.addSelectionListener(selectionListener); + toolbar.addStatusListener(this::showStatus); + boolean hasInput = provider != null; + if (hasInput) { + setInput(provider); + } + if (lastText != null) { + if (lastSearchContext != null + && lastSearchContext.equals(searchContext)) { + toolbar.setPreselect(lastObjectId); + } + toolbar.setText(lastText, hasInput); + } + lastSearchContext = null; + lastObjectId = null; + + GridDataFactory.fillDefaults().grab(true, false).applyTo(toolbar); + return toolbar; + } + + /** + * Set the {@link ICommitsProvider} implementation that provides a search + * context for the SearchBar. The list of commits and the {@link RevFlag} + * are passed on to the {@link FindToolbar}. + * + * @param provider + */ + public void setInput(ICommitsProvider provider) { + this.provider = provider; + if (toolbar != null) { + searchContext = provider.getSearchContext(); + toolbar.setInput(provider.getHighlight(), + graph.getTableView().getTable(), provider.getCommits()); + } + } + + /** + * The {@link FindToolbar} notifies clients of status changes (e.g. number + * of matching commits) by calling this method. A sub-class can provide a + * way to display these status messages to the user. + * + * @param originator + * the toolbar object that produced the new message + * @param text + * the new status message to be displayed + */ + abstract protected void showStatus(FindToolbar originator, String text); + +} |
