|
| 1 | +package com.dengzii.plugin.template.ui; |
| 2 | + |
| 3 | +import com.dengzii.plugin.template.model.FileTreeNode; |
| 4 | + |
| 5 | +import javax.swing.*; |
| 6 | +import java.awt.*; |
| 7 | +import java.awt.event.KeyAdapter; |
| 8 | +import java.awt.event.KeyEvent; |
| 9 | + |
| 10 | +public class CreateFileDialog extends JDialog { |
| 11 | + private JPanel contentPane; |
| 12 | + private JComboBox comboBox1; |
| 13 | + private JTextField textField1; |
| 14 | + private JLabel label1; |
| 15 | + private JLabel label2; |
| 16 | + |
| 17 | + private CreateFileCallback createFileCallback; |
| 18 | + private boolean isDir; |
| 19 | + private FileTreeNode parent; |
| 20 | + private FileTreeNode current; |
| 21 | + |
| 22 | + public static void showForRename(FileTreeNode node, CreateFileCallback createFileCallback) { |
| 23 | + CreateFileDialog createFileDialog = new CreateFileDialog(); |
| 24 | + createFileDialog.createFileCallback = createFileCallback; |
| 25 | + createFileDialog.isDir = node.isDir(); |
| 26 | + createFileDialog.parent = node.getParent(); |
| 27 | + createFileDialog.current = node; |
| 28 | + createFileDialog.initDialog(); |
| 29 | + } |
| 30 | + |
| 31 | + public static void showForCreate(FileTreeNode parent, boolean isDir, CreateFileCallback createFileCallback) { |
| 32 | + CreateFileDialog createFileDialog = new CreateFileDialog(); |
| 33 | + createFileDialog.createFileCallback = createFileCallback; |
| 34 | + createFileDialog.isDir = isDir; |
| 35 | + createFileDialog.parent = parent; |
| 36 | + createFileDialog.initDialog(); |
| 37 | + } |
| 38 | + |
| 39 | + private CreateFileDialog() { |
| 40 | + setContentPane(contentPane); |
| 41 | + setModal(true); |
| 42 | + setDefaultCloseOperation(DISPOSE_ON_CLOSE); |
| 43 | + |
| 44 | + contentPane.registerKeyboardAction(e -> { |
| 45 | + }, |
| 46 | + KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), |
| 47 | + JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); |
| 48 | + } |
| 49 | + |
| 50 | + private void initDialog() { |
| 51 | + |
| 52 | + if (isDir) { |
| 53 | + label1.setVisible(false); |
| 54 | + comboBox1.setVisible(false); |
| 55 | + } |
| 56 | + pack(); |
| 57 | + |
| 58 | + Dimension screen = getToolkit().getScreenSize(); |
| 59 | + int w = getWidth(); |
| 60 | + int h = getHeight(); |
| 61 | + int x = screen.width / 2 - w / 2; |
| 62 | + int y = screen.height / 2 - h / 2 - 100; |
| 63 | + setLocation(x, y); |
| 64 | + setPreferredSize(new Dimension(w, h)); |
| 65 | + |
| 66 | + setTitle((isRename() ? "New " : "Rename ") + (isDir ? "Directory" : "File")); |
| 67 | + if (isRename()) { |
| 68 | + textField1.setText(current.getRealName()); |
| 69 | + } |
| 70 | + textField1.addKeyListener(new KeyAdapter() { |
| 71 | + @Override |
| 72 | + public void keyReleased(KeyEvent e) { |
| 73 | + super.keyReleased(e); |
| 74 | + if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
| 75 | + if (parent != null && parent.hasChild(textField1.getText(), isDir)) { |
| 76 | + label2.setText(label2.getText() + " (already exist.)"); |
| 77 | + return; |
| 78 | + } |
| 79 | + if (isRename()) { |
| 80 | + current.setName(textField1.getText()); |
| 81 | + createFileCallback.callback(current); |
| 82 | + } else { |
| 83 | + createFileCallback.callback(new FileTreeNode(parent, textField1.getText(), isDir)); |
| 84 | + } |
| 85 | + |
| 86 | + dispose(); |
| 87 | + } |
| 88 | + } |
| 89 | + }); |
| 90 | + setVisible(true); |
| 91 | + textField1.requestFocus(); |
| 92 | + } |
| 93 | + |
| 94 | + private boolean isRename() { |
| 95 | + return current != null; |
| 96 | + } |
| 97 | + |
| 98 | + public interface CreateFileCallback { |
| 99 | + void callback(FileTreeNode fileTreeNode); |
| 100 | + } |
| 101 | + |
| 102 | + public static void main(String[] args) { |
| 103 | + CreateFileDialog.showForCreate(null, true, fileTreeNode -> { |
| 104 | + System.out.println("CreateFileDialog.main " + fileTreeNode.getName()); |
| 105 | + }); |
| 106 | + } |
| 107 | +} |
0 commit comments