Revision 503

View differences:

org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.97/pom.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
	<modelVersion>4.0.0</modelVersion>
4

  
5
	<groupId>org.gvsig</groupId>
6
	<artifactId>org.gvsig.toolbox</artifactId>
7
	<packaging>pom</packaging>
8
	<name>${project.artifactId}</name>
9
	<version>1.0.97</version>
10

  
11
	<parent>
12
		<groupId>org.gvsig</groupId>
13
		<artifactId>org.gvsig.desktop</artifactId>
14
		<version>2.0.301</version>
15
	</parent>
16

  
17
	<scm>
18
        <connection>scm:svn:https://devel.gvsig.org/svn/gvsig-toolbox/org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.97</connection>
19
        <developerConnection>scm:svn:https://devel.gvsig.org/svn/gvsig-toolbox/org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.97</developerConnection>
20
        <url>https://devel.gvsig.org/redmine/projects/gvsig-toolbox/repository/show/org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.97</url>
21
    </scm>
22

  
23
	<repositories>
24
      <repository>
25
        <id>gvsig-public-http-repository</id>
26
        <name>gvSIG maven public HTTP repository</name>
27
        <url>http://devel.gvsig.org/m2repo/j2se</url>
28
        <releases>
29
          <enabled>true</enabled>
30
          <updatePolicy>daily</updatePolicy>
31
          <checksumPolicy>warn</checksumPolicy>
32
        </releases>
33
        <snapshots>
34
          <enabled>true</enabled>
35
          <updatePolicy>daily</updatePolicy>
36
          <checksumPolicy>warn</checksumPolicy>
37
        </snapshots>
38
      </repository>
39
    </repositories>
40

  
41
	<properties>
42
    	<maven.compiler.source>1.8</maven.compiler.source>
43
    	<maven.compiler.target>1.8</maven.compiler.target>
44
	</properties>
45

  
46
	 <build>
47

  
48
	    <pluginManagement>
49
	       <plugins>
50
           <!--
51
	          <plugin>
52
                <groupId>org.codehaus.mojo</groupId>
53
                <artifactId>animal-sniffer-maven-plugin</artifactId>
54
                <version>1.7</version>
55
              </plugin>
56
                         -->
57
	       </plugins>
58
	    </pluginManagement>
59

  
60
	    <plugins>
61
        	<plugin>
62
            	<groupId>org.apache.maven.plugins</groupId>
63
            	<artifactId>maven-compiler-plugin</artifactId>
64
            	<version>2.0.2</version>
65
            	<configuration>
66
                	<source>1.6</source>
67
                	<target>1.8</target>
68
                	<encoding>ISO-8859-1</encoding>
69
            	</configuration>
70
        	</plugin>
71
          <!--
72
        	<plugin>
73
              <groupId>org.codehaus.mojo</groupId>
74
              <artifactId>animal-sniffer-maven-plugin</artifactId>
75
              <executions>
76
                <execution>
77
                    <id>check-java-api</id>
78
                    <phase>test</phase>
79
                    <goals>
80
                        <goal>check</goal>
81
                    </goals>
82
                    <configuration>
83
                        <signature>
84
                            <groupId>org.codehaus.mojo.signature
85
                            </groupId>
86
                            <artifactId>java16</artifactId>
87
                            <version>1.0</version>
88
                        </signature>
89
                    </configuration>
90
                </execution>
91
              </executions>
92
             </plugin>
93
            -->
94
             <plugin>
95
                <groupId>org.apache.maven.plugins</groupId>
96
                <artifactId>maven-release-plugin</artifactId>
97
                <configuration>
98
                    <tagBase>https://devel.gvsig.org/svn/gvsig-toolbox/org.gvsig.toolbox/tags/</tagBase>
99
                    <goals>deploy</goals>
100
                </configuration>
101
            </plugin>
102

  
103
    	  </plugins>
104
    </build>
105

  
106
	<dependencyManagement>
107
      <dependencies>
108
		<dependency>
109
			<groupId>gishur</groupId>
110
			<artifactId>gishur_x</artifactId>
111
			<version>unknown</version>
112
		</dependency>
113
		<dependency>
114
			<groupId>gishur</groupId>
115
			<artifactId>gishur_core</artifactId>
116
			<version>unknown</version>
117
		</dependency>
118
		<dependency>
119
			<groupId>com.vividsolutions</groupId>
120
			<artifactId>jts</artifactId>
121
			<version>1.13</version>
122
		</dependency>
123
		<dependency>
124
			<groupId>org.gvsig</groupId>
125
			<artifactId>org.gvsig.toolbox.algorithm</artifactId>
126
			<version>1.0.97</version>
127
		</dependency>
128
		<dependency>
129
			<groupId>org.gvsig</groupId>
130
			<artifactId>org.gvsig.toolbox.core</artifactId>
131
			<version>1.0.97</version>
132
		</dependency>
133
		<dependency>
134
			<groupId>org.gvsig</groupId>
135
			<artifactId>org.gvsig.toolbox.gui</artifactId>
136
			<version>1.0.97</version>
137
		</dependency>
138
		<dependency>
139
			<groupId>org.gvsig</groupId>
140
			<artifactId>org.gvsig.toolbox.math</artifactId>
141
			<version>1.0.97</version>
142
		</dependency>
143
		<dependency>
144
			<groupId>gnu.trove</groupId>
145
			<artifactId>trove</artifactId>
146
			<version>0.1.8</version>
147
		</dependency>
148
		<dependency>
149
			  <groupId>org.gvsig.legacy</groupId>
150
			  <artifactId>jama</artifactId>
151
			  <version>1.0.2</version>
152
		</dependency>
153
        <dependency>
154
			<groupId>org.nfunk</groupId>
155
			<artifactId>jep</artifactId>
156
			<version>2.4.0</version>
157
		</dependency>
158
		<dependency>
159
			<groupId>commons-codec</groupId>
