Ticket #2372: panelScroller.patch
File panelScroller.patch, 30.2 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 private JButton backwardArrow; 41 private JButton forwardArrow; 42 43 private JComponent viewportView; 44 45 private JPanel scrollPane; 46 47 private Dimension viewportViewSize; 48 private JPanel viewport; 49 50 private int step = 5; 51 private int delay = 100; 52 private int translation = 0; 53 private boolean orientation; 54 55 private Timer forwardScrollTimer; 56 private Timer backwardScrollTimer; 57 58 /** 59 * Constructs a scroll pane with given orientation. The values of the 60 * orientation can be {@link #HORIZONTAL} or {@link #VERTICAL}. If the value 61 * of orientation is <b>true</b> then the components will be laid out from 62 * left to right. Otherwise, if it is <b>false</b>, then the components will 63 * be laid out from top to bottom. 64 * 65 * @param orientation 66 * <b>true</b> if components should be laid out from left to 67 * right, <b>false</b> if components should be laid out from top 68 * to bottom. 69 * 70 */ 71 public CustomScrollPane(boolean orientation) { 72 this.orientation = orientation; 73 74 this.backwardArrow = new JButton("backward"); 75 this.forwardArrow = new JButton("forward"); 76 77 this.scrollPane = new JPanel(new ScrollPaneLayout(this.orientation)); 78 this.scrollPane.add(this.backwardArrow, ScrollPaneLayout.BACKWARD); 79 this.scrollPane.add(this.forwardArrow, ScrollPaneLayout.FORWARD); 80 81 setupTimers(); 82 setup(); 83 } 84 85 private void setupTimers() { 86 this.forwardScrollTimer = new Timer(this.delay, new ActionListener() { 87 88 @SuppressWarnings("synthetic-access") 89 public void actionPerformed(ActionEvent e) { 90 tryForward(); 91 } 92 }); 93 94 this.forwardScrollTimer.setInitialDelay(this.delay); 95 96 97 this.backwardScrollTimer = new Timer(this.delay, new ActionListener() { 98 99 @SuppressWarnings("synthetic-access") 100 public void actionPerformed(ActionEvent e) { 101 tryBackward(); 102 } 103 }); 104 105 this.backwardScrollTimer.setInitialDelay(this.delay); 106 } 107 108 /** 109 * Sets which component is to be scrolled. The scroll pane has two buttons - 110 * forward and backward, which are used for scrolling a component, and a 111 * component to be scrolled. 112 * 113 * @param component 114 * The component to be scrolled. 115 */ 116 public void setViewportView(final JComponent component) { 117 this.viewportView = component; 118 this.viewportViewSize = this.viewportView.getPreferredSize(); 119 120 this.viewport = new JPanel(null); 121 this.viewport.add(component); 122 this.viewport.validate(); 123 this.scrollPane.add(this.viewport, ScrollPaneLayout.CENTER); 124 125 this.viewportView.setLocation(0, 0); 126 this.viewportView.setSize(this.viewportViewSize); 127 128 this.scrollPane.revalidate(); 129 this.scrollPane.repaint(); 130 } 131 132 /** 133 * Change the step of scrolling. With one click over the backward or forward 134 * button the location of the component is changed. The step defines the 135 * displacement. 136 * 137 * @param newStep 138 * The new step of displacement. 139 */ 140 public void incrementStep(int newStep) { 141 assert newStep > 0; 142 143 this.step = newStep; 144 } 145 146 /** 147 * Changes the number of milliseconds between the action events of the 148 * timers. 149 * 150 * @param newDelay 151 * The new number of milliseconds. 152 */ 153 public void incrementTimerDelay(int newDelay) { 154 this.delay = newDelay; 155 } 156 157 private void tryBackward() { 158 int min = (-CustomScrollPane.this.translation < CustomScrollPane.this.step) ? 159 -CustomScrollPane.this.translation : CustomScrollPane.this.step; 160 CustomScrollPane.this.translation += min; 161 162 Point currLocation = CustomScrollPane.this.viewportView.getLocation(); 163 if (this.orientation) { 164 CustomScrollPane.this.viewportView.setLocation( 165 CustomScrollPane.this.translation, currLocation.y); 166 } else { 167 CustomScrollPane.this.viewportView.setLocation( 168 currLocation.x, CustomScrollPane.this.translation); 169 } 170 171 CustomScrollPane.this.swingComponent().validate(); 172 } 173 174 private void tryForward() { 175 //the whole component 176 int scrollPaneLength = this.orientation? 177 CustomScrollPane.this.scrollPane.getWidth() : 178 CustomScrollPane.this.scrollPane.getHeight(); 179 180 SophieLog.trace(scrollPaneLength); 181 182 //the size of the part where the scrollable component to be put 183 int minLength = this.orientation? 184 CustomScrollPane.this.scrollPane.getMinimumSize().width : 185 CustomScrollPane.this.scrollPane.getMinimumSize().height; 186 187 SophieLog.trace(minLength); 188 189 int viewportLength = - CustomScrollPane.this.translation + scrollPaneLength - minLength; 190 191 SophieLog.trace(viewportLength); 192 193 //the size of the scrollable component 194 int viewportViewLength = this.orientation? 195 this.viewportView.getWidth() : 196 this.viewportView.getHeight(); 197 198 SophieLog.trace(viewportLength); 199 SophieLog.trace(viewportViewLength); 200 201 int min = (viewportViewLength - viewportLength < CustomScrollPane.this.step) ? 202 viewportViewLength - viewportLength : CustomScrollPane.this.step; 203 CustomScrollPane.this.translation -= min; 204 205 Point currLocation = CustomScrollPane.this.viewportView.getLocation(); 206 if (this.orientation) { 207 CustomScrollPane.this.viewportView.setLocation( 208 CustomScrollPane.this.translation, currLocation.y); 209 } else { 210 CustomScrollPane.this.viewportView.setLocation( 211 currLocation.x, CustomScrollPane.this.translation); 212 } 213 214 CustomScrollPane.this.swingComponent().validate(); 215 } 216 217 private void setup() { 218 219 this.forwardArrow.addMouseListener(new MouseListener() { 220 221 @SuppressWarnings("synthetic-access") 222 public void mouseReleased(MouseEvent e) { 223 CustomScrollPane.this.forwardScrollTimer.stop(); 224 } 225 226 @SuppressWarnings("synthetic-access") 227 public void mousePressed(MouseEvent e) { 228 229 if (!SwingUtilities.isLeftMouseButton(e)) { 230 return; 231 } 232 233 CustomScrollPane.this.forwardScrollTimer.start(); 234 } 235 236 public void mouseExited(MouseEvent e) { 237 // nothing 238 } 239 240 public void mouseEntered(MouseEvent e) { 241 // nothing 242 } 243 244 public void mouseClicked(MouseEvent e) { 245 // nothing 246 } 247 }); 248 249 this.backwardArrow.addMouseListener(new MouseListener() { 250 251 @SuppressWarnings("synthetic-access") 252 public void mouseReleased(MouseEvent e) { 253 CustomScrollPane.this.backwardScrollTimer.stop(); 254 } 255 256 @SuppressWarnings("synthetic-access") 257 public void mousePressed(MouseEvent e) { 258 259 if (!SwingUtilities.isLeftMouseButton(e)) { 260 return; 261 } 262 CustomScrollPane.this.backwardScrollTimer.start(); 263 } 264 265 public void mouseExited(MouseEvent e) { 266 // nothing 267 } 268 269 public void mouseEntered(MouseEvent e) { 270 // nothing 271 } 272 273 public void mouseClicked(MouseEvent e) { 274 // nothing 275 } 276 }); 277 278 this.forwardArrow.addActionListener(new ActionListener() { 279 280 @SuppressWarnings("synthetic-access") 281 public void actionPerformed(ActionEvent e) { 282 tryForward(); 283 CustomScrollPane.this.scrollPane.validate(); 284 CustomScrollPane.this.scrollPane.repaint(); 285 } 286 }); 287 288 this.backwardArrow.addActionListener(new ActionListener() { 289 290 @SuppressWarnings("synthetic-access") 291 public void actionPerformed(ActionEvent e) { 292 tryBackward(); 293 CustomScrollPane.this.scrollPane.validate(); 294 CustomScrollPane.this.scrollPane.repaint(); 295 } 296 }); 297 } 298 299 /** 300 * Returns the swing component of this pane. 301 * @return 302 * The swing component of this object. 303 */ 304 public JComponent swingComponent() { 305 return this.scrollPane; 306 } 307 } -
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 tmp = this.backward.getPreferredSize(); 136 this.backward.setSize(tmp); 137 138 139 if (this.orientation) { 140 this.xCoordinate += tmp.width; 141 } else { 142 this.yCoordinate += tmp.height; 143 } 144 145 this.minimumDimension = new Dimension(tmp.width, 146 tmp.height); 147 } 148 149 int maxWidth = 0; 150 int maxHeight = 0; 151 152 //paint the forward button 153 if (this.forward != null) { 154 155 tmp = this.forward.getPreferredSize(); 156 this.forward.setSize(tmp); 157 158 if (this.orientation) { 159 this.forward.setLocation(parentDim.width - tmp.width, 160 this.yCoordinate); 161 162 maxHeight = (this.minimumDimension.height >= tmp.height)? 163 this.minimumDimension.height : tmp.height; 164 165 this.minimumDimension = new Dimension( 166 this.minimumDimension.width + tmp.width, 167 maxHeight); 168 } else { 169 this.forward.setLocation(this.xCoordinate, 170 parentDim.height - tmp.height); 171 172 maxWidth = (this.minimumDimension.width >= tmp.width)? 173 this.minimumDimension.width : tmp.width; 174 175 this.minimumDimension = new Dimension(maxWidth, 176 this.minimumDimension.height + tmp.height); 177 } 178 } 179 180 this.center.setLocation(this.xCoordinate, this.yCoordinate); 181 182 tmp = scrollableDim; 183 Dimension centerDim; 184 if (this.orientation) { 185 centerDim = new Dimension(parent.getSize().width 186 - this.minimumDimension.width, 187 tmp.getSize().height); 188 } else { 189 centerDim = new Dimension(tmp.getSize().width, 190 parent.getSize().height - this.minimumDimension.height); 191 } 192 193 this.center.setSize(centerDim); 194 this.scrollable.setSize(tmp); 195 196 if (this.orientation) { 197 if (this.scrollable.getPreferredSize().width <= 198 -this.scrollable.getX() + centerDim.width) { 199 200 this.scrollable.setLocation( 201 centerDim.width - scrollableDim.width, 202 this.scrollable.getY()); 203 204 this.forward.setEnabled(false); 205 } else { 206 this.forward.setEnabled(true); 207 } 208 } else { 209 if (this.scrollable.getPreferredSize().height <= 210 -this.scrollable.getY() + centerDim.height) { 211 212 this.scrollable.setLocation(this.scrollable.getX(), 213 centerDim.height - scrollableDim.height); 214 this.forward.setEnabled(false); 215 } else { 216 this.forward.setEnabled(true); 217 } 218 } 219 220 if (this.scrollable.getLocation().equals(new Point(0, 0))) { 221 this.backward.setEnabled(false); 222 } else { 223 this.backward.setEnabled(true); 224 } 225 } else { 226 227 this.backward.setSize(0, 0); 228 this.forward.setSize(0, 0); 229 230 this.center.setLocation(0, 0); 231 this.scrollable.setLocation(0, 0); 232 233 tmp = scrollableDim; 234 this.center.setSize(tmp); 235 this.scrollable.setSize(tmp); 236 } 237 } 238 } 239 240 public Dimension minimumLayoutSize(Container parent) { 241 return this.minimumDimension; 242 } 243 244 public Dimension preferredLayoutSize(Container parent) { 245 246 int width = 0; 247 int height = 0; 248 249 if (this.orientation) { 250 width = this.minimumDimension.width + this.scrollable.getPreferredSize().width; 251 height = (this.minimumDimension.height > this.scrollable.getPreferredSize().height)? 252 this.minimumDimension.height : this.scrollable.getPreferredSize().height; 253 } else { 254 height = this.minimumDimension.height + this.scrollable.getPreferredSize().height; 255 width = (this.minimumDimension.width > this.scrollable.getPreferredSize().width)? 256 this.minimumDimension.width : this.scrollable.getPreferredSize().width; 257 } 258 259 return new Dimension(width, height); 260 } 261 262 public void removeLayoutComponent(Component comp) { 263 // nothing 264 } 265 } 266 No newline at end of file