Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/vinculum/processeditor/model/ProcessFlowElement.java


1   /* * ** **  BEGIN LICENSE BLOCK * ** **
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version 
5    * 1.1 (the "License"); you may not use this file except in compliance with 
6    * the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is Vinculum Open Source.
15   *
16   * The Initial Developer of the Original Code is
17   * Gerard Toonstra.
18   * Portions created by the Initial Developer are Copyright (C) 2003
19   * the Initial Developer. All Rights Reserved.
20   *
21   * Contributor(s):
22   *
23   * Alternatively, the contents of this file may be used under the terms of
24   * either the GNU General Public License Version 2 or later (the "GPL"), or
25   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26   * in which case the provisions of the GPL or the LGPL are applicable instead
27   * of those above. If you wish to allow use of your version of this file only
28   * under the terms of either the GPL or the LGPL, and not to allow others to
29   * use your version of this file under the terms of the MPL, indicate your
30   * decision by deleting the provisions above and replace them with the notice
31   * and other provisions required by the GPL or the LGPL. If you do not delete
32   * the provisions above, a recipient may use your version of this file under
33   * the terms of any one of the MPL, the GPL or the LGPL.
34   *
35   * ** ** * END LICENSE BLOCK * ** **
36   */
37  
38  /***************************************************************************
39                            $RCSfile: ProcessFlowElement.java,v $  -  description
40                               -------------------
41      begin                : $Date: 2003/07/08 08:02:25 $
42      copyright            : Vinculum (C) 2002
43      author               : $Author: chiraz $
44   ***************************************************************************/
45  
46  /* $Log: ProcessFlowElement.java,v $
47  /* Revision 1.1.1.1  2003/07/08 08:02:25  chiraz
48  /* egg
49  /* */
50  
51  package com.vinculum.processeditor.model;
52  
53  import java.io.IOException;
54  import java.io.ObjectInputStream;
55  import java.util.Vector;
56  
57  import com.vinculum.processeditor.ProcessMessages;
58  
59  import org.eclipse.draw2d.geometry.Dimension;
60  import org.eclipse.jdt.core.IMethod;
61  import org.eclipse.jdt.core.IType;
62  import org.eclipse.swt.graphics.Image;
63  import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
64  import org.eclipse.ui.views.properties.IPropertyDescriptor;
65  import org.eclipse.ui.views.properties.PropertyDescriptor;
66  import org.eclipse.ui.views.properties.TextPropertyDescriptor;
67  import com.vinculum.processeditor.model.ProcessDecisionElement;
68  
69  /**
70   * @author chilan
71   *
72   */
73  public class ProcessFlowElement extends ProcessSubPart
74  {
75    static final long serialVersionUID = 1;
76    private static int count;
77    private String id;
78      
79    public static String ID_CLASS = "startclass"; //$NON-NLS-1$  
80    public static String ID_METHOD = "startmethod"; //$NON-NLS-1$
81    public static String ID_IN_ARGS = "sinargs"; //$NON-NLS-1$
82    public static String ID_OUT_ARG = "soutarg"; //$NON-NLS-1$
83    public static String ID_EL_NAME = "startelname"; //$NON-NLS-1$
84    public static String ID_MAP_REF = "startmapref"; //$NON-NLS-1$
85    public static String ID_SYNCHRONISE = "synchronise"; //$NON-NLS-1$
86    
87    private static final String CLASS_NAME_CONTEXT = ProcessMessages.HelpContext_ProcessFlowStart_ClassName; //$NON-NLS-1$
88    private static final String METHOD_NAME_CONTEXT = ProcessMessages.HelpContext_ProcessFlowStart_MethodName; //$NON-NLS-1$  
89    private static final String ARGS_CONTEXT = ProcessMessages.HelpContext_ProcessFlowStart_Args; //$NON-NLS-1$
90    private static final String EL_NAME_CONTEXT = ProcessMessages.HelpContext_ProcessFlowStart_ElName; //$NON-NLS-1$
91    private static final String MAP_REF_CONTEXT = ProcessMessages.HelpContext_ProcessFlowStart_MapRef; //$NON-NLS-1$
92    private static final String SYNC_CONTEXT = ProcessMessages.HelpContext_ProcessFlowStart_Sync; //$NON-NLS-1$
93    
94    private static final String[] choices = {"false", "true" };
95      
96    protected transient Vector descriptors = null;
97    private String className = new String();
98    private Integer methodID = new Integer(0);
99    private InArguments inArguments = new InArguments();
100   private OutArgument outArgument = new OutArgument();
101   private String mapRef;
102   private String elementName;
103   private ProcessJoinElement inElement = null;
104   private ProcessSplitElement outElement = null;
105   private Integer synchronisation = null;  
106   
107   private transient IType selectedClass = null;
108   private transient IMethod selectedMethod = null;
109   private transient IMethod[] methods = null;
110   private transient String[] protoTypes = null;
111   private transient String   methodName = null;
112 
113   public ProcessFlowElement()
114   {
115     super();
116     setID(getNewID());    
117   }
118 
119   protected String getNewID()
120   {
121     return Integer.toString(++count);
122   }
123   
124   public String getID() 
125   {
126     return id; 
127   }
128   
129   public String getTargetID()
130   {
131     if ( inElement != null )
132     {
133       return inElement.getId();
134     }
135     return getID();
136   }
137 
138   public String getSourceID()
139   {
140     if ( outElement != null )
141     {
142       return outElement.getId();
143     }
144     return getID();
145   }
146   
147   public void setID(String s) 
148   {
149     id = s;
150   }
151 
152   protected void refreshDescriptors()
153   {
154     PropertyDescriptor propertyDescriptor = null;
155     
156     if (selectedClass == null )
157     {
158       if ((className != null) && (! className.equals("")) )
159       {
160         selectedClass = findType(className);
161       }      
162     }
163     
164     if (descriptors == null)
165     {
166       descriptors = new Vector();
167     }
168     
169     if ( protoTypes == null )
170     {
171       protoTypes = new String[1];
172     }
173     
174     descriptors.clear();
175     propertyDescriptor = new TextPropertyDescriptor(ID_CLASS, ProcessMessages.PropertyDescriptor_ProcessFlowStart_ClassName);
176     propertyDescriptor.setCategory(ProcessMessages.PropertyDescriptor_ProcessFlowStart_General);
177     propertyDescriptor.setHelpContextIds(CLASS_NAME_CONTEXT);
178     descriptors.add( propertyDescriptor );
179 
180     propertyDescriptor = new TextPropertyDescriptor(ID_EL_NAME, ProcessMessages.PropertyDescriptor_ProcessFlowStart_ElementName);
181     propertyDescriptor.setCategory(ProcessMessages.PropertyDescriptor_ProcessFlowStart_General);
182     propertyDescriptor.setHelpContextIds(EL_NAME_CONTEXT);
183     descriptors.add( propertyDescriptor );
184     
185     propertyDescriptor = new TextPropertyDescriptor(ID_MAP_REF, ProcessMessages.PropertyDescriptor_ProcessFlowStart_MapRef);
186     propertyDescriptor.setCategory(ProcessMessages.PropertyDescriptor_ProcessFlowStart_General);
187     propertyDescriptor.setHelpContextIds(MAP_REF_CONTEXT);
188     descriptors.add( propertyDescriptor );
189     
190     protoTypes[0] = new String("no methods available");
191     try
192     {
193       if ( selectedClass != null )
194       {      
195         methods = selectedClass.getMethods();
196         protoTypes = parseMethodPrototypes(methods);
197       }
198     }
199     catch (Exception e)
200     {
201       protoTypes[0] = new String("no methods available");
202     }
203     propertyDescriptor = new ComboBoxPropertyDescriptor(
204       ID_METHOD, 
205       ProcessMessages.PropertyDescriptor_ProcessFlowStart_MethodName,
206       protoTypes
207       );
208     propertyDescriptor.setCategory(ProcessMessages.PropertyDescriptor_ProcessFlowStart_General);
209     propertyDescriptor.setHelpContextIds(METHOD_NAME_CONTEXT);
210     propertyDescriptor.setLabelProvider(new MethodNameLabelProvider());
211     descriptors.add( propertyDescriptor );
212 
213     propertyDescriptor = new PropertyDescriptor(ID_IN_ARGS, ProcessMessages.PropertyDescriptor_ProcessFlowStart_InArgs);
214     propertyDescriptor.setCategory(ProcessMessages.PropertyDescriptor_ProcessFlowStart_General);
215     propertyDescriptor.setHelpContextIds(ARGS_CONTEXT);
216     propertyDescriptor.setValidator(new InArgumentNameValidator());    
217     descriptors.add(propertyDescriptor);
218 
219     propertyDescriptor = new PropertyDescriptor(ID_OUT_ARG, ProcessMessages.PropertyDescriptor_ProcessFlowStart_OutArg);
220     propertyDescriptor.setCategory(ProcessMessages.PropertyDescriptor_ProcessFlowStart_General);
221     propertyDescriptor.setHelpContextIds(ARGS_CONTEXT);
222     propertyDescriptor.setValidator(new OutArgumentNameValidator());    
223     descriptors.add(propertyDescriptor);
224     
225     propertyDescriptor = new ComboBoxPropertyDescriptor(
226       ID_SYNCHRONISE, 
227       ProcessMessages.PropertyDescriptor_ProcessFlowStart_Sync,
228       choices
229       );
230     propertyDescriptor.setCategory(ProcessMessages.PropertyDescriptor_ProcessFlowStart_General);
231     propertyDescriptor.setHelpContextIds(SYNC_CONTEXT);
232     propertyDescriptor.setLabelProvider(new SyncLabelProvider());
233     descriptors.add( propertyDescriptor );
234     
235     Vector parentDescriptors = ProcessSubPart.getDescriptorVector();
236     for (int i = 0; i < parentDescriptors.size(); i++) 
237     {
238       descriptors.addElement(parentDescriptors.elementAt(i));
239     }
240   }  
241   
242   public Vector getDescriptorsVector()
243   {
244     if (descriptors == null )
245     {
246       refreshDescriptors();
247     }
248     return descriptors;
249   }
250   
251   public Image getIconImage()
252   {
253     return null;
254   }
255   
256   public Dimension getSize()
257   {
258     return new Dimension(-1, -1);
259   }
260 
261   public void update()
262   {
263     if ((inputs.size() > 1 ) && (inElement == null ))
264     {
265       inElement = new ProcessJoinElement(getNewID());
266     }
267     if (( outputs.size() > 1 ) && ( outElement == null ))
268     {
269       outElement = new ProcessSplitElement(getNewID());
270     }
271     if (inputs.size() <= 1 )
272     {
273       inElement = null;
274     }
275     if ( outputs.size() <= 1 )
276     {
277       outElement = null;
278     }    
279   }
280   
281   public ProcessDecisionElement getSplitElement()
282   {
283     return outElement;
284   }
285   
286   public ProcessDecisionElement getJoinElement()
287   {
288     return inElement;
289   }
290   
291   public boolean getResult() 
292   {
293     return false;
294   }
295 
296   public String toString()
297   {
298     return ProcessMessages.ProcessFlowStart_LabelText + " #" + getID(); //$NON-NLS-1$
299   }
300   
301   public Object getPropertyValue(Object propName) 
302   {
303     if ( propName.equals(ID_CLASS) )
304     {
305       return getClassName();
306     }
307     if ( propName.equals( ID_METHOD ))
308     {
309       return getMethodID();
310     }
311     if ( propName.equals( ID_IN_ARGS))
312     {
313       return getInArguments();
314     }
315     if ( propName.equals( ID_OUT_ARG))
316     {
317       return getOutArgument();
318     }
319     if ( propName.equals( ID_EL_NAME ))
320     {
321       return getElementName();
322     }
323     if ( propName.equals( ID_MAP_REF ))
324     {
325       return getMapRef();
326     }
327     if ( propName.equals( ID_SYNCHRONISE) )
328     {
329       return getSynchronisation();
330     }
331     return super.getPropertyValue(propName);
332   }  
333   
334   public boolean isPropertySet(Object property) 
335   {
336     if (property.equals(ID_CLASS))
337     {
338       return getClassName() != "";
339     }
340     if (property.equals(ID_METHOD))
341     {
342       return getMethodID().intValue() != Integer.MAX_VALUE;
343     }
344     if (property.equals(ID_IN_ARGS))
345     {
346       return getInArguments() != new InArguments();
347     }
348     if ( property.equals( ID_EL_NAME ))
349     {
350       return ! getElementName().equals("");
351     }
352     if ( property.equals( ID_MAP_REF ))
353     {
354       return ! getMapRef().equals("");
355     }
356     if ( property.equals( ID_SYNCHRONISE ))
357     {
358       return false; 
359     }
360     return false;
361   }
362   
363   public void resetPropertyValue(Object property)
364   {
365     if (property.equals(ID_CLASS)) 
366     {
367       setClassName("");
368       return;
369     }
370     if (property.equals(ID_METHOD)) 
371     {
372       setMethodID(Integer.MAX_VALUE);
373       return;
374     }
375     if (property.equals(ID_IN_ARGS))
376     {
377       setInArguments(new InArguments());
378       return;  
379     }
380     if (property.equals(ID_OUT_ARG))
381     {
382       setOutArgument(new OutArgument());
383       return;  
384     }  
385     if ( property.equals( ID_EL_NAME ))
386     {
387       setElementName("");
388       return;      
389     }
390     if ( property.equals( ID_MAP_REF ))
391     {
392       setMapRef("");
393       return;      
394     }
395     if ( property.equals( ID_SYNCHRONISE ))
396     {
397       setSynchronisation( new Integer( 0 ) );
398       return;
399     }
400   }
401   
402   public void setPropertyValue(Object id, Object value)
403   {
404     if ( id.equals(ID_SIZE) )
405     { 
406       super.setPropertyValue(id,new Dimension(getSize()));
407       return;
408     }
409     else if ( id.equals(ID_CLASS))
410     {
411       setClassName((String)value);
412       return;
413     }
414     else if ( id.equals(ID_METHOD))
415     {
416       setMethodID(((Integer)value).intValue());
417       return;
418     }
419     else if ( id.equals(ID_IN_ARGS))
420     {
421       // setInArguments((InArguments)value);
422       return;      
423     }
424     else if ( id.equals(ID_OUT_ARG))
425     {
426       // setOutArgument((OutArgument)value);
427       return;      
428     }
429     else if ( id.equals( ID_EL_NAME ))
430     {
431       setElementName((String)value);
432       return;      
433     }
434     else if ( id.equals( ID_MAP_REF ))
435     {
436       setMapRef((String)value);
437       return;      
438     }
439     else if ( id.equals( ID_SYNCHRONISE ))
440     {
441       setSynchronisation( (Integer)value );
442       return;
443     }
444     super.setPropertyValue(id,value);
445   }
446   
447   private String getClassName()
448   {
449     if ( className != null )
450     {
451       return new String(className);
452     }
453     else
454     {
455       return new String("");  
456     }
457   }
458   
459   private void setClassName(String value)
460   {
461     className = new String(value);
462     selectedClass = findType(value);    
463     refreshDescriptors();
464   }
465   
466   private Integer getMethodID()
467   {
468     if ( methodID == null )
469     {
470       methodID = new Integer(0);
471     } 
472 
473     return new Integer(methodID.intValue());
474   }
475   
476   private void setMethodID(int value)
477   {
478     methodID = new Integer(value);
479     refreshDescriptors();
480     if (methods != null)
481     {
482       selectedMethod = methods[ methodID.intValue() ];
483       setInArguments( new InArguments( methods, value, inArguments ) );
484       setOutArgument( new OutArgument( methods, value, outArgument ) );
485     }
486   }
487   
488   private void setInArguments( InArguments value )
489   {
490     inArguments = value;
491   }
492 
493   private void setOutArgument( OutArgument value )
494   {
495     outArgument = value;
496   }
497   
498   private OutArgument getOutArgument()
499   {
500     if ( outArgument == null )
501     {
502       return new OutArgument();
503     }
504     return outArgument;    
505   }
506   
507   private InArguments getInArguments()
508   {
509     if ( inArguments == null )
510     {
511       return new InArguments();
512     }
513     return inArguments;
514   }
515   
516   private String getMapRef()
517   {
518     if ( mapRef == null )
519     {
520       mapRef = new String("");
521     }
522     return mapRef;
523   }
524   
525   private void setMapRef( String newValue )
526   {
527     mapRef = new String(newValue);
528   }
529   
530   private String getElementName()
531   {
532     if ( elementName == null )
533     {
534       elementName = new String("");
535     }
536     return elementName;
537   }  
538   
539   private void setElementName( String newValue )
540   {
541     elementName = new String(newValue);
542   }
543   
544   private Integer getSynchronisation()
545   {
546     if ( synchronisation == null )
547     {
548       synchronisation = new Integer(0);
549     }
550     return synchronisation;
551   }
552   
553   private void setSynchronisation( Integer newValue )
554   {
555     synchronisation = new Integer(newValue.intValue());
556   }
557   
558   private class MethodNameLabelProvider 
559     extends org.eclipse.jface.viewers.LabelProvider
560   {
561     public MethodNameLabelProvider()
562     {
563       super();
564     }
565     public String getText(Object element)
566     {
567       if (element instanceof Integer)
568       {
569         Integer integer = (Integer)element;
570         
571         if (   methods == null || 
572             methods.length == 0 || 
573             methodID == null || 
574             protoTypes.length == 0 )
575         {
576           return new String("");
577         }
578         
579         return protoTypes[integer.intValue()];
580       }
581       return super.getText(element);
582     }
583   }
584   
585   private class SyncLabelProvider 
586     extends org.eclipse.jface.viewers.LabelProvider
587   {
588     public SyncLabelProvider()
589     {
590       super();
591     }
592     public String getText(Object element)
593     {
594       if (element instanceof Integer)
595       {
596         Integer choice = (Integer)element;
597         
598         if ( choice.intValue() == 0 )
599         {
600           return new String("false");
601         }
602         else
603         {
604           return new String("true");
605         }
606       }
607       return super.getText(element);
608     }
609   }  
610   
611   public IPropertyDescriptor[] getPropertyDescriptors() 
612   {
613     if ( descriptors == null )
614     {
615       refreshDescriptors();
616     }
617       
618     return (IPropertyDescriptor[])descriptors.toArray(new IPropertyDescriptor[descriptors.size()]);
619   }
620   
621   private void readObject(ObjectInputStream in)
622     throws IOException, ClassNotFoundException 
623   {
624     in.defaultReadObject();
625 
626     refreshDescriptors();
627 
628     if ( methodID != null )
629     {
630       setMethodID( methodID.intValue() );
631     }
632   }  
633   
634   public IMethod getSelectedMethod()
635   {
636     return selectedMethod;    
637   }
638 }