KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > util > StringSelector


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
31
32 /**
33  * Provides chooser from list of strings.
34  *
35  * @author Maros Sandor
36  */

37 public class StringSelector extends javax.swing.JPanel JavaDoc {
38
39     public static String JavaDoc select(String JavaDoc title, String JavaDoc prompt, List<String JavaDoc> 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 JavaDoc dialog = DialogDisplayer.getDefault().createDialog(descriptor);
50         dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(StringSelector.class, "ACSD_StringSelectorDialog")); // NOI18N
51
dialog.setVisible(true);
52         if (descriptor.getValue() != DialogDescriptor.OK_OPTION) return null;
53         
54         return (String JavaDoc) panel.listValues.getSelectedValue();
55     }
56
57     private List<String JavaDoc> choices;
58     
59     private void setChoices(List<String JavaDoc> strings) {
60         choices = strings;
61         
62         listValues.setModel(new AbstractListModel() {
63             public int getSize() {
64                 return choices.size();
65             }
66
67             public Object JavaDoc getElementAt(int index) {
68                 return choices.get(index);
69             }
70         });
71     }
72
73     /** Creates new form StringSelector */
74     public StringSelector() {
75         initComponents();
76     }
77     
78     /** This method is called from within the constructor to
79      * initialize the form.
80      * WARNING: Do NOT modify this code. The content of this method is
81      * always regenerated by the Form Editor.
82      */

83     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
84
private void initComponents() {
85
86         promptLabel = new javax.swing.JLabel JavaDoc();
87         jScrollPane = new javax.swing.JScrollPane JavaDoc();
88         listValues = new javax.swing.JList JavaDoc();
89
90         promptLabel.setLabelFor(listValues);
91         promptLabel.setText(org.openide.util.NbBundle.getMessage(StringSelector.class, "StringSelector.promptLabel.text")); // NOI18N
92

93         listValues.setModel(new javax.swing.AbstractListModel JavaDoc() {
94             String JavaDoc[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
95             public int getSize() { return strings.length; }
96             public Object JavaDoc 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     }// </editor-fold>//GEN-END:initComponents
123

124     
125     // Variables declaration - do not modify//GEN-BEGIN:variables
126
private javax.swing.JScrollPane JavaDoc jScrollPane;
127     private javax.swing.JList JavaDoc listValues;
128     private javax.swing.JLabel JavaDoc promptLabel;
129     // End of variables declaration//GEN-END:variables
130
}
131
Popular Tags