Revision 42553

View differences:

tags/org.gvsig.desktop-2.0.116/org.gvsig.desktop.library/org.gvsig.utils/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.tools.IverUtilesLibrary
tags/org.gvsig.desktop-2.0.116/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/tools/IverUtilesLibrary.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools;
25

  
26
import org.gvsig.tools.library.AbstractLibrary;
27
import org.gvsig.tools.library.LibraryException;
28
import org.gvsig.utils.swing.jcomboServer.ServerData;
29

  
30
public class IverUtilesLibrary extends AbstractLibrary {
31

  
32
    @Override
33
    public void doRegistration() {
34
        registerAsAPI(IverUtilesLibrary.class);
35
    }
36

  
37
	@Override
38
	protected void doInitialize() throws LibraryException {
39
		// Nothing to do
40
	}
41

  
42
	@Override
43
	protected void doPostInitialize() throws LibraryException {
44
		ServerData.registerPersistence();
45
	}
46
}
0 47

  
tags/org.gvsig.desktop-2.0.116/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/tools/backup/BackupGenerator.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.backup;
25

  
26
/* gvSIG. Geographic Information System of the Valencian Government
27
 *
28
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
29
 * of the Valencian Government (CIT)
30
 * 
31
 * This program is free software; you can redistribute it and/or
32
 * modify it under the terms of the GNU General Public License
33
 * as published by the Free Software Foundation; either version 2
34
 * of the License, or (at your option) any later version.
35
 * 
36
 * This program is distributed in the hope that it will be useful,
37
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39
 * GNU General Public License for more details.
40
 *  
41
 * You should have received a copy of the GNU General Public License
42
 * along with this program; if not, write to the Free Software
43
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
44
 * MA  02110-1301, USA.
45
 * 
46
 */
47

  
48
import java.io.File;
49

  
50
import org.gvsig.tools.backup.exceptions.BackupException;
51

  
52

  
53
/**
54
 * <p>A tagging interface that all file backups must implement.</p>
55
 *
56
 * @author Jose Ignacio Yarza (jiyarza@opensistemas.com)
57
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
58
 */
59
public interface BackupGenerator {
60
	/**
61
	 * <p>Performs a backup of <code>source</code>.</p>
62
	 * 
63
	 * @param source the source file
64
	 */
65
	public void backup(File source) throws BackupException;
66
}
0 67

  
tags/org.gvsig.desktop-2.0.116/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/tools/backup/DefaultBackupGenerator.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.backup;
25

  
26
/* gvSIG. Geographic Information System of the Valencian Government
27
 *
28
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
29
 * of the Valencian Government (CIT)
30
 * 
31
 * This program is free software; you can redistribute it and/or
32
 * modify it under the terms of the GNU General Public License
33
 * as published by the Free Software Foundation; either version 2
34
 * of the License, or (at your option) any later version.
35
 * 
36
 * This program is distributed in the hope that it will be useful,
37
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39
 * GNU General Public License for more details.
40
 *  
41
 * You should have received a copy of the GNU General Public License
42
 * along with this program; if not, write to the Free Software
43
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
44
 * MA  02110-1301, USA.
45
 * 
46
 */ 
47

  
48
import java.io.File;
49
import java.io.FileInputStream;
50
import java.io.FileOutputStream;
51
import java.nio.channels.FileChannel;
52

  
53
import org.gvsig.tools.backup.exceptions.BackupException;
54

  
55

  
56
/**
57
 * <p>Performs a backup of a file, into another file at the same path (directory), with the file extension 
58
 *  changed to <i>.bak</i>.</p>
59
 *
60
 * @author Jose Ignacio Yarza (jiyarza@opensistemas.com)
61
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
62
 */
