001    /*
002     *  Licensed under the Apache License, Version 2.0 (the "License");
003     *  you may not use this file except in compliance with the License.
004     *  You may obtain a copy of the License at
005     *
006     *      http://www.apache.org/licenses/LICENSE-2.0
007     *
008     *  Unless required by applicable law or agreed to in writing, software
009     *  distributed under the License is distributed on an "AS IS" BASIS,
010     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011     *  See the License for the specific language governing permissions and
012     *  limitations under the License.
013     */
014    /*
015     * This file has been modified by Chris Pheby in accordance with Section 4.2
016     * of the Apache Software License
017     */
018    package org.jadira.dependencynavigator.config;
019    
020    import java.awt.Dimension;
021    import java.awt.FlowLayout;
022    import java.awt.event.MouseAdapter;
023    import java.awt.event.MouseEvent;
024    import java.io.File;
025    import java.io.FileInputStream;
026    import java.io.FileOutputStream;
027    import java.io.IOException;
028    import java.util.Properties;
029    
030    import javax.swing.BoxLayout;
031    import javax.swing.JButton;
032    import javax.swing.JDialog;
033    import javax.swing.JFileChooser;
034    import javax.swing.JFrame;
035    import javax.swing.JLabel;
036    import javax.swing.JPanel;
037    import javax.swing.JTextField;
038    
039    import org.jadira.dependencynavigator.implementations.local.LocalDiskRepository;
040    import org.jadira.dependencynavigator.implementations.local.LocalDiskWorkspace;
041    
042    public class Config {
043    
044        public static final String ROLE = "org.jadira.dependencynavigator.config.Config";
045    
046        private static final String KEY_WORKSPACE = LocalDiskWorkspace.KEY_WORKSPACE_PATH;
047        private static final String KEY_REPOSITORY = LocalDiskRepository.KEY_LOCAL_REPO_PATH;
048        private static final String CONFIG_FILE = ".dependency-browser";
049    
050        private JDialog dialog;
051    
052        private JTextField workspace;
053        private JTextField repository;
054    
055        private JButton okButton;
056        private JButton cancelButton;
057    
058        public Properties configure() {
059            dialog = new JDialog(new JFrame(), true);
060            dialog.setTitle("Configure Maven Dependency Navigator");
061            dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
062            dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.PAGE_AXIS));
063    
064            final Properties properties = getProperties();
065            workspace = new JTextField(properties.getProperty(KEY_WORKSPACE));
066            repository = new JTextField(properties.getProperty(KEY_REPOSITORY));
067    
068            JPanel buttons = new JPanel();
069            buttons.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
070            okButton = new JButton("OK");
071            okButton.setPreferredSize(new Dimension(75, 20));
072            okButton.addMouseListener(new MouseAdapter() {
073    
074                @Override
075                public void mouseClicked(MouseEvent e) {
076                    properties.setProperty(KEY_WORKSPACE, workspace.getText());
077                    properties.setProperty(KEY_REPOSITORY, repository.getText());
078                    saveProperties(properties);
079                    dialog.dispose();
080                }
081    
082            });
083            buttons.add(okButton);
084            cancelButton = new JButton("Cancel");
085            cancelButton.setPreferredSize(new Dimension(75, 20));
086            cancelButton.addMouseListener(new MouseAdapter() {
087    
088                @Override
089                public void mouseClicked(MouseEvent e) {
090                    dialog.dispose();
091                }
092    
093            });
094            buttons.add(cancelButton);
095            validate();
096    
097            dialog.getContentPane().add(createPanel("Workspace: ", workspace));
098            dialog.getContentPane().add(createPanel("Repository: ", repository));
099            dialog.getContentPane().add(buttons);
100            dialog.pack();
101            dialog.setVisible(true);
102            return properties;
103        }
104    
105        private void validate() {
106            if (workspace.getText() == null || workspace.getText().length() == 0) {
107                okButton.setEnabled(false);
108                return;
109            }
110            if (workspace.getText() == null || repository.getText().length() == 0) {
111                okButton.setEnabled(false);
112                return;
113            }
114            okButton.setEnabled(true);
115        }
116    
117        private JPanel createPanel(String label, final JTextField text) {
118            JPanel panel = new JPanel();
119            panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
120            panel.add(new JLabel(label));
121            text.setEditable(false);
122            text.setPreferredSize(new Dimension(300, 20));
123            panel.add(text);
124            JButton button = new JButton("...");
125            button.setPreferredSize(new Dimension(25, 20));
126            panel.add(button);
127            button.addMouseListener(new MouseAdapter() {
128    
129                @Override
130                public void mouseClicked(MouseEvent e) {
131                    File dir = selectDirectory();
132                    if (dir != null) {
133                        text.setText(dir.getPath());
134                        validate();
135                    }
136                }
137    
138            });
139            return panel;
140        }
141    
142        private File selectDirectory() {
143    
144            JFileChooser fc = new JFileChooser() {
145    
146                private static final long serialVersionUID = 7609125749403145970L;
147    
148                @Override
149                public void updateUI() {
150                    // workaround for bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6317789
151                    putClientProperty("FileChooser.useShellFolder", Boolean.FALSE);
152                    super.updateUI();
153                }
154            };
155            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
156            int returnVal = fc.showOpenDialog(dialog);
157            if (returnVal == JFileChooser.APPROVE_OPTION) {
158                File file = fc.getSelectedFile();
159                if (file.isDirectory()) {
160                    return file;
161                }
162            }
163            return null;
164        }
165    
166        private Properties getProperties() {
167            Properties properties = new Properties();
168            File propertiesFile = getPropertiesFile();
169            if (propertiesFile != null && propertiesFile.exists()) {
170                try {
171                    properties.load(new FileInputStream(propertiesFile));
172                } catch (IOException e) {
173                    System.err.println("Could not open properties file: " + propertiesFile.getPath());
174                    e.printStackTrace(System.err);
175                }
176            }
177            return properties;
178        }
179    
180        private File getPropertiesFile() {
181            return new File(System.getProperty("user.home"), CONFIG_FILE);
182        }
183    
184        private void saveProperties(Properties properties) {
185            File propertiesFile = getPropertiesFile();
186            try {
187                properties.store(new FileOutputStream(propertiesFile), "");
188            } catch (IOException e) {
189                System.err.println("Could not save properties file: " + propertiesFile.getPath());
190                e.printStackTrace(System.err);
191            }
192        }
193    }