160
			<artifactId>commons-codec</artifactId>
161
			<version>1.8</version>
162
		</dependency>
163
		<dependency>
164
            <groupId>es.unex</groupId>
165
            <artifactId>sextante_wps</artifactId>
166
            <version>0.6.0</version>
167
        </dependency>
168
        <dependency>
169
			<groupId>es.unex</groupId>
170
			<artifactId>libDocEngines</artifactId>
171
			<version>0.6.0</version>
172
		</dependency>
173
		<dependency>
174
			<groupId>org.beanshell</groupId>
175
			<artifactId>bsh</artifactId>
176
			<version>2.0b4</version>
177
		</dependency>
178
        <dependency>
179
            <groupId>org.japura</groupId>
180
            <artifactId>org.japura</artifactId>
181
            <version>1.14.0</version>
182
        </dependency>
183
<!--		<dependency>
184
			<groupId>commons-codec</groupId>
185
			<artifactId>commons-codec</artifactId>
186
			<version>1.8</version>
187
		</dependency>-->
188
	  </dependencies>
189
	</dependencyManagement>
190

  
191
	 <modules>
192
		<module>org.gvsig.toolbox.math</module>
193
		<module>org.gvsig.toolbox.gui</module>
194
		<module>org.gvsig.toolbox.core</module>
195
		<module>org.gvsig.toolbox.algorithm</module>
196
	</modules>
197
</project>
0 198

  
org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.97/org.gvsig.toolbox.gui/MANIFEST.MF
1
Manifest-Version: 1.0
2
Ant-Version: Apache Ant 1.7.1
3
Created-By: 20.1-b02 (Sun Microsystems Inc.)
4
Implementation-Version: 0.7
5
Built-Date: 2013-03-04 16:06:37
6

  
0 7

  
org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.97/org.gvsig.toolbox.gui/src/main/java/jsh/shell/Utils.java
1
/*
2
 * 20:25:20 20/05/99
3
 *
4
 * The Java Shell: Utilities.
5
 * (C)1999 Romain Guy, Osvaldo Pinali Doederlein.
6
 *
7
 * LICENSE
8
 * =======
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 *
23
 * CHANGES
24
 * =======
25
 * 1.0.8 - Filled the listRoots method                   (Romain & Osvaldo)
26
 * 1.0.7 - Several bug fixes in constructPath            (Romain)
27
 * 1.0.6 - Split JDK1.1/1.2                              (Osvaldo)
28
 * 1.0.5 - Important bug fix in constructPath(String)    (Romain)
29
 * 1.0.4 - Added getSize(Enumeration)                    (Osvaldo)
30
 * 1.0.3 - Changed sortStrings bubble-sort algorithm to  (Romain)
31
 *         quick-sort (James Gosling)
32
 * 1.0.2 - Fixed two little bug in constructPath(String) (Romain)
33
 * 1.0.1 - Added listFiles(String[], boolean)            (Romain)
34
 *       - Removed unecessary createWhiteSpace(int)      (Romain)
35
 *       - Modified getWildCardMatches(String, boolean)  (Romain)
36
 *       - Slighty improved javadoc comments             (Romain)
37
 * 1.0.0 - Initial release.                              (Romain & Osvaldo)
38
 *
39
 * LINKS
40
 * =====
41
 * Contact: mailto@osvaldo.visionnaire.com.br
42
 * Site #1: http://www.geocities.com/ResearchTriangle/Node/2005/
43
 * Site #2: http://student.vub.ac.be/~opinalid/
44
 */
45

  
46
package jsh.shell;
47

  
48
import java.io.File;
49
import java.util.Enumeration;
50
import java.util.Vector;
51

  
52
import es.unex.sextante.gui.core.SextanteGUI;
53

  
54
/**
55
 * The Java Shell: Utility pack.
56
 *
57
 * @author Romain Guy.
58
 * @author Osvaldo Pinali Doederlein.
59
 */
