diff --git a/src/net/i2p/itoopie/Main.java b/src/net/i2p/itoopie/Main.java index 72c5bf91c..11d65aa8b 100644 --- a/src/net/i2p/itoopie/Main.java +++ b/src/net/i2p/itoopie/Main.java @@ -5,6 +5,7 @@ package net.i2p.itoopie; */ import java.util.Arrays; +import java.util.EnumMap; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; @@ -82,7 +83,7 @@ public class Main { } catch (Exception e) { _log.error("Error during TrayManager launch.", e); } - testStuff(); // Delete Me + //testStuff(); // Delete Me } @SuppressWarnings("static-access") @@ -146,9 +147,9 @@ public class Main { // Test reading all settings System.out.println("GetNetworkSetting"); try { - HashMap hm = GetNetworkSetting.execute(NETWORK_SETTING.values()); + EnumMap em = GetNetworkSetting.execute(NETWORK_SETTING.values()); System.out.println("getNetworkInfo: All: "); - Set set = hm.entrySet(); + Set> set = em.entrySet(); for (Entry e : set){ System.out.println(e.getKey() +":"+ e.getValue()); } @@ -169,9 +170,9 @@ public class Main { for (NETWORK_SETTING i : list){ hm.put(i, "66"); // 66 is an arbitrary number that should work for most fields. } - HashMap nextHM= SetNetworkSetting.execute(hm); + EnumMap nextHM= SetNetworkSetting.execute(hm); System.out.println("setNetworkInfo: All: "); - Set set = nextHM.entrySet(); + Set> set = nextHM.entrySet(); for (Entry e : set){ System.out.println(e.getKey() +":"+ e.getValue()); } @@ -201,9 +202,9 @@ public class Main { hm.put(NETWORK_SETTING.UDP_PORT, "66"); hm.put(NETWORK_SETTING.UPNP, "true"); - HashMap nextHM= SetNetworkSetting.execute(hm); + EnumMap nextHM= SetNetworkSetting.execute(hm); System.out.println("setNetworkInfo: Manual: "); - Set set = nextHM.entrySet(); + Set> set = nextHM.entrySet(); for (Entry e : set){ System.out.println(e.getKey() +":"+ e.getValue()); } diff --git a/src/net/i2p/itoopie/gui/ConfigurationPanel.java b/src/net/i2p/itoopie/gui/ConfigurationPanel.java deleted file mode 100644 index e5f347fe0..000000000 --- a/src/net/i2p/itoopie/gui/ConfigurationPanel.java +++ /dev/null @@ -1,230 +0,0 @@ -package net.i2p.itoopie.gui; - -import java.awt.BorderLayout; -import java.awt.EventQueue; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; -import java.util.HashMap; - -import javax.swing.JButton; -import javax.swing.JCheckBox; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.JTextField; -import javax.swing.SwingConstants; -import javax.swing.SwingUtilities; - -import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException; - -import net.i2p.itoopie.gui.StatusHandler.DEFAULT_STATUS; -import net.i2p.itoopie.gui.component.LogoPanel; -import net.i2p.itoopie.i18n.Transl; -import net.i2p.itoopie.i2pcontrol.InvalidPasswordException; -import net.i2p.itoopie.i2pcontrol.methods.GetNetworkSetting; -import net.i2p.itoopie.i2pcontrol.methods.NetworkSetting.NETWORK_SETTING; - -// The width of this panel (on ubuntu) will be the width of the main menu -24px. -public class ConfigurationPanel extends LogoPanel { - // Components for the network panel - private JLabel lblNetwork; - private JLabel lblTCPPort; - private JTextField txtTCPPort; - private JLabel lblUDPPort; - private JTextField txtUDPPort; - private JLabel lblUPNP; - private JCheckBox chkbxUPNP; - // Components for the bandwidth panel - private JLabel lblBW; - private JLabel lblDownload; - private JTextField txtDownload; - private JLabel lblDownloadUnit; - private JLabel lblUpload; - private JTextField txtUpload; - private JLabel lblUploadUnit; - private JLabel lblShare; - private JTextField txtShare; - private JLabel lblShareUnit; - - private static final long serialVersionUID = 328657255717753899L; - - public ConfigurationPanel(String imageName) { - super(imageName); - setLayout(null); - - JPanel networkPanel = new JPanel(); - add(networkPanel); - networkPanel.setLayout(null); - networkPanel.setOpaque(false); - networkPanel.setBounds(0, 110, 262, 115); - setupNetworkPanel(networkPanel); - - JPanel bwPanel = new JPanel(); - add(bwPanel); - bwPanel.setLayout(null); - bwPanel.setOpaque(false); - bwPanel.setBounds(0, 5, 262, 105); - setupBandwidthPanel(bwPanel); - - - final JButton btnApply = new JButton(Transl._("Apply")); - add(btnApply); - btnApply.setBounds(450, 272, 75, 24); - btnApply.addActionListener(new ActionListener(){ - @Override - public void actionPerformed(ActionEvent arg0) { - System.out.println("Clicked"); - } - }); - - - - - SwingUtilities.invokeLater(new Runnable(){ - @Override - public void run() { - System.out.println("Running populate thread."); - populate(); - System.out.println("Populate thread done."); - } - }); - - validate(); - } - - /** - * Setup bandwidth panel - */ - private void setupBandwidthPanel(JPanel bwPanel){ - lblBW = new JLabel(); - bwPanel.add(lblBW); - lblBW.setBounds(10, 10, 120, 15); - lblBW.setText(Transl._("Bandwidth:")); - - lblDownload = new JLabel(); - bwPanel.add(lblDownload); - lblDownload.setBounds(40, 35, 100, 15); - lblDownload.setText(Transl._("Download:")); - - txtDownload = new JTextField(); - bwPanel.add(txtDownload); - txtDownload.setBounds(130, 35, 55, 20); - - lblDownloadUnit = new JLabel(); - bwPanel.add(lblDownloadUnit); - lblDownloadUnit.setBounds(190, 37, 35, 15); - lblDownloadUnit.setText(Transl._("KB/s")); - - lblUpload = new JLabel(); - bwPanel.add(lblUpload); - lblUpload.setBounds(40, 60, 100, 15); - lblUpload.setText(Transl._("Upload:")); - - txtUpload = new JTextField(); - bwPanel.add(txtUpload); - txtUpload.setBounds(130, 60, 55, 20); - - lblUploadUnit = new JLabel(); - bwPanel.add(lblUploadUnit); - lblUploadUnit.setBounds(190, 62, 35, 15); - lblUploadUnit.setText(Transl._("KB/s")); - - lblShare = new JLabel(); - bwPanel.add(lblShare); - lblShare.setBounds(40, 85, 100, 15); - lblShare.setText(Transl._("Share:")); - - txtShare = new JTextField(); - bwPanel.add(txtShare); - txtShare.setBounds(130, 85, 55, 20); - - lblShareUnit = new JLabel(); - bwPanel.add(lblShareUnit); - lblShareUnit.setBounds(190, 87, 35, 15); - lblShareUnit.setText(Transl._("%")); - } - - /** - * Setup network panel - */ - private void setupNetworkPanel(JPanel networkPanel){ - lblNetwork = new JLabel(); - networkPanel.add(lblNetwork); - lblNetwork.setBounds(10, 10, 75, 15); - lblNetwork.setText(Transl._("Network:")); - - lblTCPPort = new JLabel(); - networkPanel.add(lblTCPPort); - lblTCPPort.setBounds(40, 35, 75, 15); - lblTCPPort.setText(Transl._("TCP port:")); - - txtTCPPort = new JTextField(); - networkPanel.add(txtTCPPort); - txtTCPPort.setBounds(130, 35, 55, 20); - txtTCPPort.setColumns(5); - - lblUDPPort = new JLabel(); - networkPanel.add(lblUDPPort); - lblUDPPort.setBounds(40, 60, 75, 15); - lblUDPPort.setText(Transl._("UDP port:")); - - txtUDPPort = new JTextField(); - networkPanel.add(txtUDPPort); - txtUDPPort.setBounds(130, 60, 55, 20); - - - lblUPNP = new JLabel(); - networkPanel.add(lblUPNP); - lblUPNP.setBounds(40, 85, 75, 15); - lblUPNP.setText(Transl._("UPNP:")); - - chkbxUPNP = new JCheckBox(Transl._("Enable UPNP")); - networkPanel.add(chkbxUPNP); - chkbxUPNP.setBounds(127, 85, 120, 15); - } - - private void populate(){ - try { - HashMap hm = GetNetworkSetting.execute(NETWORK_SETTING.TCP_PORT, NETWORK_SETTING.UDP_PORT, - NETWORK_SETTING.UPNP, NETWORK_SETTING.BW_IN, NETWORK_SETTING.BW_OUT); - - System.out.println("TCP_PORT: " + (String) hm.get(NETWORK_SETTING.TCP_PORT.toString())); - System.out.println("UDP_PORT: " + (String) hm.get(NETWORK_SETTING.UDP_PORT.toString())); - - txtTCPPort.setText((String) hm.get(NETWORK_SETTING.TCP_PORT.toString())); - txtUDPPort.setText((String) hm.get(NETWORK_SETTING.UDP_PORT.toString())); - boolean upnpValue = Boolean.parseBoolean((String) hm.get(NETWORK_SETTING.UPNP)); - chkbxUPNP.setSelected(upnpValue); - txtDownload.setText((String) hm.get(NETWORK_SETTING.BW_IN)); - txtUpload.setText((String) hm.get(NETWORK_SETTING.BW_OUT)); - txtShare.setText((String) hm.get(NETWORK_SETTING.BW_SHARE)); - txtShare.setText("TEST"); - } catch (InvalidPasswordException e) { - StatusHandler.setDefaultStatus(DEFAULT_STATUS.INVALID_PASSWORD); - } catch (JSONRPC2SessionException e) { - StatusHandler.setDefaultStatus(DEFAULT_STATUS.NOT_CONNECTED); - } - } - - /** - * Launch the application. - */ - public static void main(String[] args) { - EventQueue.invokeLater(new Runnable() { - public void run() { - try { - JFrame frame = new JFrame(); - frame.setBounds(0, 0, Main.FRAME_WIDTH, Main.FRAME_HEIGHT); - ConfigurationPanel window = new ConfigurationPanel("itoopie-opaque12"); - frame.add(window); - frame.setVisible(true); - } catch (Exception e) { - e.printStackTrace(); - } - } - }); - } -} \ No newline at end of file diff --git a/src/net/i2p/itoopie/gui/ConfigurationTab.java b/src/net/i2p/itoopie/gui/ConfigurationTab.java new file mode 100644 index 000000000..ac302f313 --- /dev/null +++ b/src/net/i2p/itoopie/gui/ConfigurationTab.java @@ -0,0 +1,396 @@ +package net.i2p.itoopie.gui; + +import java.awt.BorderLayout; +import java.awt.EventQueue; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.FocusAdapter; +import java.awt.event.FocusEvent; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Map.Entry; + +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.SwingConstants; +import javax.swing.SwingUtilities; +import javax.swing.event.ChangeEvent; + +import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException; + +import net.i2p.itoopie.gui.StatusHandler.DEFAULT_STATUS; +import net.i2p.itoopie.gui.component.LogoPanel; +import net.i2p.itoopie.gui.component.TabLogoPanel; +import net.i2p.itoopie.i18n.Transl; +import net.i2p.itoopie.i2pcontrol.InvalidParametersException; +import net.i2p.itoopie.i2pcontrol.InvalidPasswordException; +import net.i2p.itoopie.i2pcontrol.methods.GetNetworkSetting; +import net.i2p.itoopie.i2pcontrol.methods.NetworkSetting.NETWORK_SETTING; +import net.i2p.itoopie.i2pcontrol.methods.SetNetworkSetting; + +// The width of this panel (on ubuntu) will be the width of the main menu -24px. +public class ConfigurationTab extends TabLogoPanel { + private final static String SETTINGS_READ = Transl._("Settings read from I2P router."); + private static enum SAVE_STATUS{ + SAVE_FAILED_LOCALLY { public String toString(){return Transl._("Settings aren't valid, not saving.");} }, + SAVE_FAILED_REMOTELY { public String toString(){return Transl._("I2P router rejected settings.");} }, + SAVED_OK { public String toString(){return Transl._("Saved settings on I2P router.");} }, + SAVED_RESTART_NEEDED { public String toString(){return Transl._("Saved settings on I2P router. I2P router needs to be restarted.");} } + }; + + // Components for the network panel + private JLabel lblNetwork; + private JLabel lblTCPPort; + private JTextField txtTCPPort; + private JLabel lblUDPPort; + private JTextField txtUDPPort; + private JLabel lblUPNP; + private JCheckBox chkbxUPNP; + // Components for the bandwidth panel + private JLabel lblBW; + private JLabel lblDownload; + private JTextField txtDownload; + private JLabel lblDownloadUnit; + private JLabel lblUpload; + private JTextField txtUpload; + private JLabel lblUploadUnit; + private JLabel lblShare; + private JTextField txtShare; + private JLabel lblShareUnit; + + private static final long serialVersionUID = 328657255717753899L; + + public ConfigurationTab(String imageName) { + super(imageName); + setLayout(null); + + JPanel networkPanel = new JPanel(); + add(networkPanel); + networkPanel.setLayout(null); + networkPanel.setOpaque(false); + networkPanel.setBounds(0, 110, 262, 115); + setupNetworkPanel(networkPanel); + + JPanel bwPanel = new JPanel(); + add(bwPanel); + bwPanel.setLayout(null); + bwPanel.setOpaque(false); + bwPanel.setBounds(0, 5, 262, 105); + setupBandwidthPanel(bwPanel); + + + final JButton btnApply = new JButton(Transl._("Apply")); + add(btnApply); + btnApply.setBounds(450, 272, 75, 24); + btnApply.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent arg0) { + SwingUtilities.invokeLater(new Runnable(){ + @Override + public void run() { + applyNewSettings(); + btnApply.repaint(); // Removes a transparent ring around button after clicks. + } + }); + } + }); + + + validate(); + } + + /** + * Setup bandwidth panel + */ + private void setupBandwidthPanel(JPanel bwPanel){ + lblBW = new JLabel(); + bwPanel.add(lblBW); + lblBW.setBounds(10, 10, 120, 15); + lblBW.setText(Transl._("Bandwidth:")); + + lblDownload = new JLabel(); + bwPanel.add(lblDownload); + lblDownload.setBounds(40, 35, 100, 15); + lblDownload.setText(Transl._("Download:")); + + txtDownload = new JTextField(); + bwPanel.add(txtDownload); + txtDownload.setBounds(130, 35, 55, 20); + + lblDownloadUnit = new JLabel(); + bwPanel.add(lblDownloadUnit); + lblDownloadUnit.setBounds(190, 37, 35, 15); + lblDownloadUnit.setText(Transl._("KB/s")); + + lblUpload = new JLabel(); + bwPanel.add(lblUpload); + lblUpload.setBounds(40, 60, 100, 15); + lblUpload.setText(Transl._("Upload:")); + + txtUpload = new JTextField(); + bwPanel.add(txtUpload); + txtUpload.setBounds(130, 60, 55, 20); + + lblUploadUnit = new JLabel(); + bwPanel.add(lblUploadUnit); + lblUploadUnit.setBounds(190, 62, 35, 15); + lblUploadUnit.setText(Transl._("KB/s")); + + lblShare = new JLabel(); + bwPanel.add(lblShare); + lblShare.setBounds(40, 85, 100, 15); + lblShare.setText(Transl._("Share:")); + + txtShare = new JTextField(); + bwPanel.add(txtShare); + txtShare.setBounds(130, 85, 55, 20); + + lblShareUnit = new JLabel(); + bwPanel.add(lblShareUnit); + lblShareUnit.setBounds(190, 87, 35, 15); + lblShareUnit.setText(Transl._("%")); + } + + /** + * Setup network panel + */ + private void setupNetworkPanel(JPanel networkPanel){ + lblNetwork = new JLabel(); + networkPanel.add(lblNetwork); + lblNetwork.setBounds(10, 10, 75, 15); + lblNetwork.setText(Transl._("Network:")); + + lblTCPPort = new JLabel(); + networkPanel.add(lblTCPPort); + lblTCPPort.setBounds(40, 35, 75, 15); + lblTCPPort.setText(Transl._("TCP port:")); + + txtTCPPort = new JTextField(); + networkPanel.add(txtTCPPort); + txtTCPPort.setBounds(130, 35, 55, 20); + txtTCPPort.setColumns(5); + + lblUDPPort = new JLabel(); + networkPanel.add(lblUDPPort); + lblUDPPort.setBounds(40, 60, 75, 15); + lblUDPPort.setText(Transl._("UDP port:")); + + txtUDPPort = new JTextField(); + networkPanel.add(txtUDPPort); + txtUDPPort.setBounds(130, 60, 55, 20); + + + lblUPNP = new JLabel(); + networkPanel.add(lblUPNP); + lblUPNP.setBounds(40, 85, 75, 15); + lblUPNP.setText(Transl._("UPNP:")); + + chkbxUPNP = new JCheckBox(Transl._("Enable UPNP")); + networkPanel.add(chkbxUPNP); + chkbxUPNP.setBounds(127, 85, 120, 15); + } + + private void populateSettings(){ + try { + EnumMap em = GetNetworkSetting.execute(NETWORK_SETTING.TCP_PORT, NETWORK_SETTING.UDP_PORT, + NETWORK_SETTING.UPNP, NETWORK_SETTING.BW_IN, NETWORK_SETTING.BW_OUT, NETWORK_SETTING.BW_SHARE); + + txtTCPPort.setText((String) em.get(NETWORK_SETTING.TCP_PORT)); + txtUDPPort.setText((String) em.get(NETWORK_SETTING.UDP_PORT)); + boolean upnpValue = Boolean.parseBoolean((String) em.get(NETWORK_SETTING.UPNP)); + chkbxUPNP.setSelected(upnpValue); + txtDownload.setText((String) em.get(NETWORK_SETTING.BW_IN)); + txtUpload.setText((String) em.get(NETWORK_SETTING.BW_OUT)); + txtShare.setText((String) em.get(NETWORK_SETTING.BW_SHARE)); + StatusHandler.setStatus(SETTINGS_READ); + + } catch (InvalidPasswordException e) { + StatusHandler.setDefaultStatus(DEFAULT_STATUS.INVALID_PASSWORD); + } catch (JSONRPC2SessionException e) { + StatusHandler.setDefaultStatus(DEFAULT_STATUS.NOT_CONNECTED); + } + } + + /** + * Launch the application. + */ + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + JFrame frame = new JFrame(); + frame.setBounds(0, 0, Main.FRAME_WIDTH, Main.FRAME_HEIGHT); + ConfigurationTab window = new ConfigurationTab("itoopie-opaque12"); + frame.add(window); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + private SAVE_STATUS saveSettings(){ + String tcpPort = txtTCPPort.getText(); + String udpPort = txtUDPPort.getText(); + String upnp = Boolean.toString(chkbxUPNP.isSelected()); + String download = txtDownload.getText(); + String upload = txtUpload.getText(); + String share = txtShare.getText(); + + int tcpPortOutput = -1; // Not a valid value. + int udpPortOutput = -1; // Not a valid value. + int downloadOutput; + int uploadOutput; + int shareOutput = -1; // Not a valid value. + + //Check TCP port + try { + tcpPortOutput = Integer.parseInt(tcpPort); + if (tcpPortOutput < 1 || tcpPortOutput > 65535){ + throw new NumberFormatException(); + } + } catch (NumberFormatException e){ + JOptionPane.showConfirmDialog( + this, + Transl._("' "+tcpPort + " ' can not be interpreted as a TCP port.\r\n" + + "\r\nA port has to be a number in the range 1-65535." + + "\r\n\r\nWould you like to ignore that the TCP port isn't set?"), + Transl._("Invalid port."), + JOptionPane.YES_NO_OPTION, + JOptionPane.ERROR_MESSAGE); + } + + //Check UDP port + try { + udpPortOutput = Integer.parseInt(udpPort); + if (udpPortOutput < 1 || udpPortOutput > 65535){ + throw new NumberFormatException(); + } + } catch (NumberFormatException e){ + JOptionPane.showConfirmDialog( + this, + Transl._("' "+udpPort + " ' can not be interpreted as a UDP port.\r\n" + + "\r\nA port has to be a number in the range 1-65535."), + Transl._("Invalid port."), + JOptionPane.DEFAULT_OPTION, + JOptionPane.ERROR_MESSAGE); + return SAVE_STATUS.SAVE_FAILED_LOCALLY; + } + + //Check download + try { + downloadOutput = Integer.parseInt(download); + if (downloadOutput < 1){ + throw new NumberFormatException(); + } + } catch (NumberFormatException e){ + JOptionPane.showConfirmDialog( + this, + Transl._("' "+download + " ' can not be interpreted as an download spped.\r\n" + + "\r\nA port has to be a number larger than 0."), + Transl._("Invalid download speed."), + JOptionPane.DEFAULT_OPTION, + JOptionPane.ERROR_MESSAGE); + return SAVE_STATUS.SAVE_FAILED_LOCALLY; + } + + //Check Upload + try { + uploadOutput = Integer.parseInt(upload); + if (uploadOutput < 1){ + throw new NumberFormatException(); + } + } catch (NumberFormatException e){ + JOptionPane.showConfirmDialog( + this, + Transl._("' "+upload + " ' can not be interpreted as an upload spped.\r\n" + + "\r\nA port has to be a number larger than 0."), + Transl._("Invalid upload speed."), + JOptionPane.DEFAULT_OPTION, + JOptionPane.ERROR_MESSAGE); + return SAVE_STATUS.SAVE_FAILED_LOCALLY; + } + + //Check share + try { + shareOutput = Integer.parseInt(share); + if (shareOutput < 0 || shareOutput > 100){ + throw new NumberFormatException(); + } + } catch (NumberFormatException e){ + JOptionPane.showConfirmDialog( + this, + Transl._("' "+share + " ' can not be interpreted as a percentage.\r\n" + + "\r\nThe percentage has to be a number in the range 0-100."), + Transl._("Invalid share percentage."), + JOptionPane.DEFAULT_OPTION, + JOptionPane.ERROR_MESSAGE); + } + + HashMap output = new HashMap(); + + output.put(NETWORK_SETTING.TCP_PORT, tcpPortOutput+""); + output.put(NETWORK_SETTING.UDP_PORT, udpPortOutput+""); + output.put(NETWORK_SETTING.BW_IN, downloadOutput+""); + output.put(NETWORK_SETTING.BW_OUT, uploadOutput+""); + output.put(NETWORK_SETTING.BW_SHARE, shareOutput+""); + + try { + EnumMap em = SetNetworkSetting.execute(output); + StatusHandler.setDefaultStatus(DEFAULT_STATUS.CONNECTED); + boolean settingsSaved = (Boolean) em.get(NETWORK_SETTING.SETTINGS_SAVED); + boolean restartNeeded = (Boolean) em.get(NETWORK_SETTING.RESTART_NEEDED); + if (settingsSaved && restartNeeded){ + return SAVE_STATUS.SAVED_RESTART_NEEDED; + } else if (settingsSaved){ + return SAVE_STATUS.SAVED_OK; + } else { + return SAVE_STATUS.SAVE_FAILED_REMOTELY; + } + } catch (InvalidPasswordException e) { + StatusHandler.setDefaultStatus(DEFAULT_STATUS.INVALID_PASSWORD); + return SAVE_STATUS.SAVE_FAILED_LOCALLY; + } catch (JSONRPC2SessionException e) { + StatusHandler.setDefaultStatus(DEFAULT_STATUS.NOT_CONNECTED); + return SAVE_STATUS.SAVE_FAILED_LOCALLY; + } catch (InvalidParametersException e) { + return SAVE_STATUS.SAVE_FAILED_REMOTELY; + } + } + + private void applyNewSettings(){ + switch (saveSettings()){ + case SAVED_RESTART_NEEDED: + StatusHandler.setStatus(SAVE_STATUS.SAVED_RESTART_NEEDED.toString()); + break; + case SAVED_OK: + StatusHandler.setStatus(SAVE_STATUS.SAVED_OK.toString()); + break; + case SAVE_FAILED_REMOTELY: + StatusHandler.setStatus(SAVE_STATUS.SAVE_FAILED_REMOTELY.toString()); + break; + case SAVE_FAILED_LOCALLY: + StatusHandler.setStatus(SAVE_STATUS.SAVE_FAILED_LOCALLY.toString()); + break; + } + } + + @Override + public void onTabFocus(ChangeEvent e) { + SwingUtilities.invokeLater(new Runnable(){ + @Override + public void run() { + populateSettings(); + } + }); + } +} \ No newline at end of file diff --git a/src/net/i2p/itoopie/gui/Main.java b/src/net/i2p/itoopie/gui/Main.java index e23e2ee10..5dcf401cd 100644 --- a/src/net/i2p/itoopie/gui/Main.java +++ b/src/net/i2p/itoopie/gui/Main.java @@ -22,6 +22,8 @@ import javax.swing.ImageIcon; import net.i2p.itoopie.gui.component.BandwidthChart; import net.i2p.itoopie.gui.component.LogoPanel; import net.i2p.itoopie.gui.component.ParticipatingTunnelsChart; +import net.i2p.itoopie.gui.component.TabLogoPanel; +import net.i2p.itoopie.gui.component.util.TabChangeListener; import net.i2p.itoopie.util.IconLoader; import javax.swing.UIManager; import javax.swing.border.Border; @@ -79,27 +81,14 @@ public class Main { JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); frame.getContentPane().add(tabbedPane, BorderLayout.CENTER); - JPanel overviewPanel = new LogoPanel("itoopie-opaque12"); + TabLogoPanel overviewPanel = new OverviewTab("itoopie-opaque12"); tabbedPane.addTab("Overview", null, overviewPanel, null); - overviewPanel.setLayout(null); - - Chart2D bwChart = BandwidthChart.getChart(); - Chart2D partTunnelChart = ParticipatingTunnelsChart.getChart(); - ChartPanel pt = new ChartPanel(partTunnelChart); - pt.setSize(300, 135); - pt.setLocation(15, 10); - pt.setBorder(BorderFactory.createLineBorder(Color.GRAY)); - ChartPanel cp = new ChartPanel(bwChart); - cp.setSize(300,135); - cp.setLocation(15, 155); - cp.setBorder(BorderFactory.createLineBorder(Color.GRAY)); - - overviewPanel.add(pt); - overviewPanel.add(cp); + tabbedPane.addChangeListener(new TabChangeListener(overviewPanel)); - JPanel configPanel = new ConfigurationPanel("itoopie-opaque12"); + TabLogoPanel configPanel = new ConfigurationTab("itoopie-opaque12"); tabbedPane.addTab("Configuration", null, configPanel, null); + tabbedPane.addChangeListener(new TabChangeListener(configPanel)); JPanel logPanel = new LogoPanel("itoopie-opaque12"); diff --git a/src/net/i2p/itoopie/gui/OverviewTab.java b/src/net/i2p/itoopie/gui/OverviewTab.java new file mode 100644 index 000000000..131461616 --- /dev/null +++ b/src/net/i2p/itoopie/gui/OverviewTab.java @@ -0,0 +1,44 @@ +package net.i2p.itoopie.gui; + +import java.awt.Color; + +import info.monitorenter.gui.chart.Chart2D; +import info.monitorenter.gui.chart.views.ChartPanel; + +import javax.swing.BorderFactory; +import javax.swing.event.ChangeEvent; + +import net.i2p.itoopie.gui.component.BandwidthChart; +import net.i2p.itoopie.gui.component.ParticipatingTunnelsChart; +import net.i2p.itoopie.gui.component.TabLogoPanel; + +public class OverviewTab extends TabLogoPanel { + + public OverviewTab(String imageName) { + super(imageName); + super.setLayout(null); + + Chart2D bwChart = BandwidthChart.getChart(); + Chart2D partTunnelChart = ParticipatingTunnelsChart.getChart(); + ChartPanel pt = new ChartPanel(partTunnelChart); + pt.setSize(300, 135); + pt.setLocation(15, 10); + pt.setBorder(BorderFactory.createLineBorder(Color.GRAY)); + ChartPanel cp = new ChartPanel(bwChart); + cp.setSize(300,135); + cp.setLocation(15, 155); + cp.setBorder(BorderFactory.createLineBorder(Color.GRAY)); + + add(pt); + add(cp); + } + + + + @Override + public void onTabFocus(ChangeEvent e) { + System.out.println("OverviewTab onTabFocus()"); + + } + +} diff --git a/src/net/i2p/itoopie/gui/component/TabLogoPanel.java b/src/net/i2p/itoopie/gui/component/TabLogoPanel.java new file mode 100644 index 000000000..b33e6285a --- /dev/null +++ b/src/net/i2p/itoopie/gui/component/TabLogoPanel.java @@ -0,0 +1,14 @@ +package net.i2p.itoopie.gui.component; + +import javax.swing.event.ChangeEvent; + +public abstract class TabLogoPanel extends LogoPanel { + + private static final long serialVersionUID = 4526458659908868337L; + + public TabLogoPanel(String imageName) { + super(imageName); + } + + public abstract void onTabFocus(ChangeEvent e); +} diff --git a/src/net/i2p/itoopie/gui/component/util/TabChangeListener.java b/src/net/i2p/itoopie/gui/component/util/TabChangeListener.java new file mode 100644 index 000000000..07e7afca6 --- /dev/null +++ b/src/net/i2p/itoopie/gui/component/util/TabChangeListener.java @@ -0,0 +1,34 @@ +package net.i2p.itoopie.gui.component.util; + +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +import net.i2p.itoopie.gui.component.TabLogoPanel; + + +/** + * Allows for tabs to be registered and notified when selected. + * @author hottuna + * + */ +public class TabChangeListener implements ChangeListener { + + private TabLogoPanel adaptee; + + /** + * Register a TabLogoPanel to be notified when parent JTabbedPane fires sateChanged + * @param adaptee + */ + public TabChangeListener(TabLogoPanel adaptee) { + this.adaptee = adaptee; + } + + /** + * Fire onTabFocus for visible tabs when stateChanged. + */ + public void stateChanged(ChangeEvent e) { + if (adaptee.isVisible()){ + adaptee.onTabFocus(e); + } + } +} diff --git a/src/net/i2p/itoopie/i2pcontrol/methods/GetNetworkSetting.java b/src/net/i2p/itoopie/i2pcontrol/methods/GetNetworkSetting.java index bb5d57669..23b3b4234 100644 --- a/src/net/i2p/itoopie/i2pcontrol/methods/GetNetworkSetting.java +++ b/src/net/i2p/itoopie/i2pcontrol/methods/GetNetworkSetting.java @@ -1,6 +1,7 @@ package net.i2p.itoopie.i2pcontrol.methods; import java.util.Arrays; +import java.util.EnumMap; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -23,7 +24,7 @@ import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException; public class GetNetworkSetting { private final static Log _log = LogFactory.getLog(GetNetworkSetting.class); - public static HashMap execute(NETWORK_SETTING ... options) + public static EnumMap execute(NETWORK_SETTING ... options) throws InvalidPasswordException, JSONRPC2SessionException{ JSONRPC2Request req = new JSONRPC2Request("NetworkSetting", JSONRPC2Interface.incrNonce()); @@ -31,8 +32,10 @@ public class GetNetworkSetting { Map outParams = new HashMap(); List list = Arrays.asList(options); - for (NETWORK_SETTING i : list){ - outParams.put(i.toString(), null); + for (NETWORK_SETTING e : list){ + if(e.isReadable()){ + outParams.put(e.toString(), null); + } } req.setParams(outParams); @@ -43,13 +46,19 @@ public class GetNetworkSetting { HashMap map = (HashMap) resp.getResult(); if (map != null){ Set set = map.entrySet(); - HashMap output = new HashMap(); + EnumMap output = new EnumMap(NETWORK_SETTING.class); + // Present the result as an map. for (Entry e: set){ - output.put(NetworkSetting.enumMap.get(e.getKey()), e.getValue()); + String key = (String) e.getKey(); + NETWORK_SETTING NS = NetworkSetting.getEnum(key); + // If the enum exists. They should exists, but safety first. + if (NS != null){ + output.put(NS, e.getValue()); + } } - return map; + return output; } else { - return new HashMap(); + return new EnumMap(NETWORK_SETTING.class); } } catch (UnrecoverableFailedRequestException e) { _log.error("getNetworkInfo failed.", e); diff --git a/src/net/i2p/itoopie/i2pcontrol/methods/NetworkSetting.java b/src/net/i2p/itoopie/i2pcontrol/methods/NetworkSetting.java index 775d302a4..813146a9e 100644 --- a/src/net/i2p/itoopie/i2pcontrol/methods/NetworkSetting.java +++ b/src/net/i2p/itoopie/i2pcontrol/methods/NetworkSetting.java @@ -20,27 +20,29 @@ import java.util.HashMap; public class NetworkSetting{ - public final static HashMap enumMap; + private final static HashMap enumMap; /** * Describes the most common network related settings and their API key. * @author hottuna */ - public enum NETWORK_SETTING implements RemoteSetable{ - DETECTED_IP { public boolean isSetable(){ return false;} public String toString() { return "i2p.router.net.ssu.detectedip"; }}, - TCP_PORT { public boolean isSetable(){ return true;} public String toString() { return "i2p.router.net.ntcp.port"; }}, - TCP_HOSTNAME { public boolean isSetable(){ return true;} public String toString() { return "i2p.router.net.ntcp.hostname"; }}, - TCP_AUTOIP { public boolean isSetable(){ return true;} public String toString() { return "i2p.router.net.ntcp.autoip"; }}, - UDP_PORT { public boolean isSetable(){ return true;} public String toString() { return "i2p.router.net.ssu.port"; }}, - UDP_HOSTNAME { public boolean isSetable(){ return true;} public String toString() { return "i2p.router.net.ssu.hostname"; }}, - UDP_AUTO_IP { public boolean isSetable(){ return true;} public String toString() { return "i2p.router.net.ssu.autoip"; }}, - UPNP { public boolean isSetable(){ return true;} public String toString() { return "i2p.router.net.upnp"; }}, - BW_SHARE { public boolean isSetable(){ return true;} public String toString() { return "i2p.router.net.bw.share"; }}, - BW_IN { public boolean isSetable(){ return true;} public String toString() { return "i2p.router.net.bw.in"; }}, - BW_OUT { public boolean isSetable(){ return true;} public String toString() { return "i2p.router.net.bw.out"; }}, - LAPTOP_MODE { public boolean isSetable(){ return true;} public String toString() { return "i2p.router.net.laptopmode"; }} - }; + public enum NETWORK_SETTING implements Remote{ + DETECTED_IP { public boolean isReadable(){ return true;} public boolean isWritable(){ return false;} public String toString() { return "i2p.router.net.ssu.detectedip"; }}, + TCP_PORT { public boolean isReadable(){ return true;} public boolean isWritable(){ return true;} public String toString() { return "i2p.router.net.ntcp.port"; }}, + TCP_HOSTNAME { public boolean isReadable(){ return true;} public boolean isWritable(){ return true;} public String toString() { return "i2p.router.net.ntcp.hostname"; }}, + TCP_AUTOIP { public boolean isReadable(){ return true;} public boolean isWritable(){ return true;} public String toString() { return "i2p.router.net.ntcp.autoip"; }}, + UDP_PORT { public boolean isReadable(){ return true;} public boolean isWritable(){ return true;} public String toString() { return "i2p.router.net.ssu.port"; }}, + UDP_HOSTNAME { public boolean isReadable(){ return true;} public boolean isWritable(){ return true;} public String toString() { return "i2p.router.net.ssu.hostname"; }}, + UDP_AUTO_IP { public boolean isReadable(){ return true;} public boolean isWritable(){ return true;} public String toString() { return "i2p.router.net.ssu.autoip"; }}, + UPNP { public boolean isReadable(){ return true;} public boolean isWritable(){ return true;} public String toString() { return "i2p.router.net.upnp"; }}, + BW_SHARE { public boolean isReadable(){ return true;} public boolean isWritable(){ return true;} public String toString() { return "i2p.router.net.bw.share"; }}, + BW_IN { public boolean isReadable(){ return true;} public boolean isWritable(){ return true;} public String toString() { return "i2p.router.net.bw.in"; }}, + BW_OUT { public boolean isReadable(){ return true;} public boolean isWritable(){ return true;} public String toString() { return "i2p.router.net.bw.out"; }}, + LAPTOP_MODE { public boolean isReadable(){ return true;} public boolean isWritable(){ return true;} public String toString() { return "i2p.router.net.laptopmode"; }}, + SETTINGS_SAVED {public boolean isReadable(){ return false;} public boolean isWritable(){ return false;} public String toString() { return "SettingsSaved";}}, + RESTART_NEEDED {public boolean isReadable(){ return false;} public boolean isWritable(){ return false;} public String toString() { return "RestartNeeded";}} + }; static { enumMap = new HashMap(); @@ -48,4 +50,8 @@ public class NetworkSetting{ enumMap.put(n.toString(), n); } } + + public static NETWORK_SETTING getEnum(String key){ + return enumMap.get(key); + } } diff --git a/src/net/i2p/itoopie/i2pcontrol/methods/Remote.java b/src/net/i2p/itoopie/i2pcontrol/methods/Remote.java new file mode 100644 index 000000000..2ab4fcc7d --- /dev/null +++ b/src/net/i2p/itoopie/i2pcontrol/methods/Remote.java @@ -0,0 +1,19 @@ +package net.i2p.itoopie.i2pcontrol.methods; + +/** + * Describes how an option can be used against a server implementing the I2PControl API. + * @author hottuna + * + */ +public interface Remote{ + + /** + * @return - Does an I2PControl server allow this setting to saved. + */ + public boolean isWritable(); + + /** + * @return - Does an I2PControl server allow this setting to be read. + */ + public boolean isReadable(); +} \ No newline at end of file diff --git a/src/net/i2p/itoopie/i2pcontrol/methods/RemoteSetable.java b/src/net/i2p/itoopie/i2pcontrol/methods/RemoteSetable.java deleted file mode 100644 index 529302d02..000000000 --- a/src/net/i2p/itoopie/i2pcontrol/methods/RemoteSetable.java +++ /dev/null @@ -1,5 +0,0 @@ -package net.i2p.itoopie.i2pcontrol.methods; - -public interface RemoteSetable{ - public boolean isSetable(); -} \ No newline at end of file diff --git a/src/net/i2p/itoopie/i2pcontrol/methods/SetNetworkSetting.java b/src/net/i2p/itoopie/i2pcontrol/methods/SetNetworkSetting.java index 27e79dd1c..e57f55166 100644 --- a/src/net/i2p/itoopie/i2pcontrol/methods/SetNetworkSetting.java +++ b/src/net/i2p/itoopie/i2pcontrol/methods/SetNetworkSetting.java @@ -1,5 +1,6 @@ package net.i2p.itoopie.i2pcontrol.methods; +import java.util.EnumMap; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -22,7 +23,7 @@ public class SetNetworkSetting { private final static Log _log = LogFactory.getLog(SetNetworkSetting.class); - public static HashMap execute(Map settings) + public static EnumMap execute(Map settings) throws InvalidPasswordException, JSONRPC2SessionException, InvalidParametersException{ JSONRPC2Request req = new JSONRPC2Request("NetworkSetting", JSONRPC2Interface.incrNonce()); @@ -31,7 +32,7 @@ public class SetNetworkSetting { Set> set = settings.entrySet(); for (Entry e : set){ - if(e.getKey().isSetable()){ + if(e.getKey().isWritable()){ outParams.put(e.getKey().toString(), e.getValue()); } } @@ -41,7 +42,22 @@ public class SetNetworkSetting { try { resp = JSONRPC2Interface.sendReq(req); HashMap map = (HashMap) resp.getResult(); - return map; + if (map != null){ + Set inputSet = map.entrySet(); + EnumMap output = new EnumMap(NETWORK_SETTING.class); + // Present the result as an map. + for (Entry e: inputSet){ + String key = (String) e.getKey(); + NETWORK_SETTING NS = NetworkSetting.getEnum(key); + // If the enum exists. They should exists, but safety first. + if (NS != null){ + output.put(NS, e.getValue()); + } + } + return output; + } else { + return new EnumMap(NETWORK_SETTING.class); + } } catch (UnrecoverableFailedRequestException e) { _log.error("setNetworkInfo failed.", e); }