KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > sendopts > Provider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.sendopts;
20
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25 import junit.framework.Assert;
26 import org.netbeans.spi.sendopts.OptionGroups;
27 import org.netbeans.spi.sendopts.Env;
28 import org.netbeans.spi.sendopts.Option;
29 import org.netbeans.spi.sendopts.OptionProcessor;
30 import org.openide.util.Lookup;
31 import org.openide.util.lookup.AbstractLookup;
32 import org.openide.util.lookup.InstanceContent;
33
34 public final class Provider extends AbstractLookup {
35     static {
36         System.setProperty("org.openide.util.Lookup", "org.netbeans.api.sendopts.Provider");
37     }
38     
39     final InstanceContent ic;
40     
41     public Provider() {
42         this(new InstanceContent());
43     }
44     
45     private Provider(InstanceContent ic) {
46         super(ic);
47         this.ic = ic;
48     }
49     
50     
51     public static void clearAll() {
52         Lookup l = Lookup.getDefault();
53         Assert.assertEquals("Our class", org.netbeans.api.sendopts.Provider.class, l.getClass());
54         
55         org.netbeans.api.sendopts.Provider p = (org.netbeans.api.sendopts.Provider)l;
56         p.ic.set(Collections.emptyList(), null);
57     }
58     
59     public static void add(Processor proc, Option... options) {
60         Lookup l = Lookup.getDefault();
61         Assert.assertEquals("Our class", org.netbeans.api.sendopts.Provider.class, l.getClass());
62         
63         org.netbeans.api.sendopts.Provider p = (org.netbeans.api.sendopts.Provider)l;
64         p.ic.add(new DefProvider(options, proc));
65     }
66
67     public static void assertOptionValues(Map JavaDoc<Option, String JavaDoc[]> optionValues) {
68         for (Map.Entry JavaDoc<Option,String JavaDoc[]> entry : optionValues.entrySet()) {
69             if (entry.getValue() == null) {
70                 Assert.fail("No value for option: " + entry.getKey());
71             }
72         }
73     }
74     
75     private static final class DefProvider extends OptionProcessor {
76         private Processor p;
77         private Option o;
78         
79         public DefProvider(Option[] arr, Processor p) {
80             this(OptionGroups.someOf(arr), p);
81         }
82         private DefProvider(Option o, Processor p) {
83             this.p = p;
84             this.o = o;
85         }
86         
87         protected Set JavaDoc<Option> getOptions() {
88             return Collections.singleton(o);
89         }
90
91         protected void process(Env env, Map JavaDoc<Option, String JavaDoc[]> optionValues) throws CommandException {
92             Map JavaDoc<Option, String JavaDoc[]> minus = new HashMap JavaDoc<Option, String JavaDoc[]>(optionValues);
93             minus.remove(o);
94             assertOptionValues(optionValues);
95             
96             p.process(env, minus);
97         }
98     }
99 }
100
Popular Tags