1 19 20 package org.netbeans.modules.versioning.util; 21 22 import org.openide.util.HelpCtx; 23 import org.openide.util.NbBundle; 24 import org.openide.DialogDescriptor; 25 import org.openide.DialogDisplayer; 26 import org.openide.awt.Mnemonics; 27 28 import javax.swing.*; 29 import java.util.*; 30 import java.awt.Dialog ; 31 32 37 public class StringSelector extends javax.swing.JPanel { 38 39 public static String select(String title, String prompt, List<String > strings) { 40 StringSelector panel = new StringSelector(); 41 Mnemonics.setLocalizedText(panel.promptLabel, prompt); 42 panel.listValues.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 43 panel.setChoices(strings); 44 45 DialogDescriptor descriptor = new DialogDescriptor(panel, title); 46 descriptor.setClosingOptions(null); 47 descriptor.setHelpCtx(new HelpCtx(StringSelector.class)); 48 49 Dialog dialog = DialogDisplayer.getDefault().createDialog(descriptor); 50 dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(StringSelector.class, "ACSD_StringSelectorDialog")); dialog.setVisible(true); 52 if (descriptor.getValue() != DialogDescriptor.OK_OPTION) return null; 53 54 return (String ) panel.listValues.getSelectedValue(); 55 } 56 57 private List<String > choices; 58 59 private void setChoices(List<String > strings) { 60 choices = strings; 61 62 listValues.setModel(new AbstractListModel() { 63 public int getSize() { 64 return choices.size(); 65 } 66 67 public Object getElementAt(int index) { 68 return choices.get(index); 69 } 70 }); 71 } 72 73 74 public StringSelector() { 75 initComponents(); 76 } 77 78 83 private void initComponents() { 85 86 promptLabel = new javax.swing.JLabel (); 87 jScrollPane = new javax.swing.JScrollPane (); 88 listValues = new javax.swing.JList (); 89 90 promptLabel.setLabelFor(listValues); 91 promptLabel.setText(org.openide.util.NbBundle.getMessage(StringSelector.class, "StringSelector.promptLabel.text")); 93 listValues.setModel(new javax.swing.AbstractListModel () { 94 String [] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; 95 public int getSize() { return strings.length; } 96 public Object getElementAt(int i) { return strings[i]; } 97 }); 98 jScrollPane.setViewportView(listValues); 99 100 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 101 this.setLayout(layout); 102 layout.setHorizontalGroup( 103 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 104 .add(layout.createSequentialGroup() 105 .addContainerGap() 106 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 107 .add(layout.createSequentialGroup() 108 .add(jScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 633, Short.MAX_VALUE) 109 .addContainerGap()) 110 .add(layout.createSequentialGroup() 111 .add(promptLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 547, Short.MAX_VALUE) 112 .add(96, 96, 96)))) 113 ); 114 layout.setVerticalGroup( 115 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 116 .add(layout.createSequentialGroup() 117 .addContainerGap() 118 .add(promptLabel) 119 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 120 .add(jScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)) 121 ); 122 } 124 125 private javax.swing.JScrollPane jScrollPane; 127 private javax.swing.JList listValues; 128 private javax.swing.JLabel promptLabel; 129 } 131 | Popular Tags |