Package net.sf.jpluck.apps.jpluckx.ui

Source Code of net.sf.jpluck.apps.jpluckx.ui.PreferencesDialog

package net.sf.jpluck.apps.jpluckx.ui;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FilenameFilter;
import java.net.URI;
import java.net.URL;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.sf.jpluck.ClientConfiguration;
import net.sf.jpluck.apps.jpluckx.JPluckX;
import net.sf.jpluck.conversion.Destination;
import net.sf.jpluck.http.HttpCache;
import net.sf.jpluck.http.HttpClient;
import net.sf.jpluck.palm.win32.PalmInstall;
import net.sf.jpluck.swing.ComponentGroup;
import net.sf.jpluck.swing.EntryTextField;
import net.sf.jpluck.swing.FileBrowseButton;
import net.sf.jpluck.swing.GUIUtil;
import net.sf.jpluck.swing.JXLFileFilter;
import net.sf.jpluck.swing.LAFSelectBox;
import net.sf.jpluck.swing.PlasticThemeCombo;
import net.sf.jpluck.swing.TextFileFilter;
import net.sf.jpluck.ui.Banner;
import net.sf.jpluck.ui.StandardDialog;
import net.sf.jpluck.ui.ValidationException;
import net.sf.jpluck.util.Period;
import net.sf.jpluck.util.Platform;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.FormLayout;


public class PreferencesDialog extends StandardDialog {
    private static int activeTab = 0;
    private static final String[] SHEETS = { "main", "http", "cache", "misc", "autoupdate", "application" };
    private static final String[] DATE_FORMATS = { "short", "medium", "long", "full" };
    private AutoUpdatePanel autoUpdatePanel = new AutoUpdatePanel();
    private AutoUpdatePeriodListener autoUpdatePeriodListener = new AutoUpdatePeriodListener();
    private Banner banner;
    private CompactCacheAction compactCacheAction;
    private ComponentGroup logGroup = new ComponentGroup();
    private ComponentGroup proxyGroup = new ComponentGroup();
    private DestinationListener destinationListener = new DestinationListener();
    private EntryTextField acceptCharsetText = new EntryTextField(25);
    private EntryTextField backupText = new EntryTextField(30);
    private EntryTextField cacheDirectoryText = new EntryTextField(30);
    private EntryTextField cookiesTxtText = new EntryTextField(30);
    private EntryTextField directoryText = new EntryTextField(30);
    private EntryTextField logFileText = new EntryTextField(30);
    private EntryTextField pluckthisJXLText = new EntryTextField(30);
    private EntryTextField proxyHostText = new EntryTextField(20);
    private EntryTextField proxyUserText = new EntryTextField(20);
    private FileBrowseButton backupBrowse = new FileBrowseButton(backupText, "Select Backup Directory", "Select",
                                                                 true, false, null);
    private FileBrowseButton cacheDirectoryBrowse = new FileBrowseButton(cacheDirectoryText,
                                                                         "Select Cache Directory", "Select", true,
                                                                         false, null);
    private FileBrowseButton cookiesTxtBrowse = new FileBrowseButton(cookiesTxtText, "Select cookies.txt file",
                                                                     "Select", false, false, new TextFileFilter());
    private FileBrowseButton directoryBrowse = new FileBrowseButton(directoryText, "Select Destination Directory",
                                                                    "Select", true, false, null);
    private FileBrowseButton logFileBrowse = new FileBrowseButton(logFileText, "Select Log File", "Select", false,
                                                                  false, null);
    private FileBrowseButton pluckthisJXLBrowse = new FileBrowseButton(pluckthisJXLText,
                                                                       "Select PluckThis template JXL file",
                                                                       "Select", false, false, new JXLFileFilter());
    private EntryTextField browserText = new EntryTextField(30);
    private FileBrowseButton browserBrowse = new FileBrowseButton(browserText, "Select web browser", "Select", false,
                                                                  false, null);
    private JCheckBox bookmarksCheck = new JCheckBox("Generate bookmarks for feed items");
    private JLabel showcaseCacheLabel = new JLabel();
    private ClearShowcaseCacheAction clearShowcaseCacheAction = new ClearShowcaseCacheAction();
    private HotSyncListener hotSyncListener = new HotSyncListener();
    private JButton compactCacheButton = new JButton(compactCacheAction = new CompactCacheAction());
    private JButton defaultAcceptCharsetButton = new JButton(new DefaultAcceptCharsetAction());
    private JCheckBox autoCloseCheck = new JCheckBox("Close conversion window automatically");
    private JCheckBox autoSelectDueCheck = new JCheckBox("Select due documents automatically");
    private JCheckBox autoUpdatePeriodCheck = new JCheckBox("AutoUpdate only from ");
    private JCheckBox categoryDirectoriesCheck = new JCheckBox("Create category subdirectories");
    private JCheckBox compactCacheCheck = new JCheckBox("Compact cache after each conversion");
    private JCheckBox confirmCutCheck = new JCheckBox("Confirm cut to clipboard");
    private JCheckBox displayFullFilePathCheck = new JCheckBox("Display full path of JXL file in title bar");
    private JCheckBox feedDateCheck = new JCheckBox("Use feed date for document");
    private JCheckBox logFileCheck = new JCheckBox("Create conversion log file");
    private JCheckBox memoryCardCheck = new JCheckBox("Transfer to memory card");
    private JCheckBox multiImageCheck = new JCheckBox("Generate multi-images");
    //private JCheckBox segmentLinksCheck = new JCheckBox("Insert links between page segments");
    private JCheckBox showSplashScreenCheck = new JCheckBox("Show splash screen at startup");
    private JCheckBox trayIconCheck = new JCheckBox("Minimize to system tray when closing main window");
    private JCheckBox useProxyCheck = new JCheckBox("Use proxy server");
    private JComboBox compressionCombo = new JComboBox(new String[] {
                                                           "No compression", "DOC compression", "ZLIB compression"
                                                       });
    private JComboBox dateFormatCombo;
    private JComboBox logLevelCombo = new JComboBox(new String[] { "SEVERE", "WARNING", "INFO", "FINER", "FINEST", "ALL" });
    private JComboBox userCombo;
    private JList autoUpdateList;
    private JPanel autoUpdatePeriodPanel;
    private JPasswordField proxyPasswordText = new JPasswordField(20);
    private JRadioButton directoryRadio = new JRadioButton("Directory");
    private JRadioButton hotsyncRadio = new JRadioButton("HotSync");
    private JSpinner autoUpdateFromSpinner = new JSpinner(new SpinnerDateModel());
    private JSpinner autoUpdateIntervalSpinner = new JSpinner(new SpinnerNumberModel(15, 5, 60, 5));
    private JSpinner autoUpdateToSpinner = new JSpinner(new SpinnerDateModel());
    private JSpinner cacheExpirySpinner = new JSpinner(new SpinnerNumberModel(1, 1, 30, 1));
    private JSpinner downloadAttemptsSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 10, 1));
    private JSpinner logFileSizeSpinner = new JSpinner(new SpinnerNumberModel(50, 10, 9999, 10));
    private JSpinner maxConnectionsSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 4, 1));
    private JSpinner proxyPortSpinner = new JSpinner(new SpinnerNumberModel(8080, 1, 65536, 1));
    private JSpinner timeoutSpinner = new JSpinner(new SpinnerNumberModel(20, 3, 90, 1));
    private JTabbedPane tabbedPane = new JTabbedPane();
    private LAFSelectBox lafBox = new LAFSelectBox();
    private PlasticThemeCombo plasticThemeCombo = new PlasticThemeCombo();
    private LogListener logListener = new LogListener();
    private UseProxyListener proxyListener = new UseProxyListener();
    private String[] users;

    private JCheckBox imageIOCheck;
  private LAFListener lafListener;
  public PreferencesDialog(JFrame owner) {
        super(owner);
        initComponents();
        initValues();
    }

    protected void cancel() {
        super.cancel();
        activeTab = tabbedPane.getSelectedIndex();
    }

    protected boolean ok() {
        try {
            String directory = checkIfFileExists(directoryText);
            String user = null;
            boolean memoryCard = false;
            if (directoryRadio.isSelected()) {
                if (directory.length() == 0) {
                    throw ValidationException.required(directoryText, "Please enter a destination directory.");
                }
            } else if (hotsyncRadio.isSelected()) {
                user = (String) userCombo.getSelectedItem();
                if (PalmInstall.getSlotCount(user) > 0) {
                    memoryCard = memoryCardCheck.isSelected();
                }
            } else {
                throw ValidationException.required(directoryRadio, "Please select a destination");
            }

            boolean useProxy = useProxyCheck.isSelected();
            if (useProxy) {
                if (proxyHostText.getText().length() == 0) {
                    throw ValidationException.required(proxyHostText, "Please enter a proxy host.");
                }
                if ((proxyUserText.getText().length() > 0) && (proxyPasswordText.getPassword().length == 0)) {
                    throw ValidationException.required(proxyPasswordText,
                                                       "Please enter a proxy authorization password.");
                }
            }

            if (logFileCheck.isSelected()) {
                if (logFileText.getText().length() == 0) {
                    throw ValidationException.required(logFileText, "Please enter a log file.");
                }
            }

            String backupDirectory = checkIfFileExists(backupText);
            String cacheDirectory = checkIfFileExists(cacheDirectoryText);
            String cookiesTxt = checkIfFileExists(cookiesTxtText);
            String pluckthisJXL = checkIfFileExists(pluckthisJXLText);

            ClientConfiguration conf = ClientConfiguration.getDefault();
            conf.setCompression(compressionCombo.getSelectedIndex());
            conf.setUseMultiImage(multiImageCheck.isSelected());
            //conf.setUseSegmentLinks(segmentLinksCheck.isSelected());
            conf.setDirectoryDestination(directory);
            if (user != null) {
                conf.setHotSyncDestination(user, memoryCard);
            }
            conf.setCreateCategoryDirectories(categoryDirectoriesCheck.isSelected());
            conf.setDateFormat(DATE_FORMATS[dateFormatCombo.getSelectedIndex()]);
            conf.setUseFeedDate(feedDateCheck.isSelected());
            conf.setGenerateBookmarks(bookmarksCheck.isSelected());

            conf.setMaxConnections(((Integer) maxConnectionsSpinner.getValue()).intValue());
            conf.setTimeout(((Integer) timeoutSpinner.getValue()).intValue());
            conf.setDownloadAttempts(((Integer) downloadAttemptsSpinner.getValue()).intValue());
            String s = acceptCharsetText.getText();
            if (s.length() == 0) {
                s = HttpClient.DEFAULT_ACCEPT_CHARSET;
            }
            conf.setAcceptCharset(s);
            conf.setCacheDirectory(cacheDirectory);
            conf.setCacheExpiration(((Integer) cacheExpirySpinner.getValue()).intValue());
            conf.setAutoCompactCache(compactCacheCheck.isSelected());

            conf.setUseProxy(useProxyCheck.isSelected());
            conf.setProxyHost(proxyHostText.getText());
            conf.setProxyPort(((Integer) proxyPortSpinner.getValue()).intValue());
            conf.setProxyUser(proxyUserText.getText());
            conf.setProxyPassword(new String(proxyPasswordText.getPassword()));

            conf.setLogEnabled(logFileCheck.isSelected());
            conf.setLogFile(logFileText.getText());
            conf.setMaxLogSize(((Integer) logFileSizeSpinner.getValue()).intValue());
            conf.setLogLevel((String) logLevelCombo.getSelectedItem());
            conf.setPluckThisJXL(pluckthisJXL);
            conf.setCookiesTxt(cookiesTxt);

            conf.setLookAndFeel(lafBox.getSelectedLAF().getClassName());
            conf.setPlasticTheme(plasticThemeCombo.getSelectedTheme().getName());
            conf.setAutoCloseConversion(autoCloseCheck.isSelected());
            conf.setDisplayFullFilePath(displayFullFilePathCheck.isSelected());
            conf.setAutoSelectDue(autoSelectDueCheck.isSelected());
            conf.setShowSplashScreen(showSplashScreenCheck.isSelected());
            conf.setConfirmCut(confirmCutCheck.isSelected());
            conf.setMinimizeToTrayIcon(trayIconCheck.isSelected());
            conf.setBrowser(browserText.getText());
            conf.setUseImageIO(imageIOCheck.isSelected());

            conf.setAutoUpdateJXLs(autoUpdatePanel.getFiles());
            conf.setAutoUpdatePeriodEnabled(autoUpdatePeriodCheck.isSelected());
            Period.Time from = Period.Time.fromDate((Date) autoUpdateFromSpinner.getValue());
            Period.Time to = Period.Time.fromDate((Date) autoUpdateToSpinner.getValue());
            conf.setAutoUpdatePeriod(new Period(from, to));
            conf.setAutoUpdateInterval(((Integer) autoUpdateIntervalSpinner.getValue()).intValue());

            activeTab = tabbedPane.getSelectedIndex();
            return true;
        } catch (ValidationException e) {
            e.showMessageDialog();
            return false;
        }
    }

    private String checkIfFileExists(JTextField textField)
                              throws ValidationException {
        String s = textField.getText().trim(); // trim() = workaround(?) for bug reported by Lo Yuk Fai
        if (s.length() > 0) {
            if (!new File(s).exists()) {
                throw ValidationException.invalid(textField, "File does not exist:\n" + s);
            }
        }
        return s;
    }

    private JPanel createBrowsePanel(JTextField textField, FileBrowseButton button) {
        PanelBuilder builder = new PanelBuilder(new FormLayout("fill:pref:grow, 4dlu, 20dlu", "pref"));
        builder.add(textField, "1,1");
        builder.add(button, "3,1");
        return builder.getPanel();
    }

    private void initComponents() {
        if (Platform.isWindows()) {
            users = PalmInstall.getUsers();
        } else {
            users = new String[0];
        }
        if (users.length > 0) {
            userCombo = new JComboBox(users);
            hotsyncRadio.addItemListener(destinationListener);
            userCombo.addItemListener(hotSyncListener);
        } else {
            hotsyncRadio.setEnabled(false);
            userCombo = new JComboBox(new String[] { "<Not available>" });
            userCombo.setEnabled(false);
            memoryCardCheck.setEnabled(false);
        }

        ButtonGroup g1 = new ButtonGroup();
        g1.add(hotsyncRadio);
        g1.add(directoryRadio);

        Locale locale = Locale.getDefault();
        DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
        DateFormat mediumFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
        DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG, locale);
        DateFormat fullFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);
        Date date = new Date();
        dateFormatCombo = new JComboBox(new String[] {
                                            "short: " + shortFormat.format(date),
                                            "medium: " + mediumFormat.format(date), "long: " +
                                            longFormat.format(date), "full: " + fullFormat.format(date)
                                        });

        JPanel acceptCharsetPanel = new JPanel(new FormLayout("pref, 4dlu, pref", "pref"));
        acceptCharsetPanel.add(acceptCharsetText, "1,1");
        defaultAcceptCharsetButton.putClientProperty("jgoodies.narrow", Boolean.TRUE);
        GUIUtil.makeEqualHeight(acceptCharsetText, defaultAcceptCharsetButton);
        acceptCharsetPanel.add(defaultAcceptCharsetButton, "3,1");

        useProxyCheck.addChangeListener(proxyListener);
        proxyPortSpinner.setEditor(new JSpinner.NumberEditor(proxyPortSpinner, "#####"));

        logFileCheck.addChangeListener(logListener);

        autoUpdatePeriodCheck.addChangeListener(autoUpdatePeriodListener);
        autoUpdateFromSpinner.setEditor(new JSpinner.DateEditor(autoUpdateFromSpinner,
                                                                ClientConfiguration.getDefault().getTimeFormat()));
        autoUpdateToSpinner.setEditor(new JSpinner.DateEditor(autoUpdateToSpinner,
                                                              ClientConfiguration.getDefault().getTimeFormat()));
        autoUpdatePeriodPanel = new JPanel(new FormLayout("pref, pref, pref", "pref"));
        autoUpdatePeriodPanel.add(autoUpdateFromSpinner, "1,1");
        autoUpdatePeriodPanel.add(new JLabel(" to "), "2,1");
        autoUpdatePeriodPanel.add(autoUpdateToSpinner, "3,1");
        JPanel autoUpdateIntervalPanel = new JPanel(new FormLayout("pref, pref, pref", "pref"));
        autoUpdateIntervalPanel.add(new JLabel("Check for due documents every "), "1,1");
        autoUpdateIntervalPanel.add(autoUpdateIntervalSpinner, "2,1");
        autoUpdateIntervalPanel.add(new JLabel(" minutes"), "3,1");

        FormLayout formLayout = new FormLayout("pref, 6dlu, left:pref:grow",
                                               "pref, 4dlu, pref, 4dlu, pref, 7dlu, " +
                                               "pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 7dlu, " +
                                               "pref, 4dlu, pref, 4dlu, pref, 4dlu, pref");
        PanelBuilder documentBuilder = new PanelBuilder(formLayout);
        documentBuilder.setBorder(Borders.TABBED_DIALOG_BORDER);
        documentBuilder.addSeparator("Plucker Settings", "1,1,3,1");
        documentBuilder.addLabel("Compression", "1,3").setLabelFor(compressionCombo);
        documentBuilder.add(compressionCombo, "3,3");
        //documentBuilder.addLabel("Pages", "1,5").setLabelFor(segmentLinksCheck);
        //documentBuilder.add(segmentLinksCheck, "3,5");
        documentBuilder.addLabel("Images", "1,5").setLabelFor(multiImageCheck);
        documentBuilder.add(multiImageCheck, "3,5");
        documentBuilder.addSeparator("Destination", "1,7,3,1");
        documentBuilder.add(hotsyncRadio, "1,9");
        documentBuilder.add(userCombo, "3,9");
        documentBuilder.add(memoryCardCheck, "3,11");
        documentBuilder.add(directoryRadio, "1,13");
        documentBuilder.add(createBrowsePanel(directoryText, directoryBrowse), "3,13,1,1,fill,center");
        documentBuilder.add(categoryDirectoriesCheck, "3,15");
        documentBuilder.addSeparator("Feed", "1,17,3,1");
        documentBuilder.addLabel("Date Format", "1,19").setLabelFor(dateFormatCombo);
        documentBuilder.add(dateFormatCombo, "3,19");
        documentBuilder.addLabel("Feed Date", "1,21").setLabelFor(feedDateCheck);
        documentBuilder.add(feedDateCheck, "3,21");
        documentBuilder.addLabel("Bookmarks", "1,23");
        documentBuilder.add(bookmarksCheck, "3,23");

        formLayout = new FormLayout("pref, 6dlu, fill:pref, left:pref:grow",
                                    "pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 7dlu, " +
                                    "pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref");

        PanelBuilder networkBuilder = new PanelBuilder(formLayout);
        networkBuilder.setBorder(Borders.TABBED_DIALOG_BORDER);
        networkBuilder.addSeparator("HTTP Settings", "1,1,4,1");
        networkBuilder.addLabel("Connections", "1,3").setLabelFor(maxConnectionsSpinner);
        networkBuilder.add(maxConnectionsSpinner, "3,3");
        networkBuilder.addLabel(" (It's recommended you leave this at 2.)", "4,3");
        networkBuilder.addLabel("Timeout", "1,5");
        networkBuilder.add(timeoutSpinner, "3,5");
        networkBuilder.addLabel(" seconds (3-90)", "4,5");
        networkBuilder.addLabel("Attempts", "1,7");
        networkBuilder.add(downloadAttemptsSpinner, "3,7");
        networkBuilder.addLabel(" (1-10)", "4,7");
        networkBuilder.addLabel("Accept-Charset", "1,9");
        networkBuilder.add(acceptCharsetPanel, "3,9,2,1");
        networkBuilder.addSeparator("Proxy Server", "1,11,4,1");
        networkBuilder.add(useProxyCheck, "1,13,4,1");
        proxyGroup.add(networkBuilder.addLabel("Host", "1,15"));
        proxyGroup.add(networkBuilder.add(proxyHostText, "3,15,2,1,left,center"));
        proxyGroup.add(networkBuilder.addLabel("Port", "1,17"));
        proxyGroup.add(networkBuilder.add(proxyPortSpinner, "3,17,2,1,left,center"));
        proxyGroup.add(networkBuilder.addLabel("User", "1,19"));
        proxyGroup.add(networkBuilder.add(proxyUserText, "3,19,2,1,left,center"));
        proxyGroup.add(networkBuilder.addLabel("Password", "1,21"));
        proxyGroup.add(networkBuilder.add(proxyPasswordText, "3,21,2,1,left,center"));

        formLayout = new FormLayout("pref, 6dlu, pref, pref:grow",
                                    "pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 7dlu, " +
                                    "pref, 4dlu, pref, 4dlu, pref");

        PanelBuilder cacheBuilder = new PanelBuilder(formLayout);
        cacheBuilder.setBorder(Borders.TABBED_DIALOG_BORDER);
        cacheBuilder.addSeparator("Download Cache", "1,1,4,1");
        cacheBuilder.addLabel("Note: the cache will be disabled if you leave the Directory blank.", "1,3,4,1");
        cacheBuilder.addLabel("Directory", "1,5").setLabelFor(cacheDirectoryText);
        cacheBuilder.add(createBrowsePanel(cacheDirectoryText, cacheDirectoryBrowse), "3,5,2,1");
        cacheBuilder.addLabel("Cache for", "1,7").setLabelFor(cacheExpirySpinner);
        cacheBuilder.add(cacheExpirySpinner, "3,7");
        cacheBuilder.addLabel(" days", "4,7");
        cacheBuilder.addLabel("Compact", "1,9").setLabelFor(compactCacheCheck);
        cacheBuilder.add(compactCacheCheck, "3,9,2,1");
        cacheBuilder.add(compactCacheButton, "3,11,2,1,left,center");
        cacheBuilder.addSeparator("Showcase", "1,13,4,1");
        cacheBuilder.add(showcaseCacheLabel, "1,15,4,1");
        cacheBuilder.add(new JButton(clearShowcaseCacheAction), "3,17,2,1,left,center");

        formLayout = new FormLayout("pref, 6dlu, left:pref, pref:grow",
                                    "pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 7dlu, " +
                                    "pref, 4dlu, pref, 7dlu, " + "pref, 4dlu, pref, 4dlu, pref, 4dlu, pref");

        PanelBuilder miscBuilder = new PanelBuilder(formLayout);
        miscBuilder.setBorder(Borders.TABBED_DIALOG_BORDER);
        miscBuilder.addSeparator("Conversion Log", "1,1,4,1").getComponent(0);
        miscBuilder.addLabel("Log Level", "1,3");
        miscBuilder.add(logLevelCombo, "3,3,2,1,left,center");
        miscBuilder.addLabel("Log", "1,5");
        miscBuilder.add(logFileCheck, "3,5,2,1");
        logGroup.add(miscBuilder.addLabel("Log File", "1,7"));
        miscBuilder.add(createBrowsePanel(logFileText, logFileBrowse), "3,7,2,1");
        logGroup.add(logFileText);
        logGroup.add(logFileBrowse);
        logGroup.add(miscBuilder.addLabel("Max. Size", "1,9"));
        logGroup.add(miscBuilder.add(logFileSizeSpinner, "3,9"));
        logGroup.add(miscBuilder.addLabel(" KB", "4,9"));
        miscBuilder.addSeparator("Mozilla / Netscape Cookies", "1,11,4,1");
        miscBuilder.addLabel("Cookies.txt", "1,13");
        miscBuilder.add(createBrowsePanel(cookiesTxtText, cookiesTxtBrowse), "3,13,2,1");
        miscBuilder.addSeparator("PluckThis", "1,15,4,1");
        miscBuilder.addLabel("Switch to this JXL when invoking PluckThis:", "1,17,4,1");
        miscBuilder.addLabel("JXL", "1,19");
        miscBuilder.add(createBrowsePanel(pluckthisJXLText, pluckthisJXLBrowse), "3,19,2,1");
        miscBuilder.addLabel("(Leave blank to use the current JXL.)", "3,21,2,1");

        formLayout = new FormLayout("pref, fill:pref:grow",
                                    "pref, 4dlu, fill:pref:grow, 7dlu, " + "pref, 4dlu, pref, 4dlu, pref");
        PanelBuilder autoUpdateBuilder = autoUpdateBuilder = new PanelBuilder(formLayout);
        autoUpdateBuilder.setBorder(Borders.TABBED_DIALOG_BORDER);
        autoUpdateBuilder.addSeparator("AutoUpdate JXLs", "1,1,2,1");
        autoUpdateBuilder.add(autoUpdatePanel, "1,3,2,1");
        autoUpdateBuilder.addSeparator("Settings", "1,5,2,1");
        autoUpdateBuilder.add(autoUpdateIntervalPanel, "1,7,2,1");
        autoUpdateBuilder.add(autoUpdatePeriodCheck, "1,9");
        autoUpdateBuilder.add(autoUpdatePeriodPanel, "2,9");

        formLayout = new FormLayout("pref, 6dlu, pref:grow",
                                    "pref, 4dlu, pref, 4dlu, pref, 7dlu, " +
                                    ((!Platform.isWindows() && !Platform.isMacOSX()) ? "pref, 4dlu, pref, 7dlu, " : "") +
                                    "pref, 4dlu, pref, 2dlu, pref, 2dlu, pref, 2dlu, pref, 2dlu, pref, " +
                                    (Platform.isWindows() ? "2dlu, pref, " : "") + "7dlu, pref, 4dlu, pref, 2dlu, pref");
        PanelBuilder applicationBuilder = new PanelBuilder(formLayout);
        applicationBuilder.setBorder(Borders.TABBED_DIALOG_BORDER);
        applicationBuilder.addSeparator("Look && Feel", "1,1,3,1");
        applicationBuilder.add(lafBox, "1,3,1,1,left,center");
        applicationBuilder.addLabel("(Restart JPluck for changes to take effect.)", "3,3,1,3,left,center");
    applicationBuilder.add(plasticThemeCombo, "1,5,1,1,left,center");
        int row = 7;
        if (!Platform.isWindows() && !Platform.isMacOSX()) {
            applicationBuilder.addSeparator("Web Browser", "1," + row + ",3,1");
            row += 2;
            applicationBuilder.addLabel("Browser Path", "1," + row);
            applicationBuilder.add(createBrowsePanel(browserText, browserBrowse), "3," + row);
            row += 2;
        }
        applicationBuilder.addSeparator("Interface", "1," + row + ",3,1");
        row += 2;
        applicationBuilder.add(showSplashScreenCheck, "1," + row + ",3,1");
        row += 2;
        applicationBuilder.add(displayFullFilePathCheck, "1," + row + ",3,1");
        row += 2;
        applicationBuilder.add(confirmCutCheck, "1," + row + ",3,1");
        row += 2;
        applicationBuilder.add(autoSelectDueCheck, "1," + row + ",3,1");
        row += 2;
        applicationBuilder.add(autoCloseCheck, "1," + row + ",3,1");
        if (Platform.isWindows()) {
            row += 2;
            applicationBuilder.add(trayIconCheck, "1," + row + ",3,1");
        }
            row += 2;
            applicationBuilder.addSeparator("Image parsing", "1," + row + ",3,1");
            row += 2;
      imageIOCheck = new JCheckBox("Use ImageIO for parsing images");
            applicationBuilder.add(imageIOCheck, "1," + row + ",3,1");
            row+=2;
            applicationBuilder.addLabel("(Disable this ONLY if you see garbled images in the Plucker documents.)", "1,"+row+",3,1");

        banner = new Banner();
        banner.addSheet("Document Preferences", "Plucker, destination and feed settings", SHEETS[0]);
        banner.addSheet("Network Preferences", "HTTP and proxy settings", SHEETS[1]);
        banner.addSheet("Cache Preferences", "Download and Showcase cache settings", SHEETS[2]);
        banner.addSheet("Miscellaneous Preferences", "Log, PluckThis and cookie settings ", SHEETS[3]);
        banner.addSheet("AutoUpdate Preferences", "AutoUpdate JXLs and settings", SHEETS[4]);
        banner.addSheet("Application Preferences", "Look & Feel and interface settings ", SHEETS[5]);
        tabbedPane.setBorder(Borders.createEmptyBorder("4dlu, 7dlu, 0dlu, 7dlu"));
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        tabbedPane.addTab("Document", documentBuilder.getPanel());
        tabbedPane.addTab("Network", networkBuilder.getPanel());
        tabbedPane.addTab("Cache", cacheBuilder.getPanel());
        tabbedPane.addTab("Miscellaneous", miscBuilder.getPanel());
        tabbedPane.addTab("AutoUpdate", autoUpdateBuilder.getPanel());
        tabbedPane.addTab("Application", applicationBuilder.getPanel());
        tabbedPane.setSelectedIndex(activeTab);
        tabbedPane.addChangeListener(new TabListener());

        setTitle("Preferences");
        setModal(true);
        setResizable(false);

        initDialog(banner, tabbedPane);

        setLocationRelativeTo(getOwner());
    }

    private void initValues() {
        ClientConfiguration conf = ClientConfiguration.getDefault();
        compressionCombo.setSelectedIndex(conf.getCompression());
        multiImageCheck.setSelected(conf.isUseMultiImage());

        Destination dest = conf.getDestination();
        if (dest != null) {
            hotsyncRadio.setSelected(dest.isHotsync());
            directoryRadio.setSelected(!dest.isHotsync());
            dest = conf.getHotSyncDestination();
            if (dest != null) {
                userCombo.setSelectedItem(dest.getUser());
                memoryCardCheck.setSelected(dest.isMemoryCard());
            }
            dest = conf.getDirectoryDestination();
            if (dest != null) {
                directoryText.setText(dest.getDirectory().getAbsolutePath());
            }
        }

        String backupDirectory = conf.getBackupDirectory();
        if (backupDirectory != null) {
            backupText.setText(backupDirectory);
        }
        categoryDirectoriesCheck.setSelected(conf.isCreateCategoryDirectories());

        String s = conf.getDateFormatName();
        int idx;
        if (s.equals("short")) {
            idx = 0;
        } else if (s.equals("medium")) {
            idx = 1;
        } else if (s.equals("long")) {
            idx = 2;
        } else {
            // Full
            idx = 3;
        }
        dateFormatCombo.setSelectedIndex(idx);
        feedDateCheck.setSelected(conf.isUseFeedDate());
        bookmarksCheck.setSelected(conf.isGenerateBookmarks());

        maxConnectionsSpinner.setValue(new Integer(conf.getMaxConnections()));
        timeoutSpinner.setValue(new Integer(conf.getTimeout()));
        downloadAttemptsSpinner.setValue(new Integer(conf.getDownloadAttempts()));
        acceptCharsetText.setText(conf.getAcceptCharset());
        cacheDirectoryText.setText(conf.getCacheDirectory());
        cacheExpirySpinner.setValue(new Integer(conf.getCacheExpiration()));
        compactCacheCheck.setSelected(conf.isAutoCompactCache());

        useProxyCheck.setSelected(conf.isUseProxy());
        proxyHostText.setText(conf.getProxyHost());
        proxyPortSpinner.setValue(new Integer(conf.getProxyPort()));
        proxyUserText.setText(conf.getProxyUser());
        proxyPasswordText.setText(conf.getProxyPassword());

        logFileCheck.setSelected(conf.isLogEnabled());
        logFileText.setText(conf.getLogFile());
        logFileSizeSpinner.setValue(new Integer(conf.getMaxLogSize()));
        logLevelCombo.setSelectedItem(conf.getLogLevel().getName());
        cookiesTxtText.setText(conf.getCookiesTxt());
        pluckthisJXLText.setText(conf.getPluckThisJXL());

        autoUpdatePanel.setFiles(conf.getAutoUpdateJXLs());
        autoUpdatePeriodCheck.setSelected(conf.isAutoUpdatePeriodEnabled());
        Period period = conf.getAutoUpdatePeriod();
        autoUpdateFromSpinner.setValue(period.getFrom().toDate());
        autoUpdateToSpinner.setValue(period.getTo().toDate());
        autoUpdateIntervalSpinner.setValue(new Integer(conf.getAutoUpdateInterval()));

        lafBox.selectCurrent();
    lafListener = new LAFListener();
    lafBox.addItemListener(lafListener);
        plasticThemeCombo.setSelectedTheme(conf.getPlasticTheme());
        browserText.setText(conf.getBrowser());
        autoCloseCheck.setSelected(conf.isAutoCloseConversion());
        displayFullFilePathCheck.setSelected(conf.isDisplayFullFilePath());
        autoSelectDueCheck.setSelected(conf.isAutoSelectDue());
        showSplashScreenCheck.setSelected(conf.isShowSplashScreen());
        confirmCutCheck.setSelected(conf.isConfirmCut());
        trayIconCheck.setSelected(conf.isMinimizeToTrayIcon());
        imageIOCheck.setSelected(conf.isUseImageIO());

        hotSyncListener.itemStateChanged(null);
        destinationListener.itemStateChanged(null);
        proxyListener.stateChanged(null);
        logListener.stateChanged(null);
        autoUpdatePeriodListener.stateChanged(null);
        lafListener.itemStateChanged(null);

        if (JPluckX.getInstance().isWebStart()) {
            clearShowcaseCacheAction.setEnabled(false);
        } else {
            updateShowcaseSize();
        }
    }

    private File getShowcaseImagesDir() {
        URL url = PreferencesDialog.class.getClassLoader().getResource("showcase/images");
        URI uri = URI.create(url.toString());
        return new File(uri);
    }

    private void updateShowcaseSize() {
        File[] files = getShowcaseImagesDir().listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.endsWith(".gif");
                }
            });
        long size = 0;
        for (int i = 0; i < files.length; i++) {
            size += files[i].length();
        }
        clearShowcaseCacheAction.setEnabled(size > 0);
        showcaseCacheLabel.setText("Showcase image cache size: " + (size / 1024) + "Kb");
    }

    public static String configureBrowser(JFrame owner) {
        activeTab = 5;
        PreferencesDialog dlg = new PreferencesDialog(owner);
        dlg.browserText.requestFocus();
        dlg.show();
        return ClientConfiguration.getDefault().getBrowser();
    }

    private class AutoUpdatePeriodListener implements ChangeListener {
        public void stateChanged(ChangeEvent ev) {
            GUIUtil.enablePanel(autoUpdatePeriodPanel, autoUpdatePeriodCheck.isSelected());
        }
    }

    private class ClearShowcaseCacheAction extends AbstractAction {
        ClearShowcaseCacheAction() {
            super("Clear...");
        }

        public void actionPerformed(ActionEvent ev) {
            int result = JOptionPane.showConfirmDialog(PreferencesDialog.this, "Clear Showcase cache?", "Clear",
                                                       JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (result == JOptionPane.OK_OPTION) {
                File[] files = getShowcaseImagesDir().listFiles(new FilenameFilter() {
                        public boolean accept(File dir, String name) {
                            return name.endsWith(".gif");
                        }
                    });
                for (int i = 0; i < files.length; i++) {
                    files[i].delete();
                }
                updateShowcaseSize();
                setEnabled(false);
            }
        }
    }

    private class CompactCacheAction extends AbstractAction {
        CompactCacheAction() {
            super("Compact now");
        }

        public void actionPerformed(ActionEvent ev) {
            HttpCache httpCache = ClientConfiguration.getDefault().getHttpCache();
            if (httpCache != null) {
                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                httpCache.open();
                httpCache.shutdownCompact();
                setCursor(Cursor.getDefaultCursor());
            }
        }
    }

    /*private class ContinuousPagesWarning implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (!segmentLinksCheck.isSelected()) {
                JOptionPane.showMessageDialog(multiImageCheck,
                                              "NOTE:\nYou must have a Plucker reader capable of rendering\n" +
                                              "continuous pages when you disable segment links.", "Segment Links",
                                              JOptionPane.WARNING_MESSAGE);
            }
        }
    }*/

    private class DefaultAcceptCharsetAction extends AbstractAction {
        public DefaultAcceptCharsetAction() {
            super("Default");
        }

        public void actionPerformed(ActionEvent e) {
            acceptCharsetText.setText(HttpClient.DEFAULT_ACCEPT_CHARSET);
        }
    }

    private class DestinationListener implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            boolean b = hotsyncRadio.isSelected();
            userCombo.setEnabled(b);
            directoryText.setEnabled(!b);
            directoryBrowse.setEnabled(!b);
            categoryDirectoriesCheck.setEnabled(!b);
            memoryCardCheck.setEnabled(b);
            hotSyncListener.itemStateChanged(e);
        }
    }

    private class HotSyncListener implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            if (users.length > 0) {
                int slotCount = PalmInstall.getSlotCount((String) userCombo.getSelectedItem());
                memoryCardCheck.setEnabled(hotsyncRadio.isSelected() && (slotCount > 0));
            }
        }
    }

    private class LogListener implements ChangeListener {
        public void stateChanged(ChangeEvent e) {
            logGroup.setEnabled(logFileCheck.isSelected());
        }
    }

    /*private class MultiImageWarning implements ActionListener {
        public void actionPerformed(ActionEvent arg0) {
            if (multiImageCheck.isSelected()) {
                JOptionPane.showMessageDialog(multiImageCheck,
                                              "NOTE:\nYou must have a Plucker reader\ncapable " +
                                              "of displaying multi-images.", "Multi-images",
                                              JOptionPane.WARNING_MESSAGE);
            }
        }
    }*/

    private class TabListener implements ChangeListener {
        public void stateChanged(ChangeEvent e) {
            banner.showSheet(SHEETS[tabbedPane.getSelectedIndex()]);
        }
    }

    private class UseProxyListener implements ChangeListener {
        public void stateChanged(ChangeEvent e) {
            proxyGroup.setEnabled(useProxyCheck.isSelected());
        }
    }
   
    private class LAFListener implements ItemListener {

    public void itemStateChanged(ItemEvent e) {
      plasticThemeCombo.setEnabled(lafBox.getSelectedLAF().getName().startsWith("Plastic"));
    }
     
    }
}
TOP

Related Classes of net.sf.jpluck.apps.jpluckx.ui.PreferencesDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.