Search in sources :

Example 1 with Pair

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());
    }
}
Also used : Pair(com.github.weisj.darklaf.util.Pair) AttachedPopupComponent(com.github.weisj.darklaf.components.popup.AttachedPopupComponent) PropertyLoader(com.github.weisj.darklaf.properties.PropertyLoader) java.util(java.util) AllIcons(com.github.weisj.darklaf.iconset.AllIcons) ListChooser(com.github.weisj.darklaf.components.chooser.ListChooser) LafManager(com.github.weisj.darklaf.LafManager) DarkBorders(com.github.weisj.darklaf.components.border.DarkBorders) com.github.weisj.darklaf.properties.icons(com.github.weisj.darklaf.properties.icons) Collectors(java.util.stream.Collectors) Theme(com.github.weisj.darklaf.theme.Theme) SharedComponent(com.github.weisj.darklaf.components.popup.SharedComponent) java.awt(java.awt) Consumer(java.util.function.Consumer) LayoutHelper(com.github.weisj.darklaf.layout.LayoutHelper) List(java.util.List) SimpleListCellRenderer(com.github.weisj.darklaf.components.renderer.SimpleListCellRenderer) ComponentHelper(com.github.weisj.darklaf.components.ComponentHelper) DarkLaf(com.github.weisj.darklaf.DarkLaf) QuickColorChooser(com.github.weisj.darklaf.components.color.QuickColorChooser) DarkUIUtil(com.github.weisj.darklaf.ui.util.DarkUIUtil) DynamicUI(com.github.weisj.darklaf.components.DynamicUI) javax.swing(javax.swing) QuickColorChooser(com.github.weisj.darklaf.components.color.QuickColorChooser) Pair(com.github.weisj.darklaf.util.Pair)

Example 2 with Pair

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);
}
Also used : Alignment(com.github.weisj.darklaf.util.Alignment) Pair(com.github.weisj.darklaf.util.Pair)

Example 3 with Pair

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);
}
Also used : Transferable(java.awt.datatransfer.Transferable) DragSource(java.awt.dnd.DragSource) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) DataFlavor(java.awt.datatransfer.DataFlavor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Pair(com.github.weisj.darklaf.util.Pair)

Example 4 with Pair

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;
}
Also used : java.awt(java.awt) DemoExecutor(com.github.weisj.darklaf.ui.demo.DemoExecutor) Pair(com.github.weisj.darklaf.util.Pair) IconEditor(com.github.weisj.darklaf.components.iconeditor.IconEditor) DarklafOnly(com.github.weisj.darklaf.core.test.DarklafOnly) BaseComponentDemo(com.github.weisj.darklaf.ui.demo.BaseComponentDemo) DemoPanel(com.github.weisj.darklaf.ui.DemoPanel) Collectors(java.util.stream.Collectors) javax.swing(javax.swing) DemoPanel(com.github.weisj.darklaf.ui.DemoPanel) IconEditor(com.github.weisj.darklaf.components.iconeditor.IconEditor)

Example 5 with Pair

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);
}
Also used : ArrayList(java.util.ArrayList) OverlayScrollPane(com.github.weisj.darklaf.components.OverlayScrollPane) DemoPanel(com.github.weisj.darklaf.ui.DemoPanel) RSyntaxTextArea(org.fife.ui.rsyntaxtextarea.RSyntaxTextArea) Random(java.util.Random) RTextScrollPane(org.fife.ui.rtextarea.RTextScrollPane) Pair(com.github.weisj.darklaf.util.Pair)

Aggregations

Pair (com.github.weisj.darklaf.util.Pair)5 DemoPanel (com.github.weisj.darklaf.ui.DemoPanel)2 java.awt (java.awt)2 Collectors (java.util.stream.Collectors)2 javax.swing (javax.swing)2 DarkLaf (com.github.weisj.darklaf.DarkLaf)1 LafManager (com.github.weisj.darklaf.LafManager)1 ComponentHelper (com.github.weisj.darklaf.components.ComponentHelper)1 DynamicUI (com.github.weisj.darklaf.components.DynamicUI)1 OverlayScrollPane (com.github.weisj.darklaf.components.OverlayScrollPane)1 DarkBorders (com.github.weisj.darklaf.components.border.DarkBorders)1 ListChooser (com.github.weisj.darklaf.components.chooser.ListChooser)1 QuickColorChooser (com.github.weisj.darklaf.components.color.QuickColorChooser)1 IconEditor (com.github.weisj.darklaf.components.iconeditor.IconEditor)1 AttachedPopupComponent (com.github.weisj.darklaf.components.popup.AttachedPopupComponent)1 SharedComponent (com.github.weisj.darklaf.components.popup.SharedComponent)1 SimpleListCellRenderer (com.github.weisj.darklaf.components.renderer.SimpleListCellRenderer)1 DarklafOnly (com.github.weisj.darklaf.core.test.DarklafOnly)1 AllIcons (com.github.weisj.darklaf.iconset.AllIcons)1 LayoutHelper (com.github.weisj.darklaf.layout.LayoutHelper)1