001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.demo;
018:
019: import com.l2fprod.common.swing.JDirectoryChooser;
020: import com.l2fprod.common.swing.LookAndFeelTweaks;
021: import com.l2fprod.common.swing.PercentLayout;
022: import com.l2fprod.common.util.ResourceManager;
023:
024: import java.awt.Component;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.io.File;
028:
029: import javax.swing.JButton;
030: import javax.swing.JOptionPane;
031: import javax.swing.JPanel;
032: import javax.swing.JTextArea;
033: import javax.swing.UIManager;
034:
035: /**
036: * A simple example showing how to use the JDirectoryChooser.
037: */
038: public class ChooseDirectory extends JPanel {
039:
040: public final static ResourceManager RESOURCE = ResourceManager
041: .get(ChooseDirectory.class);
042:
043: public ChooseDirectory() {
044: setLayout(new PercentLayout(PercentLayout.VERTICAL, 3));
045:
046: if (System.getProperty("javawebstart.version") != null) {
047: JTextArea area = new JTextArea(RESOURCE
048: .getString("message.webstart"));
049: LookAndFeelTweaks.makeMultilineLabel(area);
050: area.setFocusable(false);
051: add(area);
052: }
053:
054: final JButton button = new JButton(RESOURCE
055: .getString("selectDirectory"));
056: add(button);
057: button.addActionListener(new ActionListener() {
058: public void actionPerformed(ActionEvent e) {
059: selectDirectory(button, null);
060: }
061: });
062: }
063:
064: static void selectDirectory(Component parent, String selectedFile) {
065: JDirectoryChooser chooser;
066:
067: if (System.getProperty("javawebstart.version") != null) {
068: chooser = new JDirectoryChooser(new FakeFileSystemView()) {
069: public void rescanCurrentDirectory() {
070: }
071:
072: public void setCurrentDirectory(File dir) {
073: }
074: };
075: chooser.setShowingCreateDirectory(false);
076: } else {
077: chooser = new JDirectoryChooser();
078: if (selectedFile != null) {
079: chooser.setSelectedFile(new File(selectedFile));
080: }
081: }
082:
083: JTextArea accessory = new JTextArea(RESOURCE
084: .getString("selectDirectory.message"));
085: accessory.setLineWrap(true);
086: accessory.setWrapStyleWord(true);
087: accessory.setEditable(false);
088: accessory.setOpaque(false);
089: accessory.setFont(UIManager.getFont("Tree.font"));
090: accessory.setFocusable(false);
091: chooser.setAccessory(accessory);
092:
093: chooser.setMultiSelectionEnabled(true);
094:
095: int choice = chooser.showOpenDialog(parent);
096: if (choice == JDirectoryChooser.APPROVE_OPTION) {
097: String filenames = "";
098: File[] selectedFiles = chooser.getSelectedFiles();
099: for (int i = 0, c = selectedFiles.length; i < c; i++) {
100: filenames += "\n" + selectedFiles[i];
101: }
102: JOptionPane.showMessageDialog(parent, RESOURCE.getString(
103: "selectDirectory.confirm",
104: new Object[] { filenames }));
105: } else {
106: JOptionPane.showMessageDialog(parent, RESOURCE
107: .getString("selectDirectory.cancel"));
108: }
109: }
110:
111: public static void main(String[] args) throws Exception {
112: UIManager.setLookAndFeel(UIManager
113: .getSystemLookAndFeelClassName());
114:
115: if (args.length > 0) {
116: selectDirectory(null, args[0]);
117: } else {
118: selectDirectory(null, null);
119: }
120:
121: System.exit(0);
122: }
123:
124: }
|