summaryrefslogtreecommitdiffstats
blob: 3a4e44cec38615a30320ca2c3820d3e2b60b6efa (plain)
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
/*******************************************************************************
 * Copyright (c) 2011 SunGard CSA LLC 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:
 *    SunGard CSA LLC - initial API and implementation and/or initial documentation
 *******************************************************************************/
package org.eclipse.stardust.modeling.core.properties;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.preference.IPreferenceNode;
import org.eclipse.jface.preference.PreferenceManager;
import org.eclipse.stardust.model.xpdl.carnot.IModelElement;
import org.eclipse.stardust.modeling.core.editors.ui.CarnotPreferenceNode;


/**
 * @author fherinean
 * @version $Revision$
 */
public class ModelElementsOutlineSynchronizer
      extends ModelElementNotificationAdapter
{
   private OutlineProvider provider;
   private boolean isInit = false;

   public ModelElementsOutlineSynchronizer(OutlineProvider provider)
   {
      super(provider.getElementListFeature(), new int[0], provider.getFilter(), false);
      this.provider = provider;
   }

   public void init(EObject target)
   {
      if(!isInit)
      {
         dispose();
         super.init(target);
         provider.removeChildrenNodes(provider.getParentNodeId());
         if (target != null) {
            List elements = getChildren(target);
            for (int i = 0; i < elements.size(); i++)
            {
                addNode((EObject) elements.get(i), i); 
            }         
         }
         isInit = true;
      }
      else
      {
         super.init(target);         
      }
      provider.updateVisuals();
   }

   public void elementChanged(EObject element)
   {
      // for the moment nothing to do since label/icon changes are performed somwhere else
      // todo: implement it here
   }

   public void elementAdded(EObject element)
   {
      addNode(element, getNewIndex());
      provider.updateVisuals();
   }

   private void addNode(final EObject element, int index)
   {
	  IModelElement modelElement = null;
	  if (element instanceof IModelElement)
	  {
		 modelElement = (IModelElement) element;
	  }
	  else
	  {
	     ClassLoader classLoader = IModelElement.class.getClassLoader();
		 Class<?>[] interfaces = element.getClass().getInterfaces();
		 Class<?>[] classes = new Class[interfaces.length + 1];
		 System.arraycopy(interfaces, 0, classes, 0, interfaces.length);
		 classes[interfaces.length] = IModelElement.class;
		 InvocationHandler invocationHandler = new InvocationHandler()
		 {
			public Object invoke(Object proxy, Method mtd, Object[] args)
					throws Throwable
		    {
				if (IModelElement.class.equals(mtd.getDeclaringClass()))
				{
					return null;
				}
				return mtd.invoke(element, args);
			}
	     };
		 modelElement = (IModelElement) Proxy.newProxyInstance(classLoader, classes, invocationHandler);
	  }
      provider.addNodesFor(provider.getParentNodeId(), modelElement,
            new ModelElementAdaptable(
               new Class[] {IModelElement.class},
               new Object[] {modelElement},
               provider.getAdaptable()
            ), index);
   }

   public List<IPreferenceNode> getNodes()
   {
      List<IPreferenceNode> nodesList = new ArrayList<IPreferenceNode>();
      
      PreferenceManager pm = provider.getPreferenceManager();
      CarnotPreferenceNode parent = (CarnotPreferenceNode) pm.find(provider.getParentNodeId());
      if(parent == null)
      {
         return nodesList;         
      }
      
      IPreferenceNode[] nodes = parent.getSubNodes();
      for (int i = 0; i < nodes.length; i++)
      {
         nodesList.add(nodes[i]);
      }
      return nodesList;
   }   
   
   private int getNewIndex()
   {
      int index = -1;
      PreferenceManager pm = provider.getPreferenceManager();
      CarnotPreferenceNode parent = (CarnotPreferenceNode) pm.find(provider.getParentNodeId());
      IPreferenceNode[] nodes = parent.getSubNodes();
      for (int i = 0; i < nodes.length; i++)
      {
         index = Math.max(index, ((CarnotPreferenceNode) nodes[i]).getSortOrder());
      }
      return index + 1;
   }

   public void elementMoved(final EObject element)
   {
	  IModelElement modelElement = null;
	  if (element instanceof IModelElement)
	  {
		 modelElement = (IModelElement) element;
	  } else
	  {
		ClassLoader classLoader = IModelElement.class.getClassLoader();
		Class<?>[] interfaces = element.getClass().getInterfaces();
		Class<?>[] classes = new Class[interfaces.length + 1];
		System.arraycopy(interfaces, 0, classes, 0, interfaces.length);
		classes[interfaces.length] = IModelElement.class;
		InvocationHandler invocationHandler = new InvocationHandler()
		{
			public Object invoke(Object proxy, Method mtd, Object[] args)
					throws Throwable
			{
			 if (IModelElement.class.equals(mtd.getDeclaringClass()))
			 {
				return null;
			 }
			 return mtd.invoke(element, args);
		}
	   };
	 modelElement = (IModelElement) Proxy.newProxyInstance(classLoader, classes, invocationHandler);
	}
 
	  PreferenceManager pm = provider.getPreferenceManager();
      String prefix = provider.getParentNodeId();
      List elements = getChildren(element.eContainer());
      CarnotPreferenceNode parentNode = (CarnotPreferenceNode) pm.find(prefix);
      for (int i = 0; i < elements.size(); i++)
      {
         EObject me =  (EObject)elements.get(i);
         IPreferenceNode[] subnodes = parentNode.getSubNodes();
         for (int j = 0; j < subnodes.length; j++)
         {
            if (subnodes[j] instanceof CarnotPreferenceNode)
            {
               CarnotPreferenceNode node = (CarnotPreferenceNode) subnodes[j];
               if (me.equals(node.getAdaptable().getAdapter(IModelElement.class)))
               {
                  node.setSortOrder(i);
               }
            }
         }
      }
      provider.updateVisuals();
   }

   public void elementRemoved(final EObject element)
   {
	   
		  IModelElement modelElement = null;
		  if (element instanceof IModelElement)
		  {
			 modelElement = (IModelElement) element;
		  } else
		  {
			ClassLoader classLoader = IModelElement.class.getClassLoader();
			Class<?>[] interfaces = element.getClass().getInterfaces();
			Class<?>[] classes = new Class[interfaces.length + 1];
			System.arraycopy(interfaces, 0, classes, 0, interfaces.length);
			classes[interfaces.length] = IModelElement.class;
			InvocationHandler invocationHandler = new InvocationHandler()
			{
				public Object invoke(Object proxy, Method mtd, Object[] args)
						throws Throwable
				{
				 if (IModelElement.class.equals(mtd.getDeclaringClass()))
				 {
					return null;
				 }
				 return mtd.invoke(element, args);
			}
		   };
		 modelElement = (IModelElement) Proxy.newProxyInstance(classLoader, classes, invocationHandler);
		} 
	   
	   
      //String id = provider.getId((IModelElement) element);
	  String id = provider.getId(modelElement);	  
      provider.removeNode(provider.getParentNodeId() + "." + id); //$NON-NLS-1$
      provider.updateVisuals();
   }
}