Ticket #2372: scrollable.2.patch
File scrollable.2.patch, 31.1 KB (added by tanya, 15 years ago) |
---|
-
src/test/java/org/sophie2/base/commons/gui/VerticalJScrollPaneDemo.java
### Eclipse Workspace Patch 1.0 #P org.sophie2.base.commons
1 package org.sophie2.base.commons.gui; 2 3 import java.awt.Color; 4 import java.awt.Dimension; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 8 import javax.swing.BoxLayout; 9 import javax.swing.JButton; 10 import javax.swing.JFrame; 11 import javax.swing.JPanel; 12 13 import org.sophie2.base.commons.gui.CustomScrollPane; 14 import org.sophie2.core.logging.SophieLog; 15 16 /** 17 * Demo for testing {@link CustomScrollPane} with {@link CustomScrollPane#VERTICAL} orientation. 18 * 19 * @author tanya 20 */ 21 public class VerticalJScrollPaneDemo { 22 23 private JFrame frame; 24 25 /** 26 * Constructs an object - frame with {@link CustomScrollPane} 27 * with {@link CustomScrollPane#VERTICAL} orientation. 28 */ 29 public VerticalJScrollPaneDemo() { 30 this.frame = new JFrame(); 31 this.frame.setVisible(true); 32 this.frame.setSize(new Dimension(500, 500)); 33 this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 34 35 36 CustomScrollPane sophie = new CustomScrollPane(CustomScrollPane.VERTICAL); 37 38 this.frame.setContentPane(sophie.swingComponent()); 39 40 JButton but1 = new JButton("but1"); 41 but1.addActionListener(new ActionListener() { 42 43 public void actionPerformed(ActionEvent e) { 44 SophieLog.trace("but1"); 45 } 46 }); 47 48 JButton but2 = new JButton("but2"); 49 but2.addActionListener(new ActionListener() { 50 51 public void actionPerformed(ActionEvent e) { 52 SophieLog.trace("but2"); 53 } 54 }); 55 56 JButton but3 = new JButton("but3"); 57 but3.addActionListener(new ActionListener() { 58 59 public void actionPerformed(ActionEvent e) { 60 SophieLog.trace("but3"); 61 } 62 }); 63 64 JButton but4 = new JButton("but4"); 65 but4.addActionListener(new ActionListener() { 66 67 public void actionPerformed(ActionEvent e) { 68 SophieLog.trace("but4"); 69 } 70 }); 71 72 JButton but5 = new JButton("but5"); 73 but5.addActionListener(new ActionListener() { 74 75 public void actionPerformed(ActionEvent e) { 76 SophieLog.trace("but5"); 77 } 78 }); 79 80 JButton but6 = new JButton("but6"); 81 but6.addActionListener(new ActionListener() { 82 83 public void actionPerformed(ActionEvent e) { 84 SophieLog.trace("but6"); 85 } 86 }); 87 88 JButton but7 = new JButton("but7"); 89 but7.addActionListener(new ActionListener() { 90 91 public void actionPerformed(ActionEvent e) { 92 SophieLog.trace("but7"); 93 } 94 }); 95 96 JButton but8 = new JButton("but8"); 97 but8.addActionListener(new ActionListener() { 98 99 public void actionPerformed(ActionEvent e) { 100 SophieLog.trace("but8"); 101 } 102 }); 103 104 JButton but9 = new JButton("but9"); 105 but9.addActionListener(new ActionListener() { 106 107 public void actionPerformed(ActionEvent e) { 108 SophieLog.trace("but9"); 109 } 110 }); 111 112 JButton but10 = new JButton("but10"); 113 but10.addActionListener(new ActionListener() { 114 115 public void actionPerformed(ActionEvent e) { 116 SophieLog.trace("but10"); 117 } 118 }); 119 120 JButton but11 = new JButton("but11"); 121 but11.addActionListener(new ActionListener() { 122 123 public void actionPerformed(ActionEvent e) { 124 SophieLog.trace("but11"); 125 } 126 }); 127 128 JButton but12 = new JButton("but12"); 129 but12.addActionListener(new ActionListener() { 130 131 public void actionPerformed(ActionEvent e) { 132 SophieLog.trace("but12"); 133 } 134 }); 135 136 JButton but13 = new JButton("but13"); 137 but13.addActionListener(new ActionListener() { 138 139 public void actionPerformed(ActionEvent e) { 140 SophieLog.trace("but13"); 141 } 142 }); 143 144 JButton but14 = new JButton("but14"); 145 but14.addActionListener(new ActionListener() { 146 147 public void actionPerformed(ActionEvent e) { 148 SophieLog.trace("but14"); 149 } 150 }); 151 152 JButton but15 = new JButton("but15"); 153 but15.addActionListener(new ActionListener() { 154 155 public void actionPerformed(ActionEvent e) { 156 SophieLog.trace("but15"); 157 } 158 }); 159 160 JButton but16 = new JButton("but16"); 161 but16.addActionListener(new ActionListener() { 162 163 public void actionPerformed(ActionEvent e) { 164 SophieLog.trace("but16"); 165 } 166 }); 167 168 JButton but17 = new JButton("but17"); 169 but17.addActionListener(new ActionListener() { 170 171 public void actionPerformed(ActionEvent e) { 172 SophieLog.trace("but17"); 173 } 174 }); 175 176 JButton but18 = new JButton("but18"); 177 but18.addActionListener(new ActionListener() { 178 179 public void actionPerformed(ActionEvent e) { 180 SophieLog.trace("but18"); 181 } 182 }); 183 184 JButton but19 = new JButton("but19"); 185 but19.addActionListener(new ActionListener() { 186 187 public void actionPerformed(ActionEvent e) { 188 SophieLog.trace("but19"); 189 } 190 }); 191 192 JButton but20 = new JButton("but20"); 193 but20.addActionListener(new ActionListener() { 194 195 public void actionPerformed(ActionEvent e) { 196 SophieLog.trace("but20"); 197 } 198 }); 199 200 JPanel p = new JPanel(); 201 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); 202 203 p.add(but1); 204 p.add(but2); 205 p.add(but3); 206 p.add(but4); 207 p.add(but5); 208 p.add(but6); 209 p.add(but7); 210 p.add(but8); 211 p.add(but9); 212 p.add(but10); 213 p.add(but11); 214 p.add(but12); 215 p.add(but13); 216 p.validate(); 217 p.add(but14); 218 p.add(but15); 219 p.add(but16); 220 p.add(but17); 221 p.add(but18); 222 p.add(but19); 223 p.add(but20); 224 p.validate(); 225 sophie.setViewportView(p); 226 sophie.swingComponent().setBackground(Color.WHITE); 227 } 228 229 /** 230 * The main method! 231 * @param args 232 */ 233 public static void main(String args[]) { 234 new VerticalJScrollPaneDemo(); 235 } 236 } -
src/main/java/org/sophie2/base/commons/gui/CustomScrollPane.java
1 package org.sophie2.base.commons.gui; 2 3 import java.awt.Dimension; 4 import java.awt.Point; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.awt.event.MouseEvent; 8 import java.awt.event.MouseListener; 9 10 import javax.swing.JButton; 11 import javax.swing.JComponent; 12 import javax.swing.JPanel; 13 import javax.swing.JScrollBar; 14 import javax.swing.SwingUtilities; 15 import javax.swing.Timer; 16 17 import org.sophie2.core.logging.SophieLog; 18 import org.sophie2.core.prolib.impl.BaseProObject; 19 20 /** 21 * Scroll pane that provides a component to be scrolled without the presence of 22 * {@link JScrollBar}. The user will be able to scroll using two buttons - 23 * forward and backward. The component to be scrolled is the viewport view of 24 * the scroll pane. The viewport if this part of the scroll pane in which the viewport view will be visualized. 25 * 26 * @author tanya 27 */ 28 public class CustomScrollPane extends BaseProObject { 29 30 /** 31 * Constant used for defining that the orientation is from left to right. 32 */ 33 public static final boolean HORIZONTAL = true; 34 35 /** 36 * Constant used for defining that the orientation is from top to bottom. 37 */ 38 public static final boolean VERTICAL = false; 39 40 /** 41 * Default value for the step of translation. 42 */ 43 public static final int DEFAULT_STEP = 5; 44 45 /** 46 * Default value for the delay of the timer 47 * used while pressing the forward and backward buttons. 48 */ 49 public static final int DEFAULT_DELAY = 100; 50 51 /** 52 * Default value for the translation of the scrollable pane. 53 */ 54 public static final int DEFAULT_TRANSLATION = 0; 55 56 private JButton backwardArrow; 57 private JButton forwardArrow; 58 59 private JComponent viewportView; 60 61 private JPanel scrollPane; 62 63 private Dimension viewportViewSize; 64 private JPanel viewport; 65 66 private int step = DEFAULT_STEP; 67 private int delay = DEFAULT_DELAY; 68 private int translation = DEFAULT_TRANSLATION; 69 private boolean orientation; 70 71 private Timer forwardScrollTimer; 72 private Timer backwardScrollTimer; 73 74 /** 75 * Constructs a scroll pane with given orientation. The values of the 76 * orientation can be {@link #HORIZONTAL} or {@link #VERTICAL}. If the value 77 * of orientation is <b>true</b> then the components will be laid out from 78 * left to right. Otherwise, if it is <b>false</b>, then the components will 79 * be laid out from top to bottom. 80 * 81 * @param orientation 82 * <b>true</b> if components should be laid out from left to 83 * right, <b>false</b> if components should be laid out from top 84 * to bottom. 85 * 86 */ 87 public CustomScrollPane(boolean orientation) { 88 this.orientation = orientation; 89 90 if (this.orientation) { 91 this.backwardArrow = new JButton("<"); 92 this.forwardArrow = new JButton(">"); 93 } else { 94 this.backwardArrow = new JButton("^"); 95 this.forwardArrow = new JButton("v"); 96 } 97 98 this.scrollPane = new JPanel(new ScrollPaneLayout(this.orientation)); 99 this.scrollPane.add(this.backwardArrow, ScrollPaneLayout.BACKWARD); 100 this.scrollPane.add(this.forwardArrow, ScrollPaneLayout.FORWARD); 101 102 setupTimers(); 103 setup(); 104 } 105 106 private void setupTimers() { 107 this.forwardScrollTimer = new Timer(this.delay, new ActionListener() { 108 109 @SuppressWarnings("synthetic-access") 110 public void actionPerformed(ActionEvent e) { 111 tryForward(); 112 } 113 }); 114 115 this.forwardScrollTimer.setInitialDelay(this.delay); 116 117 118 this.backwardScrollTimer = new Timer(this.delay, new ActionListener() { 119 120 @SuppressWarnings("synthetic-access") 121 public void actionPerformed(ActionEvent e) { 122 tryBackward(); 123 } 124 }); 125 126 this.backwardScrollTimer.setInitialDelay(this.delay); 127 } 128 129 /** 130 * Sets which component is to be scrolled. The scroll pane has two buttons - 131 * forward and backward, which are used for scrolling a component, and a 132 * component to be scrolled. 133 * 134 * @param component 135 * The component to be scrolled. 136 */ 137 public void setViewportView(final JComponent component) { 138 this.viewportView = component; 139 this.viewportViewSize = this.viewportView.getPreferredSize(); 140 141 this.viewport = new JPanel(null); 142 this.viewport.add(component); 143 this.viewport.validate(); 144 this.scrollPane.add(this.viewport, ScrollPaneLayout.CENTER); 145 146 this.viewportView.setLocation(0, 0); 147 this.viewportView.setSize(this.viewportViewSize); 148 149 this.scrollPane.revalidate(); 150 this.scrollPane.repaint(); 151 } 152 153 /** 154 * Change the step of scrolling. With one click over the backward or forward 155 * button the location of the component is changed. The step defines the 156 * displacement. 157 * 158 * @param newStep 159 * The new step of displacement. 160 */ 161 public void incrementStep(int newStep) { 162 assert newStep > 0; 163 164 this.step = newStep; 165 } 166 167 /** 168 * Changes the number of milliseconds between the action events of the 169 * timers. 170 * 171 * @param newDelay 172 * The new number of milliseconds. 173 */ 174 public void incrementTimerDelay(int newDelay) { 175 this.delay = newDelay; 176 } 177 178 private void tryBackward() { 179 int min = (-CustomScrollPane.this.translation < CustomScrollPane.this.step) ? 180 -CustomScrollPane.this.translation : CustomScrollPane.this.step; 181 CustomScrollPane.this.translation += min; 182 183 Point currLocation = CustomScrollPane.this.viewportView.getLocation(); 184 if (this.orientation) { 185 CustomScrollPane.this.viewportView.setLocation( 186 CustomScrollPane.this.translation, currLocation.y); 187 } else { 188 CustomScrollPane.this.viewportView.setLocation( 189 currLocation.x, CustomScrollPane.this.translation); 190 } 191 192 CustomScrollPane.this.swingComponent().validate(); 193 } 194 195 private void tryForward() { 196 //the whole component 197 int scrollPaneLength = this.orientation? 198 CustomScrollPane.this.scrollPane.getWidth() : 199 CustomScrollPane.this.scrollPane.getHeight(); 200 201 SophieLog.trace(scrollPaneLength); 202 203 //the size of the part where the scrollable component to be put 204 int minLength = this.orientation? 205 CustomScrollPane.this.scrollPane.getMinimumSize().width : 206 CustomScrollPane.this.scrollPane.getMinimumSize().height; 207 208 SophieLog.trace(minLength); 209 210 int viewportLength = - CustomScrollPane.this.translation + scrollPaneLength - minLength; 211 212 SophieLog.trace(viewportLength); 213 214 //the size of the scrollable component 215 int viewportViewLength = this.orientation? 216 this.viewportView.getWidth() : 217 this.viewportView.getHeight(); 218 219 SophieLog.trace(viewportLength); 220 SophieLog.trace(viewportViewLength); 221 222 int min = (viewportViewLength - viewportLength < CustomScrollPane.this.step) ? 223 viewportViewLength - viewportLength : CustomScrollPane.this.step; 224 CustomScrollPane.this.translation -= min; 225 226 Point currLocation = CustomScrollPane.this.viewportView.getLocation(); 227 if (this.orientation) { 228 CustomScrollPane.this.viewportView.setLocation( 229 CustomScrollPane.this.translation, currLocation.y); 230 } else { 231 CustomScrollPane.this.viewportView.setLocation( 232 currLocation.x, CustomScrollPane.this.translation); 233 } 234 235 CustomScrollPane.this.swingComponent().validate(); 236 } 237 238 private void setup() { 239 240 this.forwardArrow.addMouseListener(new MouseListener() { 241 242 @SuppressWarnings("synthetic-access") 243 public void mouseReleased(MouseEvent e) { 244 CustomScrollPane.this.forwardScrollTimer.stop(); 245 } 246 247 @SuppressWarnings("synthetic-access") 248 public void mousePressed(MouseEvent e) { 249 250 if (!SwingUtilities.isLeftMouseButton(e)) { 251 return; 252 } 253 254 CustomScrollPane.this.forwardScrollTimer.start(); 255 } 256 257 public void mouseExited(MouseEvent e) { 258 // nothing 259 } 260 261 public void mouseEntered(MouseEvent e) { 262 // nothing 263 } 264 265 public void mouseClicked(MouseEvent e) { 266 // nothing 267 } 268 }); 269 270 this.backwardArrow.addMouseListener(new MouseListener() { 271 272 @SuppressWarnings("synthetic-access") 273 public void mouseReleased(MouseEvent e) { 274 CustomScrollPane.this.backwardScrollTimer.stop(); 275 } 276 277 @SuppressWarnings("synthetic-access") 278 public void mousePressed(MouseEvent e) { 279 280 if (!SwingUtilities.isLeftMouseButton(e)) { 281 return; 282 } 283 CustomScrollPane.this.backwardScrollTimer.start(); 284 } 285 286 public void mouseExited(MouseEvent e) { 287 // nothing 288 } 289 290 public void mouseEntered(MouseEvent e) { 291 // nothing 292 } 293 294 public void mouseClicked(MouseEvent e) { 295 // nothing 296 } 297 }); 298 299 this.forwardArrow.addActionListener(new ActionListener() { 300 301 @SuppressWarnings("synthetic-access") 302 public void actionPerformed(ActionEvent e) { 303 tryForward(); 304 CustomScrollPane.this.scrollPane.validate(); 305 CustomScrollPane.this.scrollPane.repaint(); 306 } 307 }); 308 309 this.backwardArrow.addActionListener(new ActionListener() { 310 311 @SuppressWarnings("synthetic-access") 312 public void actionPerformed(ActionEvent e) { 313 tryBackward(); 314 CustomScrollPane.this.scrollPane.validate(); 315 CustomScrollPane.this.scrollPane.repaint(); 316 } 317 }); 318 } 319 320 /** 321 * Returns the swing component of this pane. 322 * @return 323 * The swing component of this object. 324 */ 325 public JComponent swingComponent() { 326 return this.scrollPane; 327 } 328 } -
src/test/java/org/sophie2/base/commons/gui/HorizontalJScrollPaneDemo.java
1 package org.sophie2.base.commons.gui; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.Dimension; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 import java.util.ArrayList; 9 10 import javax.swing.BoxLayout; 11 import javax.swing.JButton; 12 import javax.swing.JFrame; 13 import javax.swing.JPanel; 14 15 import org.sophie2.core.logging.SophieLog; 16 17 /** 18 * Demo for testing {@link CustomScrollPane} with {@link CustomScrollPane#HORIZONTAL} orientation. 19 * 20 * @author tanya 21 */ 22 public class HorizontalJScrollPaneDemo { 23 24 private JFrame frame; 25 26 private CustomScrollPane sophie; 27 28 private int index = 0; 29 30 private ArrayList<JButton> buttons; 31 32 private JPanel p; 33 34 /** 35 * Constructs an object - frame with {@link CustomScrollPane} 36 * with {@link CustomScrollPane#HORIZONTAL} orientation. 37 */ 38 public HorizontalJScrollPaneDemo() { 39 this.frame = new JFrame(); 40 this.frame.setVisible(true); 41 this.frame.setSize(new Dimension(500, 500)); 42 this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 43 44 45 this.sophie = new CustomScrollPane(CustomScrollPane.HORIZONTAL); 46 47 this.frame.getContentPane().setLayout(new BorderLayout()); 48 this.frame.getContentPane().add(this.sophie.swingComponent(), BorderLayout.CENTER); 49 JButton addButton = new JButton("add"); 50 addButton.addActionListener(new ActionListener() { 51 52 @SuppressWarnings("synthetic-access") 53 public void actionPerformed(ActionEvent e) { 54 if (HorizontalJScrollPaneDemo.this.index <= 20) { 55 HorizontalJScrollPaneDemo.this.index++; 56 HorizontalJScrollPaneDemo.this.p.add( 57 HorizontalJScrollPaneDemo.this.buttons.get(HorizontalJScrollPaneDemo.this.index)); 58 59 HorizontalJScrollPaneDemo.this.frame.getContentPane().validate(); 60 HorizontalJScrollPaneDemo.this.frame.getContentPane().repaint(); 61 } 62 } 63 }); 64 65 JButton removeButton = new JButton("remove"); 66 removeButton.addActionListener(new ActionListener() { 67 68 @SuppressWarnings("synthetic-access") 69 public void actionPerformed(ActionEvent e) { 70 if (HorizontalJScrollPaneDemo.this.index >= 0) { 71 HorizontalJScrollPaneDemo.this.p.remove( 72 HorizontalJScrollPaneDemo.this.buttons.get(HorizontalJScrollPaneDemo.this.index)); 73 HorizontalJScrollPaneDemo.this.index--; 74 75 HorizontalJScrollPaneDemo.this.frame.getContentPane().validate(); 76 HorizontalJScrollPaneDemo.this.frame.getContentPane().repaint(); 77 } 78 } 79 }); 80 81 JPanel buttonsPanel = new JPanel(new BorderLayout()); 82 buttonsPanel.add(addButton, BorderLayout.WEST); 83 buttonsPanel.add(removeButton, BorderLayout.EAST); 84 85 this.frame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH); 86 87 this.buttons = new ArrayList<JButton>(); 88 89 JButton but1 = new JButton("but1"); 90 but1.addActionListener(new ActionListener() { 91 92 public void actionPerformed(ActionEvent e) { 93 SophieLog.trace("but1"); 94 } 95 }); 96 97 JButton but2 = new JButton("but2"); 98 but2.addActionListener(new ActionListener() { 99 100 public void actionPerformed(ActionEvent e) { 101 SophieLog.trace("but2"); 102 } 103 }); 104 105 JButton but3 = new JButton("but3"); 106 but3.addActionListener(new ActionListener() { 107 108 public void actionPerformed(ActionEvent e) { 109 SophieLog.trace("but3"); 110 } 111 }); 112 113 JButton but4 = new JButton("but4"); 114 but4.addActionListener(new ActionListener() { 115 116 public void actionPerformed(ActionEvent e) { 117 SophieLog.trace("but4"); 118 } 119 }); 120 121 JButton but5 = new JButton("but5"); 122 but5.addActionListener(new ActionListener() { 123 124 public void actionPerformed(ActionEvent e) { 125 SophieLog.trace("but5"); 126 } 127 }); 128 129 JButton but6 = new JButton("but6"); 130 but6.addActionListener(new ActionListener() { 131 132 public void actionPerformed(ActionEvent e) { 133 SophieLog.trace("but6"); 134 } 135 }); 136 137 JButton but7 = new JButton("but7"); 138 but7.addActionListener(new ActionListener() { 139 140 public void actionPerformed(ActionEvent e) { 141 SophieLog.trace("but7"); 142 } 143 }); 144 145 JButton but8 = new JButton("but8"); 146 but8.addActionListener(new ActionListener() { 147 148 public void actionPerformed(ActionEvent e) { 149 SophieLog.trace("but8"); 150 } 151 }); 152 153 JButton but9 = new JButton("but9"); 154 but9.addActionListener(new ActionListener() { 155 156 public void actionPerformed(ActionEvent e) { 157 SophieLog.trace("but9"); 158 } 159 }); 160 161 JButton but10 = new JButton("but10"); 162 but10.addActionListener(new ActionListener() { 163 164 public void actionPerformed(ActionEvent e) { 165 SophieLog.trace("but10"); 166 } 167 }); 168 169 JButton but11 = new JButton("but11"); 170 but11.addActionListener(new ActionListener() { 171 172 public void actionPerformed(ActionEvent e) { 173 SophieLog.trace("but11"); 174 } 175 }); 176 177 JButton but12 = new JButton("but12"); 178 but12.addActionListener(new ActionListener() { 179 180 public void actionPerformed(ActionEvent e) { 181 SophieLog.trace("but12"); 182 } 183 }); 184 185 JButton but13 = new JButton("but13"); 186 but13.addActionListener(new ActionListener() { 187 188 public void actionPerformed(ActionEvent e) { 189 SophieLog.trace("but13"); 190 } 191 }); 192 193 JButton but14 = new JButton("but14"); 194 but14.addActionListener(new ActionListener() { 195 196 public void actionPerformed(ActionEvent e) { 197 SophieLog.trace("but14"); 198 } 199 }); 200 201 JButton but15 = new JButton("but15"); 202 but15.addActionListener(new ActionListener() { 203 204 public void actionPerformed(ActionEvent e) { 205 SophieLog.trace("but15"); 206 } 207 }); 208 209 JButton but16 = new JButton("but16"); 210 but16.addActionListener(new ActionListener() { 211 212 public void actionPerformed(ActionEvent e) { 213 SophieLog.trace("but16"); 214 } 215 }); 216 217 JButton but17 = new JButton("but17"); 218 but17.addActionListener(new ActionListener() { 219 220 public void actionPerformed(ActionEvent e) { 221 SophieLog.trace("but17"); 222 } 223 }); 224 225 JButton but18 = new JButton("but18"); 226 but18.addActionListener(new ActionListener() { 227 228 public void actionPerformed(ActionEvent e) { 229 SophieLog.trace("but18"); 230 } 231 }); 232 233 JButton but19 = new JButton("but19"); 234 but19.addActionListener(new ActionListener() { 235 236 public void actionPerformed(ActionEvent e) { 237 SophieLog.trace("but19"); 238 } 239 }); 240 241 JButton but20 = new JButton("but20"); 242 but20.addActionListener(new ActionListener() { 243 244 public void actionPerformed(ActionEvent e) { 245 SophieLog.trace("but20"); 246 } 247 }); 248 249 this.buttons.add(but1); 250 this.buttons.add(but2); 251 this.buttons.add(but3); 252 this.buttons.add(but4); 253 this.buttons.add(but5); 254 this.buttons.add(but6); 255 this.buttons.add(but7); 256 this.buttons.add(but8); 257 this.buttons.add(but9); 258 this.buttons.add(but10); 259 this.buttons.add(but11); 260 this.buttons.add(but12); 261 this.buttons.add(but13); 262 this.buttons.add(but14); 263 this.buttons.add(but15); 264 this.buttons.add(but16); 265 this.buttons.add(but17); 266 this.buttons.add(but18); 267 this.buttons.add(but19); 268 this.buttons.add(but20); 269 270 this.p = new JPanel(); 271 this.p.setLayout(new BoxLayout(this.p, BoxLayout.X_AXIS)); 272 273 this.p.add(this.buttons.get(0)); 274 this.p.add(this.buttons.get(1)); 275 this.p.add(this.buttons.get(2)); 276 this.p.add(this.buttons.get(3)); 277 this.p.add(this.buttons.get(4)); 278 this.p.add(this.buttons.get(5)); 279 this.p.add(this.buttons.get(6)); 280 this.p.add(this.buttons.get(7)); 281 this.index = 7; 282 this.p.validate(); 283 this.sophie.setViewportView(this.p); 284 this.p.validate(); 285 this.sophie.swingComponent().setBackground(Color.WHITE); 286 } 287 288 /** 289 * The main method! 290 * @param args 291 */ 292 public static void main(String args[]) { 293 new HorizontalJScrollPaneDemo(); 294 } 295 } -
src/main/java/org/sophie2/base/commons/gui/ScrollPaneLayout.java
1 package org.sophie2.base.commons.gui; 2 3 import java.awt.Component; 4 import java.awt.Container; 5 import java.awt.Dimension; 6 import java.awt.LayoutManager2; 7 import java.awt.Point; 8 9 import javax.swing.JPanel; 10 11 /** 12 * Layout Manager that is created for the need of {@link CustomScrollPane}. 13 * 14 * @author tanya 15 */ 16 class ScrollPaneLayout implements LayoutManager2, java.io.Serializable { 17 18 /** 19 * Used for serialization. 20 */ 21 private static final long serialVersionUID = 810513263396699528L; 22 23 /** 24 * Used for constraint for adding a component at the center. This component 25 * is the one that is going to be scrollable. 26 */ 27 public static final String CENTER = "Center"; 28 29 /** 30 * Used for constraint for adding a component that is going to be visible all the time. 31 */ 32 public static final String FORWARD = "Forward"; 33 34 /** 35 * Used for constraint for adding a component that is going to be visible all the time. 36 */ 37 public static final String BACKWARD = "Backward"; 38 39 /** 40 * Created new layout manager with specific orientation. Orientation can be 41 * horizontal and vertical. 42 * 43 * @param orientation 44 * <b>true</b> for horizontal orientation and <b>false</b> 45 * for vertical orientation. 46 */ 47 public ScrollPaneLayout(boolean orientation) { 48 this.orientation = orientation; 49 } 50 51 private Component backward; 52 private Component forward; 53 private Component scrollable; 54 private JPanel center; 55 56 /** 57 * if <b>true</b> then HORIZONTAL, if <b>false</b> then VERTICAL. 58 */ 59 private boolean orientation; 60 61 62 public void addLayoutComponent(Component comp, Object constraints) { 63 if (constraints == BACKWARD) { 64 this.backward = comp; 65 } 66 67 if (constraints == FORWARD) { 68 this.forward = comp; 69 } 70 71 if (constraints == CENTER) { 72 this.center = (JPanel)comp; 73 this.scrollable = this.center.getComponent(0); 74 } 75 } 76 77 public float getLayoutAlignmentX(Container target) { 78 return 0.0f; 79 } 80 81 public float getLayoutAlignmentY(Container target) { 82 return 0.0f; 83 } 84 85 public void invalidateLayout(Container target) { 86 // nothing 87 } 88 89 public Dimension maximumLayoutSize(Container target) { 90 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); 91 } 92 93 public void addLayoutComponent(String name, Component comp) { 94 // nothing 95 } 96 97 private int xCoordinate = 0; 98 private int yCoordinate = 0; 99 100 //The preferred size includes the size of the buttons. 101 //The preferred size excludes the size of the scrollable part. 102 private Dimension minimumDimension = new Dimension(0, 0); 103 104 public void layoutContainer(Container parent) { 105 this.xCoordinate = 0; 106 this.yCoordinate = 0; 107 108 this.minimumDimension = new Dimension(0, 0); 109 110 Dimension tmp; 111 112 if (this.scrollable != null) { 113 Dimension scrollableDim = this.scrollable.getPreferredSize(); 114 Dimension parentDim = parent.getSize(); 115 116 //whether or not the forward and backward buttons should be painted 117 boolean paintButtons = false; 118 119 if (this.orientation) { 120 if (scrollableDim.width > parentDim.width) { 121 paintButtons = true; 122 } 123 } else { 124 if (scrollableDim.height > parentDim.height) { 125 paintButtons = true; 126 } 127 } 128 129 if (paintButtons) { 130 //paint the backward button 131 if (this.backward != null) { 132 this.backward.setLocation(this.xCoordinate, 133 this.yCoordinate); 134 135 if (this.orientation) { 136 tmp = new Dimension(this.backward.getPreferredSize().width, 137 scrollableDim.height); 138 } else { 139 tmp = new Dimension(scrollableDim.width, 140 this.backward.getPreferredSize().height); 141 } 142 this.backward.setSize(tmp); 143 144 145 if (this.orientation) { 146 this.xCoordinate += tmp.width; 147 } else { 148 this.yCoordinate += tmp.height; 149 } 150 151 this.minimumDimension = new Dimension(tmp.width, 152 tmp.height); 153 } 154 155 int maxWidth = 0; 156 int maxHeight = 0; 157 158 //paint the forward button 159 if (this.forward != null) { 160 161 if (this.orientation) { 162 tmp = new Dimension(this.forward.getPreferredSize().width, 163 scrollableDim.height); 164 } else { 165 tmp = new Dimension(scrollableDim.width, 166 this.forward.getPreferredSize().height); 167 } 168 this.forward.setSize(tmp); 169 170 if (this.orientation) { 171 this.forward.setLocation(parentDim.width - tmp.width, 172 this.yCoordinate); 173 174 maxHeight = (this.minimumDimension.height >= tmp.height)? 175 this.minimumDimension.height : tmp.height; 176 177 this.minimumDimension = new Dimension( 178 this.minimumDimension.width + tmp.width, 179 maxHeight); 180 } else { 181 this.forward.setLocation(this.xCoordinate, 182 parentDim.height - tmp.height); 183 184 maxWidth = (this.minimumDimension.width >= tmp.width)? 185 this.minimumDimension.width : tmp.width; 186 187 this.minimumDimension = new Dimension(maxWidth, 188 this.minimumDimension.height + tmp.height); 189 } 190 } 191 192 this.center.setLocation(this.xCoordinate, this.yCoordinate); 193 194 tmp = scrollableDim; 195 Dimension centerDim; 196 if (this.orientation) { 197 centerDim = new Dimension(parent.getSize().width 198 - this.minimumDimension.width, 199 tmp.getSize().height); 200 } else { 201 centerDim = new Dimension(tmp.getSize().width, 202 parent.getSize().height - this.minimumDimension.height); 203 } 204 205 this.center.setSize(centerDim); 206 this.scrollable.setSize(tmp); 207 208 if (this.orientation) { 209 if (this.scrollable.getPreferredSize().width <= 210 -this.scrollable.getX() + centerDim.width) { 211 212 this.scrollable.setLocation( 213 centerDim.width - scrollableDim.width, 214 this.scrollable.getY()); 215 216 this.forward.setEnabled(false); 217 } else { 218 this.forward.setEnabled(true); 219 } 220 } else { 221 if (this.scrollable.getPreferredSize().height <= 222 -this.scrollable.getY() + centerDim.height) { 223 224 this.scrollable.setLocation(this.scrollable.getX(), 225 centerDim.height - scrollableDim.height); 226 this.forward.setEnabled(false); 227 } else { 228 this.forward.setEnabled(true); 229 } 230 } 231 232 if (this.scrollable.getLocation().equals(new Point(0, 0))) { 233 this.backward.setEnabled(false); 234 } else { 235 this.backward.setEnabled(true); 236 } 237 } else { 238 239 this.backward.setSize(0, 0); 240 this.forward.setSize(0, 0); 241 242 this.center.setLocation(0, 0); 243 this.scrollable.setLocation(0, 0); 244 245 tmp = scrollableDim; 246 this.center.setSize(tmp); 247 this.scrollable.setSize(tmp); 248 } 249 } 250 } 251 252 public Dimension minimumLayoutSize(Container parent) { 253 return this.minimumDimension; 254 } 255 256 public Dimension preferredLayoutSize(Container parent) { 257 258 int width = 0; 259 int height = 0; 260 261 if (this.orientation) { 262 width = this.minimumDimension.width + this.scrollable.getPreferredSize().width; 263 height = (this.minimumDimension.height > this.scrollable.getPreferredSize().height)? 264 this.minimumDimension.height : this.scrollable.getPreferredSize().height; 265 } else { 266 height = this.minimumDimension.height + this.scrollable.getPreferredSize().height; 267 width = (this.minimumDimension.width > this.scrollable.getPreferredSize().width)? 268 this.minimumDimension.width : this.scrollable.getPreferredSize().width; 269 } 270 271 return new Dimension(width, height); 272 } 273 274 public void removeLayoutComponent(Component comp) { 275 // nothing 276 } 277 } 278 No newline at end of file