Skip to main content
summaryrefslogtreecommitdiffstats
blob: 84097618ee5ea111cd6f6b499d672851c0ce3a54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*******************************************************************************
 * Copyright (c) 2003 - 2006 University Of British Columbia 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:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylyn.internal.bugzilla.ui.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaPlugin;
import org.eclipse.mylyn.internal.bugzilla.core.IBugzillaConstants;
import org.eclipse.mylyn.internal.bugzilla.core.internal.Favorite;
import org.eclipse.mylyn.internal.bugzilla.ui.BugzillaImages;
import org.eclipse.mylyn.internal.bugzilla.ui.FavoritesView;
import org.eclipse.mylyn.internal.bugzilla.ui.search.BugzillaSearchResultView;

/**
 * Bookmark a returned Bugzilla Search result.
 */
public class AddFavoriteAction extends AbstractFavoritesAction {

	/** Selected objects */
	private List<Favorite> selected;

	/** The view where the Bugzilla search results are displayed */
	private BugzillaSearchResultView resultView;

	/**
	 * Constructor
	 * 
	 * @param text
	 *            The label for the context menu item
	 * @param resultView
	 *            The view where the Bugzilla search results are displayed
	 */
	public AddFavoriteAction(String text, BugzillaSearchResultView resultView) {
		setText(text);
		setImageDescriptor(BugzillaImages.IMG_TOOL_ADD_TO_FAVORITES);
		this.resultView = resultView;
		selected = null;
	}

	/**
	 * Bookmark the selected items
	 * 
	 * @see org.eclipse.ui.actions.ActionDelegate#run(IAction)
	 */
	@Override
	public void run() {
		FavoritesView.checkWindow();

		selected = new ArrayList<Favorite>();
		buildSelection();

		// go through each of the selected items and add its data to the
		// favorites file
		for (int i = 0; i < selected.size(); i++) {
			BugzillaPlugin.getDefault().getFavorites().add(selected.get(i));
		}
		FavoritesView.add();
		FavoritesView.updateActionEnablement();
	}

	/**
	 * Gets the entire selection and puts each bug report into a list. Only the
	 * relevent parts of each bug report are put into the list.
	 */
	@SuppressWarnings("unchecked")
	private void buildSelection() {
		ISelection s = resultView.getViewer().getSelection();
		if (s instanceof IStructuredSelection) {
			IStructuredSelection selection = (IStructuredSelection) s;
			for (Iterator<IMarker> it = selection.iterator(); it.hasNext();) {
				IMarker marker = it.next();

				try {
					// try to get the attribute for the marker
					Integer attributeId = (Integer) marker.getAttribute(IBugzillaConstants.HIT_MARKER_ATTR_ID);
					String repositoryUrl = (String) marker.getAttribute(IBugzillaConstants.HIT_MARKER_ATTR_REPOSITORY);

					// add the selected item to the list of items that are
					// selected
					String description = (String) marker.getAttribute(IBugzillaConstants.HIT_MARKER_ATTR_DESC);
					String query = (String) marker.getAttribute(IBugzillaConstants.HIT_MARKER_ATTR_QUERY);

					// create a map to add attributes to so that the favorites
					// file can sort
					HashMap<String, Object> attributes = new HashMap<String, Object>();
					attributes.put(IBugzillaConstants.HIT_MARKER_ATTR_ID, attributeId);
					attributes.put(IBugzillaConstants.HIT_MARKER_ATTR_REPOSITORY, repositoryUrl);
					attributes.put(IBugzillaConstants.HIT_MARKER_ATTR_PRIORITY, marker
							.getAttribute(IBugzillaConstants.HIT_MARKER_ATTR_PRIORITY));
					attributes.put(IBugzillaConstants.HIT_MARKER_ATTR_SEVERITY, marker
							.getAttribute(IBugzillaConstants.HIT_MARKER_ATTR_SEVERITY));
					attributes.put(IBugzillaConstants.HIT_MARKER_ATTR_STATE, marker
							.getAttribute(IBugzillaConstants.HIT_MARKER_ATTR_STATE));
					attributes.put(IBugzillaConstants.HIT_MARKER_ATTR_RESULT, marker
							.getAttribute(IBugzillaConstants.HIT_MARKER_ATTR_RESULT));

					Favorite favorite = new Favorite(repositoryUrl, attributeId.intValue(), description, query,
							attributes);
					selected.add(favorite);
				} catch (CoreException ignored) {
					// do nothing
				}
			}
		}
	}

}

Back to the top