60

  
61
public class Utils {
62

  
63
   /**
64
    * Removes some elements from a String array.
65
    *
66
    * @param arr
67
    *                The array.
68
    * @param first
69
    *                First element to keep (new 0-index).
70
    * @param last
71
    *                Last element to keep.
72
    * @return Copy of arr[first..]
73
    */
74
   public static String[] subarray(final String[] arr,
75
                                   int first,
76
                                   final int last) {
77
      final String[] newArr = new String[last - first + 1];
78

  
79
      for (int i = 0; i < newArr.length; ++i, ++first) {
80
         newArr[i] = arr[first];
81
      }
82

  
83
      return newArr;
84
   }
85

  
86

  
87
   /**
88
    * Removes some leading elements from a String array.
89
    *
90
    * @param arr
91
    *                The array.
92
    * @param first
93
    *                First element to keep (new 0-index).
94
    * @return Copy of arr[first..]
95
    */
96
   public static String[] subarray(final String[] arr,
97
                                   final int first) {
98
      return subarray(arr, first, arr.length - 1);
99
   }
100

  
101

  
102
   /**
103
    * Formats a number of bytes for output.
104
    *
105
    * @param bytes
106
    *                Number of bytes.
107
    * @return "xxxxK" form.
108
    */
109
   public static String fmtBytes(final long bytes) {
110
      return Long.toString(bytes / 1024) + "K";
111
   }
112

  
113

  
114
   /**
115
    * Formats time for output.
116
    *
117
    * @param bytes
118
    *                Number of milliseconds.
119
    * @return "x,yyys" form.
120
    */
121
   public static String fmtTime(final long ms) {
122
      return Float.toString(ms / 1000.0f) + "s";
123
   }
124

  
125

  
126
   /**
127
    * Return a String made of spaces.
128
    *
129
    * @param len
130
    *                Number of spaces
131
    */
132
   public static String getSpaces(final int len) {
133
      final StringBuffer buf = new StringBuffer();
134

  
135
      for (int i = 0; i < len; i++) {
136
         buf.append(' ');
137
      }
138

  
139
      return buf.toString();
140
   }
141

  
142

  
143
   public static String[] toArray(final Vector strings) {
144
      final String[] arr = new String[strings.size()];
145

  
146
      for (int i = 0; i < strings.size(); ++i) {
147
         arr[i] = (String) strings.elementAt(i);
148
      }
149

  
150
      return arr;
151
   }
152

  
153

  
154
   /**
155
    * When the user has to specify file names, he can use wildcards (*, ?). This methods handles the usage of these wildcards.
156
    *
157
    * @param s
158
    *                Wilcards
159
    * @param sort
160
    *                Set to true will sort file names
161
    * @return An array of String which contains all files matching <code>s</code> in current directory.
162
    * @see Utils#match(String, String)
163
    */
164

  
165
   public static String[] getWildCardMatches(final String s,
166
                                             final boolean sort) {
167
      final String args = new String(s.trim());
168
      String files[];
169
      final Vector filesThatMatchVector = new Vector();
170
      String filesThatMatch[];
171

  
172
      files = (new File(getUserDirectory())).list();
173

  
174
      for (int i = 0; i < files.length; i++) {
175
         if (match(args, files[i])) {
176
            final File temp = new File(getUserDirectory(), files[i]);
177
            filesThatMatchVector.addElement(new String(temp.getName()));
178
         }
179
      }
180

  
181
      filesThatMatch = new String[filesThatMatchVector.size()];
182
      filesThatMatchVector.copyInto(filesThatMatch);
183

  
184
      if (sort) {
185
         sortStrings(filesThatMatch);
186
      }
187

  
188
      return filesThatMatch;
189
   }
190

  
191

  
192
   /**
193
    * This method can determine if a String matches a pattern of wildcards
194
    *
195
    * @param pattern
196
    *                The pattern used for comparison
197
    * @param string
198
    *                The String to be checked
199
    * @return true if <code>string</code> matches <code>pattern</code>
200
    * @see Utils#getWildCardMatches(String)
201
    */
202

  
203
   public static boolean match(final String pattern,
204
                               final String string) {
205
      for (int p = 0;; p++) {
206
         for (int s = 0;; p++, s++) {
207
            final boolean sEnd = (s >= string.length());
208
            final boolean pEnd = (p >= pattern.length() || pattern.charAt(p) == '|');
209

  
210
            if (sEnd && pEnd) {
211
               return true;
212
            }
213
            if (sEnd || pEnd) {
214
               break;
215
            }
216
            if (pattern.charAt(p) == '?') {
217
               continue;
218
            }
219

  
220
            if (pattern.charAt(p) == '*') {
221
               int i;
222
               p++;
223

  
224
               for (i = string.length(); i >= s; --i) {
225
                  if (match(pattern.substring(p), string.substring(i))) {
226
                     return true;
227
                  }
228
               }
229

  
230
               break;
231
            }
232

  
233
            if (pattern.charAt(p) != string.charAt(s)) {
234
               break;
235
            }
236
         }
237

  
238
         p = pattern.indexOf('|', p);
239

  
240
         if (p == -1) {
241
            return false;
242
         }
243
      }
244
   }
245

  
246

  
247
   /**
248
    * Quick sort an array of Strings.
249
    *
250
    * @param string
251
    *                Strings to be sorted
252
    */
253

  
254
   public static void sortStrings(final String[] strings) {
255
      sortStrings(strings, 0, strings.length - 1);
256
   }
257

  
258

  
259
   /**
260
    * Quick sort an array of Strings.
261
    *
262
    * @param a
263
    *                Strings to be sorted
264
    * @param lo0
265
    *                Lower bound
266
    * @param hi0
267
    *                Higher bound
268
    */
269

  
270
   public static void sortStrings(final String a[],
271
                                  final int lo0,
272
                                  final int hi0) {
273
      int lo = lo0;
274
      int hi = hi0;
275
      String mid;
276

  
277
      if (hi0 > lo0) {
278
         mid = a[(lo0 + hi0) / 2];
279

  
280
         while (lo <= hi) {
281
            while (lo < hi0 && a[lo].compareTo(mid) < 0) {
282
               ++lo;
283
            }
284

  
285
            while (hi > lo0 && a[hi].compareTo(mid) > 0) {
286
               --hi;
287
            }
288

  
289
            if (lo <= hi) {
290
               swap(a, lo, hi);
291
               ++lo;
292
               --hi;
293
            }
294
         }
295

  
296
         if (lo0 < hi) {
297
            sortStrings(a, lo0, hi);
298
         }
299

  
300
         if (lo < hi0) {
301
            sortStrings(a, lo, hi0);
302
         }
303
      }
304
   }
305

  
306

  
307
   /**
308
    * Swaps two Strings.
309
    *
310
    * @param a
311
    *                The array to be swapped
312
    * @param i
313
    *                First String index
314
    * @param j
315
    *                Second String index
316
    */
317

  
318
   public static void swap(final String a[],
319
                           final int i,
320
                           final int j) {
321
      String T;
322
      T = a[i];
323
      a[i] = a[j];
324
      a[j] = T;
325
   }
326

  
327

  
328
   /**
329
    * Returns the user current directory.
330
    */
331
   public static String getUserDirectory() {
332

  
333
      String folder = SextanteGUI.getOutputFolder();
334
      if (folder == null) {
335
         folder = System.getProperty("user.home");
336
      }
337

  
338
      return folder;
339

  
340
   }
341

  
342

  
343
   /**
344
    * Sometimes, Strings are too long to be correctly displayed. This method will reduce a String, keeping first and last
345
    * characters.
346
    *
347
    * @param longString
348
    *                The String to be modified
349
    * @param maxLength
350
    *                The maximum length of the String
351
    * @return A shorter String
352
    */
353

  
354
   public static String getShortStringOf(final String longString,
355
                                         final int maxLength) {
356
      final int len = longString.length();
357

  
358
      if (len < maxLength) {
359
         return longString;
360
      }
361
      else {
362
         return longString.substring(0, maxLength / 2) + "..." + longString.substring(len - (maxLength / 2));
363
      }
364
   }
365

  
366

  
367
   /**
368
    * Because a lot of people still use JDK 1.1, we need this method to create an array of Files from an array of String.
369
    *
370
    * @param names
371
    *                Names of the files
372
    * @param construct
373
    *                Set it to true if names does not contain full paths
374
    * @return An array of Files
375
    */
376
   public static File[] listFiles(final String[] names,
377
                                  final boolean construct) {
378
      final File[] files = new File[names.length];
379

  
380
      String path = Utils.getUserDirectory();
381

  
382
      if (construct) {
383
         if (!path.endsWith(File.separator)) {
384
            path += File.separator;
385
         }
386
      }
387

  
388
      for (int i = 0; i < files.length; i++) {
389
         if (construct) {
390
            files[i] = new File(path + names[i]);
391
         }
392
         else {
393
            files[i] = new File(names[i]);
394
         }
395
      }
396

  
397
      return files;
398
   }
399

  
400

  
401
   /**
402
    * Counts things in an Enumeration (and destroys it as a side effect).
403
    *
404
    * @param enum
405
    *                The enumeration, in the start position.
406
    * @return Elements found.
407
    */
408
   public static int getSize(final Enumeration enu) {
409
      int size = 0;
410

  
411
      while (enu.hasMoreElements()) {
412
         ++size;
413
         enu.nextElement();
414
      }
415

  
416
      return size;
417
   }
418

  
419

  
420
   /**
421
    * Constructs a new path from current user path. This is an easy way to get a path if the user specified, for example,
422
    * "..\Java" as new path. This method will return the argument if this one is a path to a root (i.e, if <code>change</code>
423
    * is equal to C:\Jdk, constructPath will return C:\Jdk).
424
    *
425
    * @param change
426
    *                The modification to apply to the path
427
    * @see Utils#beginsWithRoot(String), Utils#getRoot(String)
428
    */
429

  
430
   public static String constructPath(final String change) {
431
      if (change == null || beginsWithRoot(change)) {
432
         return change;
433
      }
434

  
435
      String newPath = getUserDirectory();
436

  
437
      char current;
438
      char lastChar = '\0';
439
      boolean toAdd = false;
440
      StringBuffer buf = new StringBuffer(change.length());
441

  
442
      for (int i = 0; i < change.length(); i++) {
443
         switch ((current = change.charAt(i))) {
444
            case '.':
445
               if (lastChar == '.') {
446
                  final String parent = (new File(newPath)).getParent();
447
                  if (parent != null) {
448
                     newPath = parent;
449
                  }
450
               }
451
               else if (lastChar != '\0' && lastChar != '\\' && lastChar != '/') {
452
                  buf.append('.');
453
               }
454
               lastChar = '.';
455
               break;
456
            case '\\':
457
            case '/':
458
               if (lastChar == '\0') {
459
                  newPath = getRoot(newPath);
460
               }
461
               else {
462
                  if (!newPath.endsWith("\\")) {
463
                     newPath += File.separator + buf.toString();
464
                  }
465
                  else {
466
                     newPath += buf.toString();
467
                  }
468
                  buf = new StringBuffer();
469
                  toAdd = false;
470
               }
471
               lastChar = '\\';
472
               break;
473
            case '~':
474
               if (i < change.length() - 1) {
475
                  if (change.charAt(i + 1) == '\\' || change.charAt(i + 1) == '/') {
476
                     newPath = System.getProperties().getProperty("user.home");
477
                  }
478
                  else {
479
                     buf.append('~');
480
                  }
481
               }
482
               else if (i == 0) {
483
                  newPath = System.getProperties().getProperty("user.home");
484
               }
485
               else {
486
                  buf.append('~');
487
               }
488
               lastChar = '~';
489
               break;
490
            default:
491
               lastChar = current;
492
               buf.append(current);
493
               toAdd = true;
494
               break;
495
         }
496
      }
497

  
498
      if (toAdd) {
499
         if (!newPath.endsWith(File.separator)) {
500
            newPath += File.separator + buf.toString();
501
         }
502
         else {
503
            newPath += buf.toString();
504
         }
505
      }
506

  
507
      return newPath;
508
   }
509

  
510

  
511
   /**
512
    * It can be necessary to determine which is the root of a path. For example, the root of D:\Projects\Java is D:\.
513
    *
514
    * @param path
515
    *                The path used to get a root
516
    * @return The root which contais the specified path
517
    */
518

  
519
   public static String getRoot(final String path) {
520
      final File roots[] = listRoots(new File(path));
521

  
522
      for (int i = 0; i < roots.length; i++) {
523
         if (path.startsWith(roots[i].getPath())) {
524
            return roots[i].getPath();
525
         }
526
      }
527

  
528
      return path;
529
   }
530

  
531

  
532
   /**
533
    * It can be necessary to determine if a path begins with a root.
534
    *
535
    * @param path
536
    *                The path to check
537
    * @return True if path begins with a root, false otherwise
538
    */
539

  
540
   public static boolean beginsWithRoot(final String path) {
541
      final File roots[] = listRoots(new File(path));
542

  
543
      for (int i = 0; i < roots.length; i++) {
544
         if (path.regionMatches(true, 0, roots[i].getPath(), 0, roots[i].getPath().length())) {
545
            return true;
546
         }
547
      }
548

  
549
      return false;
550
   }
551

  
552

  
553
   /**
554
    * We override a Java2 specific method.
555
    *
556
    * @param f
557
    *                A File
558
    * @return A list of standards roots
559
    */
560

  
561
   public static File[] listRoots(final File f) {
562
      if (System.getProperty("os.name").startsWith("Windows")) {
563
         return new File[] { new File("A:\\"), new File("B:\\"), new File("C:\\"), new File("D:\\"), new File("E:\\"),
564
                  new File("F:\\"), new File("G:\\"), new File("H:\\"), new File("I:\\") };
565
         //return new File[]{new File(System.getProperty("java.home").substring(0, 3))};
566
      }
567
      else {
568
         return new File[] { new File("/") };
569
      }
570
   }
571

  
572

  
573
   /**
574
    * We override a Java2 spcific method.
575
    *
576
    * @param file
577
    *                Determine if this file is hidden or not
578
    * @return Always false
579
    */
580

  
581
   public boolean isHidden(final File file) {
582
      return false;
583
   }
584

  
585
}
586

  
587
// End of Utils.java
0 588

  
org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.97/org.gvsig.toolbox.gui/src/main/java/es/unex/sextante/gui/additionalResults/TableTools.java
1
package es.unex.sextante.gui.additionalResults;
2

  
3
import javax.swing.JScrollPane;
4
import javax.swing.JTable;
5
import javax.swing.table.DefaultTableModel;
6
import javax.swing.table.TableModel;
7

  
8
import es.unex.sextante.dataObjects.IDataObject;
9
import es.unex.sextante.dataObjects.IFeature;
10
import es.unex.sextante.dataObjects.IFeatureIterator;
11
import es.unex.sextante.dataObjects.IRecord;
12
import es.unex.sextante.dataObjects.IRecordsetIterator;
13
import es.unex.sextante.dataObjects.ITable;
14
import es.unex.sextante.dataObjects.IVectorLayer;
15
import es.unex.sextante.exceptions.IteratorException;
16

  
17
public class TableTools {
18

  
19
   public static JScrollPane getScrollableTablePanelFromITable(final IDataObject obj) {
20

  
21
      final JScrollPane jScrollPane = new JScrollPane();
22

  
23
      final JTable jTable = new JTable();
24
      jScrollPane.setViewportView(jTable);
25
      final TableModel model = getTableModel(obj);
26
      if (model != null) {
27
         jTable.setModel(model);
28
      }
29
      jTable.setEnabled(false);
30

  
31
      return jScrollPane;
32

  
33

  
34
   }
35

  
36

  
37
   private static TableModel getTableModel(final IDataObject obj) {
38

  
39
      final DefaultTableModel model = new DefaultTableModel();
40

  
41
      String[] fields;
42
      int iCount;
43

  
44

  
45
      if (obj instanceof ITable) {
46
         iCount = (int) ((ITable) obj).getRecordCount();
47
         fields = ((ITable) obj).getFieldNames();
48

  
49
      }
50
      else if (obj instanceof IVectorLayer) {
51
         iCount = ((IVectorLayer) obj).getShapesCount();
52
         fields = ((IVectorLayer) obj).getFieldNames();
53
      }
54
      else {
55
         return null;
56
      }
57

  
58
      final String[][] data = new String[iCount][fields.length];
59

  
60
      if (obj instanceof ITable) {
61
         final IRecordsetIterator iter = ((ITable) obj).iterator();
62
         int i = 0;
63
         while (iter.hasNext()) {
64
            IRecord record;
65
            try {
66
               record = iter.next();
67
               for (int j = 0; j < fields.length; j++) {
68
                  data[i][j] = record.getValue(j).toString();
69
               }
70
               i++;
71
            }
72
            catch (final IteratorException e) {
73
               e.printStackTrace();
74
            }
75
         }
76
      }
77
      else {
78
         final IFeatureIterator iter = ((IVectorLayer) obj).iterator();
79
         int i = 0;
80
         while (iter.hasNext()) {
81
            IFeature feature;
82
            try {
83
               feature = iter.next();
84
               for (int j = 0; j < fields.length; j++) {
85
                  data[i][j] = feature.getRecord().getValue(j).toString();
86
               }
87
               i++;
88
            }
89
            catch (final IteratorException e) {
90
               e.printStackTrace();
91
            }
92
         }
93
      }
94

  
95
      model.setDataVector(data, fields);
96

  
97
      return model;
98

  
99
   }
100

  
101

  
102
}
0 103

  
org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.97/org.gvsig.toolbox.gui/src/main/java/es/unex/sextante/gui/additionalResults/AdditionalResults.java
1
package es.unex.sextante.gui.additionalResults;
2

  
3
import java.util.ArrayList;
4

  
5
import es.unex.sextante.core.ObjectAndDescription;
6
import es.unex.sextante.gui.core.SextanteGUI;
7

  
8
/**
9
 * This class stores results generated by SEXTANTE geoalgorithms that are neither layers nor tables (i.e text results and charts).
10
 * They are all stored as java Component objects, so they can be shown in a dialogs. Charts do not have to be modified, since they
11
 * already extend the Component class. Text strings should be put into some kind of panel or TextArea. This is not done by this
12
 * class, but should be done by the corresponding post-process task (see {IPostProcessFactory})
13
 *
14
 * @author volaya
15
 *
16
 */
17
public class AdditionalResults {
18

  
19
   private static ArrayList m_Components = new ArrayList();
20

  
21

  
22
   /**
23
    * Shows a panel with results, only if there is at least one of them,
24
    */
25
   public static void showPanel() {
26

  
27
      if (m_Components == null || m_Components.size() == 0) {
28
         return;
29
      }
30

  
31
      SextanteGUI.getGUIFactory().showAdditionalResultsDialog(m_Components);
32

  
33
   }
34

  
35

  
36
   /**
37
    * Returns the list of results as a list of components
38
    *
39
    * @return the list of results
40
    */
41
   public static ArrayList getComponents() {
42

  
43
      return m_Components;
44

  
45
   }
46

  
47

  
48
   /**
49
    * Adds a new result
50
    *
51
    * @param oad
52
    *                a result and its description, to be shown in the corresponding panel
53
    */
54
   public static void addComponent(final ObjectAndDescription oad) {
55

  
56
      m_Components.add(oad);
57

  
58

  
59
   }
60

  
61

  
62
   /**
63
    * Removes a result
64
    *
65
    * @param oad
66
    *                The result to remove
67
    */
68
   public static void removeComponent(final ObjectAndDescription oad) {
69

  
70
      m_Components.remove(oad);
71

  
72
   }
73

  
74
}
0 75

  
org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.97/org.gvsig.toolbox.gui/src/main/java/es/unex/sextante/gui/additionalResults/AdditionalResultsDialog.java
1

  
2

  
3
package es.unex.sextante.gui.additionalResults;
4

  
5
import java.awt.BorderLayout;
6
import java.awt.Component;
7
import java.awt.Dimension;
8
import java.awt.Frame;
9
import java.awt.event.ActionEvent;
10
import java.awt.event.ActionListener;
11
import java.awt.event.MouseAdapter;
12
import java.awt.event.MouseEvent;
13
import java.awt.event.MouseListener;
14
import java.io.BufferedWriter;
15
import java.io.File;
16
import java.io.FileWriter;
17
import java.util.ArrayList;
18
import java.util.Enumeration;
19

  
20
import javax.swing.JDialog;
21
import javax.swing.JFileChooser;
22
import javax.swing.JMenuItem;
23
import javax.swing.JOptionPane;
24
import javax.swing.JPanel;
25
import javax.swing.JPopupMenu;
26
import javax.swing.JScrollPane;
27
import javax.swing.JSplitPane;
28
import javax.swing.JTable;
29
import javax.swing.JTextPane;
30
import javax.swing.JTree;
31
import javax.swing.ScrollPaneConstants;
32
import javax.swing.table.TableModel;
33
import javax.swing.tree.DefaultMutableTreeNode;
34
import javax.swing.tree.DefaultTreeModel;
35
import javax.swing.tree.TreePath;
36

  
37
import org.jfree.chart.ChartPanel;
38
import org.jfree.chart.ChartUtilities;
39

  
40
import es.unex.sextante.core.ObjectAndDescription;
41
import es.unex.sextante.core.Sextante;
42
import es.unex.sextante.gui.algorithm.GenericFileFilter;
43

  
44

  
45
public class AdditionalResultsDialog
46
         extends
47
            JDialog {
48

  
49
   private JSplitPane  jSplitPane;
50
   private JTree       jTree;
51
   private TreePath    m_Path;
52
   private JScrollPane jScrollPane;
53
   private JMenuItem   menuItemSave;
54
   private JPopupMenu  popupMenu;
55
   private JMenuItem   menuItemRemove;
56
   private JMenuItem   menuItemRename;
57

  
58

  
59
   public AdditionalResultsDialog(final ArrayList components,
60
                                  final Frame mainFrame) {
61

  
62
      super(mainFrame, Sextante.getText("Result"), true);
63

  
64
      initGUI(components);
65
      setLocationRelativeTo(null);
66

  
67
   }
68

  
69

  
70
   private boolean initGUI(final ArrayList components) {
71

  
72
      final JPanel panel = new JPanel();
73
      final BorderLayout thisLayout = new BorderLayout();
74
      panel.setLayout(thisLayout);
75
      this.setContentPane(panel);
76

  
77
      if (components.size() == 0) {
78
         return false;
79
      }
80
      try {
81
         {
82
            this.setPreferredSize(new java.awt.Dimension(700, 350));
83
            this.setSize(new java.awt.Dimension(700, 350));
84
            {
85
               jSplitPane = new JSplitPane();
86
               panel.add(jSplitPane, BorderLayout.CENTER);
87
               {
88
                  jTree = new JTree();
89
                  jTree.setCellRenderer(new AdditionalResultsTreeCellRenderer());
90
                  final MouseListener ml = new MouseAdapter() {
91
                     @Override
92
                     public void mousePressed(MouseEvent e) {
93
                        m_Path = jTree.getPathForLocation(e.getX(), e.getY());
94
                        showComponent();
95
                        if ((e.getButton() == MouseEvent.BUTTON3) && (m_Path != null)) {
96
                           DefaultMutableTreeNode node = (DefaultMutableTreeNode) m_Path.getLastPathComponent();
97
                           if (node.getUserObject() instanceof ObjectAndDescription) {
98
                              showPopupMenu(e);
99
                           }
100
                        }
101
                     }
102
                  };
103
                  jTree.addMouseListener(ml);
104

  
105
                  fillTree(components);
106

  
107
                  if (components.size() > 0) {
108
                     final DefaultMutableTreeNode node = findNode((ObjectAndDescription) components.get(components.size() - 1));
109
                     final DefaultTreeModel model = (DefaultTreeModel) jTree.getModel();
110
                     final TreePath path = new TreePath(model.getPathToRoot(node));
111
                     jTree.setSelectionPath(path);
112
                     jTree.scrollPathToVisible(path);
113
                     m_Path = path;
114
                     showComponent();
115
                  }
116

  
117
                  jScrollPane = new JScrollPane(jTree, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
118
                           ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
119

  
120
                  jScrollPane.setPreferredSize(new Dimension(200, 450));
121
                  jScrollPane.setMinimumSize(new Dimension(200, 450));
122
                  jScrollPane.setMaximumSize(new Dimension(200, 450));
123
               }
124
               {
125
                  jSplitPane.add(jScrollPane, JSplitPane.LEFT);
126
               }
127
            }
128
         }
129

  
130
         popupMenu = new JPopupMenu("Menu");
131

  
132
         menuItemSave = new JMenuItem(Sextante.getText("Save"));
133
         menuItemSave.addActionListener(new ActionListener() {
134
            public void actionPerformed(final ActionEvent evt) {
135
               if (m_Path != null) {
136
                  try {
137
                     final DefaultMutableTreeNode node = (DefaultMutableTreeNode) m_Path.getLastPathComponent();
138
                     if (node.getUserObject() instanceof ObjectAndDescription) {
139
                        save();
140
                     }
141
                  }
142
                  catch (final Exception e) {
143
                  }
144
               }
145
            }
146
         });
147
         popupMenu.add(menuItemSave);
148

  
149
         menuItemRemove = new JMenuItem(Sextante.getText("Remove"));
150
         menuItemRemove.addActionListener(new ActionListener() {
151
            public void actionPerformed(final ActionEvent evt) {
152
               if (m_Path != null) {
153
                  try {
154
                     final DefaultMutableTreeNode node = (DefaultMutableTreeNode) m_Path.getLastPathComponent();
155
                     if (node.getUserObject() instanceof ObjectAndDescription) {
156
                        remove();
157
                     }
158
                  }
159
                  catch (final Exception e) {
160
                  }
161
               };
162
            }
163
         });
164
         popupMenu.add(menuItemRemove);
165

  
166
         menuItemRename = new JMenuItem(Sextante.getText("Rename"));
167
         menuItemRename.addActionListener(new ActionListener() {
168
            public void actionPerformed(final ActionEvent evt) {
169
               if (m_Path != null) {
170
                  try {
171
                     final DefaultMutableTreeNode node = (DefaultMutableTreeNode) m_Path.getLastPathComponent();
172
                     if (node.getUserObject() instanceof ObjectAndDescription) {
173
                        rename();
174
                     }
175
                  }
176
                  catch (final Exception e) {
177
                  }
178
               };
179
            }
180

  
181
         });
182
         popupMenu.add(menuItemRename);
183

  
184
         panel.updateUI();
185
         return true;
186
      }
187
      catch (final Exception e) {
188
         Sextante.addErrorToLog(e);
189
         return false;
190
      }
191

  
192
   }
193

  
194

  
195
   protected void showPopupMenu(final MouseEvent e) {
196

  
197
      jTree.setSelectionPath(m_Path);
198

  
199
      final DefaultMutableTreeNode node = (DefaultMutableTreeNode) m_Path.getLastPathComponent();
200
      final ObjectAndDescription oad = (ObjectAndDescription) node.getUserObject();
201
      final Component c = (Component) oad.getObject();
202
      menuItemSave.setEnabled(true);
203
      popupMenu.show(e.getComponent(), e.getX(), e.getY());
204

  
205
   }
206

  
207

  
208
   private void rename() {
209

  
210
      if (m_Path != null) {
211
         try {
212
            final DefaultMutableTreeNode node = (DefaultMutableTreeNode) m_Path.getLastPathComponent();
213
            final ObjectAndDescription oad = (ObjectAndDescription) node.getUserObject();
214
            final String sName = oad.getDescription();
215

  
216
            final JOptionPane pane = new JOptionPane();
217
            pane.setMessage(Sextante.getText("introduce_nombre"));
218
            pane.setMessageType(JOptionPane.QUESTION_MESSAGE);
219
            pane.setWantsInput(true);
220
            pane.setInitialSelectionValue(sName);
221
            pane.setInputValue(sName);
222
            final JDialog dlg = pane.createDialog(null, Sextante.getText("renombrar"));
223
            dlg.setModal(true);
224
            dlg.setVisible(true);
225

  
226
            final String sNewName = pane.getInputValue().toString().trim();
227

  
228
            if ((sNewName != null) && (sNewName.length() != 0)) {
229
               oad.setDescription(sNewName);
230
            }
231

  
232
            update();
233
         }
234
         catch (final Exception e) {
235
         }
236
      }
237

  
238

  
239
   }
240

  
241

  
242
   protected void remove() {
243

  
244
      if (m_Path != null) {
245
         try {
246
            final DefaultMutableTreeNode node = (DefaultMutableTreeNode) m_Path.getLastPathComponent();
247
            final ObjectAndDescription oad = (ObjectAndDescription) node.getUserObject();
248
            AdditionalResults.removeComponent(oad);
249
            update();
250
         }
251
         catch (final Exception e) {
252
         }
253
      }
254

  
255
   }
256

  
257

  
258
   protected void save() {
259

  
260
      if (m_Path != null) {
261
         try {
262
            final DefaultMutableTreeNode node = (DefaultMutableTreeNode) m_Path.getLastPathComponent();
263
            final ObjectAndDescription oad = (ObjectAndDescription) node.getUserObject();
264
            final Component c = (Component) oad.getObject();
265
            if (c instanceof JScrollPane) {
266
               final JScrollPane pane = (JScrollPane) c;
267
               final Component view = pane.getViewport().getView();
268
               if (view instanceof JTextPane) {
269
                  final JTextPane text = (JTextPane) pane.getViewport().getView();
270
                  final JFileChooser fc = new JFileChooser();
271
                  fc.setFileFilter(new GenericFileFilter(new String[] { "htm" }, "HTML"));
272
                  final int returnVal = fc.showSaveDialog(this);
273
                  if (returnVal == JFileChooser.APPROVE_OPTION) {
274
                     try {
275
                        final File file = fc.getSelectedFile();
276
                        final FileWriter fileWriter = new FileWriter(file);
277
                        final BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
278
                        bufferedWriter.write(text.getText());
279
                        bufferedWriter.close();
280
                     }
281
                     catch (final Exception e) {
282
                        JOptionPane.showMessageDialog(null, "Could not save selected result", Sextante.getText("Warning"),
283
                                 JOptionPane.WARNING_MESSAGE);
284
                     }
285
                  }
286
               }
287
               else if (view instanceof JTable) {
288
                  final JTable table = (JTable) pane.getViewport().getView();
289
                  final TableModel model = table.getModel();
290
                  final JFileChooser fc = new JFileChooser();
291
                  fc.setFileFilter(new GenericFileFilter(new String[] { "csv" }, "Comma-Separated Values (csv)"));
292
                  final int returnVal = fc.showSaveDialog(this);
293
                  if (returnVal == JFileChooser.APPROVE_OPTION) {
294
                     try {
295
                        final File file = fc.getSelectedFile();
296
                        final FileWriter fileWriter = new FileWriter(file);
297
                        final BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
298
                        for (int j = 0; j < model.getColumnCount(); j++) {
299
                           bufferedWriter.write(model.getColumnName(j));
300
                           if (j == model.getColumnCount() - 1) {
301
                              bufferedWriter.write("\n");
302
                           }
303
                           else {
304
                              bufferedWriter.write(",");
305
                           }
306
                        }
307
                        for (int i = 0; i < model.getRowCount(); i++) {
308
                           for (int j = 0; j < model.getColumnCount(); j++) {
309
                              bufferedWriter.write(model.getValueAt(i, j).toString());
310
                              if (j == model.getColumnCount() - 1) {
311
                                 bufferedWriter.write("\n");
312
                              }
313
                              else {
314
                                 bufferedWriter.write(",");
315
                              }
316
                           }
317
                        }
318
                        bufferedWriter.close();
319
                     }
320
                     catch (final Exception e) {
321
                        JOptionPane.showMessageDialog(null, "Could not save selected result", Sextante.getText("Warning"),
322
                                 JOptionPane.WARNING_MESSAGE);
323
                     }
324
                  }
325
               }
326
            }
327
            else if (c instanceof ChartPanel) {
328
               final ChartPanel panel = (ChartPanel) c;
329
               final JFileChooser fc = new JFileChooser();
330
               fc.setFileFilter(new GenericFileFilter(new String[] { "png" }, "png"));
331
               final int returnVal = fc.showSaveDialog(this);
332
               if (returnVal == JFileChooser.APPROVE_OPTION) {
333
                  ChartUtilities.saveChartAsPNG(fc.getSelectedFile(), panel.getChart(), 600, 350);
334
               }
335
            }
336

  
337
         }
338
         catch (final Exception e) {
339
            Sextante.addErrorToLog(e);
340
         }
341
      }
342

  
343
   }
344

  
345

  
346
   protected void showComponent() {
347

  
348
      if (m_Path != null) {
349
         try {
350
            final DefaultMutableTreeNode node = (DefaultMutableTreeNode) m_Path.getLastPathComponent();
351
            final ObjectAndDescription oad = (ObjectAndDescription) node.getUserObject();
352
            final Component c = (Component) oad.getObject();
353
            c.setMinimumSize(new Dimension(300, 200));
354
            jSplitPane.setRightComponent(c);
355
         }
356
         catch (final Exception e) {
357
            Sextante.addErrorToLog(e);
358
         }
359

  
360
      }
361

  
362
   }
363

  
364

  
365
   public void fillTree(final ArrayList components) {
366

  
367
      DefaultMutableTreeNode node;
368
      final DefaultMutableTreeNode mainNode = new DefaultMutableTreeNode(Sextante.getText("Toolbox"));;
369
      final DefaultMutableTreeNode componentsNode = new DefaultMutableTreeNode(Sextante.getText("Result"));
370

  
371
      for (int i = 0; i < components.size(); i++) {
372
         node = new DefaultMutableTreeNode(components.get(i));
373
         componentsNode.add(node);
374
      }
375

  
376
      mainNode.add(componentsNode);
377

  
378
      jTree.setModel(new DefaultTreeModel(mainNode));
379

  
380
   }
381

  
382

  
383
   public void update() {
384

  
385
      if (!initGUI(AdditionalResults.getComponents())) {
386
         dispose();
387
         setVisible(false);
388
      }
389

  
390
   }
391

  
392

  
393
   private DefaultMutableTreeNode findNode(final ObjectAndDescription oad) {
394

  
395
      Object ob;
396
      final DefaultTreeModel data = (DefaultTreeModel) jTree.getModel();
397
      final DefaultMutableTreeNode root = (DefaultMutableTreeNode) data.getRoot();
398
      DefaultMutableTreeNode node = null;
399

  
400
      if (root != null) {
401
         for (final Enumeration e = root.breadthFirstEnumeration(); e.hasMoreElements();) {
402
            final DefaultMutableTreeNode current = (DefaultMutableTreeNode) e.nextElement();
403
            ob = current.getUserObject();
404
            if (ob instanceof ObjectAndDescription) {
405
               if (ob == oad) {
406
                  node = current;
407
                  break;
408
               }
409
            }
410
         }
411
      }
412

  
413
      return node;
414

  
415
   }
416

  
417
}
0 418

  
org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.97/org.gvsig.toolbox.gui/src/main/java/es/unex/sextante/gui/additionalResults/AdditionalResultsTreeCellRenderer.java
1
package es.unex.sextante.gui.additionalResults;
2

  
3
import java.awt.Color;
4
import java.awt.Component;
5

  
6
import javax.swing.JLabel;
7
import javax.swing.JTree;
8
import javax.swing.tree.TreeCellRenderer;
9

  
10
public class AdditionalResultsTreeCellRenderer
11
         extends
12
            JLabel
13
         implements
14
            TreeCellRenderer {
15

  
16
   public Component getTreeCellRendererComponent(final JTree tree,
17
                                                 final Object value,
18
                                                 final boolean sel,
19
                                                 final boolean expanded,
20
                                                 boolean leaf,
21
                                                 final int row,
22
                                                 final boolean hasFocus) {
23

  
24
      final String sName = tree.convertValueToText(value, sel, expanded, leaf, row, hasFocus);
25

  
26
      setFont(tree.getFont());
27
      setEnabled(tree.isEnabled());
28
      setText(sName);
29

  
30
      if (!leaf) {
31
         setFont(new java.awt.Font("Tahoma", 1, 11));
32
         setForeground(Color.black);
33
      }
34
      else {
35
         if (sel) {
36
            setForeground(Color.blue);
37
         }
38
         else {
39
            setForeground(Color.black);
40
         }
41
      }
42
      return this;
43

  
44
   }
45

  
46
}
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff