Skip to content

Commit

Permalink
Improvements to the CellEditDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Xoriun committed Mar 17, 2022
1 parent 41b740b commit 5266c8d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 75 deletions.
170 changes: 100 additions & 70 deletions src/edit/CellEditDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
Expand All @@ -28,6 +29,8 @@
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.MouseInputAdapter;

import gui.Abbreviations;
Expand Down Expand Up @@ -65,7 +68,8 @@ public class CellEditDialog
private static String[] possibleActionsArray = new String[] {"", "text_to_clipboard", "file_to_clipboard"};

public static boolean iconEditEditorOpen = false;
private static boolean selectedByUser = true;

private static String[] abbreviationsAndFileNamesList;

public static void initializeCellEditDialog()
{
Expand Down Expand Up @@ -442,6 +446,8 @@ private static ActionListener getIconEditListener()
JLabel main_image_label;
JLabel layered_image_label;

Dimension combobox_main_dim, combobox_layered_dim;

private void closeDialog(JDialog dialog)
{
iconEditEditorOpen = false;
Expand All @@ -451,6 +457,8 @@ private void closeDialog(JDialog dialog)
@Override
public void actionPerformed(ActionEvent ae)
{
updateAbbreviationsAndFileNamesList();

iconEditEditorOpen = true;

main_image_abbr = ( (EditIconLabel) selectedCellPanel).getMainImageAbbr();
Expand Down Expand Up @@ -498,52 +506,42 @@ public void actionPerformed(ActionEvent ae)
gbc_main.fill = GridBagConstraints.HORIZONTAL;
gbc_main.gridx = gbc_main.gridy = 0;

main_image_edit_panel.add(GuiHelper.getAlignedNonOpaqueJLabelWithCurrentColors("Abbreviations:", GuiHelper.LEFT), gbc_main);
main_image_edit_panel.add(GuiHelper.getAlignedNonOpaqueJLabelWithCurrentColors("List:", GuiHelper.LEFT), gbc_main);

gbc_main.gridx ++;
JComboBox<String> abbr_list_main = new JComboBox<String>(Abbreviations.getArrayOfAbbreviations() );
abbr_list_main.setSelectedItem(main_image_abbr);
main_image_edit_panel.add(abbr_list_main, gbc_main);
JComboBox<String> combobox_main = new JComboBox<String>(abbreviationsAndFileNamesList);
combobox_main.setSelectedItem(getLowercaseStringWithFirstCharCapitablized(main_image_abbr) );
main_image_edit_panel.add(combobox_main, gbc_main);

gbc_main.gridy ++;
gbc_main.gridx = 0;
main_image_edit_panel.add(GuiHelper.getAlignedNonOpaqueJLabelWithCurrentColors("Images:", GuiHelper.LEFT), gbc_main);
main_image_edit_panel.add(GuiHelper.getAlignedNonOpaqueJLabelWithCurrentColors("Filter:", GuiHelper.LEFT), gbc_main);

gbc_main.gridx ++;
JComboBox<String> images_list_main = new JComboBox<String>(FileOperations.getNamesOfImagesInImagesDirectory() );
images_list_main.setSelectedItem(main_image_abbr);
main_image_edit_panel.add(images_list_main, gbc_main);
JTextField filter_textFiled_main = new JTextField();
main_image_edit_panel.add(filter_textFiled_main, gbc_main);

abbr_list_main.addItemListener(new ItemListener() {
combobox_main.addActionListener(new ActionListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(selectedByUser && e.getStateChange() == ItemEvent.SELECTED)
{
selectedByUser = false;
images_list_main.setSelectedIndex(0);
selectedByUser = true;
main_image_abbr = (String) abbr_list_main.getSelectedItem();
main_image_label.setIcon(GuiHelper.getScaledImageIconFromAbbreviation(main_image_abbr) );
icon_dialog.pack();
}
public void actionPerformed(ActionEvent e)
{
main_image_abbr = (String) combobox_main.getSelectedItem();
main_image_label.setIcon(GuiHelper.getScaledImageIconFromAbbreviation(main_image_abbr) );
icon_dialog.pack();
}
});
images_list_main.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(selectedByUser && e.getStateChange() == ItemEvent.SELECTED)
{
selectedByUser = false;
abbr_list_main.setSelectedIndex(0);
selectedByUser = true;
main_image_abbr = (String) images_list_main.getSelectedItem();
main_image_label.setIcon(GuiHelper.getScaledImageIconFromAbbreviation(main_image_abbr) );
icon_dialog.pack();
}
}
filter_textFiled_main.getDocument().addDocumentListener(new DocumentListener() {
@Override public void removeUpdate(DocumentEvent e) { update(); }
@Override public void insertUpdate(DocumentEvent e) { update(); }
@Override public void changedUpdate(DocumentEvent e) { update(); }

private void update()
{
combobox_main.setModel(new DefaultComboBoxModel<>(getFilteredAbbreviationsAndFileNamesList(filter_textFiled_main.getText() ) ) );
combobox_main.setPreferredSize(combobox_main_dim);
}
});


content_panel.add(main_image_edit_panel, gbc);

// layered?
Expand Down Expand Up @@ -583,52 +581,46 @@ public void itemStateChanged(ItemEvent e) {
gbc_layered.fill = GridBagConstraints.HORIZONTAL;
gbc_layered.gridx = gbc_layered.gridy = 0;

layered_image_edit_panel.add(GuiHelper.getAlignedNonOpaqueJLabelWithCurrentColors("Abbreviations:", GuiHelper.LEFT), gbc_layered);
layered_image_edit_panel.add(GuiHelper.getAlignedNonOpaqueJLabelWithCurrentColors("List:", GuiHelper.LEFT), gbc_layered);

gbc_layered.gridx ++;
JComboBox<String> abbr_list_layered = new JComboBox<String>(Abbreviations.getArrayOfAbbreviations() );
abbr_list_layered.setSelectedItem(layered_image_abbr);
layered_image_edit_panel.add(abbr_list_layered, gbc_layered);
JComboBox<String> combobox_layered = new JComboBox<String>(abbreviationsAndFileNamesList);
combobox_layered.setSelectedItem(getLowercaseStringWithFirstCharCapitablized(layered_image_abbr) );
layered_image_edit_panel.add(combobox_layered, gbc_layered);

gbc_layered.gridx = 0;
gbc_layered.gridy ++;
layered_image_edit_panel.add(GuiHelper.getAlignedNonOpaqueJLabelWithCurrentColors("Images:", GuiHelper.LEFT), gbc_layered);
layered_image_edit_panel.add(GuiHelper.getAlignedNonOpaqueJLabelWithCurrentColors("Filter:", GuiHelper.LEFT), gbc_layered);

gbc_layered.gridx ++;
JComboBox<String> images_list_layered = new JComboBox<String>(FileOperations.getNamesOfImagesInImagesDirectory() );
images_list_layered.setSelectedItem(layered_image_abbr);
layered_image_edit_panel.add(images_list_layered, gbc_layered);
JTextField filter_textFiled_layered = new JTextField();
layered_image_edit_panel.add(filter_textFiled_layered, gbc_layered);

content_panel.add(layered_image_edit_panel, gbc);


abbr_list_layered.addItemListener(new ItemListener() {
combobox_layered.addActionListener(new ActionListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(selectedByUser && e.getStateChange() == ItemEvent.SELECTED)
{
selectedByUser = false;
images_list_layered.setSelectedIndex(0);
selectedByUser = true;
layered_image_abbr = (String) abbr_list_layered.getSelectedItem();
layered_image_label.setIcon(GuiHelper.getScaledImageIconFromAbbreviation(layered_image_abbr) );
icon_dialog.pack();
}
}
public void actionPerformed(ActionEvent e)
{
layered_image_abbr = (String) combobox_layered.getSelectedItem();
content_panel.remove(layered_image_label);
layered_image_label = new JLabel(GuiHelper.getScaledImageIconFromAbbreviation(layered_image_abbr) );
gbc.gridx = 1;
gbc.gridy = 2;
content_panel.add(layered_image_label, gbc);
icon_dialog.pack();
}
});
images_list_layered.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(selectedByUser && e.getStateChange() == ItemEvent.SELECTED)
{
selectedByUser = false;
abbr_list_layered.setSelectedIndex(0);
selectedByUser = true;
layered_image_abbr = (String) images_list_layered.getSelectedItem();
layered_image_label.setIcon(GuiHelper.getScaledImageIconFromAbbreviation(layered_image_abbr) );
icon_dialog.pack();
}
}
filter_textFiled_layered.getDocument().addDocumentListener(new DocumentListener() {
@Override public void removeUpdate(DocumentEvent e) { update(); }
@Override public void insertUpdate(DocumentEvent e) { update(); }
@Override public void changedUpdate(DocumentEvent e) { update(); }

private void update()
{
combobox_layered.setModel(new DefaultComboBoxModel<>(getFilteredAbbreviationsAndFileNamesList(filter_textFiled_layered.getText() ) ) );
combobox_layered.setPreferredSize(combobox_layered_dim);
}
});

// horizontal alignment
Expand Down Expand Up @@ -677,6 +669,9 @@ public void itemStateChanged(ItemEvent e) {

icon_dialog.pack();
icon_dialog.setVisible(true);

combobox_main_dim = combobox_main.getSize();
combobox_layered_dim = combobox_layered.getSize();
}
};
}
Expand Down Expand Up @@ -764,4 +759,39 @@ public void actionPerformed(ActionEvent e)
}
};
}