63
public class DefaultBackupGenerator implements BackupGenerator {
64
	/*
65
	 * (non-Javadoc)
66
	 * @see com.iver.utiles.backup.BackupGenerator#backup(java.io.File)
67
	 */
68
	public void backup(File source) throws BackupException {
69
		try {
70
			int index = source.getAbsolutePath().lastIndexOf(".");
71

  
72
			if (index == -1)
73
				return;
74

  
75
			File dest = new File(source.getAbsolutePath().substring(0, index) + ".bak");
76

  
77
	        // Create channel on the source
78
	        FileChannel srcChannel = new FileInputStream(source).getChannel();
79

  
80
	        // Create channel on the destination
81
	        FileChannel dstChannel = new FileOutputStream(dest).getChannel();
82

  
83
	        // Copy file contents from source to destination
84
	        dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
85

  
86
	        // Close the channels
87
	        srcChannel.close();
88
	        dstChannel.close();
89
	    } catch (Exception ex) {
90
	    	throw new BackupException(ex.getMessage(), ex, source);
91
	    }
92
	}
93
}
0 94

  
tags/org.gvsig.desktop-2.0.116/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/tools/backup/exceptions/BackupException.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.backup.exceptions;
25

  
26
/* gvSIG. Geographic Information System of the Valencian Government
27
 *
28
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
29
 * of the Valencian Government (CIT)
30
 * 
31
 * This program is free software; you can redistribute it and/or
32
 * modify it under the terms of the GNU General Public License
33
 * as published by the Free Software Foundation; either version 2
34
 * of the License, or (at your option) any later version.
35
 * 
36
 * This program is distributed in the hope that it will be useful,
37
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39
 * GNU General Public License for more details.
40
 *  
41
 * You should have received a copy of the GNU General Public License
42
 * along with this program; if not, write to the Free Software
43
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
44
 * MA  02110-1301, USA.
45
 * 
46
 */
47

  
48
import java.io.File;
49

  
50
/**
51
 * <p>Exception to report that a backup process has failed.</p>
52
 *
53
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
54
 */
55
public class BackupException extends Exception {
56
	private static final long serialVersionUID = -2846421984617208883L;
57

  
58
	/**
59
	 * <p>The source file to be backup.</p>
60
	 */
61
	protected File source;
62

  
63
	/**
64
	 * <p>Constructs a new backup exception with the specified detail message and cause.</p>
65
	 * 
66
	 * @param message the detail message (which is saved for later retrieval by the <code>getMessage()</code> method).
67
	 * @param cause the cause (which is saved for later retrieval by the <code>getCause()</code> method). (A <code>null</code>
68
	 *  value is permitted, and indicates that the cause is nonexistent or unknown.)
69
	 * @param source the file from that was going to be done a backup
70
	 * 
71
	 * @see Exception#Exception(String, Throwable)
72
	 */
73
	public BackupException(String message, Throwable cause, File source) {
74
		super(message, cause);
75
		this.source = source;
76
	}
77

  
78
	/**
79
	 * <p>Gets the source file to be backup.</p> 
80
	 * 
81
	 * @return the source file
82
	 */
83
	public File getSource() {
84
		return source;
85
	}
86
}
0 87

  
tags/org.gvsig.desktop-2.0.116/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/tools/backup/BackupGeneratorFactory.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.backup;
25

  
26
/* gvSIG. Geographic Information System of the Valencian Government
27
 *
28
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
29
 * of the Valencian Government (CIT)
30
 * 
31
 * This program is free software; you can redistribute it and/or
32
 * modify it under the terms of the GNU General Public License
33
 * as published by the Free Software Foundation; either version 2
34
 * of the License, or (at your option) any later version.
35
 * 
36
 * This program is distributed in the hope that it will be useful,
37
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39
 * GNU General Public License for more details.
40
 *  
41
 * You should have received a copy of the GNU General Public License
42
 * along with this program; if not, write to the Free Software
43
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
44
 * MA  02110-1301, USA.
45
 * 
46
 */
47
 
48
/**
49
 * <p>Generic factory that creates a {@link BackupGenerator BackupGenerator} that performs a particular
50
 *  kind of backup.</p>
51
 *
52
 * @author Jose Ignacio Yarza (jiyarza@opensistemas.com)
53
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
54
 */
55
public abstract class BackupGeneratorFactory {
56
	/**
57
	 * <p>Gets an instance of a backup generator that performs a particular kind of backup.</p>
58
	 * 
59
	 * @return an instance of a backup generator that performs a particular kind of backup
60
	 */
61
	public abstract BackupGenerator getBackupGenerator();
62
}
0 63

  
tags/org.gvsig.desktop-2.0.116/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/tools/backup/DefaultBackupGeneratorFactory.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.backup;
25

  
26
/* gvSIG. Geographic Information System of the Valencian Government
27
 *
28
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
29
 * of the Valencian Government (CIT)
30
 * 
31
 * This program is free software; you can redistribute it and/or
32
 * modify it under the terms of the GNU General Public License
33
 * as published by the Free Software Foundation; either version 2
34
 * of the License, or (at your option) any later version.
35
 * 
36
 * This program is distributed in the hope that it will be useful,
37
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39
 * GNU General Public License for more details.
40
 *  
41
 * You should have received a copy of the GNU General Public License
42
 * along with this program; if not, write to the Free Software
43
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
44
 * MA  02110-1301, USA.
45
 * 
46
 */
47

  
48
/**
49
 * <p>Factory that gets a {@link DefaultBackupGenerator DefaultBackupGenerator} as a particular version of
50
 *  backup generator.</p>
51
 *
52
 * @author Jose Ignacio Yarza (jiyarza@opensistemas.com)
53
 * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
54
 */
55
public class DefaultBackupGeneratorFactory extends BackupGeneratorFactory {
56
	/*
57
	 * (non-Javadoc)
58
	 * @see com.iver.utiles.backup.BackupGeneratorFactory#getBackupGenerator()
59
	 */
60
	public BackupGenerator getBackupGenerator() {		
61
		return new DefaultBackupGenerator();
62
	}
63
}
0 64

  
tags/org.gvsig.desktop-2.0.116/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/utils/StringComparator.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils;
25

  
26
import java.text.Collator;
27
import java.util.Comparator;
28

  
29
/**
30
 * Compares two chain of characters alphabetically
31
 *
32
 * @author Fernando Gonz?lez Cort?s
33
 * @author Pablo Piqueras Bartolom?
34
 */
35
public class StringComparator implements Comparator {
36
	protected boolean caseSensitive = true;
37
	protected LocaleRules localeRules = null;
38

  
39
    /**
40
     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
41
     */
42
    public int compare(Object o1, Object o2) {
43
        String s1 = o1.toString();
44
        String s2 = o2.toString();
45

  
46
        // If localeRules is null -> use the default rules
47
        if (localeRules == null) {
48
        	if (caseSensitive) {
49
        		return s1.compareTo(s2);
50
        	}
51
        	else {
52
        		return s1.compareToIgnoreCase(s2);
53
        	}
54
        }
55
        else {
56
        	if (localeRules.isUseLocaleRules()) {
57
        		Collator collator = localeRules.getCollator();
58
        		
59
        		if (caseSensitive) {
60
        			return collator.compare(s1, s2);
61
        		}
62
        		else {
63
        			//return collator.compare(s1.toLowerCase(), s2.toLowerCase());
64
        			return collator.compare(s1.toUpperCase(), s2.toUpperCase());
65
        		}
66
        	}
67
        	else {
68
            	if (caseSensitive) {
69
            		return s1.compareTo(s2);
70
            	}
71
            	else {
72
            		return s1.compareToIgnoreCase(s2);
73
            	}
74
        	}
75
        }
76
    }
77

  
78
    /**
79
     * Returns if the comparator is sensitive to small and big letters
80
     *
81
     * @return
82
     */
83
    public boolean isCaseSensitive() {
84
        return caseSensitive;
85
    }
86

  
87
    /**
88
     * Establece la sensibilidad del comparador a las mayusculas y minusculas
89
     *
90
     * @param b
91
     */
92
    public void setCaseSensitive(boolean b) {
93
        caseSensitive = b;
94
    }
95
    
96
    /**
97
     * Gets an object with the information for use the locale rules in comparation between strings. <br>
98
     * <ul>
99
     * <li>A boolean value -> if want or not use the locale rules</li>
100
     * <li>A reference to the locale rules</li>
101
     * </ul>
102
     * 
103
     * @return @see LocaleRules
104
     */
105
    public LocaleRules getLocaleRules() {
106
    	return localeRules;
107
    }    
108
    /**
109
     * Sets an object with the information for use the locale rules in comparation between strings. <br>
110
     * <ul>
111
     * <li>A boolean value -> if want or not use the locale rules</li>
112
     * <li>A reference to the locale rules</li>
113
     * </ul>
114
     * 
115
     * @param @see LocaleRules
116
     */
117
    public void setLocaleRules(LocaleRules locRules) {
118
    	localeRules = locRules;
119
    }
120
    
121
    /**
122
     * Represents the information needed by <i>StringComparator</i> for use or not locale-sensitive String comparison-rules in the <b><i>compare</i></b> method
123
     * 
124
     * @author Pablo Piqueras Bartolom?
125
     */
126
    public class LocaleRules {
127
    	 private boolean useLocaleRules;
128
    	 private Collator _collator;
129
    	 
130
    	 /**
131
    	  * Default constructor without parameters
132
    	  */
133
    	 public LocaleRules() {
134
    		 useLocaleRules = false;
135
    		 _collator = null;
136
    	 }
137
    	 
138
    	 /**
139
    	  * Default constructor with two parameters
140
    	  * 
141
    	  * @param b Use locale rules
142
    	  * @param collator A reference to an object configurated for locale-sensitive String comparison
143
    	  */
144
    	 public LocaleRules(boolean b, Collator collator) {
145
    		 useLocaleRules = b;
146
    		 _collator = collator;
147
    	 }
148
    	 
149
 		/**
150
 		 * Gets the value of the inner attribute <i>_collator</i>
151
 		 * 
152
 		 * @return Returns A reference to an object configurated for locale-sensitive String comparison
153
 		 */
154
 		public Collator getCollator() {
155
 			return _collator;
156
 		}
157

  
158
 		/**
159
 		 * Sets a value to the inner attribute <i>_collator</i>
160
 		 * 
161
 		 * @param collator A reference to an object configurated for locale-sensitive String comparison
162
 		 */
163
 		public void setCollator(Collator collator) {
164
 			this._collator = collator;
165
 		}
166

  
167
		/**
168
		 * Gets the value of the inner attribute <i>useLocaleRules</i>
169
		 * 
170
		 * @return Returns the useLocaleRules.
171
		 */
172
		public boolean isUseLocaleRules() {
173
			return useLocaleRules;
174
		}
175

  
176
		/**
177
		 * Sets a value to the inner attribute <i>useLocaleRules</i>
178
		 * 
179
		 * @param useLocaleRules The useLocaleRules to set.
180
		 */
181
		public void setUseLocaleRules(boolean useLocaleRules) {
182
			this.useLocaleRules = useLocaleRules;
183
		}
184
    }
185
}
0 186

  
tags/org.gvsig.desktop-2.0.116/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/utils/SimpleFileFilter.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils;
25

  
26
import java.io.File;
27

  
28
import javax.swing.filechooser.FileFilter;
29

  
30

  
31
/**
32
 * A simple file filter implementation that allow to specify one or more file
33
 * extensions
34
 *
35
 * @author wolf
36
 */
37
public class SimpleFileFilter extends FileFilter implements java.io.FileFilter {
38
    String[] extensions;
39
    String description;
40

  
41
    public SimpleFileFilter(String extension, String description) {
42
        this.extensions = new String[] { extension };
43
        this.description = description;
44
    }
45

  
46
    public SimpleFileFilter(String[] extensions, String description) {
47
        this.extensions = extensions;
48
        this.description = description;
49
    }
50

  
51
    /**
52
     * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
53
     */
54
    public boolean accept(File f) {
55
        if (f.isDirectory()) {
56
            return true;
57
        }
58

  
59
        String extension = FileUtils.getFileExtension(f);
60

  
61
        for (int i = 0; i < extensions.length; i++) {
62
            if (extension.equals(extensions[i])) {
63
                return true;
64
            }
65
        }
66

  
67
        return false;
68
    }
69

  
70
    /**
71
     * @see javax.swing.filechooser.FileFilter#getDescription()
72
     */
73
    public String getDescription() {
74
        return description;
75
    }
76
}
0 77

  
tags/org.gvsig.desktop-2.0.116/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/utils/ExceptionDescription.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* CVS MESSAGES:
25
 *
26
 * $Id: ExceptionDescription.java 29631 2009-06-29 16:56:19Z jpiera $
27
 * $Log$
28
 * Revision 1.2  2006-09-22 08:08:04  ldiaz
29
 * layerName y driverName gestionados/almacenados desde esta clase
30
 *
31
 * Revision 1.1  2006/09/21 17:04:52  azabala
32
 * First version in cvs
33
 *
34
 *
35
 */
36
package org.gvsig.utils;
37

  
38
/**
39
 * <p>Most exceptions produced in a layer, has common information (layer name, driver name, error description, error code).</p>
40
 * 
41
 * <p>An <code>ExceptionDescription</code> can provide this extra information describing the cause of the
42
 *  error, and more useful information to give to the user that works with a layer.<p>
43
 * 
44
 * @author azabala
45
 */
