下面列出了javax.swing.JViewport#getView ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void configureEnclosingScrollPane()
{
super.configureEnclosingScrollPane();
Container p = getParent();
if (p instanceof JViewport)
{
Container gp = p.getParent();
if (gp instanceof JScrollPane)
{
JScrollPane scrollPane = (JScrollPane) gp;
// Make certain we are the viewPort's view and not, for
// example, the rowHeaderView of the scrollPane -
// an implementor of fixed columns might do this.
JViewport viewport = scrollPane.getViewport();
if (viewport == null || viewport.getView() != this)
{
return;
}
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER,
wrappedCornerButton);
}
}
}
@Test
public void categoryNodesHaveCorrectExtraInformation() {
JPanel toolPanel = toolWindow.getContent();
Component[] components = toolPanel.getComponents();
OnePixelSplitter splitter = (OnePixelSplitter) components[0];
JBScrollPane leftPane = (JBScrollPane) splitter.getFirstComponent();
JViewport leftView = leftPane.getViewport();
Tree suggestionTree = (Tree) leftView.getView();
TreePath webPPath = getTreePathWithString(suggestionTree, webPCategoryData.toString());
TreeCellRenderer treeCellRenderer = suggestionTree.getCellRenderer();
Component renderedNode =
treeCellRenderer.getTreeCellRendererComponent(
suggestionTree,
webPPath.getLastPathComponent(),
false,
false,
false,
suggestionTree.getRowForPath(webPPath),
false);
assertThat(renderedNode.toString()).contains("2 recommendations");
assertThat(renderedNode.toString()).contains("29.30 KB");
}
protected void paint(SeaGlassContext context, Graphics g) {
JComponent c = context.getComponent();
JViewport viewport = (JViewport) c;
if (c.isOpaque()) {
Component view = viewport.getView();
Object ui = (view == null) ? null : invokeGetter(view, "getUI", null);
if (ui instanceof ViewportPainter) {
((ViewportPainter) ui).paintViewport(context, g, viewport);
} else {
if (viewport.getView() != null) {
g.setColor(viewport.getView().getBackground());
g.fillRect(0, 0, c.getWidth(), c.getHeight());
}
}
}
}
@Test
public void autofixIsNotShownWhenNull() {
JPanel toolPanel = toolWindow.getContent();
Component[] components = toolPanel.getComponents();
OnePixelSplitter splitter = (OnePixelSplitter) components[0];
JBScrollPane leftPane = (JBScrollPane) splitter.getFirstComponent();
JViewport leftView = leftPane.getViewport();
Tree suggestionTree = (Tree) leftView.getView();
JPanel rightPane = (JPanel) splitter.getSecondComponent();
TreePath streamingPath =
getTreePathWithString(suggestionTree, streamingSuggestionData.toString());
suggestionTree.addSelectionPath(streamingPath);
for (Component component : rightPane.getComponents()) {
if (component instanceof JButton) {
assertThat(component.isVisible()).isFalse();
}
}
}
@Override
protected void unconfigureEnclosingScrollPane()
{
super.unconfigureEnclosingScrollPane();
Container p = getParent();
if (p instanceof JViewport)
{
Container gp = p.getParent();
if (gp instanceof JScrollPane)
{
JScrollPane scrollPane = (JScrollPane) gp;
// Make certain we are the viewPort's view and not, for
// example, the rowHeaderView of the scrollPane -
// an implementor of fixed columns might do this.
JViewport viewport = scrollPane.getViewport();
if (viewport == null || viewport.getView() != this)
{
return;
}
scrollPane.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER, null);
}
}
}
/**
* This method update mouse listener on the scrollPane if it is needed.
* It also recomputes the model of searchCombo. Both actions are needed after
* the set of visible columns is changed.
*/
void updateColumnSelectionMouseListener() {
Container p = getParent();
if (p instanceof JViewport) {
Container gp = p.getParent();
if (gp instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)gp;
// Make certain we are the viewPort's view and not, for
// example, the rowHeaderView of the scrollPane -
// an implementor of fixed columns might do this.
JViewport viewport = scrollPane.getViewport();
if (viewport == null || viewport.getView() != this) {
return;
}
scrollPane.removeMouseListener(columnSelectionMouseListener);
if (getColumnModel().getColumnCount() == 0) {
scrollPane.addMouseListener(columnSelectionMouseListener);
}
}
}
if (searchCombo != null) {
searchCombo.setModel(getSearchComboModel());
}
}
@Override
protected void configureEnclosingScrollPane() {
// Si cette table est la viewportView d'un JScrollPane (la situation habituelle),
// configure ce ScrollPane en positionnant la barre verticale à "always"
// (et en installant le tableHeader comme columnHeaderView).
super.configureEnclosingScrollPane();
final Container parent = getParent();
if (parent instanceof JViewport && parent.getParent() instanceof JScrollPane) {
final JScrollPane scrollPane = (JScrollPane) parent.getParent();
if (scrollPane.getVerticalScrollBar() != null) {
scrollPane.getVerticalScrollBar().setFocusable(false);
}
if (scrollPane.getHorizontalScrollBar() != null) {
scrollPane.getHorizontalScrollBar().setFocusable(false);
}
final JViewport viewport = scrollPane.getViewport();
if (viewport == null || viewport.getView() != this) {
return;
}
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
}
}
@Override
protected void configureEnclosingScrollPane()
{
super.configureEnclosingScrollPane();
Container p = getParent();
if (p instanceof JViewport)
{
Container gp = p.getParent();
if (gp instanceof JScrollPane)
{
JScrollPane scrollPane = (JScrollPane) gp;
// Make certain we are the viewPort's view and not, for
// example, the rowHeaderView of the scrollPane -
// an implementor of fixed columns might do this.
JViewport viewport = scrollPane.getViewport();
if (viewport == null || viewport.getView() != this)
{
return;
}
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setCorner(
ScrollPaneConstants.UPPER_TRAILING_CORNER,
wrappedCornerButton);
}
}
}
@Override
protected void unconfigureEnclosingScrollPane()
{
super.unconfigureEnclosingScrollPane();
Container p = getParent();
if (p instanceof JViewport)
{
Container gp = p.getParent();
if (gp instanceof JScrollPane)
{
JScrollPane scrollPane = (JScrollPane) gp;
// Make certain we are the viewPort's view and not, for
// example, the rowHeaderView of the scrollPane -
// an implementor of fixed columns might do this.
JViewport viewport = scrollPane.getViewport();
if (viewport == null || viewport.getView() != this)
{
return;
}
scrollPane.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER, null);
}
}
}
@Override
protected void unconfigureEnclosingScrollPane()
{
super.unconfigureEnclosingScrollPane();
Container p = getParent();
if (p instanceof JViewport)
{
Container gp = p.getParent();
if (gp instanceof JScrollPane)
{
JScrollPane scrollPane = (JScrollPane) gp;
// Make certain we are the viewPort's view and not, for
// example, the rowHeaderView of the scrollPane -
// an implementor of fixed columns might do this.
JViewport viewport = scrollPane.getViewport();
if (viewport == null || viewport.getView() != this)
{
return;
}
scrollPane.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER, null);
}
}
}
@Override
protected void unconfigureEnclosingScrollPane()
{
super.unconfigureEnclosingScrollPane();
Container p = getParent();
if (p instanceof JViewport)
{
Container gp = p.getParent();
if (gp instanceof JScrollPane)
{
JScrollPane scrollPane = (JScrollPane) gp;
// Make certain we are the viewPort's view and not, for
// example, the rowHeaderView of the scrollPane -
// an implementor of fixed columns might do this.
JViewport viewport = scrollPane.getViewport();
if (viewport == null || viewport.getView() != this)
{
return;
}
scrollPane.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER, null);
}
}
}
private void addViewportListeners( JViewport viewport ) {
if( viewport == null )
return;
viewport.addContainerListener( getHandler() );
Component view = viewport.getView();
if( view != null )
view.addFocusListener( getHandler() );
}
protected void uninstallListeners(JComponent c) {
JViewport viewport = scrollpane.getViewport();
JScrollBar vsb = scrollpane.getVerticalScrollBar();
JScrollBar hsb = scrollpane.getHorizontalScrollBar();
if (viewport != null) {
viewport.removeChangeListener(viewportChangeListener);
}
if (vsb != null) {
vsb.getModel().removeChangeListener(vsbChangeListener);
vsb.removePropertyChangeListener(vsbPropertyChangeListener);
}
if (hsb != null) {
hsb.getModel().removeChangeListener(hsbChangeListener);
hsb.removePropertyChangeListener(hsbPropertyChangeListener);
}
scrollpane.removePropertyChangeListener(spPropertyChangeListener);
if (mouseScrollListener != null) {
scrollpane.removeMouseWheelListener(mouseScrollListener);
}
vsbChangeListener = null;
hsbChangeListener = null;
viewportChangeListener = null;
spPropertyChangeListener = null;
mouseScrollListener = null;
handler = null;
// From SynthScrollPaneUI.
c.removePropertyChangeListener(this);
if (viewportViewFocusHandler != null) {
viewport.removeContainerListener(viewportViewFocusHandler);
if (viewport.getView() != null) {
viewport.getView().removeFocusListener(viewportViewFocusHandler);
}
viewportViewFocusHandler = null;
}
}
protected boolean isEnabled( Component c ) {
if( c instanceof JScrollPane ) {
// check whether view component is disabled
JViewport viewport = ((JScrollPane)c).getViewport();
Component view = (viewport != null) ? viewport.getView() : null;
if( view != null && !isEnabled( view ) )
return false;
}
return c.isEnabled() && (!(c instanceof JTextComponent) || ((JTextComponent)c).isEditable());
}
protected boolean isFocused( Component c ) {
if( c instanceof JScrollPane ) {
JViewport viewport = ((JScrollPane)c).getViewport();
Component view = (viewport != null) ? viewport.getView() : null;
if( view != null ) {
if( FlatUIUtils.isPermanentFocusOwner( view ) )
return true;
if( (view instanceof JTable && ((JTable)view).isEditing()) ||
(view instanceof JTree && ((JTree)view).isEditing()) )
{
Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if( focusOwner != null )
return SwingUtilities.isDescendingFrom( focusOwner, view );
}
}
return false;
} else if( c instanceof JComboBox && ((JComboBox<?>)c).isEditable() ) {
Component editorComponent = ((JComboBox<?>)c).getEditor().getEditorComponent();
return (editorComponent != null) ? FlatUIUtils.isPermanentFocusOwner( editorComponent ) : false;
} else if( c instanceof JSpinner ) {
if( FlatUIUtils.isPermanentFocusOwner( c ) )
return true;
JComponent editor = ((JSpinner)c).getEditor();
if( editor instanceof JSpinner.DefaultEditor ) {
JTextField textField = ((JSpinner.DefaultEditor)editor).getTextField();
if( textField != null )
return FlatUIUtils.isPermanentFocusOwner( textField );
}
return false;
} else
return FlatUIUtils.isPermanentFocusOwner( c );
}
/**
* Restore stored scroll position.
*/
private void restoreScrollPosition(boolean delayScrollWithMarkingDirtyRegion) {
if (visibleTreePosition != null) {
JTree tree = getJTree();
if (tree != null) {
int row = tree.getRowForPath(visibleTreePosition.getPath());
if (row != -1) {
Rectangle bounds = tree.getRowBounds(row);
if (bounds != null) {
int scrollY = bounds.y - visibleTreePosition.getOffset();
JViewport viewport = mainScrollPane.getViewport();
Rectangle rect = viewport.getViewRect();
rect.y = scrollY;
if (!rect.isEmpty()) {
JComponent view = (JComponent) viewport.getView();
if (delayScrollWithMarkingDirtyRegion) {
RepaintManager.currentManager(viewport).addDirtyRegion(
view,
rect.x, rect.x, rect.width, rect.height);
}
ignoreScrollAdjustment = true;
try {
view.scrollRectToVisible(
rect);
} finally {
ignoreScrollAdjustment = false;
}
}
}
}
}
}
}
private void tabPaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tabPaneStateChanged
JScrollPane sPane = (JScrollPane) tabPane.getSelectedComponent();
if (sPane != null) {
JViewport view = (JViewport) sPane.getComponent(0);
selectedTable = (JTable) view.getView();
// re-apply the filter to the current tab
if (selectedTable.getRowSorter() != null) {
((TableRowSorter) selectedTable.getRowSorter()).sort();
}
if (tabManagers != null) {
// Remove all toggle button listeners
for (ChangeListener l : autoScrollToggleButton.getChangeListeners()) {
autoScrollToggleButton.removeChangeListener(l);
}
LogTableManager manager = tabManagers.get(tabPane.getSelectedIndex());
// Add current listener
autoScrollToggleButton.addChangeListener(manager);
// Refresh buttons
autoScrollToggleButton.setSelected(manager.isAutoFollowScroll());
}
}
}
/**
* 重写父类方法,以实现对文件查看列表的额外设置.
* <p>
* 为什么要重写此方法,没有更好的方法吗?<br>
* 答:因父类的封装结构不佳,filePane是private私有,子类中无法直接引用,
* 要想对filePane中的文列表额外设置,目前重写本方法是个没有办法的方法.
* <p>
* sun.swing.FilePane源码可查看地址:<a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/swing/FilePane.java">Click here.</a>
*
* @param fc the fc
* @return the j panel
*/
protected JPanel createList(JFileChooser fc)
{
JPanel p = super.createList(fc);
//* 以下代码的作用就是将文件列表JList对象引用给找回来(通过从它的父面板中层层向下搜索)
//* ,因无法从父类中直接获得列表对象的直接引用,只能用此笨办法了
if(p.getComponentCount() > 0)
{
Component scollPane = p.getComponent(0);
if(scollPane != null && scollPane instanceof JScrollPane)
{
JViewport vp = ((JScrollPane)scollPane).getViewport();
if(vp != null)
{
Component fileListView = vp.getView();
//终于找到了文件列表的实例引用
if(fileListView != null && fileListView instanceof JList)
{
//把列表的行高改成-1(即自动计算列表每个单元的行高而不指定固定值)
//* 说明:在BeautyEye LNF中,为了便JList的UI更好看,在没有其它方法有前
//* 提下就在JList的BEListUI中给它设置了默写行高32,而JFildChooser中的
//* 文件列表将会因此而使得单元行高很大——从而导致文件列表很难看,此处就是恢复
//* 文件列表单元行高的自动计算,而非指定固定行高。
//*
//* 说明2:为什么不能利用list.getClientProperty("List.isFileList")从而在JList
//* 的ui中进行判断并区别对待是否是文件列表呢?
//* 答:因为"List.isFileList"是在BasicFileChooserUI中设置的,也就是说当为个属性被
//* 设置的时候JFileChooser中的文件列表已经实例化完成(包括它的ui初始化),所以此时
//* 如果在JList的ui中想区分是不可能的,因它还没有被调置,这个设置主要是供BasicListUI
//* 在被实例化完成后,来异步处理这个属性的(通过监听属性改变事件来实现的)
((JList)fileListView).setFixedCellHeight(-1);
}
}
}
}
return p;
}
/**
* 重写父类方法,以实现对文件查看列表的额外设置.
* <p>
* 为什么要重写此方法,没有更好的方法吗?<br>
* 答:因父类的封装结构不佳,filePane是private私有,子类中无法直接引用,
* 要想对filePane中的文列表额外设置,目前重写本方法是个没有办法的方法.
* <p>
* sun.swing.FilePane源码可查看地址:<a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/swing/FilePane.java">Click here.</a>
*
* @param fc the fc
* @return the j panel
*/
protected JPanel createList(JFileChooser fc)
{
JPanel p = super.createList(fc);
//* 以下代码的作用就是将文件列表JList对象引用给找回来(通过从它的父面板中层层向下搜索)
//* ,因无法从父类中直接获得列表对象的直接引用,只能用此笨办法了
if(p.getComponentCount() > 0)
{
Component scollPane = p.getComponent(0);
if(scollPane != null && scollPane instanceof JScrollPane)
{
JViewport vp = ((JScrollPane)scollPane).getViewport();
if(vp != null)
{
Component fileListView = vp.getView();
//终于找到了文件列表的实例引用
if(fileListView != null && fileListView instanceof JList)
{
//把列表的行高改成-1(即自动计算列表每个单元的行高而不指定固定值)
//* 说明:在BeautyEye LNF中,为了便JList的UI更好看,在没有其它方法有前
//* 提下就在JList的BEListUI中给它设置了默写行高32,而JFildChooser中的
//* 文件列表将会因此而使得单元行高很大——从而导致文件列表很难看,此处就是恢复
//* 文件列表单元行高的自动计算,而非指定固定行高。
//*
//* 说明2:为什么不能利用list.getClientProperty("List.isFileList")从而在JList
//* 的ui中进行判断并区别对待是否是文件列表呢?
//* 答:因为"List.isFileList"是在BasicFileChooserUI中设置的,也就是说当为个属性被
//* 设置的时候JFileChooser中的文件列表已经实例化完成(包括它的ui初始化),所以此时
//* 如果在JList的ui中想区分是不可能的,因它还没有被调置,这个设置主要是供BasicListUI
//* 在被实例化完成后,来异步处理这个属性的(通过监听属性改变事件来实现的)
((JList)fileListView).setFixedCellHeight(-1);
}
}
}
}
return p;
}
private void mouseWheelMovedSmooth( MouseWheelEvent e ) {
// return if there is no viewport
JViewport viewport = scrollpane.getViewport();
if( viewport == null )
return;
// find scrollbar to scroll
JScrollBar scrollbar = scrollpane.getVerticalScrollBar();
if( scrollbar == null || !scrollbar.isVisible() || e.isShiftDown() ) {
scrollbar = scrollpane.getHorizontalScrollBar();
if( scrollbar == null || !scrollbar.isVisible() )
return;
}
// consume event
e.consume();
// get precise wheel rotation
double rotation = e.getPreciseWheelRotation();
// get unit and block increment
int unitIncrement;
int blockIncrement;
int orientation = scrollbar.getOrientation();
Component view = viewport.getView();
if( view instanceof Scrollable ) {
Scrollable scrollable = (Scrollable) view;
// Use (0, 0) view position to obtain constant unit increment of first item
// (which might otherwise be variable on smaller-than-unit scrolling).
Rectangle visibleRect = new Rectangle( viewport.getViewSize() );
unitIncrement = scrollable.getScrollableUnitIncrement( visibleRect, orientation, 1 );
blockIncrement = scrollable.getScrollableBlockIncrement( visibleRect, orientation, 1 );
if( unitIncrement > 0 ) {
// For the case that the first item (e.g. in a list) is larger
// than the other items, get the unit increment of the second item
// and use the smaller one.
if( orientation == SwingConstants.VERTICAL ) {
visibleRect.y += unitIncrement;
visibleRect.height -= unitIncrement;
} else {
visibleRect.x += unitIncrement;
visibleRect.width -= unitIncrement;
}
int unitIncrement2 = scrollable.getScrollableUnitIncrement( visibleRect, orientation, 1 );
if( unitIncrement2 > 0 )
unitIncrement = Math.min( unitIncrement, unitIncrement2 );
}
} else {
int direction = rotation < 0 ? -1 : 1;
unitIncrement = scrollbar.getUnitIncrement( direction );
blockIncrement = scrollbar.getBlockIncrement( direction );
}
// limit scroll amount (number of units to scroll) for small viewports
// (e.g. vertical scrolling in file chooser)
int scrollAmount = e.getScrollAmount();
int viewportWH = (orientation == SwingConstants.VERTICAL)
? viewport.getHeight()
: viewport.getWidth();
if( unitIncrement * scrollAmount > viewportWH )
scrollAmount = Math.max( viewportWH / unitIncrement, 1 );
// compute relative delta
double delta = rotation * scrollAmount * unitIncrement;
boolean adjustDelta = Math.abs( rotation ) < (1.0 + EPSILON);
double adjustedDelta = adjustDelta
? Math.max( -blockIncrement, Math.min( delta, blockIncrement ) )
: delta;
// compute new value
int value = scrollbar.getValue();
double minDelta = scrollbar.getMinimum() - value;
double maxDelta = scrollbar.getMaximum() - scrollbar.getModel().getExtent() - value;
double boundedDelta = Math.max( minDelta, Math.min( adjustedDelta, maxDelta ) );
int newValue = value + (int) Math.round( boundedDelta );
// set new value
if( newValue != value )
scrollbar.setValue( newValue );
/*debug
System.out.println( String.format( "%4d %9f / %4d %4d / %12f %5s %12f / %4d %4d %4d / %12f %12f %12f / %4d",
e.getWheelRotation(),
e.getPreciseWheelRotation(),
unitIncrement,
blockIncrement,
delta,
adjustDelta,
adjustedDelta,
value,
scrollbar.getMinimum(),
scrollbar.getMaximum(),
minDelta,
maxDelta,
boundedDelta,
newValue ) );
*/
}