KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > browser > config > classpath > ClasspathSetupDialog


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.browser.config.classpath;
9
10 import org.gjt.jclasslib.browser.BrowserMDIFrame;
11 import org.gjt.jclasslib.browser.config.BrowserConfig;
12 import org.gjt.jclasslib.mdi.BasicFileFilter;
13 import org.gjt.jclasslib.util.GUIHelper;
14
15 import javax.swing.*;
16 import javax.swing.event.ListSelectionEvent JavaDoc;
17 import javax.swing.event.ListSelectionListener JavaDoc;
18 import java.awt.*;
19 import java.awt.event.*;
20 import java.io.File JavaDoc;
21 import java.util.*;
22 import java.util.List JavaDoc;
23
24 /**
25     Dialog for viewing and modifying the classpath.
26
27     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
28     @version $Revision: 1.1 $ $Date: 2003/08/18 08:10:15 $
29 */

30 public class ClasspathSetupDialog extends JDialog
31                                   implements ActionListener, ListSelectionListener JavaDoc {
32
33     private static final int DIALOG_WIDTH = 500;
34     private static final int DIALOG_HEIGHT = 300;
35
36     private static final Dimension IMAGE_BUTTON_SIZE = new Dimension(28, 28);
37     private static final Icon ICON_ADD = BrowserMDIFrame.loadIcon("add.png");
38     private static final Icon ICON_REMOVE = BrowserMDIFrame.loadIcon("remove.png");
39     private static final Icon ICON_UP = BrowserMDIFrame.loadIcon("up.png");
40     private static final Icon ICON_DOWN = BrowserMDIFrame.loadIcon("down.png");
41
42     private BrowserMDIFrame frame;
43
44     private DefaultListModel listModel;
45
46     private JList lstElements;
47     private JScrollPane scpLstElements;
48     private JButton btnAdd;
49     private JButton btnRemove;
50     private JButton btnUp;
51     private JButton btnDown;
52
53     private JButton btnOk;
54     private JButton btnCancel;
55     private JFileChooser fileChooser;
56
57     /**
58      * Constructor.
59      * @param frame the parent frame.
60      */

61     public ClasspathSetupDialog(BrowserMDIFrame frame) {
62         super(frame);
63         this.frame = frame;
64         setupControls();
65         setupAccelerators();
66         setupComponent();
67         setupEventHandlers();
68         checkEnabledStatus();
69     }
70
71     public void valueChanged(ListSelectionEvent JavaDoc event) {
72         checkEnabledStatus();
73     }
74
75     public void actionPerformed(ActionEvent event) {
76         Object JavaDoc source = event.getSource();
77         if (source == btnAdd) {
78             doAdd();
79         } else if (source == btnRemove) {
80             doRemove();
81         } else if (source == btnUp) {
82             doUp();
83         } else if (source == btnDown) {
84             doDown();
85         } else if (source == btnCancel) {
86             doCancel();
87         } else if (source == btnOk) {
88             doOk();
89         }
90         checkEnabledStatus();
91     }
92
93     public void setVisible(boolean visible) {
94         if (visible) {
95             updateList();
96         }
97         super.setVisible(visible);
98     }
99
100     private void updateList() {
101
102         listModel.clear();
103         Iterator it = frame.getConfig().getClasspath().iterator();
104         while (it.hasNext()) {
105             listModel.addElement(it.next());
106         }
107     }
108
109     private void setupControls() {
110
111         listModel = new DefaultListModel();
112
113         lstElements = new JList(listModel);
114         lstElements.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
115         lstElements.setCellRenderer(new ClasspathCellRenderer());
116         scpLstElements = new JScrollPane(lstElements);
117         scpLstElements.setBorder(BorderFactory.createEtchedBorder());
118
119         btnAdd = new JButton(ICON_ADD);
120         btnAdd.setToolTipText("Add a classpath entry (INS)");
121         makeImageButton(btnAdd);
122         btnRemove = new JButton(ICON_REMOVE);
123         btnRemove.setToolTipText("Remove a classpath entry (DEL)");
124         makeImageButton(btnRemove);
125         btnUp = new JButton(ICON_UP);
126         btnUp.setToolTipText("Move a classpath entry up (ALT-UP)");
127         makeImageButton(btnUp);
128         btnDown = new JButton(ICON_DOWN);
129         btnDown.setToolTipText("Move a classpath entry down (ALT-DOWN)");
130         makeImageButton(btnDown);
131
132         btnOk = new JButton("Ok");
133         btnCancel = new JButton("Cancel");
134         btnOk.setPreferredSize(btnCancel.getPreferredSize());
135
136     }
137
138     private void setupComponent() {
139
140         Container contentPane = getContentPane();
141         contentPane.setLayout(new GridBagLayout());
142         GridBagConstraints gc = new GridBagConstraints();
143         gc.gridx = 0;
144         gc.gridy = 0;
145         gc.insets = new Insets(5, 5, 0, 5);
146         gc.weightx = 1;
147         gc.anchor = GridBagConstraints.NORTHWEST;
148         contentPane.add(new JLabel("Classpath:"), gc);
149         gc.gridy++;
150
151         gc.weighty = 1;
152         gc.insets.top = 0;
153         gc.fill = GridBagConstraints.BOTH;
154         contentPane.add(createListPanel(), gc);
155         gc.gridy++;
156         gc.fill = GridBagConstraints.HORIZONTAL;
157         gc.weighty = 0;
158         gc.insets.top = 3;
159         gc.insets.bottom = 5;
160         contentPane.add(createButtonBox(), gc);
161         getRootPane().setDefaultButton(btnOk);
162
163         setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
164         setModal(true);
165         setTitle("Setup classpath");
166         GUIHelper.centerOnParentWindow(this, getOwner());
167         setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
168
169     }
170
171     private void setupEventHandlers() {
172
173         btnCancel.addActionListener(this);
174         btnOk.addActionListener(this);
175
176         btnAdd.addActionListener(this);
177         btnRemove.addActionListener(this);
178         btnUp.addActionListener(this);
179         btnDown.addActionListener(this);
180
181         lstElements.addListSelectionListener(this);
182
183         addWindowListener(new WindowAdapter() {
184             public void windowClosing(WindowEvent event) {
185                 doCancel();
186             }
187         });
188     }
189
190     private void setupAccelerators() {
191
192         addAccelerator((JComponent)getContentPane(), KeyEvent.VK_ESCAPE, 0, new AbstractAction() {
193             public void actionPerformed(ActionEvent event) {
194                 doCancel();
195             }
196         });
197
198         addAccelerator(lstElements, KeyEvent.VK_INSERT, 0, new AbstractAction() {
199             public void actionPerformed(ActionEvent event) {
200                 doAdd();
201             }
202         });
203         addAccelerator(lstElements, KeyEvent.VK_DELETE, 0, new AbstractAction() {
204             public void actionPerformed(ActionEvent event) {
205                 doRemove();
206             }
207         });
208         addAccelerator(lstElements, KeyEvent.VK_UP, KeyEvent.ALT_MASK, new AbstractAction() {
209             public void actionPerformed(ActionEvent event) {
210                 doUp();
211             }
212         });
213         addAccelerator(lstElements, KeyEvent.VK_DOWN, KeyEvent.ALT_MASK, new AbstractAction() {
214             public void actionPerformed(ActionEvent event) {
215                 doDown();
216             }
217         });
218
219     }
220
221     private void addAccelerator(JComponent component, int keyCode, int keyMask, AbstractAction action) {
222
223         KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, keyMask);
224         Object JavaDoc key = new Object JavaDoc();
225         component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke, key);
226         component.getActionMap().put(key, action);
227     }
228
229     private JPanel createListPanel() {
230
231         JPanel panel = new JPanel();
232         panel.setLayout(new BorderLayout());
233
234         panel.add(scpLstElements, BorderLayout.CENTER);
235         panel.add(createModificationButtonBox(), BorderLayout.EAST);
236
237         return panel;
238     }
239
240     private Box createModificationButtonBox() {
241
242         Box box = Box.createVerticalBox();
243         box.add(btnAdd);
244         box.add(btnRemove);
245         box.add(Box.createVerticalGlue());
246         box.add(btnUp);
247         box.add(btnDown);
248         return box;
249     }
250
251     private Box createButtonBox() {
252
253         Box box = Box.createHorizontalBox();
254         box.add(Box.createHorizontalGlue());
255         box.add(btnOk);
256         box.add(btnCancel);
257
258         return box;
259     }
260
261     private void makeImageButton(AbstractButton button) {
262         button.setMinimumSize(IMAGE_BUTTON_SIZE);
263         button.setPreferredSize(IMAGE_BUTTON_SIZE);
264         button.setMaximumSize(IMAGE_BUTTON_SIZE);
265     }
266
267
268     private void doCancel() {
269         setVisible(false);
270     }
271
272     private void doOk() {
273
274         List JavaDoc newEntries = new ArrayList();
275         for (int i = 0; i < listModel.getSize(); i++) {
276             newEntries.add(listModel.getElementAt(i));
277         }
278         BrowserConfig config = frame.getConfig();
279         List JavaDoc oldEntries = new ArrayList(config.getClasspath());
280
281         Iterator itOld = oldEntries.iterator();
282         while (itOld.hasNext()) {
283             ClasspathEntry entry = (ClasspathEntry)itOld.next();
284             if (!newEntries.contains(entry)) {
285                 config.removeClasspathEntry(entry);
286             }
287         }
288         Iterator itNew = newEntries.iterator();
289         while (itNew.hasNext()) {
290             ClasspathEntry entry = (ClasspathEntry)itNew.next();
291             if (!oldEntries.contains(entry)) {
292                 config.addClasspathEntry(entry);
293             }
294         }
295
296         config.setClasspath(newEntries);
297         setVisible(false);
298     }
299
300     private void doAdd() {
301
302         if (fileChooser == null) {
303             fileChooser = new JFileChooser(frame.getClassesChooserPath());
304             fileChooser.setDialogTitle("Choose directory or jar file");
305             fileChooser.setFileFilter(new BasicFileFilter("jar", "jar files and directories"));
306             fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
307             fileChooser.setMultiSelectionEnabled(true);
308         }
309
310         if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
311             frame.setClassesChooserPath(fileChooser.getCurrentDirectory().getAbsolutePath());
312             File JavaDoc[] files = fileChooser.getSelectedFiles();
313             for (int i = 0; i < files.length; i++) {
314                 File JavaDoc file = files[i];
315                 ClasspathEntry entry;
316                 if (file.isDirectory()) {
317                     entry = new ClasspathDirectoryEntry();
318                     entry.setFileName(file.getPath());
319                 } else {
320                     entry = new ClasspathArchiveEntry();
321                     entry.setFileName(file.getPath());
322
323                 }
324                 if (!isInModel(entry)) {
325                     listModel.addElement(entry);
326                     selectIndex(listModel.getSize() - 1);
327                 }
328             }
329         }
330     }
331
332     private boolean isInModel(ClasspathEntry entry) {
333
334         for (int i = 0; i < listModel.getSize(); i++) {
335             if (listModel.getElementAt(i).equals(entry)) {
336                 return true;
337             }
338         }
339         return false;
340     }
341
342     private void doRemove() {
343         int selectedIndex = lstElements.getSelectedIndex();
344         if (selectedIndex > -1) {
345             listModel.remove(selectedIndex);
346             selectIndex(selectedIndex);
347         }
348     }
349
350     private void doUp() {
351         int selectedIndex = lstElements.getSelectedIndex();
352         if (selectedIndex > 0) {
353             Object JavaDoc entry = listModel.remove(selectedIndex);
354             int newSelectedIndex = selectedIndex - 1;
355             listModel.insertElementAt(entry, newSelectedIndex);
356             selectIndex(newSelectedIndex);
357         }
358     }
359
360     private void doDown() {
361         int selectedIndex = lstElements.getSelectedIndex();
362         if (selectedIndex < listModel.getSize() - 1) {
363             Object JavaDoc entry = listModel.remove(selectedIndex);
364             int newSelectedIndex = selectedIndex + 1;
365             listModel.insertElementAt(entry, newSelectedIndex);
366             selectIndex(newSelectedIndex);
367         }
368     }
369
370     private void selectIndex(int newSelectedIndex) {
371         newSelectedIndex = Math.min(newSelectedIndex, listModel.getSize() - 1);
372         if (newSelectedIndex > -1) {
373             lstElements.setSelectedIndex(newSelectedIndex);
374             lstElements.ensureIndexIsVisible(newSelectedIndex);
375         }
376     }
377
378     private void checkEnabledStatus() {
379
380         int selectedIndex = lstElements.getSelectedIndex();
381         boolean removeEnabled = selectedIndex > -1;
382         boolean upEnabled = selectedIndex > 0;
383         boolean downEnabled = selectedIndex > -1 && selectedIndex < listModel.getSize() - 1;
384         btnRemove.setEnabled(removeEnabled);
385         btnUp.setEnabled(upEnabled);
386         btnDown.setEnabled(downEnabled);
387     }
388
389 }
390
Popular Tags