From 1e03335bb902f73b87d2ca7109ae26149fe928db Mon Sep 17 00:00:00 2001 From: Labunsky Artem Date: Mon, 19 Jan 2015 10:22:46 +0300 Subject: [PATCH] Settings dialog moved to external class, new search Now you can search with ">", "<" and "" prefix in height and width fields, you can also do it separately +many small fixes --- META-INF/MANIFEST.MF | 3 - Sources/Listeners.java | 205 ++---------------------------- Sources/SettingsDialog.java | 203 +++++++++++++++++++++++++++++ Sources/Window.java | 49 +++++-- Sources/resources/closeNya.png | Bin 292 -> 259 bytes Sources/resources/maximizeNya.png | Bin 257 -> 258 bytes Sources/resources/minimizeNya.png | Bin 240 -> 245 bytes 7 files changed, 251 insertions(+), 209 deletions(-) delete mode 100644 META-INF/MANIFEST.MF create mode 100644 Sources/SettingsDialog.java diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF deleted file mode 100644 index fcb9722..0000000 --- a/META-INF/MANIFEST.MF +++ /dev/null @@ -1,3 +0,0 @@ -Manifest-Version: 1.0 -Main-Class: Solution - diff --git a/Sources/Listeners.java b/Sources/Listeners.java index 29db803..9689958 100644 --- a/Sources/Listeners.java +++ b/Sources/Listeners.java @@ -1,7 +1,5 @@ import javax.imageio.ImageIO; import javax.swing.*; -import javax.swing.event.DocumentEvent; -import javax.swing.event.DocumentListener; import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.*; import java.awt.event.*; @@ -13,7 +11,11 @@ * Protected with GNU GPLv2 and your honesty */ -class Listeners implements ActionListener { +class Listeners { + //I just want to use this name for .java +} + +class Save2File implements ActionListener{ private File lastSave = null; private Boolean firstTime = true; @@ -71,9 +73,7 @@ class getNewNya implements ActionListener{ public void actionPerformed(ActionEvent e) { try { Solution.getNya.drawNya(); - } catch (IOException e1) { - e1.printStackTrace(); - } + } catch (IOException ignored) {} } } @@ -107,200 +107,11 @@ public void actionPerformed(ActionEvent e) { } class Settings implements ActionListener{ - private Integer positionX = 0; - private Integer positionY = 0; - @Override public void actionPerformed(ActionEvent e) { if (!Window.settingsIsOpen) { - Window.settingsIsOpen = true; - - final JFrame settingsDialog = new JFrame("Settings"); - final JTextField tag = new JTextField(Window.tag); - final JTextField widthScan = new JTextField(Integer.toString(Window.customResolution.width)); - final JTextField heightScan = new JTextField(Integer.toString(Window.customResolution.height)); - - JTextField tagPointer = new JTextField(" Tag (only one, could works slowly): "); - JTextField width = new JTextField(" Width: "); - JTextField height = new JTextField(" Height: "); - - JPanel mainPanel = new JPanel(); - JPanel hdCheckBoxPanel = new JPanel(); - JPanel buttonsPanel = new JPanel(); - JPanel tagPanel = new JPanel(); - JPanel sizePanel = new JPanel(); - JPanel prevNyaPanel = new JPanel(); - JPanel closePanel = new JPanel(); - - Checkbox hdCheckBox = new Checkbox("HD nya only"); - - SimpleButton closeButton = new SimpleButton("resources/closeNya.png"); - SimpleButton prevNya = new SimpleButton("resources/prevNya.png", "resources/prevNyaPressed.png"); - - ActionListener getPrevNya = new GetPrevNya(); - DocumentListener documentListener = new DocumentListener() { - @Override - public void insertUpdate(DocumentEvent e) { - warn(); - } - - @Override - public void removeUpdate(DocumentEvent e) { - warn(); - } - - @Override - public void changedUpdate(DocumentEvent e) { - warn(); - } - - public void warn() { - if (tag.getText().length() == 0) { - Window.useTag = false; - } else { - Window.useTag = true; - Window.tag = tag.getText().toLowerCase(); - } - } - }; - DocumentListener customWidthListener = new DocumentListener() { - @Override - public void insertUpdate(DocumentEvent e) {warn();} - @Override - public void removeUpdate(DocumentEvent e) {warn();} - @Override - public void changedUpdate(DocumentEvent e) {warn();} - - public void warn() { - if (widthScan.getText().length() == 0) { - Window.currentResolution = false; - Window.customResolution = new Dimension(0, Window.customResolution.height); - } else { - Window.currentResolution = true; - Window.customResolution = new Dimension(Integer.valueOf(widthScan.getText()), Window.customResolution.height); - } - } - }; - DocumentListener customHeightListener = new DocumentListener() { - @Override - public void insertUpdate(DocumentEvent e) {warn();} - @Override - public void removeUpdate(DocumentEvent e) {warn();} - @Override - public void changedUpdate(DocumentEvent e) {warn();} - - public void warn() { - if (heightScan.getText().length() == 0) { - Window.currentResolution = false; - Window.customResolution = new Dimension(Window.customResolution.width, 0); - } else { - Window.currentResolution = true; - Window.customResolution = new Dimension(Window.customResolution.width, Integer.valueOf(heightScan.getText())); - } - } - }; - MouseAdapter dragDialogListener = new MouseAdapter() { - @Override - public void mouseDragged(MouseEvent e) { - super.mouseDragged(e); - settingsDialog.setLocation(e.getXOnScreen() - positionX, e.getYOnScreen() - positionY); - } - }; - MouseAdapter mouseMoveListener = new MouseAdapter() { - @Override - public void mousePressed(MouseEvent e) { - super.mousePressed(e); - positionX = e.getX(); - positionY = e.getY(); - } - }; - ActionListener closeSettings = new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - settingsDialog.dispose(); - Window.settingsIsOpen = false; - } - }; - - hdCheckBox.addItemListener(new ItemListener() { - @Override - public void itemStateChanged(ItemEvent e) { - Window.hdOnly = !Window.hdOnly; - } - }); - mainPanel.addMouseListener(mouseMoveListener); - mainPanel.addMouseMotionListener(dragDialogListener); - closeButton.addActionListener(closeSettings); - prevNya.addActionListener(getPrevNya); - tag.getDocument().addDocumentListener(documentListener); - widthScan.getDocument().addDocumentListener(customWidthListener); - heightScan.getDocument().addDocumentListener(customHeightListener); - - tag.setPreferredSize(new Dimension(100, 20)); - heightScan.setPreferredSize(new Dimension(50, 20)); - widthScan.setPreferredSize(new Dimension(50, 20)); - - hdCheckBox.setBackground(Color.WHITE); - hdCheckBoxPanel.setLayout(new FlowLayout()); - hdCheckBoxPanel.setBackground(Color.WHITE); - hdCheckBoxPanel.add(hdCheckBox); - - sizePanel.setBackground(Color.WHITE); - sizePanel.setLayout(new FlowLayout()); - sizePanel.add(width); - sizePanel.add(widthScan); - sizePanel.add(height); - sizePanel.add(heightScan); - - tagPanel.setLayout(new FlowLayout()); - tagPanel.setBackground(Color.WHITE); - tagPanel.add(tagPointer); - tagPanel.add(tag); - - prevNyaPanel.setLayout(new FlowLayout()); - prevNyaPanel.setBackground(Color.WHITE); - prevNyaPanel.add(prevNya); - - closePanel.setLayout(new BoxLayout(closePanel, BoxLayout.Y_AXIS)); - closePanel.setBackground(Color.WHITE); - closePanel.add(Box.createRigidArea(new Dimension(0, 10))); - closePanel.add(closeButton); - - buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS)); - buttonsPanel.add(prevNyaPanel); - buttonsPanel.add(closePanel); - buttonsPanel.setBackground(Color.WHITE); - - tagPointer.setBackground(Color.WHITE); - tagPointer.setDisabledTextColor(Color.BLACK); - tagPointer.setEnabled(false); - - height.setBackground(Color.WHITE); - height.setDisabledTextColor(Color.BLACK); - height.setEnabled(false); - - width.setBackground(Color.WHITE); - width.setDisabledTextColor(Color.BLACK); - width.setEnabled(false); - - mainPanel.setBackground(Color.WHITE); - mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); - mainPanel.add(hdCheckBoxPanel); - mainPanel.add(tagPanel); - mainPanel.add(sizePanel); - mainPanel.add(buttonsPanel); - - settingsDialog.add(mainPanel); - settingsDialog.setUndecorated(true); - settingsDialog.setAlwaysOnTop(true); - settingsDialog.pack(); - settingsDialog.setLocation(Solution.getNya.getX() + Solution.getNya.getWidth() / 2 - settingsDialog.getSize().width / 2, Solution.getNya.getY() + Solution.getNya.getHeight() / 2 - settingsDialog.getSize().height); - settingsDialog.setVisible(true); - - if (Window.hdOnly) - hdCheckBox.setState(true); - else - hdCheckBox.setState(false); + SettingsDialog settingsDialog = new SettingsDialog(); + settingsDialog.ShowSettingsDialog(); } } } diff --git a/Sources/SettingsDialog.java b/Sources/SettingsDialog.java new file mode 100644 index 0000000..4ddb312 --- /dev/null +++ b/Sources/SettingsDialog.java @@ -0,0 +1,203 @@ +import javax.swing.*; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import java.awt.*; +import java.awt.event.*; + +/** + * Created by LabunskyA. + * Protected with GNU GPLv2 and your honesty + */ +class SettingsDialog { + private Integer positionX = 0; + private Integer positionY = 0; + + public void ShowSettingsDialog(){ + Window.settingsIsOpen = true; + + final JFrame settingsDialog = new JFrame("Settings"); + final JTextField width = new JTextField(" Width: "); + final JTextField height = new JTextField(" Height: "); + final JTextField tag = new JTextField(Window.tag); + + String width4Scan = ""; + String height4Scan = ""; + if (Window.lessThanX) + width4Scan = "<"; + else if (Window.moreThanX) + width4Scan = ">"; + if (Window.moreThanY) + height4Scan = ">"; + else if (Window.lessThanY) + height4Scan = "<"; + if (Window.customResolution.width != 0) + width4Scan = width4Scan + Integer.toString(Window.customResolution.width); + if (Window.customResolution.height != 0) + height4Scan = height4Scan + Integer.toString(Window.customResolution.height); + final JTextField widthScan = new JTextField(width4Scan); + final JTextField heightScan = new JTextField(height4Scan); + + JTextField tagPointer = new JTextField(" Tag (only one, could works slowly): "); + + JPanel mainPanel = new JPanel(); + JPanel hdCheckBoxPanel = new JPanel(); + JPanel buttonsPanel = new JPanel(); + JPanel tagPanel = new JPanel(); + JPanel sizePanel = new JPanel(); + JPanel prevNyaPanel = new JPanel(); + JPanel closePanel = new JPanel(); + + Checkbox hdCheckBox = new Checkbox("HD nya only"); + + SimpleButton closeButton = new SimpleButton("resources/closeNya.png"); + SimpleButton prevNya = new SimpleButton("resources/prevNya.png", "resources/prevNyaPressed.png"); + + ActionListener getPrevNya = new GetPrevNya(); + + MouseAdapter dragDialogListener = new MouseAdapter() { + @Override + public void mouseDragged(MouseEvent e) { + super.mouseDragged(e); + settingsDialog.setLocation(e.getXOnScreen() - positionX, e.getYOnScreen() - positionY); + } + }; + MouseAdapter mouseMoveListener = new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) { + super.mousePressed(e); + positionX = e.getX(); + positionY = e.getY(); + } + }; + ActionListener closeSettings = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String value = widthScan.getText(); + if (value.length() == 0) { + Window.currentResolution = false; + Window.customResolution = new Dimension(0, Window.customResolution.height); + } else { + Window.currentResolution = true; + if (value.contains(">")) { + Window.moreThanX = true; + value = value.substring(1); + } + else Window.moreThanX = false; + if (value.contains("<")) { + Window.lessThanX = true; + value = value.substring(1); + } + else Window.lessThanX = false; + Window.customResolution = new Dimension(Integer.valueOf(value), Window.customResolution.height); + } + + value = heightScan.getText(); + if (value.length() == 0) { + Window.currentResolution = false; + Window.customResolution = new Dimension(Window.customResolution.width, 0); + } else { + Window.currentResolution = true; + if (value.contains(">")) { + Window.moreThanY = true; + value = value.substring(1); + } + else Window.moreThanY = false; + if (value.contains("<")) { + Window.lessThanY = true; + value = value.substring(1); + } + else Window.lessThanY = false; + Window.customResolution = new Dimension(Window.customResolution.width, Integer.valueOf(value)); + } + + if (tag.getText().length() == 0) { + Window.useTag = false; + Window.tag = ""; + } else { + Window.useTag = true; + Window.tag = tag.getText().toLowerCase(); + } + + settingsDialog.dispose(); + Window.settingsIsOpen = false; + } + }; + + hdCheckBox.addItemListener(new ItemListener() { + @Override + public void itemStateChanged(ItemEvent e) { + Window.hdOnly = !Window.hdOnly; + } + }); + mainPanel.addMouseListener(mouseMoveListener); + mainPanel.addMouseMotionListener(dragDialogListener); + closeButton.addActionListener(closeSettings); + prevNya.addActionListener(getPrevNya); + + tag.setPreferredSize(new Dimension(100, 20)); + heightScan.setPreferredSize(new Dimension(50, 20)); + widthScan.setPreferredSize(new Dimension(50, 20)); + + hdCheckBox.setBackground(Color.WHITE); + hdCheckBoxPanel.setLayout(new FlowLayout()); + hdCheckBoxPanel.setBackground(Color.WHITE); + hdCheckBoxPanel.add(hdCheckBox); + + sizePanel.setBackground(Color.WHITE); + sizePanel.setLayout(new FlowLayout()); + sizePanel.add(width); + sizePanel.add(widthScan); + sizePanel.add(height); + sizePanel.add(heightScan); + + tagPanel.setLayout(new FlowLayout()); + tagPanel.setBackground(Color.WHITE); + tagPanel.add(tagPointer); + tagPanel.add(tag); + + prevNyaPanel.setLayout(new FlowLayout()); + prevNyaPanel.setBackground(Color.WHITE); + prevNyaPanel.add(prevNya); + + closePanel.setLayout(new BoxLayout(closePanel, BoxLayout.Y_AXIS)); + closePanel.setBackground(Color.WHITE); + closePanel.add(Box.createRigidArea(new Dimension(0, 10))); + closePanel.add(closeButton); + + buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS)); + buttonsPanel.add(prevNyaPanel); + buttonsPanel.add(closePanel); + buttonsPanel.setBackground(Color.WHITE); + + tagPointer.setBackground(Color.WHITE); + tagPointer.setDisabledTextColor(Color.BLACK); + tagPointer.setEnabled(false); + + height.setBackground(Color.WHITE); + height.setDisabledTextColor(Color.BLACK); + height.setEnabled(false); + + width.setBackground(Color.WHITE); + width.setDisabledTextColor(Color.BLACK); + width.setEnabled(false); + + mainPanel.setBackground(Color.WHITE); + mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); + mainPanel.add(hdCheckBoxPanel); + mainPanel.add(tagPanel); + mainPanel.add(sizePanel); + mainPanel.add(buttonsPanel); + + settingsDialog.add(mainPanel); + settingsDialog.setUndecorated(true); + settingsDialog.setAlwaysOnTop(true); + settingsDialog.pack(); + settingsDialog.setLocation(Solution.getNya.getX() + Solution.getNya.getWidth() / 2 - settingsDialog.getSize().width / 2, Solution.getNya.getY() + Solution.getNya.getHeight() / 2 - settingsDialog.getSize().height); + settingsDialog.setVisible(true); + + if (Window.hdOnly) + hdCheckBox.setState(true); + else + hdCheckBox.setState(false); + } +} diff --git a/Sources/Window.java b/Sources/Window.java index 47e11f1..08dbc87 100644 --- a/Sources/Window.java +++ b/Sources/Window.java @@ -14,6 +14,10 @@ class Window extends JFrame { static Boolean hdOnly = false; static Boolean currentResolution = false; + static Boolean moreThanX = false; + static Boolean lessThanX = false; + static Boolean moreThanY = false; + static Boolean lessThanY = false; static Boolean settingsIsOpen = false; static Boolean useTag = false; static String tag = ""; @@ -47,7 +51,7 @@ BufferedImage getBufferedFullImage() { setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - ActionListener saveFullNya = new Listeners(); + ActionListener saveFullNya = new Save2File(); ActionListener getNewNya = new getNewNya(); ActionListener exitNya = new CloseNya(); ActionListener maximizeNyaWindow = new MaximizeNya(); @@ -137,6 +141,7 @@ void drawNya() throws MalformedURLException { setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); Boolean check = true; + Checker checker = new Checker(); LittleParser littleParser = new LittleParser(); while (check) { @@ -144,20 +149,14 @@ void drawNya() throws MalformedURLException { Zerochan.generateNumberNya(); Zerochan.generateURLs(); - if (useTag) { - if (littleParser.parse("http://www.zerochan.net/" + Zerochan.numberNya, LittleParser.TAG).contains(tag)) - check = false; - } else check = false; + check = checker.CheckTag(littleParser, true); } try { bufferedFullImage = ImageIO.read(Zerochan.fullURL); nyaFullHeight = bufferedFullImage.getHeight(); nyaFullWidth = bufferedFullImage.getWidth(); - if (hdOnly && (nyaFullHeight < 1080 || nyaFullWidth < 1920)) - check = true; - else if (currentResolution && (nyaFullHeight != customResolution.getHeight() || nyaFullWidth != customResolution.getWidth())) - check = true; + check = checker.CheckSize(nyaFullWidth, nyaFullHeight, check); } catch (IOException e) { check = true; } //sometimes Zerochan.net blocks some pictures, so I need to handle this occasion and get another url with another picture } @@ -240,4 +239,36 @@ public SimpleButton(String path) { setBackground(Color.WHITE); } catch (IOException ignored) {} } +} + +class Checker { + public boolean CheckTag(LittleParser littleParser, Boolean check) throws MalformedURLException { + if (Window.useTag) { + if (littleParser.parse("http://www.zerochan.net/" + Zerochan.numberNya, LittleParser.TAG).contains(Window.tag)) + check = false; + } else check = false; + + return check; + } + + public boolean CheckSize(Integer nyaWidth, Integer nyaHeight, Boolean check) { + if (Window.hdOnly && (nyaWidth < 1920 || nyaHeight < 1080)) + check = true; + + if (Window.currentResolution) { + if (Window.moreThanY && nyaHeight < Window.customResolution.height) + check = true; + if (Window.lessThanY && nyaHeight > Window.customResolution.height) + check = true; + if (Window.moreThanX && nyaWidth < Window.customResolution.width) + check = true; + if (Window.lessThanX && nyaWidth > Window.customResolution.width) + check = true; + + if (!Window.moreThanY && !Window.moreThanX && !Window.lessThanY && !Window.lessThanX && (nyaHeight != Window.customResolution.height || nyaWidth != Window.customResolution.width)) + check = true; + } + + return check; + } } \ No newline at end of file diff --git a/Sources/resources/closeNya.png b/Sources/resources/closeNya.png index 4b02571ba703468e651a3d7d9bce63c9e9c9913e..d6db5350f37c5e5d46ebf85ba94d0b982109ee6f 100644 GIT binary patch delta 211 zcmZ3&)XX$Ng`X|S+uel$41PNAuAgWpQO{oD>FdgVpHYa%$S}4-{vuFvf~SjPh{frp zlO4s5C~!DW-e)N`Pww5+$V|=JS0$(1#GXkDa5z*5&Y!pAw{l{3?vtYOuf^g&^%!Jt zIxbrmcP@|pz!?Qj75{9ZC7WGU6gkt%*i!=AC+vA5X~r6I!mH+It7wYaJgW+!V2ACo zmZyJm24u%M_RXzemu%>WD6@7}dc?Ov*sR-XTEa9zrJk`k$lZxy-8q?;3=9k`>5jgR3=A9lx&I`x0{NT;9+AZi417mGm~pB$ zpEOXAEy>&6h2cL4F4((#GEjuQ#M9T6{XQcXzp&PsOs4NZp(&m&jv*HQOD9M29Z=wK zS^E9c7r8a19>;wIdOQEzV3j+(XzyJwbK}GHg*o*Rh71ySqO6&&MBU{VSW?PrGAVq? ziZ6|iGt*j)jxkPoJZXt|MY!t^@%_9FO&kYTo{y{)PB3wwVo-c-y2yeFu?BypH7u@t z$8c-I-APNi7QL~M4K-&BGhMI1d|={Rvxx$_FRP}$*T4L>^dG}D*R0w1EtPA44rK6j M^>bP0#FWqk00_xt(f|Me diff --git a/Sources/resources/maximizeNya.png b/Sources/resources/maximizeNya.png index a5c073cbc16a88b847cbbba276c42d1ce8264a13..1269f2de4497d5c07b4d8f62274f8459d9737301 100644 GIT binary patch delta 143 zcmV;A0C4|-0)hgNdw+6CL_t(IPqmX_4uBvCg`so7>G56^Z8R%rs~jHt~u4*m>u xje~3pr5R)c^!s#;{yZ!F_u4t1sZur5>?G?!7oA;#wEzGB00>D%PDHLkV1fxxJo*3t delta 142 zcmV;90CE3<0)YaMdw+3BL_t(IPqmYQ3cw%;M05Y;*ZW~xCkf(eI4nVe$mew>-91_l zBEpi6QPv0mD;ALyfSL6iAZV)!jF=)g#TN>)oju%Y6i|s50DgRBY_?y4846Ql&aQe! w!5>r4C|JEh%7dACelJ4GU%5U^)p33R+8tP%GG%GZ00000Ne4wvM6N<$f;AO5-2eap diff --git a/Sources/resources/minimizeNya.png b/Sources/resources/minimizeNya.png index e5c19f97574ef3b72ba5eb1fbd6fe4694e8deade..8eb16f8f3272a66c782fba7eb6d36073714a821f 100644 GIT binary patch delta 129 zcmV-{0Dk}Q0rdfpdwOL_L_t(IPwkUI4uCKS1Q*{0uZMr3m{hB=4QV*qWN*XJKuZAp z$nv2OsOpkHMDP$Wb4p}pCl`XPRb-2U@7YU`YW)HI0^D0D8zK>5mUpB2Ex0>o8Q!2h j&&(`(w&pWsx{jC~G;%K&f2bqm00000NkvXXu0mjfYQs0I delta 124 zcmV-?0E7Sa0q_Bkdv#z*L_t(IPwkVz3IHGoLtX#n*ZX1BBq2J4V8;g1r9DR}`jkuo z;E~p{2?z+YnWX@#>h}m?TTc+g6k$u;XvlWn;o)&WMIRvY{tY-(w>p39eF9G8t