📄 splitterbar.java
字号:
package com.magelang.splitter;
import java.awt.*;
import java.awt.event.*;
import com.magelang.Colors;
/** A class that implements a splitter bar. It is only intended to be used
* in a SplitterLayout. Because it is dervied from Panel, a SplitterBar
* can do anything a normal panel can do. However, if you add any
* components to this panel, the scroll handle will not be accessible. In a
* case like this, you need to explicitly add a SplitterSpace component to the
* SplitterBar, guaranteeing a place for the handle to be available.
*
* <p>Use this code at your own risk! MageLang Institute is not
* responsible for any damage caused directly or indirctly through
* use of this code.
* <p><p>
* <b>SOFTWARE RIGHTS</b>
* <p>
* MageLang support classes, version 1.0, MageLang Institute
* <p>
* We reserve no legal rights to this code--it is fully in the
* public domain. An individual or company may do whatever
* they wish with source code distributed with it, including
* including the incorporation of it into commerical software.
*
* <p>However, this code cannot be sold as a standalone product.
* <p>
* We encourage users to develop software with this code. However,
* we do ask that credit is given to us for developing it
* By "credit", we mean that if you use these components or
* incorporate any source code into one of your programs
* (commercial product, research project, or otherwise) that
* you acknowledge this fact somewhere in the documentation,
* research report, etc... If you like these components and have
* developed a nice tool with the output, please mention that
* you developed it using these components. In addition, we ask that
* the headers remain intact in our source code. As long as these
* guidelines are kept, we expect to continue enhancing this
* system and expect to make other tools available as they are
* completed.
* <p>
* The MageLang Support Classes Gang:
* @version MageLang Support Classes 1.0, MageLang Insitute, 1997
* @author <a href="http:www.scruz.net/~thetick">Scott Stanchfield</a>, <a href=http://www.MageLang.com>MageLang Institute</a>
* @see SplitterLayout
* @see SplitterSpace
*/
public class SplitterBar extends Panel {
static final Cursor VERT_CURSOR = new Cursor(Cursor.N_RESIZE_CURSOR);
static final Cursor HORIZ_CURSOR = new Cursor(Cursor.E_RESIZE_CURSOR);
static final Cursor DEF_CURSOR = new Cursor(Cursor.DEFAULT_CURSOR);
private int orientation = SplitterLayout.VERTICAL;
private boolean alreadyDrawn = false;
private Rectangle originalBounds=null;
private Window wBar;
private boolean mouseInside=false;
/** Creates a new SpliiterBar */
public SplitterBar() {
setBackground(Color.lightGray);
addMouseMotionListener(new SplitterBarMouseMotionListener(this));
addMouseListener(new SplitterBarMouseListener(this));
}
private void checkOtherComponents() {
Rectangle currBounds = getBounds(); // get current position
Component comps[] = getParent().getComponents();
Insets insets = getParent().getInsets();
Rectangle parentBounds = getParent().getBounds();
// determine which component "this" is
int curr;
for(curr=0; (curr < comps.length) && (comps[curr] != this); curr++);
int origCurr = curr; // hold for part II check
if (orientation == SplitterLayout.VERTICAL) {
if (currBounds.y < originalBounds.y) { // moved up
// could have moved _into_ splitter bars above (or top edge)
// and/or away from splitter bars below (or bottom edge)
// check to see it we've bumped into a splitter above us.
boolean done=false;
for (int temp=curr-1; !done && temp > -1; temp--) {
if (comps[temp] instanceof SplitterBar) {
Rectangle r = comps[temp].getBounds();
if (currBounds.y <= r.y + r.height) { // touching or above...
comps[temp].setLocation(r.x, currBounds.y - r.height);
// any comps in between should be hidden
for(int c=curr-1; c > temp; c--)
comps[c].setVisible(false);
curr = temp;
currBounds = comps[temp].getBounds();
} // touching or above
else done=true; // no more compression
} // it's a splitter bar
} // for each component before us
// did we push to far?
if (currBounds.y <= insets.top) {
int delta = currBounds.y - insets.top;
// hide all components before that one
for(int temp=curr-1; temp > -1; temp--)
comps[temp].setVisible(false);
// push all splitter bars into view
done = false;
for(int temp=curr;!done && temp <= origCurr; temp++)
if (comps[temp] instanceof SplitterBar) {
Point p = comps[temp].getLocation();
p.y -= delta;
comps[temp].setLocation(p);
}
else done = comps[temp].isVisible();
} // pushed highest component off top edge
// next, check if we exposed components below us
curr = origCurr;
// if the next component is not visible, show all between us & next
// splitter bar or bottom edge
for(int temp=curr+1; temp<comps.length && !comps[temp].isVisible(); temp++)
comps[temp].setVisible(true);
} // VERTICAL -- moved up
else if (currBounds.y > originalBounds.y) { // moved down
// could have moved _into_ splitter bars below (or bottom edge)
// and/or away from splitter bars above (or top edge)
// check to see it we've bumped into a splitter below us.
boolean done=false;
for (int temp=curr+1; !done && temp < comps.length; temp++) {
if (comps[temp] instanceof SplitterBar) {
Rectangle r = comps[temp].getBounds();
if (currBounds.y + currBounds.height >= r.y) { // touching or below...
comps[temp].setLocation(r.x, currBounds.y + currBounds.height);
// any comps in between should be hidden
for(int c=curr+1; c < temp; c++)
comps[c].setVisible(false);
curr = temp;
currBounds = comps[temp].getBounds();
} // touching or above
else done=true; // no more compression
} // it's a splitter bar
} // for each component before us
// did we push to far?
if ((currBounds.y + currBounds.height) >= (parentBounds.height-insets.bottom)) {
int delta = currBounds.y + currBounds.height - (parentBounds.height-insets.bottom);
// hide all components before that one
for(int temp=curr+1; temp < comps.length; temp++)
comps[temp].setVisible(false);
// push all splitter bars into view
done = false;
for(int temp=curr;!done && temp >= origCurr; temp--)
if (comps[temp] instanceof SplitterBar) {
Point p = comps[temp].getLocation();
p.y -= delta;
comps[temp].setLocation(p);
}
else done = comps[temp].isVisible();
} // pushed highest component off top edge
// next, check if we exposed components below us
curr = origCurr;
// if the next component is not visible, show all between us & next
// splitter bar or bottom edge
for(int temp=curr-1; temp>-1 && !comps[temp].isVisible(); temp--)
comps[temp].setVisible(true);
} // VERTICAL -- moved down
} // orientation==VERTICAL
else { // orientation == HORIZONTAL
if (currBounds.x < originalBounds.x) { // moved left
// could have moved _into_ splitter bars to left (or left edge)
// and/or away from splitter bars to right (or right edge)
// check to see it we've bumped into a splitter above us.
boolean done=false;
for (int temp=curr-1; !done && temp > -1; temp--) {
if (comps[temp] instanceof SplitterBar) {
Rectangle r = comps[temp].getBounds();
if (currBounds.x <= r.x + r.width) { // touching or above...
comps[temp].setLocation(currBounds.x - r.width, r.y);
// any comps in between should be hidden
for(int c=curr-1; c > temp; c--)
comps[c].setVisible(false);
curr = temp;
currBounds = comps[temp].getBounds();
} // touching or above
else done=true; // no more compression
} // it's a splitter bar
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -