Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 552923f308b9c06afa53595df7d88764989ad9d6 (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
/*******************************************************************************
 * Copyright (c) 2010, 2017 IBM Corporation 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:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui.wizards;

import java.util.Vector;

import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.team.internal.ui.TeamUIPlugin;

public abstract class PsfStore {
	// Most recently used filename is first in the array.
	// Least recently used filename is at the end of the list.
	// When the list overflows, items drop off the end.
	private static final int HISTORY_LENGTH = 10;

	private static final String STORE_SECTION = "ImportPSFDialog"; //$NON-NLS-1$

	private static IDialogSettings _section;

	protected abstract String getPreviousTag();
	protected abstract String getListTag();

	public abstract String getSuggestedDefault();

	protected String getPrevious() {
		IDialogSettings section = getSettingsSection();
		String retval = section.get(getPreviousTag());
		if (retval == null) {
			retval = ""; //$NON-NLS-1$
		}
		return retval;
	}

	public String[] getHistory() {
		IDialogSettings section = getSettingsSection();
		String[] arr = section.getArray(getListTag());
		if (arr == null) {
			arr = new String[0];
		}
		return arr;
	}

	public void remember(String filename) {
		Vector<String> filenames = createVector(getHistory());
		if (filenames.contains(filename)) {
			// The item is in the list. Remove it and add it back at the
			// beginning. If it already was at the beginning this will be a
			// waste of time, but it's not even measurable so I don't care.
			filenames.remove(filename);
		}
		// Most recently used filename goes to the beginning of the list
		filenames.add(0, filename);

		// Forget any overflowing items
		while (filenames.size() > HISTORY_LENGTH) {
			filenames.remove(HISTORY_LENGTH);
		}

		// Make it an array
		String[] arr = filenames.toArray(new String[filenames.size()]);

		IDialogSettings section = getSettingsSection();
		section.put(getListTag(), arr);
		section.put(getPreviousTag(), filename);
	}

	private Vector<String> createVector(String[] arr) {
		Vector<String> v = new Vector<>();
		for (int ix = 0; ix < arr.length; ++ix) {
			v.add(ix, arr[ix]);
		}
		return v;
	}

	private IDialogSettings getSettingsSection() {
		if (_section != null)
			return _section;

		IDialogSettings settings = TeamUIPlugin.getPlugin().getDialogSettings();
		_section = settings.getSection(STORE_SECTION);
		if (_section != null)
			return _section;

		_section = settings.addNewSection(STORE_SECTION);
		return _section;
	}
}

Back to the top