Skip to main content
summaryrefslogtreecommitdiffstats
blob: 46556e418fa08da5fc0a6bc8b962c32b785e632d (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.editors.palette;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.Tool;
import org.eclipse.gef.palette.MarqueeToolEntry;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.SelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.editors.pagedesigner.PageDesignerResources;
import org.eclipse.jst.pagedesigner.itemcreation.ItemToolEntry;
import org.eclipse.jst.pagedesigner.tools.RangeSelectionTool;

/**
 * @author mengbo
 */
public class DesignerPaletteRoot extends PaletteRoot {
	private IPaletteItemManager _manager;

	private final static ImageDescriptor DEFAULT_SMALL_ICON = PDPlugin
			.getDefault().getImageDescriptor(
					"palette/GENERIC/small/PD_Palette_Default.gif");

	private final static ImageDescriptor DEFAULT_LARGE_ICON = PDPlugin
			.getDefault().getImageDescriptor(
					"palette/GENERIC/large/PD_Palette_Default.gif");

	// _showAll will be initialized in init()
	// private boolean _showAll;

	/**
	 * Creates a new JSFPaletteRoot instance.
	 */
	public DesignerPaletteRoot(IPaletteItemManager manager) {
		// create root
		super();

		this._manager = manager;
		setupBasicItems();
		loadItems();

		// TODO: register listener on the manager for toolpalette change event.

	}

	public IPaletteItemManager getPaletteManager() {
		return this._manager;
	}

	protected void setupBasicItems() {
		// Preferences prefs = PDPlugin.getDefault().getPluginPreferences();
		// _showAll = prefs.getBoolean(IJMTConstants.PREF_PALETTE_SHOW_ALL);

		// a group of default control tools
		// JSFPalette.DefaultGroup.LabelJSFPallete=Controls
		PaletteGroup controls = new PaletteGroup(PageDesignerResources
				.getInstance().getString(
						"JSFPalette.DefaultGroup.LabelJSFPallete")); //$NON-NLS-1$
		add(controls);
		// the selection tool
		ToolEntry tool = new SelectionToolEntry() {
			public Tool createTool() {
				return new RangeSelectionTool();
			}
		};
		controls.add(tool);

		// use selection tool as default entry
		setDefaultEntry(tool);

		// the marquee selection tool
		controls.add(new MarqueeToolEntry());
	}

	public void loadItems() {
		// _showAll =
		// PDPlugin.getDefault().getPluginPreferences().getBoolean(IJMTConstants.PREF_PALETTE_SHOW_ALL);
		// remove other things first.
		removeItems();

		PaletteEntry entry;

		List categories = _manager.getAllCategories();
		List drawers = new ArrayList(categories.size());
		for (int i = 0, m = categories.size(); i < m; i++) {
			IPaletteItemCategory cat = (IPaletteItemCategory) categories.get(i);
			PaletteDrawer drawer = new PaletteDrawer(cat.getLabel());
			cat.setPaletteEntry(drawer);
			drawer.setId(cat.getId());
			drawer.setLabel(cat.getLabel());
			drawer.setDescription(cat.getDescription());
			drawer.setVisible(cat.isVisible());
			drawer.setInitialState(cat.getInitialState());
			// if (i==categories.size()-1)
			// {
			// drawer.setInitialState(PaletteDrawer.INITIAL_STATE_OPEN);
			// }
			// else
			// {
			// drawer.setInitialState(PaletteDrawer.INITIAL_STATE_CLOSED);
			// }
			List items = cat.getPaletteItems();
			for (int j = 0, n = items.size(); j < n; j++) {
				entry = createEntry((IPaletteItemDescriptor) items.get(j));
				if (entry != null) {
					drawer.add(entry);
				}
			}
			drawers.add(drawer);
		}
		// ok, add all the drawers at once
		this.addAll(drawers);
	}

	/**
	 * @param descriptor
	 * @return
	 */
	private PaletteEntry createEntry(IPaletteItemDescriptor descriptor) {
		// check expert flag.
		// if (descriptor.isVisible() && !_showAll)
		// return null;

		// XXX: it is possible to let descriptor to implement certain interface
		// to provide
		// special palette entry other than the default one.
		String label = descriptor.getLabel();
		if (label == null || label.length() == 0) {
			label = descriptor.getTagName();
		}
		String shortDesc = descriptor.getDescription();
		if (shortDesc == null || shortDesc.length() == 0) {
			shortDesc = label;
		}
		shortDesc = formatDescription(shortDesc);
		ImageDescriptor iconSmall = descriptor.getSmallIcon();
		if (iconSmall == null) {
			iconSmall = getDefaultSmallIcon();
		}
		ImageDescriptor iconLarge = descriptor.getLargeIcon();
		if (iconLarge == null) {
			iconLarge = getDefaultLargeIcon();
		}

		// TODO: suggested prefix is null now. We may also put that into
		// itemdescriptor
		// CreationFactory factory = new
		// NodeCreationFactory(descriptor.getURI(), descriptor.getTagName(),
		// null, descriptor.getInitialAttributes());

		// CreationToolEntry entry = new CreationToolEntry(label, shortDesc,
		// factory, iconSmall, iconLarge);
		ItemToolEntry entry = new ItemToolEntry(label, shortDesc, iconSmall,
				iconLarge, descriptor);
		entry.setId(descriptor.getId());

		boolean isVisible = descriptor.isVisible();
		entry.setVisible(isVisible);
		// if (_showAll)
		// {
		// entry.setVisible(true);
		// }
		// else
		// {
		// entry.setVisible(descriptor.isVisible());
		// }
		descriptor.setPaletteEntry(entry);
		return entry;
	}

	private String formatDescription(String desc) {
		if (desc.indexOf("\n") != -1) {
			String[] fragments = desc.split("\n");
			StringBuffer result = new StringBuffer();
			for (int i = 0, n = fragments.length; i < n; i++) {
				result.append(fragments[i].trim()).append(' ');
			}
			desc = result.toString().trim();
		}
		return desc;
	}

	/**
	 * @return
	 */
	private ImageDescriptor getDefaultLargeIcon() {
		return DEFAULT_LARGE_ICON;
	}

	/**
	 * @return
	 */
	private ImageDescriptor getDefaultSmallIcon() {
		return DEFAULT_SMALL_ICON;
	}

	/**
	 * remove everything from the paletteroot
	 * 
	 */
	protected void removeItems() {
		// we try to remove anything other than the basic
		// group that have the selectentry and marqeeentry
		List children = new ArrayList(getChildren());
		children.remove(0); // remove the first one
		for (int i = 0, n = children.size(); i < n; i++) {
			this.remove((PaletteEntry) children.get(i));
		}
	}

	/**
	 * refresh the palette, normally caused by preference change.
	 */
	public void refresh() {
		loadItems();
	}
}

Back to the top