KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > gui > ClassPathPanel


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.gui;
22
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.io.File JavaDoc;
26
27 import javax.swing.*;
28
29 import proguard.*;
30
31 /**
32  * This <code>ListPanel</code> allows the user to add, edit, filter, move, and
33  * remove ClassPathEntry objects in a ClassPath object.
34  *
35  * @author Eric Lafortune
36  */

37 class ClassPathPanel extends ListPanel
38 {
39     private JFrame owner;
40     private boolean inputAndOutput;
41     private JFileChooser chooser;
42     private FilterDialog filterDialog;
43
44
45     public ClassPathPanel(JFrame owner, boolean inputAndOutput)
46     {
47         super();
48
49         super.firstSelectionButton = inputAndOutput ? 3 : 2;
50
51         this.owner = owner;
52         this.inputAndOutput = inputAndOutput;
53
54         list.setCellRenderer(new MyListCellRenderer());
55
56         chooser = new JFileChooser("");
57         chooser.setMultiSelectionEnabled(true);
58         chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
59         chooser.addChoosableFileFilter(
60             new ExtensionFileFilter(GUIResources.getMessage("jarWarEarZipExtensions"),
61                                     new String JavaDoc[] { ".jar", ".war", ".ear", ".zip" }));
62         chooser.setApproveButtonText(GUIResources.getMessage("ok"));
63
64         filterDialog = new FilterDialog(owner, GUIResources.getMessage("enterFilter"));
65
66         addAddButton(inputAndOutput, false);
67         if (inputAndOutput)
68         {
69             addAddButton(inputAndOutput, true);
70         }
71         addEditButton();
72         addFilterButton();
73         addRemoveButton();
74         addUpButton();
75         addDownButton();
76
77         enableSelectionButtons();
78     }
79
80
81     protected void addAddButton(boolean inputAndOutput,
82                                 final boolean isOutput)
83     {
84         JButton addButton = new JButton(GUIResources.getMessage(inputAndOutput ?
85                                                                 isOutput ? "addOutput" :
86                                                                            "addInput" :
87                                                                            "add"));
88         addButton.addActionListener(new ActionListener()
89         {
90             public void actionPerformed(ActionEvent e)
91             {
92                 chooser.setDialogTitle(GUIResources.getMessage("addJars"));
93                 chooser.setSelectedFile(null);
94                 chooser.setSelectedFiles(null);
95
96                 int returnValue = chooser.showOpenDialog(owner);
97                 if (returnValue == JFileChooser.APPROVE_OPTION)
98                 {
99                     File JavaDoc[] selectedFiles = chooser.getSelectedFiles();
100                     ClassPathEntry[] entries = classPathEntries(selectedFiles, isOutput);
101
102                     // Add the new elements.
103
addElements(entries);
104                 }
105             }
106         });
107
108         addButton(addButton);
109     }
110
111
112     protected void addEditButton()
113     {
114         JButton editButton = new JButton(GUIResources.getMessage("edit"));
115         editButton.addActionListener(new ActionListener()
116         {
117             public void actionPerformed(ActionEvent e)
118             {
119                 boolean isOutput = false;
120
121                 int[] selectedIndices = list.getSelectedIndices();
122
123                 // Copy the Object array into a File array.
124
File JavaDoc[] selectedFiles = new File JavaDoc[selectedIndices.length];
125                 for (int index = 0; index < selectedFiles.length; index++)
126                 {
127                     ClassPathEntry entry =
128                         (ClassPathEntry)listModel.getElementAt(selectedIndices[index]);
129
130                     isOutput = entry.isOutput();
131
132                     selectedFiles[index] = entry.getFile();
133                 }
134
135                 chooser.setDialogTitle(GUIResources.getMessage("chooseJars"));
136
137                 // Up to JDK 1.3.1, setSelectedFiles doesn't show in the file
138
// chooser, so we just use setSelectedFile first. It also sets
139
// the current directory.
140
chooser.setSelectedFile(selectedFiles[0]);
141                 chooser.setSelectedFiles(selectedFiles);
142
143                 int returnValue = chooser.showOpenDialog(owner);
144                 if (returnValue == JFileChooser.APPROVE_OPTION)
145                 {
146                     selectedFiles = chooser.getSelectedFiles();
147                     ClassPathEntry[] entries = classPathEntries(selectedFiles, isOutput);
148
149                     // If there are the same number of files selected now as
150
// there were before, we can just replace the old ones.
151
if (selectedIndices.length == selectedFiles.length)
152                     {
153                         // Replace the old elements.
154
setElementsAt(entries, selectedIndices);
155                     }
156                     else
157                     {
158                         // Remove the old elements.
159
removeElementsAt(selectedIndices);
160
161                         // Add the new elements.
162
addElements(entries);
163                     }
164                 }
165             }
166         });
167
168         addButton(editButton);
169     }
170
171
172     protected void addFilterButton()
173     {
174         JButton filterButton = new JButton(GUIResources.getMessage("filter"));
175         filterButton.addActionListener(new ActionListener()
176         {
177             public void actionPerformed(ActionEvent e)
178             {
179                 if (!list.isSelectionEmpty())
180                 {
181                     int[] selectedIndices = list.getSelectedIndices();
182
183                     // Put the filters of the first selected entry in the dialog.
184
getFiltersFrom(selectedIndices[0]);
185
186                     int returnValue = filterDialog.showDialog();
187                     if (returnValue == FilterDialog.APPROVE_OPTION)
188                     {
189                         // Apply the entered filters to all selected entries.
190
setFiltersAt(selectedIndices);
191                     }
192                 }
193             }
194         });
195
196         addButton(filterButton);
197     }
198
199
200     /**
201      * Sets the ClassPath to be represented in this panel.
202      */

203     public void setClassPath(ClassPath classPath)
204     {
205         listModel.clear();
206
207         if (classPath != null)
208         {
209             for (int index = 0; index < classPath.size(); index++)
210             {
211                 listModel.addElement(classPath.get(index));
212             }
213         }
214
215         // Make sure the selection buttons are properly enabled,
216
// since the clear method doesn't seem to notify the listener.
217
enableSelectionButtons();
218     }
219
220
221     /**
222      * Returns the ClassPath currently represented in this panel.
223      */

224     public ClassPath getClassPath()
225     {
226         int size = listModel.size();
227         if (size == 0)
228         {
229             return null;
230         }
231
232         ClassPath classPath = new ClassPath();
233         for (int index = 0; index < size; index++)
234         {
235             classPath.add((ClassPathEntry)listModel.get(index));
236         }
237
238         return classPath;
239     }
240
241
242     /**
243      * Converts the given array of File objects into a corresponding array of
244      * ClassPathEntry objects.
245      */

246     private ClassPathEntry[] classPathEntries(File JavaDoc[] files, boolean isOutput)
247     {
248         ClassPathEntry[] entries = new ClassPathEntry[files.length];
249         for (int index = 0; index < entries.length; index++)
250         {
251             entries[index] = new ClassPathEntry(files[index], isOutput);
252         }
253         return entries;
254     }
255
256
257     /**
258      * Sets up the filter dialog with the filters from the specified class path
259      * entry.
260      */

261     private void getFiltersFrom(int index)
262     {
263         ClassPathEntry firstEntry = (ClassPathEntry)listModel.get(index);
264
265         filterDialog.setFilter(firstEntry.getFilter());
266         filterDialog.setJarFilter(firstEntry.getJarFilter());
267         filterDialog.setWarFilter(firstEntry.getWarFilter());
268         filterDialog.setEarFilter(firstEntry.getEarFilter());
269         filterDialog.setZipFilter(firstEntry.getZipFilter());
270     }
271
272
273     /**
274      * Applies the entered filter to the specified class path entries.
275      * Any previously set filters are discarded.
276      */

277     private void setFiltersAt(int[] indices)
278     {
279         for (int index = indices.length - 1; index >= 0; index--)
280         {
281             ClassPathEntry entry = (ClassPathEntry)listModel.get(indices[index]);
282             entry.setFilter(filterDialog.getFilter());
283             entry.setJarFilter(filterDialog.getJarFilter());
284             entry.setWarFilter(filterDialog.getWarFilter());
285             entry.setEarFilter(filterDialog.getEarFilter());
286             entry.setZipFilter(filterDialog.getZipFilter());
287         }
288
289         // Make sure they are selected and thus repainted.
290
list.setSelectedIndices(indices);
291     }
292
293
294     /**
295      * This ListCellRenderer renders ClassPathEntry objects.
296      */

297     private class MyListCellRenderer implements ListCellRenderer
298     {
299         private static final String JavaDoc ARROW_IMAGE_FILE = "arrow.gif";
300
301         private JPanel cellPanel = new JPanel(new GridBagLayout());
302         private JLabel iconLabel = new JLabel("", JLabel.RIGHT);
303         private JLabel jarNameLabel = new JLabel("", JLabel.RIGHT);
304         private JLabel filterLabel = new JLabel("", JLabel.RIGHT);
305
306         private Icon arrowIcon;
307
308
309         public MyListCellRenderer()
310         {
311             GridBagConstraints jarNameLabelConstraints = new GridBagConstraints();
312             jarNameLabelConstraints.anchor = GridBagConstraints.WEST;
313             jarNameLabelConstraints.insets = new Insets(1, 2, 1, 2);
314
315             GridBagConstraints filterLabelConstraints = new GridBagConstraints();
316             filterLabelConstraints.gridwidth = GridBagConstraints.REMAINDER;
317             filterLabelConstraints.fill = GridBagConstraints.HORIZONTAL;
318             filterLabelConstraints.weightx = 1.0;
319             filterLabelConstraints.anchor = GridBagConstraints.EAST;
320             filterLabelConstraints.insets = jarNameLabelConstraints.insets;
321
322             arrowIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource(ARROW_IMAGE_FILE)));
323
324             cellPanel.add(iconLabel, jarNameLabelConstraints);
325             cellPanel.add(jarNameLabel, jarNameLabelConstraints);
326             cellPanel.add(filterLabel, filterLabelConstraints);
327         }
328
329
330         // Implementations for ListCellRenderer.
331

