Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.gui / src / main / java / es / unex / sextante / gui / toolbox / AlgorithmGroupsOrganizer.java @ 412

History | View | Annotate | Download (3.85 KB)

1
package es.unex.sextante.gui.toolbox;
2

    
3
import java.io.BufferedReader;
4
import java.io.BufferedWriter;
5
import java.io.File;
6
import java.io.FileReader;
7
import java.io.FileWriter;
8
import java.io.IOException;
9
import java.io.Writer;
10
import java.util.HashMap;
11
import java.util.Iterator;
12
import java.util.Set;
13

    
14
import es.unex.sextante.core.GeoAlgorithm;
15
import es.unex.sextante.core.Sextante;
16
import es.unex.sextante.gui.core.SextanteGUI;
17
import org.slf4j.Logger;
18
import org.slf4j.LoggerFactory;
19

    
20
public class AlgorithmGroupsOrganizer {
21

    
22
    private static final Logger LOG = LoggerFactory.getLogger(AlgorithmGroupsOrganizer.class);
23

    
24
   private static HashMap<String, AlgorithmGroupConfiguration> m_Map = null;
25

    
26
    private static void readConfigFile() {
27

    
28
        BufferedReader input = null;
29
        String fname = "(unknow)";
30
        try {
31
            fname = getConfigFile();
32
            File ffile = new File(fname);
33
            if (!ffile.exists()) {
34
                return;
35
            }
36
            FileReader freader = new FileReader(fname);
37
            input = new BufferedReader(freader);
38
            String sLine = null;
39
            while ((sLine = input.readLine()) != null) {
40
                final String[] sTokens = sLine.split("@");
41
                String sName = "(unknow)";
42
                try {
43
                    sName = sTokens[0];
44
                    final AlgorithmGroupConfiguration conf = AlgorithmGroupConfiguration.fromString(sTokens[1]);
45
                    if (conf != null) {
46
                        getGrouppingMap().put(sName, conf);
47
                    }
48
                } catch (final Exception e) {
49
                    LOG.warn("Can't read configuration of '"+sName+"'.", e);
50
                }
51
            }
52
        } catch (final Exception e) {
53
            LOG.warn("Can't read config file '"+fname+"'.",e);
54
        } finally {
55
            try {
56
                if (input != null) {
57
                    input.close();
58
                }
59
            } catch (final IOException e) {
60
                Sextante.addErrorToLog(e);
61
            }
62
        }
63

    
64
    }
65

    
66

    
67
   public static void saveSettings() {
68

    
69
      Writer output = null;
70
      try {
71
         output = new BufferedWriter(new FileWriter(getConfigFile()));
72
         final Set<String> set = getGrouppingMap().keySet();
73
         final Iterator<String> iter = set.iterator();
74
         while (iter.hasNext()) {
75
            final String sKey = iter.next();
76
            final AlgorithmGroupConfiguration conf = getGrouppingMap().get(sKey);
77
            output.write(sKey + "@" + conf.toString() + "\n");
78
         }
79
      }
80
      catch (final IOException e) {
81
         Sextante.addErrorToLog(e);
82
      }
83
      finally {
84
         if (output != null) {
85
            try {
86
               output.close();
87
            }
88
            catch (final IOException e) {
89
               Sextante.addErrorToLog(e);
90
            }
91
         }
92
      }
93

    
94
   }
95

    
96

    
97
   public static HashMap<String, AlgorithmGroupConfiguration> getGrouppingMap() {
98
      if( m_Map == null ) { 
99
        m_Map = new HashMap<String, AlgorithmGroupConfiguration>();
100
        readConfigFile();
101
      }
102
      return m_Map;
103

    
104
   }
105

    
106

    
107
   private static String getConfigFile() {
108

    
109
//      String sPath = System.getProperty("user.home") + File.separator + "sextante";
110
      String sPath = SextanteGUI.getSextanteHomePath();
111
      
112
      sPath = sPath + File.separator + "sextante_alg_groups.settings";
113

    
114
      return sPath;
115

    
116
   }
117

    
118

    
119
   public static void setConfiguration(final HashMap<String, AlgorithmGroupConfiguration> map) {
120
      m_Map = map;
121
   }
122

    
123

    
124
   public static AlgorithmGroupConfiguration getGroupConfiguration(final GeoAlgorithm alg) {
125

    
126

    
127
      return getGrouppingMap().get(alg.getCommandLineName());
128

    
129
   }
130

    
131

    
132
   public static void restore() {
133

    
134
      getGrouppingMap().clear();
135

    
136
   }
137

    
138
}