001    /**
002     * jline - Java console input library
003     * Copyright (c) 2002,2003 Marc Prud'hommeaux mwp1@cornell.edu
004     *
005     * This library is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public
007     * License as published by the Free Software Foundation; either
008     * version 2.1 of the License, or (at your option) any later version.
009     *
010     * This library is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this library; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018     */
019    package jline;
020    
021    import java.io.*;
022    import java.util.*;
023    
024    /** 
025     *      <p>
026     *      A completor that contains multiple embedded completors. This differs
027     *      from the {@link ArgumentCompletor}, in that the nested completors
028     *      are dispatched individually, rather than delimited by arguments.
029     *      </p>
030     *
031     *  @author  <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
032     */
033    public class MultiCompletor
034            implements Completor
035    {
036            Completor [] completors = new Completor [0];
037    
038    
039            /** 
040             *  Construct a MultiCompletor with no embedded completors.
041             */
042            public MultiCompletor ()
043            {
044                    this (new Completor [0]);
045            }
046    
047    
048            /** 
049             *  Construct a MultiCompletor with the specified list of
050             *  {@link Completor} instances.
051             */
052            public MultiCompletor (List completors)
053            {
054                    this ((Completor [])completors.toArray (
055                            new Completor [completors.size ()]));
056            }
057    
058    
059            /** 
060             *  Construct a MultiCompletor with the specified 
061             *  {@link Completor} instances.
062             */
063            public MultiCompletor (Completor [] completors)
064            {
065                    this.completors = completors;
066            }
067    
068    
069            public int complete (String buffer, int pos, List cand)
070            {
071                    int [] positions = new int [completors.length];
072                    List [] copies = new List [completors.length];
073                    for (int i = 0; i < completors.length; i++)
074                    {
075                            // clone and save the candidate list
076                            copies [i] = new LinkedList (cand);
077                            positions [i] = completors [i].complete (buffer, pos, copies [i]);
078                    }
079    
080                    int maxposition = -1;
081                    for (int i = 0; i < positions.length; i++)
082                            maxposition = Math.max (maxposition, positions [i]);
083    
084                    // now we have the max cursor value: build up all the
085                    // candidate lists that have the same cursor value
086                    for (int i = 0; i < copies.length; i++)
087                    {
088                            if (positions [i] == maxposition)
089                                    cand.addAll (copies [i]);
090                    }
091    
092                    return maxposition;
093            }
094    
095    
096            public void setCompletors (Completor [] completors)
097            {
098                    this.completors = completors;
099            }
100    
101    
102            public Completor [] getCompletors ()
103            {
104                    return this.completors;
105            }
106    }