01: /*
02: * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved.
03: *
04: * This software is distributable under the BSD license. See the terms of the
05: * BSD license in the documentation provided with this software.
06: */
07: package jline;
08:
09: import java.util.*;
10:
11: /**
12: * <p>
13: * A completor that contains multiple embedded completors. This differs
14: * from the {@link ArgumentCompletor}, in that the nested completors
15: * are dispatched individually, rather than delimited by arguments.
16: * </p>
17: *
18: * @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
19: */
20: public class MultiCompletor implements Completor {
21: Completor[] completors = new Completor[0];
22:
23: /**
24: * Construct a MultiCompletor with no embedded completors.
25: */
26: public MultiCompletor() {
27: this (new Completor[0]);
28: }
29:
30: /**
31: * Construct a MultiCompletor with the specified list of
32: * {@link Completor} instances.
33: */
34: public MultiCompletor(final List completors) {
35: this ((Completor[]) completors.toArray(new Completor[completors
36: .size()]));
37: }
38:
39: /**
40: * Construct a MultiCompletor with the specified
41: * {@link Completor} instances.
42: */
43: public MultiCompletor(final Completor[] completors) {
44: this .completors = completors;
45: }
46:
47: public int complete(final String buffer, final int pos,
48: final List cand) {
49: int[] positions = new int[completors.length];
50: List[] copies = new List[completors.length];
51:
52: for (int i = 0; i < completors.length; i++) {
53: // clone and save the candidate list
54: copies[i] = new LinkedList(cand);
55: positions[i] = completors[i].complete(buffer, pos,
56: copies[i]);
57: }
58:
59: int maxposition = -1;
60:
61: for (int i = 0; i < positions.length; i++) {
62: maxposition = Math.max(maxposition, positions[i]);
63: }
64:
65: // now we have the max cursor value: build up all the
66: // candidate lists that have the same cursor value
67: for (int i = 0; i < copies.length; i++) {
68: if (positions[i] == maxposition) {
69: cand.addAll(copies[i]);
70: }
71: }
72:
73: return maxposition;
74: }
75:
76: public void setCompletors(final Completor[] completors) {
77: this .completors = completors;
78: }
79:
80: public Completor[] getCompletors() {
81: return this.completors;
82: }
83: }
|