332         public Component getListCellRendererComponent(JList list,
333                                                       Object JavaDoc value,
334                                                       int index,
335                                                       boolean isSelected,
336                                                       boolean cellHasFocus)
337         {
338             ClassPathEntry entry = (ClassPathEntry)value;
339
340             // Prepend an arrow to the output entries.
341
if (inputAndOutput && entry.isOutput())
342             {
343                 iconLabel.setIcon(arrowIcon);
344             }
345             else
346             {
347                 iconLabel.setIcon(null);
348             }
349
350             // Set the entry name text.
351
jarNameLabel.setText(entry.getName());
352
353             // Set the filter text.
354
StringBuffer JavaDoc filter = null;
355             filter = appendFilter(filter, entry.getZipFilter());
356             filter = appendFilter(filter, entry.getEarFilter());
357             filter = appendFilter(filter, entry.getWarFilter());
358             filter = appendFilter(filter, entry.getJarFilter());
359             filter = appendFilter(filter, entry.getFilter());
360
361             if (filter != null)
362             {
363                 filter.append(')');
364             }
365
366             filterLabel.setText(filter != null ? filter.toString() : "");
367
368             // Set the colors.
369
if (isSelected)
370             {
371                 cellPanel.setBackground(list.getSelectionBackground());
372                 jarNameLabel.setForeground(list.getSelectionForeground());
373                 filterLabel.setForeground(list.getSelectionForeground());
374             }
375             else
376             {
377                 cellPanel.setBackground(list.getBackground());
378                 jarNameLabel.setForeground(list.getForeground());
379                 filterLabel.setForeground(list.getForeground());
380             }
381
382             // Make the font color red if this is an input file that can't be read.
383
if (!(inputAndOutput && entry.isOutput()) &&
384                 !entry.getFile().canRead())
385             {
386                 jarNameLabel.setForeground(Color.red);
387             }
388
389             cellPanel.setOpaque(true);
390
391             return cellPanel;
392         }
393
394
395         private StringBuffer JavaDoc appendFilter(StringBuffer JavaDoc filter, String JavaDoc additionalFilter)
396         {
397             if (filter != null)
398             {
399                 filter.append(';');
400             }
401
402             if (additionalFilter != null)
403             {
404                 if (filter == null)
405                 {
406                     filter = new StringBuffer JavaDoc().append('(');
407                 }
408
409                 filter.append(additionalFilter);
410             }
411
412             return filter;
413         }
414     }
415 }
416
Popular Tags