NXvalidate  1
 All Classes Namespaces Files Functions Variables
UserSettings.java
Go to the documentation of this file.
1 /* NeXus - Neutron & X-ray Common Data Format
2  *
3  * NeXus file validation GUI tool.
4  *
5  * Copyright (C) 2010 Stephen Rankin
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library 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 GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * For further information, see <http://www.nexusformat.org/>
22  *
23  * UserSettings.java
24  *
25  */
26 
27 package org.nexusformat.nxvalidate;
28 
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.util.Map;
35 import java.util.Properties;
36 import java.util.ResourceBundle;
37 import java.util.logging.Level;
38 import javax.swing.JOptionPane;
39 import java.util.logging.Logger;
44 public class UserSettings {
45 
46  private Properties props = null;
47  private String nxconvertCommand = null;
48  private ResourceBundle bundle = null;
49  private boolean foundNXconvert = false;
50 
51 
52  public UserSettings(){
53  props = new Properties();
54  bundle = ResourceBundle.getBundle(
55  "org/nexusformat/nxvalidate/resources/nxvalidate");
56  }
57 
58  public void loadUserSettings() throws FileNotFoundException, IOException {
59 
60  props = new Properties();
61 
62  Map<String, String> env = System.getenv();
63  for (String envName : env.keySet()) {
64  System.out.format("%s=%s%n", envName, env.get(envName));
65  }
66 
67  File settings = new File(System.getProperty("user.home")
68  + System.getProperty("file.separator")
69  + ".nxvalidate.properties");
70 
71  if (settings.exists()) {
72  props.load(new FileInputStream(settings));
73  if (props.getProperty("nxconvert") != null) {
74 
75  if(chechExists(props.getProperty("nxconvert"))){
76  nxconvertCommand = props.getProperty("nxconvert");
77  foundNXconvert = true;
78  }
79  else{
80  defaultNXconvert();
81  }
82  }
83  else{
84  defaultNXconvert();
85  }
86  } else {
87  settings.createNewFile();
88  defaultNXconvert();
89  }
90 
91  }
92 
93  public void saveUserSettings() {
94 
95  File settings = new File(System.getProperty("user.home")
96  + System.getProperty("file.separator") + ".nxvalidate.properties");
97  try {
98  if (settings.exists()) {
99 
100  props.setProperty("nxconvert", nxconvertCommand);
101  props.store(new FileOutputStream(settings), "NXvalidate");
102 
103  } else {
104  settings.createNewFile();
105  props.setProperty("nxconvert", nxconvertCommand);
106  props.store(new FileOutputStream(settings), "NXvalidate");
107 
108  }
109  } catch (FileNotFoundException ex) {
110  Logger.getLogger(
111  NXvalidateFrame.class.getName()).log(Level.SEVERE,
112  "saveUserSettings(): The settings file cannot be found.", ex);
113  } catch (IOException ex) {
114  Logger.getLogger(
115  NXvalidateFrame.class.getName()).log(Level.SEVERE,
116  "saveUserSettings(): The settings file IO error.", ex);
117  }
118  }
119 
120  public String getNXconvert(){
121  return nxconvertCommand;
122  }
123 
124  public void setNXconvert(String nxconvertCommand){
125  this.nxconvertCommand = nxconvertCommand;
126  if(testPath(nxconvertCommand)){
127  foundNXconvert = true;
128  }
129  else{
130  foundNXconvert = false;
131  }
132  }
133 
134  public boolean foundNXconvert(){
135  return foundNXconvert;
136  }
137 
138  private void defaultNXconvert(){
139  OSValidator os = new OSValidator();
140 
141  if(os.isWindows()){
142  nxconvertCommand = bundle.getString("defaultWindowsNXconvert");
143  } else if(os.isMac()){
144  nxconvertCommand = bundle.getString("defaultMacNXconvert");
145  } else if(os.isUnix()){
146  nxconvertCommand = bundle.getString("defaultUNIXNXconvert");
147  if(!chechExists(nxconvertCommand)){
148  nxconvertCommand = bundle.getString("defaultUNIXNXconvert2");
149  }
150  }
151 
152  if(!chechExists(nxconvertCommand)){
153  nxconvertCommand = "nxconvert";
154  }
155 
156  if(testPath(nxconvertCommand)){
157  System.out.println("defaultNXconvert foundNXconvert:" + foundNXconvert);
158  foundNXconvert = true;
159  }
160 
161  }
162 
163  private boolean chechExists(String filename){
164 
165  File file = null;
166  boolean exists = false;
167  if(filename!=null){
168 
169  if(!filename.equals("")){
170  file = new File(filename);
171  }
172  else{
173  return false;
174  }
175 
176  if(file.exists()){
177  exists = true;
178  }
179 
180  }
181  return exists;
182 
183  }
184 
185  private boolean testPath(String path){
186 
187  boolean result = false;
188 
189  if(path == null){
190  return result;
191  }
192  if(path.equals("")){
193  return result;
194  }
195 
196  try {
197  // execute the command
198  Runtime rt = Runtime.getRuntime();
199  Process proc = rt.exec(path + " --help");
200  result = true;
201  return result;
202  } catch (IOException ex) {
203  return result;
204  }
205 
206  }
207 
208 }