KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openfile > PackagePanel


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.openfile;
22
23
24 import java.io.File JavaDoc;
25 import java.awt.BorderLayout JavaDoc;
26 import java.awt.Component JavaDoc;
27 import java.awt.Dimension JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.swing.event.ListSelectionEvent JavaDoc;
30 import javax.swing.event.ListSelectionListener JavaDoc;
31 import javax.swing.Icon JavaDoc;
32 import javax.swing.ImageIcon JavaDoc;
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JLabel JavaDoc;
35 import javax.swing.JList JavaDoc;
36 import javax.swing.JTextField JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38 import javax.swing.JScrollPane JavaDoc;
39 import javax.swing.JTextArea JavaDoc;
40 import javax.swing.ListCellRenderer JavaDoc;
41 import javax.swing.ListSelectionModel JavaDoc;
42 import java.awt.event.*;
43 import org.openide.util.NbBundle;
44
45
46 // XXX This entire class should be refactored using form.
47
/**
48  * Panel offering mounting points to user, when opening .java file.
49  */

50 public class PackagePanel extends JPanel JavaDoc {
51
52     private File JavaDoc f;
53     
54     private int pkgLevel;
55     
56     private List JavaDoc dirs;
57     
58     private List JavaDoc pkgs;
59
60     
61     /** Creates new form PackagePanel */
62     public PackagePanel(File JavaDoc f, int pkgLevel, List JavaDoc dirs, List JavaDoc pkgs) {
63         this.f = f;
64         this.pkgLevel = pkgLevel;
65         this.dirs = dirs;
66         this.pkgs = pkgs;
67         
68         initComponents2();
69         
70         initAccessibility();
71     }
72     
73     
74     JButton JavaDoc getOKButton() {
75         return okButton;
76     }
77     
78     JButton JavaDoc getCancelButton() {
79         return cancelButton;
80     }
81     
82     JList JavaDoc getList() {
83         return list;
84     }
85
86     /** */
87     private void initComponents2() {
88         okButton = new JButton JavaDoc (NbBundle.getMessage(PackagePanel.class, "LBL_okButton"));
89         cancelButton = new JButton JavaDoc (NbBundle.getMessage(PackagePanel.class, "LBL_cancelButton"));
90         list = new JList JavaDoc(pkgs.toArray());
91         
92         setLayout (new BorderLayout JavaDoc (0, 5));
93         setBorder (new javax.swing.border.EmptyBorder JavaDoc (8, 8, 8, 8));
94
95         textArea = new JTextArea JavaDoc ();
96         //textArea.setBackground (new Color(204, 204, 204));
97
textArea.setDisabledTextColor (javax.swing.UIManager.getColor ("Label.foreground"));
98         //textArea.setFont (new Font ("SansSerif", Font.PLAIN, 11)); // NOI18N
99
textArea.setFont (javax.swing.UIManager.getFont ("Label.font"));
100         textArea.setText (NbBundle.getMessage(PackagePanel.class, pkgLevel == -1 ? "TXT_whereMountNoSuggest" : "TXT_whereMountSuggest", f.getName ()));
101         textArea.setEditable (false);
102         textArea.setEnabled (false);
103         textArea.setOpaque (false);
104         textArea.setLineWrap (true);
105         textArea.setWrapStyleWord (true);
106         add (textArea, BorderLayout.NORTH);
107         
108         list.setVisibleRowCount (5);
109         list.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
110         if (pkgLevel != -1) list.setSelectedIndex (pkgLevel);
111         list.setCellRenderer (new ListCellRenderer JavaDoc () {
112             private Icon JavaDoc folderIcon = new ImageIcon JavaDoc (OpenFile.class.getResource ("folder.gif")); // NOI18N
113
private Icon JavaDoc rootFolderIcon = new ImageIcon JavaDoc (OpenFile.class.getResource ("rootFolder.gif")); // NOI18N
114
private final JLabel JavaDoc lab = new JLabel JavaDoc();
115             
116             public Component JavaDoc getListCellRendererComponent (JList JavaDoc lst, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
117                 String JavaDoc pkg2 = (String JavaDoc) value;
118                 if (pkg2.equals ("")) { // NOI18N
119
lab.setText (NbBundle.getMessage(PackagePanel.class, "LBL_packageWillBeDefault"));
120                     lab.setIcon (rootFolderIcon);
121                 } else {
122                     lab.setText (NbBundle.getMessage(PackagePanel.class, "LBL_packageWillBe", pkg2));
123                     lab.setIcon (folderIcon);
124                 }
125                 if (isSelected) {
126                     lab.setBackground (lst.getSelectionBackground ());
127                     lab.setForeground (lst.getSelectionForeground ());
128                 } else {
129                     lab.setBackground (lst.getBackground ());
130                     lab.setForeground (lst.getForeground ());
131                 }
132                 lab.setEnabled (lst.isEnabled ());
133                 lab.setFont (lst.getFont ());
134                 lab.setOpaque (true);
135                 return lab;
136             }
137         });
138         add (new JScrollPane JavaDoc (list), BorderLayout.CENTER);
139
140         // Name of mount point:
141
//final JLabel label = new JLabel ();
142
final JTextField JavaDoc field = new JTextField JavaDoc ();
143         //label.setFont (new Font ("Monospaced", Font.PLAIN, 12)); // NOI18N
144
//add (label, BorderLayout.SOUTH);
145
field.setEditable(false);
146         field.setEnabled(true);
147     //Accessibility
148
field.getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage(PackagePanel.class, "ACS_Field"));
149         field.selectAll();
150         field.addFocusListener(new FocusListener() {
151             public void focusGained(FocusEvent e) {
152                 field.selectAll();
153             }
154             
155             public void focusLost(java.awt.event.FocusEvent JavaDoc e){
156             }
157     });
158         add (field, BorderLayout.SOUTH);
159                 
160         setPreferredSize (new Dimension JavaDoc (450, 300));
161
162         list.addListSelectionListener (new ListSelectionListener JavaDoc () {
163                                            public void valueChanged (ListSelectionEvent JavaDoc ev) {
164                                                updateLabelEtcFromList (field, list, dirs, okButton);
165                                            }
166                                        });
167         updateLabelEtcFromList (field, list, dirs, okButton);
168     }
169
170     private void initAccessibility() {
171         this.getAccessibleContext ().setAccessibleDescription (textArea.getText ());
172         okButton.getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage(PackagePanel.class, "ACS_LBL_okButton"));
173         cancelButton.getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage(PackagePanel.class, "ACS_LBL_cancelButton"));
174         list.getAccessibleContext().setAccessibleName(NbBundle.getMessage(PackagePanel.class, "ACSN_List"));
175         list.getAccessibleContext ().setAccessibleDescription (NbBundle.getMessage(PackagePanel.class, "ACSD_List"));
176     }
177         
178     /** Updates label and enables/disables ok button. */
179     private static void updateLabelEtcFromList (JTextField JavaDoc field, JList JavaDoc list, List JavaDoc dirs, JButton JavaDoc okButton) {
180         int idx = list.getSelectedIndex ();
181         if (idx == -1) {
182             field.setText (" "); // NOI18N
183
field.getAccessibleContext().setAccessibleName(" ");
184             okButton.setEnabled (false);
185         } else {
186             File JavaDoc dir = (File JavaDoc) dirs.get(idx);
187             field.setText (NbBundle.getMessage(PackagePanel.class, "LBL_dirWillBe", dir.getAbsolutePath ()));
188             field.getAccessibleContext().setAccessibleName(NbBundle.getMessage(PackagePanel.class, "LBL_dirWillBe", dir.getAbsolutePath ()));
189             okButton.setEnabled (true);
190         }
191     }
192
193     
194     /** This method is called from within the constructor to
195      * initialize the form.
196      * WARNING: Do NOT modify this code. The content of this method is
197      * always regenerated by the Form Editor.
198      */

199     private void initComponents() {//GEN-BEGIN:initComponents
200

201         setLayout(new java.awt.BorderLayout JavaDoc());
202
203     }//GEN-END:initComponents
204

205
206     private JButton JavaDoc okButton;
207     private JButton JavaDoc cancelButton;
208     private JList JavaDoc list;
209     private JTextArea JavaDoc textArea;
210     // Variables declaration - do not modify//GEN-BEGIN:variables
211
// End of variables declaration//GEN-END:variables
212

213 }
214
Popular Tags