Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 87b7702bc71bf7542e9263c7058ab4b2d40c5e6a (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
/*******************************************************************************
 * Copyright (c) 2009 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
 *******************************************************************************/
//------------------------------------------------------------------------------
// Copyright (c) 2005, 2007 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 implementation
//------------------------------------------------------------------------------
package org.eclipse.epf.richtext.actions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

import org.eclipse.epf.richtext.IRichText;
import org.eclipse.epf.richtext.RichTextCommand;
import org.eclipse.epf.richtext.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

/**
 * Sets the font name for the selected text in a rich text control.
 * 
 * @author Kelvin Low
 * @author Jeff Hardy
 * @since 1.0
 */
public class FontNameAction extends RichTextComboAction {

	/**
	 * Creates a new instance.
	 */
	public FontNameAction(final IRichText richText) {
		super(richText);
		setToolTipText(Messages.fontNameAction_toolTipText);
		// get system fonts
		SortedSet<String> fontSet = new TreeSet<String>();
		FontData[] fonts = Display.getCurrent().getFontList(null, true);
		for (int i = 0; i < fonts.length; i++) {
			fontSet.add(((FontData) fonts[i]).getName());
		}
		input = new ArrayList<String>();
		input.add(Messages.fontNameAction_DefaultFontName);
		for (Iterator<String> iter = fontSet.iterator(); iter.hasNext();) {
			String fontName = iter.next();
			input.add(fontName);
		}
		
		// add listener
		richText.addListener(SWT.SELECTED, new Listener() {
			public void handleEvent(Event event) {
				String fontName = richText.getSelected().getFontName();
				if (fontName
						.equals(Messages.fontNameAction_CSS_Default)
						|| fontName
								.equals(Messages.fontNameAction_CSS_Default_Mozilla)
						|| fontName.equals("default")) { //$NON-NLS-1$
					fontName = Messages.fontNameAction_DefaultFontName;
				}
				int index = findFontNameInItems(fontName);
				setNotifyListeners(false);
				getCCombo().select(index);
				setNotifyListeners(true);
			}
		});
	}

	private int findFontNameInItems(String fontName) {
		int index = -1;
		for (Iterator<String> iter = input.iterator(); iter.hasNext();) {
			String font = iter.next();
			index++;
			if (font.equalsIgnoreCase(fontName)) {
				return index;
			}
		}
		return -1;
	}
	
	/**
	 * Executes the action.
	 * 
	 * @param richText
	 *            a rich text control
	 * @param index
	 *            the index of the selected item
	 */
	public void execute(IRichText richText) {
		if (richText != null) {
			String selected = getCComboSelection();
			if (selected.equals(Messages.fontNameAction_DefaultFontName)) {
				richText.executeCommand(RichTextCommand.SET_FONT_NAME, ""); //$NON-NLS-1$
			} else {
				richText.executeCommand(RichTextCommand.SET_FONT_NAME, selected);
			}
		}
	}

	@Override
	public Collection<String> getInput() {
		return input;
	}

}

Back to the top