1 /*
2 * NeonZip - archive tool
3 * Copyright (C) 2001 Peter Ivanov
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 *
20 * @author Peter Ivanov
21 * <a href=mailto:peterivanov@europe.com>peterivanov@europe.com</a>,
22 * <br>Copyright (c) 2001 Peter K. Ivanov.
23 *
24 * @version (November 06 2001)
25 */
26
27 package net.sourceforge.neonzip;
28
29 import javax.swing;
30 import javax.swing.border;
31 import javax.swing.tree;
32 import java.awt;
33 import java.awt.event;
34 import java.io;
35 import java.util;
36 import java.net.URL;
37
38 public final class ExtractDialog extends JDialog {
39
40 private boolean fIsApproved = false;
41 private File fExtractionPath;
42
43 private Vector fPathList;
44
45 //private boolean fSelectedFiles = false;
46 //private boolean fAllFiles = false;
47
48 //private boolean fOverwriteExisting = false;
49 //private boolean fSkipOlder = false;
50 //private boolean fUseFolder = false;
51
52 /* Actions */
53 private CancelAction cancelAction;
54 private ExtractAction extractAction;
55 private FilteredFilesAction filteredFilesAction;
56 private ExpandFolderAction expandFolderAction;
57 private NewDirAction newDirAction;
58
59 private JComboBox comboPath;
60 private ButtonGroup radioGroup;
61 private JRadioButton radioSelectedFiles;
62 private JRadioButton radioAllFiles;
63 private JRadioButton radioFilteredFiles;
64 private JTextField fieldFilter;
65 private JCheckBox checkOverwriteExisting;
66 private JCheckBox checkSkipOlder;
67 private JCheckBox checkFolders;
68
69 private NeonDirectoryChooser dirChooser;
70
71 /**
72 * Class constructor.
73 */
74 ExtractDialog( JFrame aOwner, boolean aModal, File aExtractionPath,
75 Vector aPathList) {
76
77 super(aOwner, aModal);
78 fExtractionPath = aExtractionPath;
79 fPathList = aPathList;
80 initComponents();
81 pack();
82 Utils.centerWindow(this);
83 //setSize(400, 300);
84 }
85
86
87
88 /**
89 * Determine if dialog is approved or canceled.
90 * @return true if dialog is approved; false otherwise
91 */
92 public boolean isApproved() {
93 return fIsApproved;
94 }
95
96
97
98 /**
99 * Return filter for which files will be selected and extracted.
100 * @return String with filter.
101 */
102 public String getFileFilter() {
103 String filter = fieldFilter.getText();
104 if (filter.length() == 0)
105 return "*";
106 else
107 return filter;
108 }
109
110
111
112 /**
113 * Determine if sellected by user files have to be extracted.
114 * @return true if option is selected; false otherwise
115 *
116 */
117 public boolean isSelectedFilesSelected() {
118 return radioSelectedFiles.isSelected();
119 }
120
121
122
123 /**
124 * Enabled or disables 'selected files' radio button.
125 * @param aEnables true to enable button; false otherwise
126 */
127 public void setSelectedFilesEnabled(boolean aEnabled) {
128 radioSelectedFiles.setEnabled(aEnabled);
129 }
130
131
132
133 /**
134 * Sets the state of the button.
135 * @param aSelected true if the button is selected; false otherwise
136 */
137 public void setSelectedFilesSelected(boolean aSelected) {
138 radioSelectedFiles.setSelected(aSelected);
139 }
140
141
142
143 /**
144 * Determine if current files will be overwriten from archive.
145 * @return true if option is checked; false otherwise
146 */
147 public boolean isOverwriteExistingChecked() {
148 return checkOverwriteExisting.isSelected();
149 }
150
151
152
153 /**
154 * Check/uncheck programmatic overwrite existing file option
155 * @param aChecked option state
156 */
157 public void setOverwriteExisting(boolean aChecked) {
158 //fOverwriteExisting = aChecked;
159 checkOverwriteExisting.setSelected(aChecked);
160 }
161
162
163
164 /**
165 * Determine if files on the disk are newer then skip older file
166 * extraction.
167 * @return true if option is checked; false otherwise
168 */
169 public boolean isSkipOlderChecked() {
170 return checkSkipOlder.isSelected();
171 }
172
173
174
175 /**
176 * Check/uncheck programmatic skip older option.
177 * @param aChecked option state
178 */
179 public void setSkipOlder(boolean aChecked) {
180 //fSkipOlder = aChecked;
181 checkSkipOlder.setSelected(aChecked);
182 }
183
184
185
186 /**
187 * Determine when folder stored in archive will be extracted and appended
188 * to extraction path.
189 * @return true if options is checked; false otherwise
190 */
191 public boolean isUseFolderChecked() {
192 return checkFolders.isSelected();
193 }
194
195
196
197 /**
198 * Check/uncheck programmatic use folder option.
199 * @param aChecked option state
200 */
201 public void setUseFolder(boolean aChecked) {
202 //fUseFolder = aChecked;
203 checkFolders.setSelected(aChecked);
204 }
205
206
207
208 /**
209 * Determine if all files have to be extracted.
210 * @return true if option is selected; false otherwise.
211 */
212 public boolean isAllFilesSelected() {
213 return radioAllFiles.isSelected();
214 }
215
216
217
218 /**
219 * Determine if user select files by filter to be exracted.
220 * @return true if option is selected; false otherwise
221 */
222 public boolean isFilteredFilesSelected() {
223 return radioFilteredFiles.isSelected();
224 }
225
226
227
228 /**
229 * Sets the state of the 'all files' radio button.
230 * @param aSelected true if button is selected; false otherwise
231 */
232 public void setAllFilesSelected(boolean aSelected) {
233 radioAllFiles.setSelected(aSelected);
234 }
235
236
237
238 /**
239 * Extraction path.
240 * @return string with path to extraction
241 */
242 public File getExtractionPath() {
243 return fExtractionPath;
244 }
245
246
247
248 /**
249 * Returns list with last (six) extraction paths.
250 * @return Vector with string items
251 */
252 private Vector getPathList() {
253 return fPathList;
254 }
255
256
257
258 /**
259 * Load small image by given package path.
260 */
261 private ImageIcon loadImage(String aImage) {
262 URL url = getClass().getResource(aImage);
263 return new ImageIcon(url);
264 }
265
266
267
268 /**
269 * Init components.
270 */
271 private void initComponents() {
272 setTitle(Main.fResources.getString("key.title.extract"));
273 JPanel root = new JPanel();
274 root.setBorder(new EmptyBorder(5, 5, 5, 5));
275 root.setLayout(new BorderLayout());
276 setContentPane(root);
277
278 // Register escape key to close Dialog
279 root.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ESCAPE"), "close_dialog");
280 root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "close_dialog");
281 root.getActionMap().put("close_dialog", new CancelAction());
282
283 cancelAction = new CancelAction();
284 extractAction = new ExtractAction();
285 filteredFilesAction = new FilteredFilesAction();
286 expandFolderAction = new ExpandFolderAction();
287 newDirAction = new NewDirAction();
288
289 JPanel panelWest = new JPanel();
290 panelWest.setLayout(new BoxLayout(panelWest, BoxLayout.Y_AXIS));
291 //panelWest.setPreferredSize(new Dimension(200, 200));
292 JLabel labelExtract = new JLabel(Main.fResources.getString("key.label.extract_to") + ":");
293 labelExtract.setAlignmentX(Component.LEFT_ALIGNMENT);
294
295 comboPath = new JComboBox(fPathList);
296 comboPath.setEditable(true);
297 comboPath.setAlignmentX(Component.LEFT_ALIGNMENT);
298 //comboPath.addItem(fExtractionPath.toString());
299 ComboBoxEditor comboEditor = comboPath.getEditor();
300 comboEditor.setItem(fExtractionPath.toString());
301 comboEditor.selectAll();
302
303 JButton btnPointer = new JButton(expandFolderAction);
304 btnPointer.setIcon(loadImage("images/right.png"));
305 btnPointer.setAlignmentX(Component.LEFT_ALIGNMENT);
306
307 JPanel panelCombos = new JPanel();
308 panelCombos.setLayout(new BoxLayout(panelCombos, BoxLayout.X_AXIS));
309 panelCombos.setBorder(null);
310 panelCombos.setAlignmentX(Component.LEFT_ALIGNMENT);
311 panelCombos.add(comboPath);
312 panelCombos.add(Box.createHorizontalStrut(5));
313 panelCombos.add(btnPointer);
314
315 panelWest.add(labelExtract);
316 panelWest.add(Box.createRigidArea(new Dimension(5, 5)));
317 panelWest.add(panelCombos);
318 //panelWest.add(comboPath);
319 panelWest.add(Box.createRigidArea(new Dimension(5, 5)));
320
321 JPanel panelFileOptions = new JPanel();
322 panelFileOptions.setBorder(new TitledBorder(Main.fResources.getString("key.title.files")));
323 panelFileOptions.setLayout(new BoxLayout(panelFileOptions, BoxLayout.Y_AXIS));
324
325 radioGroup = new ButtonGroup();
326
327 radioSelectedFiles = new JRadioButton(Main.fResources.getString("key.radiobutton.selected_files"));
328 radioSelectedFiles.setAlignmentX(Component.LEFT_ALIGNMENT);
329 radioGroup.add(radioSelectedFiles);
330
331 JPanel panelSelectedFiles = new JPanel(new FlowLayout(FlowLayout.LEFT));
332 panelSelectedFiles.setAlignmentX(Component.LEFT_ALIGNMENT);
333 panelSelectedFiles.add(radioSelectedFiles);
334
335 panelFileOptions.add(panelSelectedFiles);
336
337 radioAllFiles = new JRadioButton(Main.fResources.getString("key.radiobutton.all_files"));
338 radioAllFiles.setAlignmentX(Component.LEFT_ALIGNMENT);
339 radioGroup.add(radioAllFiles);
340
341 JPanel panelAllFiles = new JPanel(new FlowLayout(FlowLayout.LEFT));
342 panelAllFiles.setAlignmentX(Component.LEFT_ALIGNMENT);
343 panelAllFiles.add(radioAllFiles);
344
345 panelFileOptions.add(panelAllFiles);
346
347 JPanel panelFilters = new JPanel(new FlowLayout(FlowLayout.LEFT));
348 panelFilters.setAlignmentX(Component.LEFT_ALIGNMENT);
349
350 radioFilteredFiles = new JRadioButton(filteredFilesAction);
351 radioFilteredFiles.setAlignmentX(Component.LEFT_ALIGNMENT);
352 radioGroup.add(radioFilteredFiles);
353
354 fieldFilter = new JTextField();
355 fieldFilter.setAlignmentX(Component.LEFT_ALIGNMENT);
356 fieldFilter.setColumns(16);
357
358 panelFilters.add(radioFilteredFiles);
359 panelFilters.add(fieldFilter);
360 panelFileOptions.add(panelFilters);
361
362 panelWest.add(panelFileOptions);
363 panelWest.add(Box.createRigidArea(new Dimension(5, 5)));
364
365
366 checkOverwriteExisting = new JCheckBox(Main.fResources.getString("key.checkbox.overwrite_existing"));
367 panelWest.add(checkOverwriteExisting);
368 panelWest.add(Box.createRigidArea(new Dimension(5, 5)));
369
370 checkSkipOlder = new JCheckBox(Main.fResources.getString("key.checkbox.skip_older"));
371 panelWest.add(checkSkipOlder);
372 panelWest.add(Box.createRigidArea(new Dimension(5, 5)));
373
374 checkFolders = new JCheckBox(Main.fResources.getString("key.checkbox.use_folder"));
375 panelWest.add(checkFolders);
376 panelWest.add(Box.createRigidArea(new Dimension(5, 5)));
377
378 JPanel panelEast = new JPanel();
379 panelEast.setBorder(new EmptyBorder(5, 5, 5, 5));
380 panelEast.setLayout(new BoxLayout(panelEast, BoxLayout.Y_AXIS));
381 JButton btnExtract = new JButton(extractAction);
382 getRootPane().setDefaultButton(btnExtract);
383 JButton btnCancel = new JButton(cancelAction);
384 JButton btnNewDir = new JButton(newDirAction);
385
386 panelEast.add(btnExtract);
387 panelEast.add(Box.createRigidArea(new Dimension(10, 10)));
388 panelEast.add(btnCancel);
389 panelEast.add(Box.createRigidArea(new Dimension(10, 30)));
390 panelEast.add(btnNewDir);
391
392 JPanel panelCenter = new JPanel(new BorderLayout());
393 panelCenter.setBorder(new EmptyBorder(5, 5, 5, 5));
394 dirChooser = new NeonDirectoryChooser(comboPath);
395 JScrollPane scrollBrowser = new JScrollPane(dirChooser);
396 JLabel labelDirs = new JLabel(Main.fResources.getString("key.label.directories_roots") + ":");
397 panelCenter.setPreferredSize(new Dimension(250, panelCenter.getHeight()));
398 panelCenter.add(labelDirs, "North");
399 panelCenter.add(scrollBrowser, "Center");
400
401 root.add(panelWest, "West");
402 root.add(panelCenter, "Center");
403 root.add(panelEast, "East");
404 }
405
406
407
408 /**
409 * Extract button action.
410 */
411 private final class ExtractAction extends AbstractAction {
412 ExtractAction() {
413 super(Main.fResources.getString("key.button.extract"));
414 }
415
416 public void actionPerformed(ActionEvent aEvent) {
417 fIsApproved = true;
418 String item = (String) comboPath.getEditor().getItem();
419 fExtractionPath = new File(item);
420 dispose();
421 }
422 }
423
424
425
426 /**
427 * Cancel button action.
428 */
429 private final class CancelAction extends AbstractAction {
430 CancelAction() {
431 super(Main.fResources.getString("key.button.cancel"));
432 }
433
434 public void actionPerformed(ActionEvent aEvent) {
435 fIsApproved = false;
436 dispose();
437 }
438 }
439
440
441
442 /**
443 * Filtered files radio button action.
444 */
445 private final class FilteredFilesAction extends AbstractAction {
446 FilteredFilesAction() {
447 super(Main.fResources.getString("key.radiobutton.indicated_files"));
448 }
449
450 public void actionPerformed(ActionEvent aEvent) {
451 //radioFilteredFiles.setSelected(true);
452 fieldFilter.requestFocus();
453 }
454 }
455
456
457
458 /**
459 * Expand tree button action.
460 */
461 private final class ExpandFolderAction extends AbstractAction {
462 ExpandFolderAction() {
463 super();
464 }
465
466 public void actionPerformed(ActionEvent aEvent) {
467 ComboBoxEditor comboEditor = comboPath.getEditor();
468 File file = new File((String)comboEditor.getItem());
469 if (!file.exists()) return;
470
471 dirChooser.gotoDirectory(file);
472 }
473 }
474
475
476
477 /**
478 * New directory button action.
479 */
480 private final class NewDirAction extends AbstractAction {
481 NewDirAction() {
482 super(Main.fResources.getString("key.button.new_directory") + "...");
483 }
484
485 public void actionPerformed(ActionEvent e) {
486 NewDirDialog dialog = new NewDirDialog(comboPath);
487 dialog.show();
488 }
489 }
490
491
492
493 /**
494 * NewDirectory Dialog Box.
495 */
496 private final class NewDirDialog extends JDialog {
497
498 private JComboBox fComboPath;
499 private JTextField fFieldName;
500
501 NewDirDialog(JComboBox aComboPath) {
502 super(ExtractDialog.this, true);
503
504 fComboPath = aComboPath;
505
506 initComponents();
507 Utils.centerWindow(NewDirDialog.this);
508 pack();
509 }
510
511
512
513 public void initComponents() {
514 setTitle(Main.fResources.getString("key.title.create_directory"));
515 JPanel root = new JPanel(new BorderLayout());
516 root.setBorder(new EmptyBorder(5, 5, 5, 5));
517 setContentPane(root);
518
519 JPanel panelCenter = new JPanel();
520 panelCenter.setLayout(new BoxLayout(panelCenter, BoxLayout.Y_AXIS));
521 root.add(panelCenter, "Center");
522
523 JLabel labelCurDir = new JLabel(Main.fResources.getString("key.label.current_directory") + ":");
524 labelCurDir.setAlignmentX(Component.LEFT_ALIGNMENT);
525 panelCenter.add(labelCurDir);
526
527 ComboBoxEditor editor = fComboPath.getEditor();
528 String current = (String) editor.getItem();
529
530 JLabel labelCurDirIs = new JLabel(current);
531 labelCurDirIs.setAlignmentX(Component.LEFT_ALIGNMENT);
532 panelCenter.add(labelCurDirIs);
533 panelCenter.add(Box.createRigidArea(new Dimension(10, 10)));
534
535 JLabel labelName = new JLabel(Main.fResources.getString("key.label.name") + ":");
536 labelName.setAlignmentX(Component.LEFT_ALIGNMENT);
537 panelCenter.add(labelName);
538
539 fFieldName = new JTextField();
540 fFieldName.setAlignmentX(Component.LEFT_ALIGNMENT);
541 fFieldName.setColumns(30);
542 panelCenter.add(fFieldName);
543
544 JPanel panelEast = new JPanel();
545 panelEast.setLayout(new BoxLayout(panelEast, BoxLayout.Y_AXIS));
546 root.add(panelEast, "East");
547
548 JButton btnCreate = new JButton(new CreateAction());
549 getRootPane().setDefaultButton(btnCreate);
550 panelEast.add(btnCreate);
551 panelEast.add(Box.createRigidArea(new Dimension(10, 10)));
552
553 JButton btnCancel = new JButton(new CancelAction());
554 panelEast.add(btnCancel);
555 }
556
557
558
559 /**
560 * Create directory.
561 */
562 private final class CreateAction extends AbstractAction {
563
564 private CreateAction() {
565 super(Main.fResources.getString("key.button.create"));
566 }
567
568 public void actionPerformed(ActionEvent e) {
569 ComboBoxEditor editor = fComboPath.getEditor();
570 String current = (String) editor.getItem();
571 File dir = new File(current, fFieldName.getText());
572 boolean success = dir.mkdir();
573 if (!success) {
574 Utils.sayError(Main.fResources.getString(
575 "key.message.could_not_create_directory"), NewDirDialog.this);
576 } else
577 dirChooser.gotoDirectory(dir);
578
579 dispose();
580 }
581 }
582
583
584
585 /**
586 * Cancel create dir dialog.
587 */
588 private final class CancelAction extends AbstractAction {
589
590 private CancelAction() {
591 super(Main.fResources.getString("key.button.cancel"));
592 }
593
594 public void actionPerformed(ActionEvent e) {
595 dispose();
596 }
597 }
598
599 }// End of NewDirectory Dialog Box
600
601 }// End of ExtractDialog