public static String getLowercaseStringWithFirstCharCapitablized(String str)
{
return str.length() == 0 ? str : str.length() == 1 ? str.toUpperCase() : Character.toUpperCase(str.charAt(0) ) + str.substring(1).toLowerCase();
}

private static void updateAbbreviationsAndFileNamesList()
{
ArrayList<String> completeNames = Abbreviations.getStreamOfCompleteNames().collect(Collectors.toCollection(ArrayList::new) );
abbreviationsAndFileNamesList = Stream.concat(
Stream.concat(Stream.of(""), Abbreviations.getStreamOfAbbreviations() ),
FileOperations.getStreamOfNamesOfImagesInImagesDirectory()
.filter(e -> ! completeNames.contains(e) )
.map(e -> getLowercaseStringWithFirstCharCapitablized(e) )
)
.map(e -> getLowercaseStringWithFirstCharCapitablized(e) )
.sorted( (e,f) -> e.compareTo(f) )
.toArray(String[]::new);
}

private static String[] getFilteredAbbreviationsAndFileNamesList(String filter)
{
return Stream.of(abbreviationsAndFileNamesList)
.map(e -> e.toLowerCase() )
.filter(e -> e.contains(filter.toLowerCase() ) ).
sorted( (String e, String f) -> {
boolean eStartsWith = e.startsWith(filter);
boolean fStartsWith = f.startsWith(filter);
if (eStartsWith && !fStartsWith) return -1;
else if ( !eStartsWith && fStartsWith) return 1;
else return e.compareTo(f);
} )
.map(e -> getLowercaseStringWithFirstCharCapitablized(e) )
.toArray(String[]::new);
}
}
9 changes: 7 additions & 2 deletions src/gui/Abbreviations.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,14 @@ public static ArrayList<String[]> getAbbreviationsListFromTextfiles(ArrayList<JT
return textfields.stream().map(row -> new String[] {row[0].getText(), row[1].getText() } ).collect(Collectors.toCollection(ArrayList::new) );
}

