001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.openfile;
043:
044: import java.io.File;
045: import java.awt.BorderLayout;
046: import java.awt.Component;
047: import java.awt.Dimension;
048: import java.util.List;
049: import javax.swing.event.ListSelectionEvent;
050: import javax.swing.event.ListSelectionListener;
051: import javax.swing.Icon;
052: import javax.swing.ImageIcon;
053: import javax.swing.JButton;
054: import javax.swing.JLabel;
055: import javax.swing.JList;
056: import javax.swing.JTextField;
057: import javax.swing.JPanel;
058: import javax.swing.JScrollPane;
059: import javax.swing.JTextArea;
060: import javax.swing.ListCellRenderer;
061: import javax.swing.ListSelectionModel;
062: import java.awt.event.*;
063: import org.openide.util.NbBundle;
064:
065: // XXX This entire class should be refactored using form.
066: /**
067: * Panel offering mounting points to user, when opening .java file.
068: */
069: public class PackagePanel extends JPanel {
070:
071: private File f;
072:
073: private int pkgLevel;
074:
075: private List dirs;
076:
077: private List pkgs;
078:
079: /** Creates new form PackagePanel */
080: public PackagePanel(File f, int pkgLevel, List dirs, List pkgs) {
081: this .f = f;
082: this .pkgLevel = pkgLevel;
083: this .dirs = dirs;
084: this .pkgs = pkgs;
085:
086: initComponents2();
087:
088: initAccessibility();
089: }
090:
091: JButton getOKButton() {
092: return okButton;
093: }
094:
095: JButton getCancelButton() {
096: return cancelButton;
097: }
098:
099: JList getList() {
100: return list;
101: }
102:
103: /** */
104: private void initComponents2() {
105: okButton = new JButton(NbBundle.getMessage(PackagePanel.class,
106: "LBL_okButton"));
107: cancelButton = new JButton(NbBundle.getMessage(
108: PackagePanel.class, "LBL_cancelButton"));
109: list = new JList(pkgs.toArray());
110:
111: setLayout(new BorderLayout(0, 5));
112: setBorder(new javax.swing.border.EmptyBorder(8, 8, 8, 8));
113:
114: textArea = new JTextArea();
115: //textArea.setBackground (new Color(204, 204, 204));
116: textArea.setDisabledTextColor(javax.swing.UIManager
117: .getColor("Label.foreground"));
118: //textArea.setFont (new Font ("SansSerif", Font.PLAIN, 11)); // NOI18N
119: textArea.setFont(javax.swing.UIManager.getFont("Label.font"));
120: textArea.setText(NbBundle.getMessage(PackagePanel.class,
121: pkgLevel == -1 ? "TXT_whereMountNoSuggest"
122: : "TXT_whereMountSuggest", f.getName()));
123: textArea.setEditable(false);
124: textArea.setEnabled(false);
125: textArea.setOpaque(false);
126: textArea.setLineWrap(true);
127: textArea.setWrapStyleWord(true);
128: add(textArea, BorderLayout.NORTH);
129:
130: list.setVisibleRowCount(5);
131: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
132: if (pkgLevel != -1)
133: list.setSelectedIndex(pkgLevel);
134: list.setCellRenderer(new ListCellRenderer() {
135: private Icon folderIcon = new ImageIcon(OpenFile.class
136: .getResource("folder.gif")); // NOI18N
137: private Icon rootFolderIcon = new ImageIcon(OpenFile.class
138: .getResource("rootFolder.gif")); // NOI18N
139: private final JLabel lab = new JLabel();
140:
141: public Component getListCellRendererComponent(JList lst,
142: Object value, int index, boolean isSelected,
143: boolean cellHasFocus) {
144: String pkg2 = (String) value;
145: if (pkg2.equals("")) { // NOI18N
146: lab.setText(NbBundle.getMessage(PackagePanel.class,
147: "LBL_packageWillBeDefault"));
148: lab.setIcon(rootFolderIcon);
149: } else {
150: lab.setText(NbBundle.getMessage(PackagePanel.class,
151: "LBL_packageWillBe", pkg2));
152: lab.setIcon(folderIcon);
153: }
154: if (isSelected) {
155: lab.setBackground(lst.getSelectionBackground());
156: lab.setForeground(lst.getSelectionForeground());
157: } else {
158: lab.setBackground(lst.getBackground());
159: lab.setForeground(lst.getForeground());
160: }
161: lab.setEnabled(lst.isEnabled());
162: lab.setFont(lst.getFont());
163: lab.setOpaque(true);
164: return lab;
165: }
166: });
167: add(new JScrollPane(list), BorderLayout.CENTER);
168:
169: // Name of mount point:
170: //final JLabel label = new JLabel ();
171: final JTextField field = new JTextField();
172: //label.setFont (new Font ("Monospaced", Font.PLAIN, 12)); // NOI18N
173: //add (label, BorderLayout.SOUTH);
174: field.setEditable(false);
175: field.setEnabled(true);
176: //Accessibility
177: field.getAccessibleContext().setAccessibleDescription(
178: NbBundle.getMessage(PackagePanel.class, "ACS_Field"));
179: field.selectAll();
180: field.addFocusListener(new FocusListener() {
181: public void focusGained(FocusEvent e) {
182: field.selectAll();
183: }
184:
185: public void focusLost(java.awt.event.FocusEvent e) {
186: }
187: });
188: add(field, BorderLayout.SOUTH);
189:
190: setPreferredSize(new Dimension(450, 300));
191:
192: list.addListSelectionListener(new ListSelectionListener() {
193: public void valueChanged(ListSelectionEvent ev) {
194: updateLabelEtcFromList(field, list, dirs, okButton);
195: }
196: });
197: updateLabelEtcFromList(field, list, dirs, okButton);
198: }
199:
200: private void initAccessibility() {
201: this .getAccessibleContext().setAccessibleDescription(
202: textArea.getText());
203: okButton.getAccessibleContext().setAccessibleDescription(
204: NbBundle.getMessage(PackagePanel.class,
205: "ACS_LBL_okButton"));
206: cancelButton.getAccessibleContext().setAccessibleDescription(
207: NbBundle.getMessage(PackagePanel.class,
208: "ACS_LBL_cancelButton"));
209: list.getAccessibleContext().setAccessibleName(
210: NbBundle.getMessage(PackagePanel.class, "ACSN_List"));
211: list.getAccessibleContext().setAccessibleDescription(
212: NbBundle.getMessage(PackagePanel.class, "ACSD_List"));
213: }
214:
215: /** Updates label and enables/disables ok button. */
216: private static void updateLabelEtcFromList(JTextField field,
217: JList list, List dirs, JButton okButton) {
218: int idx = list.getSelectedIndex();
219: if (idx == -1) {
220: field.setText(" "); // NOI18N
221: field.getAccessibleContext().setAccessibleName(" ");
222: okButton.setEnabled(false);
223: } else {
224: File dir = (File) dirs.get(idx);
225: field.setText(NbBundle.getMessage(PackagePanel.class,
226: "LBL_dirWillBe", dir.getAbsolutePath()));
227: field.getAccessibleContext().setAccessibleName(
228: NbBundle.getMessage(PackagePanel.class,
229: "LBL_dirWillBe", dir.getAbsolutePath()));
230: okButton.setEnabled(true);
231: }
232: }
233:
234: /** This method is called from within the constructor to
235: * initialize the form.
236: * WARNING: Do NOT modify this code. The content of this method is
237: * always regenerated by the Form Editor.
238: */
239: private void initComponents() {//GEN-BEGIN:initComponents
240:
241: setLayout(new java.awt.BorderLayout());
242:
243: }//GEN-END:initComponents
244:
245: private JButton okButton;
246: private JButton cancelButton;
247: private JList list;
248: private JTextArea textArea;
249: // Variables declaration - do not modify//GEN-BEGIN:variables
250: // End of variables declaration//GEN-END:variables
251:
252: }
|