下面列出了java.awt.dnd.DragSourceEvent#com.alee.utils.SwingUtils 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Collapses {@link AccordionPane} with the specified identifier without any additional checks.
* This method is intended for internal use only to avoid issues with overlapping minimum/maximum conditions.
*
* @param id {@link AccordionPane} identifier
*/
protected void collapseUnconditionally ( @NotNull final String id )
{
getPaneState ( id ).setExpanded ( false );
final WebAccordion accordion = getAccordion ();
final AccordionPane pane = accordion.getPane ( id );
final AccordionLayout layout = accordion.getLayout ();
if ( layout != null )
{
layout.collapsePane ( accordion, id );
}
else
{
pane.fireCollapsing ( accordion );
accordion.fireCollapsing ( pane );
SwingUtils.update ( accordion );
pane.fireCollapsed ( accordion );
accordion.fireCollapsed ( pane );
}
}
@Override
public boolean shouldUseSystemProxy ()
{
// Whether should save the choice or not
alwaysDoTheSame = new WebCheckBox ();
alwaysDoTheSame.setLanguage ( "weblaf.proxy.use.system.save" );
alwaysDoTheSame.setSelected ( false );
alwaysDoTheSame.setFocusable ( false );
// Ask for settings replacement with system ones
final String message = LM.get ( "weblaf.proxy.use.system.text" );
final String title = LM.get ( "weblaf.proxy.use.system.title" );
final int options = WebExtendedOptionPane.YES_NO_OPTION;
final int type = WebExtendedOptionPane.QUESTION_MESSAGE;
final WebExtendedOptionPane dialog =
WebExtendedOptionPane.showConfirmDialog ( SwingUtils.getActiveWindow (), message, alwaysDoTheSame, title, options, type );
return dialog.getResult () == WebOptionPane.YES_OPTION;
}
/**
* Uninstalls date field UI elements.
*/
protected void uninstallComponents ()
{
dateField.removeAll ();
dateField.revalidate ();
dateField.repaint ();
if ( calendar != null )
{
calendar.resetStyleId ();
calendar = null;
}
if ( popup != null )
{
popup.resetStyleId ();
popup = null;
}
button.resetStyleId ();
button = null;
field.resetStyleId ();
field = null;
SwingUtils.removeHandlesEnableStateMark ( dateField );
}
/**
* Toggles focus between split pane sides.
*
* @param splitPane {@link JSplitPane}
*/
protected void toggleFocus ( @NotNull final S splitPane )
{
final Component left = splitPane.getLeftComponent ();
final Component right = splitPane.getRightComponent ();
final KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
final Component focus = manager.getFocusOwner ();
final Component focusOn = getNextSide ( splitPane, focus );
if ( focusOn != null )
{
/**
* Don't change the focus if the new focused component belongs to the same splitpane and the same side.
*/
if ( focus == null ||
!( SwingUtilities.isDescendingFrom ( focus, left ) && SwingUtilities.isDescendingFrom ( focusOn, left ) ||
SwingUtilities.isDescendingFrom ( focus, right ) && SwingUtilities.isDescendingFrom ( focusOn, right ) ) )
{
SwingUtils.compositeRequestFocus ( focusOn );
}
}
}
/**
* Returns maximum {@link Dimension} combined from maximum sizes of all {@link Container}'s children.
*
* @param container {@link Container}
* @param cache {@link SizeCache} to reuse sizes from
* @return maximum {@link Dimension} combined from maximum sizes of all {@link Container}'s children
*/
public Dimension maxMaximum ( @NotNull final Container container, @NotNull final SizeCache cache )
{
if ( preferred == null )
{
preferred = new Dimension ( 0, 0 );
for ( int index = 0; index < container.getComponentCount (); index++ )
{
preferred = SwingUtils.maxNonNull (
preferred,
cache.maximum ( container, index )
);
}
}
return preferred;
}
/**
* Shortcut method for double-click mouse event.
*
* @param component component to handle events for
* @param runnable mouse event runnable
* @return created {@link MouseAdapter}
*/
@NotNull
public static MouseAdapter onDoubleClick ( @NotNull final Component component, @NotNull final MouseEventRunnable runnable )
{
final MouseAdapter mouseAdapter = new MouseAdapter ()
{
@Override
public void mouseClicked ( @NotNull final MouseEvent e )
{
if ( SwingUtils.isDoubleClick ( e ) )
{
runnable.run ( e );
}
}
};
component.addMouseListener ( mouseAdapter );
return mouseAdapter;
}
/**
* Shortcut method for mouse event triggering popup menu.
*
* @param component component to handle events for
* @param runnable mouse event runnable
* @return created {@link MouseAdapter}
*/
@NotNull
public static MouseAdapter onMenuTrigger ( @NotNull final Component component, @NotNull final MouseEventRunnable runnable )
{
final MouseAdapter mouseAdapter = new MouseAdapter ()
{
@Override
public void mousePressed ( @NotNull final MouseEvent e )
{
if ( SwingUtils.isRightMouseButton ( e ) )
{
runnable.run ( e );
}
}
};
component.addMouseListener ( mouseAdapter );
return mouseAdapter;
}
/**
* Requests focus for frame content if possible.
*/
protected void requestFocusInFrame ()
{
CoreSwingUtils.invokeLater ( new Runnable ()
{
@Override
public void run ()
{
if ( !SwingUtils.hasFocusOwner ( frame ) )
{
final Component component = SwingUtils.findFocusableComponent ( frame );
if ( component != null )
{
// Pass focus to the first focusable component within container
component.requestFocusInWindow ();
}
else
{
// Pass focus onto the frame itself
// Normally focus will never get onto the frame, but we can still use it when we have no other options
frame.requestFocusInWindow ();
}
}
}
} );
}
protected void layoutPlainMenu ( final WebDynamicMenu menu, final float displayProgress, final DynamicMenuType type )
{
final Dimension max = SwingUtils.maxPreferredSize ( menu.getComponents () );
final int itemsCount = menu.getComponentCount ();
switch ( type )
{
case list:
{
final int x = 0;
int y = 0;
for ( int i = 0; i < itemsCount; i++ )
{
placePlainElement ( menu, i, x, y );
y += max.height * displayProgress;
}
}
break;
}
}
public DictionariesTransferHandler ( final DictionariesTree tree )
{
super ();
this.tree = tree;
tree.addMouseMotionListener ( new MouseAdapter ()
{
@Override
public void mouseDragged ( final MouseEvent e )
{
if ( SwingUtilities.isLeftMouseButton ( e ) )
{
exportAsDrag ( DictionariesTransferHandler.this.tree, e,
SwingUtils.isCtrl ( e ) ? TransferHandler.COPY : TransferHandler.MOVE );
}
}
} );
}
@NotNull
@Override
public Dimension getPreferredSize ()
{
// Minimum size
final Dimension min = getMinimumContentSize ();
int w = min.width;
int h = min.height;
// Text size
// todo Retrieve size from painter
if ( component.isStringPainted () )
{
final boolean hor = isHorizontal ();
final FontMetrics fontSizer = component.getFontMetrics ( component.getFont () );
final String progString = component.getString ();
final int stringWidth = SwingUtils.stringWidth ( fontSizer, progString );
final int stringHeight = fontSizer.getHeight () + fontSizer.getDescent ();
w = Math.max ( w, hor ? stringWidth : stringHeight );
h = Math.max ( h, hor ? stringHeight : stringWidth );
}
// Final size
final Insets border = component.getInsets ();
return new Dimension ( border.left + w + border.right, border.top + h + border.bottom );
}
/**
* Performs on-release action.
*
* @param label {@link JLabel}
*/
protected void doRelease ( final L label )
{
final Component labelFor = label.getLabelFor ();
if ( labelFor != null && labelFor.isEnabled () )
{
final InputMap inputMap = SwingUtilities.getUIInputMap ( label, JComponent.WHEN_FOCUSED );
if ( inputMap != null )
{
inputMap.remove ( KeyStroke.getKeyStroke ( label.getDisplayedMnemonic (), ActionEvent.ALT_MASK, true ) );
inputMap.remove ( KeyStroke.getKeyStroke ( KeyEvent.VK_ALT, 0, true ) );
}
if ( labelFor instanceof Container && ( ( Container ) labelFor ).isFocusCycleRoot () )
{
labelFor.requestFocus ();
}
else
{
SwingUtils.compositeRequestFocus ( labelFor );
}
}
}
/**
* Replaces content {@link Component}.
*
* @param content new content {@link Component}
*/
public void setContent ( @Nullable final Component content )
{
if ( this.content != content )
{
if ( this.content != null )
{
remove ( this.content );
}
this.content = content;
if ( this.content != null )
{
add ( this.content );
}
SwingUtils.update ( this );
}
}
@Override
public void mouseClicked ( final MouseEvent e )
{
if ( !isEnabled () )
{
return;
}
// Displaying color chooser dialog on double-click
if ( SwingUtils.isDoubleClick ( e ) )
{
final GradientColorData colorData = getColorDataUnderPoint ( e.getPoint () );
if ( colorData != null )
{
final Color newColor = WebColorChooser.showDialog ( WebGradientColorChooser.this, colorData.getColor () );
if ( newColor != null )
{
// Updating color
colorData.setColor ( newColor );
fireStateChanged ();
repaint ();
}
}
}
}
/**
* Installs start edit actions in the list.
*
* @param list list to process
*/
protected void installStartEditActions ( final JList list )
{
mouseAdapter = new MouseAdapter ()
{
@Override
public void mouseClicked ( final MouseEvent e )
{
if ( getClicksToEdit () > 0 && e.getClickCount () == getClicksToEdit () && SwingUtils.isLeftMouseButton ( e ) )
{
final Point point = e.getPoint ();
final int index = list.getUI ().locationToIndex ( list, point );
if ( index >= 0 && index < list.getModel ().getSize () )
{
final Rectangle cell = list.getCellBounds ( index, index );
if ( cell.contains ( point ) )
{
startEdit ( list, index );
}
}
}
}
};
list.addMouseListener ( mouseAdapter );
keyAdapter = new KeyAdapter ()
{
@Override
public void keyReleased ( final KeyEvent e )
{
if ( Hotkey.F2.isTriggered ( e ) )
{
startEdit ( list, list.getSelectedIndex () );
}
}
};
list.addKeyListener ( keyAdapter );
}
/**
* Adds specified {@link AccordionPane} to this accordion.
*
* @param pane {@link AccordionPane} to add
* @param index {@link AccordionPane} index
* @return added {@link AccordionPane}
*/
@NotNull
public AccordionPane addPane ( final int index, @NotNull final AccordionPane pane )
{
add ( pane, index );
SwingUtils.update ( this );
return pane;
}
@Override
public void applyEnabled ( final boolean enabled )
{
for ( final JComponent component : getPreviewElements () )
{
SwingUtils.setEnabledRecursively ( component, enabled );
}
}
@NotNull
@Override
protected List<? extends JComponent> createPreviewElements ()
{
final WebButton e1 = new WebButton ( "First" );
final WebComboBox e2 = new WebComboBox ( new String[]{ "First", "Second", "Third" }, 1 );
final WebTextField e3 = new WebTextField ( "Third" );
final WebButton e4 = new WebButton ( "Last" );
final GroupPane groupPane = new GroupPane ( getStyleId (), e1, e2, e3, e4 );
SwingUtils.equalizeComponentsWidth ( groupPane.getComponents () );
return CollectionUtils.asList ( groupPane );
}
/**
* Shortcut method for double-click mouse event on specific tree node with condition.
*
* @param tree tree to handle events for
* @param condition node condition
* @param runnable tree node event runnable
* @param <N> {@link MutableTreeNode} type
* @return used mouse adapter
*/
@NotNull
public static <N extends MutableTreeNode> MouseAdapter onNodeDoubleClick ( @NotNull final WebTree<N> tree,
@Nullable final Predicate<N> condition,
@NotNull final TreeNodeEventRunnable<N> runnable )
{
final MouseAdapter mouseAdapter = new MouseAdapter ()
{
@Override
public void mouseClicked ( @NotNull final MouseEvent e )
{
if ( SwingUtils.isDoubleClick ( e ) )
{
final int row = tree.getUI ().getExactRowForLocation ( e.getPoint () );
if ( row != -1 )
{
final N node = tree.getNodeForRow ( row );
if ( node != null && ( condition == null || condition.test ( node ) ) )
{
runnable.run ( node );
}
}
}
}
};
tree.addMouseListener ( mouseAdapter );
return mouseAdapter;
}
@NotNull
@Override
protected List<? extends JComponent> createPreviewElements ()
{
final WebToggleButton b1 = new WebToggleButton ( "First", true );
final WebToggleButton b2 = new WebToggleButton ( "Second" );
final WebToggleButton b3 = new WebToggleButton ( "Third" );
final WebToggleButton b4 = new WebToggleButton ( "Last" );
final GroupPane groupPane = new GroupPane ( getStyleId (), b1, b2, b3, b4 );
SwingUtils.equalizeComponentsWidth ( groupPane.getComponents () );
return CollectionUtils.asList ( groupPane );
}
/**
* Paints single table header cell (column).
*
* @param g2d graphics context
* @param c table header
* @param rect cell bounds
* @param columnIndex column index
* @param column table column
* @param draggedColumn currently dragged table column
* @param columnModel table column model
*/
protected void paintCell ( final Graphics2D g2d, final C c, final Rectangle rect, final int columnIndex, final TableColumn column,
final TableColumn draggedColumn, final TableColumnModel columnModel )
{
// Table reference
final JTable table = c.getTable ();
// Complex check for the cases when trailing border should be painted
// It can be painted for middle columns, dragged column or when table is smaller than viewport
final JScrollPane scrollPane = SwingUtils.getScrollPane ( table );
final boolean paintTrailingBorder = scrollPane != null && ( column == draggedColumn ||
table.getAutoResizeMode () == JTable.AUTO_RESIZE_OFF && scrollPane.getViewport ().getWidth () > table.getWidth () ||
( ltr ? columnIndex != columnModel.getColumnCount () - 1 : columnIndex != 0 ) );
// Left side border
if ( ltr || paintTrailingBorder )
{
g2d.setColor ( gridColor );
g2d.drawLine ( rect.x - 1, rect.y + 2, rect.x - 1, rect.y + rect.height - 4 );
}
// Painting dragged cell renderer
final JComponent headerRenderer = ( JComponent ) getHeaderRenderer ( columnIndex );
headerRenderer.setOpaque ( false );
headerRenderer.setEnabled ( table == null || table.isEnabled () );
rendererPane.paintComponent ( g2d, headerRenderer, component, rect.x, rect.y, rect.width, rect.height, true );
// Right side border
if ( !ltr || paintTrailingBorder )
{
g2d.setColor ( gridColor );
g2d.drawLine ( rect.x + rect.width - 1, rect.y + 2, rect.x + rect.width - 1, rect.y + rect.height - 4 );
}
}
/**
* Displays single component on glass pane.
*
* @param component component to display
*/
public void showComponent ( @NotNull final JComponent component )
{
// Updating added component and its children orientation
SwingUtils.copyOrientation ( WebGlassPane.this, component );
// Adding with 0 index to put component on top of all existing
WebGlassPane.this.add ( component, 0 );
WebGlassPane.this.revalidate ();
WebGlassPane.this.repaint ( component.getBounds () );
}
/**
* Performs style caches update.
*
* @param c painted component
* @param d painted decoration state
*/
protected void initializeContentCache ( @NotNull final C c, @NotNull final D d )
{
// Updating styled text
styleRanges = new StyleRanges ( getComponentText ( c, d ) );
// Updating painted mnemonic index
// We cannot use button mnemonic index since it doesn't exclude style syntax from calculations
mnemonicIndex = SwingUtils.getMnemonicIndex ( styleRanges.getPlainText (), getComponentMnemonic ( c, d ) );
}
/**
* Creates and returns {@link Insets} text.
*
* @param insets {@link Insets} to render
* @param color text color
* @return {@link Insets} text
*/
@NotNull
protected String renderInsets ( @Nullable final Insets insets, @NotNull final String color )
{
final String text;
if ( !SwingUtils.isEmpty ( insets ) )
{
text = " {[ " + InsetsConverter.insetsToString ( insets ) + " ]:b;c(" + color + ")}";
}
else
{
text = "";
}
return text;
}
/**
* Constructs hotkey using the specified key stroke.
*
* @param keyStroke key stroke
*/
public HotkeyData ( @NotNull final KeyStroke keyStroke )
{
this (
SwingUtils.isCtrl ( keyStroke.getModifiers () ),
SwingUtils.isAlt ( keyStroke.getModifiers () ),
SwingUtils.isShift ( keyStroke.getModifiers () ),
keyStroke.getKeyCode ()
);
}
/**
* Returns whether hotkey controls are triggered by the key event or not.
*
* @param event processed key event
* @return true if hotkey controls are triggered by the key event, false otherwise
*/
public boolean areControlsTriggered ( @NotNull final KeyEvent event )
{
return SwingUtils.isShortcut ( event ) == isCtrl &&
SwingUtils.isAlt ( event ) == isAlt &&
SwingUtils.isShift ( event ) == isShift;
}
/**
* Highlights components with specified text on window
*/
public static List<Component> highlightComponentsWithText ( final String text, final Component highlightBase )
{
final List<Component> found = SwingUtils.findComponentsWithText ( text, highlightBase );
if ( found.size () > 0 )
{
setHiglightedComponents ( found, highlightBase );
}
else
{
clearHighlightedComponents ( highlightBase );
}
return found;
}
@Override
public void mousePressed ( final MouseEvent e )
{
if ( isEnabled () && isSelectionEnabled () && SwingUtils.isLeftMouseButton ( e ) )
{
selecting = true;
updateProgress ( e.getPoint () );
}
}
public void slideIn ()
{
if ( animator != null && animator.isRunning () )
{
animator.stop ();
}
slideY = 0;
animator = new WebTimer ( "SlidingLayout.slideInTimer", SwingUtils.frameRateDelay ( 36 ), new ActionListener ()
{
@Override
public void actionPerformed ( final ActionEvent e )
{
if ( slideY < height )
{
slideY += slideSpeed;
container.revalidate ();
}
else
{
slideY = height;
animator.stop ();
}
}
} );
animator.start ();
}
@Override
public void uninstallUI ( @NotNull final JComponent c )
{
// Uninstalling applied skin
StyleManager.uninstallSkin ( popupMenu );
// Uninstalling enabled state handling marker
SwingUtils.removeHandlesEnableStateMark ( popupMenu );
// Uninstalling UI
super.uninstallUI ( c );
}