46
public abstract class ExceptionDescription {
47
	/**
48
	 * <p>Code which identifies the kind of error which is the cause of this exception.</p>
49
	 */
50
	private int errorCode;
51

  
52
	/**
53
	 * <p>A description about the error which produced this exception.</p>
54
	 */
55
	private String errorDescription;
56

  
57
	/**
58
	 * <p>If this exception was produced using a driver, this attribute describes that driver.</p>
59
	 */
60
	private String driverName;
61

  
62
	/**
63
	 * <p>If this exception was produced using a layer, this attribute describes that layer.</p> 
64
	 */
65
	private String layerName;
66
	
67
	/**
68
	 * <p>Gets the name of the driver (if a driver was using) which this exception was produced.</p>
69
	 * 
70
	 * @return the name of the driver (if a driver was using) which this exception was produced
71
	 */	
72
	public String getDriverName() {
73
		return driverName;
74
	}
75

  
76
	/**
77
	 * <p>Sets the name of the driver (if a driver was using) which this exception was produced.</p>
78
	 * 
79
	 * @param driverName the name of the driver (if a driver was using) which this exception was produced
80
	 */
81
	public void setDriverName(String driverName){
82
		this.driverName = driverName;
83
	}	
84

  
85
	/**
86
	 * <p>Gets the name of the layer (if a layer was using) with that this exception was produced.</p>
87
	 * 
88
	 * @return the name of the layer (if a layer was using) with that this exception was produced
89
	 */
90
	public String getLayerName() {
91
		return driverName;
92
	}
93

  
94
	/**
95
	 * <p>Sets the name of the layer (if a layer was using) with that this exception was produced.</p>
96
	 * 
97
	 * @param layerName the name of the layer (if a layer was using) with that this exception was produced
98
	 */
99
	public void setLayerName(String layerName){
100
		this.layerName = layerName;
101
	}	
102

  
103
	/**
104
	 * <p>Sets the name of the layer (if a layer was using) with that this exception was produced.</p>
105
	 * 
106
	 * @param layerName the name of the layer (if a layer was using) with that this exception was produced
107
	 */
108
	public ExceptionDescription() {
109
	}
110

  
111
	/**
112
	 * <p>Creates a new <code>ExceptionDescription</code> with the useful values initialized.</p>
113
	 * 
114
	 * @param errorCode code which identifies the kind of error which is the cause of this exception
115
	 * @param errorDescription description about the error which produced this exception
116
	 */
117
	public ExceptionDescription(int errorCode, String errorDescription) {
118
		this.errorCode = errorCode;
119
		this.errorDescription = errorDescription;
120
	}
121

  
122
	/**
123
	 * <p>Sets the code which identifies the kind of error which is the cause of this exception.</p>
124
	 * 
125
	 * @param errorCode code which identifies the kind of error which is the cause of this exception
126
	 */
127
	public void setCode(int errorCode) {
128
		this.errorCode = errorCode;
129
	}
130

  
131
	/**
132
	 * <p>Sets the description about the error which produced this exception.</p>
133
	 * 
134
	 * @param description description about the error which produced this exception
135
	 */
136
	public void setDescription(String description) {
137
		this.errorDescription = description;
138
	}
139

  
140
	/**
141
	 * <p>Gets the code which identifies the kind of error which is the cause of this exception.</p>
142
	 * 
143
	 * @return code which identifies the kind of error which is the cause of this exception
144
	 */
145
	public int getErrorCode() {
146
		return errorCode;
147
	}
148

  
149
	/**
150
	 * <p>Gets the description about the error which produced this exception.</p>
151
	 * 
152
	 * @return description about the error which produced this exception
153
	 */
154
	public String getErrorDescription() {
155
		return errorDescription;
156
	}
157

  
158
	/**
159
	 * <p>Returns a message that describes the error which produced this exception, formatted in HTML code.</p>
160
	 * 
161
	 * @return message that describes the error which produced this exception, formatted in HTML code
162
	 */
163
	public abstract String getHtmlErrorMessage();
164
}
0 165

  
tags/org.gvsig.desktop-2.0.116/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/utils/CompareLists.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils;
25

  
26
import java.util.Comparator;
27
import java.util.List;
28

  
29
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
30
 *
31
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
32
 *
33
 * This program is free software; you can redistribute it and/or
34
 * modify it under the terms of the GNU General Public License
35
 * as published by the Free Software Foundation; either version 2
36
 * of the License, or (at your option) any later version.
37
 *
38
 * This program is distributed in the hope that it will be useful,
39
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41
 * GNU General Public License for more details.
42
 *
43
 * You should have received a copy of the GNU General Public License
44
 * along with this program; if not, write to the Free Software
45
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
46
 *
47
 * For more information, contact:
48
 *
49
 *  Generalitat Valenciana
50
 *   Conselleria d'Infraestructures i Transport
51
 *   Av. Blasco Ib??ez, 50
52
 *   46010 VALENCIA
53
 *   SPAIN
54
 *
55
 *      +34 963862235
56
 *   gvsig@gva.es
57
 *      www.gvsig.gva.es
58
 *
59
 *    or
60
 *
61
 *   IVER T.I. S.A
62
 *   Salamanca 50
63
 *   46005 Valencia
64
 *   Spain
65
 *
66
 *   +34 963163400
67
 *   dac@iver.es
68
 */
69

  
70
/**
71
 * This class has methods to compare lists of objects
72
 * 
73
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
74
 */
