KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > idea > IdeaManager


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

39
40 public class IdeaManager {
41
42     protected List JavaDoc<IdeaProvider> providers = new ArrayList JavaDoc<IdeaProvider>();
43     protected Timer timer;
44     protected IdeaOverlay overlay;
45     protected IdeaManagerDelegate delegate;
46     protected boolean enabled = true;
47     protected int lastPosition;
48
49     public IdeaManager() {
50         timer = new Timer(1000, new TimerActionListener());
51         timer.setRepeats(false);
52     }
53
54     public void setDelegate(IdeaManagerDelegate delegate) {
55         this.delegate = delegate;
56     }
57
58     public void setOverlay(IdeaOverlay overlay) {
59         this.overlay = overlay;
60     }
61
62     public void setEnabled(boolean flag) {
63         this.enabled = flag;
64         if(!enabled) {
65             hide();
66         }
67     }
68
69     public boolean enabled() {
70         return enabled;
71     }
72
73     public void addProvider(IdeaProvider provider) {
74         providers.add(provider);
75     }
76
77     public void close() {
78         timer.stop();
79     }
80
81     public void hide() {
82         timer.stop();
83         overlay.hide();
84     }
85
86     public void displayAnyIdeasAvailable(int position) {
87         if(!enabled)
88             return;
89
90         List JavaDoc<IdeaAction> ideas = generateIdeaActions(position);
91         if(ideas == null || ideas.isEmpty())
92             overlay.hide();
93         else {
94             lastPosition = position;
95             overlay.setIdeas(ideas);
96             timer.restart();
97         }
98     }
99
100     public List JavaDoc<IdeaAction> generateIdeaActions(int position) {
101         List JavaDoc<IdeaAction> actions = new ArrayList JavaDoc<IdeaAction>();
102         for(Iterator JavaDoc<IdeaProvider> iter = providers.iterator(); iter.hasNext(); ) {
103             IdeaProvider provider = iter.next();
104             List JavaDoc<IdeaAction> pactions = provider.ideaProviderGetActions(position);
105             if(pactions != null && !pactions.isEmpty()) {
106                 actions.addAll(pactions);
107             }
108         }
109         return actions;
110     }
111
112     protected class TimerActionListener implements ActionListener JavaDoc {
113         public void actionPerformed(ActionEvent JavaDoc e) {
114             /** Make sure there is still some ideas to display */
115             List JavaDoc<IdeaAction> ideas = generateIdeaActions(lastPosition);
116             if(ideas.size() == 0)
117                 return;
118
119             /** Update the current list in case it has changed since the timer
120              * has been fired
121              */

122             overlay.setIdeas(ideas);
123
124             if(delegate != null) {
125                 if(!delegate.ideaManagerWillDisplayIdea())
126                     return;
127             }
128
129             overlay.display();
130         }
131     }
132 }
133
Popular Tags