📄 mypreferencepanel.java
字号:
/* * MyPreferencePanel.java * * Created on December 31, 2005, 7:33 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package net.jxta.myjxta.util.preferences;import info.clearthought.layout.TableLayout;import java.awt.CardLayout;import java.awt.Dimension;import java.awt.Font;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Hashtable;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.MissingResourceException;import java.util.ResourceBundle;import javax.swing.Box;import javax.swing.BoxLayout;import javax.swing.ButtonGroup;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JScrollPane;import javax.swing.JTabbedPane;import net.jxta.myjxta.util.Resources;import java.util.logging.Logger;import org.jdesktop.swingx.JXPanel;import org.jdesktop.swingx.JXTitledPanel;/** * * @author polo */public class MyPreferencePanel extends JXPanel{ private static final Logger LOG = Logger.getLogger(MyPreferencePanel.class.getName()); private static final ResourceBundle STRINGS = Resources.getStrings(); private static final String SEP = MyPreferenceManager.SEP; private static final String ATTRIBUTE_NAME = MyPreferenceManager.ATTRIBUTE_NAME; private static final String ATTRIBUTE_LABEL = MyPreferenceManager.ATTRIBUTE_LABEL; public static final String ATTRIBUTE_ICON = MyPreferenceManager.ATTRIBUTE_ICON; public static final String ATTRIBUTE_TOOLTIP = MyPreferenceManager.ATTRIBUTE_VALUE; private ButtonGroup categoryButtonGroup = null; private JXPanel categoryPanel = null; private JXPanel content = null; private JXPanel prefSetPanel = null; private CardLayout categoryCards = null; private LinkedHashMap<String, JPanel> categoryPanelTable = null; private LinkedHashMap<String, JTabbedPane> categoryTabsTable = null; private LinkedHashMap prefSetObjectTable = null; private MyPreferenceManager prefMan = null; private Resources res = null; /** * Creates a new instance of MyPreferencePanel */ public MyPreferencePanel() { super(); prefMan = MyPreferenceManager.getInstance(); res = Resources.getInstance(); categoryPanelTable = new LinkedHashMap<String, JPanel>(); categoryTabsTable = new LinkedHashMap<String, JTabbedPane>(); prefSetObjectTable = new LinkedHashMap(); buildUI(); } public Dimension getPanelSize() { String dim = prefMan.getAttribute(MyPreferenceManager.ROOT + SEP + MyPreferenceManager.PREFERENCES , MyPreferenceManager.ATTRIBUTE_DIMENSION); return MyPreferenceManager.stringToDimension(dim); } private void buildUI() { double[][] mysizes = {{TableLayout.FILL},{TableLayout.FILL}}; TableLayout mytl = new TableLayout(mysizes); this.setLayout(mytl); JButton b = new JButton(); Font font = b.getFont(); float size = font.getSize(); Font newFont = font.deriveFont(size+10); final MyPreferenceManager instance = MyPreferenceManager.getInstance(); final String attribute = instance.getAttribute("/"+MyPreferenceManager.ROOT+MyPreferenceManager.SEP+MyPreferenceManager.PREFERENCES, MyPreferenceManager.ATTRIBUTE_LABEL); String title = STRINGS.getString(attribute); JXTitledPanel titledPanel = new JXTitledPanel(title); titledPanel.setTitleFont(newFont); categoryCards = new CardLayout(); prefSetPanel = new JXPanel(categoryCards); double[][] pssizes = {{TableLayout.FILL},{TableLayout.FILL}}; TableLayout pstl = new TableLayout(pssizes); JXPanel cardsPanel = new JXPanel();//pstl); cardsPanel.add(prefSetPanel,"0,0"); double[][] sizes = {{TableLayout.PREFERRED,TableLayout.FILL},{TableLayout.FILL}}; TableLayout tl = new TableLayout(sizes); content = new JXPanel(); content.add(buildCategoryPanel(), "0,0"); content.add(cardsPanel, "1,0" ); titledPanel.add(content); titledPanel.addRightDecoration(buildButtonPanel()); JLabel iconLabel = new JLabel(); iconLabel.setIcon(getIcon(prefMan.getAttribute(MyPreferenceManager.ROOT+MyPreferenceManager.SEP+MyPreferenceManager.PREFERENCES,MyPreferenceManager.ATTRIBUTE_ICON), 24,24)); titledPanel.addLeftDecoration(iconLabel); this.add(titledPanel,"0,0"); } private JComponent buildButtonPanel() { JButton applyButton = new JButton("Apply"); applyButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { apply(); } }); JButton closeButton = new JButton("Close"); closeButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { close(); } }); JPanel buttonPanel = new JPanel(); buttonPanel.setOpaque(false); buttonPanel.add(applyButton); buttonPanel.add(closeButton); return buttonPanel; } private void apply() { MyPreferenceManager.getInstance().save(); //todo apply changes from currently visible tab } private void close() { this.getRootPane().getParent().setVisible(false); } private void setupCategoryPanel() { Iterator itr = prefMan.getCategorys().iterator(); while(itr.hasNext()) { String categoryPath = (String) itr.next(); String title = null; try { title = STRINGS.getString(prefMan.getAttribute(categoryPath, ATTRIBUTE_LABEL)); }catch(MissingResourceException x) { title = prefMan.getAttribute(categoryPath, ATTRIBUTE_LABEL); } JXTitledPanel panel = new JXTitledPanel(title); JButton b = new JButton(); Font font = b.getFont(); float size = font.getSize(); Font newFont = font.deriveFont(size+4); panel.setTitleFont(newFont); double[][] sizes = {{TableLayout.FILL},{TableLayout.FILL}}; TableLayout tl = new TableLayout(sizes); JPanel p = new JPanel(tl); p.add(panel,"0,0"); prefSetPanel.add(p, categoryPath); if(!categoryPanelTable.keySet().contains(categoryPath)) { categoryPanelTable.put(categoryPath, p); JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT); panel.add(tab); categoryTabsTable.put(categoryPath, tab); } } } private void setupPreferenceSetsPanel(String categoryPath) { JTabbedPane tabs = categoryTabsTable.get(categoryPath);// JXTitledPanel catPanel =(JXTitledPanel) categoryPanelTable.get(categoryPath); Hashtable prefTable = prefMan.getPreferenceSets(categoryPath); Iterator itr = prefTable.keySet().iterator(); while(itr.hasNext()) { String prefSetPath = (String) itr.next(); if(Boolean.valueOf(prefMan.getAttribute(prefSetPath, MyPreferenceManager.ATTRIBUTE_VISIBLE)) == true) { String label = null; try{ label = STRINGS.getString(prefMan.getAttribute(prefSetPath,ATTRIBUTE_LABEL)); }catch(MissingResourceException x) { label = prefMan.getAttribute(prefSetPath,ATTRIBUTE_LABEL); } if(label == null) { label = prefMan.getAttribute(prefSetPath,ATTRIBUTE_NAME); } Icon icon = getIcon(prefSetPath, 24,24); String toolTip = prefMan.getAttribute(prefSetPath,ATTRIBUTE_TOOLTIP); if(Boolean.valueOf(prefMan.getAttribute(prefSetPath, MyPreferenceManager.ATTRIBUTE_ENABLE)) == false) { JXPanel disabledPanel = new JXPanel(); disabledPanel.setDrawGradient(true); JLabel disabledLabel = new JLabel("Disabled Feature Set"); Font disabledFont = disabledLabel.getFont(); disabledLabel.setFont(disabledFont.deriveFont(disabledFont.getSize()+10)); disabledPanel.add(disabledLabel); tabs.addTab(label, icon, disabledPanel, toolTip); }else{ tabs.addTab(label, icon, ((PreferenceSet)prefTable.get(prefSetPath)).getUI(), toolTip); } } } } private Icon getIcon(String xpath, int x, int y) { String iconResourceName = prefMan.getAttribute(xpath, ATTRIBUTE_ICON); if(iconResourceName == null) { iconResourceName = MyPreferenceManager.DEFAULT_ICON; } ImageIcon icon = res.getIconResource(iconResourceName); if(icon == null) { icon = res.getIconResource(MyPreferenceManager.DEFAULT_ICON); } return new ImageIcon(icon.getImage().getScaledInstance(x,y,Image.SCALE_REPLICATE)); } private String getToolTipText(String xpath) { String toolTip = prefMan.getAttribute(xpath, ATTRIBUTE_TOOLTIP); return toolTip; } private void categoryChosen(String categoryPath) { categoryCards.show(prefSetPanel, categoryPath); } private JXPanel buildCategoryPanel() { setupCategoryPanel(); categoryPanel = new JXPanel(true); categoryPanel.setDrawGradient(true); //categoryPanel.setAlpha(.54f); JScrollPane categoryScroller = new JScrollPane(categoryPanel); categoryScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); categoryButtonGroup = new ButtonGroup(); BoxLayout bl = new BoxLayout(categoryPanel, BoxLayout.Y_AXIS); categoryPanel.setLayout(bl); Iterator<String> keys = categoryPanelTable.keySet().iterator(); while(keys.hasNext()) { String path = keys.next(); if(Boolean.valueOf(prefMan.getAttribute(path, MyPreferenceManager.ATTRIBUTE_VISIBLE)) == true) { final boolean enabled = Boolean.valueOf(prefMan.getAttribute(path, MyPreferenceManager.ATTRIBUTE_ENABLE)); String label = null; try { label = STRINGS.getString(prefMan.getAttribute(path, ATTRIBUTE_LABEL)); }catch(MissingResourceException x) { label = prefMan.getAttribute(path, ATTRIBUTE_LABEL); } JRadioButton button = new JRadioButton(label); if(!enabled) { button.setText(button.getText() + " (DISABLED)"); button.setEnabled(enabled); } button.setIcon(getIcon(path, 48, 48)); button.setToolTipText(getToolTipText(path)); button.setActionCommand(path); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if(enabled) { categoryChosen(ae.getActionCommand()); } } }); categoryPanel.add(Box.createRigidArea(new Dimension(0, 40))); categoryButtonGroup.add(button); categoryPanel.add(button);//,categoryPanel.getComponentCount()-1); if(enabled) { setupPreferenceSetsPanel(path); } categoryPanel.add(Box.createRigidArea(new Dimension(0, 40))); } } categoryPanel.add(Box.createRigidArea(new Dimension(0, 40))); JXPanel p = new JXPanel(); p.add(categoryScroller); return p; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -