jdk/src/solaris/classes/sun/awt/X11/XScrollbarPeer.java
author asaha
Wed, 21 Oct 2009 11:28:46 -0700
changeset 4214 0fa32d38146b
parent 3938 ef327bd847c0
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.peer.*;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    31
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
class XScrollbarPeer extends XComponentPeer implements ScrollbarPeer, XScrollbarClient {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    34
    private final static PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XScrollbarPeer");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private static final int DEFAULT_LENGTH = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private static final int DEFAULT_WIDTH_SOLARIS = 19;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static final int DEFAULT_WIDTH_LINUX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    XScrollbar tsb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        DEFAULT_WIDTH_LINUX = XToolkit.getUIDefaults().getInt("ScrollBar.defaultWidth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public void preInit(XCreateWindowParams params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        super.preInit(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        Scrollbar target = (Scrollbar) this.target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (target.getOrientation() == Scrollbar.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            tsb = new XVerticalScrollbar(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            tsb = new XHorizontalScrollbar(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        int min = target.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        int max = target.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        int vis = target.getVisibleAmount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        int val = target.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        int line = target.getLineIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        int page = target.getPageIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        tsb.setValues(val, vis, min, max, line, page);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Create a scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    XScrollbarPeer(Scrollbar target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        super(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        xSetVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Returns default size of scrollbar on the platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Currently uses hardcoded values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private int getDefaultDimension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (System.getProperty("os.name").equals("Linux")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return DEFAULT_WIDTH_LINUX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return DEFAULT_WIDTH_SOLARIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Compute the minimum size for the scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        Scrollbar sb = (Scrollbar)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return (sb.getOrientation() == Scrollbar.VERTICAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            ? new Dimension(getDefaultDimension(), DEFAULT_LENGTH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                : new Dimension(DEFAULT_LENGTH, getDefaultDimension());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public void repaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Graphics g = getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (g != null) paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Paint the scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        Scrollbar sb = (Scrollbar)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        Color colors[] = getGUIcolors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        g.setColor(colors[BACKGROUND_COLOR]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        tsb.paint(g, colors, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // paint the whole scrollbar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void repaintScrollbarRequest(XScrollbar sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * The value has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public void notifyValue(XScrollbar obj, int type, int value, boolean isAdjusting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Scrollbar sb = (Scrollbar)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        sb.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        postEvent( new AdjustmentEvent(sb, AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED, type, value, isAdjusting));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see java.awt.event.MouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * MouseEvent.MOUSE_CLICKED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * MouseEvent.MOUSE_PRESSED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * MouseEvent.MOUSE_RELEASED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * MouseEvent.MOUSE_MOVED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * MouseEvent.MOUSE_ENTERED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * MouseEvent.MOUSE_EXITED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * MouseEvent.MOUSE_DRAGGED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public void handleJavaMouseEvent( MouseEvent mouseEvent ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        super.handleJavaMouseEvent(mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        int x = mouseEvent.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int y = mouseEvent.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int modifiers = mouseEvent.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int id = mouseEvent.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if ((mouseEvent.getModifiers() & InputEvent.BUTTON1_MASK) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        switch (mouseEvent.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
          case MouseEvent.MOUSE_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
              target.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
              tsb.handleMouseEvent(id, modifiers,x,y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
          case MouseEvent.MOUSE_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
              tsb.handleMouseEvent(id, modifiers,x,y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
          case MouseEvent.MOUSE_DRAGGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
              tsb.handleMouseEvent(id, modifiers,x,y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public void handleJavaKeyEvent(KeyEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        super.handleJavaKeyEvent(event);
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
   165
        if (log.isLoggable(PlatformLogger.FINEST)) log.finer("KeyEvent on scrollbar: " + event);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (!(event.isConsumed()) && event.getID() == KeyEvent.KEY_RELEASED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            switch(event.getKeyCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            case KeyEvent.VK_UP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                log.finer("Scrolling up");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                tsb.notifyValue(tsb.getValue() - tsb.getUnitIncrement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            case KeyEvent.VK_DOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                log.finer("Scrolling down");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                tsb.notifyValue(tsb.getValue() + tsb.getUnitIncrement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            case KeyEvent.VK_LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                log.finer("Scrolling up");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                tsb.notifyValue(tsb.getValue() - tsb.getUnitIncrement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            case KeyEvent.VK_RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                log.finer("Scrolling down");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                tsb.notifyValue(tsb.getValue() + tsb.getUnitIncrement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            case KeyEvent.VK_PAGE_UP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                log.finer("Scrolling page up");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                tsb.notifyValue(tsb.getValue() - tsb.getBlockIncrement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            case KeyEvent.VK_PAGE_DOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                log.finer("Scrolling page down");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                tsb.notifyValue(tsb.getValue() + tsb.getBlockIncrement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            case KeyEvent.VK_HOME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                log.finer("Scrolling to home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                tsb.notifyValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            case KeyEvent.VK_END:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                log.finer("Scrolling to end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                tsb.notifyValue(tsb.getMaximum());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public void setValue(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        tsb.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public void setValues(int value, int visible, int minimum, int maximum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        tsb.setValues(value, visible, minimum, maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public void setLineIncrement(int l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        tsb.setUnitIncrement(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public void setPageIncrement(int p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        tsb.setBlockIncrement(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public void layout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        super.layout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        tsb.setSize(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}