1 19 20 package org.netbeans.modules.xml.xam.ui.column; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Dimension ; 26 import java.awt.Graphics ; 27 import java.awt.GridBagConstraints ; 28 import java.awt.GridBagLayout ; 29 import java.awt.GridLayout ; 30 import java.awt.Insets ; 31 import java.awt.Point ; 32 import java.awt.Rectangle ; 33 import java.awt.event.ActionEvent ; 34 import java.awt.event.ActionListener ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.Map ; 38 import java.util.Map.Entry; 39 import java.util.Set ; 40 import javax.accessibility.AccessibleContext ; 41 import javax.swing.AbstractAction ; 42 import javax.swing.BorderFactory ; 43 import javax.swing.Box ; 44 import javax.swing.Icon ; 45 import javax.swing.JButton ; 46 import javax.swing.JLabel ; 47 import javax.swing.JPanel ; 48 import javax.swing.JScrollPane ; 49 import javax.swing.JViewport ; 50 import javax.swing.UIManager ; 51 import javax.swing.border.Border ; 52 import javax.swing.border.EmptyBorder ; 53 import javax.swing.event.ChangeEvent ; 54 import javax.swing.event.ChangeListener ; 55 56 62 public class LinkPanel extends JPanel implements ActionListener { 63 private static final long serialVersionUID = 1L; 64 65 private Component layoutFiller; 66 67 private Map <LinkButton, Column> buttonColumnMap; 68 69 private JScrollPane scrollPane; 70 71 private ColumnView columnView; 72 73 private JPanel linkPanel; 74 private static final int ICON_WIDTH = 11; 75 private static final int ICON_HEIGHT = 11; 76 private static final int[] xpoints = new int[20]; 77 private static final int[] ypoints = new int[20]; 78 79 84 public LinkPanel(ColumnView view) { 85 super(new BorderLayout ()); 86 linkPanel = new JPanel (new GridBagLayout ()); 87 scrollPane = new JScrollPane (linkPanel, 88 JScrollPane.VERTICAL_SCROLLBAR_NEVER, 89 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 90 scrollPane.setBorder(null); 91 add(scrollPane, BorderLayout.CENTER); 92 columnView = view; 93 Border b = (Border ) UIManager.get("Nb.Editor.Toolbar.border"); if (b == null) { 96 b = BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK); 98 } 99 setBorder(b); 100 buttonColumnMap = new HashMap <LinkButton, Column>(); 101 102 JButton left = new TimerButton(new ScrollLeftAction(scrollPane)); 104 JButton right = new TimerButton(new ScrollRightAction(scrollPane)); 105 configureButton(left, new LeftIcon()); 106 configureButton(right, new RightIcon()); 107 left.setPreferredSize(new Dimension (17, 17)); 108 right.setPreferredSize(new Dimension (17, 17)); 109 JPanel buttonPanel = new JPanel (new GridLayout (1, 0)); 110 buttonPanel.setBorder(new EmptyBorder (0, 3, 1, 2)); 111 buttonPanel.add(left); 112 buttonPanel.add(right); 113 add(buttonPanel, BorderLayout.EAST); 114 } 115 116 public void actionPerformed(ActionEvent e) { 117 Object src = e.getSource(); 118 if (src instanceof LinkButton) { 119 LinkButton button = (LinkButton) src; 120 if (scrollPane != null) { 124 JViewport vp = scrollPane.getViewport(); 125 Rectangle visRect = vp.getViewRect(); 126 Rectangle compRect = button.getBounds(); 127 Component view = vp.getView(); 128 visRect.x = Math.max(0, Math.min(compRect.x - 129 (visRect.width - compRect.width) / 2, 130 view.getWidth() - visRect.width)); 131 vp.scrollRectToVisible(visRect); 132 } 133 Column column = buttonColumnMap.get(button); 135 columnView.scrollToColumn(column, true); 136 } 137 } 138 139 144 public void appendLink(Column column) { 145 if (linkPanel.getComponentCount() > 0) { 146 GridBagConstraints gbc = new GridBagConstraints (); 148 gbc.insets = new Insets (0, 3, 2, 0); 152 linkPanel.add(new JLabel (">"), gbc); } 154 LinkButton button = new LinkButton(column.getTitle()); 155 AccessibleContext ac = button.getAccessibleContext(); 156 ac.setAccessibleName(column.getTitle()); 157 ac.setAccessibleDescription(column.getDescription()); 158 button.addActionListener(this); 159 buttonColumnMap.put(button, column); 160 GridBagConstraints gbc = new GridBagConstraints (); 161 gbc.insets = new Insets (0, 3, 0, 0); 162 linkPanel.add(button, gbc); 163 if (layoutFiller != null) { 165 linkPanel.remove(layoutFiller); 166 } else { 167 layoutFiller = Box.createHorizontalGlue(); 168 } 169 gbc.fill = GridBagConstraints.HORIZONTAL; 170 gbc.weightx = 1.0d; 171 linkPanel.add(layoutFiller, gbc); 172 linkPanel.revalidate(); 173 linkPanel.repaint(); 174 } 175 176 179 public void clearLinks() { 180 linkPanel.removeAll(); 181 buttonColumnMap.clear(); 182 linkPanel.revalidate(); 183 linkPanel.repaint(); 184 } 185 186 192 private static void configureButton(JButton button, Icon icon) { 193 button.setIcon(icon); 194 button.setMargin(null); 195 button.setText(null); 196 button.setFocusable(false); 197 } 198 199 204 public void truncateLinks(int index) { 205 int count = linkPanel.getComponentCount() - 1; 207 index = index * 2 - 1; 209 while (count > index) { 210 Component child = linkPanel.getComponent(index); 211 if (child instanceof LinkButton) { 212 buttonColumnMap.remove((LinkButton) child); 213 } 214 linkPanel.remove(index); 215 count--; 216 } 217 linkPanel.revalidate(); 218 linkPanel.repaint(); 219 } 220 221 226 public void updateLink(Column column) { 227 Set <Entry<LinkButton, Column>> entries = buttonColumnMap.entrySet(); 228 Iterator <Entry<LinkButton, Column>> iter = entries.iterator(); 229 while (iter.hasNext()) { 230 Entry<LinkButton, Column> entry = iter.next(); 231 if (entry.getValue().equals(column)) { 232 LinkButton button = entry.getKey(); 233 button.setText(column.getTitle()); 234 } 235 } 236 } 237 238 241 private static class ScrollLeftAction extends AbstractAction implements 242 ChangeListener { 243 244 private static final long serialVersionUID = 1L; 245 246 private JScrollPane pane; 247 248 253 public ScrollLeftAction(JScrollPane pane) { 254 super(); 255 this.pane = pane; 256 pane.getViewport().addChangeListener(this); 257 } 258 259 public void actionPerformed(ActionEvent e) { 260 JViewport vp = pane.getViewport(); 261 Dimension size = vp.getExtentSize(); 262 Point p = vp.getViewPosition(); 263 p.x -= (size.width / 10); 264 if (p.x < 0) { 265 p.x = 0; 266 } 267 vp.setViewPosition(p); 268 } 269 270 public void stateChanged(ChangeEvent e) { 271 JViewport vp = pane.getViewport(); 272 Point p = vp.getViewPosition(); 273 setEnabled(p.x > 0); 274 } 275 } 276 277 280 private static class ScrollRightAction extends AbstractAction implements 281 ChangeListener { 282 283 private static final long serialVersionUID = 1L; 284 285 private JScrollPane pane; 286 287 292 public ScrollRightAction(JScrollPane pane) { 293 super(); 294 this.pane = pane; 295 pane.getViewport().addChangeListener(this); 296 } 297 298 public void actionPerformed(ActionEvent e) { 299 JViewport vp = pane.getViewport(); 300 Dimension size = vp.getExtentSize(); 301 Point p = vp.getViewPosition(); 302 p.x += (size.width / 10); 303 int max = vp.getViewSize().width - size.width; 304 if (p.x > max) { 305 p.x = max; 306 } 307 vp.setViewPosition(p); 308 } 309 310 public void stateChanged(ChangeEvent e) { 311 JViewport vp = pane.getViewport(); 312 Dimension size = vp.getExtentSize(); 313 Point p = vp.getViewPosition(); 314 int max = vp.getViewSize().width - size.width; 315 setEnabled(p.x < max); 316 } 317 } 318 319 322 private static class LeftIcon implements Icon { 323 324 public int getIconHeight() { 325 return ICON_HEIGHT; 326 } 327 328 public int getIconWidth() { 329 return ICON_WIDTH; 330 } 331 332 public void paintIcon(Component c, Graphics g, int x, int y) { 333 y -= 2; 334 g.setColor(c.isEnabled() ? c.getForeground() : 335 UIManager.getColor("controlShadow")); int wid = getIconWidth(); 337 int hi = getIconHeight() + 1; 338 xpoints[0] = x + (wid - 4); 339 ypoints[0] = y + 2; 340 xpoints[1] = xpoints[0]; 341 ypoints[1] = y + hi + 1; 342 xpoints[2] = x + 2; 343 ypoints[2] = y + (hi / 2) + 1; 344 g.fillPolygon(xpoints, ypoints, 3); 345 } 346 } 347 348 351 private static class RightIcon implements Icon { 352 353 public int getIconWidth() { 354 return ICON_WIDTH; 355 } 356 357 public int getIconHeight() { 358 return ICON_HEIGHT - 2; 359 } 360 361 public void paintIcon(Component c, Graphics g, int x, int y) { 362 y -= 2; 363 g.setColor(c.isEnabled() ? c.getForeground() : 364 UIManager.getColor("controlShadow")); int wid = getIconWidth(); 366 int hi = getIconHeight() + 1; 367 xpoints[0] = x + 3; 368 ypoints[0] = y + 1; 369 xpoints[1] = x + 3; 370 ypoints[1] = y + hi + 1; 371 xpoints[2] = x + (wid - 4) + 1; 372 ypoints[2] = y + (hi / 2) + 1; 373 g.fillPolygon(xpoints, ypoints, 3); 374 } 375 } 376 } 377 | Popular Tags |