From df6b7e149e56443dea38d73e2d316b9d7ba73e38 Mon Sep 17 00:00:00 2001 From: Snowyfox Date: Mon, 30 May 2022 03:56:09 -0400 Subject: [PATCH] Added copy-paste menu to text widgets. Added base for media uploading. --- BasicHTMLParser.java | 0 ClipboardApi.java | 0 ComposeWindow.java | 219 +++++++++++++++++++++++++++----- ImageApi.java | 0 ImageWindow.java | 0 JKomasto.java | 7 + KDE_Dialog_Appear.wav | Bin LoginWindow.java | 0 MastodonApi.java | 0 NotificationsWindow.java | 0 PostWindow.java | 0 ProfileWindow.java | 0 RepliesWindow.java | 0 RequestListener.java | 0 RichTextPane.java | 0 RichTextPane2.java | 0 RichTextPane3.java | 1 + RudimentaryHTMLParser.java | 0 TimelineWindow.java | 0 TwoToggleButton.java | 0 WindowUpdater.java | 0 graphics/Federated.xcf | Bin graphics/Flags.xcf | Bin graphics/Home.xcf | Bin graphics/Hourglass.xcf | Bin graphics/Kettle.xcf | Bin graphics/boostToggled.png | Bin graphics/boostUntoggled.png | Bin graphics/button.png | Bin graphics/disabledOverlay.png | Bin graphics/favouriteToggled.png | Bin graphics/favouriteUntoggled.png | Bin graphics/federated.png | Bin graphics/home.png | Bin graphics/kettle.png | Bin graphics/miscToggled.png | Bin graphics/miscUntoggled.png | Bin graphics/nextToggled.png | Bin graphics/nextUntoggled.png | Bin graphics/postWindow.png | Bin graphics/prevToggled.png | Bin graphics/prevUntoggled.png | Bin graphics/ref1.png | Bin graphics/replyToggled.png | Bin graphics/replyUntoggled.png | Bin graphics/selectedOverlay.png | Bin graphics/test1.png | Bin graphics/test2.png | Bin graphics/test3.png | Bin graphics/test4.png | Bin notifOptions.txt | 0 notifOptions.txt~ | 0 52 files changed, 196 insertions(+), 31 deletions(-) mode change 100644 => 100755 BasicHTMLParser.java mode change 100644 => 100755 ClipboardApi.java mode change 100644 => 100755 ComposeWindow.java mode change 100644 => 100755 ImageApi.java mode change 100644 => 100755 ImageWindow.java mode change 100644 => 100755 JKomasto.java mode change 100644 => 100755 KDE_Dialog_Appear.wav mode change 100644 => 100755 LoginWindow.java mode change 100644 => 100755 MastodonApi.java mode change 100644 => 100755 NotificationsWindow.java mode change 100644 => 100755 PostWindow.java mode change 100644 => 100755 ProfileWindow.java mode change 100644 => 100755 RepliesWindow.java mode change 100644 => 100755 RequestListener.java mode change 100644 => 100755 RichTextPane.java mode change 100644 => 100755 RichTextPane2.java mode change 100644 => 100755 RichTextPane3.java mode change 100644 => 100755 RudimentaryHTMLParser.java mode change 100644 => 100755 TimelineWindow.java mode change 100644 => 100755 TwoToggleButton.java mode change 100644 => 100755 WindowUpdater.java mode change 100644 => 100755 graphics/Federated.xcf mode change 100644 => 100755 graphics/Flags.xcf mode change 100644 => 100755 graphics/Home.xcf mode change 100644 => 100755 graphics/Hourglass.xcf mode change 100644 => 100755 graphics/Kettle.xcf mode change 100644 => 100755 graphics/boostToggled.png mode change 100644 => 100755 graphics/boostUntoggled.png mode change 100644 => 100755 graphics/button.png mode change 100644 => 100755 graphics/disabledOverlay.png mode change 100644 => 100755 graphics/favouriteToggled.png mode change 100644 => 100755 graphics/favouriteUntoggled.png mode change 100644 => 100755 graphics/federated.png mode change 100644 => 100755 graphics/home.png mode change 100644 => 100755 graphics/kettle.png mode change 100644 => 100755 graphics/miscToggled.png mode change 100644 => 100755 graphics/miscUntoggled.png mode change 100644 => 100755 graphics/nextToggled.png mode change 100644 => 100755 graphics/nextUntoggled.png mode change 100644 => 100755 graphics/postWindow.png mode change 100644 => 100755 graphics/prevToggled.png mode change 100644 => 100755 graphics/prevUntoggled.png mode change 100644 => 100755 graphics/ref1.png mode change 100644 => 100755 graphics/replyToggled.png mode change 100644 => 100755 graphics/replyUntoggled.png mode change 100644 => 100755 graphics/selectedOverlay.png mode change 100644 => 100755 graphics/test1.png mode change 100644 => 100755 graphics/test2.png mode change 100644 => 100755 graphics/test3.png mode change 100644 => 100755 graphics/test4.png mode change 100644 => 100755 notifOptions.txt mode change 100644 => 100755 notifOptions.txt~ diff --git a/BasicHTMLParser.java b/BasicHTMLParser.java old mode 100644 new mode 100755 diff --git a/ClipboardApi.java b/ClipboardApi.java old mode 100644 new mode 100755 diff --git a/ComposeWindow.java b/ComposeWindow.java old mode 100644 new mode 100755 index 4e23029..dba0eb9 --- a/ComposeWindow.java +++ b/ComposeWindow.java @@ -16,6 +16,12 @@ 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 java.nio.file.Files; import java.awt.GridLayout; import java.awt.BorderLayout; import java.awt.Dimension; @@ -23,6 +29,8 @@ import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseEvent; import java.awt.Cursor; import java.awt.Color; import java.awt.Font; @@ -30,6 +38,8 @@ import java.awt.Insets; import java.awt.FlowLayout; import java.awt.Component; import java.awt.Container; +import java.awt.Image; +import java.io.File; import javax.swing.event.CaretListener; import javax.swing.event.CaretEvent; import java.util.List; @@ -92,6 +102,15 @@ ComposeWindow extends JFrame { if (composition.contentWarning != null) assert !composition.contentWarning.trim().isEmpty(); + // Oh, this is gonna be hell. + /* + * (未) For every attachment in composition, + * attempt to upload it, then fill the media ID. + * Then change MastodonApi#submit to accept an + * array of media IDs. If any upload fails, + * show an error message then return. + */ + contentsDisplay.setSubmitting(true); api.submit( composition.text, composition.visibility, @@ -135,22 +154,29 @@ ComposeWindow extends JFrame { private synchronized void syncDisplayToComposition() { - ComposeComponent d = contentsDisplay; - d.setText(composition.text); - d.setReplyToPostId(composition.replyToPostId); - d.setVisibility(stringFor(composition.visibility)); - d.setContentWarning(composition.contentWarning); + ComposeComponent d1 = contentsDisplay; + d1.setText(composition.text); + d1.setReplyToPostId(composition.replyToPostId); + d1.setVisibility(stringFor(composition.visibility)); + d1.setContentWarning(composition.contentWarning); + + AttachmentsComponent d2 = attachmentsDisplay; + d2.setAttachments(composition.attachments); } private synchronized void syncCompositionToDisplay() { - ComposeComponent d = contentsDisplay; - Composition c = composition; - c.text = d.getText(); - c.visibility = visibilityFrom(d.getVisibility()); - c.replyToPostId = nonEmpty(d.getReplyToPostId()); - c.contentWarning = nonEmpty(d.getContentWarning()); + Composition c = composition; + + ComposeComponent d1 = contentsDisplay; + c.text = d1.getText(); + c.visibility = visibilityFrom(d1.getVisibility()); + c.replyToPostId = nonEmpty(d1.getReplyToPostId()); + c.contentWarning = nonEmpty(d1.getContentWarning()); + + AttachmentsComponent d2 = attachmentsDisplay; + c.attachments = d2.getAttachments(); } // - -%- - @@ -248,9 +274,6 @@ implements ActionListener, CaretListener, KeyListener { private JButton submit; - private JButton - showAttachmentsPage; - // ---%-@-%--- public void @@ -332,10 +355,6 @@ implements ActionListener, CaretListener, KeyListener { public void actionPerformed(ActionEvent eA) { - if (eA.getSource() == showAttachmentsPage) - //primaire.showAttachmentsPage(); - ; - if (eA.getSource() == submit) primaire.submit(); } @@ -398,13 +417,18 @@ implements ActionListener, CaretListener, KeyListener { Border b3 = BorderFactory.createLineBorder(Color.GRAY); Border bc = BorderFactory.createCompoundBorder(b3, b2); + TextActionPopupMenu textActionPopup; + textActionPopup = new TextActionPopupMenu(); + reply = new JTextField(); JLabel replyLabel = new JLabel("In reply to: "); replyLabel.setLabelFor(reply); + reply.addMouseListener(textActionPopup); contentWarning = new JTextField(); JLabel cwLabel = new JLabel("Content warning: "); cwLabel.setLabelFor(contentWarning); + contentWarning.addMouseListener(textActionPopup); JPanel top = new JPanel(); top.setOpaque(false); @@ -429,11 +453,7 @@ implements ActionListener, CaretListener, KeyListener { submit = new JButton("Submit"); submit.addActionListener(this); - showAttachmentsPage = new JButton("Media"); - showAttachmentsPage.addActionListener(this); - Box bottom = Box.createHorizontalBox(); - //bottom.add(showAttachmentsPage); bottom.add(Box.createGlue()); bottom.add(textLength); bottom.add(Box.createHorizontalStrut(12)); @@ -448,6 +468,7 @@ implements ActionListener, CaretListener, KeyListener { text.setBorder(bc); text.addCaretListener(this); text.addKeyListener(this); + text.addMouseListener(textActionPopup); setLayout(new BorderLayout(0, 8)); add(top, BorderLayout.NORTH); @@ -496,8 +517,30 @@ implements ActionListener { private JTextArea description; + private JFileChooser + chooser; + // ---%-@-%--- + public void + setAttachments(Attachment[] n) + { + working.clear(); + if (n != null) for (Attachment attachment: n) + { + working.add(attachment); + } + updateButtons(); + } + + public Attachment[] + getAttachments() + { + return working.toArray(new Attachment[0]); + } + +// - -%- - + private void updateButtons() { @@ -510,10 +553,30 @@ implements ActionListener { if (working.size() > 3) selections.add(attachment4); if (working.size() < 4) selections.add(add); - if (working.size() > 3) attachment4.doClick(); - else if (working.size() > 2) attachment3.doClick(); - else if (working.size() > 1) attachment2.doClick(); - else if (working.size() > 0) attachment1.doClick(); + if (working.size() > 3) + { + attachment4.doClick(); + Image i = working.get(3).image; + attachment4.setIcon(new ImageIcon(i)); + } + else if (working.size() > 2) + { + attachment3.doClick(); + Image i = working.get(2).image; + attachment3.setIcon(new ImageIcon(i)); + } + else if (working.size() > 1) + { + attachment2.doClick(); + Image i = working.get(1).image; + attachment2.setIcon(new ImageIcon(i)); + } + else if (working.size() > 0) + { + attachment1.doClick(); + Image i = working.get(0).image; + attachment1.setIcon(new ImageIcon(i)); + } else selectionsGroup.clearSelection(); int bw = sz.width; @@ -539,10 +602,30 @@ implements ActionListener { if (src == add) { - // Invoke file picker. Try to get file. - // Try to upload file. So on. - // Then add to working. - working.add(new Attachment()); + int r = chooser.showOpenDialog(this); + if (r != JFileChooser.APPROVE_OPTION) return; + + File f = chooser.getSelectedFile(); + + Attachment a = new Attachment(); + a.uploadee = f; + + String mime = "", primary = ""; + try + { + mime = Files.probeContentType(f.toPath()); + primary = mime.split("/")[0]; + // Too lazy to instantiate a + // javax.activation.MimeType. + } + catch (IOException eIo) { } + if (primary.equals("image")) + { + String urlr = f.toURI().toString(); + a.image = ImageApi.remote(urlr); + } + + working.add(a); updateButtons(); } @@ -596,10 +679,15 @@ implements ActionListener { Border bc1 = BorderFactory.createCompoundBorder(b3, b2); Border bc2 = BorderFactory.createCompoundBorder(b4, b2); + TextActionPopupMenu textActionPopup; + textActionPopup = new TextActionPopupMenu(); + + chooser = new JFileChooser(); + add = new JButton("+"); add.setPreferredSize(new Dimension(32, 32)); add.setMargin(new Insets(0, 0, 0, 0)); - add.addActionListener(this); + add.addActionListener(this); attachment1 = new JToggleButton("1"); attachment2 = new JToggleButton("2"); attachment3 = new JToggleButton("3"); @@ -663,6 +751,7 @@ implements ActionListener { java.awt.Font f = description.getFont(); description.setFont(f.deriveFont(16f)); description.setBorder(bc1); + description.addMouseListener(textActionPopup); descriptionLabel = new JLabel("Description"); descriptionLabel.setLabelFor(description); @@ -693,3 +782,71 @@ implements ActionListener { } } + +class +TextActionPopupMenu extends JPopupMenu +implements MouseListener, ActionListener { + + private JMenuItem + copy, + cut, + paste; + + private long + pressed; + +// ---%-@-%--- + + public void + mousePressed(MouseEvent eM) + { + assert eM.getSource() instanceof JTextComponent; + if (!eM.isPopupTrigger()) return; + show((Component)eM.getSource(), eM.getX(), eM.getY()); + } + + public void + mouseReleased(MouseEvent eM) + { + if (eM.getClickCount() == 0) setVisible(false); + } + + public void + mouseClicked(MouseEvent eM) { } + + public void + mouseEntered(MouseEvent eM) { } + + public void + mouseExited(MouseEvent eM) { } + + public void + actionPerformed(ActionEvent eA) + { + assert getInvoker() instanceof JTextComponent; + JTextComponent inv = (JTextComponent)getInvoker(); + + if (eA.getSource() == copy) inv.copy(); + if (eA.getSource() == cut) inv.cut(); + if (eA.getSource() == paste) inv.paste(); + } + +// ---%-@-%--- + + public + TextActionPopupMenu() + { + copy = new JMenuItem("Copy"); + cut = new JMenuItem("Cut"); + paste = new JMenuItem("Paste"); + copy.addActionListener(this); + cut.addActionListener(this); + paste.addActionListener(this); + + add(cut); + add(copy); + add(paste); + } + +} + diff --git a/ImageApi.java b/ImageApi.java old mode 100644 new mode 100755 diff --git a/ImageWindow.java b/ImageWindow.java old mode 100644 new mode 100755 diff --git a/JKomasto.java b/JKomasto.java old mode 100644 new mode 100755 index 3f4e04b..0c7a2d3 --- a/JKomasto.java +++ b/JKomasto.java @@ -22,6 +22,7 @@ import javax.swing.plaf.metal.DefaultMetalTheme; import javax.swing.plaf.metal.OceanTheme; import javax.swing.plaf.ColorUIResource; import javax.swing.UIDefaults; +import java.io.File; import cafe.biskuteri.hinoki.Tree; @@ -569,6 +570,9 @@ Attachment { public Image image; + public File + uploadee; + // ---%-@-%--- public void @@ -616,6 +620,9 @@ Composition { public Attachment[] attachments; + private File + uploadee; + // ---%-@-%--- public diff --git a/KDE_Dialog_Appear.wav b/KDE_Dialog_Appear.wav old mode 100644 new mode 100755 diff --git a/LoginWindow.java b/LoginWindow.java old mode 100644 new mode 100755 diff --git a/MastodonApi.java b/MastodonApi.java old mode 100644 new mode 100755 diff --git a/NotificationsWindow.java b/NotificationsWindow.java old mode 100644 new mode 100755 diff --git a/PostWindow.java b/PostWindow.java old mode 100644 new mode 100755 diff --git a/ProfileWindow.java b/ProfileWindow.java old mode 100644 new mode 100755 diff --git a/RepliesWindow.java b/RepliesWindow.java old mode 100644 new mode 100755 diff --git a/RequestListener.java b/RequestListener.java old mode 100644 new mode 100755 diff --git a/RichTextPane.java b/RichTextPane.java old mode 100644 new mode 100755 diff --git a/RichTextPane2.java b/RichTextPane2.java old mode 100644 new mode 100755 diff --git a/RichTextPane3.java b/RichTextPane3.java old mode 100644 new mode 100755 index 3a2c5a7..558cce0 --- a/RichTextPane3.java +++ b/RichTextPane3.java @@ -366,6 +366,7 @@ implements public void mousePressed(MouseEvent eM) { + if (eM.getButton() != MouseEvent.BUTTON1) return; selStart = identifyNodeAt(eM.getX(), eM.getY()); selEnd = null; repaint(); diff --git a/RudimentaryHTMLParser.java b/RudimentaryHTMLParser.java old mode 100644 new mode 100755 diff --git a/TimelineWindow.java b/TimelineWindow.java old mode 100644 new mode 100755 diff --git a/TwoToggleButton.java b/TwoToggleButton.java old mode 100644 new mode 100755 diff --git a/WindowUpdater.java b/WindowUpdater.java old mode 100644 new mode 100755 diff --git a/graphics/Federated.xcf b/graphics/Federated.xcf old mode 100644 new mode 100755 diff --git a/graphics/Flags.xcf b/graphics/Flags.xcf old mode 100644 new mode 100755 diff --git a/graphics/Home.xcf b/graphics/Home.xcf old mode 100644 new mode 100755 diff --git a/graphics/Hourglass.xcf b/graphics/Hourglass.xcf old mode 100644 new mode 100755 diff --git a/graphics/Kettle.xcf b/graphics/Kettle.xcf old mode 100644 new mode 100755 diff --git a/graphics/boostToggled.png b/graphics/boostToggled.png old mode 100644 new mode 100755 diff --git a/graphics/boostUntoggled.png b/graphics/boostUntoggled.png old mode 100644 new mode 100755 diff --git a/graphics/button.png b/graphics/button.png old mode 100644 new mode 100755 diff --git a/graphics/disabledOverlay.png b/graphics/disabledOverlay.png old mode 100644 new mode 100755 diff --git a/graphics/favouriteToggled.png b/graphics/favouriteToggled.png old mode 100644 new mode 100755 diff --git a/graphics/favouriteUntoggled.png b/graphics/favouriteUntoggled.png old mode 100644 new mode 100755 diff --git a/graphics/federated.png b/graphics/federated.png old mode 100644 new mode 100755 diff --git a/graphics/home.png b/graphics/home.png old mode 100644 new mode 100755 diff --git a/graphics/kettle.png b/graphics/kettle.png old mode 100644 new mode 100755 diff --git a/graphics/miscToggled.png b/graphics/miscToggled.png old mode 100644 new mode 100755 diff --git a/graphics/miscUntoggled.png b/graphics/miscUntoggled.png old mode 100644 new mode 100755 diff --git a/graphics/nextToggled.png b/graphics/nextToggled.png old mode 100644 new mode 100755 diff --git a/graphics/nextUntoggled.png b/graphics/nextUntoggled.png old mode 100644 new mode 100755 diff --git a/graphics/postWindow.png b/graphics/postWindow.png old mode 100644 new mode 100755 diff --git a/graphics/prevToggled.png b/graphics/prevToggled.png old mode 100644 new mode 100755 diff --git a/graphics/prevUntoggled.png b/graphics/prevUntoggled.png old mode 100644 new mode 100755 diff --git a/graphics/ref1.png b/graphics/ref1.png old mode 100644 new mode 100755 diff --git a/graphics/replyToggled.png b/graphics/replyToggled.png old mode 100644 new mode 100755 diff --git a/graphics/replyUntoggled.png b/graphics/replyUntoggled.png old mode 100644 new mode 100755 diff --git a/graphics/selectedOverlay.png b/graphics/selectedOverlay.png old mode 100644 new mode 100755 diff --git a/graphics/test1.png b/graphics/test1.png old mode 100644 new mode 100755 diff --git a/graphics/test2.png b/graphics/test2.png old mode 100644 new mode 100755 diff --git a/graphics/test3.png b/graphics/test3.png old mode 100644 new mode 100755 diff --git a/graphics/test4.png b/graphics/test4.png old mode 100644 new mode 100755 diff --git a/notifOptions.txt b/notifOptions.txt old mode 100644 new mode 100755 diff --git a/notifOptions.txt~ b/notifOptions.txt~ old mode 100644 new mode 100755