KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > ac > roe > antigen > textcomponents > TextTargetChooser


1 /*
2  * Created on 09-Feb-2005
3  */

4 package uk.ac.roe.antigen.textcomponents;
5
6 import java.io.BufferedInputStream JavaDoc;
7 import java.io.DataInputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Hashtable JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Vector JavaDoc;
14 import java.util.logging.Logger JavaDoc;
15
16 import org.apache.tools.ant.Project;
17 import org.apache.tools.ant.Target;
18
19 import sun.rmi.runtime.Log;
20 import uk.ac.roe.antigen.builder.TargetChooser;
21 import uk.ac.roe.antigen.utils.Config;
22
23 /**
24  * A Text version of the TargetChoiceFrame
25  *
26  * @author jdt
27  */

28 public class TextTargetChooser implements TargetChooser {
29     /**
30      * Logger for this class
31      */

32     private static final Logger JavaDoc logger = Logger.getLogger(TextTargetChooser.class.getName());
33
34
35     /**
36      * Auto-generated main method to display this JDialog
37      */

38     public static void main(String JavaDoc[] args) {
39         Config.load("/config.properties");
40         Config.setProperty(ANTIGEN_TARGETS, "foo,bar,not,eek");
41         Config.setProperty(ANTIGEN_TARGET_ + "foo.selected", "true");
42         Config.setProperty(ANTIGEN_TARGET_ + "bar.selected", "false");
43         Config.setProperty(ANTIGEN_TARGET_ + "not.selected", "true");
44
45         Project project2 = new Project();
46         Target target = new Target();
47         target.setName("foo");
48         target.setDescription("Foos description");
49         project2.addTarget(target);
50
51         target = new Target();
52         target.setName("bar");
53         project2.addTarget(target);
54         target = new Target();
55         target.setName("eek");
56         project2.addTarget(target);
57
58         TargetChooser inst = new TextTargetChooser(project2);
59
60         List JavaDoc targets = inst.showAndWaitForResponse();
61         System.out.println("Selected tasks:");
62         Iterator JavaDoc it = targets.iterator();
63         while (it.hasNext()) {
64             String JavaDoc taskName = (String JavaDoc) it.next();
65             System.out.println(taskName);
66         }
67     }
68
69     private Project project;
70
71     public TextTargetChooser(Project project) {
72         this.project = project;
73         setUpTasks();
74     }
75
76     /**
77      * Parse the list of targets and set up the checkboxes accordingly
78      */

79     private void setUpTasks() {
80         String JavaDoc targetString = Config.getProperty(ANTIGEN_TARGETS);
81         logger.fine("Found targets: " + targetString);
82         if (targetString == null) {
83             return;
84         }
85         Hashtable JavaDoc projectTargets = project.getTargets();
86         String JavaDoc[] targets = targetString.split(",");
87         logger.fine("Split into " + targets.length);
88         for (int i = 0; i < targets.length; ++i) {
89             String JavaDoc targetName = targets[i];
90             Target target = (Target) projectTargets.get(targetName);
91             if (target != null) {
92                 String JavaDoc description = target.getDescription();
93                 String JavaDoc selectedOpt = Config.getProperty(ANTIGEN_TARGET_
94                         + targetName + ".selected");
95                 boolean selected = true;
96                 if ("false".equals(selectedOpt)) {
97                     selected = false;
98                 }
99                 taskDescs.add(new TaskDescription(targetName, description,
100                         selected));
101                 logger.fine("Target " + targetName + " found (" + description
102                         + "). Is selected initially? " + selected);
103             } else {
104                 logger.fine("Target " + targetName
105                         + " not found in project, skipping");
106             }
107         }
108
109     }
110
111     private List JavaDoc taskDescs = new ArrayList JavaDoc();
112
113     private class TaskDescription {
114
115         private String JavaDoc name;
116
117         private String JavaDoc description;
118
119         private boolean defaultSelected;
120
121         private String JavaDoc label;
122
123         public TaskDescription(String JavaDoc name, String JavaDoc description, boolean selected) {
124             this.name = name;
125             this.description = description;
126             this.defaultSelected = selected;
127             assert name != null;
128             label = description != null ? description : name;
129         }
130
131         public boolean isSelectedByDefault() {
132             return defaultSelected;
133         }
134
135         /**
136          * @return Returns the label.
137          */

138         public String JavaDoc getLabel() {
139             return label;
140         }
141
142         /**
143          * @return Returns the description.
144          */

145         public String JavaDoc getDescription() {
146             return description;
147         }
148
149         /**
150          * @return Returns the name.
151          */

152         public String JavaDoc getName() {
153             return name;
154         }
155     }
156
157     /**
158      * Display the dialog and return an array of the selected options
159      *
160      * @return
161      */

162     public Vector JavaDoc showAndWaitForResponse() {
163         showChoices();
164         System.out
165                 .println("---Enter choices as a comma-separated list, or press return to accept the [defaults]");
166
167         DataInputStream JavaDoc dataInputStream = new DataInputStream JavaDoc(
168                 new BufferedInputStream JavaDoc(System.in));
169         boolean unhappy = true;
170         Vector JavaDoc theChosen=null;
171         while (unhappy) {
172             theChosen = new Vector JavaDoc();
173             String JavaDoc input;
174             try {
175                 logger.fine("Waiting for input");
176                 input = dataInputStream.readLine().trim();
177             } catch (IOException JavaDoc e) {
178                 //ignore and retry
179
logger.warning(e.getMessage());
180                 break;
181             }
182             logger.fine("Input: |" + input + "|");
183             if ("".equals(input)) {
184                 logger.fine("return pressed - selecting defaults");
185                 Iterator JavaDoc it = taskDescs.iterator();
186                 while (it.hasNext()) {
187                     TaskDescription desc = (TaskDescription) it.next();
188                     if (desc.isSelectedByDefault()) theChosen.add(desc.getName());
189                 }
190                 unhappy = false;
191             } else {
192                 String JavaDoc[] choices = input.split(",");
193                 for (int i = 0; i < choices.length; ++i) {
194                     unhappy = false;
195                     try {
196                         int choice = Integer.parseInt(choices[i]);
197                         if (choice < 1 || choice > taskDescs.size()) {
198                             System.out.println("Choosing "+choice+" is invalid");
199                             unhappy=true;
200                             break;
201                         } else {
202                             TaskDescription taskDescription = (TaskDescription)taskDescs.get(choice-1);
203                             theChosen.add(taskDescription.getName());
204                         }
205                     } catch (NumberFormatException JavaDoc e1) {
206                         //e1.printStackTrace();
207
System.out
208                                 .println("Invalid entry - must be a comma-separated list of numbers");
209                         unhappy=true;
210                         break;
211                     }
212
213                 }
214
215                 
216             }
217         }
218         logger.fine("Choice made");
219
220         return theChosen;
221     }
222
223     /**
224      * Display the choices of tasks to the user
225      */

226     private void showChoices() {
227         System.out.println("Please select from the following tasks:");
228         for (int i = 0; i < taskDescs.size(); ++i) {
229             TaskDescription description = (TaskDescription) taskDescs.get(i);
230             String JavaDoc prompt = description.isSelectedByDefault() ? "["
231                     + Integer.toString(i + 1) + "]" : " "
232                     + Integer.toString(i + 1) + " ";
233             System.out.println(prompt + " - " + description.getLabel());
234         }
235     }
236
237 }
Popular Tags