From 0fad4bfacdb8d51ce687ec43fbbaf01d2e5b428e Mon Sep 17 00:00:00 2001 From: Snowyfox Date: Thu, 2 Jun 2022 09:16:34 -0400 Subject: [PATCH] Added basically correct description editing for attachments. Added desktop file. --- ComposeWindow.java | 262 +++++++++++++++++++++------------------------ JKomasto.desktop | 13 +++ 2 files changed, 138 insertions(+), 137 deletions(-) create mode 100644 JKomasto.desktop diff --git a/ComposeWindow.java b/ComposeWindow.java index 0089040..eed1fe2 100644 --- a/ComposeWindow.java +++ b/ComposeWindow.java @@ -14,13 +14,16 @@ import javax.swing.JScrollPane; import javax.swing.JTabbedPane; import javax.swing.UIManager; import javax.swing.JToggleButton; -import javax.swing.ButtonGroup; import javax.swing.border.Border; import javax.swing.JPopupMenu; import javax.swing.JMenuItem; import javax.swing.JFileChooser; import javax.swing.ImageIcon; import javax.swing.text.JTextComponent; +import javax.swing.undo.UndoableEdit; +import javax.swing.undo.UndoManager; +import javax.swing.event.UndoableEditListener; +import javax.swing.event.UndoableEditEvent; import java.nio.file.Files; import java.awt.GridLayout; import java.awt.BorderLayout; @@ -31,6 +34,8 @@ import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; +import java.awt.event.ComponentListener; +import java.awt.event.ComponentEvent; import java.awt.Cursor; import java.awt.Color; import java.awt.Font; @@ -550,7 +555,7 @@ implements ActionListener, CaretListener, KeyListener { class AttachmentsComponent extends JPanel -implements ActionListener { +implements ComponentListener, ActionListener { private ComposeWindow primaire; @@ -567,7 +572,8 @@ implements ActionListener { attachment1, attachment2, attachment3, - attachment4; + attachment4, + selected; private JButton add; @@ -585,6 +591,9 @@ implements ActionListener { private JFileChooser chooser; + private UndoManager + descriptionUndos; + // ---%-@-%--- public void @@ -611,48 +620,51 @@ implements ActionListener { { Dimension sz = add.getPreferredSize(); - selections.removeAll(); - if (working.size() > 0) selections.add(attachment1); - if (working.size() > 1) selections.add(attachment2); - if (working.size() > 2) selections.add(attachment3); - if (working.size() > 3) selections.add(attachment4); - if (working.size() < 4) selections.add(add); - - if (working.size() > 3) - { - Image i = working.get(3).image; - attachment4.setIcon(new ImageIcon(i)); - } - else if (working.size() > 2) - { - Image i = working.get(2).image; - attachment3.setIcon(new ImageIcon(i)); - } - else if (working.size() > 1) - { - Image i = working.get(1).image; - attachment2.setIcon(new ImageIcon(i)); - } - else if (working.size() > 0) + selections.removeAll(); + selected = null; + if (working.size() > 0) { + selections.add(attachment1); Image i = working.get(0).image; attachment1.setIcon(new ImageIcon(i)); } + if (working.size() > 1) + { + selections.add(attachment2); + Image i = working.get(1).image; + attachment2.setIcon(new ImageIcon(i)); + } + if (working.size() > 2) + { + selections.add(attachment3); + Image i = working.get(2).image; + attachment3.setIcon(new ImageIcon(i)); + } + if (working.size() > 3) + { + selections.add(attachment4); + Image i = working.get(3).image; + attachment4.setIcon(new ImageIcon(i)); + } + if (working.size() < 4) selections.add(add); - attachment4.setSelected(working.size() > 3); - attachment3.setSelected(working.size() > 2); - attachment2.setSelected(working.size() > 1); - attachment1.setSelected(working.size() > 0); + if (working.size() > 3) open(attachment4); + else if (working.size() > 2) open(attachment3); + else if (working.size() > 1) open(attachment2); + else if (working.size() > 0) open(attachment1); - int bw = sz.width; + int bw = sz.width; int hgap = 4; int count = selections.getComponents().length; int w = count * bw + (count - 1) * hgap; int h = bw; selections.setPreferredSize(new Dimension(w, h)); selections.setMaximumSize(new Dimension(w, h)); + selections.revalidate(); - selections.revalidate(); + delete.setEnabled(selected != null); + revert.setEnabled(selected != null); + description.setEnabled(selected != null); } public void @@ -669,6 +681,8 @@ implements ActionListener { Attachment a = new Attachment(); a.uploadee = f; + a.description = ""; + a.type = "unknown"; String mime = "", primary = ""; try @@ -683,101 +697,100 @@ implements ActionListener { { String urlr = f.toURI().toString(); a.image = ImageApi.remote(urlr); + a.type = "image"; } + if (selected != null) open(selected); + // Save first before resetting + working.add(a); updateButtons(); } if (src == delete) { - if (attachment1.isSelected()) - { - assert working.size() > 0; - working.remove(0); - } - if (attachment2.isSelected()) - { - assert working.size() > 1; - working.remove(1); - } - if (attachment3.isSelected()) - { - assert working.size() > 2; - working.remove(2); - } - if (attachment4.isSelected()) - { - assert working.size() > 3; - working.remove(3); - } + assert selected != null; + working.remove(getAttachmentFor(selected)); updateButtons(); return; } if (src != add && selections.isAncestorOf((Component)src)) { - int iCurr = getSelectedIndex(); - int iNext = getIndex(src); - System.err.println(iCurr + ":" + iNext); - assert iNext != 0; - if (iCurr == iNext) { - save(); - return; - } - if (iCurr != 0) save(); - load(iNext); + assert src instanceof JToggleButton; + if (src == selected) preview(getAttachmentFor(src)); + else open((JToggleButton)src); + return; } if (src == revert) { - assert getSelectedIndex() != 0; - // Should the controls be disabled until - // there is an attachment? - load(getSelectedIndex()); + while (descriptionUndos.canUndo()) + descriptionUndos.undo(); + return; } } - private int - getIndex(Object src) + private Attachment + getAttachmentFor(Object button) { - if (src == attachment4) return 4; - if (src == attachment3) return 3; - if (src == attachment2) return 2; - if (src == attachment1) return 1; - return 0; - } + if (button == null) return null; + assert button instanceof JToggleButton; + assert selections.isAncestorOf((Component)button); + + int index = 0; + if (button == attachment4) index = 4; + if (button == attachment3) index = 3; + if (button == attachment2) index = 2; + if (button == attachment1) index = 1; - private int - getSelectedIndex() - { - if (attachment4.isSelected()) return 4; - if (attachment3.isSelected()) return 3; - if (attachment2.isSelected()) return 2; - if (attachment1.isSelected()) return 1; - return 0; - } - - private void - save() - { - int index = getSelectedIndex(); assert index != 0; - - Attachment a = working.get(index - 1); - a.description = this.description.getText(); + assert index <= working.size(); + return working.get(index - 1); } private void - load(int index) + open(JToggleButton button) { - assert index > 0; - assert working.size() >= index; + assert selections.isAncestorOf(button); - Attachment a = working.get(index - 1); - this.description.setText(a.description); + if (selected != null) + { + Attachment a = getAttachmentFor(selected); + a.description = description.getText(); + selected.setSelected(false); + } + + Attachment a = getAttachmentFor(button); + description.setText(a.description); + descriptionUndos.discardAllEdits(); + (selected = button).setSelected(true); } + public void + componentHidden(ComponentEvent eC) + { + if (selected != null) open(selected); + } + + private void + preview(Attachment a) + { + ImageWindow w = new ImageWindow(); + w.showAttachments(new Attachment[] { a } ); + w.setTitle("Attachment preview"); + w.setVisible(true); + } + + public void + componentShown(ComponentEvent eC) { } + + public void + componentMoved(ComponentEvent eC) { } + + public void + componentResized(ComponentEvent eC) { } + // ---%-@-%--- AttachmentsComponent(ComposeWindow primaire) @@ -818,42 +831,19 @@ implements ActionListener { selections.setOpaque(false); selections.setLayout(new GridLayout(1, 0, 4, 0)); working = new ArrayList(); - ButtonGroup selectionsGroup = new ButtonGroup(); - selectionsGroup.add(attachment1); - selectionsGroup.add(attachment2); - selectionsGroup.add(attachment3); - selectionsGroup.add(attachment4); - updateButtons(); - JButton del = new JButton("D"); - JButton rev = new JButton("R"); - JButton ml = new JButton("←"); - JButton mr = new JButton("→"); - del.setMargin(new Insets(0, 0, 0, 0)); - //ml.setMargin(new Insets(0, 0, 0, 0)); - //mr.setMargin(new Insets(0, 0, 0, 0)); - rev.setMargin(new Insets(0, 0, 0, 0)); - - JPanel actions = new JPanel(); - actions.setOpaque(false); - actions.setLayout(new GridLayout(1, 4, 4, 4)); - actions.add(del); - actions.add(rev); - actions.add(ml); - actions.add(mr); - actions.setPreferredSize(new Dimension(108, 24)); - actions.setMaximumSize(new Dimension(108, 24)); - - delete = new JButton("Delete"); - revert = new JButton("Revert"); - delete.addActionListener(this); - revert.addActionListener(this); - - Box top = Box.createHorizontalBox(); + Box top = Box.createHorizontalBox(); top.add(selections); top.add(Box.createGlue()); - Box bottom = Box.createHorizontalBox(); + delete = new JButton("Delete"); + revert = new JButton("Revert"); + JButton ml = new JButton("←"); + JButton mr = new JButton("→"); + delete.addActionListener(this); + revert.addActionListener(this); + + Box bottom = Box.createHorizontalBox(); bottom.add(ml); bottom.add(mr); bottom.add(Box.createHorizontalStrut(8)); @@ -870,6 +860,11 @@ implements ActionListener { description.addMouseListener(textActionPopup); descriptionLabel = new JLabel("Description"); descriptionLabel.setLabelFor(description); + descriptionUndos = new UndoManager(); + description.getDocument(). + addUndoableEditListener(descriptionUndos); + + updateButtons(); JPanel row1 = new JPanel(); row1.setOpaque(false); @@ -877,15 +872,7 @@ implements ActionListener { row1.add(descriptionLabel, BorderLayout.NORTH); row1.add(description, BorderLayout.CENTER); - /* - Box row2 = Box.createHorizontalBox(); - row2.add(Box.createGlue()); - row2.add(delete); - row2.add(Box.createHorizontalStrut(8)); - row2.add(revert); - */ - - Box centre = Box.createVerticalBox(); + Box centre = Box.createVerticalBox(); centre.setBorder(b4); centre.add(row1); @@ -895,6 +882,7 @@ implements ActionListener { add(bottom, BorderLayout.SOUTH); setBorder(b1); + this.addComponentListener(this); } } diff --git a/JKomasto.desktop b/JKomasto.desktop new file mode 100644 index 0000000..e7bfec4 --- /dev/null +++ b/JKomasto.desktop @@ -0,0 +1,13 @@ +[Desktop Entry] +Version=1.5 + +Type=Application +Name=JKomasto +GenericName=Mastodon client +GenericName[ja]=Mastodon クライアント +Categories=Network + +Path=/home/snowyfox/Documents/Programming/JKomasto2/ +Icon=/home/snowyfox/Documents/Programming/JKomasto2/graphics/kettle.png +Exec=java -cp ".:../Hinoki:/usr/share/java/javax.json.jar" -ea JKomasto +Terminal=false \ No newline at end of file