|
5 | 5 | import dev.ambershadow.cogfly.util.*; |
6 | 6 |
|
7 | 7 | import javax.swing.*; |
| 8 | +import javax.swing.event.DocumentEvent; |
| 9 | +import javax.swing.event.DocumentListener; |
8 | 10 | import java.awt.*; |
| 11 | +import java.awt.event.MouseEvent; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Paths; |
9 | 14 | import java.util.ArrayList; |
10 | 15 | import java.util.List; |
11 | 16 | import java.util.concurrent.CompletableFuture; |
12 | 17 |
|
13 | 18 | public class ProfilesScreenElement extends JPanel implements ReloadablePage { |
| 19 | + public static void promptCreation(Runnable callback){ |
| 20 | + JDialog prompt = new JDialog(FrameManager.getOrCreate().frame); |
| 21 | + prompt.setModal(true); |
| 22 | + prompt.setSize(new Dimension(300, 150)); |
| 23 | + prompt.setResizable(false); |
| 24 | + prompt.setLocationRelativeTo(null); |
| 25 | + prompt.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
| 26 | + JPanel holder = new JPanel(); |
| 27 | + JLabel name = new JLabel("Name: "); |
| 28 | + JTextField nameField = new JTextField(""); |
| 29 | + JLabel icon = new JLabel("Icon (optional): "); |
| 30 | + JButton button = new JButton("Click here to select a file"); |
| 31 | + JPanel extraHolder = new JPanel(); |
| 32 | + extraHolder.add(icon); |
| 33 | + extraHolder.add(button); |
| 34 | + |
| 35 | + JButton create = new JButton("Create"); |
| 36 | + create.setEnabled(false); |
| 37 | + create.setPreferredSize(new Dimension(50, 20)); |
| 38 | + create.addActionListener(_ -> { |
| 39 | + prompt.dispose(); |
| 40 | + ProfileManager.createProfile(nameField.getText(), |
| 41 | + button.getText().equals("Click here to select a file") ? "" : |
| 42 | + button.getText()); |
| 43 | + FrameManager.getOrCreate().getCurrentPage().reload(); |
| 44 | + callback.run(); |
| 45 | + }); |
| 46 | + |
| 47 | + nameField.getDocument().addDocumentListener(new DocumentListener() { |
| 48 | + @Override |
| 49 | + public void insertUpdate(DocumentEvent e) { |
| 50 | + updateValidity(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void removeUpdate(DocumentEvent e) { |
| 55 | + updateValidity(); |
| 56 | + } |
| 57 | + @Override |
| 58 | + public void changedUpdate(DocumentEvent e) {} |
| 59 | + |
| 60 | + private void updateValidity(){ |
| 61 | + boolean valid = nameField.getText().matches("\\w+"); |
| 62 | + create.setEnabled(valid); |
| 63 | + nameField.setToolTipText("Profile names can only contain letters, numbers, and underscores."); |
| 64 | + forceTooltip(valid); |
| 65 | + if (!valid) |
| 66 | + return; |
| 67 | + boolean exists = Files.exists(Paths.get(Cogfly.settings.profileSavePath).resolve(nameField.getText())); |
| 68 | + create.setEnabled(!exists); |
| 69 | + nameField.setToolTipText("A profile with this name already exists in the profile save folder."); |
| 70 | + forceTooltip(exists); |
| 71 | + } |
| 72 | + |
| 73 | + private void forceTooltip(boolean show){ |
| 74 | + if (show){ |
| 75 | + ToolTipManager toolTipManager = ToolTipManager.sharedInstance(); |
| 76 | + toolTipManager.setInitialDelay(0); |
| 77 | + MouseEvent phantomEvent = new MouseEvent( |
| 78 | + nameField, |
| 79 | + MouseEvent.MOUSE_MOVED, |
| 80 | + System.currentTimeMillis(), |
| 81 | + 0, |
| 82 | + nameField.getWidth() / 2, |
| 83 | + nameField.getHeight() / 2, |
| 84 | + 0, |
| 85 | + false |
| 86 | + ); |
| 87 | + toolTipManager.mouseMoved(phantomEvent); |
| 88 | + } else { |
| 89 | + ToolTipManager ttm = ToolTipManager.sharedInstance(); |
| 90 | + |
| 91 | + MouseEvent exitEvent = new MouseEvent( |
| 92 | + nameField, |
| 93 | + MouseEvent.MOUSE_EXITED, |
| 94 | + System.currentTimeMillis(), |
| 95 | + 0, |
| 96 | + -1, |
| 97 | + -1, |
| 98 | + 0, |
| 99 | + false |
| 100 | + ); |
| 101 | + |
| 102 | + ttm.mouseExited(exitEvent); |
| 103 | + } |
| 104 | + } |
| 105 | + }); |
14 | 106 |
|
| 107 | + button.addActionListener(_ -> Utils.pickFile((path) -> button.setText(path.toString()), "*", "png", "jpg", "jpeg", "gif")); |
| 108 | + create.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); |
| 109 | + holder.add(name, BorderLayout.WEST); |
| 110 | + holder.add(nameField, BorderLayout.EAST); |
| 111 | + prompt.add(holder, BorderLayout.NORTH); |
| 112 | + prompt.add(extraHolder, BorderLayout.CENTER); |
| 113 | + prompt.add(create, BorderLayout.SOUTH); |
| 114 | + prompt.setVisible(true); |
| 115 | + } |
15 | 116 | private final JPanel parentPanel; |
16 | 117 | private final JScrollPane pane; |
17 | 118 | public ProfilesScreenElement() { |
@@ -98,40 +199,7 @@ public ProfilesScreenElement() { |
98 | 199 |
|
99 | 200 |
|
100 | 201 | JButton createProfile = new JButton("Create Profile"); |
101 | | - createProfile.addActionListener(_ -> { |
102 | | - JDialog prompt = new JDialog(FrameManager.getOrCreate().frame); |
103 | | - prompt.setModal(true); |
104 | | - prompt.setSize(new Dimension(300, 150)); |
105 | | - prompt.setResizable(false); |
106 | | - prompt.setLocationRelativeTo(null); |
107 | | - prompt.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
108 | | - JPanel holder = new JPanel(); |
109 | | - JLabel name = new JLabel("Name: "); |
110 | | - JTextField nameField = new JTextField(""); |
111 | | - JLabel icon = new JLabel("Icon (optional): "); |
112 | | - JButton button = new JButton("Click here to select a file"); |
113 | | - JPanel extraHolder = new JPanel(); |
114 | | - extraHolder.add(icon); |
115 | | - extraHolder.add(button); |
116 | | - |
117 | | - JButton create = new JButton("Create"); |
118 | | - create.setPreferredSize(new Dimension(50, 20)); |
119 | | - create.addActionListener(_ -> { |
120 | | - prompt.dispose(); |
121 | | - ProfileManager.createProfile(nameField.getText(), |
122 | | - button.getText().equals("Click here to select a file") ? "" : |
123 | | - button.getText()); |
124 | | - drawProfiles(); |
125 | | - }); |
126 | | - button.addActionListener(_ -> Utils.pickFile((path) -> button.setText(path.toString()), "*", "png", "jpg", "jpeg", "gif")); |
127 | | - create.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); |
128 | | - holder.add(name, BorderLayout.WEST); |
129 | | - holder.add(nameField, BorderLayout.EAST); |
130 | | - prompt.add(holder, BorderLayout.NORTH); |
131 | | - prompt.add(extraHolder, BorderLayout.CENTER); |
132 | | - prompt.add(create, BorderLayout.SOUTH); |
133 | | - prompt.setVisible(true); |
134 | | - }); |
| 202 | + createProfile.addActionListener(_ -> promptCreation(() -> {})); |
135 | 203 |
|
136 | 204 | upperPanel.add(createProfile); |
137 | 205 | upperPanel.add(launchVanilla); |
|
0 commit comments