下面列出了javax.swing.PopupFactory#getSharedInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void showPopup(Painter p, Rectangle rect) {
mouse.deinstall();
Point l = table.getLocationOnScreen();
rect.translate(l.x, l.y);
popupRect = rect;
popupLocation = new Point(l.x + p.getX(), l.y + p.getY());
PopupFactory popupFactory = PopupFactory.getSharedInstance();
popup = popupFactory.getPopup(table, p, popupLocation.x, popupLocation.y);
popup.show();
paranoid = new Paranoid(p);
paranoid.install();
awt = new AWT();
awt.install();
}
private void showPopup(Set<AppearancePort> portObjects) {
dragStart = null;
CircuitState circuitState = canvas.getCircuitState();
if (circuitState == null)
return;
ArrayList<Instance> ports = new ArrayList<Instance>(portObjects.size());
for (AppearancePort portObject : portObjects) {
ports.add(portObject.getPin());
}
hideCurrentPopup();
LayoutThumbnail layout = new LayoutThumbnail();
layout.setCircuit(circuitState, ports);
JViewport owner = canvasPane.getViewport();
Point ownerLoc = owner.getLocationOnScreen();
Dimension ownerDim = owner.getSize();
Dimension layoutDim = layout.getPreferredSize();
int x = ownerLoc.x + Math.max(0, ownerDim.width - layoutDim.width - 5);
int y = ownerLoc.y + Math.max(0, ownerDim.height - layoutDim.height - 5);
PopupFactory factory = PopupFactory.getSharedInstance();
Popup popup = factory.getPopup(canvasPane.getViewport(), layout, x, y);
popup.show();
curPopup = popup;
curPopupTime = System.currentTimeMillis();
}
public static void uninstall() {
PopupFactory factory = PopupFactory.getSharedInstance();
if (!(factory instanceof RoundedPopupFactory)) {
return;
}
PopupFactory stored = ((RoundedPopupFactory) factory).storedFactory;
PopupFactory.setSharedInstance(stored);
}
/**
* Installs the ShadowPopupFactory as the shared popup factory
* on non-Mac platforms. Also stores the previously set factory,
* so that it can be restored in <code>#uninstall</code>.<p>
*
* In some Mac Java environments the popup factory throws
* a NullPointerException when we call <code>#getPopup</code>.<p>
*
* The Mac case shows that we may have problems replacing
* non PopupFactory instances. Therefore we should consider
* replacing only instances of PopupFactory.
*
* @see #uninstall()
*/
public static void install() {
final String os = System.getProperty("os.name");
final boolean macintosh = os != null && os.indexOf("Mac") != -1;
if (macintosh) {
return;
}
final PopupFactory factory = PopupFactory.getSharedInstance();
if (factory instanceof ShadowPopupFactory) {
return;
}
PopupFactory.setSharedInstance(new ShadowPopupFactory(factory));
}
/**
* Create and display the popup at the given bounds.
*
* @param popupBounds location and size of the popup.
* @param displayAboveCaret whether the popup is displayed above the anchor
* bounds or below them (it does not be right above them).
*/
private void show(Rectangle popupBounds, boolean displayAboveCaret) {
// Hide the original popup if exists
if (popup != null) {
popup.hide();
popup = null;
}
// Explicitly set the preferred size
Dimension origPrefSize = getPreferredSize();
Dimension newPrefSize = popupBounds.getSize();
JComponent contComp = getContentComponent();
if (contComp == null){
return;
}
contComp.setPreferredSize(newPrefSize);
showRetainedPreferredSize = newPrefSize.equals(origPrefSize);
PopupFactory factory = PopupFactory.getSharedInstance();
// Lightweight completion popups don't work well on the Mac - trying
// to click on its scrollbars etc. will cause the window to be hidden,
// so force a heavyweight parent by passing in owner==null. (#96717)
JTextComponent owner = layout.getEditorComponent();
if(owner != null && owner.getClientProperty("ForceHeavyweightCompletionPopup") != null) {
owner = null;
}
// #76648: Autocomplete box is too close to text
if(displayAboveCaret && Utilities.isMac()) {
popupBounds.y -= 10;
}
popup = factory.getPopup(owner, contComp, popupBounds.x, popupBounds.y);
popup.show();
this.popupBounds = popupBounds;
this.displayAboveCaret = displayAboveCaret;
}
@Override
public void actionPerformed(final ActionEvent e) {
if (text != null && text.length() > 0) {
final Point currentPoint = MouseInfo.getPointerInfo().getLocation();
if (isPointWithinParentBounds(currentPoint)) {
final PopupFactory popupFactory = PopupFactory.getSharedInstance();
final JToolTip info = new JToolTip();
info.setTipText("<html>" + text + "</html>");
popup = popupFactory.getPopup(parent, info, currentPoint.x + 20, currentPoint.y - 20);
popup.show();
}
}
}
/**
* Create a new QPopup to display this popover's {@link #getContents()}.
*/
protected QPopup createPopup() {
PopupFactory pf = PopupFactory.getSharedInstance();
QPopupFactory qpf = null;
if (pf instanceof QPopupFactory) {
qpf = (QPopupFactory) pf;
} else {
qpf = new QPopupFactory(pf);
}
return qpf.getQPopup(getOwner(), popupTarget, getContents());
}
/**
* Create and display the popup at the given bounds.
*
* @param popupBounds location and size of the popup.
* @param displayAboveCaret whether the popup is displayed above the anchor
* bounds or below them (it does not be right above them).
*/
private void show(Rectangle popupBounds, boolean displayAboveCaret) {
// Hide the original popup if exists
if (popup != null) {
popup.hide();
popup = null;
}
// Explicitly set the preferred size
Dimension origPrefSize = getPreferredSize();
Dimension newPrefSize = popupBounds.getSize();
JComponent contComp = getContentComponent();
if (contComp == null){
return;
}
contComp.setPreferredSize(newPrefSize);
showRetainedPreferredSize = newPrefSize.equals(origPrefSize);
PopupFactory factory = PopupFactory.getSharedInstance();
// Lightweight completion popups don't work well on the Mac - trying
// to click on its scrollbars etc. will cause the window to be hidden,
// so force a heavyweight parent by passing in owner==null. (#96717)
JTextComponent owner = getEditorComponent();
if(owner != null && owner.getClientProperty("ForceHeavyweightCompletionPopup") != null) { //NOI18N
owner = null;
}
// #76648: Autocomplete box is too close to text
if(displayAboveCaret && Utilities.isMac()) {
popupBounds.y -= 10;
}
popup = factory.getPopup(owner, contComp, popupBounds.x, popupBounds.y);
popup.show();
this.popupBounds = popupBounds;
this.displayAboveCaret = displayAboveCaret;
}
private void addStatistics(String kind, int count, int numRow,
final MouseEvent e) {
JLabel label = (JLabel)e.getComponent();
if(!label.getToolTipText().contains(kind)) {
// add the statistics to the tooltip
String toolTip = label.getToolTipText();
toolTip = toolTip.replaceAll("</?html>", "");
toolTip = kind + " = " + count + "<br>" + toolTip;
toolTip = "<html>" + toolTip + "</html>";
label.setToolTipText(toolTip);
}
if(bottomSplitPane.getDividerLocation()
/ bottomSplitPane.getSize().getWidth() < 0.90) {
// select the row in the statistics table
statisticsTabbedPane.setSelectedIndex(1);
oneRowStatisticsTable.setRowSelectionInterval(numRow, numRow);
oneRowStatisticsTable.scrollRectToVisible(oneRowStatisticsTable
.getCellRect(numRow, 0, true));
} else {
// display a tooltip
JToolTip tip = label.createToolTip();
tip.setTipText(kind + " = " + count);
PopupFactory popupFactory = PopupFactory.getSharedInstance();
final Popup tipWindow =
popupFactory.getPopup(label, tip, e.getX()
+ e.getComponent().getLocationOnScreen().x, e.getY()
+ e.getComponent().getLocationOnScreen().y);
tipWindow.show();
Date timeToRun = new Date(System.currentTimeMillis() + 2000);
Timer timer = new Timer("Annic statistics hide tooltip timer", true);
timer.schedule(new TimerTask() {
@Override
public void run() {
// hide the tooltip after 2 seconds
tipWindow.hide();
}
}, timeToRun);
}
}
@Override
public void initialize() {
if( SystemInfo.IS_MAC )
initializeAqua();
super.initialize();
// install popup factory
oldPopupFactory = PopupFactory.getSharedInstance();
PopupFactory.setSharedInstance( new FlatPopupFactory() );
// install mnemonic handler
mnemonicHandler = new MnemonicHandler();
mnemonicHandler.install();
// listen to desktop property changes to update UI if system font or scaling changes
if( SystemInfo.IS_WINDOWS ) {
// Windows 10 allows increasing font size independent of scaling:
// Settings > Ease of Access > Display > Make text bigger (100% - 225%)
desktopPropertyName = "win.messagebox.font";
} else if( SystemInfo.IS_LINUX ) {
// Linux/Gnome allows changing font in "Tweaks" app
desktopPropertyName = "gnome.Gtk/FontName";
// Linux/Gnome allows extra scaling and larger text:
// Settings > Devices > Displays > Scale (100% or 200%)
// Settings > Universal access > Large Text (off or on, 125%)
// "Tweaks" app > Fonts > Scaling Factor (0,5 - 3)
desktopPropertyName2 = "gnome.Xft/DPI";
}
if( desktopPropertyName != null ) {
desktopPropertyListener = e -> {
String propertyName = e.getPropertyName();
if( desktopPropertyName.equals( propertyName ) || propertyName.equals( desktopPropertyName2 ) )
reSetLookAndFeel();
else if( DESKTOPFONTHINTS.equals( propertyName ) ) {
if( UIManager.getLookAndFeel() instanceof FlatLaf ) {
putAATextInfo( UIManager.getLookAndFeelDefaults() );
updateUILater();
}
}
};
Toolkit toolkit = Toolkit.getDefaultToolkit();
toolkit.addPropertyChangeListener( desktopPropertyName, desktopPropertyListener );
if( desktopPropertyName2 != null )
toolkit.addPropertyChangeListener( desktopPropertyName2, desktopPropertyListener );
toolkit.addPropertyChangeListener( DESKTOPFONTHINTS, desktopPropertyListener );
}
// Following code should be ideally in initialize(), but needs color from UI defaults.
// Do not move this code to getDefaults() to avoid side effects in the case that
// getDefaults() is directly invoked from 3rd party code. E.g. `new FlatLightLaf().getDefaults()`.
postInitialization = defaults -> {
// update link color in HTML text
Color linkColor = defaults.getColor( "Component.linkColor" );
if( linkColor != null ) {
new HTMLEditorKit().getStyleSheet().addRule(
String.format( "a { color: #%06x; }", linkColor.getRGB() & 0xffffff ) );
}
};
// enable/disable window decorations, but only if system property is either
// "true" or "false"; in other cases it is not changed
Boolean useWindowDecorations = FlatSystemProperties.getBooleanStrict( FlatSystemProperties.USE_WINDOW_DECORATIONS, null );
if( useWindowDecorations != null ) {
oldFrameWindowDecorated = JFrame.isDefaultLookAndFeelDecorated();
oldDialogWindowDecorated = JDialog.isDefaultLookAndFeelDecorated();
JFrame.setDefaultLookAndFeelDecorated( useWindowDecorations );
JDialog.setDefaultLookAndFeelDecorated( useWindowDecorations );
}
}
private void showPopupAtMousePosition() {
// StaticDebug.debug("DND POPUP: Show popup at Mouse position");
// get mouse location
Point screenLocation = MouseInfo.getPointerInfo().getLocation();
screenLocation.x += 26;
screenLocation.y += 10;
// if tooltip is shown
if (tipWindow != null) {
// StaticDebug.debug("DND POPUP: Popup is already shown");
// check if mouse has moved
if (mouseX != screenLocation.x || mouseY != screenLocation.y) {
// StaticDebug.debug("DND POPUP: old position x = "+mouseX);
// StaticDebug.debug("DND POPUP: old position y = "+mouseY);
// StaticDebug.debug("DND POPUP: new position x = "+screenLocation.x);
// StaticDebug.debug("DND POPUP: new position y = "+screenLocation.y);
// StaticDebug.debug("DND POPUP: Mouse position has changed.. hide popup first");
// hide tooltip
hideDropDeniedTooltip();
} else {
// StaticDebug.debug("DND POPUP: Restart hide timer to prevent popup from being hidden.");
// otherwise restart hide timer
hideTimer.restart();
return;
}
}
Point componentLocation = (Point) screenLocation.clone();
SwingUtilities.convertPointFromScreen(componentLocation, popupSource);
if (tipWindow == null && popupSource.contains(componentLocation)) {
// StaticDebug.debug("DND POPUP: Mouse is inside popupSource and popup is not shown");
JToolTip tip = popupSource.createToolTip();
tip.setTipText(reason);
PopupFactory popupFactory = PopupFactory.getSharedInstance();
mouseX = screenLocation.x;
mouseY = screenLocation.y;
// StaticDebug.debug("DND POPUP: show popup at "+mouseX+","+mouseY+" and start hide timer");
tipWindow = popupFactory.getPopup(popupSource, tip, mouseX, mouseY);
tipWindow.show();
hideTimer.restart();
}
}
private PopupFactory getPopupFactory() {
if (pf == null) {
pf = PopupFactory.getSharedInstance();
}
return pf;
}
protected void refreshUI() {
if (qPopupFactory == null) {
qPopupFactory = new QPopupFactory(PopupFactory.getSharedInstance());
}
if (toolTipTypeComboBox.getSelectedIndex() == 1) {
PopupFactory.setSharedInstance(qPopupFactory.getParentDelegate());
} else {
qPopupFactory.setToolTipCallout(!NONE.equals(calloutTypeComboBox
.getSelectedItem()));
PopupFactory.setSharedInstance(qPopupFactory);
}
boolean tooltipsActive = toolTipTypeComboBox.getSelectedIndex() != 2;
if (tooltipsActive) {
Font font = fontComboBox.getSelectedFont();
float size = fontSizeSlider.getValue();
font = font.deriveFont(size);
UIManager.getDefaults().put("ToolTip.font", font);
Color background = color.getColorSelectionModel()
.getSelectedColor();
Color foreground;
float[] hsl = HSLColor.fromRGB(background, null);
if (hsl[2] < .5) {
foreground = Color.white;
} else {
foreground = Color.black;
}
UIManager.getDefaults().put("ToolTip.background", background);
UIManager.getDefaults().put("ToolTip.foreground", foreground);
}
ToolTipManager.sharedInstance().setEnabled(
toolTipTypeComboBox.getSelectedIndex() != 2);
colorLabel.setVisible(tooltipsActive);
color.setVisible(tooltipsActive);
fontSizeLabel.setVisible(tooltipsActive);
fontSizeSlider.setVisible(tooltipsActive);
fontLabel.setVisible(tooltipsActive);
fontComboBox.setVisible(tooltipsActive);
calloutTypeLabel
.setVisible(toolTipTypeComboBox.getSelectedIndex() == 0);
calloutTypeComboBox
.setVisible(toolTipTypeComboBox.getSelectedIndex() == 0);
CalloutType type = null;
try {
type = CalloutType.valueOf((String) calloutTypeComboBox
.getSelectedItem());
} catch (Exception e) {
}
sampleButton.putClientProperty(QPopup.PROPERTY_CALLOUT_TYPE, type);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}
/**
* Returns the <code>Popup</code> that will be responsible for
* displaying the <code>JPopupMenu</code>.
*
* @param popup JPopupMenu requesting Popup
* @param x Screen x location Popup is to be shown at
* @param y Screen y location Popup is to be shown at.
* @return Popup that will show the JPopupMenu
* @since 1.4
*/
public Popup getPopup(JPopupMenu popup, int x, int y) {
PopupFactory popupFactory = PopupFactory.getSharedInstance();
return popupFactory.getPopup(popup.getInvoker(), popup, x, y);
}