OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 18 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 18 Rev 25
Line 30... Line 30...
30
import javax.swing.Scrollable;
30
import javax.swing.Scrollable;
31
 
31
 
32
public class TouchScrollPane extends JScrollPane implements MouseListener, MouseMotionListener {
32
public class TouchScrollPane extends JScrollPane implements MouseListener, MouseMotionListener {
33
 
33
 
34
    private JComponent child;
34
    private JComponent child;
35
    int dy = 0;
-
 
36
    int olddy = 0;
-
 
37
    private int mousePressedY;
35
    private int mousePressedY;
38
    private double viewY;
36
    private double viewY;
39
    private int lastLocationOnscreen;
37
    private int lastLocationOnscreen;
40
 
38
 
41
    public TouchScrollPane(JComponent l) {
39
    public TouchScrollPane(JComponent l) {
42
        super(l);
40
        super(l);
43
        this.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
41
        this.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
44
        this.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
42
        this.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
45
 
-
 
46
        if (!(l instanceof Scrollable)) {
43
        if (!(l instanceof Scrollable)) {
47
            throw new IllegalArgumentException("Argument is not implementing Scrollable");
44
            throw new IllegalArgumentException("Argument is not implementing Scrollable");
48
        }
45
        }
49
        this.child = l;
46
        this.child = l;
50
        child.addMouseListener(this);
47
        child.addMouseListener(this);
51
        child.addMouseMotionListener(this);
48
        child.addMouseMotionListener(this);
52
 
-
 
53
    }
49
    }
54
 
50
 
55
    @Override
51
    @Override
56
    public void paint(Graphics g) {
52
    public void paint(Graphics g) {
57
        g.setColor(Color.RED);
53
        g.setColor(Color.RED);
58
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
54
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
59
        super.paint(g);
55
        super.paint(g);
60
    }
56
    }
61
 
57
 
62
    /*
-
 
63
     * @Override public void paint(Graphics g, int x, int y, int width, int height) { int oldOffsetY
-
 
64
     * = g.getOffsetY(); g.setColor(Color.RED); g.fillRect(0, 0, this.getWidth(), this.getHeight());
-
 
65
     * g.setOffset(g.getOffsetX(), oldOffsetY + dy); child.paint(g, x, y, width, height);
-
 
66
     * g.setOffset(g.getOffsetX(), oldOffsetY); }
-
 
67
     */
-
 
68
 
-
 
69
    /**
58
    /**
70
     * @param args
59
     * @param args
71
     */
60
     */
72
    public static void main(String[] args) {
61
    public static void main(String[] args) {
73
        JFrame f = new JFrame();
62
        JFrame f = new JFrame();
Line 81... Line 70...
81
        l.setFixedCellHeight(50);
70
        l.setFixedCellHeight(50);
82
        TouchScrollPane t = new TouchScrollPane(l);
71
        TouchScrollPane t = new TouchScrollPane(l);
83
        f.setContentPane(t);
72
        f.setContentPane(t);
84
        f.setSize(200, 400);
73
        f.setSize(200, 400);
85
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
74
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
86
        f.show();
75
        f.setVisible(true);
87
    }
-
 
88
 
-
 
89
    @Override
-
 
90
    public void mouseClicked(MouseEvent e) {
-
 
91
        // TODO Auto-generated method stub
-
 
92
        System.out.println("TouchScrollPane.mouseClicked()");
-
 
93
    }
-
 
94
 
-
 
95
    @Override
-
 
96
    public void mouseEntered(MouseEvent e) {
-
 
97
        // TODO Auto-generated method stub
-
 
98
        System.out.println("TouchScrollPane.mouseEntered()");
-
 
99
    }
-
 
100
 
-
 
101
    @Override
-
 
102
    public void mouseExited(MouseEvent e) {
-
 
103
        // TODO Auto-generated method stub
-
 
104
        System.out.println("TouchScrollPane.mouseExited()");
-
 
105
    }
76
    }
106
 
77
 
107
    @Override
78
    @Override
108
    public void mousePressed(MouseEvent e) {
79
    public void mousePressed(MouseEvent e) {
109
        this.mousePressedY = e.getLocationOnScreen().y;
80
        this.mousePressedY = e.getLocationOnScreen().y;
110
        System.out.println("TouchScrollPane.mousePressed():" + e.getY());
-
 
111
 
-
 
112
        viewY = this.getViewport().getViewPosition().getY();
81
        viewY = this.getViewport().getViewPosition().getY();
113
        e.consume();
82
        e.consume();
114
 
-
 
115
    }
-
 
116
 
-
 
117
    @Override
-
 
118
    public void mouseReleased(MouseEvent e) {
-
 
119
        // TODO Auto-generated method stub
-
 
120
        System.out.println("TouchScrollPane.mouseReleased()");
-
 
121
    }
83
    }
122
 
84
 
123
    @Override
85
    @Override
124
    public void mouseDragged(MouseEvent e) {
86
    public void mouseDragged(MouseEvent e) {
125
        e.consume();
87
        e.consume();
126
        if (e.getLocationOnScreen().y == lastLocationOnscreen) {
88
        if (e.getLocationOnScreen().y == lastLocationOnscreen) {
127
            return;
89
            return;
128
        }
90
        }
129
 
-
 
130
        lastLocationOnscreen = e.getLocationOnScreen().y;
91
        lastLocationOnscreen = e.getLocationOnScreen().y;
131
        System.out.println("TouchScrollPane.mouseDragged()");
-
 
132
        // final int newDy = olddy + (e.getY() - this.mousePressedY);
-
 
133
        //
-
 
134
        // dy = newDy;
-
 
135
        // if (dy >= 0) {
-
 
136
        // dy = 0;
-
 
137
        // }
-
 
138
        // if (dy <= -(child.getHeight() - this.getHeight())) {
-
 
139
        // dy = -(child.getHeight() - this.getHeight());
-
 
140
        // }
-
 
141
        //
-
 
142
        // this.getViewport().setViewPosition(new Point(0,dy));
-
 
143
        int xDiff = 0;
-
 
144
        int yDiff = mousePressedY;
92
        int yDiff = mousePressedY;
145
        System.out.println("DY:" + yDiff);
-
 
146
        JViewport jv = this.getViewport();
93
        final JViewport jv = this.getViewport();
147
        Point p = jv.getViewPosition();
94
        int newX = 0;
148
        int newX = 0;// p.x - (e.getX() - xDiff);
-
 
149
        int deplacementY = e.getLocationOnScreen().y - yDiff;
95
        int deplacementY = e.getLocationOnScreen().y - yDiff;
150
        int newY = (int) viewY - deplacementY;
96
        int newY = (int) viewY - deplacementY;
151
        System.out.println("View Position:" + p.y);
-
 
152
        System.out.println("Decallage depuis mouse pressed:" + deplacementY + "(" + e.getLocationOnScreen().y + " - " + yDiff + ")");
-
 
153
        int maxX = child.getWidth() - jv.getWidth();
-
 
154
        int maxY = child.getHeight() - jv.getHeight();
-
 
155
        /*
-
 
156
         * if (newX < 0) newX = 0; if (newX > maxX) newX = maxX;
-
 
157
         */
-
 
158
        if (newY < 0)
-
 
159
            newY = 0;/*
-
 
160
                      * if (newY > maxY) newY = maxY;
-
 
161
                      */
-
 
162
 
97
 
-
 
98
        if (newY < 0) {
-
 
99
            newY = 0;
-
 
100
        }
163
        jv.setViewPosition(new Point(newX, newY));
101
        jv.setViewPosition(new Point(newX, newY));
164
        System.out.println(newY);
-
 
165
        e.consume();
102
        e.consume();
166
    }
103
    }
167
 
104
 
168
    @Override
105
    @Override
169
    public void mouseMoved(MouseEvent e) {
106
    public void mouseMoved(MouseEvent e) {
-
 
107
    }
-
 
108
 
-
 
109
    @Override
170
        // TODO Auto-generated method stub
110
    public void mouseClicked(MouseEvent e) {
-
 
111
    }
-
 
112
 
-
 
113
    @Override
171
        System.out.println("TouchScrollPane.mouseMoved()");
114
    public void mouseReleased(MouseEvent e) {
-
 
115
    }
-
 
116
 
-
 
117
    @Override
-
 
118
    public void mouseEntered(MouseEvent e) {
-
 
119
    }
-
 
120
 
-
 
121
    @Override
-
 
122
    public void mouseExited(MouseEvent e) {
172
    }
123
    }
173
 
124
 
174
}
125
}