Skip to content
This repository has been archived by the owner on Jul 16, 2022. It is now read-only.

Commit

Permalink
Support appearance configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
  • Loading branch information
miurahr committed Mar 19, 2022
1 parent 4e43c98 commit 5433ad2
Show file tree
Hide file tree
Showing 15 changed files with 443 additions and 131 deletions.
30 changes: 5 additions & 25 deletions src/main/java/io/github/eb4j/ebview/EBViewer.java
Expand Up @@ -19,6 +19,7 @@
package io.github.eb4j.ebview;

import com.formdev.flatlaf.FlatLightLaf;
import io.github.eb4j.ebview.core.Core;
import io.github.eb4j.ebview.dictionary.DictionariesManager;
import io.github.eb4j.ebview.gui.MainWindow;
import io.github.eb4j.ebview.gui.MainWindowMenu;
Expand All @@ -32,29 +33,7 @@

public class EBViewer implements Runnable {

private final DictionariesManager dictionariesManager;
private final MainWindow mw;

public EBViewer(final File dictionaryDirectory, final boolean remote) {
dictionariesManager = new DictionariesManager();
if (dictionaryDirectory != null) {
dictionariesManager.loadDictionaries(dictionaryDirectory);
}
mw = new MainWindow(dictionariesManager);
if (!remote) {
new MainWindowMenu(this);
mw.showMessage("Please add dictionaries from Dictionary menu at first.");
} else {
mw.showMessage("Please enter search word above input box.");
}
}

public DictionariesManager getDictionariesManager() {
return dictionariesManager;
}

public JFrame getApplicationFrame() {
return mw.getApplicationFrame();
public EBViewer() {
}

/**
Expand All @@ -76,7 +55,8 @@ public static void main(final String... args) {
}
try {
Preferences.init();
EBViewer viewer = new EBViewer(dictionaryDirectory, remote);
Core.initializeGUI(dictionaryDirectory, remote);
EBViewer viewer = new EBViewer();
Thread t = new Thread(viewer);
t.start();
} catch (Exception e) {
Expand All @@ -97,6 +77,6 @@ public static void main(final String... args) {
*/
@Override
public void run() {
mw.setVisible(true);
Core.run();
}
}
101 changes: 101 additions & 0 deletions src/main/java/io/github/eb4j/ebview/core/Core.java
@@ -0,0 +1,101 @@
/*
* EBViewer, a dictionary viewer application.
* Copyright (C) 2022 Hiroshi Miura.
*
* 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 <https://www.gnu.org/licenses/>.
*/

package io.github.eb4j.ebview.core;

import io.github.eb4j.ebview.data.DictionaryEntry;
import io.github.eb4j.ebview.dictionary.DictionariesManager;
import io.github.eb4j.ebview.gui.IMainWindow;
import io.github.eb4j.ebview.gui.MainWindow;
import io.github.eb4j.ebview.gui.MainWindowMenu;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.Font;
import java.io.File;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Core {

private static final List<IFontChangedListener> FONT_CHANGED_EVENT_LISTENERS = new CopyOnWriteArrayList<>();
private static DictionariesManager dictionariesManager;
private static MainWindow mw;

private Core() {
}

public static void initializeGUI(final File dictionaryDirectory, final boolean remote) {
dictionariesManager = new DictionariesManager();
mw = new MainWindow();
if (dictionaryDirectory != null) {
dictionariesManager.loadDictionaries(dictionaryDirectory);
}
if (!remote) {
new MainWindowMenu();
mw.showMessage("Please add dictionaries from Dictionary menu at first.");
} else {
mw.showMessage("Please enter search word above input box.");
}
}

public static void run() {
mw.setVisible(true);
}

public static IMainWindow getMainWindow() {
return mw;
}
public static DictionariesManager getDictionariesManager() {
return dictionariesManager;
}

public static JFrame getApplicationFrame() {
return mw.getApplicationFrame();
}

/** Register listener. */
public static void registerFontChangedEventListener(final IFontChangedListener listener) {
FONT_CHANGED_EVENT_LISTENERS.add(listener);
}

/** Unregister listener. */
public static void unregisterFontChangedEventListener(final IFontChangedListener listener) {
FONT_CHANGED_EVENT_LISTENERS.remove(listener);
}

/** Fire event. */
public static void fireFontChanged(final Font newFont) {
SwingUtilities.invokeLater(() -> {
for (IFontChangedListener listener : FONT_CHANGED_EVENT_LISTENERS) {
try {
listener.onFontChanged(newFont);
} catch (Throwable ignored) {
}
}
});
}

public static void updateDictionaryPane(final List<DictionaryEntry> entries) {
mw.updateDictionaryPane(entries);
}

public static void moveTo(final int index) {
mw.moveTo(index);
}
}
25 changes: 25 additions & 0 deletions src/main/java/io/github/eb4j/ebview/core/IFontChangedListener.java
@@ -0,0 +1,25 @@
/*
* EBViewer, a dictionary viewer application.
* Copyright (C) 2022 Hiroshi Miura.
*
* 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 <https://www.gnu.org/licenses/>.
*/

package io.github.eb4j.ebview.core;

import java.awt.Font;

public interface IFontChangedListener {
void onFontChanged(Font newFont);
}
@@ -1,3 +1,21 @@
/*
* EBViewer, a dictionary viewer application.
* Copyright (C) 2021-2022 Hiroshi Miura.
*
* 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 <https://www.gnu.org/licenses/>.
*/

package io.github.eb4j.ebview.dictionary.oxford;

import io.github.eb4j.ebview.data.DictionaryEntry;
Expand Down
55 changes: 10 additions & 45 deletions src/main/java/io/github/eb4j/ebview/gui/DictionaryPane.java
@@ -1,5 +1,6 @@
package io.github.eb4j.ebview.gui;

import io.github.eb4j.ebview.core.Core;
import io.github.eb4j.ebview.data.DictionaryEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -39,17 +40,12 @@ public class DictionaryPane extends JTextPane implements IThreadPane {

private final HTMLEditorKit htmlEditorKit = new HTMLEditorKit();

private StyleSheet baseStyleSheet;
private int zoomLevel = 0;

public DictionaryPane() {
public DictionaryPane(final Font font) {
super();
setContentType("text/html");
((HTMLDocument) getDocument()).setPreservesUnknownTags(false);
setFont(getFont());
setStyle();
htmlEditorKit.setStyleSheet(baseStyleSheet);
setEditorKit(htmlEditorKit);
setFont(font);
Core.registerFontChangedEventListener(this::setFont);
FocusListener listener = new FocusAdapter() {
@Override
public void focusGained(final FocusEvent e) {
Expand All @@ -70,58 +66,27 @@ public void setFont(final Font font) {
Map attributes = font.getAttributes();
attributes.put(TextAttribute.LIGATURES, TextAttribute.LIGATURES_ON);
super.setFont(font.deriveFont(attributes));
Document doc = getDocument();
if (!(doc instanceof HTMLDocument)) {
return;
if (htmlEditorKit != null) {
setStyle();
}
}

@SuppressWarnings({"avoidinlineconditionals"})
private void setStyle() {
Font font = getFont();
baseStyleSheet = new StyleSheet();
StyleSheet baseStyleSheet = new StyleSheet();
baseStyleSheet.addRule("body { font-family: " + font.getName() + "; "
+ " font-size: " + font.getSize() + "; "
+ " font-style: " + (font.getStyle() == Font.BOLD ? "bold"
: font.getStyle() == Font.ITALIC ? "italic" : "normal") + "; "
+ " color: " + toHex(UIManager.getColor("TextPane.foreground")) + "; "
+ " background: " + toHex(UIManager.getColor("TextPane.background")) + "; } "
+ ".word {font-size: " + (zoomLevel + 2 + font.getSize()) + "; font-style: bold; }"
+ ".article {font-size: " + (zoomLevel + font.getSize()) + "; }"
+ ".word {font-size: " + (2 + font.getSize()) + "; font-style: bold; }"
+ ".article {font-size: " + (font.getSize()) + "; }"
+ ".reference { font-style: italic; }"
);
htmlEditorKit.setStyleSheet(baseStyleSheet);
}

private void limitZoom() {
int size = getFont().getSize();
if (size + zoomLevel > 64) {
zoomLevel = 64 - size;
return;
}
if (size + zoomLevel < 6) {
zoomLevel = size - 6;
}
}

public void increaseZoom() {
zoomLevel++;
limitZoom();
setStyle();
}

public void decreaseZoom() {
zoomLevel--;
limitZoom();
setStyle();
}

public String getZoomLevel() {
if (zoomLevel > 0) {
return "+" + zoomLevel;
} else {
return String.valueOf(zoomLevel);
}
setEditorKit(htmlEditorKit);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/github/eb4j/ebview/gui/EBViewerModel.java
Expand Up @@ -18,6 +18,7 @@

package io.github.eb4j.ebview.gui;

import io.github.eb4j.ebview.core.Core;
import io.github.eb4j.ebview.data.DictionaryEntry;

import javax.swing.DefaultListModel;
Expand Down Expand Up @@ -113,7 +114,7 @@ public void updateResult() {
}
}
updateHeadingsModel(wordList);
MainWindow.updateDictionaryPane(entries);
Core.updateDictionaryPane(entries);
}

}
6 changes: 4 additions & 2 deletions src/main/java/io/github/eb4j/ebview/gui/IMainWindow.java
Expand Up @@ -3,10 +3,12 @@
import io.github.eb4j.ebview.dictionary.DictionariesManager;

import javax.swing.JFrame;
import java.awt.Font;

public interface IMainWindow {

DictionariesManager getDictionariesManager();
void showMessage(String msg);

JFrame getApplicationFrame();

Font getApplicationFont();
}

0 comments on commit 5433ad2

Please sign in to comment.