Ticket #2372: scrollable.patch

File scrollable.patch, 14.4 KB (added by tanya, 15 years ago)
  • src/test/java/org/sophie2/main/layout/mydoggy/CustomScrollPane.java

    ### Eclipse Workspace Patch 1.0
    #P org.sophie2.main.layout.mydoggy
     
     1package org.sophie2.main.layout.mydoggy; 
     2 
     3import java.awt.Dimension; 
     4import java.awt.Point; 
     5import java.awt.event.ActionEvent; 
     6import java.awt.event.ActionListener; 
     7 
     8import javax.swing.JButton; 
     9import javax.swing.JComponent; 
     10import javax.swing.JPanel; 
     11 
     12public class CustomScrollPane { 
     13         
     14        public static final boolean HORIZONTAL = Boolean.TRUE; 
     15        public static final boolean VERTICAL = Boolean.FALSE; 
     16         
     17        private JButton backwardArrow; 
     18        private JButton forwardArrow; 
     19        private JComponent viewportView; 
     20 
     21        private JPanel scrollPane; 
     22         
     23        private Dimension viewportViewSize; 
     24        private JPanel viewport; 
     25 
     26        private int step = 10; 
     27        private int translation = 0; 
     28        private boolean orientation; 
     29         
     30        public CustomScrollPane(boolean orientation) { 
     31                this.orientation = orientation; 
     32 
     33                this.backwardArrow = new JButton("backward"); 
     34                this.forwardArrow = new JButton("forward"); 
     35 
     36                this.scrollPane = new JPanel(new ScrollPaneLayout(this.orientation)); 
     37                this.scrollPane.add(this.backwardArrow, ScrollPaneLayout.BACKWARD); 
     38                this.scrollPane.add(this.forwardArrow, ScrollPaneLayout.FORWARD); 
     39 
     40                setup(); 
     41        } 
     42 
     43         
     44         
     45        public void setViewportView(final JComponent component) { 
     46                this.viewportView = component; 
     47                this.viewportViewSize = this.viewportView.getPreferredSize(); 
     48                 
     49                this.viewport = new JPanel(null); 
     50                this.viewport.add(component); 
     51                this.viewport.validate(); 
     52                this.scrollPane.add(this.viewport, ScrollPaneLayout.CUSTOM); 
     53                 
     54                this.viewportView.setLocation(0, 0); 
     55                this.viewportView.setSize(this.viewportViewSize); 
     56                 
     57//              this.scrollable.validate(); 
     58//              this.scrollable.repaint(); 
     59//              this.helper.validate(); 
     60//              this.helper.repaint(); 
     61                this.scrollPane.revalidate(); 
     62                this.scrollPane.repaint(); 
     63        } 
     64         
     65        public void incrementStep(int newStep) { 
     66                assert newStep > 0; 
     67 
     68                this.step = newStep; 
     69        } 
     70         
     71        private void tryBackward() { 
     72                if (CustomScrollPane.this.translation < 0) { 
     73                        int min = (-CustomScrollPane.this.translation < CustomScrollPane.this.step) ? 
     74                                        -CustomScrollPane.this.translation : CustomScrollPane.this.step; 
     75                        CustomScrollPane.this.translation += min; 
     76                } 
     77                 
     78                Point currLocation = CustomScrollPane.this.viewportView.getLocation(); 
     79                if (this.orientation) { 
     80                        CustomScrollPane.this.viewportView.setLocation(CustomScrollPane.this.translation, currLocation.y); 
     81                } else { 
     82                        CustomScrollPane.this.viewportView.setLocation(currLocation.x, CustomScrollPane.this.translation); 
     83                } 
     84        } 
     85         
     86        private void tryForward() { 
     87                //the whole component 
     88                int scrollPaneLength = this.orientation? CustomScrollPane.this.scrollPane.getWidth() : 
     89                        CustomScrollPane.this.scrollPane.getHeight(); 
     90                //the size of the part where to be put 
     91                int minLength = this.orientation? CustomScrollPane.this.scrollPane.getMinimumSize().width : 
     92                        CustomScrollPane.this.scrollPane.getMinimumSize().height; 
     93                int viewportLength = - CustomScrollPane.this.translation + scrollPaneLength  
     94                        - minLength; 
     95                 
     96                //the size of the component to be scrolled 
     97                int viewportViewLength = this.orientation? this.viewportView.getWidth() : this.viewportView.getHeight();  
     98                 
     99                if (viewportLength < viewportViewLength) { 
     100                        int min = (viewportViewLength - viewportLength < CustomScrollPane.this.step)?  
     101                                        viewportViewLength - viewportLength : CustomScrollPane.this.step; 
     102                        CustomScrollPane.this.translation -= min; 
     103                } 
     104                 
     105                Point currLocation = CustomScrollPane.this.viewportView.getLocation(); 
     106                if (this.orientation) { 
     107                        CustomScrollPane.this.viewportView.setLocation(CustomScrollPane.this.translation, currLocation.y); 
     108                } else { 
     109                        CustomScrollPane.this.viewportView.setLocation(currLocation.x, CustomScrollPane.this.translation); 
     110                } 
     111        } 
     112         
     113        private void setup() { 
     114                this.backwardArrow.addActionListener(new ActionListener() { 
     115                         
     116                        @SuppressWarnings("synthetic-access") 
     117                        public void actionPerformed(ActionEvent e) { 
     118                                tryBackward(); 
     119                                CustomScrollPane.this.scrollPane.validate(); 
     120                                CustomScrollPane.this.scrollPane.repaint(); 
     121                        } 
     122                }); 
     123                 
     124                this.forwardArrow.addActionListener(new ActionListener() { 
     125                         
     126                        @SuppressWarnings("synthetic-access") 
     127                        public void actionPerformed(ActionEvent e) { 
     128                                tryForward(); 
     129                                CustomScrollPane.this.scrollPane.validate(); 
     130                                CustomScrollPane.this.scrollPane.repaint(); 
     131                        } 
     132                }); 
     133        } 
     134         
     135        public JComponent swingComponent() { 
     136                return this.scrollPane; 
     137        } 
     138} 
  • src/test/java/org/sophie2/main/layout/mydoggy/ScrollPaneLayout.java

     
     1package org.sophie2.main.layout.mydoggy; 
     2 
     3import java.awt.BorderLayout; 
     4import java.awt.Component; 
     5import java.awt.Container; 
     6import java.awt.Dimension; 
     7import java.awt.LayoutManager2; 
     8import java.awt.Point; 
     9 
     10import javax.swing.JDesktopPane; 
     11import javax.swing.JLayeredPane; 
     12import javax.swing.JPanel; 
     13import javax.swing.plaf.DimensionUIResource; 
     14 
     15import org.sophie2.core.logging.SophieLog; 
     16 
     17/** 
     18 * @author tanya 
     19 */ 
     20class ScrollPaneLayout implements LayoutManager2, java.io.Serializable { 
     21 
     22        /** 
     23         * Used for serialization. 
     24         */ 
     25        private static final long serialVersionUID = 810513263396699528L; 
     26 
     27        public static final String CUSTOM = "Custom"; 
     28        public static final String FORWARD = "Forward"; 
     29        public static final String BACKWARD = "Backward"; 
     30 
     31        private boolean orientation;  
     32         
     33        public ScrollPaneLayout(boolean orientation) { 
     34                this.orientation = orientation; 
     35        } 
     36 
     37        private Component backward; 
     38        private Component forward; 
     39        private Component scrollable; 
     40        private Component center; 
     41         
     42        public void addLayoutComponent(Component comp, Object constraints) { 
     43                if (constraints == BACKWARD) { 
     44                        this.backward = comp; 
     45                } 
     46                 
     47                if (constraints == FORWARD) { 
     48                        this.forward = comp; 
     49                } 
     50                 
     51                if (constraints == CUSTOM) { 
     52                        this.scrollable = ((JPanel)comp).getComponent(0); 
     53                        this.center = comp; 
     54                } 
     55        } 
     56 
     57        public float getLayoutAlignmentX(Container target) { 
     58                return 0.0f;//this.layoutManager.getLayoutAlignmentX(target); 
     59        } 
     60 
     61        public float getLayoutAlignmentY(Container target) { 
     62                return 0.0f;//this.layoutManager.getLayoutAlignmentY(target); 
     63        } 
     64 
     65        public void invalidateLayout(Container target) { 
     66                //this.layoutManager.invalidateLayout(target); 
     67        } 
     68 
     69        public Dimension maximumLayoutSize(Container target) { 
     70                return new Dimension(0, 0);// this.layoutManager.maximumLayoutSize(target); 
     71        } 
     72 
     73        public void addLayoutComponent(String name, Component comp) { 
     74                System.out.println("xxxx"); 
     75//              this.layoutManager.addLayoutComponent(name, comp); 
     76        } 
     77 
     78        int x = 0; 
     79        int y = 0; 
     80        Dimension dim; 
     81         
     82        public void layoutContainer(Container parent) { 
     83                x = 0; 
     84                y = 0; 
     85                 
     86                dim = new Dimension(0, 0); 
     87                 
     88                Dimension d; 
     89                 
     90                 
     91                if (this.backward != null) { 
     92                        this.backward.setLocation(0,0); 
     93                        d = this.backward.getPreferredSize(); 
     94                        this.backward.setSize(d); 
     95                        y += d.height; 
     96                         
     97                        dim = new Dimension(d.width, d.height); 
     98                } 
     99                 
     100                if (this.forward != null) { 
     101                        d = this.forward.getPreferredSize(); 
     102                        this.forward.setSize(d); 
     103                         
     104                        Dimension ddd = parent.getSize(); 
     105                        if (this.orientation) { 
     106                                this.forward.setLocation(ddd.width - d.width, 0); 
     107                                dim = new Dimension(dim.width + this.forward.getSize().width, 0); 
     108                        } else { 
     109                                this.forward.setLocation(0, ddd.height - d.height); 
     110                                dim = new Dimension(0, dim.height + this.forward.getSize().height); 
     111                        } 
     112                         
     113                } 
     114 
     115                if (this.scrollable != null) { 
     116                        this.scrollable.validate(); 
     117                        if (this.orientation) { 
     118                                this.center.setLocation(x, 0); 
     119                        } else { 
     120                                this.center.setLocation(0, y); 
     121                        } 
     122                         
     123                        d = this.scrollable.getPreferredSize(); 
     124                        Dimension sss = new Dimension(d.getSize().width, parent.getSize().height - dim.height);  
     125                        this.center.setSize(sss); 
     126                        this.scrollable.setSize(d); 
     127                } 
     128        } 
     129 
     130        public Dimension minimumLayoutSize(Container parent) { 
     131                Dimension parentDim = parent.getSize(); 
     132                Dimension centerDim = this.center.getSize(); 
     133 
     134                return new Dimension(parentDim.width - centerDim.width, 
     135                                parentDim.height - centerDim.height);//this.layoutManager.minimumLayoutSize(parent); 
     136        } 
     137 
     138        public Dimension preferredLayoutSize(Container parent) { 
     139                return new Dimension(0, 0);//this.layoutManager.preferredLayoutSize(parent); 
     140        } 
     141 
     142        public void removeLayoutComponent(Component comp) { 
     143//              this.layoutManager.removeLayoutComponent(comp); 
     144        } 
     145} 
     146 No newline at end of file 
  • src/test/java/org/sophie2/main/layout/mydoggy/JScrollPaneDemo.java

     
     1package org.sophie2.main.layout.mydoggy; 
     2 
     3import java.awt.BorderLayout; 
     4import java.awt.Color; 
     5import java.awt.Container; 
     6import java.awt.Dimension; 
     7import java.awt.FlowLayout; 
     8import java.awt.event.ActionEvent; 
     9import java.awt.event.ActionListener; 
     10import java.beans.PropertyChangeEvent; 
     11import java.beans.PropertyChangeListener; 
     12 
     13import javax.swing.BoxLayout; 
     14import javax.swing.JButton; 
     15import javax.swing.JComponent; 
     16import javax.swing.JFrame; 
     17import javax.swing.JPanel; 
     18import javax.swing.JScrollBar; 
     19import javax.swing.JScrollPane; 
     20 
     21import org.sophie2.core.logging.SophieLog; 
     22 
     23@SuppressWarnings("all") 
     24public class JScrollPaneDemo { 
     25         
     26        private JFrame frame; 
     27        private JPanel panel; 
     28         
     29        public JScrollPaneDemo() { 
     30                frame = new JFrame(); 
     31                frame.setVisible(true); 
     32                frame.setSize(new Dimension(500, 500)); 
     33                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     34                 
     35                 
     36                CustomScrollPane sophie = new CustomScrollPane(CustomScrollPane.VERTICAL); 
     37                 
     38                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.warn("but1"); 
     45                        } 
     46                }); 
     47                 
     48                JButton but2 = new JButton("but2"); 
     49                but2.addActionListener(new ActionListener() { 
     50                         
     51                        public void actionPerformed(ActionEvent e) { 
     52                                SophieLog.warn("but2"); 
     53                        } 
     54                }); 
     55                 
     56                JButton but3 = new JButton("but3"); 
     57                but3.addActionListener(new ActionListener() { 
     58                         
     59                        public void actionPerformed(ActionEvent e) { 
     60                                SophieLog.warn("but3"); 
     61                        } 
     62                }); 
     63                 
     64                JButton but4 = new JButton("but4"); 
     65                but4.addActionListener(new ActionListener() { 
     66                         
     67                        public void actionPerformed(ActionEvent e) { 
     68                                SophieLog.warn("but4"); 
     69                        } 
     70                }); 
     71                 
     72                JButton but5 = new JButton("but5"); 
     73                but5.addActionListener(new ActionListener() { 
     74                         
     75                        public void actionPerformed(ActionEvent e) { 
     76                                SophieLog.warn("but5"); 
     77                        } 
     78                }); 
     79                 
     80                JButton but6 = new JButton("but6"); 
     81                but6.addActionListener(new ActionListener() { 
     82                         
     83                        public void actionPerformed(ActionEvent e) { 
     84                                SophieLog.warn("but6"); 
     85                        } 
     86                }); 
     87                 
     88                JButton but7 = new JButton("but7"); 
     89                but7.addActionListener(new ActionListener() { 
     90                         
     91                        public void actionPerformed(ActionEvent e) { 
     92                                SophieLog.warn("but7"); 
     93                        } 
     94                }); 
     95                 
     96                JButton but8 = new JButton("but8"); 
     97                but8.addActionListener(new ActionListener() { 
     98                         
     99                        public void actionPerformed(ActionEvent e) { 
     100                                SophieLog.warn("but8"); 
     101                        } 
     102                }); 
     103                 
     104                JButton but9 = new JButton("but9"); 
     105                but9.addActionListener(new ActionListener() { 
     106                         
     107                        public void actionPerformed(ActionEvent e) { 
     108                                SophieLog.warn("but9"); 
     109                        } 
     110                }); 
     111                 
     112                JButton but10 = new JButton("but10"); 
     113                but10.addActionListener(new ActionListener() { 
     114                         
     115                        public void actionPerformed(ActionEvent e) { 
     116                                SophieLog.warn("but10"); 
     117                        } 
     118                }); 
     119                 
     120                JButton but11 = new JButton("but11"); 
     121                but11.addActionListener(new ActionListener() { 
     122                         
     123                        public void actionPerformed(ActionEvent e) { 
     124                                SophieLog.warn("but11"); 
     125                        } 
     126                }); 
     127                 
     128                JButton but12 = new JButton("but12"); 
     129                but12.addActionListener(new ActionListener() { 
     130                         
     131                        public void actionPerformed(ActionEvent e) { 
     132                                SophieLog.warn("but12"); 
     133                        } 
     134                }); 
     135                 
     136                JButton but13 = new JButton("but13"); 
     137                but13.addActionListener(new ActionListener() { 
     138                         
     139                        public void actionPerformed(ActionEvent e) { 
     140                                SophieLog.warn("but13"); 
     141                        } 
     142                }); 
     143                 
     144                JButton but14 = new JButton("but14"); 
     145                but14.addActionListener(new ActionListener() { 
     146                         
     147                        public void actionPerformed(ActionEvent e) { 
     148                                SophieLog.warn("but14"); 
     149                        } 
     150                }); 
     151                 
     152                JButton but15 = new JButton("but15"); 
     153                but15.addActionListener(new ActionListener() { 
     154                         
     155                        public void actionPerformed(ActionEvent e) { 
     156                                SophieLog.warn("but15"); 
     157                        } 
     158                }); 
     159                 
     160                JButton but16 = new JButton("but16"); 
     161                but16.addActionListener(new ActionListener() { 
     162                         
     163                        public void actionPerformed(ActionEvent e) { 
     164                                SophieLog.warn("but16"); 
     165                        } 
     166                }); 
     167                 
     168                JButton but17 = new JButton("but17"); 
     169                but17.addActionListener(new ActionListener() { 
     170                         
     171                        public void actionPerformed(ActionEvent e) { 
     172                                SophieLog.warn("but17"); 
     173                        } 
     174                }); 
     175                 
     176                JButton but18 = new JButton("but18"); 
     177                but18.addActionListener(new ActionListener() { 
     178                         
     179                        public void actionPerformed(ActionEvent e) { 
     180                                SophieLog.warn("but18"); 
     181                        } 
     182                }); 
     183                 
     184                JButton but19 = new JButton("but19"); 
     185                but19.addActionListener(new ActionListener() { 
     186                         
     187                        public void actionPerformed(ActionEvent e) { 
     188                                SophieLog.warn("but19"); 
     189                        } 
     190                }); 
     191                 
     192                JButton but20 = new JButton("but20"); 
     193                but20.addActionListener(new ActionListener() { 
     194                         
     195                        public void actionPerformed(ActionEvent e) { 
     196                                SophieLog.warn("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                sophie.setViewportView(p); 
     218                p.add(but14); 
     219                p.add(but15); 
     220                p.add(but16); 
     221                p.add(but17); 
     222                p.add(but18); 
     223                p.add(but19); 
     224                p.add(but20); 
     225                p.validate(); 
     226                sophie.swingComponent().setBackground(Color.WHITE);      
     227        } 
     228         
     229        public static void main(String args[]) { 
     230                new JScrollPaneDemo(); 
     231        } 
     232}