Skip to main content
summaryrefslogtreecommitdiffstats
blob: 937d38d13e69d877d3ea9916675353561d3bd69e (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.command.internal.env.core.selection;

import java.util.Vector;

/**
 * This is a Dynamic version of the SelectionListChoices class.
 */
public class DynamicList 
{
  private Vector stringList_ = new Vector();
  private Vector dynamicListVectorList_ = new Vector();
  
  /**
   * Adds an entry into the dynamic list.
   * 
   * @param values the string values that lead to the target.
   * @param target the target.
   */
  public void add( String[] values, Object target )
  {
    DynamicList currentList = this;
    
    for( int index = 0; index < values.length; index++ )
    {
      String value      = values[index];
      int    length     = currentList.stringList_.size();
      int    foundIndex = -1;
      
      // Find the string in the current list.
      for( int searchIndex = 0; searchIndex < length; searchIndex++ )
      {
        String string = (String)currentList.stringList_.elementAt( searchIndex ); 
        
        if( string.equals( value ) ) 
        {
          foundIndex = searchIndex;
          break;
        }
      }
      
      if( foundIndex == -1 )
      {
        // We have a new string so add it to the list.
        currentList.stringList_.add( value );
        currentList.dynamicListVectorList_.add( new DynamicList() );
        foundIndex = length;
      }
      
      currentList = (DynamicList)currentList.dynamicListVectorList_.elementAt( foundIndex );       
    }
    
    currentList.dynamicListVectorList_.add( target );
  }
  
  /**
   * 
   * @return returns a SelectionListChoices object from this DynamicList
   * object.
   */
  public SelectionListChoices toListChoices()
  {
    SelectionList list   = new SelectionList( (String[])stringList_.toArray( new String[0] ), 0);
    int           length = dynamicListVectorList_.size();
    Vector        vector = new Vector(); 
    
    for( int index = 0; index < length; index++ )
    {
      Object obj = dynamicListVectorList_.elementAt(index);
      
      if( obj instanceof DynamicList )
      {
        vector.add( ((DynamicList)obj).toListChoices() );
      }
      else
      {
        vector.add( obj );
      }
    }
    
    return new SelectionListChoices( list, vector );
  }
}

Back to the top