public static String[] getArrayOfAbbreviations()
public static Stream<String> getStreamOfAbbreviations()
{
return Stream.concat(Stream.of(""), abbreviationsList.stream().map(e -> e[0] ) ).toArray(String[]::new);
return abbreviationsList.stream().map(e -> e[0] );
}

public static Stream<String> getStreamOfCompleteNames()
{
return abbreviationsList.stream().map(e -> e[1] );
}

public static String getNameFromAbbreviation(String str)
Expand Down
6 changes: 3 additions & 3 deletions src/logic/FileOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ public static void selectNotesFile()
MainGui.window.setTitle(new_title);
}

public static String[] getNamesOfImagesInImagesDirectory()
public static Stream<String> getStreamOfNamesOfImagesInImagesDirectory()
{
return Stream.concat(Stream.of(""), Stream.of(new File(imagesDirectory).list(new FilenameFilter() {
return Stream.of(new File(imagesDirectory).list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name)
{
return name.toLowerCase().endsWith(".png");
}
}) ).sorted( (e,f) -> e.compareTo(f) ).map(e -> e.substring(0, e.length() - 4) ) ).toArray(String[]::new);
}) ).map(e -> e.substring(0, e.length() - 4) );
}

private static void setDefaulSettings()
Expand Down

0 comments on commit 5266c8d

Please sign in to comment.