Path: blob/master/src/demo/share/jfc/SwingSet2/ListDemo.java
41152 views
/*1*2* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233import javax.swing.*;34import javax.swing.event.*;35import javax.swing.text.*;36import javax.swing.border.*;37import javax.swing.colorchooser.*;38import javax.swing.filechooser.*;39import javax.accessibility.*;4041import java.awt.*;42import java.awt.event.*;43import java.beans.*;44import java.util.*;45import java.io.*;46import java.applet.*;47import java.net.*;4849/**50* List Demo. This demo shows that it is not51* always necessary to have an array of objects52* as big as the size of the list stored.53*54* Indeed, in this example, there is no array55* kept for the list data, rather it is generated56* on the fly as only those elements are needed.57*58* @author Jeff Dinkins59*/60public class ListDemo extends DemoModule {61JList<String> list;6263JPanel prefixList;64JPanel suffixList;6566Action prefixAction;67Action suffixAction;6869GeneratedListModel listModel;7071Vector<JCheckBox> checkboxes = new Vector<>();7273/**74* main method allows us to run as a standalone demo.75*/76public static void main(String[] args) {77ListDemo demo = new ListDemo(null);78demo.mainImpl();79}8081/**82* ListDemo Constructor83*/84public ListDemo(SwingSet2 swingset) {85super(swingset, "ListDemo", "toolbar/JList.gif");8687loadImages();8889JLabel description = new JLabel(getString("ListDemo.description"));90getDemoPanel().add(description, BorderLayout.NORTH);9192JPanel centerPanel = new JPanel();93centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS));94centerPanel.add(Box.createRigidArea(HGAP10));95getDemoPanel().add(centerPanel, BorderLayout.CENTER);9697JPanel listPanel = new JPanel();98listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS));99listPanel.add(Box.createRigidArea(VGAP10));100101centerPanel.add(listPanel);102centerPanel.add(Box.createRigidArea(HGAP30));103104// Create the list105list = new JList<>();106list.setCellRenderer(new CompanyLogoListCellRenderer());107listModel = new GeneratedListModel(this);108list.setModel(listModel);109110// Set the preferred row count. This affects the preferredSize111// of the JList when it's in a scrollpane.112list.setVisibleRowCount(22);113114// Add list to a scrollpane115JScrollPane scrollPane = new JScrollPane(list);116listPanel.add(scrollPane);117listPanel.add(Box.createRigidArea(VGAP10));118119// Add the control panel (holds the prefix/suffix list and prefix/suffix checkboxes)120centerPanel.add(createControlPanel());121122// create prefixes and suffixes123addPrefix("Tera", true);124addPrefix("Micro", false);125addPrefix("Southern", false);126addPrefix("Net", true);127addPrefix("YoYo", true);128addPrefix("Northern", false);129addPrefix("Tele", false);130addPrefix("Eastern", false);131addPrefix("Neo", false);132addPrefix("Digi", false);133addPrefix("National", false);134addPrefix("Compu", true);135addPrefix("Meta", true);136addPrefix("Info", false);137addPrefix("Western", false);138addPrefix("Data", false);139addPrefix("Atlantic", false);140addPrefix("Advanced", false);141addPrefix("Euro", false);142addPrefix("Pacific", false);143addPrefix("Mobile", false);144addPrefix("In", false);145addPrefix("Computa", false);146addPrefix("Digital", false);147addPrefix("Analog", false);148149addSuffix("Tech", true);150addSuffix("Soft", true);151addSuffix("Telecom", true);152addSuffix("Solutions", false);153addSuffix("Works", true);154addSuffix("Dyne", false);155addSuffix("Services", false);156addSuffix("Vers", false);157addSuffix("Devices", false);158addSuffix("Software", false);159addSuffix("Serv", false);160addSuffix("Systems", true);161addSuffix("Dynamics", true);162addSuffix("Net", false);163addSuffix("Sys", false);164addSuffix("Computing", false);165addSuffix("Scape", false);166addSuffix("Com", false);167addSuffix("Ware", false);168addSuffix("Widgets", false);169addSuffix("Media", false);170addSuffix("Computer", false);171addSuffix("Hardware", false);172addSuffix("Gizmos", false);173addSuffix("Concepts", false);174}175176void updateDragEnabled(boolean dragEnabled) {177list.setDragEnabled(dragEnabled);178}179180public JPanel createControlPanel() {181JPanel controlPanel = new JPanel() {182Insets insets = new Insets(0, 4, 10, 10);183public Insets getInsets() {184return insets;185}186};187controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS));188189JPanel prefixPanel = new JPanel();190prefixPanel.setLayout(new BoxLayout(prefixPanel, BoxLayout.Y_AXIS));191prefixPanel.add(new JLabel(getString("ListDemo.prefixes")));192193JPanel suffixPanel = new JPanel();194suffixPanel.setLayout(new BoxLayout(suffixPanel, BoxLayout.Y_AXIS));195suffixPanel.add(new JLabel(getString("ListDemo.suffixes")));196197prefixList = new JPanel() {198Insets insets = new Insets(0, 4, 0, 0);199public Insets getInsets() {200return insets;201}202};203prefixList.setLayout(new BoxLayout(prefixList, BoxLayout.Y_AXIS));204JScrollPane scrollPane = new JScrollPane(prefixList);205scrollPane.getVerticalScrollBar().setUnitIncrement(10);206prefixPanel.add(scrollPane);207prefixPanel.add(Box.createRigidArea(HGAP10));208209suffixList = new JPanel() {210Insets insets = new Insets(0, 4, 0, 0);211public Insets getInsets() {212return insets;213}214};215suffixList.setLayout(new BoxLayout(suffixList, BoxLayout.Y_AXIS));216scrollPane = new JScrollPane(suffixList);217scrollPane.getVerticalScrollBar().setUnitIncrement(10);218suffixPanel.add(scrollPane);219suffixPanel.add(Box.createRigidArea(HGAP10));220221controlPanel.add(prefixPanel);222controlPanel.add(Box.createRigidArea(HGAP15));223controlPanel.add(suffixPanel);224return controlPanel;225}226227private FocusListener listFocusListener = new FocusAdapter() {228public void focusGained(FocusEvent e) {229JComponent c = (JComponent)e.getComponent();230c.scrollRectToVisible(new Rectangle(0, 0, c.getWidth(), c.getHeight()));231}232};233234public void addPrefix(String prefix, boolean selected) {235if(prefixAction == null) {236prefixAction = new UpdatePrefixListAction(listModel);237}238final JCheckBox cb = (JCheckBox) prefixList.add(new JCheckBox(prefix));239checkboxes.addElement(cb);240cb.setSelected(selected);241cb.addActionListener(prefixAction);242if(selected) {243listModel.addPrefix(prefix);244}245cb.addFocusListener(listFocusListener);246}247248public void addSuffix(String suffix, boolean selected) {249if(suffixAction == null) {250suffixAction = new UpdateSuffixListAction(listModel);251}252final JCheckBox cb = (JCheckBox) suffixList.add(new JCheckBox(suffix));253checkboxes.addElement(cb);254cb.setSelected(selected);255cb.addActionListener(suffixAction);256if(selected) {257listModel.addSuffix(suffix);258}259cb.addFocusListener(listFocusListener);260}261262class UpdatePrefixListAction extends AbstractAction {263GeneratedListModel listModel;264protected UpdatePrefixListAction(GeneratedListModel listModel) {265this.listModel = listModel;266}267268public void actionPerformed(ActionEvent e) {269JCheckBox cb = (JCheckBox) e.getSource();270if(cb.isSelected()) {271listModel.addPrefix(cb.getText());272} else {273listModel.removePrefix(cb.getText());274}275}276}277278class UpdateSuffixListAction extends AbstractAction {279GeneratedListModel listModel;280protected UpdateSuffixListAction(GeneratedListModel listModel) {281this.listModel = listModel;282}283284public void actionPerformed(ActionEvent e) {285JCheckBox cb = (JCheckBox) e.getSource();286if(cb.isSelected()) {287listModel.addSuffix(cb.getText());288} else {289listModel.removeSuffix(cb.getText());290}291}292}293294295class GeneratedListModel extends AbstractListModel<String> {296ListDemo demo;297Permuter permuter;298299public Vector<String> prefix = new Vector<>();300public Vector<String> suffix = new Vector<>();301302public GeneratedListModel (ListDemo demo) {303this.demo = demo;304}305306private void update() {307permuter = new Permuter(getSize());308fireContentsChanged(this, 0, getSize());309}310311public void addPrefix(String s) {312if(!prefix.contains(s)) {313prefix.addElement(s);314update();315}316}317318public void removePrefix(String s) {319prefix.removeElement(s);320update();321}322323public void addSuffix(String s) {324if(!suffix.contains(s)) {325suffix.addElement(s);326update();327}328}329330public void removeSuffix(String s) {331suffix.removeElement(s);332update();333}334335public int getSize() {336return prefix.size() * suffix.size();337}338339public String getElementAt(int index) {340if(permuter == null) {341update();342}343// morph the index to another int -- this has the benefit of344// causing the list to look random.345int j = permuter.map(index);346int ps = prefix.size();347int ss = suffix.size();348return (String) prefix.elementAt(j%ps) + (String) suffix.elementAt((j/ps)%ss);349}350}351352ImageIcon[] images = new ImageIcon[7];353void loadImages() {354images[0] = createImageIcon("list/red.gif", getString("ListDemo.red"));355images[1] = createImageIcon("list/blue.gif", getString("ListDemo.blue"));356images[2] = createImageIcon("list/yellow.gif", getString("ListDemo.yellow"));357images[3] = createImageIcon("list/green.gif", getString("ListDemo.green"));358images[4] = createImageIcon("list/gray.gif", getString("ListDemo.gray"));359images[5] = createImageIcon("list/cyan.gif", getString("ListDemo.cyan"));360images[6] = createImageIcon("list/magenta.gif", getString("ListDemo.magenta"));361}362363class CompanyLogoListCellRenderer extends DefaultListCellRenderer {364public Component getListCellRendererComponent(365JList<?> list,366Object value,367int index,368boolean isSelected,369boolean cellHasFocus)370{371Component retValue = super.getListCellRendererComponent(372list, value, index, isSelected, cellHasFocus373);374setIcon(images[index%7]);375return retValue;376}377}378}379380381