1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
package org.eclipse.gef.palette;
|
12
|
|
13
|
import java.util.HashMap;
|
14
|
import java.util.Map;
|
15
|
|
16
|
import org.eclipse.core.runtime.Assert;
|
17
|
import org.eclipse.jface.resource.ImageDescriptor;
|
18
|
|
19
|
import org.eclipse.gef.Tool;
|
20
|
|
21
|
/**
|
22
|
* A factory for returning Tools.
|
23
|
*/
|
24
|
public abstract class ToolEntry extends PaletteEntry {
|
25
|
|
26
|
/** Type Identifier **/
|
27
|
public static final Object PALETTE_TYPE_TOOL = "$Palette Tool";//$NON-NLS-1$
|
28
|
|
29
|
private Map map;
|
30
|
private Class toolClass;
|
31
|
|
32
|
/**
|
33
|
* Creates a new ToolEntry. Any parameter can be <code>null</code>.
|
34
|
*
|
35
|
* @param label
|
36
|
* the entry's name
|
37
|
* @param shortDesc
|
38
|
* the entry's description
|
39
|
* @param iconSmall
|
40
|
* the entry's small icon
|
41
|
* @param iconLarge
|
42
|
* the entry's large icon
|
43
|
*/
|
44
|
public ToolEntry(String label, String shortDesc, ImageDescriptor iconSmall,
|
45
|
ImageDescriptor iconLarge) {
|
46
|
this(label, shortDesc, iconSmall, iconLarge, null);
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Constructor to create a new ToolEntry. Any parameter can be
|
51
|
* <code>null</code>.
|
52
|
*
|
53
|
* @param label
|
54
|
* the entry's name
|
55
|
* @param description
|
56
|
* the entry's description
|
57
|
* @param iconSmall
|
58
|
* the entry's small icon
|
59
|
* @param iconLarge
|
60
|
* the entry's large icon
|
61
|
* @param tool
|
62
|
* the type of tool that this entry uses
|
63
|
* @since 3.1
|
64
|
*/
|
65
|
public ToolEntry(String label, String description,
|
66
|
ImageDescriptor iconSmall, ImageDescriptor iconLarge, Class tool) {
|
67
|
super(label, description, iconSmall, iconLarge, PALETTE_TYPE_TOOL);
|
68
|
setToolClass(tool);
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* Creates the tool of the type specified by {@link #setToolClass(Class)}
|
73
|
* for this ToolEntry. The tool is also configured with the properties set
|
74
|
* in {@link #setToolProperty(Object, Object)}. Sub-classes overriding this
|
75
|
* method should ensure that their tools are also configured with those
|
76
|
* properties.
|
77
|
*
|
78
|
* @return the tool for this entry
|
79
|
*/
|
80
|
public Tool createTool() {
|
81
|
if (toolClass == null)
|
82
|
return null;
|
83
|
Tool tool;
|
84
|
try {
|
85
|
tool = (Tool) toolClass.newInstance();
|
86
|
} catch (IllegalAccessException iae) {
|
87
|
return null;
|
88
|
} catch (InstantiationException ie) {
|
89
|
return null;
|
90
|
}
|
91
|
tool.setProperties(getToolProperties());
|
92
|
return tool;
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* @return the properties set in {@link #setToolProperty(Object, Object)}
|
97
|
* @since 3.1
|
98
|
*/
|
99
|
protected Map getToolProperties() {
|
100
|
return map;
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Returns the property value for the specified property key.
|
105
|
*
|
106
|
* @param key
|
107
|
* the property key
|
108
|
* @return the value for the requested property
|
109
|
* @since 3.1
|
110
|
*/
|
111
|
public Object getToolProperty(Object key) {
|
112
|
if (map != null)
|
113
|
return map.get(key);
|
114
|
return null;
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* Sets the type of tool to be created. This provides clients with a method
|
119
|
* of specifying a different type of tool to be created without having to
|
120
|
* sub-class. The provided class should have a default constructor for this
|
121
|
* to work successfully.
|
122
|
*
|
123
|
* @param toolClass
|
124
|
* the type of tool to be created by this entry
|
125
|
* @since 3.1
|
126
|
*/
|
127
|
public void setToolClass(Class toolClass) {
|
128
|
if (toolClass != null)
|
129
|
Assert.isTrue(Tool.class.isAssignableFrom(toolClass));
|
130
|
this.toolClass = toolClass;
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Clients can use this method to configure the associated tool without
|
135
|
* having to sub-class.
|
136
|
*
|
137
|
* @param key
|
138
|
* the property name
|
139
|
* @param value
|
140
|
* a value of type associated with the given property
|
141
|
* @since 3.1
|
142
|
* @see Tool#setProperties(Map)
|
143
|
*/
|
144
|
public void setToolProperty(Object key, Object value) {
|
145
|
if (map == null)
|
146
|
map = new HashMap();
|
147
|
map.put(key, value);
|
148
|
}
|
149
|
|
150
|
}
|