Skip to main content
summaryrefslogtreecommitdiffstats
blob: f0fb56560f8d786e1ee55d1013129ff1ec56767b (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
/*******************************************************************************
 * Copyright (c) 2004, 2008 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.sandbox.ui.actions;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.commons.ui.CommonImages;
import org.eclipse.mylyn.context.ui.AbstractContextUiBridge;
import org.eclipse.mylyn.context.ui.ContextUi;
import org.eclipse.mylyn.internal.context.ui.ContextUiPlugin;
import org.eclipse.mylyn.internal.sandbox.ui.views.ActiveSearchView;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

/**
 * @author Mik Kersten
 * @since 3.0
 */
public class LinkActiveSearchWithEditorAction extends Action {

	public static final String ID = "org.eclipse.mylyn.ui.views.active.search.link";

	private static final String LABEL = "Link with Editor";

	private final SelectionTracker selectionTracker = new SelectionTracker();

	private static LinkActiveSearchWithEditorAction INSTANCE;

	public LinkActiveSearchWithEditorAction() {
		super(LABEL, IAction.AS_CHECK_BOX);
		INSTANCE = this;
		setId(ID);
		setImageDescriptor(CommonImages.LINK_EDITOR);
		setText(LABEL);
		setToolTipText(LABEL);
		ContextUiPlugin.getDefault().getPreferenceStore().setDefault(ID, true);
		update(ContextUiPlugin.getDefault().getPreferenceStore().getBoolean(ID));
	}

	@Override
	public void run() {
		update(isChecked());
	}

	public void update(boolean on) {
		setChecked(on);
		ContextUiPlugin.getDefault().getPreferenceStore().setValue(ID, on);
		ISelectionService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
		if (on) {
			service.addPostSelectionListener(selectionTracker);
		} else {
			service.removePostSelectionListener(selectionTracker);
		}
	}

	private static class SelectionTracker implements ISelectionListener {
		public void selectionChanged(IWorkbenchPart part, ISelection selection) {
			try {
				if (selection instanceof TextSelection && part instanceof IEditorPart) {
					ActiveSearchView view = ActiveSearchView.getFromActivePerspective();
					if (view == null || !view.getViewer().getControl().isVisible()) {
						return;
					}
					AbstractContextUiBridge bridge = ContextUi.getUiBridgeForEditor((IEditorPart) part);
					Object toSelect = bridge.getObjectForTextSelection((TextSelection) selection, (IEditorPart) part);
					if (toSelect != null && view.getViewer().testFindItem(toSelect) != null) {
						view.getViewer().setSelection(new StructuredSelection(toSelect), true);
					}
				}
			} catch (Throwable t) {
				StatusHandler.log(new Status(IStatus.ERROR, ContextUiPlugin.ID_PLUGIN,
						"Could not update package explorer", t));
			}
		}
	}

	public void dispose() {
		// TODO Auto-generated method stub

	}

	public void runWithEvent(IAction action, Event event) {
		// TODO Auto-generated method stub

	}

	public static LinkActiveSearchWithEditorAction getDefault() {
		return INSTANCE;
	}

	public void propertyChange(PropertyChangeEvent event) {
		// TODO Auto-generated method stub

	}
}

Back to the top