use of com.github.weisj.darklaf.util.Pair in project darklaf by weisJ.
the class IconEditorPanel method createValueEditor.
private JComponent createValueEditor(final String key, final Object value) {
if (value instanceof Color) {
Color color = (Color) value;
JButton palette = createPaletteButton();
AttachedPopupComponent.attachChooser(palette, () -> IconValues.getSwatchChooser(theme), c -> {
if (c != null) {
updateAction.get(key).accept(c);
put(key, c);
}
}, () -> {
Pair<Object, Color> entry = getEntry(key, Color.class);
return new NamedColor(entry.getFirst().toString(), entry.getSecond());
});
if (pickerSize == null || spinnerInsets == null) {
pickerSize = palette.getPreferredSize();
JSpinner sp = new JSpinner(new SpinnerNumberModel(10, 0, 100, 1));
spinnerInsets = sp.getBorder().getBorderInsets(sp);
pickerSize.width = sp.getPreferredSize().width - spinnerInsets.left - spinnerInsets.right;
}
QuickColorChooser chooser = new QuickColorChooser(null, color, (b, c) -> put(key, c), false, pickerSize);
chooser.setDragEnabled(true);
DynamicUI.withLocalizedTooltip(chooser, "Labels.colorChooser");
chooser.setBorder(LayoutHelper.createEmptyBorder(spinnerInsets));
updateAction.put(key, o -> {
if (o instanceof Color)
chooser.setColor((Color) o, false);
});
return LayoutHelper.createHorizontalBox(GAP, chooser, palette);
} else if (value instanceof Integer) {
JButton palette = createPaletteButton();
AttachedPopupComponent.attachChooser(palette, () -> IconValues.getOpacityChooser(theme), i -> {
if (i != null) {
updateAction.get(key).accept(i.getValue());
put(key, i);
}
}, () -> {
Pair<Object, Integer> entry = getEntry(key, Integer.class);
return new NamedInt(entry.getFirst().toString(), entry.getSecond());
});
SpinnerNumberModel numberModel = new SpinnerNumberModel((int) value, 0, 100, 1);
numberModel.addChangeListener(e -> put(key, numberModel.getValue()));
updateAction.put(key, o -> {
if (o instanceof Integer)
numberModel.setValue(o);
});
return LayoutHelper.createHorizontalBox(GAP, new JSpinner(numberModel), palette);
} else {
return new JLabel(value.toString());
}
}
use of com.github.weisj.darklaf.util.Pair in project darklaf by weisJ.
the class DarkTabFrameUI method getTabIndexAtImpl.
protected Pair<JTabFrame.TabFramePosition, Point> getTabIndexAtImpl(final JTabFrame tabFrame, final Point p) {
Component tabComp = null;
Alignment a = null;
Point pos = null;
if (!layout.isDraggedOver(Alignment.NORTH)) {
getTopContainer().getBounds(calcRect);
if (p.y >= calcRect.y && p.y <= calcRect.y + calcRect.height) {
tabComp = getTopContainer();
a = Alignment.NORTH;
pos = SwingUtilities.convertPoint(tabFrame, p, tabComp);
if (pos.x > tabComp.getWidth() / 2) {
a = Alignment.NORTH_EAST;
}
}
}
if (tabComp == null && !layout.isDraggedOver(Alignment.SOUTH)) {
getBottomContainer().getBounds(calcRect);
if (p.y >= calcRect.y && p.y <= calcRect.y + calcRect.height) {
tabComp = getBottomContainer();
a = Alignment.SOUTH;
pos = SwingUtilities.convertPoint(tabFrame, p, tabComp);
if (pos.x <= tabComp.getWidth() / 2) {
a = Alignment.SOUTH_WEST;
}
}
}
if (tabComp == null && !layout.isDraggedOver(Alignment.WEST)) {
getLeftContainer().getBounds(calcRect);
if (p.x >= calcRect.x && p.x <= calcRect.x + calcRect.width) {
tabComp = getLeftContainer();
a = Alignment.WEST;
pos = SwingUtilities.convertPoint(tabFrame, p, tabComp);
if (pos.y < tabComp.getHeight() / 2) {
a = Alignment.NORTH_WEST;
}
int tmp = pos.x;
pos.x = tabComp.getHeight() - pos.y;
pos.y = tmp;
}
}
if (tabComp == null && !layout.isDraggedOver(Alignment.EAST)) {
getRightContainer().getBounds(calcRect);
if (p.x >= calcRect.x && p.x <= calcRect.x + calcRect.width) {
tabComp = getRightContainer();
a = Alignment.EAST;
pos = SwingUtilities.convertPoint(tabFrame, p, tabComp);
if (pos.y > tabComp.getHeight() / 2) {
a = Alignment.SOUTH_EAST;
}
int tmp = pos.x;
// noinspection SuspiciousNameCombination
pos.x = pos.y;
pos.y = tmp;
}
}
if (tabComp == null) {
JTabFrame.TabFramePosition tab = maybeRestoreTabContainer(tabFrame, p);
if (tab.getAlignment() != null) {
return new Pair<>(tab, pos);
}
} else {
layout.setDraggedOver(false);
}
if (tabComp == null) {
return new Pair<>(new JTabFrame.TabFramePosition(null, -1), pos);
}
List<TabFrameTab> tabs = tabFrame.tabsForAlignment(a);
for (TabFrameTab tab : tabs) {
Rectangle rect = getTabRect(tab, a, tabComp, true);
if (rect.contains(pos)) {
return new Pair<>(new JTabFrame.TabFramePosition(a, tab.getIndex()), pos);
}
}
return new Pair<>(new JTabFrame.TabFramePosition(a, -1), pos);
}
use of com.github.weisj.darklaf.util.Pair in project darklaf by weisJ.
the class TransferUtil method setupDnD.
public static <T> Pair<DropTarget, AtomicBoolean> setupDnD(final JComponent c, final int sourceActions, final Class<T> dataType, final Supplier<T> exporter, final Consumer<T> importer, final Function<T, Icon> dragImageCreator) {
DragSource ds = new DragSource();
AtomicBoolean dragEnabled = new AtomicBoolean(true);
TransferHandler handler = new TransferHandler() {
private final DataFlavor flavor;
{
try {
flavor = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class=" + dataType.getName());
} catch (final ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
@Override
public int getSourceActions(final JComponent c) {
return sourceActions;
}
@Override
public boolean canImport(final TransferSupport support) {
return support.isDataFlavorSupported(flavor);
}
@Override
public Icon getVisualRepresentation(final Transferable t) {
try {
// noinspection unchecked
return dragImageCreator.apply((T) t.getTransferData(flavor));
} catch (UnsupportedFlavorException | IOException e) {
return null;
}
}
@Override
protected Transferable createTransferable(final JComponent c) {
T value = exporter.get();
setDragImage(IconUtil.iconToImage(dragImageCreator.apply(value), c));
return new ObjectTransferable<>(value, dataType);
}
@Override
public boolean importData(final TransferSupport support) {
if (!support.isDrop())
return false;
try {
// noinspection unchecked
importer.accept((T) support.getTransferable().getTransferData(flavor));
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, "Importing failed", e);
}
return false;
}
};
c.setTransferHandler(handler);
ds.createDefaultDragGestureRecognizer(c, sourceActions, dge -> {
if (!dragEnabled.get())
return;
handler.exportAsDrag(c, dge.getTriggerEvent(), dge.getDragAction());
});
return new Pair<>(c.getDropTarget(), dragEnabled);
}
use of com.github.weisj.darklaf.util.Pair in project darklaf by weisJ.
the class IconEditorDemo method createComponent.
@Override
public JComponent createComponent() {
IconEditor editor = new IconEditor(AllIconsDemo.loadIcons(16, false).stream().map(p -> new Pair<>(p.getFirst(), p.getSecond())).collect(Collectors.toList()));
DemoPanel panel = new DemoPanel(editor, new BorderLayout(), 0);
JPanel controls = panel.addControls();
controls.add(new JCheckBox("Show add/remove controls") {
{
setSelected(editor.isShowEditorAddRemoveControls());
addActionListener(e -> editor.setShowEditorAddRemoveControls(isSelected()));
}
});
return panel;
}
use of com.github.weisj.darklaf.util.Pair in project darklaf by weisJ.
the class OverlayRSyntaxScrollPane method createComponent.
@Override
public JComponent createComponent() {
RSyntaxTextArea textArea = new RSyntaxTextArea(StringUtil.repeat(DemoResources.LOREM_IPSUM, 5));
RTextScrollPane sp = new RTextScrollPane(textArea);
OverlayScrollPane scrollPane = new OverlayScrollPane(sp);
Random r = new Random();
Color[] colors = new Color[] { ColorPalette.ORANGE, ColorPalette.RED, ColorPalette.GREEN };
int length = textArea.getDocument().getLength();
List<Pair<Position, Color>> markers = new ArrayList<>();
int count = 15;
for (int i = 0; i < count; i++) {
try {
Position position = textArea.getDocument().createPosition(i * length / count);
Color color = colors[r.nextInt(colors.length)];
markers.add(new Pair<>(position, color));
} catch (BadLocationException ignored) {
}
}
scrollPane.getVerticalScrollBar().putClientProperty(ScrollBarConstants.KEY_BACKGROUND_PAINTER, new MarkerBackgroundPainter(textArea, markers));
return new DemoPanel(scrollPane, new BorderLayout(), 0);
}
Aggregations