Import of Ekit 0.9h
[old-projects.git] / ekit / com / hexidec / ekit / component / FileDialog.java
CommitLineData
6ce136da
JL
1/*
2GNU Lesser General Public License
3
4FileDialog
5Copyright (C) 2000 Howard Kistler & other contributors
6
7This library is free software; you can redistribute it and/or
8modify it under the terms of the GNU Lesser General Public
9License as published by the Free Software Foundation; either
10version 2.1 of the License, or (at your option) any later version.
11
12This library is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public
18License along with this library; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20*/
21
22package com.hexidec.ekit.component;
23
24import com.hexidec.ekit.EkitCore;
25import java.awt.event.ActionListener;
26import java.awt.Container;
27import javax.swing.border.*;
28import javax.swing.BorderFactory;
29import javax.swing.BoxLayout;
30import javax.swing.event.ListSelectionListener;
31import javax.swing.event.ListSelectionEvent;
32import javax.swing.JButton;
33import javax.swing.JDialog;
34import javax.swing.JList;
35import javax.swing.JPanel;
36import javax.swing.JScrollPane;
37import javax.swing.ListSelectionModel;
38import javax.swing.SwingConstants;
39import javax.swing.WindowConstants;
40
41public class FileDialog extends JDialog implements ActionListener
42{
43 private EkitCore ParentEkit;
44 private JList FileList;
45 private String FileDir;
46 private String[]Files;
47 private String SelectedFile;
48
49 public FileDialog(EkitCore parentEkit, String fileDir, String[] fileList, String title, boolean modal)
50 {
51 super(parentEkit.getFrame(), title, modal);
52 FileDir = fileDir;
53 Files = fileList;
54 ParentEkit = parentEkit;
55 init();
56 }
57
58 public void actionPerformed(java.awt.event.ActionEvent e)
59 {
60 if(e.getActionCommand().equals("save"))
61 {
62 hide();
63 }
64 else if(e.getActionCommand().equals("cancel"))
65 {
66 SelectedFile = null;
67 hide();
68 }
69 }
70
71 public void init()
72 {
73 SelectedFile = "";
74 Container contentPane = getContentPane();
75 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
76 setBounds(100,100,300,200);
77 setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
78
79 FileList = new JList(Files);
80 FileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
81 FileList.clearSelection();
82 ListSelectionModel lsm = FileList.getSelectionModel();
83
84 lsm.addListSelectionListener(new ListSelectionListener()
85 {
86 public void valueChanged(ListSelectionEvent e)
87 {
88 if(!e.getValueIsAdjusting())
89 {
90 ListSelectionModel sm = FileList.getSelectionModel();
91 if(!sm.isSelectionEmpty())
92 {
93 SelectedFile = Files[sm.getMinSelectionIndex()];
94 }
95 }
96 }
97 });
98
99 JScrollPane fileScrollPane = new JScrollPane(FileList);
100 fileScrollPane.setAlignmentX(LEFT_ALIGNMENT);
101 JPanel centerPanel = new JPanel();
102 centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS));
103 centerPanel.add(fileScrollPane);
104 centerPanel.setBorder(BorderFactory.createTitledBorder("Files"));
105
106 JPanel buttonPanel= new JPanel();
107 buttonPanel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
108
109 JButton saveButton = new JButton("Accept");
110 saveButton.setActionCommand("save");
111 saveButton.addActionListener(this);
112 JButton cancelButton = new JButton("Cancel");
113 cancelButton.setActionCommand("cancel");
114 cancelButton.addActionListener(this);
115
116 buttonPanel.add(saveButton);
117 buttonPanel.add(cancelButton);
118
119 contentPane.add(centerPanel);
120 contentPane.add(buttonPanel);
121 setVisible(true);
122 }
123
124 public String getSelectedFile()
125 {
126 if(SelectedFile != null)
127 {
128 SelectedFile = FileDir + "/" + SelectedFile;
129 }
130 return SelectedFile;
131 }
132
133}