2 * ******************************************************************************
\r
3 * Copyright (C) 2007, International Business Machines Corporation and others.
\r
4 * All Rights Reserved.
\r
5 * ******************************************************************************
\r
7 package com.ibm.icu.dev.tool.tzu;
\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
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
36 * The path list GUI component.
\r
38 public class PathComponent extends JComponent {
\r
40 * The serializable UID.
\r
42 public static final long serialVersionUID = 1340;
\r
45 * A menu item for <code>pathPopup</code> to add all drives to the path model.
\r
47 private JMenuItem pathAddAllDrivesItem = new JMenuItem("Add All Drives to List");
\r
50 * The browse button where the user can browse for a particular path.
\r
52 private JButton pathBrowseButton = new JButton("Browse...");
\r
55 * A menu item that copies the selected filenames to the clipboard.
\r
57 private JMenuItem pathCopyItem = new JMenuItem("Copy selected");
\r
60 * The browse dialog that pops up when the browse button is clicked.
\r
62 private JFileChooser pathChooser = new JFileChooser();
\r
65 * The field where the user can enter a path.
\r
67 private JTextField pathField = new JTextField(30);
\r
70 * The label for path input field.
\r
72 private JLabel pathInputLabel = new JLabel("Include/exclude a directory or a file:");
\r
75 * The label for the path list.
\r
77 private JLabel pathListLabel = new JLabel("Directories to search and ICU4J jar files to check:");
\r
80 * The panel to hold the input components.
\r
82 private JPanel pathInputPanel = new JPanel();
\r
85 * The JList that holds the path model.
\r
87 private JList pathList = new JList();
\r
90 * The path model that stores all the paths.
\r
92 private PathModel pathModel;
\r
95 * The panel to hold the output components.
\r
97 private JPanel pathOptionPanel = new JPanel();
\r
100 * The context menu for extra options.
\r
102 private JPopupMenu pathPopup = new JPopupMenu();
\r
105 * A menu item for <code>pathPopup</code> to remove all paths from the path model.
\r
107 private JMenuItem pathRemoveAllItem = new JMenuItem("Remove All");
\r
110 * A menu item for <code>pathPopup</code> to remove the selected paths from the path model.
\r
112 private JMenuItem pathRemoveSelectedItem = new JMenuItem("Remove Selected Items");
\r
115 * A menu item for <code>pathPopup</code> to begin a search on the selected paths in the path
\r
118 private JMenuItem pathSearchAllItem = new JMenuItem("Search All");
\r
121 * The search button that starts the search on the selected paths (or all the paths if none are
\r
124 private JButton pathSearchAllButton = new JButton("Search All");
\r
127 * The panel to hold the search components.
\r
129 private JPanel pathSearchPanel = new JPanel();
\r
132 * A menu item for <code>pathPopup</code> to begin a search on all paths in the path model.
\r
134 private JMenuItem pathSearchSelectedItem = new JMenuItem("Search Selected Items");
\r
137 * The combobox where a user specifies whether to include or to exclude an entered path.
\r
139 private JComboBox pathSignBox = new JComboBox(new Object[] { "Include", "Exclude" });
\r
142 * The checkbox where the user can specify whether or not to search subdirectories. Set to true
\r
145 private JCheckBox pathSubdirOption = new JCheckBox("Search Subdirectories", true);
\r
148 * Preferred starting number of rows in the table.
\r
150 public static final int PATH_LIST_ROWS_PREFERRED = 5;
\r
153 * Constructs the path list GUI component.
\r
156 * The GUILoader object that ownes this component.
\r
158 public PathComponent(final GUILoader owner) {
\r
159 pathList.setVisibleRowCount(PATH_LIST_ROWS_PREFERRED);
\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
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
176 pathOptionPanel.add(pathSubdirOption);
\r
177 pathSearchPanel.add(pathSearchAllButton);
\r
179 pathChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
\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
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
197 pathList.addMouseListener(new MouseListener() {
\r
198 public void mouseClicked(MouseEvent event) {
\r
202 public void mouseEntered(MouseEvent event) {
\r
206 public void mouseExited(MouseEvent event) {
\r
210 public void mousePressed(MouseEvent event) {
\r
214 public void mouseReleased(MouseEvent event) {
\r
218 private void checkPopup(MouseEvent event) {
\r
219 if (event.isPopupTrigger())
\r
220 pathPopup.show((Component) event.getSource(), event.getX(), event.getY());
\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
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
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
245 getToolkit().getSystemClipboard().setContents(new StringSelection(selection), null);
\r
249 pathRemoveSelectedItem.addActionListener(new ActionListener() {
\r
250 public void actionPerformed(ActionEvent event) {
\r
251 pathModel.remove(pathList.getSelectedIndices());
\r
255 pathRemoveAllItem.addActionListener(new ActionListener() {
\r
256 public void actionPerformed(ActionEvent event) {
\r
257 pathModel.removeAll();
\r
261 pathSearchSelectedItem.addActionListener(new ActionListener() {
\r
262 public void actionPerformed(ActionEvent event) {
\r
263 owner.search(pathList.getSelectedIndices(), pathSubdirOption.isSelected());
\r
267 pathSearchAllItem.addActionListener(new ActionListener() {
\r
268 public void actionPerformed(ActionEvent event) {
\r
269 owner.searchAll(pathSubdirOption.isSelected());
\r
273 pathSearchAllButton.addActionListener(new ActionListener() {
\r
274 public void actionPerformed(ActionEvent event) {
\r
275 owner.searchAll(pathSubdirOption.isSelected());
\r
279 pathAddAllDrivesItem.addActionListener(new ActionListener() {
\r
280 public void actionPerformed(ActionEvent event) {
\r
281 pathModel.addAllDrives();
\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
289 File path = new File(pathField.getText().trim());
\r
291 pathChooser.setSelectedFile(path);
\r
293 // run the chooser dialog
\r
294 int returnVal = pathChooser.showOpenDialog(PathComponent.this);
\r
296 // on an accept, add the path to the model and set the text
\r
298 if (returnVal == JFileChooser.APPROVE_OPTION) {
\r
299 path = pathChooser.getSelectedFile();
\r
301 pathField.setText(path.getPath());
\r
308 * Sets the path model.
\r
313 public void setPathModel(PathModel pathModel) {
\r
314 this.pathModel = pathModel;
\r
315 pathList.setModel(pathModel);
\r
319 * Sets whether the search button should be enabled.
\r
322 * Whether the search button should be enabled.
\r
324 public void setSearchEnabled(boolean value) {
\r
325 pathSearchAllButton.setEnabled(value);
\r
329 * Attempts to add a path to the path model.
\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
342 * Returns whether the user has specified to include or to exclude the entered path.
\r
344 * @return Whether the user has specified to include or to exclude the entered path.
\r
346 private boolean isIncluded() {
\r
347 return ((String) pathSignBox.getSelectedItem()).equals("Include");
\r