|
|
@ -2,26 +2,17 @@ package com.fr.design.gui.storybook; |
|
|
|
|
|
|
|
|
|
|
|
import com.fanruan.gui.UiInspector; |
|
|
|
import com.fanruan.gui.UiInspector; |
|
|
|
import com.fine.theme.light.ui.laf.FineLightLaf; |
|
|
|
import com.fine.theme.light.ui.laf.FineLightLaf; |
|
|
|
|
|
|
|
import com.finebi.cbb.utils.StringUtils; |
|
|
|
import com.formdev.flatlaf.FlatDarkLaf; |
|
|
|
import com.formdev.flatlaf.FlatDarkLaf; |
|
|
|
import com.formdev.flatlaf.FlatLaf; |
|
|
|
import com.formdev.flatlaf.FlatLaf; |
|
|
|
import com.formdev.flatlaf.FlatLightLaf; |
|
|
|
import com.formdev.flatlaf.FlatLightLaf; |
|
|
|
import com.formdev.flatlaf.extras.FlatAnimatedLafChange; |
|
|
|
import com.formdev.flatlaf.extras.FlatAnimatedLafChange; |
|
|
|
import com.formdev.flatlaf.util.ScaledEmptyBorder; |
|
|
|
import com.formdev.flatlaf.util.ScaledEmptyBorder; |
|
|
|
import com.fr.design.gui.UILookAndFeel; |
|
|
|
import com.fr.design.gui.UILookAndFeel; |
|
|
|
import com.fr.design.gui.storybook.components.ButtonStoryBoard; |
|
|
|
import com.fr.value.NotNullLazyValue; |
|
|
|
import com.fr.design.gui.storybook.components.ButtonTabStoryBoard; |
|
|
|
|
|
|
|
import com.fr.design.gui.storybook.components.CheckBoxStoryBoard; |
|
|
|
|
|
|
|
import com.fr.design.gui.storybook.components.ComboBoxStoryBoard; |
|
|
|
|
|
|
|
import com.fr.design.gui.storybook.components.EastRegionContainer; |
|
|
|
|
|
|
|
import com.fr.design.gui.storybook.components.ExpandablePaneStoryBoard; |
|
|
|
|
|
|
|
import com.fr.design.gui.storybook.components.InputStoryBoard; |
|
|
|
|
|
|
|
import com.fr.design.gui.storybook.components.RadioButtonStoryBoard; |
|
|
|
|
|
|
|
import com.fr.design.gui.storybook.components.SliderStoryBoard; |
|
|
|
|
|
|
|
import com.fr.design.gui.storybook.components.ToggleButtonStoryBoard; |
|
|
|
|
|
|
|
import com.fr.design.gui.storybook.components.ToolTipStoryBoard; |
|
|
|
|
|
|
|
import com.fr.design.gui.storybook.components.UIHeadGroupStoryBoard; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.DefaultListCellRenderer; |
|
|
|
import javax.swing.DefaultListCellRenderer; |
|
|
|
|
|
|
|
import javax.swing.JComponent; |
|
|
|
import javax.swing.JFrame; |
|
|
|
import javax.swing.JFrame; |
|
|
|
import javax.swing.JList; |
|
|
|
import javax.swing.JList; |
|
|
|
import javax.swing.JMenu; |
|
|
|
import javax.swing.JMenu; |
|
|
@ -38,7 +29,12 @@ import java.awt.BorderLayout; |
|
|
|
import java.awt.Component; |
|
|
|
import java.awt.Component; |
|
|
|
import java.awt.event.ActionEvent; |
|
|
|
import java.awt.event.ActionEvent; |
|
|
|
import java.awt.event.KeyEvent; |
|
|
|
import java.awt.event.KeyEvent; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
import java.net.URISyntaxException; |
|
|
|
|
|
|
|
import java.net.URL; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
|
|
import static com.fine.swing.ui.layout.Layouts.cell; |
|
|
|
import static com.fine.swing.ui.layout.Layouts.cell; |
|
|
|
import static com.fine.swing.ui.layout.Layouts.row; |
|
|
|
import static com.fine.swing.ui.layout.Layouts.row; |
|
|
@ -53,21 +49,26 @@ import static com.fine.theme.utils.FineUIScale.scale; |
|
|
|
* Created on 2023/11/27 |
|
|
|
* Created on 2023/11/27 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class Storybook { |
|
|
|
public class Storybook { |
|
|
|
|
|
|
|
public static final String COMPONENTS_DIR = "/com/fr/design/gui/storybook/components"; |
|
|
|
|
|
|
|
public static final String COMPONENTS_PACKAGE = COMPONENTS_DIR.substring(1) |
|
|
|
|
|
|
|
.replaceAll("/", ".") + "."; |
|
|
|
|
|
|
|
|
|
|
|
JPanel cards; |
|
|
|
private final NotNullLazyValue<StoryBookComponent[]> StoryBookComponent = NotNullLazyValue.createValue(this::components); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private JPanel cards; |
|
|
|
|
|
|
|
private JFrame jf; |
|
|
|
|
|
|
|
|
|
|
|
public void start() { |
|
|
|
public void start() { |
|
|
|
FineLightLaf.setup(); |
|
|
|
FineLightLaf.setup(); |
|
|
|
JFrame jf = new JFrame("Story Book"); |
|
|
|
jf = new JFrame("Story Book"); |
|
|
|
jf.setJMenuBar(initMenu()); |
|
|
|
jf.setJMenuBar(initMenu()); |
|
|
|
jf.add(row( |
|
|
|
jf.add(row( |
|
|
|
cell(new JList<>(components())).with(it -> { |
|
|
|
cell(new JList<>(StoryBookComponent.getValue())).with(it -> { |
|
|
|
it.setBorder(new ScaledEmptyBorder(10, 10, 10, 10)); |
|
|
|
it.setBorder(new ScaledEmptyBorder(10, 10, 10, 10)); |
|
|
|
it.setCellRenderer(new MyListCellRenderer()); |
|
|
|
it.setCellRenderer(new MyListCellRenderer()); |
|
|
|
it.addListSelectionListener(e -> { |
|
|
|
it.addListSelectionListener(e -> { |
|
|
|
StoryBookComponent[] cs = components(); |
|
|
|
|
|
|
|
cards.removeAll(); |
|
|
|
cards.removeAll(); |
|
|
|
cards.add(cs[it.getSelectedIndex()].component); |
|
|
|
cards.add(newPanel(StoryBookComponent.getValue()[it.getSelectedIndex()])); |
|
|
|
cards.revalidate(); |
|
|
|
cards.revalidate(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}), |
|
|
|
}), |
|
|
@ -75,7 +76,7 @@ public class Storybook { |
|
|
|
cards = new JPanel(new BorderLayout()); |
|
|
|
cards = new JPanel(new BorderLayout()); |
|
|
|
it.setViewportView(cards); |
|
|
|
it.setViewportView(cards); |
|
|
|
cards.setBorder(new ScaledEmptyBorder(10, 10, 10, 10)); |
|
|
|
cards.setBorder(new ScaledEmptyBorder(10, 10, 10, 10)); |
|
|
|
cards.add(components()[0].component); |
|
|
|
cards.add(newPanel(StoryBookComponent.getValue()[0])); |
|
|
|
}) |
|
|
|
}) |
|
|
|
).getComponent()); |
|
|
|
).getComponent()); |
|
|
|
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
|
|
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
|
@ -84,6 +85,17 @@ public class Storybook { |
|
|
|
jf.setVisible(true); |
|
|
|
jf.setVisible(true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private JComponent newPanel(StoryBookComponent storyBookComponent) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(storyBookComponent.className)) { |
|
|
|
|
|
|
|
return (JComponent) Class.forName(storyBookComponent.className).newInstance(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return storyBookComponent.component.newInstance(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) { |
|
|
|
|
|
|
|
throw new RuntimeException(ex); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static class MyListCellRenderer extends DefaultListCellRenderer { |
|
|
|
static class MyListCellRenderer extends DefaultListCellRenderer { |
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -96,21 +108,47 @@ public class Storybook { |
|
|
|
|
|
|
|
|
|
|
|
private StoryBookComponent[] components() { |
|
|
|
private StoryBookComponent[] components() { |
|
|
|
ArrayList<StoryBookComponent> components = new ArrayList<>(); |
|
|
|
ArrayList<StoryBookComponent> components = new ArrayList<>(); |
|
|
|
components.add(new StoryBookComponent("Button", new ButtonStoryBoard())); |
|
|
|
for (String s : getClasses()) { |
|
|
|
components.add(new StoryBookComponent("HeadGroup", new UIHeadGroupStoryBoard())); |
|
|
|
components.add(new StoryBookComponent(s.replace("StoryBoard", ""), COMPONENTS_PACKAGE + s)); |
|
|
|
components.add(new StoryBookComponent("ToggleButton", new ToggleButtonStoryBoard())); |
|
|
|
} |
|
|
|
components.add(new StoryBookComponent("EastRegionContainer", new EastRegionContainer())); |
|
|
|
|
|
|
|
components.add(new StoryBookComponent("ButtonTabGroup", new ButtonTabStoryBoard())); |
|
|
|
|
|
|
|
components.add(new StoryBookComponent("CheckBox", new CheckBoxStoryBoard())); |
|
|
|
|
|
|
|
components.add(new StoryBookComponent("ComboBox", new ComboBoxStoryBoard())); |
|
|
|
|
|
|
|
components.add(new StoryBookComponent("RadioButton", new RadioButtonStoryBoard())); |
|
|
|
|
|
|
|
components.add(new StoryBookComponent("Input", new InputStoryBoard())); |
|
|
|
|
|
|
|
components.add(new StoryBookComponent("Slider", new SliderStoryBoard())); |
|
|
|
|
|
|
|
components.add(new StoryBookComponent("ToolTip", new ToolTipStoryBoard())); |
|
|
|
|
|
|
|
components.add(new StoryBookComponent("ExpandblePane", new ExpandablePaneStoryBoard())); |
|
|
|
|
|
|
|
return components.toArray(new StoryBookComponent[0]); |
|
|
|
return components.toArray(new StoryBookComponent[0]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static List<String> getClasses() { |
|
|
|
|
|
|
|
List<String> classNames = new ArrayList<>(); |
|
|
|
|
|
|
|
URL resource = Storybook.class.getResource(COMPONENTS_DIR); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
File folder = new File(resource.toURI()); |
|
|
|
|
|
|
|
File[] files = folder.listFiles(); |
|
|
|
|
|
|
|
if (files != null) { |
|
|
|
|
|
|
|
for (File file : files) { |
|
|
|
|
|
|
|
if (file.isFile() && file.getName().endsWith(".class")) { |
|
|
|
|
|
|
|
String className = file.getName().replace(".class", ""); |
|
|
|
|
|
|
|
if (isStory(className)) { |
|
|
|
|
|
|
|
classNames.add(className); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return classNames.stream().sorted().collect(Collectors.toList()); |
|
|
|
|
|
|
|
} catch (URISyntaxException e) { |
|
|
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 标记Story注解或者继承自StoryBoard的将展示到Storybook |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private static boolean isStory(String className) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
Class<?> aClass = Class.forName(COMPONENTS_PACKAGE + className); |
|
|
|
|
|
|
|
return aClass.isAnnotationPresent(Story.class) || StoryBoard.class.isAssignableFrom(aClass); |
|
|
|
|
|
|
|
} catch (ClassNotFoundException e) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 应用主题 |
|
|
|
* 应用主题 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -134,6 +172,7 @@ public class Storybook { |
|
|
|
FineLightLaf.setup(); |
|
|
|
FineLightLaf.setup(); |
|
|
|
FlatLaf.updateUI(); |
|
|
|
FlatLaf.updateUI(); |
|
|
|
FlatAnimatedLafChange.hideSnapshotWithAnimation(); |
|
|
|
FlatAnimatedLafChange.hideSnapshotWithAnimation(); |
|
|
|
|
|
|
|
jf.setSize(scale(600), scale(400)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -207,7 +246,7 @@ public class Storybook { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String... args) { |
|
|
|
public static void main(String... args) throws URISyntaxException { |
|
|
|
SwingUtilities.invokeLater(() -> new Storybook().start()); |
|
|
|
SwingUtilities.invokeLater(() -> new Storybook().start()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|