LookAndFeelPrefsPanel.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui.prefs |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* 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.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui.prefs;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.xnap.XNap;
import org.xnap.gui.AbstractSettingsPanel;
import org.xnap.gui.table.AbstractColumnTableModel;
import org.xnap.gui.table.Column;
import org.xnap.gui.table.TableHeaderHandler;
import org.xnap.gui.theme.Theme;
import org.xnap.gui.theme.ThemeManager;
import org.xnap.gui.util.*;
import org.xnap.gui.util.GridBagHelper;
/**
* Provides controls for look and feel settings.
*/
public class LookAndFeelPrefsPanel extends AbstractSettingsPanel
implements ListSelectionListener {
//--- Data field(s) ---
private DefaultListModel dlmLafs;
private JList jlLafs;
private DefaultListModel dlmThemes;
private JList jlThemes;
private JPanel jpPreview;
private JList iconThemesList;
private int lastIndex = 0;
private ConfigureThemeAction acConfigureTheme = new ConfigureThemeAction();
//--- Constructor(s) ---
public LookAndFeelPrefsPanel()
{
setLayout(new GridBagLayout());
// look and feel list
JPanel jpLafs = new JPanel(new BorderLayout());
jpLafs.add(new JLabel(XNap.tr("Look and Feel")), BorderLayout.NORTH);
dlmLafs = new DefaultListModel();
jlLafs = new JList(dlmLafs);
jlLafs.setVisibleRowCount(4);
JScrollPane jspLafs = new JScrollPane(jlLafs);
jpLafs.add(jspLafs, BorderLayout.CENTER);
// theme list
JPanel jpThemes = new JPanel(new BorderLayout());
jpThemes.add(new JLabel(XNap.tr("Theme")), BorderLayout.NORTH);
dlmThemes = new DefaultListModel();
jlThemes = new JList(dlmThemes);
jlThemes.setVisibleRowCount(4);
JScrollPane jspThemes = new JScrollPane(jlThemes);
jpThemes.add(jspThemes, BorderLayout.CENTER);
// icon theme list
JPanel iconThemesPanel = new JPanel(new BorderLayout());
iconThemesPanel.add
(new JLabel(XNap.tr("Icon Theme")), BorderLayout.NORTH);
iconThemesList = new JList(new DefaultListModel());
iconThemesList.setVisibleRowCount(4);
JScrollPane iconThemesScrollPane = new JScrollPane(iconThemesList);
iconThemesPanel.add(iconThemesScrollPane, BorderLayout.CENTER);
// look and feel panel
JPanel jpLists = new JPanel();
jpLists.setLayout(new BoxLayout(jpLists, BoxLayout.X_AXIS));
jpLists.add(jpLafs);
jpLists.add(Box.createHorizontalStrut(5));
jpLists.add(jpThemes);
jpLists.add(Box.createHorizontalStrut(5));
jpLists.add(iconThemesPanel);
GridBagHelper.add(this, jpLists);
// configure button
JPanel jpConfigure = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
JButton jbConfigure = new JButton(acConfigureTheme);
jpConfigure.add(jbConfigure);
GridBagHelper.add(this, jpConfigure);
// preview
JPanel jpControls = new JPanel();
jpControls.setLayout(new BoxLayout(jpControls, BoxLayout.Y_AXIS));
jpControls.add(new JLabel("I am a Label"));
jpControls.add(new JButton("I am a button"));
jpControls.add(new JCheckBox("I am a check box"));
jpControls.add(new JRadioButton("I am a radio"));
JPanel jpSlider = new JPanel();
jpSlider.setLayout(new BoxLayout(jpSlider, BoxLayout.Y_AXIS));
JSlider js = new JSlider(JSlider.HORIZONTAL, 0, 10, 3);
js.setSnapToTicks(true);
js.setPreferredSize(new Dimension(50, 50));
jpSlider.add(js);
JScrollBar jsb = new JScrollBar(JScrollBar.HORIZONTAL, 5, 2, 0, 10);
jpSlider.add(jsb);
JTabbedPane jtp = new JTabbedPane();
jtp.addTab("Controls", jpControls);
jtp.addTab("Slider", jpSlider);
MyTableModel tm = new MyTableModel();
JTable jtTable = new JTable(tm);
TableHeaderHandler.install(jtTable);
jtTable.setPreferredScrollableViewportSize(new Dimension(50, 50));
JScrollPane jspTable = new JScrollPane(jtTable);
JTree jtTree = new JTree();
jtTree.setVisibleRowCount(4);
JScrollPane jspTree = new JScrollPane(jtTree);
jpPreview = new JPanel(new GridLayout(1, 3, 10, 10));
jpPreview.add(jtp);
jpPreview.add(jspTable);
jpPreview.add(jspTree);
GridBagHelper.add(this, GUIHelper.createSeparator(XNap.tr("Preview")));
JPanel jpWrap = new JPanel(new BorderLayout());
jpWrap.add(jpPreview, BorderLayout.CENTER);
GridBagHelper.add(this, jpWrap);
GridBagHelper.addVerticalSpacer(this);
initializeLists();
}
public void initializeLists()
{
UIManager.LookAndFeelInfo[] info
= ThemeManager.getInstalledLookAndFeels();
String currentLaf = UIManager.getLookAndFeel().getClass().getName();
for (int i = 0; i < info.length; i++) {
dlmLafs.addElement(new MyLookAndFeelInfo(info[i]));
if (currentLaf.equals(info[i].getClassName())) {
jlLafs.setSelectedIndex(i);
jlLafs.ensureIndexIsVisible(i);
lastIndex = i;
}
}
jlLafs.addListSelectionListener(this);
DefaultListModel iconThemeModel
= ((DefaultListModel)iconThemesList.getModel());
Theme[] themes = ThemeManager.getThemes();
String currentTheme = prefs.getTheme();
String currentIconTheme = prefs.getIconTheme();
for (int i = 0; i < themes.length; i++) {
if (themes[i].isIconTheme()) {
iconThemeModel.addElement(themes[i]);
if (currentIconTheme.equals(themes[i].getClassName())) {
iconThemesList.setSelectedIndex
(iconThemeModel.getSize() - 1);
iconThemesList.ensureIndexIsVisible
(iconThemeModel.getSize() - 1);
}
}
else {
dlmThemes.addElement(themes[i]);
if (currentTheme.equals(themes[i].getClassName())) {
jlThemes.setSelectedIndex(dlmThemes.getSize() - 1);
jlThemes.ensureIndexIsVisible(dlmThemes.getSize() - 1);
}
}
}
jlThemes.addListSelectionListener(this);
acConfigureTheme.setEnabled
(getSelectedTheme() != null && getSelectedTheme().isConfigurable());
}
public void apply()
{
if (getSelectedLaf() != null) {
prefs.setLookAndFeel(getSelectedLaf().getClassName());
}
if (getSelectedTheme() != null) {
prefs.setTheme(getSelectedTheme().getClassName());
}
if (getSelectedIconTheme() != null) {
prefs.setIconTheme(getSelectedIconTheme().getClassName());
}
}
public Icon getIcon()
{
return null;
}
public UIManager.LookAndFeelInfo getSelectedLaf()
{
return ((MyLookAndFeelInfo)jlLafs.getSelectedValue()).info;
}
public Theme getSelectedIconTheme()
{
return (Theme)iconThemesList.getSelectedValue();
}
public Theme getSelectedTheme()
{
return (Theme)jlThemes.getSelectedValue();
}
public String getTitle()
{
return XNap.tr("Look & Feel");
}
public void updateTheme()
{
// save current theme
Object[] savedDefaults = ThemeManager.getDefaults();
if (getSelectedTheme() != null) {
// set theme
ThemeManager.setTheme(getSelectedTheme());
SwingUtilities.updateComponentTreeUI(jpPreview);
// reset theme
ThemeManager.setDefaults(savedDefaults);
// enable configuration dialog
acConfigureTheme.setEnabled(getSelectedTheme().isConfigurable());
}
}
/**
* Sets the selected look and feel for the preview panel.
*/
public void valueChanged(ListSelectionEvent event)
{
if (event.getSource() == jlLafs) {
String currentLaf = UIManager.getLookAndFeel().getClass().getName();
// set laf
try {
UIManager.setLookAndFeel(getSelectedLaf().getClassName());
}
catch (Exception e) {
JOptionPane.showMessageDialog
(this, XNap.tr("Sorry, not supported or not installed"),
"Look and Feel", JOptionPane.ERROR_MESSAGE);
int i = jlLafs.getSelectedIndex();
jlLafs.setSelectedIndex((i == dlmLafs.size() - 1) ? 0 : i + 1);
return;
}
// redraw component
SwingUtilities.updateComponentTreeUI(jpPreview);
// reset laf
lastIndex = jlLafs.getSelectedIndex();
try {
UIManager.setLookAndFeel(currentLaf);
}
catch (Exception e) {
}
}
else if (event.getSource() == jlThemes) {
updateTheme();
}
}
/**
* Wraps toString() for UIManager.LookAndFeelInfo class.
*/
class MyLookAndFeelInfo {
public UIManager.LookAndFeelInfo info;
public MyLookAndFeelInfo(UIManager.LookAndFeelInfo info)
{
this.info = info;
}
public String getClassName()
{
return info.getClassName();
}
public String toString()
{
return info.getName();
}
}
private class MyTableModel extends AbstractColumnTableModel {
//--- Data field(s) ---
private Object[][] data = {
{ "Linux" , "GPL" },
{ "FreeBSD", "BSD License" },
};
//--- Constructor(s) ---
public MyTableModel()
{
Column columns[] = new Column[] {
new Column("Operating System", String.class),
new Column("License", String.class),
};
addColumns(columns);
fireTableDataChanged();
}
//--- Method(s) ---
public Object get(int i, int j)
{
return data[i][j];
}
public int getRowCount()
{
return data.length;
}
}
private class ConfigureThemeAction extends AbstractAction
{
public ConfigureThemeAction()
{
putValue(Action.NAME, XNap.tr("Configure Theme") + "...");
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Opens the theme configuration dialog."));
putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_C));
}
public void actionPerformed(ActionEvent event)
{
if (getSelectedTheme() != null
&& getSelectedTheme().showConfigurationDialog
(LookAndFeelPrefsPanel.this)) {
updateTheme();
}
}
}
}
The table below shows all metrics for LookAndFeelPrefsPanel.java.



