forked from java-decompiler/jd-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds "Viewer > appearance > Font size" to preferences
- Loading branch information
Showing
5 changed files
with
482 additions
and
345 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
services/src/main/groovy/jd/gui/service/preferencespanel/ViewerPreferencesProvider.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) 2008-2015 Emmanuel Dupuy | ||
* This program is made available under the terms of the GPLv3 License. | ||
*/ | ||
|
||
package jd.gui.service.preferencespanel | ||
|
||
import jd.gui.spi.PreferencesPanel | ||
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea | ||
import org.fife.ui.rsyntaxtextarea.Theme | ||
|
||
import javax.swing.* | ||
import javax.swing.event.DocumentEvent | ||
import javax.swing.event.DocumentListener | ||
import java.awt.* | ||
|
||
class ViewerPreferencesProvider extends JPanel implements PreferencesPanel, DocumentListener { | ||
static final int MIN_VALUE = 2 | ||
static final int MAX_VALUE = 40 | ||
static final String FONT_SIZE_KEY = 'ViewerPreferences.fontSize' | ||
|
||
PreferencesPanel.PreferencesPanelChangeListener listener = null | ||
JTextField fontSizeTextField | ||
Color errorBackgroundColor = Color.RED | ||
Color defaultBackgroundColor | ||
|
||
ViewerPreferencesProvider() { | ||
super(new BorderLayout()) | ||
|
||
add(new JLabel('Font size (' + MIN_VALUE + '..' + MAX_VALUE + '): '), BorderLayout.WEST) | ||
|
||
fontSizeTextField = new JTextField() | ||
fontSizeTextField.document.addDocumentListener(this) | ||
add(fontSizeTextField, BorderLayout.CENTER) | ||
|
||
defaultBackgroundColor = fontSizeTextField.background | ||
} | ||
|
||
// --- PreferencesPanel --- // | ||
String getPreferencesGroupTitle() { 'Viewer' } | ||
String getPreferencesPanelTitle() { 'Appearance' } | ||
|
||
public void init(Color errorBackgroundColor) { | ||
this.errorBackgroundColor = errorBackgroundColor | ||
} | ||
|
||
public boolean isActivated() { true } | ||
|
||
void loadPreferences(Map<String, String> preferences) { | ||
def fontSize = preferences.get(FONT_SIZE_KEY) | ||
|
||
if (! fontSize) { | ||
// Search default value for the current platform | ||
def textArea = new RSyntaxTextArea() | ||
|
||
def theme = Theme.load(getClass().classLoader.getResourceAsStream('rsyntaxtextarea/themes/eclipse.xml')) | ||
theme.apply(textArea) | ||
|
||
fontSize = textArea.font.size | ||
} | ||
|
||
fontSizeTextField.text = fontSize | ||
fontSizeTextField.setCaretPosition(fontSizeTextField.text.size()) | ||
} | ||
|
||
void savePreferences(Map<String, String> preferences) { | ||
preferences.put(FONT_SIZE_KEY, fontSizeTextField.text) | ||
} | ||
|
||
boolean arePreferencesValid() { | ||
try { | ||
int i = Integer.valueOf(fontSizeTextField.text) | ||
return (i >= MIN_VALUE) && (i <= MAX_VALUE) | ||
} catch (NumberFormatException ignore) { | ||
return false | ||
} | ||
} | ||
|
||
void addPreferencesChangeListener(PreferencesPanel.PreferencesPanelChangeListener listener) { | ||
this.listener = listener | ||
} | ||
|
||
// --- DocumentListener --- // | ||
void insertUpdate(DocumentEvent e) { onTextChange() } | ||
void removeUpdate(DocumentEvent e) { onTextChange() } | ||
void changedUpdate(DocumentEvent e) { onTextChange() } | ||
|
||
void onTextChange() { | ||
fontSizeTextField.background = arePreferencesValid() ? defaultBackgroundColor : errorBackgroundColor | ||
listener?.preferencesPanelChanged(this) | ||
} | ||
} |
Oops, something went wrong.