- Timestamp:
- 2017-05-06T17:58:11+02:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/ScrollViewport.java
r10634 r12073 21 21 import org.openstreetmap.josm.tools.ImageProvider; 22 22 23 /** A viewport with UP and DOWN arrow buttons, so that the user can make the 23 /** 24 * A viewport with UP and DOWN arrow buttons, so that the user can make the 24 25 * content scroll. 26 * 27 * This should be used for long, vertical toolbars. 25 28 */ 26 29 public class ScrollViewport extends JPanel { … … 32 35 public static final int LEFT_DIRECTION = 4; 33 36 public static final int RIGHT_DIRECTION = 8; 37 /** 38 * Allow vertical scrolling 39 */ 34 40 public static final int VERTICAL_DIRECTION = UP_DIRECTION | DOWN_DIRECTION; 41 42 /** 43 * Allow horizontal scrolling 44 */ 35 45 public static final int HORIZONTAL_DIRECTION = LEFT_DIRECTION | RIGHT_DIRECTION; 46 47 /** 48 * Allow scrolling in both directions 49 */ 36 50 public static final int ALL_DIRECTION = HORIZONTAL_DIRECTION | VERTICAL_DIRECTION; 37 51 … … 72 86 private int scrollDirection = NO_SCROLL; 73 87 88 private final int allowedScrollDirections; 89 90 private final ComponentAdapter refreshButtonsOnResize = new ComponentAdapter() { 91 @Override 92 public void componentResized(ComponentEvent e) { 93 showOrHideButtons(); 94 } 95 }; 96 97 /** 98 * Create a new scroll viewport 99 * @param c The component to display as content. 100 * @param direction The direction to scroll. 101 * Should be one of {@link #VERTICAL_DIRECTION}, {@link #HORIZONTAL_DIRECTION}, {@link #ALL_DIRECTION} 102 */ 74 103 public ScrollViewport(JComponent c, int direction) { 75 104 this(direction); … … 77 106 } 78 107 108 /** 109 * Create a new scroll viewport 110 * @param direction The direction to scroll. 111 * Should be one of {@link #VERTICAL_DIRECTION}, {@link #HORIZONTAL_DIRECTION}, {@link #ALL_DIRECTION} 112 */ 79 113 public ScrollViewport(int direction) { 80 setLayout(new BorderLayout()); 81 82 JButton button; 114 super(new BorderLayout()); 115 this.allowedScrollDirections = direction; 83 116 84 117 // UP 85 118 if ((direction & UP_DIRECTION) != 0) { 86 button = new JButton(); 87 button.addMouseListener(new ScrollViewPortMouseListener(UP_DIRECTION)); 88 button.setPreferredSize(new Dimension(10, 10)); 89 button.setIcon(ImageProvider.get("svpUp")); 90 add(button, BorderLayout.NORTH); 91 buttons.add(button); 119 addScrollButton(UP_DIRECTION, "svpUp", BorderLayout.NORTH); 92 120 } 93 121 94 122 // DOWN 95 123 if ((direction & DOWN_DIRECTION) != 0) { 96 button = new JButton(); 97 button.addMouseListener(new ScrollViewPortMouseListener(DOWN_DIRECTION)); 98 button.setPreferredSize(new Dimension(10, 10)); 99 button.setIcon(ImageProvider.get("svpDown")); 100 add(button, BorderLayout.SOUTH); 101 buttons.add(button); 124 addScrollButton(DOWN_DIRECTION, "svpDown", BorderLayout.SOUTH); 102 125 } 103 126 104 127 // LEFT 105 128 if ((direction & LEFT_DIRECTION) != 0) { 106 button = new JButton(); 107 button.addMouseListener(new ScrollViewPortMouseListener(LEFT_DIRECTION)); 108 button.setPreferredSize(new Dimension(10, 10)); 109 button.setIcon(ImageProvider.get("svpLeft")); 110 add(button, BorderLayout.WEST); 111 buttons.add(button); 129 addScrollButton(LEFT_DIRECTION, "svpLeft", BorderLayout.WEST); 112 130 } 113 131 114 132 // RIGHT 115 133 if ((direction & RIGHT_DIRECTION) != 0) { 116 button = new JButton(); 117 button.addMouseListener(new ScrollViewPortMouseListener(RIGHT_DIRECTION)); 118 button.setPreferredSize(new Dimension(10, 10)); 119 button.setIcon(ImageProvider.get("svpRight")); 120 add(button, BorderLayout.EAST); 121 buttons.add(button); 134 addScrollButton(RIGHT_DIRECTION, "svpRight", BorderLayout.EAST); 122 135 } 123 136 124 137 add(vp, BorderLayout.CENTER); 125 138 126 this.addComponentListener(new ComponentAdapter() { 127 @Override public void componentResized(ComponentEvent e) { 128 showOrHideButtons(); 129 } 130 }); 139 this.addComponentListener(refreshButtonsOnResize); 131 140 132 141 showOrHideButtons(); … … 136 145 } 137 146 147 private void addScrollButton(int direction, String icon, String borderLayoutPosition) { 148 JButton button = new JButton(); 149 button.addMouseListener(new ScrollViewPortMouseListener(direction)); 150 button.setPreferredSize(new Dimension(10, 10)); 151 button.setIcon(ImageProvider.get(icon)); 152 add(button, borderLayoutPosition); 153 buttons.add(button); 154 } 155 156 /** 157 * Scrolls in the currently selected scroll direction. 158 */ 138 159 public synchronized void scroll() { 139 160 int direction = scrollDirection; … … 166 187 } 167 188 189 /** 190 * Scrolls by the given offset 191 * @param deltaX offset x 192 * @param deltaY offset y 193 */ 168 194 public synchronized void scroll(int deltaX, int deltaY) { 169 195 if (component == null) … … 196 222 */ 197 223 public void showOrHideButtons() { 198 boolean needButtons = vp.getViewSize().height > vp.getViewRect().height || 199 vp.getViewSize().width > vp.getViewRect().width; 224 boolean needButtons = false; 225 if ((allowedScrollDirections & VERTICAL_DIRECTION) != 0) { 226 needButtons |= getViewSize().height > getViewRect().height; 227 } 228 if ((allowedScrollDirections & HORIZONTAL_DIRECTION) != 0) { 229 needButtons |= getViewSize().width > getViewRect().width; 230 } 200 231 for (JButton b : buttons) { 201 232 b.setVisible(needButtons); … … 215 246 } 216 247 248 @Override 249 public Dimension getPreferredSize() { 250 return vp.getPreferredSize(); 251 } 252 253 @Override 254 public Dimension getMinimumSize() { 255 return vp.getMinimumSize(); 256 } 257 258 /** 259 * Sets the component to be used as content. 260 * @param c The component 261 */ 217 262 public void add(JComponent c) { 218 263 vp.removeAll(); 264 if (this.component != null) { 265 this.component.removeComponentListener(refreshButtonsOnResize); 266 } 219 267 this.component = c; 268 c.addComponentListener(refreshButtonsOnResize); 220 269 vp.add(c); 221 270 }
Note:
See TracChangeset
for help on using the changeset viewer.