feat: user tab preferences added

This commit is contained in:
Georgy Litvinov 2021-08-06 17:10:05 +02:00
parent 505fbd23cd
commit 1d4ce4e100
13 changed files with 343 additions and 123 deletions

View file

@ -1 +1 @@
version=0.9.20
version=0.9.21

View file

@ -24,19 +24,17 @@ public class BasePanel extends JPanel {
*/
private static final long serialVersionUID = 1L;
private Localizer localizer = null;
private JFrame singleFrame = null;
private UIPreferences preferences = null;
private HashMap<String, Component> configuration = null;
public BasePanel(Localizer localizer, JFrame singleFrame, HashMap<String, Component> configuration, UIPreferences preferences) {
public BasePanel(Localizer localizer, HashMap<String, Component> configuration, UIPreferences preferences) {
this.localizer = localizer;
this.singleFrame = singleFrame;
this.configuration = configuration;
this.preferences = preferences;
}
public BasePanel(Localizer localizer, JFrame singleFrame) {
public BasePanel(Localizer localizer) {
this.localizer = localizer;
this.singleFrame = singleFrame;
}
protected JCheckBox addMathJaxCB(Localizer localizer, UIPreferences preferences) {
@ -57,7 +55,7 @@ public class BasePanel extends JPanel {
return;
}
if (!parent.canWrite()) {
JOptionPane.showMessageDialog(singleFrame, localizer.getTranslation(CANT_WRITE_MESSAGE));
JOptionPane.showMessageDialog(ConfigurationWindow.getSingleFrame(), localizer.getTranslation(CANT_WRITE_MESSAGE));
}
}
if (newFilePath.length() < 3) {
@ -111,13 +109,6 @@ public class BasePanel extends JPanel {
return tf_FilterLetterSpacing;
}
protected JTextField addSplitByLevel() {
JTextField tf_SplitByLevel = new JTextField();
configuration.put(ARG_SPLIT_LEVEL, tf_SplitByLevel);
tf_SplitByLevel.setText(preferences.get(PREF_SPLIT_BY_LEVEL));
tf_SplitByLevel.setColumns(10);
return tf_SplitByLevel;
}
protected JCheckBox addInlineStyles() {
JCheckBox cb_InlineStyles = new JCheckBox(localizer.getTranslation(LABEL_INLINE_STYLES));
@ -189,6 +180,13 @@ public class BasePanel extends JPanel {
return cbox_image_resolution;
}
protected JComboBox addSplitByLevel() {
JComboBox cbox_SplitByLevel = new JComboBox(preferences.getAll(PREF_SPLIT_LEVELS));
configuration.put(ARG_SPLIT_LEVEL, cbox_SplitByLevel);
cbox_SplitByLevel.setSelectedIndex(Integer.parseInt(preferences.get(PREF_SPLIT_BY_LEVEL)));
return cbox_SplitByLevel;
}
protected JTextField addMaxWidth() {
JTextField tf_MaxWidth = new JTextField(preferences.get(PREF_MAX_WIDTH));
tf_MaxWidth.setColumns(10);

View file

@ -33,6 +33,10 @@ public class ConfigurationWindow extends JFrame {
private static JFrame singleFrame = null;
public static JFrame getSingleFrame() {
return singleFrame;
}
/**
* Launch the application.
*/
@ -81,10 +85,11 @@ public class ConfigurationWindow extends JFrame {
getContentPane().setLayout(new GridLayout(1, 0, 0, 0));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
getContentPane().add(tabbedPane);
PanelFactory panelFactory = new PanelFactory(localizer, singleFrame, storage, tabbedPane);
JPanel html = panelFactory.createPanel(HTML,HTML);
JPanel rdf = panelFactory.createPanel(RDF,RDF);
JPanel epub = panelFactory.createPanel(EPUB,EPUB);
PanelFactory panelFactory = new PanelFactory(localizer, storage, tabbedPane);
panelFactory.createPanels();
JPanel epub = panelFactory.createPanel(EPUB);
JPanel rdf = panelFactory.createPanel(RDF);
JPanel html = panelFactory.createPanel(HTML);
}
public static void runGUI(XComponentContext context) {

View file

@ -12,42 +12,90 @@ import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
public class ControlPanel extends BasePanel {
public ControlPanel(HashMap<String,Component> configuration,Localizer localizer, JFrame singleFrame) {
super(localizer, singleFrame);
private static final Logger logger = LoggerFactory.getLogger(ControlPanel.class);
private PreferencesStorage storage;
private String configName;
private String configType;
private PanelFactory factory = null;
public void setFactory(PanelFactory factory) {
this.factory = factory;
}
public ControlPanel(HashMap<String,Component> configuration,Localizer localizer, PreferencesStorage storage, String configName, String configType) {
super(localizer);
this.storage = storage;
this.configName = configName;
this.configType = configType;
JButton btn_Exit = new JButton(localizer.getTranslation(BUTTON_EXIT_LABEL));
btn_Exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
singleFrame.setVisible(false);
singleFrame.dispose();
ConfigurationWindow.getSingleFrame().setVisible(false);
ConfigurationWindow.getSingleFrame().dispose();
}
});
JButton btn_SaveAs = new JButton(localizer.getTranslation(BUTTON_SAVEAS_LABEL));
btn_SaveAs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
JOptionPane.showMessageDialog(singleFrame, localizer.getTranslation(MESSAGE_NOT_IMPLEMENTED));
String newConfigName = JOptionPane.showInputDialog(localizer.getTranslation(ENTER_NEW_CONFIG_NAME));
if (newConfigName == null || newConfigName.isEmpty()) {
return;
}
if (storage.has(newConfigName)) {
JOptionPane.showMessageDialog(ConfigurationWindow.getSingleFrame(), localizer.getTranslation(CONFIG_NAME_IS_ALREADY_IN_USE));
return;
}
UIPreferences newPrefs = storage.get(configName).clone();
newPrefs.updateFrom(configuration);
storage.put(newConfigName, newPrefs);
if (factory != null) {
factory.createPanel(newConfigName);
}
storage.save();
}
});
JButton btn_Save = new JButton(localizer.getTranslation(BUTTON_SAVE_LABEL));
btn_Save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
JOptionPane.showMessageDialog(singleFrame, localizer.getTranslation(MESSAGE_NOT_IMPLEMENTED));
storage.get(configName).updateFrom(configuration);
storage.save();
JOptionPane.showMessageDialog(ConfigurationWindow.getSingleFrame(), localizer.getTranslation(SUCCESSFULLY_SAVED));
}
});
JButton btn_delete = new JButton(localizer.getTranslation(BUTTON_DELETE_LABEL));
btn_delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
storage.remove(configName);
factory.removeCurPanel();
storage.save();
}
});
if (configName == HTML || configName == RDF || configName == EPUB) {
btn_Save.setVisible(false);
btn_delete.setVisible(false);
}
JButton btn_startConversion = new JButton(localizer.getTranslation(BUTTON_CONVERT_LABEL));
btn_startConversion.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
try {
ConversionExecutor executor = new ConversionExecutor(configuration, singleFrame);
ConversionExecutor executor = new ConversionExecutor(configuration, ConfigurationWindow.getSingleFrame());
executor.convert();
} catch(Throwable e) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
JOptionPane.showMessageDialog(singleFrame,errors.toString());
JOptionPane.showMessageDialog(ConfigurationWindow.getSingleFrame(),errors.toString());
}
}
@ -63,20 +111,23 @@ public class ControlPanel extends BasePanel {
.addComponent(btn_SaveAs, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(btn_Save, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(btn_delete, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(btn_startConversion, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
.addContainerGap(427, Short.MAX_VALUE))
.addContainerGap(86, Short.MAX_VALUE))
);
gl_panel_control.setVerticalGroup(
gl_panel_control.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_control.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel_control.createParallelGroup(Alignment.BASELINE)
.addComponent(btn_SaveAs)
.addComponent(btn_Save)
.addComponent(btn_Exit)
.addComponent(btn_startConversion))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(gl_panel_control.createParallelGroup(Alignment.TRAILING, false)
.addComponent(btn_startConversion, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn_delete, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn_SaveAs, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn_Save, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btn_Exit, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(54, Short.MAX_VALUE))
);
this.setLayout(gl_panel_control);

View file

@ -4,8 +4,11 @@ public class Contstants {
public static final String LABEL_HTML5 = "label_html5";
public static final String BUTTON_CONVERT_LABEL = "button_convert_label";
public static final String MESSAGE_NOT_IMPLEMENTED = "message_not_implemented";
public static final String CONFIG_NAME_IS_ALREADY_IN_USE = "config_name_is_already_in_use";
public static final String BUTTON_EXIT_LABEL = "button_exit_label";
public static final String BUTTON_SAVE_LABEL = "button_save_label";
public static final String BUTTON_DELETE_LABEL = "button_delete_label";
public static final String ENTER_NEW_CONFIG_NAME = "enter_new_config_name";
public static final String BUTTON_SAVEAS_LABEL = "button_saveas_label";
public static final String ARG_PAGINATION = "pagination";
public static final String ARG_IMAGE_RESOLUTION = "image_resolution";
@ -14,6 +17,8 @@ public class Contstants {
public static final String CSV_FILE_EXTENSION = "csv";
public static final String RDF_FILE_EXTENSION = "rdf";
public static final String CANT_WRITE_MESSAGE = "error_cant_write";
public static final String DEFAULT_CONFIG_CANT_BE_CHANGED = "default_config_cant_be_saved";
public static final String SUCCESSFULLY_SAVED = "successfully_saved_config";
public static final String HTML_FILE_EXTENSION = "html";
public static final String ARG_MAX_WIDTH = "max_width";
public static final String LABEL_SPLIT_BY_HEADING = "label_split_by_heading";
@ -69,23 +74,26 @@ public class Contstants {
public static final String RDF = "RDF";
public static final String PREF_RESOLUTIONS = "resolutions";
public static final String PREF_PUBLICATION_TYPES = "publication_types";
public static final String PREF_DEFAULT_PUBLICATION_TYPE = "default_publication_type";
public static final String PREF_DEFAULT_IMAGE_RESOLUTION = "default_image_resolution";
public static final String PREF_USE_MATHJAX = "use_mathjax";
public static final String PREF_IGNORE_EMPTY_PARS = "ignore_empty_paragraphs";
public static final String PREF_IGNORE_HARD_BREAKS = "ignore_hard_breaks";
public static final String PREF_SPLIT_BY_LEVEL = "split_by_level";
public static final String PREF_GREENSTONE_TAGS = "greenstone_tags";
public static final String PREF_SPLIT_WHOLE_PAGES = "split_whole_pages";
public static final String PREF_LETTER_SPACING = "letter_spacing_filter";
public static final String PREF_INLINE_STYLES = "inline_styles";
public static final String PREF_EMBED_RASTER = "embed_raster";
public static final String PREF_EMBED_VECTOR = "embed_vector";
public static final String PREF_SCALING = "scaling";
public static final String PREF_MAX_WIDTH = "max_width";
public static final String PREF_PAGEBREAK_STYLES = "pagebreak_styles";
public static final String PREF_CONVERT_TO_PX = "convert_to_px";
public static final String PREF_PAGINATION = "pagination";
public static final String PREF_DEFAULT_PUBLICATION_TYPE = ARG_RDF_TYPE;
public static final String PREF_DEFAULT_IMAGE_RESOLUTION = ARG_IMAGE_RESOLUTION;
public static final String PREF_USE_MATHJAX = ARG_USE_MATHJAX;
public static final String PREF_IGNORE_EMPTY_PARS = ARG_IGNORE_EMPTY_PARAGRAPHS;
public static final String PREF_IGNORE_HARD_BREAKS = ARG_IGNORE_HARD_LINE_BREAKS;
public static final String PREF_SPLIT_BY_LEVEL = ARG_SPLIT_LEVEL;
public static final String PREF_GREENSTONE_TAGS = ARG_GREENSTONE;
public static final String PREF_SPLIT_WHOLE_PAGES = ARG_ALIGN_SPLITS_TO_PAGES;
public static final String PREF_LETTER_SPACING = ARG_MIN_LETTER_SPACING;
public static final String PREF_INLINE_STYLES = ARG_CSS_INLINE;
public static final String PREF_EMBED_RASTER = ARG_EMBED_IMG;
public static final String PREF_EMBED_VECTOR = ARG_EMBED_SVG;
public static final String PREF_SCALING = ARG_SCALING;
public static final String PREF_MAX_WIDTH = ARG_MAX_WIDTH;
public static final String PREF_PAGEBREAK_STYLES = ARG_PAGE_BREAK_STYLE;
public static final String PREF_CONVERT_TO_PX = ARG_CONVERT_TO_PX;
public static final String PREF_PAGINATION = ARG_PAGINATION;
public static final String PREF_TYPE = "preferences_type";
public static final String PREF_SPLIT_LEVELS = "split_levels";
public static final String TAB_PREFERENCES = "tab_preferences";
}

View file

@ -25,8 +25,8 @@ public class EpubPanel extends BasePanel {
*/
private static final long serialVersionUID = 1L;
public EpubPanel(HashMap<String, Component> configuration, Localizer localizer, UIPreferences preferences,JFrame singleFrame) {
super(localizer, singleFrame, configuration, preferences);
public EpubPanel(HashMap<String, Component> configuration, Localizer localizer, UIPreferences preferences) {
super(localizer, configuration, preferences);
JLabel lb_FilterLetterSpacing = new JLabel(localizer.getTranslation(LABEL_MIN_LETTER_SPACING));
JLabel lb_TargetFormat_description = new JLabel(localizer.getTranslation(LABEL_TARGET_FORMAT));
@ -206,8 +206,4 @@ public class EpubPanel extends BasePanel {
});
return btn_chooseFile;
}
}

View file

@ -23,8 +23,8 @@ public class HTMLPanel extends BasePanel{
*/
private static final long serialVersionUID = 1L;
public HTMLPanel(HashMap<String, Component> configuration, Localizer localizer, UIPreferences preferences,JFrame singleFrame) {
super(localizer, singleFrame, configuration, preferences);
public HTMLPanel(HashMap<String, Component> configuration, Localizer localizer, UIPreferences preferences) {
super(localizer, configuration, preferences);
JLabel lb_SplitByLevel = new JLabel(localizer.getTranslation(LABEL_SPLIT_BY_HEADING));
JLabel lb_FilterLetterSpacing = new JLabel(localizer.getTranslation(LABEL_MIN_LETTER_SPACING));
@ -38,7 +38,6 @@ public class HTMLPanel extends BasePanel{
JLabel lb_pageBreakInlineStyle = new JLabel(localizer.getTranslation(LABEL_BREAK_STYLE));
JTextField tf_FilterLetterSpacing = addLetterSpacingFilter();
JTextField tf_SplitByLevel = addSplitByLevel();
JTextField tf_Scale = addScaling();
JTextField tf_MaxWidth = addMaxWidth();
JTextField tf_inputFile = addInputFile();
@ -57,6 +56,7 @@ public class HTMLPanel extends BasePanel{
JCheckBox cb_Pagination = addPagination();
JComboBox cbox_image_resolution = addImageResolution();
JComboBox cbox_SplitByLevel = addSplitByLevel();
JButton btn_chooseFile = addButtonChooseFile(localizer, tf_inputFile, tf_OutputFile);
JButton btn_ChooseOutputFile = addChooseOutputButton(tf_OutputFile, HTML_FILE_EXTENSION);
@ -71,7 +71,7 @@ public class HTMLPanel extends BasePanel{
.addGroup(gl_panel_configHTML.createSequentialGroup()
.addComponent(lb_SplitByLevel, GroupLayout.PREFERRED_SIZE, 454, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(tf_SplitByLevel, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
.addComponent(cbox_SplitByLevel, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
.addGap(541))
.addGroup(gl_panel_configHTML.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_configHTML.createSequentialGroup()
@ -125,8 +125,8 @@ public class HTMLPanel extends BasePanel{
.addComponent(lb_OutFilePath, GroupLayout.PREFERRED_SIZE, 191, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_configHTML.createParallelGroup(Alignment.LEADING)
.addComponent(tf_OutputFile, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)
.addComponent(tf_inputFile, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE))
.addComponent(tf_OutputFile, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
.addComponent(tf_inputFile, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel_configHTML.createParallelGroup(Alignment.LEADING)
.addComponent(btn_ChooseOutputFile, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
@ -203,7 +203,7 @@ public class HTMLPanel extends BasePanel{
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_panel_configHTML.createParallelGroup(Alignment.BASELINE)
.addComponent(lb_SplitByLevel, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
.addComponent(tf_SplitByLevel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addComponent(cbox_SplitByLevel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(cb_SplitWholePagesOnly)
.addPreferredGap(ComponentPlacement.UNRELATED)
@ -232,7 +232,5 @@ public class HTMLPanel extends BasePanel{
});
return btn_chooseFile;
}
}

View file

@ -2,6 +2,7 @@ package pro.litvinovg.w2phtml.gui;
import java.awt.Component;
import java.util.HashMap;
import java.util.Map;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
@ -9,39 +10,51 @@ import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static pro.litvinovg.w2phtml.gui.Contstants.*;
public class PanelFactory {
private Localizer localizer;
private JFrame singleFrame;
private PreferencesStorage storage;
private JTabbedPane tabbedPane;
private static final Logger logger = LoggerFactory.getLogger(PanelFactory.class);
public PanelFactory(Localizer localizer,JFrame singleFrame, PreferencesStorage preferences,JTabbedPane tabbedPane) {
public PanelFactory(Localizer localizer, PreferencesStorage storage,JTabbedPane tabbedPane) {
this.localizer = localizer;
this.singleFrame = singleFrame;
this.storage = preferences;
this.storage = storage;
this.tabbedPane = tabbedPane;
}
public JPanel createPanel(String type, String name) {
public JPanel createPanel(String name) {
UIPreferences prefs = storage.get(name);
String type = prefs.get(PREF_TYPE);
JPanel panel = new JPanel();
HashMap<String, Component> configuration = new HashMap<String, Component>();
JPanel panel_control = new ControlPanel(configuration, localizer, singleFrame);
ControlPanel panel_control = new ControlPanel(configuration, localizer, storage, name, type);
panel_control.setFactory(this);
JPanel panel_configuration = null;
if (EPUB.equals(type)){
panel_configuration = new EpubPanel(configuration, localizer, storage.get(name,type),singleFrame);
panel_configuration = new EpubPanel(configuration, localizer, storage.get(name));
} else if (RDF.equals(type)) {
panel_configuration = new RDFPanel(configuration, localizer, storage.get(name,type),singleFrame);
panel_configuration = new RDFPanel(configuration, localizer, storage.get(name));
} else {
panel_configuration = new HTMLPanel(configuration, localizer, storage.get(name,type),singleFrame);
panel_configuration = new HTMLPanel(configuration, localizer, storage.get(name));
}
applyDefaultLayout(panel, panel_configuration, panel_control);
tabbedPane.addTab(name, null, panel, null);
tabbedPane.setSelectedComponent(panel);
return panel;
}
public void removeCurPanel() {
tabbedPane.removeTabAt(tabbedPane.getSelectedIndex());
}
private void applyDefaultLayout(JPanel tabbedPanel, JPanel configPanel, JPanel controlPanel) {
GroupLayout gl_panel_html = new GroupLayout(tabbedPanel);
@ -55,5 +68,13 @@ public class PanelFactory {
.addComponent(controlPanel, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)));
tabbedPanel.setLayout(gl_panel_html);
}
public void createPanels() {
for (String name : storage.getTabNames()) {
if (!name.equals(HTML) && !name.equals(EPUB) && !name.equals(RDF)) {
createPanel(name);
logger.error("Created panel '" + name + "'");
}
}
}
}

View file

@ -4,10 +4,15 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.prefs.Preferences;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static pro.litvinovg.w2phtml.gui.Contstants.*;
@ -16,17 +21,14 @@ import static pro.litvinovg.w2phtml.gui.Contstants.*;
public class PreferencesStorage {
private static final Logger logger = LoggerFactory.getLogger(PreferencesStorage.class);
private Map<String,UIPreferences> preferences;
private Map<String,UIPreferences> tabs;
private Preferences storage;
public PreferencesStorage() {
initializeDefaults();
storage = Preferences.userRoot();
}
private void initializeDefaults() {
preferences = new HashMap();
storage = Preferences.userRoot().node(this.getClass().getName());
tabs = new HashMap();
read();
initializeDefault();
}
@ -37,7 +39,7 @@ public class PreferencesStorage {
}
private void initializeDefaultHTML() {
UIPreferences prefs = new UIPreferences();
UIPreferences prefs = new UIPreferences(this);
add(PREF_RESOLUTIONS, new String[]{"0","72","150","300","600","1200","2400"} , prefs);
add(PREF_DEFAULT_IMAGE_RESOLUTION,"2", prefs);
add(PREF_USE_MATHJAX,"true", prefs);
@ -48,15 +50,22 @@ public class PreferencesStorage {
add(PREF_INLINE_STYLES,"true", prefs);
add(PREF_EMBED_RASTER,"true", prefs);
add(PREF_EMBED_VECTOR,"true", prefs);
add(PREF_CONVERT_TO_PX,"false", prefs);
add(PREF_PAGEBREAK_STYLES,DEFAULT_PAGEBREAK_STYLE_SETTINGS,prefs);
add(PREF_PAGINATION,"true",prefs);
preferences.put(HTML, prefs);
add(PREF_SPLIT_LEVELS, new String[]{"0","1","2","3","4","5","6","7","8","9","10"} ,prefs);
add(PREF_SPLIT_BY_LEVEL,"0",prefs);
add(PREF_TYPE,HTML,prefs);
add(PREF_MAX_WIDTH,"",prefs);
add(PREF_SPLIT_WHOLE_PAGES,"false",prefs);
add(PREF_SCALING, SCALING_DEFAULT_VALUE, prefs);
prefs.setDefault(true);
tabs.put(HTML, prefs);
}
private void initializeDefaultEpub() {
UIPreferences prefs = new UIPreferences();
UIPreferences prefs = new UIPreferences(this);
add(PREF_RESOLUTIONS, new String[]{"0","72","150","300","600","1200","2400"} , prefs);
add(PREF_DEFAULT_IMAGE_RESOLUTION,"2", prefs);
add(PREF_USE_MATHJAX,"true", prefs);
@ -66,15 +75,17 @@ public class PreferencesStorage {
add(PREF_INLINE_STYLES,"true", prefs);
add(PREF_EMBED_RASTER,"true", prefs);
add(PREF_EMBED_VECTOR,"true", prefs);
add(PREF_CONVERT_TO_PX,"false", prefs);
add(PREF_SCALING, SCALING_DEFAULT_VALUE, prefs);
add(PREF_PAGEBREAK_STYLES,DEFAULT_PAGEBREAK_STYLE_SETTINGS,prefs);
add(PREF_PAGINATION,"true",prefs);
preferences.put(EPUB, prefs);
add(PREF_TYPE,EPUB,prefs);
prefs.setDefault(true);
tabs.put(EPUB, prefs);
}
private void initializeDefaultRDF() {
UIPreferences prefs = new UIPreferences();
UIPreferences prefs = new UIPreferences(this);
add(PREF_PUBLICATION_TYPES, new String[]{"elenphArticle", "encArticle", "book", "journal"} , prefs);
add(PREF_RESOLUTIONS, new String[]{"0","72","150","300","600","1200","2400"} , prefs);
add(PREF_DEFAULT_IMAGE_RESOLUTION,"2", prefs);
@ -86,40 +97,104 @@ public class PreferencesStorage {
add(PREF_INLINE_STYLES,"true", prefs);
add(PREF_EMBED_RASTER,"true", prefs);
add(PREF_EMBED_VECTOR,"true", prefs);
add(PREF_CONVERT_TO_PX,"false", prefs);
add(PREF_PAGEBREAK_STYLES,DEFAULT_PAGEBREAK_STYLE_SETTINGS,prefs);
add(PREF_TYPE,RDF,prefs);
add(PREF_SCALING, SCALING_DEFAULT_VALUE, prefs);
add(PREF_PAGINATION,"true",prefs);
prefs.setDefault(true);
tabs.put(RDF, prefs);
}
preferences.put(RDF, prefs);
public void put(String prefName, UIPreferences newPreferences) {
tabs.put(prefName, newPreferences);
}
public boolean has(String configName) {
return tabs.containsKey(configName);
}
private void add(String key, String[] strings,UIPreferences prefs) {
ArrayList<String> values = new ArrayList<String>(Arrays.asList(strings));
prefs.put(key, values);
prefs.put(key, strings);
}
private void add(String key, String value,UIPreferences prefs) {
ArrayList<String> values = new ArrayList<String>();
values.add(value);
prefs.put(key, values);
prefs.put(key, new String[] {value});
}
public UIPreferences get(String name, String type) {
if (preferences.containsKey(name)) {
return preferences.get(name);
public UIPreferences get(String name) {
if (tabs.containsKey(name)) {
return tabs.get(name);
} else {
return preferences.get(type);
logger.error("Prefereneces with name " + name + " not found.");
return null;
}
}
public void remove(String name) {
tabs.remove(name);
}
public void setSourceFileName(String fileName) {
Iterator<Entry<String, UIPreferences>> it = preferences.entrySet().iterator();
Iterator<Entry<String, UIPreferences>> it = tabs.entrySet().iterator();
while (it.hasNext()) {
Entry<String, UIPreferences> pair = it.next();
UIPreferences value = (UIPreferences) pair.getValue();
value.setSourceFileName(fileName);
}
}
public void save() {
String jsonPrefereneces = convertAllToJSON();
storage.put(TAB_PREFERENCES, jsonPrefereneces);
}
private String convertAllToJSON() {
JSONObject json = new JSONObject();
for (Map.Entry<String, UIPreferences> entry : tabs.entrySet()) {
String name = entry.getKey();
UIPreferences prefs = entry.getValue();
JSONObject json_prefs = prefs.getJSON();
json.put(name, json_prefs);
}
return json.toString();
}
public Set<String> getTabNames() {
return tabs.keySet();
}
private void read() {
String data = storage.get(TAB_PREFERENCES, null);
if (data == null) {
return;
}
try {
JSONObject json = new JSONObject(data);
Set<String> prefTabNames = json.keySet();
for (String prefTabName : prefTabNames) {
UIPreferences newPref = new UIPreferences(this);
JSONObject tabData = (JSONObject) json.get(prefTabName);
Set<String> prefKeys = tabData.keySet();
for (String name : prefKeys) {
JSONArray values = (JSONArray) tabData.get(name);
if (values != null) {
String[] valueArray = new String[values.length()];
for (int i=0;i<values.length();i++){
valueArray[i] = values.getString(i);
}
newPref.put(name, valueArray);
}
}
tabs.put(prefTabName, newPref);
}
} catch (JSONException e) {
logger.error("JSON parsing exception " + e.getLocalizedMessage());
e.printStackTrace();
} catch (Exception e) {
logger.error("JSON parsing exception " + e.getLocalizedMessage());
e.printStackTrace();
}
}
}

View file

@ -23,8 +23,8 @@ public class RDFPanel extends BasePanel {
*/
private static final long serialVersionUID = 1L;
public RDFPanel(HashMap<String, Component> configuration, Localizer localizer, UIPreferences preferences,JFrame singleFrame) {
super(localizer, singleFrame, configuration, preferences);
public RDFPanel(HashMap<String, Component> configuration, Localizer localizer, UIPreferences preferences) {
super(localizer, configuration, preferences);
JLabel lb_FilterLetterSpacing = new JLabel(localizer.getTranslation(LABEL_MIN_LETTER_SPACING));
JLabel lb_TargetFormat = addTargetFormat(RDF_TARGET_FORMAT);

View file

@ -1,30 +1,56 @@
package pro.litvinovg.w2phtml.gui;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static pro.litvinovg.w2phtml.gui.Contstants.*;
public class UIPreferences {
private Map<String,ArrayList<String>> prefs;
private static final Logger logger = LoggerFactory.getLogger(UIPreferences.class);
private Map<String,String[]> prefs;
private String sourceFileName = "";
public UIPreferences() {
prefs = new HashMap<String,ArrayList<String>>();
private boolean defaultTemplate = false;
private PreferencesStorage storage;
public void setDefault(boolean defaultTemplate) {
this.defaultTemplate = defaultTemplate;
}
public UIPreferences(Map<String,ArrayList<String>> sourcePrefs) {
Iterator<Entry<String, ArrayList<String>>> it = sourcePrefs.entrySet().iterator();
public UIPreferences(PreferencesStorage storage) {
this.storage = storage;
prefs = new HashMap<String,String[]>();
}
public UIPreferences(Map<String,String[]> sourcePrefs) {
Iterator<Entry<String, String[]>> it = sourcePrefs.entrySet().iterator();
prefs = new HashMap<String,String[]>();
while (it.hasNext()) {
Entry<String, ArrayList<String>> pair = (Entry<String, ArrayList<String>>) it.next();
prefs = new HashMap<String,ArrayList<String>>();
prefs.put(new String(pair.getKey()), new ArrayList<String>(pair.getValue()));
Entry<String, String[]> pair = (Entry<String, String[]>) it.next();
prefs.put(new String(pair.getKey()), Arrays.copyOf(pair.getValue(), pair.getValue().length));
}
}
public void put(String name, ArrayList<String>values) {
public void put(String name, String[]values) {
prefs.put(name, values);
}
@ -38,18 +64,23 @@ public class UIPreferences {
public String[] getAll(String name) {
if (prefs.containsKey(name)) {
return (String[]) prefs.get(name).toArray(new String[0]);
} else {
return new String[0];
return prefs.get(name);
}
if (defaultTemplate) {
logger.error("Default preferences doesn't have values for " + name);
return new String[]{""};
}
String[] type = prefs.get(PREF_TYPE);
if (type == null) {
logger.error("Tab preferences doesn't have type!");
return new String[] {""};
}
UIPreferences defaultPreferences = storage.get(type[0]);
return defaultPreferences.getAll(name);
}
public String get(String name) {
if (prefs.containsKey(name)) {
return prefs.get(name).get(0);
} else {
return "";
}
return getAll(name)[0];
}
public UIPreferences clone() {
@ -57,4 +88,31 @@ public class UIPreferences {
clonedPrefs.setSourceFileName(sourceFileName);
return clonedPrefs;
}
public void updateFrom(HashMap<String, Component> configuration) {
Set<String> keys = configuration.keySet();
for (String key : keys) {
String value = null;
Component component = configuration.get(key);
if (component.getClass().equals(JTextField.class)) {
value = ((JTextField) component).getText();
} else if (component.getClass().equals(JCheckBox.class)) {
value = Boolean.toString(((JCheckBox) component).isSelected());
} else if (component.getClass().equals(JLabel.class)) {
value = ((JLabel) component).getText();
} else if (component.getClass().equals(JComboBox.class)) {
value = Integer.toString(((JComboBox) component).getSelectedIndex());
}
prefs.put(key, new String[] {value});
}
}
public JSONObject getJSON() {
JSONObject prefObject = new JSONObject();
for (Map.Entry<String, String[]> entry : prefs.entrySet()) {
String name = entry.getKey();
String[] values = entry.getValue();
prefObject.put(name, values);
}
return prefObject;
}
}

View file

@ -28,3 +28,8 @@ label_html5=html5
error_cant_write=Attention. Can't save file in output directory.
message_not_implemented=Not implemented
label_image_resolution=Resize image resolution to (PPI)
enter_new_config_name=Enter name for new tab
config_name_is_already_in_use=Config name is already in use
default_config_cant_be_saved=Default tabs configuration can't be saved
successfully_saved_config=Tab configuration saved
button_delete_label=Delete

View file

@ -28,3 +28,8 @@ label_html5=html5
error_cant_write=Ошибка. Не удалось сохранить файл в целевую директорию
message_not_implemented=Данная функциональность еще не реализована
label_image_resolution=Уменьшить разрешение изображений до (PPI)
enter_new_config_name=Введите имя новой вкладки
config_name_is_already_in_use=Такое имя вкладки уже используется
default_config_cant_be_saved=Настройки вкладок по умолчанию нельзя сохранять
successfully_saved_config=Настройки вкладки сохранены
button_delete_label=Удалить