75
public class CompareLists {
76
	/**
77
	 * Compares two lists of elements, using the value returned of the <i><b>toString()</b></i> method
78
	 *   of each element. <br>
79
	 * Each element must also implement the {@link Comparable} interface. <br>
80
	 * This method considers case sensitive .
81
	 * 
82
	 * @param l1 First list of items. @see java.util.List
83
	 * @param l2 Second list of items. @see java.util.List
84
	 * @return True if the two lists have the same number of elements, and elements are in the same
85
	 *   position of the two lists and have the same <i>String</i> value .
86
	 */
87
	public synchronized static boolean compare(List l1, List l2) {
88
		// If both are null -> true; if one yes but the other no -> return false
89
		if ((l1 == null) || (l2 == null)) {
90
			if (l1 == l2)
91
				return true;
92
			else
93
				return false;
94
		}
95
		
96
		// If the length isn't equal
97
		if (l1.size() != l2.size())
98
			return false;
99

  
100
		// Compares each item: must have the same value in the same position in the list
101
		for (int i = 0; i < l1.size(); i++) {
102
			if ( l1.get(i).toString().compareTo(l2.get(i).toString()) != 0 )
103
				return false;
104
		}
105
		
106
		return true;
107
	}
108
	
109
	/**
110
	 * Compares two lists of elements, using the value returned of the <i><b>toString()</b></i> method
111
	 *   of each element. <br>
112
	 * Each element must also implement the {@link Comparable} interface. <br>
113
	 * This method ignores case sensitive during the comparation.
114
	 * 
115
	 * @param l1 First list of items. @see java.util.List
116
	 * @param l2 Second list of items. @see java.util.List
117
	 * @return True if the two lists have the same number of elements, and elements are in the same
118
	 *   position of the two lists and have the same <i>String</i> value .
119
	 */
120
	public synchronized static boolean compareIgnoringCaseSensitive(List l1, List l2) {
121
		// If both are null -> true; if one yes but the other no -> return false
122
		if ((l1 == null) || (l2 == null)) {
123
			if (l1 == l2)
124
				return true;
125
			else
126
				return false;
127
		}
128

  
129
		// If the length isn't equal
130
		if (l1.size() != l2.size())
131
			return false;
132

  
133
		// Compares each item: must have the same value in the same position in the list
134
		for (int i = 0; i < l1.size(); i++) {
135
			if ( l1.get(i).toString().compareToIgnoreCase(l2.get(i).toString()) != 0 )
136
				return false;
137
		}
138
		
139
		return true;
140
	}
141
	
142
	/**
143
	 * Compares two lists of elements, using the value returned of the <i><b>toString()</b></i> method
144
	 *   of each element. <br>
145
	 * Each element must also implement the {@link Comparable} interface. <br>
146
	 * 
147
	 * @param l1 First list of items. @see java.util.List
148
	 * @param l2 Second list of items. @see java.util.List
149
	 * @param comp A class which implements the <i><b>compare</b></i> method of the <i>Comparator</i> interface . 
150
	 * @return True if the two lists have the same number of elements, and elements are in the same
151
	 *   position of the two lists and have the same <i>String</i> value .
152
	 */
153
	public synchronized static boolean compare(List l1, List l2, Comparator comp) {
154
		// If both are null -> true; if one yes but the other no -> return false
155
		if ((l1 == null) || (l2 == null)) {
156
			if (l1 == l2)
157
				return true;
158
			else
159
				return false;
160
		}
161

  
162
		// If the length isn't equal
163
		if (l1.size() != l2.size())
164
			return false;
165

  
166
		// Compares each item: must have the same value in the same position in the list
167
		for (int i = 0; i < l1.size(); i++) {
168
			if ( comp.compare(l1.get(i).toString(), (l2.get(i).toString())) != 0 )
169
				return false;
170
		}
171

  
172
		return true;
173
	}
174
	
175
	/**
176
	 * Compares two lists of elements, using the value returned of the <i><b>toString()</b></i> method
177
	 *   of each element. <br>
178
	 * Each element must also implement the {@link Comparable} interface. <br>
179
	 * This method ignores case sensitive during the comparation.
180
	 * 
181
	 * @param l1 First list of items. @see java.util.List
182
	 * @param l2 Second list of items. @see java.util.List
183
	 * @param comp A class which implements the <i><b>compare</b></i> method of the <i>Comparator</i> interface . 
184
	 * @return True if the two lists have the same number of elements, and elements are in the same
185
	 *   position of the two lists and have the same <i>String</i> value .
186
	 */
187
	public synchronized static boolean compareIgnoringCaseSensitive(List l1, List l2, Comparator comp) {
188
		// If both are null -> true; if one yes but the other no -> return false
189
		if ((l1 == null) || (l2 == null)) {
190
			if (l1 == l2)
191
				return true;
192
			else
193
				return false;
194
		}
195

  
196
		// If the length isn't equal
197
		if (l1.size() != l2.size())
198
			return false;
199

  
200
		// Compares each item: must have the same value in the same position in the list
201
		for (int i = 0; i < l1.size(); i++) {
202
			if ( comp.compare(l1.get(i).toString().toLowerCase(), (l2.get(i).toString().toLowerCase())) != 0 )
203
				return false;
204
		}
205

  
206
		return true;
207
	}
208
}
0 209

  
tags/org.gvsig.desktop-2.0.116/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/utils/DoubleUtilities.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils;
25

  
26
import java.text.DecimalFormat;
27
import java.text.DecimalFormatSymbols;
28

  
29
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
30
 *
