]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/tool/tzu/PathComponent.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / tool / tzu / PathComponent.java
1 /*\r
2  * ******************************************************************************\r
3  * Copyright (C) 2007, International Business Machines Corporation and others.\r
4  * All Rights Reserved.\r
5  * ******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.tool.tzu;\r
8 \r
9 import java.awt.Component;\r
10 import java.awt.datatransfer.StringSelection;\r
11 import java.awt.event.ActionEvent;\r
12 import java.awt.event.ActionListener;\r
13 import java.awt.event.KeyAdapter;\r
14 import java.awt.event.KeyEvent;\r
15 import java.awt.event.MouseEvent;\r
16 import java.awt.event.MouseListener;\r
17 import java.io.File;\r
18 \r
19 import javax.swing.BoxLayout;\r
20 import javax.swing.JButton;\r
21 import javax.swing.JCheckBox;\r
22 import javax.swing.JComboBox;\r
23 import javax.swing.JComponent;\r
24 import javax.swing.JFileChooser;\r
25 import javax.swing.JLabel;\r
26 import javax.swing.JList;\r
27 import javax.swing.JMenuItem;\r
28 import javax.swing.JOptionPane;\r
29 import javax.swing.JPanel;\r
30 import javax.swing.JPopupMenu;\r
31 import javax.swing.JScrollPane;\r
32 import javax.swing.JSeparator;\r
33 import javax.swing.JTextField;\r
34 \r
35 /**\r
36  * The path list GUI component.\r
37  */\r
38 public class PathComponent extends JComponent {\r
39     /**\r
40      * The serializable UID.\r
41      */\r
42     public static final long serialVersionUID = 1340;\r
43 \r
44     /**\r
45      * A menu item for <code>pathPopup</code> to add all drives to the path model.\r
46      */\r
47     private JMenuItem pathAddAllDrivesItem = new JMenuItem("Add All Drives to List");\r
48 \r
49     /**\r
50      * The browse button where the user can browse for a particular path.\r
51      */\r
52     private JButton pathBrowseButton = new JButton("Browse...");\r
53 \r
54     /**\r
55      * A menu item that copies the selected filenames to the clipboard.\r
56      */\r
57     private JMenuItem pathCopyItem = new JMenuItem("Copy selected");\r
58 \r
59     /**\r
60      * The browse dialog that pops up when the browse button is clicked.\r
61      */\r
62     private JFileChooser pathChooser = new JFileChooser();\r
63 \r
64     /**\r
65      * The field where the user can enter a path.\r
66      */\r
67     private JTextField pathField = new JTextField(30);\r
68 \r
69     /**\r
70      * The label for path input field.\r
71      */\r
72     private JLabel pathInputLabel = new JLabel("Include/exclude a directory or a file:");\r
73 \r
74     /**\r
75      * The label for the path list.\r
76      */\r
77     private JLabel pathListLabel = new JLabel("Directories to search and ICU4J jar files to check:");\r
78 \r
79     /**\r
80      * The panel to hold the input components.\r
81      */\r
82     private JPanel pathInputPanel = new JPanel();\r
83 \r
84     /**\r
85      * The JList that holds the path model.\r
86      */\r
87     private JList pathList = new JList();\r
88 \r
89     /**\r
90      * The path model that stores all the paths.\r
91      */\r
92     private PathModel pathModel;\r
93 \r
94     /**\r
95      * The panel to hold the output components.\r
96      */\r
97     private JPanel pathOptionPanel = new JPanel();\r
98 \r
99     /**\r
100      * The context menu for extra options.\r
101      */\r
102     private JPopupMenu pathPopup = new JPopupMenu();\r
103 \r
104     /**\r
105      * A menu item for <code>pathPopup</code> to remove all paths from the path model.\r
106      */\r
107     private JMenuItem pathRemoveAllItem = new JMenuItem("Remove All");\r
108 \r
109     /**\r
110      * A menu item for <code>pathPopup</code> to remove the selected paths from the path model.\r
111      */\r
112     private JMenuItem pathRemoveSelectedItem = new JMenuItem("Remove Selected Items");\r
113 \r
114     /**\r
115      * A menu item for <code>pathPopup</code> to begin a search on the selected paths in the path\r
116      * model.\r
117      */\r
118     private JMenuItem pathSearchAllItem = new JMenuItem("Search All");\r
119 \r
120     /**\r
121      * The search button that starts the search on the selected paths (or all the paths if none are\r
122      * selected).\r
123      */\r
124     private JButton pathSearchAllButton = new JButton("Search All");\r
125 \r
126     /**\r
127      * The panel to hold the search components.\r
128      */\r
129     private JPanel pathSearchPanel = new JPanel();\r
130 \r
131     /**\r
132      * A menu item for <code>pathPopup</code> to begin a search on all paths in the path model.\r
133      */\r
134     private JMenuItem pathSearchSelectedItem = new JMenuItem("Search Selected Items");\r
135 \r
136     /**\r
137      * The combobox where a user specifies whether to include or to exclude an entered path.\r
138      */\r
139     private JComboBox pathSignBox = new JComboBox(new Object[] { "Include", "Exclude" });\r
140 \r
141     /**\r
142      * The checkbox where the user can specify whether or not to search subdirectories. Set to true\r
143      * by default.\r
144      */\r
145     private JCheckBox pathSubdirOption = new JCheckBox("Search Subdirectories", true);\r
146 \r
147     /**\r
148      * Preferred starting number of rows in the table.\r
149      */\r
150     public static final int PATH_LIST_ROWS_PREFERRED = 5;\r
151 \r
152     /**\r
153      * Constructs the path list GUI component.\r
154      * \r
155      * @param owner\r
156      *            The GUILoader object that ownes this component.\r
157      */\r
158     public PathComponent(final GUILoader owner) {\r
159         pathList.setVisibleRowCount(PATH_LIST_ROWS_PREFERRED);\r
160 \r
161         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));\r
162         add(pathInputPanel);\r
163         add(pathListLabel);\r
164         add(new JScrollPane(pathList));\r
165         add(pathOptionPanel);\r
166         add(pathSearchPanel);\r
167 \r
168         JPanel pathInputSubPanel = new JPanel();\r
169         pathInputPanel.setLayout(new BoxLayout(pathInputPanel, BoxLayout.Y_AXIS));\r
170         pathInputPanel.add(pathInputLabel);\r
171         pathInputPanel.add(pathInputSubPanel);\r
172         pathInputSubPanel.add(pathSignBox);\r
173         pathInputSubPanel.add(pathField);\r
174         pathInputSubPanel.add(pathBrowseButton);\r
175 \r
176         pathOptionPanel.add(pathSubdirOption);\r
177         pathSearchPanel.add(pathSearchAllButton);\r
178 \r
179         pathChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);\r
180 \r
181         pathPopup.add(pathCopyItem);\r
182         pathPopup.add(new JSeparator());\r
183         pathPopup.add(pathAddAllDrivesItem);\r
184         pathPopup.add(pathRemoveSelectedItem);\r
185         pathPopup.add(pathRemoveAllItem);\r
186         pathPopup.add(new JSeparator());\r
187         pathPopup.add(pathSearchSelectedItem);\r
188         pathPopup.add(pathSearchAllItem);\r
189 \r
190         pathField.addActionListener(new ActionListener() {\r
191             public void actionPerformed(ActionEvent event) {\r
192                 addFile(new File(pathField.getText().trim()));\r
193                 pathField.selectAll();\r
194             }\r
195         });\r
196 \r
197         pathList.addMouseListener(new MouseListener() {\r
198             public void mouseClicked(MouseEvent event) {\r
199                 checkPopup(event);\r
200             }\r
201 \r
202             public void mouseEntered(MouseEvent event) {\r
203                 checkPopup(event);\r
204             }\r
205 \r
206             public void mouseExited(MouseEvent event) {\r
207                 checkPopup(event);\r
208             }\r
209 \r
210             public void mousePressed(MouseEvent event) {\r
211                 checkPopup(event);\r
212             }\r
213 \r
214             public void mouseReleased(MouseEvent event) {\r
215                 checkPopup(event);\r
216             }\r
217 \r
218             private void checkPopup(MouseEvent event) {\r
219                 if (event.isPopupTrigger())\r
220                     pathPopup.show((Component) event.getSource(), event.getX(), event.getY());\r
221             }\r
222         });\r
223 \r
224         pathList.addKeyListener(new KeyAdapter() {\r
225             public void keyPressed(KeyEvent event) {\r
226                 int code = event.getKeyCode();\r
227                 if (code == KeyEvent.VK_DELETE || code == KeyEvent.VK_BACK_SPACE)\r
228                     pathModel.remove(pathList.getSelectedIndices());\r
229             }\r
230         });\r
231 \r
232         pathCopyItem.addActionListener(new ActionListener() {\r
233             public void actionPerformed(ActionEvent event) {\r
234                 String selection = "";\r
235                 int[] rows = pathList.getSelectedIndices();\r
236                 for (int i = 0; i < rows.length; i++) {\r
237                     String includePathString = pathModel.getElementAt(rows[i]).toString();\r
238                     // get rid of a + or - at the begining of includePathString\r
239                     // if one exists\r
240                     if (includePathString.length() > 0\r
241                             && (includePathString.charAt(0) == '+' || includePathString.charAt(0) == '-'))\r
242                         includePathString = includePathString.substring(1);\r
243                     selection += includePathString + "\n";\r
244                 }\r
245                 getToolkit().getSystemClipboard().setContents(new StringSelection(selection), null);\r
246             }\r
247         });\r
248 \r
249         pathRemoveSelectedItem.addActionListener(new ActionListener() {\r
250             public void actionPerformed(ActionEvent event) {\r
251                 pathModel.remove(pathList.getSelectedIndices());\r
252             }\r
253         });\r
254 \r
255         pathRemoveAllItem.addActionListener(new ActionListener() {\r
256             public void actionPerformed(ActionEvent event) {\r
257                 pathModel.removeAll();\r
258             }\r
259         });\r
260 \r
261         pathSearchSelectedItem.addActionListener(new ActionListener() {\r
262             public void actionPerformed(ActionEvent event) {\r
263                 owner.search(pathList.getSelectedIndices(), pathSubdirOption.isSelected());\r
264             }\r
265         });\r
266 \r
267         pathSearchAllItem.addActionListener(new ActionListener() {\r
268             public void actionPerformed(ActionEvent event) {\r
269                 owner.searchAll(pathSubdirOption.isSelected());\r
270             }\r
271         });\r
272 \r
273         pathSearchAllButton.addActionListener(new ActionListener() {\r
274             public void actionPerformed(ActionEvent event) {\r
275                 owner.searchAll(pathSubdirOption.isSelected());\r
276             }\r
277         });\r
278 \r
279         pathAddAllDrivesItem.addActionListener(new ActionListener() {\r
280             public void actionPerformed(ActionEvent event) {\r
281                 pathModel.addAllDrives();\r
282             }\r
283         });\r
284 \r
285         pathBrowseButton.addActionListener(new ActionListener() {\r
286             public void actionPerformed(ActionEvent event) {\r
287                 // set the chooser's intial path to be whatever is in the text\r
288                 // field\r
289                 File path = new File(pathField.getText().trim());\r
290                 if (path.exists())\r
291                     pathChooser.setSelectedFile(path);\r
292 \r
293                 // run the chooser dialog\r
294                 int returnVal = pathChooser.showOpenDialog(PathComponent.this);\r
295 \r
296                 // on an accept, add the path to the model and set the text\r
297                 // field to it\r
298                 if (returnVal == JFileChooser.APPROVE_OPTION) {\r
299                     path = pathChooser.getSelectedFile();\r
300                     addFile(path);\r
301                     pathField.setText(path.getPath());\r
302                 }\r
303             }\r
304         });\r
305     }\r
306 \r
307     /**\r
308      * Sets the path model.\r
309      * \r
310      * @param pathModel\r
311      *            The path model.\r
312      */\r
313     public void setPathModel(PathModel pathModel) {\r
314         this.pathModel = pathModel;\r
315         pathList.setModel(pathModel);\r
316     }\r
317 \r
318     /**\r
319      * Sets whether the search button should be enabled.\r
320      * \r
321      * @param value\r
322      *            Whether the search button should be enabled.\r
323      */\r
324     public void setSearchEnabled(boolean value) {\r
325         pathSearchAllButton.setEnabled(value);\r
326     }\r
327 \r
328     /**\r
329      * Attempts to add a path to the path model.\r
330      * \r
331      * @param file\r
332      *            The path to add.\r
333      */\r
334     private void addFile(File file) {\r
335         if (!pathModel.add(new IncludePath(file, isIncluded())))\r
336             JOptionPane.showMessageDialog(PathComponent.this, "\"" + file.getPath()\r
337                     + "\" is not a valid file or path.", "Cannot add path/file",\r
338                     JOptionPane.ERROR_MESSAGE);\r
339     }\r
340 \r
341     /**\r
342      * Returns whether the user has specified to include or to exclude the entered path.\r
343      * \r
344      * @return Whether the user has specified to include or to exclude the entered path.\r
345      */\r
346     private boolean isIncluded() {\r
347         return ((String) pathSignBox.getSelectedItem()).equals("Include");\r
348     }\r
349 }\r