KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > tips > TipsManager


1 package org.antlr.works.tips;
2
3 import java.awt.*;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 /*
8
9 [The "BSD licence"]
10 Copyright (c) 2005 Jean Bovet
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions
15 are met:
16
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22 3. The name of the author may not be used to endorse or promote products
23 derived from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 */

37
38 public class TipsManager {
39
40     protected List JavaDoc<TipsProvider> providers;
41     protected TipsOverlay overlay;
42     protected int lastPosition;
43     protected boolean enabled;
44
45     public TipsManager() {
46         providers = new ArrayList JavaDoc<TipsProvider>();
47         enabled = true;
48     }
49
50     public void setOverlay(TipsOverlay overlay) {
51         this.overlay = overlay;
52     }
53
54     public void addProvider(TipsProvider provider) {
55         providers.add(provider);
56     }
57
58     public void setEnabled(boolean flag) {
59         this.enabled = flag;
60         if(!enabled)
61             hide();
62     }
63
64     public boolean enabled() {
65         return enabled;
66     }
67
68     public void displayAnyTipsAvailable(int position, Point location) {
69         if(!enabled)
70             return;
71
72         if(location == null) {
73             hide();
74             return;
75         }
76         
77         if(position == lastPosition)
78             return;
79
80         lastPosition = position;
81
82         List JavaDoc<String JavaDoc> tips = generateTips(position);
83         if(tips == null || tips.isEmpty()) {
84             hide();
85         } else {
86             overlay.setTips(tips);
87             overlay.setLocation(location);
88             overlay.display();
89         }
90     }
91
92     public void hide() {
93         overlay.hide();
94         lastPosition = -1;
95     }
96
97     public List JavaDoc<String JavaDoc> generateTips(int position) {
98         List JavaDoc<String JavaDoc> tips = new ArrayList JavaDoc<String JavaDoc>();
99         for(Iterator JavaDoc<TipsProvider> iter = providers.iterator(); iter.hasNext(); ) {
100             TipsProvider provider = iter.next();
101             List JavaDoc<String JavaDoc> ptips = provider.tipsProviderGetTips(position);
102             if(ptips != null && !ptips.isEmpty()) {
103                 tips.addAll(ptips);
104             }
105         }
106         return tips;
107     }
108
109 }
110
Popular Tags