-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25a3f4a
commit babfeba
Showing
27 changed files
with
838 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/nl/andrewl/email_indexer/browser/control/DirectoryFileFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package nl.andrewl.email_indexer.browser.control; | ||
|
||
import javax.swing.filechooser.FileFilter; | ||
import java.io.File; | ||
|
||
/** | ||
* A file filter that only allows users to select directories. | ||
*/ | ||
public class DirectoryFileFilter extends FileFilter { | ||
@Override | ||
public boolean accept(File f) { | ||
return f.isDirectory(); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Directories"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/nl/andrewl/email_indexer/browser/control/PathSelectField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package nl.andrewl.email_indexer.browser.control; | ||
|
||
import javax.swing.*; | ||
import javax.swing.event.MouseInputAdapter; | ||
import javax.swing.filechooser.FileFilter; | ||
import javax.swing.filechooser.FileNameExtensionFilter; | ||
import java.awt.*; | ||
import java.awt.event.MouseEvent; | ||
import java.awt.event.MouseListener; | ||
import java.nio.file.Path; | ||
|
||
public class PathSelectField extends JPanel { | ||
private final JTextField pathField; | ||
private final JButton selectPathButton; | ||
private final MouseListener pathFieldMouseListener; | ||
|
||
private final int fileSelectionMode; | ||
private final boolean acceptAll; | ||
private final FileFilter fileFilter; | ||
private Path selectedPath = null; | ||
|
||
public PathSelectField(int fileSelectionMode, boolean acceptAll, FileFilter filter) { | ||
super(new BorderLayout()); | ||
this.fileSelectionMode = fileSelectionMode; | ||
this.acceptAll = acceptAll; | ||
this.fileFilter = filter; | ||
|
||
pathField = new JTextField(0); | ||
pathField.setMinimumSize(new Dimension(100, 30)); | ||
pathField.setEditable(false); | ||
add(pathField, BorderLayout.CENTER); | ||
|
||
selectPathButton = new JButton("Select file..."); | ||
selectPathButton.setMinimumSize(new Dimension(50, 30)); | ||
add(selectPathButton, BorderLayout.EAST); | ||
|
||
selectPathButton.addActionListener(e -> selectFile()); | ||
pathFieldMouseListener = new MouseInputAdapter() { | ||
@Override | ||
public void mouseClicked(MouseEvent e) { | ||
if (e.getClickCount() == 2 && e.getButton() == 1) { | ||
selectFile(); | ||
} | ||
} | ||
}; | ||
pathField.addMouseListener(pathFieldMouseListener); | ||
} | ||
|
||
public static PathSelectField directorySelectField() { | ||
return new PathSelectField(JFileChooser.DIRECTORIES_ONLY, false, new DirectoryFileFilter()); | ||
} | ||
|
||
public static PathSelectField fileTypeSelectField(String extension, String name) { | ||
return new PathSelectField(JFileChooser.FILES_ONLY, false, new FileNameExtensionFilter(name, extension)); | ||
} | ||
|
||
public void setEnabled(boolean enabled) { | ||
selectPathButton.setEnabled(enabled); | ||
if (enabled) { | ||
pathField.addMouseListener(pathFieldMouseListener); | ||
} else { | ||
pathField.removeMouseListener(pathFieldMouseListener); | ||
} | ||
} | ||
|
||
private void selectFile() { | ||
JFileChooser fc = new JFileChooser(selectedPath == null ? Path.of(".").toFile() : selectedPath.toFile()); | ||
fc.setFileSelectionMode(fileSelectionMode); | ||
fc.setFileFilter(fileFilter); | ||
fc.setAcceptAllFileFilterUsed(acceptAll); | ||
int result = fc.showDialog(this, "Select"); | ||
if (result == JFileChooser.APPROVE_OPTION) { | ||
setSelectPath(fc.getSelectedFile().toPath()); | ||
} | ||
} | ||
|
||
public void setSelectPath(Path p) { | ||
selectedPath = p; | ||
String text = p == null ? null : p.toAbsolutePath().toString(); | ||
pathField.setText(text); | ||
} | ||
|
||
public Path getSelectedPath() { | ||
return selectedPath; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/nl/andrewl/email_indexer/browser/control/SwingUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package nl.andrewl.email_indexer.browser.control; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.util.Random; | ||
|
||
public final class SwingUtils { | ||
public static final Random random = new Random(); | ||
private SwingUtils() {} | ||
|
||
public static void setAllButtonsEnabled(Container c, boolean enabled) { | ||
for (var component : c.getComponents()) { | ||
if (component instanceof JButton button) { | ||
button.setEnabled(enabled); | ||
} else if (component instanceof Container nested) { | ||
setAllButtonsEnabled(nested, enabled); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T extends Component> T findFirstInstance(Container c, Class<T> type) { | ||
for (var component: c.getComponents()) { | ||
if (component.getClass().equals(type)) { | ||
return (T) component; | ||
} else if (component instanceof Container nested) { | ||
T result = findFirstInstance(nested, type); | ||
if (result != null) return result; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static Color getColor(String text) { | ||
random.setSeed(text.hashCode()); | ||
float hue = random.nextFloat(); | ||
float saturation = random.nextFloat() / 4f + 0.75f; | ||
float luminance = 0.9f; | ||
return Color.getHSBColor(hue, saturation, luminance); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/nl/andrewl/email_indexer/browser/control/email/EmailAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package nl.andrewl.email_indexer.browser.control.email; | ||
|
||
import nl.andrewl.email_indexer.browser.email.EmailViewListener; | ||
import nl.andrewl.email_indexer.browser.email.EmailViewPanel; | ||
import nl.andrewl.email_indexer.data.EmailEntry; | ||
|
||
import javax.swing.*; | ||
|
||
public abstract class EmailAction extends AbstractAction implements EmailViewListener { | ||
protected final EmailViewPanel emailViewPanel; | ||
|
||
protected EmailAction(String name, EmailViewPanel emailViewPanel) { | ||
super(name); | ||
this.emailViewPanel = emailViewPanel; | ||
emailViewPanel.addListener(this); | ||
} | ||
|
||
@Override | ||
public void emailUpdated(EmailEntry email) { | ||
setEnabled(email != null && shouldBeEnabled(email)); | ||
} | ||
|
||
protected abstract boolean shouldBeEnabled(EmailEntry email); | ||
} |
Oops, something went wrong.