Changeset 2391 in josm
- Timestamp:
- 2009-11-03T21:41:37+01:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/MapFrame.java
r2269 r2391 7 7 import java.awt.Container; 8 8 import java.awt.Dimension; 9 import java.awt.event.MouseWheelListener; 10 import java.awt.event.MouseWheelEvent; 9 11 import java.util.ArrayList; 10 12 import java.util.List; … … 262 264 jb.setFloatable(false); 263 265 jb.add(toolBarActions); 264 jb.addSeparator( );266 jb.addSeparator(new Dimension(0,10)); 265 267 jb.add(toolBarToggle); 266 268 if(Main.pref.getBoolean("sidetoolbar.visible", true)) 267 269 { 268 270 if(Main.pref.getBoolean("sidetoolbar.scrollable", true)) { 269 panel.add(new ScrollViewport(jb, ScrollViewport.VERTICAL_DIRECTION), 270 BorderLayout.WEST); 271 final ScrollViewport svp = new ScrollViewport(jb, ScrollViewport.VERTICAL_DIRECTION); 272 panel.add(svp, BorderLayout.WEST); 273 jb.addMouseWheelListener(new MouseWheelListener() { 274 public void mouseWheelMoved(MouseWheelEvent e) { 275 svp.scroll(0,e.getUnitsToScroll() * 5); 276 } 277 }); 271 278 } else { 272 279 panel.add(jb, BorderLayout.WEST); -
trunk/src/org/openstreetmap/josm/gui/ScrollViewport.java
r1677 r2391 9 9 import java.awt.event.ActionEvent; 10 10 import java.awt.event.ActionListener; 11 import java.awt.event.ComponentAdapter; 12 import java.awt.event.ComponentEvent; 11 13 import java.awt.event.MouseAdapter; 12 14 import java.awt.event.MouseEvent; 15 16 import java.util.ArrayList; 17 import java.util.List; 13 18 14 19 import javax.swing.JButton; … … 62 67 private JViewport vp = new JViewport(); 63 68 private JComponent component = null; 64 65 private Timer timer = new Timer(200, new ActionListener() { 69 70 private List<JButton> buttons = new ArrayList<JButton>(); 71 72 private Timer timer = new Timer(100, new ActionListener() { 66 73 public void actionPerformed(ActionEvent arg0) { 67 74 ScrollViewport.this.scroll(); … … 78 85 public ScrollViewport(int direction) { 79 86 setLayout(new BorderLayout()); 80 87 81 88 JButton button; 82 89 … … 88 95 button.setIcon(ImageProvider.get("svpUp")); 89 96 add(button, BorderLayout.NORTH); 97 buttons.add(button); 90 98 } 91 99 … … 97 105 button.setIcon(ImageProvider.get("svpDown")); 98 106 add(button, BorderLayout.SOUTH); 107 buttons.add(button); 99 108 } 100 109 … … 106 115 button.setIcon(ImageProvider.get("svpLeft")); 107 116 add(button, BorderLayout.WEST); 117 buttons.add(button); 108 118 } 109 119 … … 115 125 button.setIcon(ImageProvider.get("svpRight")); 116 126 add(button, BorderLayout.EAST); 127 buttons.add(button); 117 128 } 118 129 119 130 add(vp, BorderLayout.CENTER); 120 131 132 this.addComponentListener(new ComponentAdapter() { 133 @Override public void componentResized(ComponentEvent e) { 134 showOrHideButtons(); 135 } 136 }); 137 138 showOrHideButtons(); 139 121 140 timer.setRepeats(true); 122 141 timer.setInitialDelay(400); … … 133 152 Rectangle viewRect = vp.getViewRect(); 134 153 135 int delta; 136 int newY = 0; 137 int newX = 0; 154 int deltaX = 0; 155 int deltaY = 0; 138 156 139 157 if (direction < LEFT_DIRECTION) { 140 newX = viewRect.x; 141 delta = viewRect.height * 4 / 5; 158 deltaY = viewRect.height * 2 / 7; 142 159 } else { 143 newY = viewRect.y; 144 delta = viewRect.width * 4 / 5; 160 deltaX = viewRect.width * 2 / 7; 145 161 } 146 162 147 163 switch (direction) { 148 case UP_DIRECTION : 149 newY = viewRect.y - delta; 150 if (newY < 0) { 151 newY = 0; 152 } 153 break; 154 case DOWN_DIRECTION : 155 newY = viewRect.y + delta; 156 if (newY > compSize.height - viewRect.height) { 157 newY = compSize.height - viewRect.height; 158 } 159 break; 160 case LEFT_DIRECTION : 161 newX = viewRect.x - delta; 162 if (newX < 0) { 163 newX = 0; 164 } 165 break; 166 case RIGHT_DIRECTION : 167 newX = viewRect.x + delta; 168 if (newX > compSize.width - viewRect.width) { 169 newX = compSize.width - viewRect.width; 170 } 171 break; 172 default : 173 throw new IllegalStateException("Unknown direction : [0]" + direction); 164 case UP_DIRECTION : 165 deltaY *= -1; 166 break; 167 case LEFT_DIRECTION : 168 deltaX *= -1; 169 break; 170 } 171 172 scroll(deltaX, deltaY); 173 } 174 public synchronized void scroll(int deltaX, int deltaY) { 175 if (component == null) { 176 return; 177 } 178 Dimension compSize = component.getSize(); 179 Rectangle viewRect = vp.getViewRect(); 180 181 int newX = viewRect.x + deltaX; 182 int newY = viewRect.y + deltaY; 183 184 if (newY < 0) { 185 newY = 0; 186 } 187 if (newY > compSize.height - viewRect.height) { 188 newY = compSize.height - viewRect.height; 189 } 190 if (newX < 0) { 191 newX = 0; 192 } 193 if (newX > compSize.width - viewRect.width) { 194 newX = compSize.width - viewRect.width; 174 195 } 175 196 … … 177 198 } 178 199 200 /** 201 * Update the visibility of the buttons 202 * Only show them if the Viewport is too small for the content. 203 */ 204 public void showOrHideButtons() { 205 boolean needButtons = vp.getViewSize().height > vp.getViewRect().height || 206 vp.getViewSize().width > vp.getViewRect().width; 207 for (JButton b : buttons) { 208 b.setVisible(needButtons); 209 } 210 } 211 179 212 public Rectangle getViewRect() { 180 213 return vp.getViewRect();
Note:
See TracChangeset
for help on using the changeset viewer.