Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5b4bbf510b998014db9ba0b11500eed9bd45c7e2 (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
/*******************************************************************************
 * Copyright (c) 2012 Original authors 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:
 *     Original authors and others - initial API and implementation
 ******************************************************************************/
package org.eclipse.nebula.widgets.nattable.persistence;

import static org.eclipse.nebula.widgets.nattable.persistence.IPersistable.DOT;

import java.util.Properties;


import org.eclipse.jface.resource.DataFormatException;
import org.eclipse.jface.resource.StringConverter;
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;

public class ColorPersistor {

	public static final String STYLE_PERSISTENCE_PREFIX = "color"; //$NON-NLS-1$
	public static final Color DEFAULT_COLOR = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);

	public static void saveColor(String prefix, Properties properties, Color color) {
		prefix = prefix + DOT + STYLE_PERSISTENCE_PREFIX;

		if (color == null) {
			return;
		}
		properties.setProperty(prefix, asString(color));
	}


	public static Color loadColor(String prefix, Properties properties) {
		prefix = prefix + DOT + STYLE_PERSISTENCE_PREFIX;

		String colorAsString = properties.getProperty(prefix);
		if (colorAsString == null) {
			return DEFAULT_COLOR;
		} else {
			return asColor(colorAsString);
		}
	}

	/**
	 * Create a String representation of the SWT Color
	 */
	public static String asString(Color color) {
		return StringConverter.asString(color.getRGB());
	}

	/**
	 * Create a Color instance using the String created by {@link ColorPersistor#asColor(String)}
	 */
	public static Color asColor(String colorAsString) {
		try {
			return GUIHelper.getColor(StringConverter.asRGB(colorAsString));
		} catch (DataFormatException e) {
			return DEFAULT_COLOR;
		}
	}
}

Back to the top