Добавлена функция удаления прокси; список прокси перенесен в подменю

This commit is contained in:
BelPE 2023-04-27 09:59:08 +08:00
parent 744bca6839
commit 6c31ac0e89
4 changed files with 118 additions and 35 deletions

View File

@ -1,8 +1,6 @@
{
"port" : "7458",
"proxyList" : [
"https://notaproxy.ru",
"http://proxy.com",
"http://ideco.mcs.br/wpad.dat",
"http://proxy.mcs.br:8080/array.dll?Get.Routing.Script"
],

Binary file not shown.

View File

@ -12,6 +12,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import static java.awt.Component.LEFT_ALIGNMENT;
import static javax.swing.JOptionPane.showMessageDialog;
import static org.dhaverdLogs.DhaverdLogs.*;
import static org.proxy.loadConfig.*;
@ -98,38 +99,8 @@ public class Main {
setLog(context,"System tray is unsupported!", true, false);
return;
}
List<String> proxyList = getProxyList();
currentProxyList = proxyList;
PopupMenu trayMenu = new PopupMenu();
MenuItem addButton = new MenuItem("Add proxy...");
addButton.setFont(trayFont);
addButton.addActionListener(addListener());
trayMenu.add(addButton);
for (String prox : proxyList){
MenuItem proxy = new MenuItem(prox);
proxy.setFont(trayFont);
proxy.addActionListener(proxyListener(prox));
trayMenu.add(proxy);
}
MenuItem stopButton = new MenuItem("Stop");
stopButton.setFont(trayFont);
stopButton.addActionListener(stopListener());
trayMenu.add(stopButton);
MenuItem trayExit = new MenuItem("Exit");
trayExit.setFont(trayFont);
trayExit.addActionListener(trayExitListener());
trayMenu.add(trayExit);
Image icon;
icon = Toolkit.getDefaultToolkit().getImage("res" + osSeparator + "icon off.png");
trayIcon = new TrayIcon(icon, "Current proxy: " + currentProxyPub, trayMenu);
trayIcon.setImageAutoSize(!isLinux);
tray = SystemTray.getSystemTray();
try {
tray.add(trayIcon);
} catch (AWTException e) {
exeptionActions(context, e);
}
loadMenu();
setLog(context,"System tray launched", true, false);
}
@ -179,8 +150,12 @@ public class Main {
}
public static void reloadMenu() throws IOException, ParseException {
String context = "Reload tray menu";
tray.remove(trayIcon);
loadMenu();
}
public static void loadMenu() throws IOException, ParseException {
String context = "Load tray menu";
List<String> proxyList = getProxyList();
currentProxyList = proxyList;
PopupMenu trayMenu = new PopupMenu();
@ -188,12 +163,18 @@ public class Main {
addButton.setFont(trayFont);
addButton.addActionListener(addListener());
trayMenu.add(addButton);
Menu proxyMenu = new Menu("Proxy");
trayMenu.add(proxyMenu);
for (String prox : proxyList){
MenuItem proxy = new MenuItem(prox);
proxy.setFont(trayFont);
proxy.addActionListener(proxyListener(prox));
trayMenu.add(proxy);
proxyMenu.add(proxy);
}
MenuItem deleteButton = new MenuItem("Delete proxy...");
deleteButton.setFont(trayFont);
deleteButton.addActionListener(deleteListener());
trayMenu.add(deleteButton);
MenuItem stopButton = new MenuItem("Stop");
stopButton.setFont(trayFont);
stopButton.addActionListener(stopListener());
@ -283,6 +264,110 @@ public class Main {
frame.setVisible(true);
};
}
public static ActionListener deleteListener(){
return e -> {
String context = "Delete Listener";
//frame
JFrame frame = new JFrame("Add proxy");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(300,200);
frame.setLocationRelativeTo(null);
//main panel
JPanel mainPanel = new JPanel();
GridBagLayout layout = new GridBagLayout();
mainPanel.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
try {
List<String> proxyList = getProxyList();
List<JCheckBox> checkBoxList = new ArrayList<>();
for (String proxy : proxyList){
JCheckBox checkBox = new JCheckBox(proxy);
checkBoxList.add(checkBox);
mainPanel.add(checkBox, gbc);
gbc.gridy++;
}
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(5, 10, 5, 10);
JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(al ->{
List<String> proxyToDelete = new ArrayList<>();
for (JCheckBox checkBox : checkBoxList){
if (checkBox.isSelected()){
proxyToDelete.add(checkBox.getText());
}
}
try {
deleteProxy(proxyToDelete);
reloadMenu();
} catch (IOException | ParseException ex) {
exeptionActions(context, ex);
}
frame.dispose();
});
gbc.weightx = 0.5;
mainPanel.add(deleteButton, gbc);
JButton closeButton = new JButton("Close");
gbc.gridx = 1;
closeButton.addActionListener(al -> {
frame.dispose();
});
mainPanel.add(closeButton, gbc);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
} catch (IOException | ParseException ex) {
exeptionActions(context, ex);
}
};
}
public static void deleteProxy(List<String> proxyToDelete) throws IOException {
File config = getConfigFile();
FileReader reader = new FileReader(config);
Scanner sc = new Scanner(reader);
List<String> propsList = new ArrayList<>();
while (sc.hasNext()){
String line = sc.nextLine();
propsList.add(line);
}
sc.close();
reader.close();
List<String> newProps = new ArrayList<>();
for (String prop : propsList){
boolean isToDelete = false;
for (String deleteProxy : proxyToDelete){
if (prop.contains(deleteProxy)){
isToDelete = true;
setLog("Delete Proxy", "Proxy is deleted: " + deleteProxy, true, false);
break;
}
}
if (!isToDelete){
newProps.add(prop);
}
}
/*
setLog("Add new proxy function", "New proxy list:", true, false);
for (String prop : newProps) {
setLog("Add new proxy function", prop, false, true);
}
*/
FileWriter writer = new FileWriter(config, false);
for (String prop : newProps) {
writer.write(prop + "\n");
}
writer.close();
}
public static ActionListener stopListener() {
return e -> {
closeThread();