001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.installer;
021:
022: import com.installshield.product.i18n.ProductResourcesConst;
023: import com.installshield.util.LocalizedStringResolver;
024: import com.installshield.util.Log;
025: import com.installshield.util.MnemonicString;
026: import com.installshield.wizard.WizardBeanEvent;
027: import com.installshield.wizard.WizardBuilderSupport;
028: import com.installshield.wizard.swing.JFlowLabel;
029: import com.installshield.wizard.swing.SwingWizardPanelImpl;
030: import com.installshield.wizard.swing.SwingWizardUI;
031: import com.installshield.wizardx.i18n.WizardXResourcesConst;
032:
033: import java.awt.BorderLayout;
034: import java.awt.Dimension;
035: import java.awt.GridBagConstraints;
036: import java.awt.GridBagLayout;
037: import java.awt.Insets;
038: import java.awt.event.ActionEvent;
039: import java.awt.event.ActionListener;
040: import javax.swing.JButton;
041: import javax.swing.JFileChooser;
042: import javax.swing.JLabel;
043: import javax.swing.JList;
044: import javax.swing.JPanel;
045: import javax.swing.JScrollPane;
046: import javax.swing.JTextField;
047: import javax.swing.ListSelectionModel;
048: import javax.swing.SwingConstants;
049: import javax.swing.event.ListSelectionEvent;
050: import javax.swing.event.ListSelectionListener;
051: import javax.swing.plaf.FileChooserUI;
052: import javax.swing.plaf.basic.BasicFileChooserUI;
053:
054: public class DirectoryChooserPanelSwingImpl extends
055: SwingWizardPanelImpl implements ActionListener,
056: ListSelectionListener {
057: private JTextField inputTextField;
058: private JButton browseButton;
059: private JList listList;
060:
061: private JFlowLabel infoLabel;
062: private JPanel inputPanel;
063:
064: public void build(WizardBuilderSupport support) {
065: super .build(support);
066: try {
067: support.putClass(Util.class.getName());
068: } catch (Exception exception) {
069: support.logEvent(this , Log.ERROR, exception);
070: }
071: }
072:
073: public void initialize(WizardBeanEvent event) {
074: super .initialize(event);
075:
076: setLayout(new BorderLayout(0, 20));
077:
078: infoLabel = new JFlowLabel();
079:
080: String description = getPanel().getDescription();
081: infoLabel.setText(resolveString(description));
082:
083: add(infoLabel, BorderLayout.NORTH);
084:
085: JPanel panel = new JPanel();
086: panel.setLayout(new BorderLayout(0, 20));
087: add(panel, BorderLayout.CENTER);
088:
089: inputPanel = new JPanel();
090: inputPanel.setLayout(new GridBagLayout());
091:
092: panel.add(inputPanel, BorderLayout.NORTH);
093:
094: JPanel inputLabelPanel = new JPanel();
095: inputLabelPanel.setLayout(new BorderLayout());
096:
097: JLabel inputLabel = new JLabel();
098:
099: MnemonicString inputLabelMn = new MnemonicString(
100: resolveString(getDirectoryChooserPanel()
101: .getDestinationCaption()));
102: inputLabel.setText(inputLabelMn.toString());
103: if (inputLabelMn.isMnemonicSpecified()) {
104: inputLabel.setDisplayedMnemonic(inputLabelMn
105: .getMnemonicChar());
106: }
107: inputLabelPanel.add(inputLabel);
108:
109: GridBagConstraints gbc = new GridBagConstraints();
110: gbc.insets = new Insets(15, 0, 3, 0);
111: gbc.gridx = 0;
112: gbc.gridy = 0;
113: gbc.weightx = 0.5;
114: gbc.gridwidth = GridBagConstraints.REMAINDER;
115: gbc.anchor = GridBagConstraints.SOUTHWEST;
116: inputPanel.add(inputLabelPanel, gbc);
117:
118: // input entry
119: inputTextField = new JTextField();
120: gbc = new GridBagConstraints();
121: gbc.insets = new Insets(0, 0, 3, 0);
122: gbc.gridx = 0;
123: gbc.gridy = 1;
124: gbc.weightx = 0.5;
125: gbc.gridheight = 1;
126: gbc.gridwidth = 1;
127: gbc.anchor = GridBagConstraints.WEST;
128: gbc.fill = GridBagConstraints.HORIZONTAL;
129: inputLabel.setLabelFor(inputTextField);
130: inputPanel.add(inputTextField, gbc);
131:
132: inputTextField.getAccessibleContext().setAccessibleName(
133: resolveString(getDirectoryChooserPanel()
134: .getDescription()));
135:
136: browseButton = new JButton();
137: MnemonicString browseMn = new MnemonicString(
138: resolveString(getDirectoryChooserPanel()
139: .getBrowseCaption()));
140: browseButton.setText(browseMn.toString());
141: if (browseMn.isMnemonicSpecified()) {
142: browseButton.setMnemonic(browseMn.getMnemonicChar());
143: }
144:
145: gbc = new GridBagConstraints();
146: gbc.insets = new Insets(0, 12, 3, 0);
147: gbc.gridx = 0;
148: gbc.gridy = 2;
149: gbc.gridheight = 1;
150: gbc.gridwidth = 1;
151: gbc.anchor = GridBagConstraints.EAST;
152: inputPanel.add(browseButton, gbc);
153: browseButton.addActionListener(this );
154: browseButton.getAccessibleContext().setAccessibleName(
155: LocalizedStringResolver.resolve(
156: ProductResourcesConst.NAME,
157: "DestinationPanelSwingImpl.browse"));
158:
159: if (getDirectoryChooserPanel().getDestinations().size() != 0) {
160: listList = new JList(getDirectoryChooserPanel()
161: .getDestinationDescriptions());
162: listList.setSelectedIndex(Integer
163: .parseInt(getDirectoryChooserPanel()
164: .getSelectedDestinationIndex()));
165: listList
166: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
167: listList.addListSelectionListener(this );
168:
169: JScrollPane listScrollPane = new JScrollPane();
170: listScrollPane.setViewportView(listList);
171:
172: JPanel listPanel = new JPanel();
173: listPanel.setLayout(new BorderLayout(3, 3));
174:
175: JLabel listLabel = new JLabel();
176: listLabel.setHorizontalAlignment(SwingConstants.LEFT);
177:
178: MnemonicString listLabelMn = new MnemonicString(
179: resolveString(getDirectoryChooserPanel()
180: .getDestinationsCaption()));
181: listLabel.setText(listLabelMn.toString());
182: if (listLabelMn.isMnemonicSpecified()) {
183: listLabel.setDisplayedMnemonic(listLabelMn
184: .getMnemonicChar());
185: listLabel.setLabelFor(listList);
186: }
187:
188: listPanel.add(listLabel, java.awt.BorderLayout.NORTH);
189: listPanel.add(listScrollPane, BorderLayout.CENTER);
190:
191: panel.add(listPanel, BorderLayout.CENTER);
192:
193: inputTextField.setText(getDirectoryChooserPanel()
194: .getDestinationValue(
195: Integer.parseInt(getDirectoryChooserPanel()
196: .getSelectedDestinationIndex())));
197: }
198: }
199:
200: public void entered(WizardBeanEvent event) {
201: inputTextField.requestFocusInWindow();
202: }
203:
204: public void exiting(WizardBeanEvent evt) {
205: super .exiting(evt);
206: getDirectoryChooserPanel().setDestination(
207: inputTextField.getText());
208: }
209:
210: public void actionPerformed(ActionEvent event) {
211: SwingWizardUI wizardUI = (SwingWizardUI) getPanel().getWizard()
212: .getUI();
213: if (wizardUI != null) {
214: wizardUI.restoreDefaultColors();
215: }
216: JFileChooser fc = new JFileChooser() {
217: public boolean accept(java.io.File f) {
218: return f.isDirectory();
219:
220: }
221:
222: public void setCurrentDirectory(java.io.File f) {
223:
224: super .setCurrentDirectory(f);
225:
226: FileChooserUI ui = getUI();
227:
228: if (ui instanceof BasicFileChooserUI) {
229: ((BasicFileChooserUI) ui).setFileName("");
230: }
231: }
232: };
233:
234: fc.setDialogTitle(LocalizedStringResolver.resolve(
235: WizardXResourcesConst.NAME,
236: "DirectoryInputComponent.selectDirectory"));
237: fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
238: // remember pref size before displaying to restore after setting selected file (file name
239: // can cause dialog to stretch horizontally)
240: Dimension prefSize = fc.getPreferredSize();
241: fc.setSelectedFile(new java.io.File(inputTextField.getText()));
242: fc.setPreferredSize(prefSize);
243: if (fc.showOpenDialog(this ) == JFileChooser.APPROVE_OPTION) {
244: try {
245: inputTextField.setText(fc.getSelectedFile()
246: .getCanonicalPath());
247: } catch (java.io.IOException e) {
248: inputTextField.setText(fc.getSelectedFile().getPath());
249: }
250: inputTextField.requestFocus();
251: inputTextField.selectAll();
252: }
253: if (wizardUI != null) {
254: wizardUI.setWizardColors();
255: }
256: }
257:
258: public void valueChanged(ListSelectionEvent listSelectionEvent) {
259: if (listList.getSelectedIndex() != -1) {
260: inputTextField.setText(getDirectoryChooserPanel()
261: .getDestinationValue(listList.getSelectedIndex()));
262: inputTextField.selectAll();
263: } else {
264: inputTextField.setText("");
265: }
266: }
267:
268: private DirectoryChooserPanel getDirectoryChooserPanel() {
269: return (DirectoryChooserPanel) getPanel();
270: }
271: }
|