/* * Copyright (C) 2015 Shashank Tulsyan <shashaank at neembuu.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package neembuu.uploader; import java.awt.Desktop; import java.awt.GridLayout; import java.net.URI; import java.util.Arrays; import java.util.Comparator; import java.util.Iterator; import java.util.Map; import java.util.logging.Level; import javax.swing.JCheckBox; import neembuu.reactivethread.CompletionCallback; import neembuu.reactivethread.ReactiveThread; import neembuu.uploader.api.accounts.EnableHostCallback; import neembuu.uploader.api.accounts.HostsAccountUI; import neembuu.uploader.external.SmallModuleEntry; import neembuu.uploader.theme.ThemeCheck; import neembuu.uploader.translation.ToHtmlMultiLine; import neembuu.uploader.translation.Translation; import neembuu.uploader.utils.NULogger; import neembuu.uploader.utils.PluginUtils; /** * * @author dsivaji */ public class HostsPanel extends javax.swing.JDialog { //private singleton private static final HostsPanel INSTANCE = new HostsPanel(NeembuuUploader.getInstance(), true); /** * * @return the singleton instance of HostsPanel. Call setVisible(true) after * this. */ public static HostsPanel getInstance() { return INSTANCE; } /** * Override setVisible so that it'll pack before displaying. :) * * @param b */ @Override public void setVisible(boolean b) { ThemeCheck.apply(this); pack(); setFocusToTabbedPane(); super.setVisible(b); } /** * Creates new form HostsPanel * @param parent * @param modal */ public HostsPanel(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); setLocationRelativeTo(NeembuuUploader.getInstance()); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { infoLabel = new javax.swing.JLabel(); limitationsLabel = new javax.swing.JLabel(); hostsTabbedPane = new javax.swing.JTabbedPane(); activatePluginsScroll = new javax.swing.JScrollPane(); activatePluginsPanel = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); freeandacctPanel = new javax.swing.JPanel(); jScrollPane2 = new javax.swing.JScrollPane(); acctonlyPanel = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); closeHostsPanel = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); infoLabel.setFont(infoLabel.getFont().deriveFont(infoLabel.getFont().getStyle() | java.awt.Font.BOLD)); infoLabel.setText("Select whichever sites you would like to upload to.. You need not select again unless you want to change.."); limitationsLabel.setFont(limitationsLabel.getFont().deriveFont((limitationsLabel.getFont().getStyle() | java.awt.Font.ITALIC), limitationsLabel.getFont().getSize()-1)); limitationsLabel.setText("<html>For filesize limitations and other info on each site, click here: <a href='http://neembuuuploader.sourceforge.net/support.html#limitations'>http://neembuuuploader.sf.net/support.html#limitations</a></html>"); limitationsLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); limitationsLabel.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { limitationsLabelMouseClicked(evt); } }); hostsTabbedPane.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { hostsTabbedPaneKeyPressed(evt); } }); javax.swing.GroupLayout activatePluginsPanelLayout = new javax.swing.GroupLayout(activatePluginsPanel); activatePluginsPanel.setLayout(activatePluginsPanelLayout); activatePluginsPanelLayout.setHorizontalGroup( activatePluginsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 1582, Short.MAX_VALUE) ); activatePluginsPanelLayout.setVerticalGroup( activatePluginsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 348, Short.MAX_VALUE) ); activatePluginsScroll.setViewportView(activatePluginsPanel); hostsTabbedPane.addTab("<html><br /><b>Activate Plugins</b><br /><br /></html>", activatePluginsScroll); javax.swing.GroupLayout freeandacctPanelLayout = new javax.swing.GroupLayout(freeandacctPanel); freeandacctPanel.setLayout(freeandacctPanelLayout); freeandacctPanelLayout.setHorizontalGroup( freeandacctPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 1582, Short.MAX_VALUE) ); freeandacctPanelLayout.setVerticalGroup( freeandacctPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 348, Short.MAX_VALUE) ); jScrollPane1.setViewportView(freeandacctPanel); hostsTabbedPane.addTab("<html><br /><b>Free and Account</b><br /><br /></html>", jScrollPane1); javax.swing.GroupLayout acctonlyPanelLayout = new javax.swing.GroupLayout(acctonlyPanel); acctonlyPanel.setLayout(acctonlyPanelLayout); acctonlyPanelLayout.setHorizontalGroup( acctonlyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 1582, Short.MAX_VALUE) ); acctonlyPanelLayout.setVerticalGroup( acctonlyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 348, Short.MAX_VALUE) ); jScrollPane2.setViewportView(acctonlyPanel); hostsTabbedPane.addTab("<html><br /><b>Account only</b><br /><br /></html>", jScrollPane2); jLabel1.setText("Version 3.0 onwards. You need to activate plugins by selecting them in the activate plugins tab."); closeHostsPanel.setText("Close"); closeHostsPanel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { closeHostsPanelActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(hostsTabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 733, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(infoLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(limitationsLabel) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup(layout.createSequentialGroup() .addGap(342, 342, 342) .addComponent(closeHostsPanel) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(infoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(limitationsLabel) .addGap(8, 8, 8) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(hostsTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(closeHostsPanel) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents private void limitationsLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_limitationsLabelMouseClicked if (!Desktop.isDesktopSupported()) { return; } try { NULogger.getLogger().log(Level.INFO, "{0}Limitations label clicked.. Opening webpage..", HostsPanel.class.getName()); Desktop.getDesktop().browse(new URI("http://neembuuuploader.sourceforge.net/support.html#limitations")); } catch (Exception ex) { } }//GEN-LAST:event_limitationsLabelMouseClicked private void hostsTabbedPaneKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_hostsTabbedPaneKeyPressed if (!evt.isControlDown()) { return; } //if((evt.getKeyCode() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())!=0) return; if(true)throw new IllegalStateException("why is this here?. do we really need to handle key press events?"); /*if (evt.getKeyCode() == KeyEvent.VK_A) { Map<JCheckBox, SmallModuleEntry> map = NeembuuUploader.getInstance().getActivatePluginsMap(); for (JCheckBox cb : map.keySet()) { if (cb.isEnabled()) { cb.setSelected(true); } } } else if (evt.getKeyCode() == KeyEvent.VK_T) { Map<JCheckBox, SmallModuleEntry> map = NeembuuUploader.getInstance().getActivatePluginsMap(); for (JCheckBox cb : map.keySet()) { if (!cb.isEnabled()) { continue; } if (cb.isSelected()) { cb.setSelected(false); } else { cb.setSelected(true); } } } NeembuuUploader.getInstance().updateSelectedHostsLabel();*/ }//GEN-LAST:event_hostsTabbedPaneKeyPressed private void closeHostsPanelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeHostsPanelActionPerformed NULogger.getLogger().info("Closing Hosts Panel ..."); dispose(); }//GEN-LAST:event_closeHostsPanelActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel acctonlyPanel; private javax.swing.JPanel activatePluginsPanel; private javax.swing.JScrollPane activatePluginsScroll; private javax.swing.JButton closeHostsPanel; private javax.swing.JPanel freeandacctPanel; private javax.swing.JTabbedPane hostsTabbedPane; private javax.swing.JLabel infoLabel; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JLabel limitationsLabel; // End of variables declaration//GEN-END:variables /** * Update the language everytime HostsPanel is displayed. */ static void updateLanguage() { INSTANCE.setTitle(Translation.T().title2()); INSTANCE.infoLabel.setText( ToHtmlMultiLine.splitToMultipleLines( Translation.T().infoLabel1(),100)); INSTANCE.limitationsLabel.setText("<html>" + Translation.T().limitationsLabel() + "<br/><a href='http://neembuuuploader.sourceforge.net/support.html#limitations'>http://neembuuuploader.sf.net/support.html#limitations</a></html>"); INSTANCE.hostsTabbedPane.setTitleAt(0, "<html><br /><b>" + Translation.T().activatePlugins() + "</b><br /><br /></html>"); INSTANCE.hostsTabbedPane.setTitleAt(1, "<html><br /><b>" + Translation.T().freeandacctPanel() + "</b><br /><br /></html>"); INSTANCE.hostsTabbedPane.setTitleAt(2, "<html><br /><b>" + Translation.T().acctonlyPanel() + "</b><br /><br /></html>"); INSTANCE.jLabel1.setText( ToHtmlMultiLine.splitToMultipleLines( Translation.T().version3activateManuallyMessage(),100)); } void arrangeCheckBoxesForAllPlugins(Map<JCheckBox, SmallModuleEntry> allmap) { //Remove the components activatePluginsPanel.removeAll(); //Set grid layout. activatePluginsPanel.setLayout(new GridLayout(0, 4)); Map.Entry<JCheckBox, SmallModuleEntry>[] all_entries = allmap.entrySet().toArray(new Map.Entry[allmap.entrySet().size()]); Arrays.sort(all_entries, new Comparator<Map.Entry<JCheckBox, SmallModuleEntry>>(){ @Override public int compare(Map.Entry<JCheckBox, SmallModuleEntry> o1, Map.Entry<JCheckBox, SmallModuleEntry> o2) { try{ return o1.getValue().getName().compareToIgnoreCase(o2.getValue().getName()); }catch(Exception a){ a.printStackTrace(); return 0; } } }); for (Map.Entry<JCheckBox, SmallModuleEntry> entry : all_entries) { //For a class, get its interfaces and interate through them. entry.getKey().setSelected(entry.getValue().isActivated()); //It's disabled by default activatePluginsPanel.add(entry.getKey()); } } /** * package private method.. to be called by NeembuuUploader class. Arranges * CheckBoxes in alphabetical order in appropriate panel based on the map */ void arrangeCheckBoxes(final Map<JCheckBox, SmallModuleEntry> map) { //Remove the components freeandacctPanel.removeAll(); acctonlyPanel.removeAll(); //Set grid layout. freeandacctPanel.setLayout(new GridLayout(0, 4)); acctonlyPanel.setLayout(new GridLayout(0, 3)); ReactiveThread rt = ReactiveThread.create(new Runnable() { @Override public void run() { arrageMap(map); } },CompletionCallback.DUMMY);rt.setName("Arrange Map"); rt.start(); } void arrageMap(final Map<JCheckBox, SmallModuleEntry> map){ long startTime = System.currentTimeMillis(); for (Map.Entry<JCheckBox, SmallModuleEntry> entry : map.entrySet()) { //For a class, get its interfaces and interate through them. if(PluginUtils.isAccountNecessary(entry.getValue(),startTime)){ //If an interface matches UploaderAccountNecessary, then add it to the //second panel and continue the next loop.. entry.getKey().setEnabled(false); //It's disabled by default acctonlyPanel.add(entry.getKey()); } else{ //If the if condition in the above loop never satisfied, //that is if the class does not inherit UploaderAccountNecessary, //then (only) the control will come here and the following code //will add the entry to the first panel. freeandacctPanel.add(entry.getKey()); } } } /*package*/ void setFocusToTabbedPane() { hostsTabbedPane.requestFocusInWindow(); } public final HostsAccountUI hostsAccountUI(){ return haui;} private final HostsAccountUI haui = new HostsAccountUI() { @Override public EnableHostCallback hostUI(String name) { final JCheckBox findCheckBox = findCheckBox(name); return new EnableHostCallback() { @Override public void setEnabled(boolean f) { findCheckBox.setEnabled(f); }@Override public void setSelected(boolean f){ findCheckBox.setSelected(f); } }; } }; public JCheckBox findCheckBox(String name){ Map<JCheckBox, SmallModuleEntry> map = NeembuuUploader.getInstance().getActivatePluginsMap(); /* name = name .replace("CheckBox", "") .replace("two", "2"); */ Iterator<JCheckBox> it = map.keySet().iterator(); while (it.hasNext()) { JCheckBox checkBox = it.next(); String text = checkBox.getText().replace("Checkbox", ""); //NULogger.getLogger().log(Level.INFO, "Name {0} Checkbox {1}", new Object[]{name, text}); if(text.equalsIgnoreCase(name)){ return checkBox; } } NULogger.getLogger().log(Level.INFO, "Not found name {0}", name); /* try{ Class c = HostsPanel.class; Field[]fs=c.getDeclaredFields(); for (Field field : fs) { if(field.getName().equalsIgnoreCase(name.trim())){ return (JCheckBox)field.get(this); } } }catch(Exception a){ a.printStackTrace(); } */ return null; } }