Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3f93db0e69c9c562dcfd179c3180954d2c467db9 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*******************************************************************************
 * Copyright (c) 2006 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.swt.examples.graphics;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Resource;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

/**
 * This class utilizes the factory design pattern to create menus that may
 * contain color items, pattern items and gradient items. To create a menu:
 * first set the menu items that you wish to have appear in the menu by calling
 * the setters (setColorItems(), setPatternItems(), setGradientItems()), and
 * then call createMenu() to get an instance of a menu. By default, the menu
 * will contain color items.
 */
public class ColorMenu {

	boolean enableColorItems, enablePatternItems, enableGradientItems;
	
	public ColorMenu() {
		enableColorItems = true;
	}
	
	/**
	 * Method used to specify whether or not the color menu items will appear in
	 * the menu.
	 * 
	 * @param enable
	 *            A boolean flag - true to make the color menu items visible in
	 *            the menu; false otherwise.
	 */
	public void setColorItems(boolean enable) {
		enableColorItems = enable;
	}
	
	/**
	 * @return true if color menu items are contained in the menu; false otherwise.
	 * */
	public boolean getColorItems() {
		return enableColorItems;
	}
	
	/**
	 * Method used to specify whether or not the pattern menu items will appear
	 * in the menu.
	 * 
	 * @param enable
	 *            A boolean flag - true to make the pattern menu items visible
	 *            in the menu; false otherwise.
	 */
	public void setPatternItems(boolean enable) {
		enablePatternItems = enable;
	}
	
	/**
	 * @return true if pattern menu items are contained in the menu; false otherwise.
	 * */
	public boolean getPatternItems() {
		return enablePatternItems;
	}
	
	/**
	 * Method used to specify whether or not the gradient menu items will appear
	 * in the menu.
	 * 
	 * @param enable
	 *            A boolean flag - true to make the gradient menu items visible
	 *            in the menu; false otherwise.
	 */
	public void setGradientItems(boolean enable) {
		enableGradientItems = enable;
	}
	
	/**
	 * @return true if gradient menu items are contained in the menu; false otherwise.
	 */
	public boolean getGradientItems() {
		return enableGradientItems;
	}
	
	/**
	 * Creates and returns the menu based on the settings provided via
	 * setColorItems(), setPatternItems() and setGradientItems()
	 * 
	 * @return A menu based on the settings
	 */
	public Menu createMenu(Control parent, ColorListener cl) {
		Menu menu = new Menu(parent);
	
		MenuItemListener menuItemListener = createMenuItemListener(parent);
		menu.addListener(SWT.Selection, menuItemListener);
		menu.addListener(SWT.Dispose, menuItemListener);
		menuItemListener.setColorListener(cl);

		if (enableColorItems) {
			addColorItems(menu, menuItemListener, menuItemListener.getMenuResources());
		}
		if (enablePatternItems) {
			addPatternItems(menu, menuItemListener, menuItemListener.getMenuResources());
		}
		if (enableGradientItems) {
			addGradientItems(menu, menuItemListener);
		}
		return menu;
	}
	
	/** Adds the colors items to the menu. */
	private void addColorItems(Menu menu, MenuItemListener menuListener,
			ArrayList<Resource> menuResources) {
		Display display = menu.getDisplay();
		
		if (menu.getItemCount() != 0) {
			new MenuItem(menu, SWT.SEPARATOR);
		}
		
		// color names
		String[] names = new String[]{
			GraphicsExample.getResourceString("White"), //$NON-NLS-1$
			GraphicsExample.getResourceString("Black"), //$NON-NLS-1$
			GraphicsExample.getResourceString("Red"), //$NON-NLS-1$
			GraphicsExample.getResourceString("Green"), //$NON-NLS-1$
			GraphicsExample.getResourceString("Blue"), //$NON-NLS-1$
			GraphicsExample.getResourceString("Yellow"), //$NON-NLS-1$
			GraphicsExample.getResourceString("Cyan"), //$NON-NLS-1$
		};
		
		// colors needed for the background menu
		Color[] colors = new Color[]{
			display.getSystemColor(SWT.COLOR_WHITE),
			display.getSystemColor(SWT.COLOR_BLACK),
			display.getSystemColor(SWT.COLOR_RED),
			display.getSystemColor(SWT.COLOR_GREEN),
			display.getSystemColor(SWT.COLOR_BLUE),
			display.getSystemColor(SWT.COLOR_YELLOW),
			display.getSystemColor(SWT.COLOR_CYAN),
		};
	
		// add standard color items to menu
		for (int i = 0; i < names.length; i++) {
			MenuItem item = new MenuItem(menu, SWT.NONE);
			item.setText(names[i]);
			item.addListener(SWT.Selection, menuListener);
			Color color = colors[i];
			GraphicsBackground gb = new GraphicsBackground();
			Image image = GraphicsExample.createImage(display, color);
			gb.setBgColor1(color);
			gb.setBgImage(image);
			gb.setThumbNail(image);
			menuResources.add(image);
			item.setImage(image);
			item.setData(gb);
		}
		
		// add custom color item to menu
		menuListener.customColorMI = new MenuItem(menu, SWT.NONE);
		menuListener.customColorMI.setText(GraphicsExample.getResourceString("CustomColor")); //$NON-NLS-1$
		menuListener.customColorMI.addListener(SWT.Selection, menuListener);
		GraphicsBackground gb = new GraphicsBackground();
		menuListener.customColorMI.setData(gb);		
	}
	
