KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > moduleloader > search > selection > InteractiveSelectionPolicy


1 /*
2  * ModuleLoader - A generic, policy-driven class loader.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.moduleloader.search.selection;
37
38 import java.io.BufferedReader JavaDoc;
39 import java.io.InputStreamReader JavaDoc;
40
41 import org.ungoverned.moduleloader.Module;
42 import org.ungoverned.moduleloader.search.SelectionPolicy;
43 import org.ungoverned.moduleloader.search.CompatibilityPolicy;
44
45 /**
46  * This class implements an interactive selection policy for the
47  * <tt>ImportSearchPolicy</tt>. This policy simply uses standard
48  * output to present the list of candidate modules and uses standard
49  * input to allow the user to select a specific module from the
50  * candidates. This selection policy is generally only useful for
51  * debugging purposes.
52  * @see org.ungoverned.moduleloader.search.SelectionPolicy
53  * @see org.ungoverned.moduleloader.search.ImportSearchPolicy
54 **/

55 public class InteractiveSelectionPolicy implements SelectionPolicy
56 {
57     /**
58      * Returns a single package from an array of packages.
59      * @param sources array of packages from which to choose.
60      * @return the selected package or <tt>null</tt> if no package
61      * can be selected.
62     **/

63     public Module select(Module module, Object JavaDoc target,
64         Object JavaDoc version, Module[] candidates, CompatibilityPolicy compatPolicy)
65     {
66         try {
67             if (candidates.length == 1)
68             {
69                 return candidates[0];
70             }
71             // Now start an interactive prompt.
72
BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
73             do
74             {
75                 System.out.println("\nImporting '" + target
76                     + "(" + version + ")" + "' for '" + module + "'.");
77                 System.out.println("");
78                 for (int i = 0; i < candidates.length; i++)
79                 {
80                     System.out.println((i + 1) + ". " + candidates[i]);
81                 }
82                 System.out.print("Select: ");
83                 String JavaDoc s = br.readLine();
84
85                 int choice = -1;
86                 try {
87                     choice = Integer.parseInt(s);
88                 } catch (Exception JavaDoc ex) {
89                 }
90
91                 if (choice == 0)
92                 {
93                     break;
94                 }
95                 else if ((choice > 0) && (choice <= candidates.length))
96                 {
97                     return candidates[choice - 1];
98                 }
99             }
100             while (true);
101         } catch (Exception JavaDoc ex) {
102         }
103
104         return null;
105     }
106 }
Popular Tags