Skip to content

Commit

Permalink
user input dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
subhra74 committed Dec 19, 2023
1 parent 8a958bf commit 6f316ed
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 40 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/muon/App.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package muon;

import muon.screens.appwin.MainContainer;
import muon.screens.appwin.UserInputDialog;
import muon.screens.sessiontabs.InputBlockerDialog;
import muon.service.UserInputService;
import muon.service.UserInputServiceImpl;
import muon.styles.AppTheme;
import muon.styles.FlatLookAndFeel;
import muon.util.AppUtils;
Expand All @@ -24,11 +27,16 @@
public class App {

private static InputBlockerDialog inputBlockerDialog;
private static UserInputService userInputService;

public static InputBlockerDialog getInputBlockerDialog() {
return inputBlockerDialog;
}

public static UserInputService getUserInputService() {
return userInputService;
}

public static void main(String[] args) throws InterruptedException, InvocationTargetException, UnsupportedLookAndFeelException, ClassNotFoundException, InstantiationException, IllegalAccessException {
System.setProperty("sun.java2d.metal", "false");
System.setProperty("apple.awt.application.appearance", "system");
Expand All @@ -38,6 +46,7 @@ public static void main(String[] args) throws InterruptedException, InvocationTa

var f = isWindows ? new CustomFrame("Muon 1.0.23") : new JFrame("Muon 1.0.23");
inputBlockerDialog = new InputBlockerDialog(f);
userInputService = new UserInputServiceImpl(new UserInputDialog(f));

f.setSize(AppUtils.calculateDefaultWindowSize());
f.setLocationRelativeTo(null);
Expand Down
118 changes: 118 additions & 0 deletions app/src/main/java/muon/screens/appwin/UserInputDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package muon.screens.appwin;

import muon.util.IconCode;
import muon.util.IconFont;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UserInputDialog extends JDialog {
private JLabel lblUser;
private java.util.List<String> inputs;
private List<JPasswordField> txtInputs;
private JPanel userInputContainer;
private JButton loginButton;
private JPanel container;

public UserInputDialog(Window window) {
super(window);
initUi();
setContentPane(container);
addWindowListener(new WindowAdapter() {
@Override
public void windowActivated(WindowEvent e) {
txtInputs.get(0).requestFocusInWindow();
}
});
}

public synchronized List<String> getInputs(String label, String[] prompt, boolean[] echo) {
inputs = Collections.emptyList();
try {
SwingUtilities.invokeAndWait(() -> {
showPrompt(label, prompt, echo);
});
} catch (Exception ex) {
ex.printStackTrace();
}
return inputs;
}

private void initUi() {
container = new JPanel(new GridBagLayout());
loginButton = new JButton("Login");
loginButton.addActionListener(e -> {
inputs = this.txtInputs.stream().map(txt -> new String(txt.getPassword())).toList();
setVisible(false);
});

var iconLabel = new JLabel();
iconLabel.setFont(IconFont.getSharedInstance().getIconFont(48.0f));
iconLabel.setText(IconCode.RI_ACCOUNT_CIRCLE_FILL.getValue());

lblUser = new JLabel();
lblUser.setText("user");
lblUser.setBorder(new EmptyBorder(0, 0, 8, 0));
lblUser.setFont(new Font(Font.DIALOG, Font.BOLD, 16));
lblUser.setAlignmentX(Component.LEFT_ALIGNMENT);

var iconPanel = new JPanel(new BorderLayout(10, 0));
iconPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
iconPanel.add(iconLabel, BorderLayout.WEST);
iconPanel.add(lblUser, BorderLayout.CENTER);

var hbox = Box.createHorizontalBox();
hbox.add(Box.createHorizontalGlue());
hbox.add(loginButton);
hbox.setMaximumSize(new Dimension(Short.MAX_VALUE, hbox.getPreferredSize().height));
hbox.setAlignmentX(Component.LEFT_ALIGNMENT);

userInputContainer = new JPanel();
userInputContainer.setAlignmentX(Component.LEFT_ALIGNMENT);
userInputContainer.setLayout(new BoxLayout(userInputContainer, BoxLayout.Y_AXIS));

var vbox = Box.createVerticalBox();
vbox.add(iconPanel);
vbox.add(Box.createRigidArea(new Dimension(10, 20)));
vbox.add(userInputContainer);
vbox.add(Box.createRigidArea(new Dimension(10, 10)));
vbox.add(hbox);

container.add(vbox, new GridBagConstraints());
}

private void showPrompt(String label, String[] prompt, boolean[] echo) {
var len = prompt.length;
lblUser.setText(label);
txtInputs = new ArrayList<>(len);
userInputContainer.removeAll();
userInputContainer.setAlignmentX(Component.LEFT_ALIGNMENT);

for (var i = 0; i < len; i++) {
var txtInput = new JPasswordField(20);
txtInput.addActionListener(e -> {
loginButton.doClick();
});
txtInput.setAlignmentX(Component.LEFT_ALIGNMENT);
if (echo[i]) {
txtInput.setEchoChar('*');
}
txtInputs.add(txtInput);
var lbl = new JLabel(prompt[i]);
lbl.setBorder(new EmptyBorder(0, 0, 10, 0));
lbl.setHorizontalAlignment(JLabel.LEFT);
lbl.setAlignmentX(Component.LEFT_ALIGNMENT);
userInputContainer.add(lbl);
userInputContainer.add(txtInput);
}
this.pack();
this.setLocationRelativeTo(this.getOwner());
this.setVisible(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ public AbstractFileBrowserView(FileBrowserViewParent parent) {
this.popup = new JPopupMenu();

createContentPanel();
inputBlockerPanel = new InputBlockerPanel(e -> {
navigate();
});
inputBlockerPanel = new InputBlockerPanel(
e -> {
navigate();
},
e -> {
inputBlockerPanel.unblockInput();
});
inputBlockerPanel.setVisible(false);

this.add(contentPanel, Integer.valueOf(1));
Expand Down Expand Up @@ -296,7 +300,7 @@ protected void navigate(final String path) {
inputBlockerPanel.blockInput();
AppUtils.runAsync(() -> {
try {
if(!isConnected()){
if (!isConnected()) {
connect();
}
var folder = StringUtils.isEmpty(path) ? getHome() : path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public void exportAsDrag(JComponent comp, InputEvent e, int action) {

@Override
public boolean canImport(TransferSupport support) {
System.out.println("canImport");
for (DataFlavor f : support.getDataFlavors()) {
System.out.println(f);
if (f.isFlavorJavaFileListType()) {
return true;
}
Expand All @@ -58,7 +56,6 @@ public boolean canImport(TransferSupport support) {
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("drop not supported");
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public void cleanup() {


protected boolean handleDrop(DndTransferData transferData) {
System.out.println(transferData);
try {
int sessionCode = transferData.getInfo();
System.out.println("Session code: " + sessionCode);
Expand All @@ -78,7 +77,6 @@ protected boolean handleDrop(DndTransferData transferData) {
} catch (Exception ex) {
ex.printStackTrace();
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package muon.screens.appwin.tabs.filebrowser.transfer.foreground;

import muon.dto.file.FileInfo;
import muon.styles.AppTheme;
import muon.util.IconCode;
import muon.util.IconFont;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class FileListRenderer<F> implements ListCellRenderer<FileInfo> {
private JPanel panel1, panel2;
private JLabel lblIcon, lblText, lblInfo;

public FileListRenderer() {
panel1 = new JPanel(new BorderLayout(5, 10));
panel1.setBorder(new EmptyBorder(0, 0, 5, 0));
lblIcon = new JLabel();
lblIcon.setForeground(AppTheme.INSTANCE.getDarkForeground());
lblIcon.setFont(IconFont.getSharedInstance().getIconFont(32.0f));

lblText = new JLabel();
lblText.setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
lblText.setForeground(AppTheme.INSTANCE.getForeground());

lblInfo = new JLabel();
lblInfo.setFont(new Font(Font.DIALOG, Font.PLAIN, 11));
lblInfo.setForeground(AppTheme.INSTANCE.getDarkForeground());

panel2 = new JPanel(new BorderLayout(5, 0));

panel2.add(lblText, BorderLayout.NORTH);
panel2.add(lblInfo);

panel1.add(lblIcon, BorderLayout.WEST);
panel1.add(panel2);

}

@Override
public Component getListCellRendererComponent(JList<? extends FileInfo> list, FileInfo value, int index, boolean isSelected, boolean cellHasFocus) {
lblIcon.setText(value.isDirectory() ? IconCode.RI_FOLDER_FILL.getValue() : IconCode.RI_FILE_FILL.getValue());
lblText.setText(value.getName());
lblInfo.setText(value.isDirectory() ? "Folder" : value.getSize() + "");
return panel1;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package muon.screens.appwin.tabs.filebrowser.transfer.foreground;

import muon.dto.file.FileInfo;
import muon.exceptions.FSAccessException;
import muon.service.GuiUserAuthFactory;
import muon.service.SftpUploadTask;
Expand All @@ -13,7 +14,9 @@
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class ForegroundTransferProgressPanel extends JLayeredPane {
private InputBlockerPanel inputBlockerPanel;
Expand All @@ -27,8 +30,13 @@ public class ForegroundTransferProgressPanel extends JLayeredPane {
public ForegroundTransferProgressPanel(Consumer<Boolean> result) {
this.result = result;

inputBlockerPanel = new InputBlockerPanel(e -> {
});
inputBlockerPanel = new InputBlockerPanel(
e -> {
},
e -> {
inputBlockerPanel.unblockInput();
result.accept(false);
});
inputBlockerPanel.setVisible(false);

createContentPanel();
Expand All @@ -51,11 +59,30 @@ public void componentResized(ComponentEvent e) {

public void setSftpUploadTask(SftpUploadTask sftpUploadTask) {
this.sftpUploadTask = sftpUploadTask;
this.transferProgressPanel.setProgress(0);
var folderCount = this.sftpUploadTask.getLocalFiles().stream().filter(FileInfo::isDirectory).count();
var fileCount = this.sftpUploadTask.getLocalFiles().size() - folderCount;
var texts = new ArrayList<String>();
texts.add("Transfer");
if (folderCount > 0) {
texts.add(folderCount + " folder" + (folderCount > 1 ? "s" : ""));
}
if (fileCount > 0) {
if (folderCount > 0) {
texts.add("and");
}
texts.add(fileCount + " file" + (fileCount > 1 ? "s" : ""));
}
var remoteFileNames = sftpUploadTask.getRemoteFiles().stream().map(FileInfo::getName).collect(Collectors.toSet());
var conflictingFiles = sftpUploadTask.getLocalFiles().stream().filter(
f -> remoteFileNames.contains(f.getName())).toList();
this.transferConfirmPanel.setFileInfo(String.format(String.join(" ", texts)),
this.sftpUploadTask.getRemoteFolder(), conflictingFiles);
}

private void createContentPanel() {
contentPanel = new JPanel(new CardLayout());
transferConfirmPanel = new TransferConfirmPanel(e -> transfer(), null);
transferConfirmPanel = new TransferConfirmPanel(e -> transfer(), e -> result.accept(false));
transferRetryPanel = new TransferRetryPanel(null, null);
transferProgressPanel = new TransferProgressPanel(null);
contentPanel.add(transferConfirmPanel, "TransferConfirmPanel");
Expand Down Expand Up @@ -97,11 +124,7 @@ private void transfer() {
result.accept(true);
} catch (Exception ex) {
ex.printStackTrace();
if (ex instanceof FSAccessException) {
inputBlockerPanel.showError();
} else {
inputBlockerPanel.showRetryOption();
}
inputBlockerPanel.showRetryOption();
}
});
}
Expand Down
Loading

0 comments on commit 6f316ed

Please sign in to comment.