499382: add Flag support for bugs and attachments

Change-Id: I1458b31f5f7f3897d83f7d9745b1df7a173fc702
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=499382
diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.ui/src/org/eclipse/mylyn/internal/bugzilla/rest/ui/BugzillaRestAttachmentTableLabelProvider.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.ui/src/org/eclipse/mylyn/internal/bugzilla/rest/ui/BugzillaRestAttachmentTableLabelProvider.java
new file mode 100644
index 0000000..00f07e4
--- /dev/null
+++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.ui/src/org/eclipse/mylyn/internal/bugzilla/rest/ui/BugzillaRestAttachmentTableLabelProvider.java
@@ -0,0 +1,54 @@
+/*******************************************************************************
+ * Copyright (c) 2017 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.bugzilla.rest.ui;
+
+import org.eclipse.jface.viewers.StyledString;
+import org.eclipse.mylyn.internal.bugzilla.rest.core.IBugzillaRestConstants;
+import org.eclipse.mylyn.internal.tasks.ui.editors.AttachmentTableLabelProvider;
+import org.eclipse.mylyn.tasks.core.ITaskAttachment;
+import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
+
+public class BugzillaRestAttachmentTableLabelProvider extends AttachmentTableLabelProvider {
+
+	@Override
+	public StyledString buildTextFromEventIndex(int index, ITaskAttachment attachment) {
+		if (index == 6) {
+			return getAttachmentFlags(attachment);
+		} else {
+			return super.buildTextFromEventIndex(index, attachment);
+		}
+	}
+
+	public static StyledString getAttachmentFlags(ITaskAttachment attachment) {
+		StyledString text = new StyledString();
+
+		for (TaskAttribute taskAttribute : attachment.getTaskAttribute().getAttributes().values()) {
+			if (taskAttribute.getId().startsWith(IBugzillaRestConstants.KIND_FLAG)) {
+				TaskAttribute stateAttribute = taskAttribute.getAttribute("state");
+				if (text.length() > 0) {
+					text.append("\n");
+				}
+				text.append(stateAttribute.getMetaData().getLabel());
+				text.append(": ");
+				text.append(stateAttribute.getValue());
+				TaskAttribute requesteeAttribute = taskAttribute.getAttribute("requestee");
+				if (!requesteeAttribute.getValue().isEmpty()) {
+					text.append(" ");
+					text.append(requesteeAttribute.getValue(), StyledString.COUNTER_STYLER);
+				}
+
+			}
+		}
+		return text;
+	}
+
+}
diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.ui/src/org/eclipse/mylyn/internal/bugzilla/rest/ui/BugzillaRestTaskEditorAttachmentPart.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.ui/src/org/eclipse/mylyn/internal/bugzilla/rest/ui/BugzillaRestTaskEditorAttachmentPart.java
new file mode 100644
index 0000000..01c7851
--- /dev/null
+++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.ui/src/org/eclipse/mylyn/internal/bugzilla/rest/ui/BugzillaRestTaskEditorAttachmentPart.java
@@ -0,0 +1,53 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Frank Becker 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:
+ *     Frank Becker - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.internal.bugzilla.rest.ui;
+
+import java.util.Arrays;
+
+import org.eclipse.jface.viewers.StyledString;
+import org.eclipse.mylyn.commons.core.CoreUtil;
+import org.eclipse.mylyn.commons.ui.TableColumnDescriptor;
+import org.eclipse.mylyn.internal.tasks.ui.editors.AttachmentTableLabelProvider;
+import org.eclipse.mylyn.internal.tasks.ui.editors.TaskEditorAttachmentPart;
+import org.eclipse.mylyn.tasks.core.ITaskAttachment;
+import org.eclipse.swt.SWT;
+
+public class BugzillaRestTaskEditorAttachmentPart extends TaskEditorAttachmentPart {
+
+	@Override
+	protected TableColumnDescriptor[] createColumnDescriptors() {
+		TableColumnDescriptor[] defined = super.createColumnDescriptors();
+
+		TableColumnDescriptor[] result = Arrays.copyOf(defined, defined.length + 1);
+		result[defined.length] = new TableColumnDescriptor(100, "Flags", SWT.LEFT, false, SWT.DOWN, true);
+		return result;
+	}
+
+	@Override
+	protected AttachmentTableLabelProvider createTableProvider() {
+		return new BugzillaRestAttachmentTableLabelProvider();
+	}
+
+	@Override
+	protected int compareColumn(ITaskAttachment attachment1, ITaskAttachment attachment2, int propertyIndex) {
+		if (propertyIndex == 6) {
+			StyledString flags1 = BugzillaRestAttachmentTableLabelProvider.getAttachmentFlags(attachment1);
+			StyledString flags2 = BugzillaRestAttachmentTableLabelProvider.getAttachmentFlags(attachment2);
+			String flags1String = flags1 != null ? flags1.getString() : null;
+			String flags2String = flags1 != null ? flags2.getString() : null;
+			return CoreUtil.compare(flags1String, flags2String);
+		} else {
+			return super.compareColumn(attachment1, attachment2, propertyIndex);
+		}
+	}
+
+}
diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.ui/src/org/eclipse/mylyn/internal/bugzilla/rest/ui/BugzillaRestTaskEditorPage.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.ui/src/org/eclipse/mylyn/internal/bugzilla/rest/ui/BugzillaRestTaskEditorPage.java
index 420ce3c..6f55bc4 100644
--- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.ui/src/org/eclipse/mylyn/internal/bugzilla/rest/ui/BugzillaRestTaskEditorPage.java
+++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.ui/src/org/eclipse/mylyn/internal/bugzilla/rest/ui/BugzillaRestTaskEditorPage.java
@@ -11,6 +11,7 @@
 
 package org.eclipse.mylyn.internal.bugzilla.rest.ui;
 
+import java.util.ArrayList;
 import java.util.Set;
 
 import org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestCore;
@@ -72,12 +73,18 @@
 	protected Set<TaskEditorPartDescriptor> createPartDescriptors() {
 		Set<TaskEditorPartDescriptor> descriptors = super.createPartDescriptors();
 		// remove unnecessary default editor parts
+		ArrayList<TaskEditorPartDescriptor> descriptorsToRemove = new ArrayList<TaskEditorPartDescriptor>(2);
+		boolean hasAttachmentPart = false;
 		for (TaskEditorPartDescriptor taskEditorPartDescriptor : descriptors) {
-			if (taskEditorPartDescriptor.getId().equals(ID_PART_PEOPLE)) {
-				descriptors.remove(taskEditorPartDescriptor);
-				break;
+			if (taskEditorPartDescriptor.getId().equals(ID_PART_PEOPLE)
+					|| taskEditorPartDescriptor.getId().equals(ID_PART_ATTACHMENTS)) {
+				hasAttachmentPart = hasAttachmentPart || taskEditorPartDescriptor.getId().equals(ID_PART_ATTACHMENTS);
+				descriptorsToRemove.add(taskEditorPartDescriptor);
+				continue;
 			}
 		}
+		descriptors.removeAll(descriptorsToRemove);
+
 		// Add the updated Bugzilla people part
 		descriptors.add(new TaskEditorPartDescriptor(ID_PART_PEOPLE) {
 			@Override
@@ -92,6 +99,15 @@
 			}
 		}.setPath(ID_PART_ATTRIBUTES + "/" + PATH_FLAGS)); //$NON-NLS-1$
 
+		if (hasAttachmentPart) {
+			descriptors.add(new TaskEditorPartDescriptor(ID_PART_ATTACHMENTS) {
+				@Override
+				public AbstractTaskEditorPart createPart() {
+					return new BugzillaRestTaskEditorAttachmentPart();
+				}
+			}.setPath(PATH_ATTACHMENTS));
+		}
+
 		return descriptors;
 	}