1 19 20 21 package org.netbeans.modules.progress.ui; 22 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Dimension ; 26 import java.awt.Graphics ; 27 import java.awt.GridLayout ; 28 import java.awt.Insets ; 29 import java.awt.Toolkit ; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.KeyEvent ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import javax.swing.AbstractAction ; 35 import javax.swing.Action ; 36 import javax.swing.BorderFactory ; 37 import javax.swing.JComponent ; 38 import javax.swing.JPanel ; 39 import javax.swing.JScrollPane ; 40 import javax.swing.KeyStroke ; 41 import javax.swing.border.Border ; 42 import org.netbeans.progress.spi.InternalHandle; 43 44 48 public class PopupPane extends JScrollPane { 49 private JPanel view; 50 private HashSet <ListComponent> listComponents; 51 52 private ListComponent selected; 53 54 public PopupPane() { 55 listComponents = new HashSet <ListComponent>(); 56 view = new JPanel (); 57 GridLayout grid = new GridLayout (0, 1); 58 grid.setHgap(0); 59 grid.setVgap(0); 60 view.setLayout(grid); 61 view.setBorder(BorderFactory.createEmptyBorder()); 62 setName("progresspopup"); setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 64 setViewportView(view); 65 setFocusable(true); 66 setRequestFocusEnabled(true); 67 68 Action 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 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 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 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 } 100 101 public void addListComponent(ListComponent lst) { 102 listComponents.add(lst); 103 if (view.getComponentCount() > 0) { 104 JComponent previous = (JComponent )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 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 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 return new Dimension (count >3 ? ListComponent.ITEM_WIDTH + 22 139 : ListComponent.ITEM_WIDTH + 2, offset); 140 } 141 142 146 public void updateBoldFont(InternalHandle handle) { 147 Iterator 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 { 157 private Insets ins = new Insets (0, 0, 1, 0); 158 private Color col = new Color (221, 229, 248); 159 160 public BottomLineBorder () {} 161 162 public Insets getBorderInsets(Component c) { 163 return ins; 164 } 165 166 public boolean isBorderOpaque() { 167 return false; 168 } 169 170 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 171 Color 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 comp) { 179 Component [] 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 super.requestFocus(); 197 } 199 200 private class MoveDownAction extends AbstractAction { 201 202 MoveDownAction() { 203 } 204 205 public void actionPerformed(ActionEvent 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 { 221 222 MoveUpAction() { 223 } 224 225 public void actionPerformed(ActionEvent actionEvent) { 226 int index = PopupPane.this.view.getComponentCount(); 227 if (selected != null) { 228 index = findIndex(selected); 229 } 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 } 240 } 241 242 private class CancelAction extends AbstractAction { 243 public CancelAction () {} 244 245 public void actionPerformed(ActionEvent actionEvent) { 246 if (selected != null) { 247 Action 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 { 258 public SelectAction () {} 259 260 public void actionPerformed(ActionEvent actionEvent) { 261 if (selected != null) { 262 selected.getHandle().requestExplicitSelection(); 263 } 264 } 265 } 266 267 private class ViewAction extends AbstractAction { 268 public ViewAction () {} 269 270 public void actionPerformed(ActionEvent actionEvent) { 271 if (selected != null) { 272 selected.getHandle().requestView(); 273 } 274 } 275 } 276 277 } 278 | Popular Tags |