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.implementations.local;
19  
20  import org.jadira.dependencynavigator.implementations.Workspace;
21  import org.jadira.dependencynavigator.implementations.WorkspaceLoader;
22  import org.jadira.dependencynavigator.implementations.local.LocalDiskWorkspace;
23  import org.jadira.dependencynavigator.model.ArtifactInitialisationException;
24  import org.jadira.dependencynavigator.model.RootPom;
25  
26  import javax.swing.*;
27  import javax.swing.filechooser.FileFilter;
28  import java.io.File;
29  
30  public class LocalDiskWorkspaceLoader implements WorkspaceLoader {
31      private Workspace workspace;
32      private JFrame frame;
33  
34      private LocalDiskWorkspace localDiskWorkspace() {
35          if (workspace instanceof LocalDiskWorkspace) {
36              return (LocalDiskWorkspace) workspace;
37          }
38          throw new RuntimeException("Expected workspace of type " + LocalDiskWorkspace.class.getName() + " not " + workspace.getClass().getName());
39      }
40  
41      public void loadPom() throws ArtifactInitialisationException {
42  
43          JFileChooser fc = new JFileChooser() {
44  
45              private static final long serialVersionUID = -3542823132189366492L;
46  
47              @Override
48              public void updateUI() {
49                  // workaround for bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6317789
50                  putClientProperty("FileChooser.useShellFolder", Boolean.FALSE);
51                  super.updateUI();
52              }
53          };
54          fc.setCurrentDirectory(localDiskWorkspace().getWorkspaceDirectory());
55          fc.setFileFilter(new FileFilter() {
56  
57              @Override
58              public boolean accept(File f) {
59                  return f.isDirectory() || "pom.xml".equals(f.getName());
60              }
61  
62              @Override
63              public String getDescription() {
64                  return "Maven 2 pom files";
65              }
66  
67          });
68          int returnVal = fc.showOpenDialog(frame);
69          if (returnVal == JFileChooser.APPROVE_OPTION) {
70              File file = fc.getSelectedFile();
71              localDiskWorkspace().loadRootPom(file);
72          }
73      }
74  
75      public RootPom getRoot() {
76          return workspace.getRoot();
77      }
78  
79      public void reloadPom() throws ArtifactInitialisationException {
80          workspace.reloadPomFile();
81      }
82  
83  }