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.bookmark;
020    
021    import com.mucommander.bookmark.Bookmark;
022    import com.mucommander.bookmark.BookmarkManager;
023    import com.mucommander.file.AbstractFile;
024    import com.mucommander.text.Translator;
025    import com.mucommander.ui.action.MuAction;
026    import com.mucommander.ui.dialog.DialogToolkit;
027    import com.mucommander.ui.dialog.FocusDialog;
028    import com.mucommander.ui.layout.XAlignedComponentPanel;
029    import com.mucommander.ui.layout.YBoxPanel;
030    import com.mucommander.ui.main.MainFrame;
031    
032    import javax.swing.*;
033    import javax.swing.event.DocumentEvent;
034    import javax.swing.event.DocumentListener;
035    import java.awt.*;
036    import java.awt.event.ActionEvent;
037    import java.awt.event.ActionListener;
038    
039    
040    /**
041     * This dialog allows the user to add a bookmark and enter a name for it. User can also
042     * choose to store login and password information in the bookmark's URL if the bookmark
043     * contains login/password information.
044     *
045     * @author Maxence Bernard
046     */
047    public class AddBookmarkDialog extends FocusDialog implements ActionListener, DocumentListener {
048    
049        private JTextField nameField;
050        private JTextField locationField;
051    
052        private JButton addButton;
053        private JButton cancelButton;
054    
055        // Dialog's width has to be at least 320
056        private final static Dimension MINIMUM_DIALOG_DIMENSION = new Dimension(320,0);     
057    
058        // Dialog's width has to be at most 400
059        private final static Dimension MAXIMUM_DIALOG_DIMENSION = new Dimension(400,10000); 
060    
061    
062        public AddBookmarkDialog(MainFrame mainFrame) {
063            super(mainFrame, MuAction.getStandardLabel(com.mucommander.ui.action.AddBookmarkAction.class), mainFrame);
064    
065            Container contentPane = getContentPane();
066            YBoxPanel mainPanel = new YBoxPanel(5);
067    
068            AbstractFile currentFolder = mainFrame.getActiveTable().getCurrentFolder();
069    
070            // Text fields panel
071            XAlignedComponentPanel compPanel = new XAlignedComponentPanel();
072    
073            // Add name field, editable
074            this.nameField = new JTextField(currentFolder.getName());
075            nameField.setEditable(true);
076            // Monitors text changes to disable 'Add' button if name field is empty
077            nameField.getDocument().addDocumentListener(this);
078            compPanel.addRow(Translator.get("name")+":", nameField, 10);
079                    
080            // Add URL field, non editable
081            this.locationField = new JTextField(currentFolder.getCanonicalPath());
082            compPanel.addRow(Translator.get("location")+":", locationField, 10);
083    
084            mainPanel.add(compPanel);
085    
086            contentPane.add(mainPanel, BorderLayout.NORTH);
087                                    
088            addButton = new JButton(Translator.get("add_bookmark_dialog.add"));
089            cancelButton = new JButton(Translator.get("cancel"));
090            contentPane.add(DialogToolkit.createOKCancelPanel(addButton, cancelButton, getRootPane(), this), BorderLayout.SOUTH);
091    
092            // Select text in name field and transfer focus to it for immediate user change
093            nameField.selectAll();
094            setInitialFocusComponent(nameField);
095    
096            // Packs dialog
097            setMinimumSize(MINIMUM_DIALOG_DIMENSION);
098            setMaximumSize(MAXIMUM_DIALOG_DIMENSION);
099            
100            showDialog();
101        }
102    
103            
104        /**
105         * Checks if bookmark name is empty (or white space), and enable/disable 'Add' button
106         * accordingly, in order to prevent user from adding a bookmark with an empty name.
107         */
108        private void checkEmptyName() {
109            if(nameField.getText().trim().equals("")) {
110                if(addButton.isEnabled())
111                    addButton.setEnabled(false);
112            }
113            else {
114                if(!addButton.isEnabled())
115                    addButton.setEnabled(true);
116            }
117        }
118            
119            
120        ///////////////////////////
121        // ActionListener method //
122        ///////////////////////////
123            
124        public void actionPerformed(ActionEvent e) {
125            Object source = e.getSource();
126                    
127            if (source==addButton)  {
128                // Starts by disposing the dialog
129                dispose();
130    
131                // Add bookmark and write bookmarks file to disk
132                BookmarkManager.addBookmark(new Bookmark(nameField.getText(), locationField.getText()));
133                try {BookmarkManager.writeBookmarks(false);}
134                // We should probably pop an error dialog here.
135                catch(Exception e2) {}
136            }
137            else if (source==cancelButton)  {
138                dispose();                  
139            }
140        }
141    
142    
143        //////////////////////////////
144        // DocumentListener methods //
145        //////////////////////////////
146            
147        public void changedUpdate(DocumentEvent e) {
148            checkEmptyName();
149        }
150            
151            
152        public void insertUpdate(DocumentEvent e) {
153            checkEmptyName();
154        }
155            
156            
157        public void removeUpdate(DocumentEvent e) {
158            checkEmptyName();
159        }
160    }