	/** Adds the pattern items to the menu. */
	private void addPatternItems(Menu menu, MenuItemListener menuListener,
			ArrayList<Resource> menuResources) {
		Display display = menu.getDisplay();
		
		if (menu.getItemCount() != 0) {
			new MenuItem(menu, SWT.SEPARATOR);
		}
		
		// pattern names
		String[] names = new String[]{
			GraphicsExample.getResourceString("Pattern1"), //$NON-NLS-1$
			GraphicsExample.getResourceString("Pattern2"), //$NON-NLS-1$
			GraphicsExample.getResourceString("Pattern3"), //$NON-NLS-1$
		};
		
		// pattern images
		Image[] images = new Image[]{
			loadImage(display, "pattern1.jpg", menuResources),	
			loadImage(display, "pattern2.jpg", menuResources),
			loadImage(display, "pattern3.jpg", menuResources),
		};
		
		// add the pre-defined patterns to the menu
		for (int i = 0; i < names.length; i++) {
			MenuItem item = new MenuItem(menu, SWT.NONE);
			item.setText(names[i]);
			item.addListener(SWT.Selection, menuListener);
			Image image = images[i];
			GraphicsBackground gb = new GraphicsBackground();			
			gb.setBgImage(image);
			gb.setThumbNail(image);
			item.setImage(image);
			item.setData(gb);
		}
		
		// add the custom pattern item	
		menuListener.customPatternMI = new MenuItem(menu, SWT.NONE);
		menuListener.customPatternMI.setText(GraphicsExample.getResourceString("CustomPattern")); //$NON-NLS-1$
		menuListener.customPatternMI.addListener(SWT.Selection, menuListener);
		GraphicsBackground gb = new GraphicsBackground();
		menuListener.customPatternMI.setData(gb);			
	}
	
	/** Adds the gradient menu item. */
	private void addGradientItems(Menu menu, MenuItemListener menuListener) {
		if (menu.getItemCount() != 0) {
			new MenuItem(menu, SWT.SEPARATOR);
		}
		menuListener.customGradientMI = new MenuItem(menu, SWT.NONE);
		menuListener.customGradientMI.setText(GraphicsExample.getResourceString("Gradient")); //$NON-NLS-1$
		menuListener.customGradientMI.addListener(SWT.Selection, menuListener);
		GraphicsBackground gb = new GraphicsBackground();
		menuListener.customGradientMI.setData(gb);
	}
	
	/** Creates and returns the listener for menu items. */
	private MenuItemListener createMenuItemListener(final Control parent) {
		return new MenuItemListener(parent);
	}
	
	/**
	 * Creates and returns an instance of Image using on the path of an image.
	 * 
	 * @param display
	 *            A Display
	 * @param name
	 *            The path of the image file
	 * @param resources
	 *            The list of resources of the menu
	 */
	private Image loadImage(Display display, String name, ArrayList<Resource> resources) {
		Image image = GraphicsExample.loadImage(display, GraphicsExample.class, name);
		if (image != null) resources.add(image);
		return image;
	}

	/**
	 * An inner class used as a listener for MenuItems added to the menu.
	 */
	static class MenuItemListener implements Listener {
		MenuItem customColorMI, customPatternMI, customGradientMI;	// custom menu items
		Control parent;
		Image customImage, customImageThumb;
		Color customColor;
		GraphicsBackground background;	// used to store information about the background
		ColorListener colorListener;
		ArrayList<Resource> resources;
		
