File: FileChooser.java

package info (click to toggle)
libjdic-java 0.9.5-2
  • links: PTS, VCS
  • area: contrib
  • in suites: lenny
  • size: 2,704 kB
  • ctags: 2,027
  • sloc: java: 8,666; xml: 3,843; cpp: 3,435; ansic: 653; sh: 318; makefile: 213
file content (370 lines) | stat: -rwxr-xr-x 13,087 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
 * subject to license terms.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.List;
import java.util.ArrayList;

import org.jdesktop.jdic.desktop.*;


/**
 * JDIC API demo main class.
 * <p>
 * Class <code>FileChooser</code> creates a UI interface demonstrating the 
 * usage of the public API of <code>org.jdesktop.jdic.desktop.*</code> classes.
 */
public class FileChooser extends JPanel {
    JPanel fileAndUrlPanel = new JPanel();
    JFileChooser jFileChooser = new JFileChooser();
    JPanel urlPanel = new JPanel();  

    JPanel jVerbPanel = new JPanel(); 
    JPanel jFileButtonPanel = new JPanel();
    JPanel jUrlButtonPanel = new JPanel(); 
    JButton jMailButton = new JButton();
    JButton jPrintButton = new JButton();
    JButton jEditButton = new JButton();
    JButton jOpenButton = new JButton();
    JButton jBrowseButton = new JButton();

    // The two spaces("  ") seperate the url label and the text field.
    JLabel jUrlLabel = new JLabel("URL:  ");
    JTextField jUrlTextField = new JTextField();
  
    File selectedFile;
  
    public FileChooser() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {}

        JFrame frame = new JFrame("JDIC API Demo - FileChooser");

        Container contentPane = frame.getContentPane();

