Skip to content

Commit

Permalink
Project Dialog: Fix action enablement; select file from the input
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Jun 19, 2024
1 parent 926f10e commit 8334324
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public JComponent createComponent() {
final PathPickerDialog dialog = new PathPickerDialog("Choose target file", root);

if (dialog.showDialog(window) == BaseDialog.BUTTON_OK) {
refPathText.setText(dialog.getPath());
return dialog.getPath();
} else {
return null;
}
});

Expand All @@ -91,8 +93,9 @@ public JComponent createComponent() {
final EntryPickerDialog dialog = new EntryPickerDialog("Choose target entry", window, controller.getProject(), path);

if (dialog.file != null && dialog.showDialog(window) == BaseDialog.BUTTON_OK) {
final RTTIObject uuid = dialog.getUUID();
refUuidText.setText(RTTIUtils.uuidToString(uuid));
return RTTIUtils.uuidToString(dialog.getUUID());
} else {
return null;
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ public ProjectEditDialog(boolean persisted, boolean editable) {
this.projectId.setEditable(false);

this.projectName = new JTextField();
this.projectName.setEnabled(editable);
this.projectName.setEditable(editable);

this.projectType = new JComboBox<>(GameType.values());
this.projectType.setEnabled(editable);

this.executableFilePath = new JTextField();
this.executableFilePath.setEnabled(editable);
this.executableFilePath.setEditable(editable);
this.executableFilePath.getDocument().addDocumentListener((DocumentAdapter) e -> {
if (UIUtils.isValid(executableFilePath)) {
fillValuesBasedOnGameExecutable(Path.of(executableFilePath.getText()));
}
});

this.archiveFolderPath = new JTextField();
this.archiveFolderPath.setEnabled(editable);
this.archiveFolderPath.setEditable(editable);

this.compressorPath = new JTextField();
this.compressorPath.setEnabled(editable);
this.compressorPath.setEditable(editable);

this.compressorNote = new ColoredComponent();
this.compressorNote.setVisible(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.lang.reflect.Field;
import java.time.Duration;
import java.util.Objects;
import java.util.function.Function;

public final class UIUtils {
private UIUtils() {
Expand Down Expand Up @@ -117,55 +119,77 @@ public static void addOpenFileAction(@NotNull JTextField component, @NotNull Str
chooser.setFileFilter(filter);
chooser.setAcceptAllFileFilterUsed(false);

trySetSelectedFileFromInput(component, chooser);

if (chooser.showOpenDialog(component) == JFileChooser.APPROVE_OPTION) {
component.setText(chooser.getSelectedFile().toString());
return chooser.getSelectedFile().toString();
} else {
return null;
}
});
}

public static void addOpenAction(@NotNull JTextField component, @NotNull ActionListener delegate) {
addAction(component, UIManager.getIcon("Tree.openIcon"), delegate);
public static void addOpenDirectoryAction(@NotNull JTextField component, @NotNull String title) {
addOpenAction(component, e -> {
final JFileChooser chooser = new JFileChooser();

chooser.setDialogTitle(title);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

trySetSelectedFileFromInput(component, chooser);

if (chooser.showOpenDialog(component) == JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile().toString();
} else {
return null;
}
});
}

private static void trySetSelectedFileFromInput(@NotNull JTextField component, @NotNull JFileChooser chooser) {
String text = component.getText();
if (text.isBlank()) {
return;
}
try {
chooser.setSelectedFile(new File(text));
} catch (Exception ignored) {
}
}

public static void addOpenAction(@NotNull JTextField component, @NotNull Function<ActionEvent, String> callback) {
addAction(component, UIManager.getIcon("Tree.openIcon"), true, e -> {
String text = callback.apply(e);
if (text != null) {
component.setText(text);
}
});
}

public static void addCopyAction(@NotNull JTextField component) {
addAction(component, UIManager.getIcon("Action.copyIcon"), e -> {
addAction(component, UIManager.getIcon("Action.copyIcon"), false, e -> {
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
final StringSelection contents = new StringSelection(component.getText());
clipboard.setContents(contents, contents);
});
}

public static void addAction(@NotNull JTextField component, @NotNull Icon icon, @NotNull ActionListener delegate) {
private static void addAction(@NotNull JTextField component, @NotNull Icon icon, boolean trackEnabledAndEditable, @NotNull ActionListener delegate) {
final AbstractAction action = new AbstractAction(null, icon) {
@Override
public void actionPerformed(ActionEvent e) {
delegate.actionPerformed(e);
}
};
action.setEnabled(component.isEnabled());

final JToolBar toolBar = new JToolBar();
JToolBar toolBar = new JToolBar();
toolBar.add(action);

component.putClientProperty(FlatClientProperties.TEXT_FIELD_TRAILING_COMPONENT, toolBar);
component.addPropertyChangeListener("enabled", e -> action.setEnabled(component.isEnabled()));
}

public static void addOpenDirectoryAction(@NotNull JTextField component, @NotNull String title) {
addOpenAction(component, e -> {
final JFileChooser chooser = new JFileChooser();

chooser.setDialogTitle(title);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

if (chooser.showOpenDialog(component) == JFileChooser.APPROVE_OPTION) {
component.setText(chooser.getSelectedFile().toString());
}
});
}

public static void delegateKey(@NotNull JComponent source, int sourceKeyCode, @NotNull JComponent target, @NotNull String targetActionKey) {
delegateAction(source, KeyStroke.getKeyStroke(sourceKeyCode, 0), target, targetActionKey);
if (trackEnabledAndEditable) {
toolBar.setVisible(component.isEnabled() && component.isEditable());
component.addPropertyChangeListener("enabled", e -> toolBar.setVisible(component.isEnabled() && component.isEditable()));
}
}

public static void delegateAction(@NotNull JComponent source, @NotNull JComponent target, @NotNull String targetActionKey, int targetCondition) {
Expand Down

0 comments on commit 8334324

Please sign in to comment.