/* * $Id: SideBarDemo.java,v 1.6 2005/11/17 04:55:46 zfq Exp $ * * Copyright (C) 2001-2003 Extreme Component, Inc. All rights reserved. * Use is subject to license terms. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; import com.zfqjava.swing.JSideBar; import com.zfqjava.swing.ModelFactory; import com.zfqjava.swing.ValueAction; import com.zfqjava.swing.cell.IconProvider; import com.zfqjava.swing.cell.IconMode; import com.zfqjava.swing.JBean; import com.zfqjava.swing.JCommonPane; /** * JSideBar Demo * * @author $Author: zfq $ * @version $Revision: 1.6 $ $Date: 2005/11/17 04:55:46 $ */ public class SideBarDemo extends JPanel { private static final String[] A = { "Alice", "Bob", "Mike", "Scott" }; private static final String[] B = { "New", "Open", "Edit", "Print", "Close", "New", "Test", "View", "Help", "Run", "Build", "Compile"}; private static final String[] C = { "Date", "Time", "Object", "Boolena" }; private JSideBar sideBar; private Icon file16 = new ImageIcon(getClass().getResource("resources/File16.gif")); private Icon file32 = new ImageIcon(getClass().getResource("resources/File32.gif")); public SideBarDemo() { super(new BorderLayout()); sideBar = createSideBar(); add(sideBar, BorderLayout.CENTER); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(createPropertyPanel(), BorderLayout.NORTH); panel.add(createDialogPanel(), BorderLayout.SOUTH); add(panel, BorderLayout.EAST); } private JSideBar createSideBar() { JSideBar sideBar = new JSideBar(); sideBar.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // sideBar.setViewMode(JSideBar.SMALL_ICON_VIEW_MODE); sideBar.addGroup(ModelFactory.createListModel(A), new ValueAction("name")); sideBar.addGroup(ModelFactory.createListModel(B), new ValueAction("action")); sideBar.addGroup(ModelFactory.createListModel(C), new ValueAction("class")); sideBar.setCellProvider(new IconProvider() { private IconMode mode; public void setIconMode(IconMode mode) { this.mode = mode; } public Icon getIcon(Object value) { if(mode == IconMode.SMALL) { return file16; } else { return file32; } //return UIManager.getIcon("FileView.fileIcon"); } }); sideBar.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JSideBar sideBar = (JSideBar)e.getSource(); JOptionPane.showMessageDialog(null, sideBar.getSelectedItem(), "Selected", JOptionPane.INFORMATION_MESSAGE); } }); return sideBar; } private JPanel createPropertyPanel() { JPanel panel = new JPanel(); Border titledBorder = BorderFactory.createTitledBorder("Change Property"); Border emptyBorder = BorderFactory.createEmptyBorder(12, 12, 12, 12); panel.setBorder(BorderFactory.createCompoundBorder(titledBorder, emptyBorder)); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); panel.setLayout(gridbag); c.weightx = 0.5; c.weighty = 0.5; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.WEST; c.insets = new Insets(0, 0, 5, 0); JLabel label = new JLabel("View Mode: "); addPanel(panel, c, label); Object[] viewModes = { "Small Icon", "Large Icon", "List", "Thumbnails"}; final JComboBox viewModeCB = new JComboBox(viewModes); viewModeCB.setSelectedIndex(1); // default view mode viewModeCB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String mode = (String)viewModeCB.getSelectedItem(); if(mode.equals("Small Icon")) { sideBar.setViewMode(JSideBar.SMALL_ICON_VIEW_MODE); } else if(mode.equals("Large Icon")) { sideBar.setViewMode(JSideBar.LARGE_ICON_VIEW_MODE); } else if(mode.equals("List")) { sideBar.setViewMode(JSideBar.LIST_VIEW_MODE); } else if(mode.equals("Thumbnails")) { sideBar.setViewMode(JSideBar.THUMBNAILS_VIEW_MODE); } } }); label.setLabelFor(viewModeCB); c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, viewModeCB); label = new JLabel("Selection Mode: "); addPanel(panel, c, label); Object[] headStyles = { "Single Selection", "Multiple Selection" }; final JComboBox headStyleCB = new JComboBox(headStyles); headStyleCB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int index = headStyleCB.getSelectedIndex(); if(index == 0) { sideBar.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } else { sideBar.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); } } }); label.setLabelFor(headStyleCB); c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, headStyleCB); return panel; } private JPanel createDialogPanel() { JPanel panel = new JPanel(); Border titledBorder = BorderFactory.createTitledBorder("Dialog and Frame"); Border emptyBorder = BorderFactory.createEmptyBorder(12, 12, 12, 12); panel.setBorder(BorderFactory.createCompoundBorder(titledBorder, emptyBorder)); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); panel.setLayout(gridbag); // c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.CENTER; c.insets = new Insets(0, 0, 5, 0); c.gridwidth = GridBagConstraints.REMAINDER; JButton b = new JButton("Show Frame"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFrame frame = new JFrame("JSideBar"); frame.getContentPane().add(createSideBar()); frame.pack(); centerOnScreen(frame); frame.show(); } }); addPanel(panel, c, b); return panel; } private static void addPanel(Container container, GridBagConstraints c, Component component) { GridBagLayout gridbag = (GridBagLayout)container.getLayout(); gridbag.setConstraints(component, c); container.add(component); } public static void main(String[] args) { DemoPanel demoPanel = new DemoPanel(); demoPanel.addDemo(new SideBarDemo()); demoPanel.setTitle("JSideBar Demo"); demoPanel.setDefaultCloseOperation(JBean.EXIT_ON_CLOSE); Dimension size = demoPanel.getPreferredSize(); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); d.width = Math.min(d.width, size.width); d.height = Math.min(d.height, size.height); demoPanel.setPreferredSize(size); demoPanel.showFrame(); } private static void centerOnScreen(Window window) { Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); window.setLocation((d.width - window.getWidth())/2, (d.height - window.getHeight())/2); } }