KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > progress > ui > PopupPane


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
20
21 package org.netbeans.modules.progress.ui;
22
23 import java.awt.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.GridLayout JavaDoc;
28 import java.awt.Insets JavaDoc;
29 import java.awt.Toolkit JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.KeyEvent JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import javax.swing.AbstractAction JavaDoc;
35 import javax.swing.Action JavaDoc;
36 import javax.swing.BorderFactory JavaDoc;
37 import javax.swing.JComponent JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.JScrollPane JavaDoc;
40 import javax.swing.KeyStroke JavaDoc;
41 import javax.swing.border.Border JavaDoc;
42 import org.netbeans.progress.spi.InternalHandle;
43
44 /**
45  *
46  * @author mkleint
47  */

48 public class PopupPane extends JScrollPane JavaDoc {
49     private JPanel JavaDoc view;
50     private HashSet JavaDoc<ListComponent> listComponents;
51     /** Creates a new instance of PopupPane */
52     private ListComponent selected;
53     
54     public PopupPane() {
55         listComponents = new HashSet JavaDoc<ListComponent>();
56         view = new JPanel JavaDoc();
57         GridLayout JavaDoc grid = new GridLayout JavaDoc(0, 1);
58         grid.setHgap(0);
59         grid.setVgap(0);
60         view.setLayout(grid);
61         view.setBorder(BorderFactory.createEmptyBorder());
62         setName("progresspopup"); //NOI18N
63
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
64         setViewportView(view);
65         setFocusable(true);
66         setRequestFocusEnabled(true);
67
68         Action JavaDoc down = new MoveDownAction();
69         getActionMap().put("Move-Down", down);
70         getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "Move-Down");
71         getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "Move-Down");
72         
73         Action JavaDoc up = new MoveUpAction();
74         getActionMap().put("Move-Up", up);
75         getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "Move-Up");
76         getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "Move-Up");
77         Action JavaDoc cancel = new CancelAction();
78         getActionMap().put("Cancel-Task", cancel);
79         getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "Cancel-Task");
80         getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "Cancel-Task");
81         
82         Action JavaDoc select = new SelectAction();
83         getActionMap().put("select-task", select);
84         getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "select-task");
85         getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "select-task");
86         
87         
88         setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
89 // addFocusListener(new FocusListener() {
90
// public void focusLost(java.awt.event.FocusEvent e) {
91
// System.out.println("popup focus gained temp?=" + e.isTemporary());
92
// }
93
//
94
// public void focusGained(java.awt.event.FocusEvent e) {
95
// System.out.println("popup focus lost temporary?=" + e.isTemporary());
96
// }
97
//
98
// });
99
}
100     
101     public void addListComponent(ListComponent lst) {
102         listComponents.add(lst);
103         if (view.getComponentCount() > 0) {
104             JComponent JavaDoc previous = (JComponent JavaDoc)view.getComponent(view.getComponentCount() - 1);
105             previous.setBorder(new BottomLineBorder());
106         }
107         lst.setBorder(BorderFactory.createEmptyBorder());
108         view.add(lst);
109         if (listComponents.size() > 3) {
110             setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
111         } else {
112             setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
113         }
114     }
115     
116     public void removeListComponent(InternalHandle handle) {
117         Iterator JavaDoc it = listComponents.iterator();
118         while (it.hasNext()) {
119             ListComponent comp = (ListComponent)it.next();
120             if (comp.getHandle() == handle) {
121                 view.remove(comp);
122                 it.remove();
123                 break;
124             }
125         }
126         if (listComponents.size() > 3) {
127             setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
128         } else {
129             setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
130         }
131     }
132
133     public Dimension JavaDoc getPreferredSize() {
134         int count = view.getComponentCount();
135         int height = count > 0 ? view.getComponent(0).getPreferredSize().height : 0;
136         int offset = count > 3 ? height * 3 + 5 : (count * height) + 5;
137         // 22 is the width of the additional scrollbar
138
return new Dimension JavaDoc(count >3 ? ListComponent.ITEM_WIDTH + 22
139                                       : ListComponent.ITEM_WIDTH + 2, offset);
140     }
141
142     /**
143      * bold font is now used not only for explicitly selected items, but for any
144      * change in currently selected task.
145      */

146     public void updateBoldFont(InternalHandle handle) {
147         Iterator JavaDoc it = listComponents.iterator();
148         while (it.hasNext()) {
149             ListComponent comp = (ListComponent)it.next();
150             comp.markAsActive(handle == comp.getHandle());
151         }
152     }
153     
154     
155     
156     private static class BottomLineBorder implements Border JavaDoc {
157         private Insets JavaDoc ins = new Insets JavaDoc(0, 0, 1, 0);
158         private Color JavaDoc col = new Color JavaDoc(221, 229, 248);
159         
160         public BottomLineBorder () {}
161         
162         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
163             return ins;
164         }
165
166         public boolean isBorderOpaque() {
167             return false;
168         }
169
170         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
171            Color JavaDoc old = g.getColor();
172            g.setColor(col);
173            g.drawRect(x, y + height - 2, width, 1);
174            g.setColor(old);
175         }
176     }
177     
178     private int findIndex(Component JavaDoc comp) {
179         Component JavaDoc[] comps = view.getComponents();
180         for (int i = 0; i < comps.length; i++) {
181             if (comps[i] == comp) {
182                 return i;
183             }
184         }
185         return -1;
186     }
187
188     public void requestFocus() {
189 //#63666 - don't focus any of the tasks explicitly, wait for user action.
190
// if (view.getComponentCount() > 1) {
191
// if (selected == null || !selected.isDisplayable()) {
192
// selected = (ListComponent)view.getComponent(0);
193
// }
194
// selected.requestFocus();
195
// } else {
196
super.requestFocus();
197 // }
198
}
199     
200     private class MoveDownAction extends AbstractAction JavaDoc {
201         
202         MoveDownAction() {
203         }
204          
205         public void actionPerformed(ActionEvent JavaDoc actionEvent) {
206             int index = -1;
207             if (selected != null) {
208                 index = findIndex(selected);
209             }
210             index = index + 1;
211             if (index >= PopupPane.this.view.getComponentCount()) {
212                 index = 0;
213             }
214             selected = (ListComponent)PopupPane.this.view.getComponent(index);
215             selected.requestFocus();
216         }
217         
218     }
219     
220     private class MoveUpAction extends AbstractAction JavaDoc {
221         
222         MoveUpAction() {
223         }
224          
225         public void actionPerformed(ActionEvent JavaDoc actionEvent) {
226             int index = PopupPane.this.view.getComponentCount();
227             if (selected != null) {
228                 index = findIndex(selected);
229 // selected.setBackground(new Color(249, 249, 249));
230
}
231             index = index - 1;
232             if (index < 0) {
233                 index = PopupPane.this.view.getComponentCount() - 1;
234             }
235             selected = (ListComponent)PopupPane.this.view.getComponent(index);
236             selected.requestFocus();
237 // selected.setBackground(selectBgColor);
238
// selected.scrollRectToVisible(selected.getBounds());
239
}
240     }
241     
242     private class CancelAction extends AbstractAction JavaDoc {
243         public CancelAction () {}
244         
245         public void actionPerformed(ActionEvent JavaDoc actionEvent) {
246             if (selected != null) {
247                 Action JavaDoc act = selected.getCancelAction();
248                 if (act != null) {
249                     act.actionPerformed(actionEvent);
250                 } else {
251                     Toolkit.getDefaultToolkit().beep();
252                 }
253             }
254         }
255     }
256
257     private class SelectAction extends AbstractAction JavaDoc {
258         public SelectAction () {}
259         
260         public void actionPerformed(ActionEvent JavaDoc actionEvent) {
261             if (selected != null) {
262                 selected.getHandle().requestExplicitSelection();
263             }
264         }
265     }
266     
267     private class ViewAction extends AbstractAction JavaDoc {
268         public ViewAction () {}
269         
270         public void actionPerformed(ActionEvent JavaDoc actionEvent) {
271             if (selected != null) {
272                 selected.getHandle().requestView();
273             }
274         }
275     }
276     
277 }
278
Popular Tags