View Javadoc

1   /*
2    *  Licensed under the Apache License, Version 2.0 (the "License");
3    *  you may not use this file except in compliance with the License.
4    *  You may obtain a copy of the License at
5    *
6    *      http://www.apache.org/licenses/LICENSE-2.0
7    *
8    *  Unless required by applicable law or agreed to in writing, software
9    *  distributed under the License is distributed on an "AS IS" BASIS,
10   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   *  See the License for the specific language governing permissions and
12   *  limitations under the License.
13   */
14  /*
15   * This file has been modified by Chris Pheby in accordance with Section 4.2
16   * of the Apache Software License
17   */
18  package org.jadira.dependencynavigator.config;
19  
20  import java.awt.Dimension;
21  import java.awt.FlowLayout;
22  import java.awt.event.MouseAdapter;
23  import java.awt.event.MouseEvent;
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.FileOutputStream;
27  import java.io.IOException;
28  import java.util.Properties;
29  
30  import javax.swing.BoxLayout;
31  import javax.swing.JButton;
32  import javax.swing.JDialog;
33  import javax.swing.JFileChooser;
34  import javax.swing.JFrame;
35  import javax.swing.JLabel;
36  import javax.swing.JPanel;
37  import javax.swing.JTextField;
38  
39  import org.jadira.dependencynavigator.implementations.local.LocalDiskRepository;
40  import org.jadira.dependencynavigator.implementations.local.LocalDiskWorkspace;
41  
42  public class Config {
43  
44      public static final String ROLE = "org.jadira.dependencynavigator.config.Config";
45  
46      private static final String KEY_WORKSPACE = LocalDiskWorkspace.KEY_WORKSPACE_PATH;
47      private static final String KEY_REPOSITORY = LocalDiskRepository.KEY_LOCAL_REPO_PATH;
48      private static final String CONFIG_FILE = ".dependency-browser";
49  
50      private JDialog dialog;
51  
52      private JTextField workspace;
53      private JTextField repository;
54  
55      private JButton okButton;
56      private JButton cancelButton;
57  
58      public Properties configure() {
59          dialog = new JDialog(new JFrame(), true);
60          dialog.setTitle("Configure Maven Dependency Navigator");
61          dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
62          dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.PAGE_AXIS));
63  
64          final Properties properties = getProperties();
65          workspace = new JTextField(properties.getProperty(KEY_WORKSPACE));
66          repository = new JTextField(properties.getProperty(KEY_REPOSITORY));
67  
68          JPanel buttons = new JPanel();
69          buttons.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
70          okButton = new JButton("OK");
71          okButton.setPreferredSize(new Dimension(75, 20));
72          okButton.addMouseListener(new MouseAdapter() {
73  
74              @Override
75              public void mouseClicked(MouseEvent e) {
76                  properties.setProperty(KEY_WORKSPACE, workspace.getText());
77                  properties.setProperty(KEY_REPOSITORY, repository.getText());
78                  saveProperties(properties);
79                  dialog.dispose();
80              }
81  
82          });
83          buttons.add(okButton);
84          cancelButton = new JButton("Cancel");
85          cancelButton.setPreferredSize(new Dimension(75, 20));
86          cancelButton.addMouseListener(new MouseAdapter() {
87  
88              @Override
89              public void mouseClicked(MouseEvent e) {
90                  dialog.dispose();
91              }
92  
93          });
94          buttons.add(cancelButton);
95          validate();
96  
97          dialog.getContentPane().add(createPanel("Workspace: ", workspace));
98          dialog.getContentPane().add(createPanel("Repository: ", repository));
99          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 }