mirror of https://github.com/weisJ/darklaf.git
weisj
5 years ago
15 changed files with 565 additions and 45 deletions
@ -0,0 +1,69 @@
|
||||
/* |
||||
* MIT License |
||||
* |
||||
* Copyright (c) 2020 Jannis Weis |
||||
* |
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
* of this software and associated documentation files (the "Software"), to deal |
||||
* in the Software without restriction, including without limitation the rights |
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
* copies of the Software, and to permit persons to whom the Software is |
||||
* furnished to do so, subject to the following conditions: |
||||
* |
||||
* The above copyright notice and this permission notice shall be included in all |
||||
* copies or substantial portions of the Software. |
||||
* |
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
* SOFTWARE. |
||||
* |
||||
*/ |
||||
package com.github.weisj.darklaf.components; |
||||
|
||||
import java.awt.event.ActionEvent; |
||||
|
||||
import javax.swing.*; |
||||
|
||||
import com.github.weisj.darklaf.icons.RotatableIcon; |
||||
import com.github.weisj.darklaf.util.Alignment; |
||||
|
||||
public class RotatableIconAnimator extends Timer { |
||||
|
||||
private final RotatableIcon icon; |
||||
private final JComponent parent; |
||||
private final int frameCount; |
||||
private int frame; |
||||
|
||||
public RotatableIconAnimator(final RotatableIcon icon, final JComponent parent) { |
||||
this(Alignment.values().length, icon, parent); |
||||
} |
||||
|
||||
public RotatableIconAnimator(final int frames, final RotatableIcon icon, final JComponent parent) { |
||||
super(100, null); |
||||
if (icon == null) throw new IllegalArgumentException("Icon is null"); |
||||
if (parent == null) throw new IllegalArgumentException("Component is null"); |
||||
addActionListener(this::onAction); |
||||
setRepeats(true); |
||||
this.icon = icon; |
||||
this.frameCount = frames; |
||||
this.parent = parent; |
||||
} |
||||
|
||||
public void resume() { |
||||
start(); |
||||
} |
||||
|
||||
public void onAction(final ActionEvent e) { |
||||
icon.setRotation(Math.PI * 2 * (((double) frame) / frameCount)); |
||||
parent.repaint(); |
||||
frame = (frame + 1) % frameCount; |
||||
} |
||||
|
||||
public void suspend() { |
||||
stop(); |
||||
} |
||||
} |
@ -0,0 +1,127 @@
|
||||
/* |
||||
* MIT License |
||||
* |
||||
* Copyright (c) 2020 Jannis Weis |
||||
* |
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
* of this software and associated documentation files (the "Software"), to deal |
||||
* in the Software without restriction, including without limitation the rights |
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
* copies of the Software, and to permit persons to whom the Software is |
||||
* furnished to do so, subject to the following conditions: |
||||
* |
||||
* The above copyright notice and this permission notice shall be included in all |
||||
* copies or substantial portions of the Software. |
||||
* |
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
* SOFTWARE. |
||||
* |
||||
*/ |
||||
package com.github.weisj.darklaf.components.loading; |
||||
|
||||
import javax.swing.*; |
||||
|
||||
import com.github.weisj.darklaf.components.RotatableIconAnimator; |
||||
import com.github.weisj.darklaf.icons.EmptyIcon; |
||||
import com.github.weisj.darklaf.icons.RotatableIcon; |
||||
import com.github.weisj.darklaf.icons.TwoIcon; |
||||
import com.github.weisj.darklaf.util.DarkUIUtil; |
||||
|
||||
/** |
||||
* Label that functions as an loading indicator. |
||||
*/ |
||||
public class LoadingIndicator extends JLabel { |
||||
|
||||
private final RotatableIcon loadIcon = new RotatableIcon(DarkUIUtil.ICON_LOADER.getIcon("progress/step_working.svg")); |
||||
private final Icon pausedIcon = DarkUIUtil.ICON_LOADER.getIcon("progress/step_passive.svg"); |
||||
private final Icon emptyIcon = EmptyIcon.create(loadIcon.getIconWidth(), loadIcon.getIconHeight()); |
||||
private final TwoIcon displayIcon = new TwoIcon(loadIcon, null); |
||||
private final RotatableIconAnimator animator = new RotatableIconAnimator(8, loadIcon, this); |
||||
private boolean running; |
||||
|
||||
public LoadingIndicator(final String text, final Icon icon, final int horizontalAlignment) { |
||||
super(text, icon, horizontalAlignment); |
||||
displayIcon.setIconGap(getIconTextGap()); |
||||
} |
||||
|
||||
public LoadingIndicator(final String text, final int horizontalAlignment) { |
||||
this(text, null, horizontalAlignment); |
||||
} |
||||
|
||||
public LoadingIndicator(final String text) { |
||||
this(text, null, LEADING); |
||||
} |
||||
|
||||
public LoadingIndicator(final Icon image, final int horizontalAlignment) { |
||||
this(null, image, horizontalAlignment); |
||||
} |
||||
|
||||
public LoadingIndicator(final Icon image) { |
||||
this(null, image, CENTER); |
||||
} |
||||
|
||||
public LoadingIndicator() { |
||||
this("", null, LEADING); |
||||
} |
||||
|
||||
/** |
||||
* Sets whether the icon should be animated. |
||||
* |
||||
* @param running true if animated. |
||||
*/ |
||||
public void setRunning(final boolean running) { |
||||
this.running = running; |
||||
setAnimatorState(running); |
||||
} |
||||
|
||||
private void setAnimatorState(final boolean running) { |
||||
if (running == animator.isRunning()) return; |
||||
if (running) { |
||||
animator.resume(); |
||||
} else { |
||||
animator.suspend(); |
||||
} |
||||
repaint(); |
||||
} |
||||
|
||||
@Override |
||||
public void setEnabled(final boolean enabled) { |
||||
super.setEnabled(enabled); |
||||
setAnimatorState(isRunning()); |
||||
} |
||||
|
||||
/** |
||||
* Returns whether the loading icon is animated and visible. |
||||
* |
||||
* @see #setRunning(boolean) |
||||
* @return true if animated and visible. |
||||
*/ |
||||
public boolean isRunning() { |
||||
return running && isEnabled(); |
||||
} |
||||
|
||||
@Override |
||||
public void setIconTextGap(final int iconTextGap) { |
||||
super.setIconTextGap(iconTextGap); |
||||
displayIcon.setIconGap(getIconTextGap()); |
||||
} |
||||
|
||||
@Override |
||||
public Icon getIcon() { |
||||
displayIcon.setLeftIcon(running ? loadIcon : pausedIcon); |
||||
displayIcon.setRightIcon(super.getIcon()); |
||||
return displayIcon; |
||||
} |
||||
|
||||
@Override |
||||
public Icon getDisabledIcon() { |
||||
displayIcon.setLeftIcon(emptyIcon); |
||||
displayIcon.setRightIcon(super.getDisabledIcon()); |
||||
return displayIcon; |
||||
} |
||||
} |
After Width: | Height: | Size: 473 B |
After Width: | Height: | Size: 423 B |
After Width: | Height: | Size: 991 B |
After Width: | Height: | Size: 1.0 KiB |
@ -0,0 +1,64 @@
|
||||
/* |
||||
* MIT License |
||||
* |
||||
* Copyright (c) 2020 Jannis Weis |
||||
* |
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
* of this software and associated documentation files (the "Software"), to deal |
||||
* in the Software without restriction, including without limitation the rights |
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
* copies of the Software, and to permit persons to whom the Software is |
||||
* furnished to do so, subject to the following conditions: |
||||
* |
||||
* The above copyright notice and this permission notice shall be included in all |
||||
* copies or substantial portions of the Software. |
||||
* |
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
* SOFTWARE. |
||||
* |
||||
*/ |
||||
package ui.label; |
||||
|
||||
import java.awt.*; |
||||
|
||||
import javax.swing.*; |
||||
|
||||
import ui.ComponentDemo; |
||||
import ui.DemoPanel; |
||||
|
||||
public abstract class LabelDemoBase<T extends JLabel> implements ComponentDemo { |
||||
|
||||
@Override |
||||
public JComponent createComponent() { |
||||
T label = createLabel(); |
||||
DemoPanel panel = new DemoPanel(label); |
||||
|
||||
JPanel controlPanel = createControlPanel(panel, label); |
||||
return panel; |
||||
} |
||||
|
||||
protected JPanel createControlPanel(final DemoPanel panel, final T label) { |
||||
JPanel controlPanel = panel.addControls(); |
||||
controlPanel.add(new JCheckBox("enabled") { |
||||
{ |
||||
setSelected(label.isEnabled()); |
||||
addActionListener(e -> label.setEnabled(isSelected())); |
||||
} |
||||
}); |
||||
controlPanel.add(new JCheckBox("LeftToRight") { |
||||
{ |
||||
setSelected(label.getComponentOrientation().isLeftToRight()); |
||||
addActionListener(e -> label.setComponentOrientation(isSelected() ? ComponentOrientation.LEFT_TO_RIGHT |
||||
: ComponentOrientation.RIGHT_TO_LEFT)); |
||||
} |
||||
}); |
||||
return controlPanel; |
||||
} |
||||
|
||||
protected abstract T createLabel(); |
||||
} |
@ -0,0 +1,61 @@
|
||||
/* |
||||
* MIT License |
||||
* |
||||
* Copyright (c) 2020 Jannis Weis |
||||
* |
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
* of this software and associated documentation files (the "Software"), to deal |
||||
* in the Software without restriction, including without limitation the rights |
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
* copies of the Software, and to permit persons to whom the Software is |
||||
* furnished to do so, subject to the following conditions: |
||||
* |
||||
* The above copyright notice and this permission notice shall be included in all |
||||
* copies or substantial portions of the Software. |
||||
* |
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
* SOFTWARE. |
||||
* |
||||
*/ |
||||
package ui.label; |
||||
|
||||
import javax.swing.*; |
||||
|
||||
import ui.ComponentDemo; |
||||
import ui.DemoPanel; |
||||
import ui.DemoResources; |
||||
|
||||
import com.github.weisj.darklaf.components.loading.LoadingIndicator; |
||||
|
||||
public class LoadIndicator extends LabelDemoBase<LoadingIndicator> { |
||||
|
||||
public static void main(final String[] args) { |
||||
ComponentDemo.showDemo(new LoadIndicator()); |
||||
} |
||||
|
||||
@Override |
||||
protected JPanel createControlPanel(final DemoPanel panel, final LoadingIndicator label) { |
||||
JPanel controls = super.createControlPanel(panel, label); |
||||
controls.add(new JCheckBox("running") { |
||||
{ |
||||
setSelected(label.isRunning()); |
||||
addActionListener(e -> label.setRunning(isSelected())); |
||||
} |
||||
}); |
||||
return controls; |
||||
} |
||||
|
||||
protected LoadingIndicator createLabel() { |
||||
return new LoadingIndicator("Load Indicator", DemoResources.FOLDER_ICON, JLabel.LEFT); |
||||
} |
||||
|
||||
@Override |
||||
public String getTitle() { |
||||
return "Load Indicator Demo"; |
||||
} |
||||
} |
@ -0,0 +1,41 @@
|
||||
/* |
||||
* MIT License |
||||
* |
||||
* Copyright (c) 2020 Jannis Weis |
||||
* |
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
* of this software and associated documentation files (the "Software"), to deal |
||||
* in the Software without restriction, including without limitation the rights |
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
* copies of the Software, and to permit persons to whom the Software is |
||||
* furnished to do so, subject to the following conditions: |
||||
* |
||||
* The above copyright notice and this permission notice shall be included in all |
||||
* copies or substantial portions of the Software. |
||||
* |
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
* SOFTWARE. |
||||
* |
||||
*/ |
||||
package com.github.weisj.darklaf.icons; |
||||
|
||||
import java.awt.*; |
||||
|
||||
public interface RotateIcon { |
||||
|
||||
/** |
||||
* Paint the icon with rotation. |
||||
* |
||||
* @param c the parent component. |
||||
* @param g the graphics object. |
||||
* @param x the x coordinate |
||||
* @param y the y coordinate |
||||
* @param rotation the rotation in radians. |
||||
*/ |
||||
void paintIcon(Component c, Graphics g, int x, int y, double rotation); |
||||
} |
@ -0,0 +1,92 @@
|
||||
/* |
||||
* MIT License |
||||
* |
||||
* Copyright (c) 2020 Jannis Weis |
||||
* |
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
* of this software and associated documentation files (the "Software"), to deal |
||||
* in the Software without restriction, including without limitation the rights |
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
* copies of the Software, and to permit persons to whom the Software is |
||||
* furnished to do so, subject to the following conditions: |
||||
* |
||||
* The above copyright notice and this permission notice shall be included in all |
||||
* copies or substantial portions of the Software. |
||||
* |
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
* SOFTWARE. |
||||
* |
||||
*/ |
||||
package com.github.weisj.darklaf.icons; |
||||
|
||||
import java.awt.*; |
||||
|
||||
import javax.swing.*; |
||||
|
||||
public class TwoIcon implements Icon { |
||||
|
||||
private int iconGap = 2; |
||||
private Icon leftIcon; |
||||
private Icon rightIcon; |
||||
|
||||
public TwoIcon(final Icon leftIcon, final Icon rightIcon) { |
||||
this(leftIcon, rightIcon, 2); |
||||
} |
||||
|
||||
public TwoIcon(final Icon leftIcon, final Icon rightIcon, final int iconGap) { |
||||
setLeftIcon(leftIcon); |
||||
setRightIcon(rightIcon); |
||||
this.iconGap = iconGap; |
||||
} |
||||
|
||||
public void setIconGap(final int iconGap) { |
||||
this.iconGap = iconGap; |
||||
} |
||||
|
||||
public void setLeftIcon(final Icon leftIcon) { |
||||
this.leftIcon = leftIcon != null ? leftIcon : EmptyIcon.create(0); |
||||
} |
||||
|
||||
public void setRightIcon(final Icon rightIcon) { |
||||
this.rightIcon = rightIcon != null ? rightIcon : EmptyIcon.create(0); |
||||
} |
||||
|
||||
public Icon getLeftIcon() { |
||||
return leftIcon; |
||||
} |
||||
|
||||
public Icon getRightIcon() { |
||||
return rightIcon; |
||||
} |
||||
|
||||
@Override |
||||
public void paintIcon(final Component c, final Graphics g, final int x, final int y) { |
||||
int mid = getIconHeight() / 2; |
||||
boolean ltr = c.getComponentOrientation().isLeftToRight(); |
||||
Icon left = ltr ? leftIcon : rightIcon; |
||||
Icon right = ltr ? rightIcon : leftIcon; |
||||
int y1 = y + mid - left.getIconHeight() / 2; |
||||
int y2 = y + mid - right.getIconHeight() / 2; |
||||
leftIcon.paintIcon(c, g, x, y1); |
||||
rightIcon.paintIcon(c, g, x + left.getIconWidth() + iconGap, y2); |
||||
} |
||||
|
||||
@Override |
||||
public int getIconWidth() { |
||||
int l = leftIcon.getIconWidth(); |
||||
int r = rightIcon.getIconWidth(); |
||||
int gap = 0; |
||||
if (l != 0 && r != 0) gap = iconGap; |
||||
return l + r + gap; |
||||
} |
||||
|
||||
@Override |
||||
public int getIconHeight() { |
||||
return Math.max(leftIcon.getIconHeight(), rightIcon.getIconHeight()); |
||||
} |
||||
} |
Loading…
Reference in new issue