001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.themes;
011:
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.jface.resource.DataFormatException;
014: import org.eclipse.swt.graphics.RGB;
015: import org.eclipse.ui.IPluginContribution;
016: import org.eclipse.ui.internal.misc.StatusUtil;
017: import org.eclipse.ui.statushandlers.StatusManager;
018: import org.eclipse.ui.themes.ColorUtil;
019:
020: /**
021: * A <code>ColorDefiniton </code> is the representation of the extensions
022: * defined by the <code>org.eclipse.ui.colorDefinitions</code> extension point.
023: *
024: * @since 3.0
025: */
026: public class ColorDefinition implements IPluginContribution,
027: IHierarchalThemeElementDefinition,
028: ICategorizedThemeElementDefinition, IEditable {
029:
030: /**
031: * Default color value - black - for colors that cannot be parsed.
032: */
033: private static final RGB DEFAULT_COLOR_VALUE = new RGB(0, 0, 0);
034:
035: private String defaultsTo;
036:
037: private String description;
038:
039: private String id;
040:
041: private String label;
042:
043: private String pluginId;
044:
045: private String rawValue;
046:
047: private String categoryId;
048:
049: boolean isEditable;
050:
051: private RGB parsedValue;
052:
053: /**
054: * Create a new instance of the receiver.
055: *
056: * @param label the label for this definition
057: * @param id the identifier for this definition
058: * @param defaultsTo the id of a definition that this definition will
059: * default to.
060: * @param value the default value of this definition, either in the form
061: * rrr,ggg,bbb or the name of an SWT color constant.
062: * @param description the description for this definition.
063: * @param pluginId the identifier of the plugin that contributed this
064: * definition.
065: */
066: public ColorDefinition(String label, String id, String defaultsTo,
067: String value, String categoryId, boolean isEditable,
068: String description, String pluginId) {
069:
070: this .label = label;
071: this .id = id;
072: this .defaultsTo = defaultsTo;
073: this .rawValue = value;
074: this .categoryId = categoryId;
075: this .description = description;
076: this .isEditable = isEditable;
077: this .pluginId = pluginId;
078: }
079:
080: /**
081: * Create a new instance of the receiver.
082: *
083: * @param original the original definition. This will be used to populate
084: * all fields except defaultsTo and value. defaultsTo will always be
085: * <code>null</code>.
086: * @param value the RGB value
087: */
088: public ColorDefinition(ColorDefinition original, RGB value) {
089:
090: this .label = original.getName();
091: this .id = original.getId();
092: this .categoryId = original.getCategoryId();
093: this .description = original.getDescription();
094: this .isEditable = original.isEditable();
095: this .pluginId = original.getPluginId();
096:
097: this .parsedValue = value;
098: }
099:
100: /**
101: * @return the categoryId, or <code>null</code> if none was supplied.
102: */
103: public String getCategoryId() {
104: return categoryId;
105: }
106:
107: /**
108: * @return the defaultsTo value, or <code>null</code> if none was supplied.
109: */
110: public String getDefaultsTo() {
111: return defaultsTo;
112: }
113:
114: /**
115: * @return the description text, or <code>null</code> if none was supplied.
116: */
117: public String getDescription() {
118: return description;
119: }
120:
121: /**
122: * @return the id of this definition. Should not be <code>null</code>.
123: */
124: public String getId() {
125: return id;
126: }
127:
128: /**
129: * @return the label text. Should not be <code>null</code>.
130: */
131: public String getName() {
132: return label;
133: }
134:
135: /* (non-Javadoc)
136: * @see org.eclipse.ui.IPluginContribution#getLocalId()
137: */
138: public String getLocalId() {
139: return getId();
140: }
141:
142: /* (non-Javadoc)
143: * @see org.eclipse.ui.IPluginContribution#getPluginId()
144: */
145: public String getPluginId() {
146: return pluginId;
147: }
148:
149: /**
150: * @return the value. Any SWT constants supplied to the constructor will be
151: * evaluated and converted into their RGB value.
152: */
153: public RGB getValue() {
154: if (parsedValue == null) {
155: try {
156: parsedValue = ColorUtil.getColorValue(rawValue);
157: } catch (DataFormatException e) {
158: parsedValue = DEFAULT_COLOR_VALUE;
159: IStatus status = StatusUtil
160: .newStatus(
161: IStatus.WARNING,
162: "Could not parse value for theme color " + id, e); //$NON-NLS-1$
163: StatusManager.getManager().handle(status,
164: StatusManager.LOG);
165: }
166: }
167: return parsedValue;
168: }
169:
170: /*
171: * (non-Javadoc)
172: *
173: * @see java.lang.Object#toString()
174: */
175: public String toString() {
176: return getId();
177: }
178:
179: /* (non-Javadoc)
180: * @see org.eclipse.ui.internal.themes.IEditable#isEditable()
181: */
182: public boolean isEditable() {
183: return isEditable;
184: }
185:
186: /* (non-Javadoc)
187: * @see java.lang.Object#equals(java.lang.Object)
188: */
189: public boolean equals(Object obj) {
190: if (obj instanceof ColorDefinition) {
191: return getId().equals(((ColorDefinition) obj).getId());
192: }
193: return false;
194: }
195:
196: /* (non-Javadoc)
197: * @see java.lang.Object#hashCode()
198: */
199: public int hashCode() {
200: return id.hashCode();
201: }
202: }
|