View Javadoc
1   /*
2    * #%L
3    * JRst :: Api
4    * %%
5    * Copyright (C) 2008 - 2012 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  package org.nuiton.jrst.ui;
23  
24  import com.google.common.io.Files;
25  
26  import javax.swing.*;
27  import javax.swing.filechooser.FileFilter;
28  import java.io.File;
29  
30  /**
31   * @author sletellier <letellier@codelutin.com>
32   */
33  public class FileEditorHandler {
34  
35      public static final String SEPARATOR_REGEX = "\\s*,\\s*";
36      protected FileEditor view;
37  
38      public FileEditorHandler(FileEditor view) {
39          this.view = view;
40      }
41  
42      public void openLocation() {
43  
44          // use last selected file
45          File startFile = view.getSelectedFile();
46          String startPath = view.getStartPath();
47          if (startFile == null && startPath != null) {
48  
49  
50              // else filed start path
51              startFile = new File(startPath);
52          } else {
53  
54              // else start with user home
55              startFile = new File(System.getProperty("user.home"));
56          }
57          JFileChooser fc = new JFileChooser(startFile);
58  
59          fc.setDialogTitle(view.getTitle());
60          fc.setAcceptAllFileFilterUsed(view.getAcceptAllFileFilterUsed());
61  
62          // TODO sletellier 14/06/2012 : activate multi selection
63  //        boolean multiSelectionEnabled = view.isMultiSelectionEnabled();
64  //        fc.setMultiSelectionEnabled(multiSelectionEnabled);
65  
66          // used to enable directory selection
67          boolean directoryEnabled = view.isDirectoryEnabled();
68          if (directoryEnabled) {
69              fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
70          }
71  
72          // used to enable file selection
73          boolean fileEnabled = view.isFileEnabled();
74          if (fileEnabled) {
75  
76              if (directoryEnabled) {
77  
78                  // both
79                  fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
80              } else {
81                  fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
82              }
83              String extsAsString = view.getExts();
84              if (extsAsString != null) {
85  
86                  // extentions can be separted by comma
87                  String[] exts = extsAsString.split(SEPARATOR_REGEX);
88                  String extsDescription = view.getExtsDescription();
89  
90                  String[] descs = new String[0];
91                  if (extsDescription != null) {
92                      descs = extsDescription.split(SEPARATOR_REGEX);
93                  }
94                  for (int i = 0;i<exts.length;i++) {
95  
96                      // use ext if no desc found
97                      String ext = exts[i];
98                      String desc = ext;
99                      if (descs.length > i) {
100                         desc = descs[i];
101                     }
102 
103                     fc.addChoosableFileFilter(new ExtentionFileFiler(ext, desc));
104                 }
105             }
106         }
107 
108         // directory or/and file must be enabled
109         if (!directoryEnabled && !fileEnabled) {
110             throw new IllegalArgumentException("You must enable at least file or directory to open dialog");
111         }
112 
113         // show dialog
114         int returnVal = fc.showOpenDialog(view);
115         if (returnVal == JFileChooser.APPROVE_OPTION) {
116 
117             // get selected to display in ui
118             File file = fc.getSelectedFile();
119 
120             view.setSelectedFile(file);
121         }
122     }
123 
124     public static class ExtentionFileFiler extends FileFilter {
125         protected String ext;
126         protected String desciption;
127 
128         public ExtentionFileFiler(String ext, String desciption) {
129             this.ext = ext;
130             this.desciption = desciption;
131         }
132 
133         @Override
134         public boolean accept(File file) {
135             if (file.isDirectory()) {
136                 return true;
137             }
138             String fileExtension = Files.getFileExtension(file.getName());
139             return ext.equals(fileExtension);
140         }
141 
142         @Override
143         public String getDescription() {
144             return desciption;
145         }
146     }
147 }