configtool/com.nokia.S60CT.CenRep.edit/src/s60cenrep/provider/KeyRangeItemProvider.java
changeset 0 30eb2d538f02
equal deleted inserted replaced
-1:000000000000 0:30eb2d538f02
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 package s60cenrep.provider;
       
    18 
       
    19 import interfaces.CellModifier;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 import java.util.Arrays;
       
    23 import java.util.Collection;
       
    24 import java.util.List;
       
    25 
       
    26 import org.eclipse.emf.common.command.Command;
       
    27 import org.eclipse.emf.common.notify.AdapterFactory;
       
    28 import org.eclipse.emf.common.notify.Notification;
       
    29 import org.eclipse.emf.ecore.EObject;
       
    30 import org.eclipse.emf.ecore.EStructuralFeature;
       
    31 import org.eclipse.emf.edit.command.SetCommand;
       
    32 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
       
    33 import org.eclipse.emf.edit.domain.EditingDomain;
       
    34 import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
       
    35 import org.eclipse.emf.edit.provider.ITableItemLabelProvider;
       
    36 import org.eclipse.emf.edit.provider.ViewerNotification;
       
    37 
       
    38 import cenrep.CenrepPackage;
       
    39 import cenrep.Key;
       
    40 import cenrep.KeyRange;
       
    41 import cenrep.Repository;
       
    42 
       
    43 public class KeyRangeItemProvider extends cenrep.provider.KeyRangeItemProvider
       
    44 		implements ITableItemLabelProvider, CellModifier {
       
    45 
       
    46 	public static final String PROP_NAME = "name";
       
    47 	public static final String PROP_REF = "ref";
       
    48 	public static final String PROP_FIRSTINT = "firstInt";
       
    49 	public static final String PROP_LASTINT = "lastInt";
       
    50 
       
    51 	//public static final EStructuralFeature FIRST_IDENT = CenrepPackage.eINSTANCE.getKeyRange_FirstInt();
       
    52 	//public static final EStructuralFeature LAST_IDENT = CenrepPackage.eINSTANCE.getKeyRange_LastInt();
       
    53 	
       
    54 	public static final EStructuralFeature NAME_FEATURE = CenrepPackage.eINSTANCE.getKeyRange_Name();
       
    55 	public static final EStructuralFeature REF_FEATURE = CenrepPackage.eINSTANCE.getRVG_Ref();
       
    56 	public static final EStructuralFeature FIRSTINT_FEATURE = CenrepPackage.eINSTANCE.getKeyRange_FirstInt();
       
    57 	public static final EStructuralFeature LASTINT_FEATURE = CenrepPackage.eINSTANCE.getKeyRange_LastInt();
       
    58 
       
    59 	private final EStructuralFeature[] REPOSITORY_CHILDREN = new EStructuralFeature[] { CenrepPackage.Literals.REPOSITORY__RANGE_KEY };
       
    60 	private final EStructuralFeature[] RANGE_CHILDREN = new EStructuralFeature[] { CenrepPackage.Literals.KEY_RANGE__KEYS };
       
    61 
       
    62 //	private static Key emptyKey = CenrepFactory.eINSTANCE.createKey();
       
    63 	
       
    64 	public KeyRangeItemProvider(AdapterFactory adapterFactory) {
       
    65 		super(adapterFactory);
       
    66 //		emptyKey.setName("<Add new key>");
       
    67 	}
       
    68 
       
    69 	@Override
       
    70 	public Collection<EStructuralFeature> getChildrenFeatures(Object object) {
       
    71 		if (object instanceof Repository)
       
    72 			return Arrays.asList(REPOSITORY_CHILDREN);
       
    73 		else if (object instanceof KeyRange)
       
    74 			return Arrays.asList(RANGE_CHILDREN);
       
    75 		return null;
       
    76 	}
       
    77 
       
    78 	@Override
       
    79 	public Collection<Key> getChildren(Object object) {
       
    80 		Collection<Key> children = (Collection<Key>) super.getChildren(object);
       
    81 //		if (object instanceof KeyRange) children.add(emptyKey);
       
    82 		return children;
       
    83 	}
       
    84 	
       
    85 	public Object getColumnImage(Object object, int columnIndex) {
       
    86 		if (columnIndex == 0)
       
    87 			return getImage(object);
       
    88 		return null;
       
    89 	}
       
    90 
       
    91 	public String getColumnText(Object object, int columnIndex) {
       
    92 		EObject eObject = (EObject) object;
       
    93 		switch (columnIndex) {
       
    94 		case 0:
       
    95 			return (String) eObject.eGet(NAME_FEATURE);
       
    96 		case 1:
       
    97 			return (String) eObject.eGet(REF_FEATURE);
       
    98 		case 2:
       
    99 			return null;
       
   100 		case 3:
       
   101 			return null;
       
   102 		case 4:
       
   103 			return (String) eObject.eGet(FIRSTINT_FEATURE);
       
   104 		case 5:
       
   105 			return (String) eObject.eGet(LASTINT_FEATURE);
       
   106 		}
       
   107 		return null;
       
   108 	}
       
   109 
       
   110 	public boolean canModify(Object element, String property) {
       
   111 		if (property.equals(PROP_NAME) || property.equals(PROP_REF) || property.equals(PROP_FIRSTINT) || property.equals(PROP_LASTINT))
       
   112 			return true;
       
   113 		else 
       
   114 			return false;
       
   115 	}
       
   116 
       
   117 	public Object getValue(Object element, String property) {
       
   118 		EObject eObject = (EObject) element;
       
   119 		String result = null;
       
   120 		if (property.equals(PROP_NAME))
       
   121 			result = (String) eObject.eGet(NAME_FEATURE);
       
   122 		else if (property.equals(PROP_REF))
       
   123 			result = (String) eObject.eGet(REF_FEATURE);
       
   124 		else if(property.equals(PROP_FIRSTINT))
       
   125 			result = (String) eObject.eGet(FIRSTINT_FEATURE);
       
   126 		else if(property.equals(PROP_LASTINT))
       
   127 			result = (String) eObject.eGet(LASTINT_FEATURE);
       
   128 		return result != null ? result : "";
       
   129 	}
       
   130 
       
   131 	public void modify(Object parent, Object element, String property,
       
   132 			Object value) {
       
   133 		EObject object = (EObject) element;
       
   134 		EditingDomain domain = AdapterFactoryEditingDomain
       
   135 				.getEditingDomainFor(object);
       
   136 		Command command = null;
       
   137 		if (property.equals(PROP_NAME)) {
       
   138 			String oldValue = (String) object.eGet(NAME_FEATURE);
       
   139 			String newValue = (String) value;
       
   140 			if (newValue != oldValue) 
       
   141 				command = SetCommand.create(domain, object, NAME_FEATURE, value);
       
   142 		} else if (property.equals(PROP_REF)) {
       
   143 			String oldRef = (String) object.eGet(REF_FEATURE);
       
   144 			String newRef = (String) value;
       
   145 			if (oldRef != newRef) {
       
   146 				command = SetCommand.create(domain, object, REF_FEATURE, newRef);
       
   147 			}
       
   148 		} else if (property.equals(PROP_FIRSTINT)) {
       
   149 			String oldFI = (String) object.eGet(FIRSTINT_FEATURE);
       
   150 			String newFI = (String) value;
       
   151 			if (newFI.startsWith("0x") || newFI.startsWith("0X"))
       
   152 				newFI = "0x"+newFI.substring(2).toUpperCase();
       
   153 			else
       
   154 				newFI = "0x"+newFI.toUpperCase();
       
   155 			if (oldFI != newFI) {
       
   156 				command = SetCommand.create(domain, object, FIRSTINT_FEATURE, newFI);
       
   157 			}
       
   158 		} else if (property.equals(PROP_LASTINT)) {
       
   159 			String oldLI = (String) object.eGet(LASTINT_FEATURE);
       
   160 			String newLI = (String) value;
       
   161 			if (newLI.startsWith("0x") || newLI.startsWith("0X"))
       
   162 				newLI = "0x"+newLI.substring(2).toUpperCase();
       
   163 			else
       
   164 				newLI = "0x"+newLI.toUpperCase();
       
   165 			if (oldLI != newLI) {
       
   166 				command = SetCommand.create(domain, object, LASTINT_FEATURE, newLI);
       
   167 			}
       
   168 		}
       
   169 		if (command != null)
       
   170 			domain.getCommandStack().execute(command);
       
   171 	}
       
   172 
       
   173 	@Override
       
   174 	public List getPropertyDescriptors(Object object) {
       
   175 		if (itemPropertyDescriptors == null) {
       
   176 			itemPropertyDescriptors = new ArrayList<IItemPropertyDescriptor>();
       
   177 			addBackupPropertyDescriptor(object);
       
   178 			addReadOnlyPropertyDescriptor(object);
       
   179 			addRefPropertyDescriptor(object);
       
   180 			addNamePropertyDescriptor(object);
       
   181 			addFirstIntPropertyDescriptor(object);
       
   182 			addLastIntPropertyDescriptor(object);
       
   183 			addIntPropertyDescriptor(object);
       
   184 			addIndexBitsPropertyDescriptor(object);
       
   185 			addCountIntPropertyDescriptor(object);
       
   186 			addFirstIndexPropertyDescriptor(object);
       
   187 		}
       
   188 		return itemPropertyDescriptors;
       
   189 	}
       
   190 
       
   191 	public void notifyChanged(Notification notification) {
       
   192 		switch (notification.getFeatureID(Repository.class)) {
       
   193 			case CenrepPackage.REPOSITORY__RANGE_KEY:
       
   194 			case CenrepPackage.KEY_RANGE__KEYS:
       
   195 				fireNotifyChanged(new ViewerNotification(notification, notification
       
   196 						.getNotifier(), true, false));
       
   197 				return;	
       
   198 			case CenrepPackage.KEY_RANGE__NAME:
       
   199 			case CenrepPackage.KEY_RANGE__REF:
       
   200 				fireNotifyChanged(new ViewerNotification(notification, notification
       
   201 						.getNotifier(), false, true));
       
   202 				return;	
       
   203 		}
       
   204 		super.notifyChanged(notification);
       
   205 	}
       
   206 }