Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Davis2014-01-28 00:38:44 +0000
committerGerrit Code Review @ Eclipse.org2014-01-28 01:39:55 +0000
commit695fed73ab8ba2b8c2fdde6de8834d2d2e7c0558 (patch)
treebd18b44b5cae1e62839e6362d217c2da3d18238d /org.eclipse.mylyn.tasks.ui
parent74aae0dd1441c655efbc159fad638501c9df1925 (diff)
downloadorg.eclipse.mylyn.tasks-695fed73ab8ba2b8c2fdde6de8834d2d2e7c0558.tar.gz
org.eclipse.mylyn.tasks-695fed73ab8ba2b8c2fdde6de8834d2d2e7c0558.tar.xz
org.eclipse.mylyn.tasks-695fed73ab8ba2b8c2fdde6de8834d2d2e7c0558.zip
134165: provide find functionality for task editor
Code moved from org.eclipse.mylyn.incubator/org.eclipse.mylyn.sandbox.ui Change-Id: Idf6c2cea96ad147e716c190149e630eec8ca2249 Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=134165
Diffstat (limited to 'org.eclipse.mylyn.tasks.ui')
-rw-r--r--org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/Messages.java4
-rw-r--r--org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/TaskEditorFindSupport.java377
-rw-r--r--org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/messages.properties3
-rw-r--r--org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ui/editors/AbstractTaskEditorPage.java23
4 files changed, 407 insertions, 0 deletions
diff --git a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/Messages.java b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/Messages.java
index 1c4ef4945..7b328b651 100644
--- a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/Messages.java
+++ b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/Messages.java
@@ -298,4 +298,8 @@ public class Messages extends NLS {
public static String TaskHyperlinkPresenter_Not_In_Task_List;
public static String TaskUrlHyperlink_Open_URL_in_Task_Editor;
+
+ public static String TaskEditorFindSupport_Find;
+
+ public static String TaskEditorFindSupport_Show_X_more_results;
}
diff --git a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/TaskEditorFindSupport.java b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/TaskEditorFindSupport.java
new file mode 100644
index 000000000..de8c70f12
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/TaskEditorFindSupport.java
@@ -0,0 +1,377 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.internal.tasks.ui.editors;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.ControlContribution;
+import org.eclipse.jface.action.IToolBarManager;
+import org.eclipse.mylyn.commons.ui.CommonImages;
+import org.eclipse.mylyn.commons.workbench.forms.CommonFormUtil;
+import org.eclipse.mylyn.internal.tasks.ui.editors.TaskEditorCommentPart.CommentGroupViewer;
+import org.eclipse.mylyn.internal.tasks.ui.editors.TaskEditorCommentPart.CommentViewer;
+import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
+import org.eclipse.mylyn.tasks.core.data.TaskData;
+import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorPage;
+import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorPart;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.StyleRange;
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.IWorkbenchActionConstants;
+import org.eclipse.ui.forms.IFormPart;
+import org.eclipse.ui.forms.events.HyperlinkAdapter;
+import org.eclipse.ui.forms.events.HyperlinkEvent;
+import org.eclipse.ui.forms.widgets.ExpandableComposite;
+import org.eclipse.ui.forms.widgets.FormToolkit;
+
+/**
+ * Adds support for finding text to the task editor.
+ *
+ * @author Jingwen Ou
+ * @author Lily Guo
+ * @author Sam Davis
+ */
+public class TaskEditorFindSupport {
+
+ private Action toggleFindAction;
+
+ private static final Color HIGHLIGHTER_YELLOW = new Color(Display.getDefault(), 255, 238, 99);
+
+ private static final Color ERROR_NO_RESULT = new Color(Display.getDefault(), 255, 150, 150);
+
+ private final List<StyledText> styledTexts = new ArrayList<StyledText>();
+
+ private final List<CommentGroupViewer> commentGroupViewers = new ArrayList<CommentGroupViewer>();
+
+ private final AbstractTaskEditorPage taskEditorPage;;
+
+ public TaskEditorFindSupport(AbstractTaskEditorPage page) {
+ Assert.isNotNull(page);
+ this.taskEditorPage = page;
+ }
+
+ public void toggleFind() {
+ if (toggleFindAction != null) {
+ toggleFindAction.setChecked(!toggleFindAction.isChecked());
+ toggleFindAction.run();
+ }
+ }
+
+ public void addFindAction(IToolBarManager toolBarManager) {
+ if (toggleFindAction != null && toggleFindAction.isChecked()) {
+ ControlContribution findTextboxControl = new ControlContribution(Messages.TaskEditorFindSupport_Find) {
+ @Override
+ protected Control createControl(Composite parent) {
+ FormToolkit toolkit = taskEditorPage.getEditor().getHeaderForm().getToolkit();
+ final Composite findComposite = toolkit.createComposite(parent);
+
+ GridLayout findLayout = new GridLayout();
+ findLayout.marginHeight = 4;
+ findComposite.setLayout(findLayout);
+ findComposite.setBackground(null);
+
+ final Text findText = toolkit.createText(findComposite, "", SWT.FLAT); //$NON-NLS-1$
+ findText.setLayoutData(new GridData(100, SWT.DEFAULT));
+ findText.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
+ findText.setFocus();
+ toolkit.adapt(findText, false, false);
+
+ findText.addModifyListener(new ModifyListener() {
+ @Override
+ public void modifyText(ModifyEvent e) {
+ if (findText.getText().equals("")) { //$NON-NLS-1$
+ clearSearchResults();
+ findText.setBackground(null);
+ }
+ }
+ });
+
+ findText.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetDefaultSelected(SelectionEvent event) {
+ searchTaskEditor(findText);
+ }
+ });
+ toolkit.paintBordersFor(findComposite);
+ return findComposite;
+ }
+
+ };
+ toolBarManager.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, findTextboxControl);
+ }
+
+ if (toggleFindAction == null) {
+ toggleFindAction = new Action("", SWT.TOGGLE) { //$NON-NLS-1$
+ @Override
+ public void run() {
+ if (!this.isChecked()) {
+ clearSearchResults();
+ }
+ taskEditorPage.getEditor().updateHeaderToolBar();
+ }
+
+ };
+ toggleFindAction.setImageDescriptor(CommonImages.FIND);
+ toggleFindAction.setToolTipText(Messages.TaskEditorFindSupport_Find);
+ }
+ toolBarManager.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, toggleFindAction);
+ }
+
+ protected void searchTaskEditor(final Text findBox) {
+ try {
+ taskEditorPage.setReflow(false);
+ findBox.setBackground(null);
+ if (findBox.getText().equals("")) { //$NON-NLS-1$
+ return;
+ }
+ clearSearchResults();
+ String searchString = findBox.getText().toLowerCase();
+ for (IFormPart part : taskEditorPage.getManagedForm().getParts()) {
+ if (!(part instanceof AbstractTaskEditorPart)) {
+ continue;
+ }
+ Control control = ((AbstractTaskEditorPart) part).getControl();
+ if (part instanceof TaskEditorSummaryPart) {
+ if (contains(taskEditorPage.getModel().getTaskData(), TaskAttribute.SUMMARY, searchString)) {
+ gatherStyledTexts(control, styledTexts);
+ }
+ } else if (part instanceof TaskEditorPlanningPart) {
+ RichTextEditor noteEditor = ((TaskEditorPlanningPart) part).getPlanningPart().getNoteEditor();
+ if (noteEditor != null && noteEditor.getText() != null
+ && noteEditor.getText().toLowerCase().contains(searchString)) {
+ gatherStyledTexts(control, styledTexts);
+ }
+ } else if (part instanceof TaskEditorDescriptionPart) {
+ if (contains(taskEditorPage.getModel().getTaskData(), TaskAttribute.DESCRIPTION, searchString)) {
+ gatherStyledTexts(control, styledTexts);
+ }
+ } else if (part instanceof TaskEditorCommentPart) {
+ commentGroupViewers.clear();
+ commentGroupViewers.addAll(((TaskEditorCommentPart) part).getCommentGroupViewers());
+ searchCommentPart(searchString, (TaskEditorCommentPart) part, commentGroupViewers, styledTexts);
+ }
+ }
+
+ for (StyledText styledText : styledTexts) {
+ highlightMatches(searchString, styledText);
+ }
+ if (styledTexts.isEmpty()) {
+ findBox.setBackground(ERROR_NO_RESULT);
+ }
+ } finally {
+ taskEditorPage.setReflow(true);
+ }
+ taskEditorPage.reflow();
+ findBox.setFocus();
+ }
+
+ protected static boolean contains(TaskData taskData, String attributeId, String searchString) {
+ TaskAttribute attribute = taskData.getRoot().getMappedAttribute(attributeId);
+ if (attribute != null) {
+ return attribute.getValue().toLowerCase().contains(searchString);
+ }
+ return false;
+ }
+
+ private void searchCommentPart(final String searchString, final TaskEditorCommentPart part,
+ List<CommentGroupViewer> commentGroupViewers, final List<StyledText> styledTexts) {
+ TaskData taskData = taskEditorPage.getModel().getTaskData();
+ List<TaskAttribute> commentAttributes = taskData.getAttributeMapper().getAttributesByType(taskData,
+ TaskAttribute.TYPE_COMMENT);
+
+ if (!anyCommentContains(commentAttributes, searchString)) {
+ return;
+ }
+
+ if (!part.isCommentSectionExpanded()) {
+ try {
+ part.setReflow(false);
+ part.expandAllComments(false);
+ } finally {
+ part.setReflow(true);
+ }
+ }
+
+ int end = commentAttributes.size();
+ boolean expandMatchingGroup = true;
+ for (int i = commentGroupViewers.size() - 1; i >= 0; i--) {
+ final CommentGroupViewer group = commentGroupViewers.get(i);
+ List<CommentViewer> commentViewers = group.getCommentViewers();
+ int start = end - commentViewers.size();
+ List<TaskAttribute> groupAttributes = commentAttributes.subList(start, end);
+ if (expandMatchingGroup && anyCommentContains(groupAttributes, searchString)) {
+ if (!group.isExpanded()) {
+ try {
+ part.setReflow(false);
+ group.setExpanded(true);
+ } finally {
+ part.setReflow(true);
+ }
+ }
+ // once we've seen a matching group, don't expand any more groups
+ expandMatchingGroup = false;
+ }
+ final List<CommentViewer> matchingViewers = searchComments(groupAttributes, commentViewers, searchString);
+ if (!group.isRenderedInSubSection() || group.isExpanded()) {
+ try {
+ part.setReflow(false);
+ gatherStyledTexts(matchingViewers, styledTexts);
+ } finally {
+ part.setReflow(true);
+ }
+ group.clearSectionHyperlink();
+ } else if (!matchingViewers.isEmpty()) {
+ addShowMoreLink(group, matchingViewers, part, searchString, styledTexts);
+ } else {
+ group.clearSectionHyperlink();
+ }
+ end = start;
+ }
+ }
+
+ protected void addShowMoreLink(final CommentGroupViewer group, final List<CommentViewer> matchingViewers,
+ final TaskEditorCommentPart part, final String searchString, final List<StyledText> styledTexts) {
+ HyperlinkAdapter listener = new HyperlinkAdapter() {
+ @Override
+ public void linkActivated(HyperlinkEvent e) {
+ List<StyledText> commentStyledTexts = new ArrayList<StyledText>();
+ try {
+ taskEditorPage.setReflow(false);
+ part.setReflow(false);
+ group.setExpanded(true);
+ gatherStyledTexts(matchingViewers, commentStyledTexts);
+ } finally {
+ taskEditorPage.setReflow(true);
+ part.setReflow(true);
+ }
+ for (StyledText styledText : commentStyledTexts) {
+ highlightMatches(searchString, styledText);
+ styledTexts.add(styledText);
+ }
+ group.clearSectionHyperlink();
+ taskEditorPage.reflow();
+ }
+ };
+ group.createSectionHyperlink(
+ NLS.bind(Messages.TaskEditorFindSupport_Show_X_more_results, matchingViewers.size()), listener);
+ }
+
+ private static boolean anyCommentContains(List<TaskAttribute> commentAttributes, String text) {
+ for (TaskAttribute commentAttribute : commentAttributes) {
+ if (commentContains(commentAttribute, text)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static boolean commentContains(TaskAttribute commentAttribute, String searchString) {
+ TaskAttribute attribute = commentAttribute.getMappedAttribute(TaskAttribute.COMMENT_TEXT);
+ return attribute.getValue().toLowerCase().contains(searchString);
+ }
+
+ private static List<CommentViewer> searchComments(List<TaskAttribute> commentAttributes,
+ List<CommentViewer> commentViewers, String searchString) {
+ List<CommentViewer> matchingViewers = new ArrayList<TaskEditorCommentPart.CommentViewer>();
+ for (int i = 0; i < commentViewers.size(); i++) {
+ CommentViewer viewer = commentViewers.get(i);
+ if (commentContains(commentAttributes.get(i), searchString)) {
+ matchingViewers.add(viewer);
+ }
+ }
+ return matchingViewers;
+ }
+
+ protected static void gatherStyledTexts(List<CommentViewer> commentViewers, List<StyledText> styledTexts) {
+ for (CommentViewer viewer : commentViewers) {
+ try {
+ ExpandableComposite composite = (ExpandableComposite) viewer.getControl();
+ viewer.suppressSelectionChanged(true);
+ if (composite != null && !composite.isExpanded()) {
+ CommonFormUtil.setExpanded(composite, true);
+ }
+ gatherStyledTextsInComposite(composite, styledTexts);
+ } finally {
+ viewer.suppressSelectionChanged(false);
+ }
+ }
+ }
+
+ private static void gatherStyledTexts(Control control, List<StyledText> result) {
+ if (control instanceof ExpandableComposite) {
+ ExpandableComposite composite = (ExpandableComposite) control;
+ if (!composite.isExpanded()) {
+ CommonFormUtil.setExpanded(composite, true);
+ }
+ gatherStyledTextsInComposite(composite, result);
+ } else if (control instanceof Composite) {
+ gatherStyledTextsInComposite((Composite) control, result);
+ }
+ }
+
+ private static void gatherStyledTextsInComposite(Composite composite, List<StyledText> result) {
+ if (composite != null && !composite.isDisposed()) {
+ for (Control child : composite.getChildren()) {
+ if (child instanceof StyledText) {
+ result.add((StyledText) child);
+ } else if (child instanceof Composite) {
+ gatherStyledTextsInComposite((Composite) child, result);
+ }
+ }
+ }
+ }
+
+ private static void highlightMatches(String searchString, StyledText styledText) {
+ String text = styledText.getText().toLowerCase();
+ for (int index = 0; index < text.length(); index += searchString.length()) {
+ index = text.indexOf(searchString, index);
+ if (index == -1) {
+ break;
+ }
+ styledText.setStyleRange(new StyleRange(index, searchString.length(), null, HIGHLIGHTER_YELLOW));
+ }
+ }
+
+ private void clearSearchResults() {
+ for (StyledText oldText : styledTexts) {
+ List<StyleRange> otherRanges = new ArrayList<StyleRange>();
+ if (!oldText.isDisposed()) {
+ for (StyleRange styleRange : oldText.getStyleRanges()) {
+ if (styleRange.background == null || !styleRange.background.equals(HIGHLIGHTER_YELLOW)) {
+ otherRanges.add(styleRange); // preserve ranges that aren't from highlighting search results
+ }
+ }
+ oldText.setStyleRanges(otherRanges.toArray(new StyleRange[otherRanges.size()]));
+ }
+ }
+ styledTexts.clear();
+ for (CommentGroupViewer group : commentGroupViewers) {
+ group.clearSectionHyperlink();
+ }
+ }
+
+}
diff --git a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/messages.properties b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/messages.properties
index 598d81eff..26880a509 100644
--- a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/messages.properties
+++ b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/editors/messages.properties
@@ -165,3 +165,6 @@ TaskEditorExtensionSettingsContribution_Rendering_Group_Info=Attributes may over
TaskEditorExtensionSettingsContribution_Select_the_capabilities_of_the_task_editor=Select the capabilities of the task editor
TaskHyperlinkPresenter_Not_In_Task_List=Open {0} from Repository
TaskUrlHyperlink_Open_URL_in_Task_Editor=Open ''{0}'' in Task Editor
+
+TaskEditorFindSupport_Find=Find
+TaskEditorFindSupport_Show_X_more_results=Show {0} more results
diff --git a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ui/editors/AbstractTaskEditorPage.java b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ui/editors/AbstractTaskEditorPage.java
index 5004104d1..70a8895ec 100644
--- a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ui/editors/AbstractTaskEditorPage.java
+++ b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ui/editors/AbstractTaskEditorPage.java
@@ -79,6 +79,7 @@ import org.eclipse.mylyn.internal.tasks.ui.editors.TaskEditorAttributePart;
import org.eclipse.mylyn.internal.tasks.ui.editors.TaskEditorCommentPart;
import org.eclipse.mylyn.internal.tasks.ui.editors.TaskEditorContributionExtensionReader;
import org.eclipse.mylyn.internal.tasks.ui.editors.TaskEditorDescriptionPart;
+import org.eclipse.mylyn.internal.tasks.ui.editors.TaskEditorFindSupport;
import org.eclipse.mylyn.internal.tasks.ui.editors.TaskEditorNewCommentPart;
import org.eclipse.mylyn.internal.tasks.ui.editors.TaskEditorOutlineNode;
import org.eclipse.mylyn.internal.tasks.ui.editors.TaskEditorOutlinePage;
@@ -132,6 +133,7 @@ import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.forms.FormColors;
import org.eclipse.ui.forms.IFormColors;
import org.eclipse.ui.forms.IFormPart;
@@ -503,6 +505,8 @@ public abstract class AbstractTaskEditorPage extends TaskFormPage implements ISe
private FocusTracker focusTracker;
+ private TaskEditorFindSupport findSupport;
+
/**
* @since 3.1
*/
@@ -542,6 +546,9 @@ public abstract class AbstractTaskEditorPage extends TaskFormPage implements ISe
}
public boolean canPerformAction(String actionId) {
+ if (findSupport != null && actionId.equals(ActionFactory.FIND.getId())) {
+ return true;
+ }
return CommonTextSupport.canPerformAction(actionId, EditorUtil.getFocusControl(this));
}
@@ -903,6 +910,9 @@ public abstract class AbstractTaskEditorPage extends TaskFormPage implements ISe
}
public void doAction(String actionId) {
+ if (findSupport != null && actionId.equals(ActionFactory.FIND.getId())) {
+ findSupport.toggleFind();
+ }
CommonTextSupport.doAction(actionId, EditorUtil.getFocusControl(this));
}
@@ -1073,6 +1083,9 @@ public abstract class AbstractTaskEditorPage extends TaskFormPage implements ISe
submitButtonContribution.marginLeft = 10;
toolBarManager.add(submitButtonContribution);
}
+ if (findSupport != null) {
+ findSupport.addFindAction(toolBarManager);
+ }
}
}
@@ -1269,6 +1282,7 @@ public abstract class AbstractTaskEditorPage extends TaskFormPage implements ISe
IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
this.textSupport = new CommonTextSupport(handlerService);
this.textSupport.setSelectionChangedListener(this);
+ createFindSupport();
initModel(taskEditorInput);
@@ -1337,6 +1351,15 @@ public abstract class AbstractTaskEditorPage extends TaskFormPage implements ISe
}
}
+ /**
+ * Subclasses may override to disable the task editor find functionality.
+ *
+ * @since 3.11
+ */
+ protected void createFindSupport() {
+ this.findSupport = new TaskEditorFindSupport(this);
+ }
+
@Override
public boolean isDirty() {
return (getModel() != null && getModel().isDirty()) || (getManagedForm() != null && getManagedForm().isDirty());

Back to the top