Commit | Line | Data |
---|---|---|
6ce136da JL |
1 | /* |
2 | GNU Lesser General Public License | |
3 | ||
4 | FileDialog | |
5 | Copyright (C) 2000 Howard Kistler & other contributors | |
6 | ||
7 | This library is free software; you can redistribute it and/or | |
8 | modify it under the terms of the GNU Lesser General Public | |
9 | License as published by the Free Software Foundation; either | |
10 | version 2.1 of the License, or (at your option) any later version. | |
11 | ||
12 | This library is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public | |
18 | License along with this library; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
22 | package com.hexidec.ekit.component; | |
23 | ||
24 | import com.hexidec.ekit.EkitCore; | |
25 | import java.awt.event.ActionListener; | |
26 | import java.awt.Container; | |
27 | import javax.swing.border.*; | |
28 | import javax.swing.BorderFactory; | |
29 | import javax.swing.BoxLayout; | |
30 | import javax.swing.event.ListSelectionListener; | |
31 | import javax.swing.event.ListSelectionEvent; | |
32 | import javax.swing.JButton; | |
33 | import javax.swing.JDialog; | |
34 | import javax.swing.JList; | |
35 | import javax.swing.JPanel; | |
36 | import javax.swing.JScrollPane; | |
37 | import javax.swing.ListSelectionModel; | |
38 | import javax.swing.SwingConstants; | |
39 | import javax.swing.WindowConstants; | |
40 | ||
41 | public 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 | } |