        contentPane.setLayout(new GridLayout(1, 1));
        contentPane.add(new FileChooser());

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        frame.pack();
        frame.setVisible(true);
    }

    private void jbInit() throws Exception {
        jFileChooser.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 0));
        jFileChooser.setControlButtonsAreShown(false);
        jFileChooser.addPropertyChangeListener(new FileChooser_jFileChooser_propertyChangeAdapter(this));
        jUrlTextField.addActionListener(new FileChooser_jUrlTextField_actionAdapter(this));
        jMailButton.setEnabled(true);
        jMailButton.setText("Mail to ...");
        jMailButton.addActionListener(new FileChooser_jCreateMailButton_actionAdapter(this));
        jPrintButton.setEnabled(false);
        jPrintButton.setText("Print");
        jPrintButton.addActionListener(new FileChooser_jPrintButton_actionAdapter(this));
        jEditButton.setEnabled(false);
        jEditButton.setText("Edit");
        jEditButton.addActionListener(new FileChooser_jEditButton_actionAdapter(this));
        jOpenButton.setEnabled(false);
        jOpenButton.setText("Open");
        jOpenButton.addActionListener(new FileChooser_jOpenButton_actionAdapter(this));
        jBrowseButton.setText("Browse");
        jBrowseButton.setToolTipText("Browse the given URL with system default browser");
        jBrowseButton.addActionListener(new FileChooser_jBrowseButton_actionAdapter(this));

        jFileButtonPanel.setLayout(new GridLayout(8, 1, 10, 10));
        // Here, it aligns the top of jFileButtonPanel with the top of the file list 
        // box in the jFileChooser.
        jFileButtonPanel.setBorder(BorderFactory.createEmptyBorder(42, 10, 0, 10));
        jFileButtonPanel.add(jOpenButton, null);
        jFileButtonPanel.add(jEditButton, null);
        jFileButtonPanel.add(jPrintButton, null);
        // jFileButtonPanel.add(new JSeparator());
        jFileButtonPanel.add(jMailButton, null);
        jUrlButtonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        jUrlButtonPanel.setLayout(new GridLayout());
        jUrlButtonPanel.add(jBrowseButton, null);     
        jVerbPanel.setLayout(new BorderLayout());
        jVerbPanel.add(jFileButtonPanel, BorderLayout.CENTER);
        jVerbPanel.add(jUrlButtonPanel, BorderLayout.SOUTH);

        urlPanel.setBorder(BorderFactory.createEmptyBorder(20, 10, 11, 0));
        urlPanel.setLayout(new BorderLayout());
        urlPanel.add(jUrlLabel, BorderLayout.WEST);
        urlPanel.add(jUrlTextField, BorderLayout.CENTER);

        fileAndUrlPanel.setLayout(new BorderLayout());
        fileAndUrlPanel.add(jFileChooser, BorderLayout.CENTER);
        fileAndUrlPanel.add(urlPanel, BorderLayout.SOUTH);

        // JSeparator jSeparator = new JSeparator();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        this.setPreferredSize(new Dimension(screenSize.width * 6 / 10,
                screenSize.height * 5 / 10));
        this.setLayout(new BorderLayout());
        this.add(fileAndUrlPanel, BorderLayout.CENTER);
        // this.add(jSeparator, BorderLayout.CENTER);
        this.add(jVerbPanel, BorderLayout.EAST);
    }

    void jOpenButton_actionPerformed(ActionEvent e) {
        try {
            Desktop.open(selectedFile);
        } catch (DesktopException de) {
            JOptionPane.showMessageDialog(this, "Exception: " + de.toString(),
                    "Error message", JOptionPane.ERROR_MESSAGE);
        }
    }

    void jBrowseButton_actionPerformed(ActionEvent e) {
        String inputUrl = jUrlTextField.getText();
        if (inputUrl == null) {
            return;
        }

        try {
            Desktop.browse(new URL(inputUrl));
        } catch (MalformedURLException de) {
            JOptionPane.showMessageDialog(this, "Invalid URL: " + inputUrl,
                    "Exception", JOptionPane.ERROR_MESSAGE); 
        } catch (DesktopException de) {
            JOptionPane.showMessageDialog(this, de.toString(), "Exception",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    void jEditButton_actionPerformed(ActionEvent e) {
        try {
            Desktop.edit(selectedFile);
        } catch (DesktopException de) {
            JOptionPane.showMessageDialog(this, de.toString(), "Exception",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    void jPrintButton_actionPerformed(ActionEvent e) {
        try {
            Desktop.print(selectedFile);
        } catch (DesktopException de) {
            JOptionPane.showMessageDialog(this, de.toString(), "Exception",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    void jCreateMailButton_actionPerformed(ActionEvent e) {
        if (selectedFile == null) {
            // No selected file as attachment, just launch the system mailer.
            try {
                Desktop.mail();
            } catch (DesktopException de) {
                JOptionPane.showMessageDialog(this, de.toString(), "Exception",
                        JOptionPane.ERROR_MESSAGE);
            }
        } else {
            // Has selected file as attachment, construct a message specifing the 
            // attachment and launch the system mailer.

            Message msg = new Message();

            List attachList = new ArrayList();

            attachList.add(selectedFile.toString());
            // attachList.add(selectedFile);
            try {
                msg.setAttachments(attachList);
            } catch (IOException ioe) {
                JOptionPane.showMessageDialog(this, ioe.toString(), "Exception",
                        JOptionPane.ERROR_MESSAGE);
            }
                  
            try {
                Desktop.mail(msg);
            } catch (DesktopException de) {
                JOptionPane.showMessageDialog(this, de.toString(), "Exception",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    void jFileChooser_propertyChange(PropertyChangeEvent e) {
    	if(JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(e.getPropertyName())){
            jOpenButton.setEnabled(false);
            jEditButton.setEnabled(false);
            jPrintButton.setEnabled(false);
    	}else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e.getPropertyName())) {
            // The selected file should always be the same as newFile
            selectedFile = jFileChooser.getSelectedFile();
            if (selectedFile != null) {
                // A file is selected, enable button "Open".
                jOpenButton.setEnabled(true);
                // To check that whether "Edit" and "Print" buttons should be enabled.
                if (Desktop.isEditable(selectedFile)) {
                    jEditButton.setEnabled(true);
                } else {
                    jEditButton.setEnabled(false);
                }
                if (Desktop.isPrintable(selectedFile)) {
                    jPrintButton.setEnabled(true);
                } else {
                    jPrintButton.setEnabled(false);
                }

            } else {
                // A directory is selected, then disable buttons
                // "Open", "Edit" and "Print".
                jOpenButton.setEnabled(false);
                jEditButton.setEnabled(false);
                jPrintButton.setEnabled(false);
            }
        }
    }
    
    void jUrlTextField_actionPerformed(ActionEvent e) {
        String inputUrl = jUrlTextField.getText();
        if (inputUrl == null) {
            return;
        }

        try {
            Desktop.browse(new URL(inputUrl));
        } catch (MalformedURLException de) {
            JOptionPane.showMessageDialog(this, "Invalid URL: " + inputUrl,
                    "Exception", JOptionPane.ERROR_MESSAGE); 
        } catch (DesktopException de) {
            JOptionPane.showMessageDialog(this, de.toString(), "Exception",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}


class FileChooser_jOpenButton_actionAdapter implements java.awt.event.ActionListener {
    FileChooser adaptee;

    FileChooser_jOpenButton_actionAdapter(FileChooser adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jOpenButton_actionPerformed(e);
    }
}


class FileChooser_jBrowseButton_actionAdapter implements java.awt.event.ActionListener {
    FileChooser adaptee;

    FileChooser_jBrowseButton_actionAdapter(FileChooser adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jBrowseButton_actionPerformed(e);
    }
}


class FileChooser_jEditButton_actionAdapter implements java.awt.event.ActionListener {
    FileChooser adaptee;

    FileChooser_jEditButton_actionAdapter(FileChooser adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jEditButton_actionPerformed(e);
    }
}


class FileChooser_jPrintButton_actionAdapter implements java.awt.event.ActionListener {
    FileChooser adaptee;

    FileChooser_jPrintButton_actionAdapter(FileChooser adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jPrintButton_actionPerformed(e);
    }
}


class FileChooser_jCreateMailButton_actionAdapter implements java.awt.event.ActionListener {
    FileChooser adaptee;

    FileChooser_jCreateMailButton_actionAdapter(FileChooser adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jCreateMailButton_actionPerformed(e);
    }
}


class FileChooser_jFileChooser_propertyChangeAdapter implements java.beans.PropertyChangeListener {
    FileChooser adaptee;

    FileChooser_jFileChooser_propertyChangeAdapter(FileChooser adaptee) {
        this.adaptee = adaptee;
    }

    public void propertyChange(PropertyChangeEvent e) {
        adaptee.jFileChooser_propertyChange(e);
    }
}

class FileChooser_jUrlTextField_actionAdapter implements java.awt.event.ActionListener {
    FileChooser adaptee;

    FileChooser_jUrlTextField_actionAdapter(FileChooser adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jUrlTextField_actionPerformed(e);
    }
}