下面列出了javax.swing.SwingUtilities#getAncestorOfClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
Rectangle getBounds() {
Rectangle bounds = null;
TopComponent modeTC = getTopComponent();
if( null == modeTC ) {
modeTC = mode.getSelectedTopComponent();
if( null == modeTC ) {
TopComponent[] tcs = mode.getTopComponents();
if( null != tcs && tcs.length > 0 )
modeTC = tcs[0];
}
}
Component modeComp = ( Component ) SwingUtilities.getAncestorOfClass(ModeComponent.class, modeTC);
if( modeComp != null ) {
bounds = modeComp.getBounds();
}
return bounds;
}
@RandomlyFails
public void testViewToModelConsistency() throws Throwable {
for(String text : TEXTS) {
JEditorPane jep = createJep(text);
try {
checkViewToModelConsistency(jep);
} catch (Throwable e) {
System.err.println("testViewToModelConsistency processing {");
System.err.println(text);
System.err.println("} failed with:");
e.printStackTrace();
throw e;
} finally {
JFrame frame = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, jep);
if (frame != null) {
frame.dispose();
}
}
}
}
public final int getSideBarWidth(){
JScrollPane scroll = (JScrollPane)SwingUtilities.getAncestorOfClass(JScrollPane.class, getParentViewport());
if (scroll!=null && scroll.getRowHeader()!=null){
Rectangle bounds = scroll.getRowHeader().getBounds();
if (bounds!=null){
return bounds.width;
}
}
return 40;
}
@Override
public void loggedActionPerformed(ActionEvent e) {
// look up the panel invoking the pop up invoking the action
BeltColumnStatisticsPanel asp = null;
// the action should only be invoked by AttributePopupMenus
Container parent = ((JComponent) e.getSource()).getParent();
if ((parent instanceof BeltColumnPopupMenu)) {
asp = ((BeltColumnPopupMenu) parent).getColumnStatisticsPanel();
} else {
asp = (BeltColumnStatisticsPanel) SwingUtilities.getAncestorOfClass(BeltColumnStatisticsPanel.class, parent);
if (asp == null) {
// we are not inside a AttributesStatisticPanel
return;
}
}
ButtonBarCardPanel cardPanel = (ButtonBarCardPanel) SwingUtilities.getAncestorOfClass(ButtonBarCardPanel.class, asp);
AbstractBeltColumnStatisticsModel model = asp.getModel();
// select the visualizations view
cardPanel.selectCard("visualizations");
// get the opened visualization
getOpenedVisualizations(cardPanel, model);
}
@Override
public void actionPerformed(final ActionEvent e) {
// Parent component.
final AdductsComponent parent = (AdductsComponent) SwingUtilities
.getAncestorOfClass(AdductsComponent.class, (Component) e.getSource());
if (parent != null) {
// Show dialog.
final ParameterSet parameters = new AddAdductParameters();
if (parameters.showSetupDialog(MZmineCore.getDesktop().getMainWindow(),
true) == ExitCode.OK) {
// Create new adduct.
final AdductType adduct =
new AdductType(parameters.getParameter(AddAdductParameters.NAME).getValue(),
parameters.getParameter(AddAdductParameters.MASS_DIFFERENCE).getValue());
// Add to list of choices (if not already present).
final Collection<AdductType> choices =
new ArrayList<AdductType>(Arrays.asList((AdductType[]) parent.getChoices()));
if (!choices.contains(adduct)) {
choices.add(adduct);
parent.setChoices(choices.toArray(new AdductType[choices.size()]));
}
}
}
}
protected JDialog createDialog(Component parent) {
Frame frame = parent instanceof Frame ? (Frame) parent
: (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);
JDialog dialog = new JDialog(frame, ("Select Font"), true);
Action okAction = new DialogOKAction(dialog);
Action cancelAction = new DialogCancelAction(dialog);
JButton okButton = new JButton(okAction);
okButton.setFont(DEFAULT_FONT);
JButton cancelButton = new JButton(cancelAction);
cancelButton.setFont(DEFAULT_FONT);
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(2, 1));
buttonsPanel.add(okButton);
buttonsPanel.add(cancelButton);
buttonsPanel.setBorder(BorderFactory.createEmptyBorder(25, 0, 10, 10));
ActionMap actionMap = buttonsPanel.getActionMap();
actionMap.put(cancelAction.getValue(Action.DEFAULT), cancelAction);
actionMap.put(okAction.getValue(Action.DEFAULT), okAction);
InputMap inputMap = buttonsPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), cancelAction.getValue(Action.DEFAULT));
inputMap.put(KeyStroke.getKeyStroke("ENTER"), okAction.getValue(Action.DEFAULT));
JPanel dialogEastPanel = new JPanel();
dialogEastPanel.setLayout(new BorderLayout());
dialogEastPanel.add(buttonsPanel, BorderLayout.NORTH);
dialog.getContentPane().add(this, BorderLayout.CENTER);
dialog.getContentPane().add(dialogEastPanel, BorderLayout.EAST);
dialog.pack();
dialog.setLocationRelativeTo(frame);
return dialog;
}
@Override
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {
if( !showCellFocusIndicator )
return;
// paint focus indicator border only if exactly one item is selected
JList<?> list = (JList<?>) SwingUtilities.getAncestorOfClass( JList.class, c );
if( list != null && list.getMinSelectionIndex() == list.getMaxSelectionIndex() )
return;
super.paintBorder( c, g, x, y, width, height );
}
public void startResizing( Component child ) {
doLayout();
MultiSplitDivider divider = findSplitDividerFor( child );
if( null == divider )
return;
MultiSplitCell resizingCell = findSplitCellFor( child );
MultiSplitDivider parentDivider = null;
MultiSplitPane parentSplit = ( MultiSplitPane ) SwingUtilities.getAncestorOfClass( MultiSplitPane.class, this );
if( null != parentSplit ) {
parentSplit.doLayout();
parentDivider = parentSplit.findSplitDividerFor( this );
}
ModeResizer.start( resizingCell.getComponent(), divider, parentDivider );
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (ExplorerManager.PROP_SELECTED_NODES.equals (evt.getPropertyName())) {
Node[] nodes = (Node[]) evt.getNewValue();
if (nodes.length!=1) {
selectPlatform (null);
}
else {
selectPlatform (nodes[0]);
Dialog dialog = (Dialog) SwingUtilities.getAncestorOfClass (Dialog.class, this);
if (dialog != null)
dialog.pack ();
}
}
}
@Override
public Object getValueAt(int row, int column) {
switch (column)
{
case 0: return SwingUtilities.getAncestorOfClass(MTGUIComponent.class, items.get(row));
case 1: return items.get(row);
case 2: return KeyEvent.getKeyText(items.get(row).getMnemonic());
default: throw new IllegalArgumentException("Unexpected value: " + column);
}
}
public void actionPerformed(ActionEvent actionEvent) {
PSheet sheet = (PSheet) SwingUtilities.getAncestorOfClass(PSheet.class, this);
if (sheet != null) {
sheet.helpRequested();
}
}
/**
* Notification method that the user has requested a popup menu.
*/
protected void popupRequested(Point p) {
PropertySheet ps = (PropertySheet) SwingUtilities.getAncestorOfClass(PropertySheet.class, this);
if (ps != null) {
ps.showPopup(p);
}
}
/**
* Notification that the user has pressed the help button
*/
protected void helpRequested() {
PropertySheet ps = (PropertySheet) SwingUtilities.getAncestorOfClass(PropertySheet.class, this);
if (ps != null) {
ps.helpAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "invokeHelp")); //NOI18N
}
}
/**
* Returns the minimum or preferred dimension needed to layout the target
* container.
*
* @param target target to get layout size for
* @param preferred should preferred size be calculated
* @return the dimension to layout the target container
*/
private Dimension layoutSize(final Container target, final boolean preferred) {
synchronized (target.getTreeLock()) {
// Each row must fit with the width allocated to the containter.
// When the container width = 0, the preferred width of the container
// has not yet been calculated so lets ask for the maximum.
int targetWidth = target.getSize().width;
if (targetWidth == 0) {
targetWidth = Integer.MAX_VALUE;
}
final int hgap = getHgap();
final int vgap = getVgap();
final Insets insets = target.getInsets();
final int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
final int maxWidth = targetWidth - horizontalInsetsAndGap;
// Fit components into the allowed width
final Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0;
final int nmembers = target.getComponentCount();
for (int i = 0; i < nmembers; i++) {
final Component m = target.getComponent(i);
if (m.isVisible()) {
final Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
// Can't add the component to current row. Start a new row.
if (rowWidth + d.width > maxWidth) {
addRow(dim, rowWidth, rowHeight);
rowWidth = 0;
rowHeight = 0;
}
// Add a horizontal gap for all components after the first
if (rowWidth != 0) {
rowWidth += hgap;
}
rowWidth += d.width;
rowHeight = Math.max(rowHeight, d.height);
}
}
addRow(dim, rowWidth, rowHeight);
dim.width += horizontalInsetsAndGap;
dim.height += insets.top + insets.bottom + vgap * 2;
// When using a scroll pane or the DecoratedLookAndFeel we need to
// make sure the preferred size is less than the size of the
// target containter so shrinking the container size works
// correctly. Removing the horizontal gap is an easy way to do this.
final Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
if (scrollPane != null && target.isValid()) {
dim.width -= (hgap + 1);
}
return dim;
}
}
/**
* Returns the minimum or preferred dimension needed to layout the target
* container.
*
* @param target target to get layout size for
* @param preferred should preferred size be calculated
* @return the dimension to layout the target container
*/
private Dimension layoutSize(Container target, boolean preferred)
{
synchronized (target.getTreeLock())
{
// Each row must fit with the width allocated to the containter.
// When the container width = 0, the preferred width of the container
// has not yet been calculated so lets ask for the maximum.
int targetWidth = target.getSize().width;
if (targetWidth == 0)
targetWidth = Integer.MAX_VALUE;
int hgap = getHgap();
int vgap = getVgap();
Insets insets = target.getInsets();
int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
int maxWidth = targetWidth - horizontalInsetsAndGap;
// Fit components into the allowed width
Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0;
int nmembers = target.getComponentCount();
for (int i = 0; i < nmembers; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
// Can't add the component to current row. Start a new row.
if (rowWidth + d.width > maxWidth)
{
addRow(dim, rowWidth, rowHeight);
rowWidth = 0;
rowHeight = 0;
}
// Add a horizontal gap for all components after the first
if (rowWidth != 0)
{
rowWidth += hgap;
}
rowWidth += d.width;
rowHeight = Math.max(rowHeight, d.height);
}
}
addRow(dim, rowWidth, rowHeight);
dim.width += horizontalInsetsAndGap;
dim.height += insets.top + insets.bottom + vgap * 2;
// When using a scroll pane or the DecoratedLookAndFeel we need to
// make sure the preferred size is less than the size of the
// target containter so shrinking the container size works
// correctly. Removing the horizontal gap is an easy way to do this.
Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
if (scrollPane != null && target.isValid())
{
dim.width -= (hgap + 1);
}
return dim;
}
}
/**
* Returns the minimum or preferred dimension needed to layout the target
* container.
*
* @param target
* target to get layout size for
* @param preferred
* should preferred size be calculated
* @return the dimension to layout the target container
*/
private Dimension layoutSize(Container target, boolean preferred)
{
synchronized (target.getTreeLock())
{
// Each row must fit with the width allocated to the containter.
// When the container width = 0, the preferred width of the
// container
// has not yet been calculated so lets ask for the maximum.
int targetWidth = target.getSize().width;
if (targetWidth == 0)
targetWidth = Integer.MAX_VALUE;
final int hgap = getHgap();
final int vgap = getVgap();
final Insets insets = target.getInsets();
final int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
final int maxWidth = targetWidth - horizontalInsetsAndGap;
// Fit components into the allowed width
final Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0;
final int nmembers = target.getComponentCount();
for (int i = 0; i < nmembers; i++)
{
final Component m = target.getComponent(i);
if (m.isVisible())
{
final Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
// Can't add the component to current row. Start a new row.
if (rowWidth + d.width > maxWidth)
{
addRow(dim, rowWidth, rowHeight);
rowWidth = 0;
rowHeight = 0;
}
// Add a horizontal gap for all components after the first
if (rowWidth != 0)
{
rowWidth += hgap;
}
rowWidth += d.width;
rowHeight = Math.max(rowHeight, d.height);
}
}
addRow(dim, rowWidth, rowHeight);
dim.width += horizontalInsetsAndGap;
dim.height += insets.top + insets.bottom + vgap * 2;
// When using a scroll pane or the DecoratedLookAndFeel we need to
// make sure the preferred size is less than the size of the
// target containter so shrinking the container size works
// correctly. Removing the horizontal gap is an easy way to do this.
final Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
if (scrollPane != null && target.isValid())
{
dim.width -= (hgap + 1);
}
return dim;
}
}
@Override
public void loggedActionPerformed(ActionEvent e) {
// look up the panel invoking the pop up invoking the action
AttributeStatisticsPanel asp = null;
// the action should only be invoked by AttributePopupMenus
Container parent = ((JComponent) e.getSource()).getParent();
if ((parent instanceof AttributePopupMenu)) {
asp = ((AttributePopupMenu) parent).getAttributeStatisticsPanel();
} else {
asp = (AttributeStatisticsPanel) SwingUtilities.getAncestorOfClass(AttributeStatisticsPanel.class, parent);
if (asp == null) {
// we are not inside a AttributesStatisticPanel
return;
}
}
ButtonBarCardPanel cardPanel = (ButtonBarCardPanel) SwingUtilities.getAncestorOfClass(ButtonBarCardPanel.class, asp);
AbstractAttributeStatisticsModel model = asp.getModel();
// select the visualizations view
cardPanel.selectCard("visualizations");
// get the opened visualization
JPanel outerPanel = (JPanel) cardPanel.getShownComponent();
for (Component innerComp : outerPanel.getComponents()) {
if (innerComp != null && innerComp.getClass().getName().equals(VISUALIZATIONS_CLASS_NAME)) {
// adjust settings
String attributeName = model.getAttribute().getName();
try {
if (model instanceof NominalAttributeStatisticsModel) {
Method showAggregatedColumnChart = innerComp.getClass().getDeclaredMethod(SHOW_AGGREGATED_COLUMN_METHOD_NAME, String.class);
showAggregatedColumnChart.setAccessible(true);
showAggregatedColumnChart.invoke(innerComp, attributeName);
} else if (model instanceof NumericalAttributeStatisticsModel
|| model instanceof DateTimeAttributeStatisticsModel) {
Method showHistogramChart = innerComp.getClass().getDeclaredMethod(SHOW_HISTOGRAM_METHOD_NAME, String.class);
showHistogramChart.setAccessible(true);
showHistogramChart.invoke(innerComp, attributeName);
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e1) {
LogService.getRoot().log(Level.WARNING, "com.rapidminer.gui.viewer.metadata.actions.OpenChartAction.cannot_show_visualization", e1);
}
break;
}
}
}
/**
* Returns the minimum or preferred dimension needed to layout the target container.
*
* @param target
* target to get layout size for
* @param preferred
* should preferred size be calculated
* @return the dimension to layout the target container
*/
private Dimension layoutSize(Container target, boolean preferred) {
synchronized (target.getTreeLock()) {
// Each row must fit with the width allocated to the containter.
// When the container width = 0, the preferred width of the
// container
// has not yet been calculated so lets ask for the maximum.
int targetWidth = target.getSize().width;
if (targetWidth == 0) {
targetWidth = Integer.MAX_VALUE;
}
int hgap = getHgap();
int vgap = getVgap();
Insets insets = target.getInsets();
int horizontalInsetsAndGap = insets.left + insets.right + hgap * 2;
int maxWidth = targetWidth - horizontalInsetsAndGap;
// Fit components into the allowed width
Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0;
int nmembers = target.getComponentCount();
for (int i = 0; i < nmembers; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
// Can't add the component to current row. Start a new row.
if (rowWidth + d.width > maxWidth) {
addRow(dim, rowWidth, rowHeight);
rowWidth = 0;
rowHeight = 0;
}
// Add a horizontal gap for all components after the first
if (rowWidth != 0) {
rowWidth += hgap;
}
rowWidth += d.width;
rowHeight = Math.max(rowHeight, d.height);
}
}
addRow(dim, rowWidth, rowHeight);
dim.width += horizontalInsetsAndGap;
dim.height += insets.top + insets.bottom + vgap * 2;
// When using a scroll pane or the DecoratedLookAndFeel we need to
// make sure the preferred size is less than the size of the
// target containter so shrinking the container size works
// correctly. Removing the horizontal gap is an easy way to do this.
Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
if (scrollPane != null && target.isValid()) {
int extra = 1;
if (scrollPane instanceof JScrollPane) {
JScrollPane jsp = (JScrollPane) scrollPane;
JScrollBar vsb = jsp.getVerticalScrollBar();
if (vsb != null) {
extra += Math.max(0, vsb.getWidth());
}
}
dim.width -= hgap + extra;
}
return dim;
}
}
public static boolean isActive( Component c ) {
Tabbed.Accessor acc = (Tabbed.Accessor)SwingUtilities.getAncestorOfClass(Tabbed.Accessor.class, c);
return acc != null
&& acc.getTabbed() instanceof TabbedImpl
&& ((TabbedImpl) acc.getTabbed()).active;
}
/**
* Retourne l'instance courante de la classe componentClass contenant l'élément component. <br/>
* Cette méthode peut-être très utile pour récupérer une référence à un parent éloigné (ancêtre), en l'absence de référence directe du type attribut.
*
* <br/>
* Ex : un composant panel désire une référence sur sa JFrame parente, alors l'instruction suivante suffit : getAncestorOfClass(JFrame.class, panel)
*
* @return Component
* @param <T>
* le type du composant recherché
* @param componentClass
* Class
* @param component
* Component
*/
@SuppressWarnings("unchecked")
public static <T> T getAncestorOfClass(final Class<T> componentClass,
final Component component) {
return (T) SwingUtilities.getAncestorOfClass(componentClass, component);
}