31
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
32
 *
33
 * This program is free software; you can redistribute it and/or
34
 * modify it under the terms of the GNU General Public License
35
 * as published by the Free Software Foundation; either version 2
36
 * of the License, or (at your option) any later version.
37
 *
38
 * This program is distributed in the hope that it will be useful,
39
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41
 * GNU General Public License for more details.
42
 *
43
 * You should have received a copy of the GNU General Public License
44
 * along with this program; if not, write to the Free Software
45
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
46
 *
47
 * For more information, contact:
48
 *
49
 *  Generalitat Valenciana
50
 *   Conselleria d'Infraestructures i Transport
51
 *   Av. Blasco Ib??ez, 50
52
 *   46010 VALENCIA
53
 *   SPAIN
54
 *
55
 *      +34 963862235
56
 *   gvsig@gva.es
57
 *      www.gvsig.gva.es
58
 *
59
 *    or
60
 *
61
 *   IVER T.I. S.A
62
 *   Salamanca 50
63
 *   46005 Valencia
64
 *   Spain
65
 *
66
 *   +34 963163400
67
 *   dac@iver.es
68
 */
69
/* CVS MESSAGES:
70
 *
71
 * $Id: DoubleUtilities.java 29631 2009-06-29 16:56:19Z jpiera $
72
 * $Log$
73
 * Revision 1.3  2006-10-30 11:59:47  nacho
74
 * formateado de double
75
 *
76
 * Revision 1.2  2006/09/13 08:10:07  jorpiell
77
 * Se limita el n?mero de decimales por arriba y por abajo
78
 *
79
 * Revision 1.1  2006/05/16 13:02:41  jorpiell
80
 * Se ha a?adido la clase DoubleUtiles, donde ha un metodo para limitar el tama?o de los decimales de un double y para quitar los "puntos" de la parte entera
81
 *
82
 *
83
 */
84
/**
85
 * This class has some methods to manage "Doubles"
86
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
87
 */
88
public class DoubleUtilities {
89
	/**
90
	 * Formats a double with an specified number of decimals. It 
91
	 * removes the separator character of the integer part. 
92
	 * @param value
93
	 * Separator char of the integer part
94
	 * @param decimalSeparator
95
	 * Separator char of the decimal part
96
	 * @param decimalsNumber
97
	 * Number of decimals
98
	 * @return
99
	 * The formatted double
100
	 */
101
	public static double format(double value,
102
			char decimalSeparator,
103
			int decimalsNumber){
104
			
105
		DecimalFormat dFormat = new DecimalFormat("#");
106
		DecimalFormatSymbols dFormatSymbols = new DecimalFormatSymbols();
107
		
108
		dFormatSymbols.setDecimalSeparator(decimalSeparator);
109
		dFormat.setMaximumFractionDigits(decimalsNumber);
110
		dFormat.setMaximumFractionDigits(decimalsNumber);
111
		dFormat.setDecimalFormatSymbols(dFormatSymbols);
112
		double d = Double.parseDouble(dFormat.format(value));
113
		return Double.parseDouble(dFormat.format(value));		
114
	}
115

  
116
	/**
117
	 * Formats a double with an specified number of decimals. 
118
	 * @param num
119
	 * Value to tail
120
	 * @param n
121
	 * Number of decimals
122
	 * @return
123
	 * The formatted double
124
	 */
125
    public static double format(double num, int n){
126
    	long m = (long)Math.pow(10, n);
127
        num *= m;
128
        long aux = ((long)num);
129
        num = (double)((double)aux / (double)m);
130
        return num;
131
    }
132

  
133
}
0 134

  
tags/org.gvsig.desktop-2.0.116/org.gvsig.desktop.library/org.gvsig.utils/src/main/java/org/gvsig/utils/swing/JComboBox.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils.swing;
25

  
26
import java.awt.event.ActionEvent;
27
import java.awt.event.KeyEvent;
28
import java.awt.event.KeyListener;
29
import java.util.Vector;
30

  
31
import javax.swing.ComboBoxModel;
32
import javax.swing.DefaultComboBoxModel;
33
import javax.swing.JTextField;
34
import javax.swing.plaf.basic.BasicComboBoxEditor;
35

  
36

  
37
/**
38
 * ComboBox autocompletable
39
 *
40
 * @author Fernando Gonz?lez Cort?s
41
 */