		public MenuItemListener(Control parent){
			this.parent = parent; 
			resources = new ArrayList<Resource>();
		}
		/**
		 * Method used to set the ColorListener
		 * 
		 * @param cl
		 *            A ColorListener
		 * @see org.eclipse.swt.examples.graphics.ColorListener.java
		 */
		public void setColorListener(ColorListener cl) {
			this.colorListener = cl;
		}
		
		public ArrayList<Resource> getMenuResources() {
			return resources;
		}

		@Override
		public void handleEvent(Event event) {
			switch (event.type) {

			case SWT.Dispose:
				for (int i = 0; i < resources.size(); i++) {
					resources.get(i).dispose();
				}
				resources = new ArrayList<Resource>();
				break;
			case SWT.Selection:
				Display display = event.display;
				MenuItem item = (MenuItem) event.widget;
				if (customColorMI == item) {
					ColorDialog dialog = new ColorDialog(parent.getShell());
					if (customColor != null && !customColor.isDisposed()) {
						dialog.setRGB(customColor.getRGB());
					}
					RGB rgb = dialog.open();
					if (rgb == null) return;
					if (customColor != null) customColor.dispose();
					customColor = new Color(display, rgb);
					if (customPatternMI != null) customPatternMI.setImage(null);
					if (customGradientMI != null) customGradientMI.setImage(null);
					if (customImage != null) customImage.dispose();
					customImage = GraphicsExample.createImage(display, customColor);
					GraphicsBackground gb = new GraphicsBackground();
					gb.setBgImage(customImage);
					gb.setThumbNail(customImage);
					gb.setBgColor1(customColor);
					item.setData(gb);
					item.setImage(customImage);
					resources.add(customColor);
					resources.add(customImage);
				} else if (customPatternMI == item) {
					FileDialog dialog = new FileDialog(parent.getShell());
					dialog.setFilterExtensions(new String[] { "*.jpg", "*.gif",	"*.*" });
					String name = dialog.open();
					if (name == null) return;
					if (customColorMI != null) customColorMI.setImage(null);
					if (customGradientMI != null) customGradientMI.setImage(null);
					if (customColor != null) customColor.dispose();
					if (customImage != null) customImage.dispose();
					if (customImageThumb != null) customImageThumb.dispose();
					customImage = new Image(display, name);
					customImageThumb = GraphicsExample.createThumbnail(display, name);
					GraphicsBackground gb = new GraphicsBackground();
					gb.setBgImage(customImage);
					gb.setThumbNail(customImageThumb);
					item.setData(gb);
					item.setImage(customImageThumb);
					resources.add(customImageThumb);
				} else if (customGradientMI == item) {
					GradientDialog dialog = new GradientDialog(parent.getShell());
					if (background != null) {
						if (background.getBgColor1() != null)
							dialog.setFirstRGB(background.getBgColor1().getRGB());
						if (background.getBgColor2() != null)
							dialog.setSecondRGB(background.getBgColor2().getRGB());						
					}					
					if (dialog.open() != SWT.OK) return;
					Color colorA = new Color(display, dialog.getFirstRGB());
					Color colorB = new Color(display, dialog.getSecondRGB());
					if (colorA == null || colorB == null) return;
					if (customColorMI != null) customColorMI.setImage(null);
					if (customPatternMI != null) customPatternMI.setImage(null);
					if (customColor != null) customColor.dispose();
					if (customImage != null) customImage.dispose();
					customImage = GraphicsExample.createImage(display, colorA, 
							colorB, 16, 16);
					GraphicsBackground gb = new GraphicsBackground();
					gb.setBgImage(customImage);
					gb.setThumbNail(customImage);
					gb.setBgColor1(colorA);
					gb.setBgColor2(colorB);
					item.setData(gb);
					item.setImage(customImage);
					resources.add(colorA);
					resources.add(colorB);
					resources.add(customImage);
				} else {
					if (customColorMI != null) customColorMI.setImage(null);
					if (customPatternMI != null) customPatternMI.setImage(null);
					if (customGradientMI != null) customGradientMI.setImage(null);
				}
				background = (GraphicsBackground) item.getData();
				colorListener.setColor(background);
				break;
			}
		}
	}
}

Back to the top