1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.eclipse.adt.internal.editors.values.uimodel; 18 19 import com.android.ide.eclipse.adt.internal.editors.descriptors.TextValueDescriptor; 20 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiAttributeNode; 21 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode; 22 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiTextValueNode; 23 24 import org.eclipse.jface.dialogs.IMessageProvider; 25 import org.eclipse.swt.events.DisposeEvent; 26 import org.eclipse.swt.events.DisposeListener; 27 import org.eclipse.swt.events.ModifyEvent; 28 import org.eclipse.swt.events.ModifyListener; 29 import org.eclipse.swt.widgets.Text; 30 31 import java.util.regex.Pattern; 32 33 /** 34 * Displays and edits a color XML element value with a custom validator. 35 * <p/> 36 * See {@link UiAttributeNode} for more information. 37 */ 38 public class UiColorValueNode extends UiTextValueNode { 39 40 /** Accepted RGBA formats are one of #RGB, #ARGB, #RRGGBB or #AARRGGBB. */ 41 private static final Pattern RGBA_REGEXP = Pattern.compile( 42 "#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})"); //$NON-NLS-1$ 43 44 public UiColorValueNode(TextValueDescriptor attributeDescriptor, UiElementNode uiParent) { 45 super(attributeDescriptor, uiParent); 46 } 47 48 /* (non-java doc) 49 * 50 * Add a modify listener that will check colors have the proper format, 51 * that is one of #RGB, #ARGB, #RRGGBB or #AARRGGBB. 52 */ 53 @Override 54 protected void onAddValidators(final Text text) { 55 ModifyListener listener = new ModifyListener() { 56 @Override 57 public void modifyText(ModifyEvent e) { 58 String color = text.getText(); 59 if (RGBA_REGEXP.matcher(color).matches()) { 60 getManagedForm().getMessageManager().removeMessage(text, text); 61 } else { 62 getManagedForm().getMessageManager().addMessage(text, 63 "Accepted color formats are one of #RGB, #ARGB, #RRGGBB or #AARRGGBB.", 64 null /* data */, IMessageProvider.ERROR, text); 65 } 66 } 67 }; 68 69 text.addModifyListener(listener); 70 71 // Make sure the validator removes its message(s) when the widget is disposed 72 text.addDisposeListener(new DisposeListener() { 73 @Override 74 public void widgetDisposed(DisposeEvent e) { 75 getManagedForm().getMessageManager().removeMessage(text, text); 76 } 77 }); 78 79 // Finally call the validator once to make sure the initial value is processed 80 listener.modifyText(null); 81 } 82 } 83