42
public class JComboBox extends javax.swing.JComboBox {
43
    /**
44
     * Construye un combobox
45
     */
46
    public JComboBox() {
47
        super();
48
        init();
49
    }
50

  
51
    /**
52
     * Construye un combobox
53
     *
54
     * @param arg0
55
     */
56
    public JComboBox(Object[] arg0) {
57
        super(arg0);
58
        init();
59
    }
60

  
61
    /**
62
     * Construye un combobox
63
     *
64
     * @param arg0
65
     */
66
    public JComboBox(Vector arg0) {
67
        super(arg0);
68
        init();
69
    }
70

  
71
    /**
72
     * Construye un combobox
73
     *
74
     * @param arg0
75
     */
76
    public JComboBox(ComboBoxModel arg0) {
77
        super(arg0);
78
        init();
79
    }
80

  
81
    /**
82
     * Inicializa el combo
83
     */
84
    private void init() {
85
        this.setEditor(new BasicComboBoxEditor());
86

  
87
        JTextField jtext = (JTextField) JComboBox.this.getEditor()
88
                                                      .getEditorComponent();
89
        jtext.addKeyListener(new MyKeyListener());
90
        jtext.setText("");
91
    }
92

  
93
    /**
94
     * Manejador de los eventos Key para hacer el autocompletado
95
     *
96
     * @author Fernando Gonz?lez Cort?s
97
     */
98
    public class MyKeyListener implements KeyListener {
99
        private int lastCaretPosition = 0;
100
        private String lastText = "";
101
        private boolean bAutocompletar=true;
102

  
103
        /**
104
         * Devuelve dada una String, la String del modelo que empieza por dicha
105
         * String si hay alguna, o null si no hay ninguna
106
         *
107
         * @param text texto que se busca en el modelo
108
         *
109
         * @return String del modelo o null
110
         */
111
        private String isInModel(String text) {
112
            DefaultComboBoxModel model = (DefaultComboBoxModel) JComboBox.this.getModel();
113

  
114
            for (int i = 0; i < model.getSize(); i++) {
115
                if ((model.getElementAt(i).toString()).startsWith(text)) {
116
                    return model.getElementAt(i).toString();
117
                }
118
            }
119

  
120
            return null;
121
        }
122

  
123
        /**
124
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
125
         */
126
        public void actionPerformed(ActionEvent arg0) {
127
            JTextField jtext = (JTextField) JComboBox.this.getEditor()
128
                                                          .getEditorComponent();
129
            String texto = jtext.getText();
130

  
131
            String text = isInModel(texto);
132
            
133
            if (text != null) {
134
                int caretPos = texto.length();
135
                jtext.setText(text);
136
                jtext.setCaretPosition(caretPos);
137
                jtext.setSelectionStart(caretPos);
138
                jtext.setSelectionEnd(text.length());
139
            }
140
        }
141

  
142
        /**
143
         * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
144
         */
145
        public void keyPressed(KeyEvent e) {
146
            JTextField jtext = (JTextField) JComboBox.this.getEditor()
147
                                                          .getEditorComponent();
148
            bAutocompletar = true;
149
            if ((e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
150
                    || (e.getKeyCode() == KeyEvent.VK_DELETE))
151
            {
152
                bAutocompletar = false;
153
                /* if ((lastCaretPosition >=0) && 
154
                        (lastCaretPosition <= jtext.getText().length()))
155
                        	jtext.setCaretPosition(lastCaretPosition); */
156
            }
157
        }
158

  
159
        /**
160
         * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
161
         */
162
        public void keyReleased(KeyEvent e) {
163
            JTextField jtext = (JTextField) JComboBox.this.getEditor()
164
                                                          .getEditorComponent();
165
            String texto = jtext.getText().substring(0, jtext.getCaretPosition());
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff