Skip to main content
summaryrefslogtreecommitdiffstats
blob: 303689b31fb3817b352dc72b97154d9c91feb1d6 (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
/*******************************************************************************
 * Copyright (c) 2012 Tasktop Technologies and others.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * https://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.index.ui;

import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.mylyn.internal.tasks.core.IRepositoryModelListener;
import org.eclipse.mylyn.internal.tasks.index.core.TaskListIndex;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.tasks.core.IRepositoryManager;

/**
 * @author David Green
 * @author Steffen Pingel
 */
public class IndexReference extends AbstractIndexReference {

	private static TaskListIndex theIndex;

	private static AtomicInteger referenceCount = new AtomicInteger();

	private static IRepositoryModelListener listener = new IRepositoryModelListener() {
		public void loaded() {
			synchronized (IndexReference.class) {
				if (theIndex != null) {
					theIndex.setLocation(getDefaultIndexLocation());
				}
			}
		}
	};

	static File getDefaultIndexLocation() {
		return new File(TasksUiPlugin.getDefault().getDataDirectory(), ".taskListIndex"); //$NON-NLS-1$
	}

	/**
	 * When not null serves as flag indicating that theIndex is referenced, thus preventing bad behaviour if dispose is
	 * called multiple times.
	 */
	private TaskListIndex index;

	@Override
	public TaskListIndex index() {
		synchronized (IndexReference.class) {
			if (index == null) {
				if (theIndex == null && TasksUiPlugin.getTaskDataManager() != null) {
					final IRepositoryManager repositoryManager = TasksUiPlugin.getRepositoryManager();
					theIndex = new TaskListIndex(TasksUiPlugin.getTaskList(), TasksUiPlugin.getTaskDataManager(),
							repositoryManager, getDefaultIndexLocation());
					TasksUiPlugin.getDefault().addModelListener(listener);
				}
				index = theIndex;
				referenceCount.incrementAndGet();
			}
		}
		return index;
	}

	public void dispose() {
		synchronized (IndexReference.class) {
			if (index != null) {
				index = null;

				if (referenceCount.decrementAndGet() == 0) {
					TasksUiPlugin.getDefault().removeModelListener(listener);
					theIndex.close();
					theIndex = null;
				}
			}
		}
	}

}

Back to the top