001    /*
002     * This file is part of muCommander, http://www.mucommander.com
003     * Copyright (C) 2002-2008 Maxence Bernard
004     *
005     * muCommander is free software; you can redistribute it and/or modify
006     * it under the terms of the GNU General Public License as published by
007     * the Free Software Foundation; either version 3 of the License, or
008     * (at your option) any later version.
009     *
010     * muCommander is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    
019    package com.mucommander.ui.dialog.file;
020    
021    import com.mucommander.file.AbstractFile;
022    import com.mucommander.file.util.FileSet;
023    import com.mucommander.job.ChangeFileAttributesJob;
024    import com.mucommander.text.CustomDateFormat;
025    import com.mucommander.text.Translator;
026    import com.mucommander.ui.action.MuAction;
027    import com.mucommander.ui.dialog.DialogToolkit;
028    import com.mucommander.ui.layout.YBoxPanel;
029    import com.mucommander.ui.main.MainFrame;
030    
031    import javax.swing.*;
032    import java.awt.*;
033    import java.awt.event.ActionEvent;
034    import java.awt.event.ActionListener;
035    import java.awt.event.ItemEvent;
036    import java.awt.event.ItemListener;
037    import java.util.Date;
038    
039    /**
040     * This dialog allows the user to change the date of the currently selected/marked file(s). By default, the date is now
041     * but a specific date can be specified.
042     *
043     * @author Maxence Bernard
044     */
045    public class ChangeDateDialog extends JobDialog implements ActionListener, ItemListener {
046    
047        private JRadioButton nowRadioButton;
048    
049        private JSpinner dateSpinner;
050    
051        private JCheckBox recurseDirCheckBox;
052        
053        private JButton okButton;
054        private JButton cancelButton;
055    
056    
057        public ChangeDateDialog(MainFrame mainFrame, FileSet files) {
058            super(mainFrame, MuAction.getStandardLabel(com.mucommander.ui.action.ChangeDateAction.class), files);
059    
060            YBoxPanel mainPanel = new YBoxPanel();
061    
062            mainPanel.add(new JLabel(MuAction.getStandardTooltip(com.mucommander.ui.action.ChangeDateAction.class)+" :"));
063            mainPanel.addSpace(5);
064    
065            ButtonGroup buttonGroup = new ButtonGroup();
066    
067            AbstractFile destFile = files.size()==1?files.fileAt(0):files.getBaseFolder();
068            boolean canChangeDate = destFile.canChangeDate();
069    
070            JPanel tempPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
071            nowRadioButton = new JRadioButton(Translator.get("change_date_dialog.now"));
072            nowRadioButton.setSelected(true);
073            nowRadioButton.addItemListener(this);
074    
075            tempPanel.add(nowRadioButton);
076    
077            mainPanel.add(tempPanel);
078    
079            buttonGroup.add(nowRadioButton);
080            JRadioButton specificDateRadioButton = new JRadioButton(Translator.get("change_date_dialog.specific_date"));
081            buttonGroup.add(specificDateRadioButton);
082    
083            tempPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
084            tempPanel.add(specificDateRadioButton);
085    
086            this.dateSpinner = new JSpinner(new SpinnerDateModel());
087            dateSpinner.setEditor(new JSpinner.DateEditor(dateSpinner, CustomDateFormat.getDateFormatString()));
088            // Use the selected file's date if there is only one file, if not use base folder's date.
089            dateSpinner.setValue(new Date(destFile.getDate()));
090            // Spinner is disabled until the 'Specific date' radio button is selected 
091            dateSpinner.setEnabled(false);
092            tempPanel.add(dateSpinner);
093    
094            mainPanel.add(tempPanel);
095    
096            mainPanel.addSpace(10);
097            recurseDirCheckBox = new JCheckBox(Translator.get("recurse_directories"));
098            mainPanel.add(recurseDirCheckBox);
099    
100            mainPanel.addSpace(15);
101    
102            // Create file details button and OK/cancel buttons and lay them out a single row
103            JPanel fileDetailsPanel = createFileDetailsPanel();
104    
105            okButton = new JButton(Translator.get("change"));
106            cancelButton = new JButton(Translator.get("cancel"));
107    
108            mainPanel.add(createButtonsPanel(createFileDetailsButton(fileDetailsPanel),
109                    DialogToolkit.createOKCancelPanel(okButton, cancelButton, getRootPane(), this)));
110            mainPanel.add(fileDetailsPanel);
111    
112            getContentPane().add(mainPanel, BorderLayout.NORTH);
113    
114            if(!canChangeDate) {
115                nowRadioButton.setEnabled(false);
116                specificDateRadioButton.setEnabled(false);
117                dateSpinner.setEnabled(false);
118                recurseDirCheckBox.setEnabled(false);
119                okButton.setEnabled(false);
120            }
121    
122            getRootPane().setDefaultButton(canChangeDate?okButton:cancelButton);
123            setInitialFocusComponent(canChangeDate?(JComponent)nowRadioButton:cancelButton);
124            setResizable(false);
125        }
126    
127    
128        ///////////////////////////////////
129        // ActionListener implementation //
130        ///////////////////////////////////
131    
132        public void actionPerformed(ActionEvent e) {
133            Object source = e.getSource();
134    
135            if(source==okButton) {
136                dispose();
137    
138                // Starts copying files
139                ProgressDialog progressDialog = new ProgressDialog(mainFrame, Translator.get("progress_dialog.processing_files"));
140                ChangeFileAttributesJob job = new ChangeFileAttributesJob(progressDialog, mainFrame, files,
141                    nowRadioButton.isSelected()?System.currentTimeMillis():((SpinnerDateModel)dateSpinner.getModel()).getDate().getTime(),
142                    recurseDirCheckBox.isSelected());
143                progressDialog.start(job);
144            }
145            else if(source==cancelButton) {
146                dispose();
147            }
148        }
149    
150    
151        /////////////////////////////////
152        // ItemListener implementation //
153        /////////////////////////////////
154    
155        // Enable/disables the date spinner component when the radio button selection has changed  
156    
157        public void itemStateChanged(ItemEvent e) {
158            if(e.getSource()==nowRadioButton) {
159                dateSpinner.setEnabled(!nowRadioButton.isSelected());
160            }
161        }
162    }