Source Code Cross Referenced for PreferencesStore.java in  » Net » j2ssh » com » sshtools » common » ui » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » j2ssh » com.sshtools.common.ui 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  SSHTools - Java SSH2 API
003:         *
004:         *  Copyright (C) 2002-2003 Lee David Painter and Contributors.
005:         *
006:         *  Contributions made by:
007:         *
008:         *  Brett Smith
009:         *  Richard Pernavas
010:         *  Erwin Bolwidt
011:         *
012:         *  This program is free software; you can redistribute it and/or
013:         *  modify it under the terms of the GNU General Public License
014:         *  as published by the Free Software Foundation; either version 2
015:         *  of the License, or (at your option) any later version.
016:         *
017:         *  This program is distributed in the hope that it will be useful,
018:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
019:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
020:         *  GNU General Public License for more details.
021:         *
022:         *  You should have received a copy of the GNU General Public License
023:         *  along with this program; if not, write to the Free Software
024:         *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
025:         */
026:        package com.sshtools.common.ui;
027:
028:        import org.apache.commons.logging.Log;
029:        import org.apache.commons.logging.LogFactory;
030:
031:        import java.awt.Rectangle;
032:
033:        import java.io.File;
034:        import java.io.FileInputStream;
035:        import java.io.FileOutputStream;
036:        import java.io.IOException;
037:        import java.io.InputStream;
038:        import java.io.OutputStream;
039:
040:        import java.util.Properties;
041:        import java.util.StringTokenizer;
042:
043:        import javax.swing.JTable;
044:
045:        /**
046:         *
047:         *
048:         * @author $author$
049:         * @version $Revision: 1.16 $
050:         */
051:        public class PreferencesStore {
052:            /**  */
053:            protected static Log log = LogFactory
054:                    .getLog(PreferencesStore.class);
055:
056:            //
057:            private static File file;
058:            private static boolean storeAvailable;
059:            private static Properties preferences;
060:
061:            //  Intialise the preferences
062:            static {
063:                preferences = new Properties();
064:            }
065:
066:            /**
067:             *
068:             *
069:             * @param table
070:             * @param pref
071:             */
072:            public static void saveTableMetrics(JTable table, String pref) {
073:                for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
074:                    int w = table.getColumnModel().getColumn(i).getWidth();
075:                    put(pref + ".column." + i + ".width", String.valueOf(w));
076:                    put(pref + ".column." + i + ".position", String
077:                            .valueOf(table.convertColumnIndexToModel(i)));
078:                }
079:            }
080:
081:            /**
082:             *
083:             *
084:             * @param table
085:             * @param pref
086:             * @param defaultWidths
087:             *
088:             * @throws IllegalArgumentException
089:             */
090:            public static void restoreTableMetrics(JTable table, String pref,
091:                    int[] defaultWidths) {
092:                //  Check the table columns may be resized correctly
093:                if (table.getAutoResizeMode() != JTable.AUTO_RESIZE_OFF) {
094:                    throw new IllegalArgumentException(
095:                            "Table AutoResizeMode must be JTable.AUTO_RESIZE_OFF");
096:                }
097:
098:                //  Restore the table column widths and positions
099:                for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
100:                    try {
101:                        table.moveColumn(table.convertColumnIndexToView(getInt(
102:                                pref + ".column." + i + ".position", i)), i);
103:                        table.getColumnModel().getColumn(i).setPreferredWidth(
104:                                getInt(pref + ".column." + i + ".width",
105:                                        (defaultWidths == null) ? table
106:                                                .getColumnModel().getColumn(i)
107:                                                .getPreferredWidth()
108:                                                : defaultWidths[i]));
109:                    } catch (NumberFormatException nfe) {
110:                    }
111:                }
112:            }
113:
114:            /**
115:             *
116:             *
117:             * @return
118:             */
119:            public static boolean isStoreAvailable() {
120:                return storeAvailable;
121:            }
122:
123:            /**
124:             *
125:             *
126:             * @param file
127:             */
128:            public static void init(File file) {
129:                PreferencesStore.file = file;
130:
131:                //  Make sure the preferences directory exists, creating it if it doesn't
132:                File dir = file.getParentFile();
133:
134:                if (!dir.exists()) {
135:                    log.info("Creating SSHTerm preferences directory "
136:                            + dir.getAbsolutePath());
137:
138:                    if (!dir.mkdirs()) {
139:                        log.error("Preferences directory "
140:                                + dir.getAbsolutePath()
141:                                + " could not be created. "
142:                                + "Preferences will not be stored");
143:                    }
144:                }
145:
146:                storeAvailable = dir.exists();
147:
148:                //  If the preferences file exists, then load it
149:                if (storeAvailable) {
150:                    if (file.exists()) {
151:                        InputStream in = null;
152:
153:                        try {
154:                            in = new FileInputStream(file);
155:                            preferences.load(in);
156:                            storeAvailable = true;
157:                        } catch (IOException ioe) {
158:                            log.error(ioe);
159:                        } finally {
160:                            if (in != null) {
161:                                try {
162:                                    in.close();
163:                                } catch (IOException ioe) {
164:                                }
165:                            }
166:                        }
167:                    }
168:                    //  Otherwise create it
169:                    else {
170:                        savePreferences();
171:                    }
172:                } else {
173:                    log.warn("Preferences store not available.");
174:                }
175:            }
176:
177:            /**
178:             *
179:             */
180:            public static void savePreferences() {
181:                if (file == null) {
182:                    log
183:                            .error("Preferences not saved as PreferencesStore has not been initialise.");
184:                } else {
185:                    OutputStream out = null;
186:
187:                    try {
188:                        out = new FileOutputStream(file);
189:                        preferences.store(out, "SSHTerm preferences");
190:                        log.info("Preferences written to "
191:                                + file.getAbsolutePath());
192:                        storeAvailable = true;
193:                    } catch (IOException ioe) {
194:                        log.error(ioe);
195:                    } finally {
196:                        if (out != null) {
197:                            try {
198:                                out.close();
199:                            } catch (IOException ioe) {
200:                            }
201:                        }
202:                    }
203:                }
204:            }
205:
206:            /**
207:             *
208:             *
209:             * @param name
210:             * @param def
211:             *
212:             * @return
213:             */
214:            public static String get(String name, String def) {
215:                return preferences.getProperty(name, def);
216:            }
217:
218:            /**
219:             *
220:             *
221:             * @param name
222:             * @param val
223:             */
224:            public static void put(String name, String val) {
225:                preferences.put(name, val);
226:            }
227:
228:            /**
229:             *
230:             *
231:             * @param name
232:             * @param def
233:             *
234:             * @return
235:             */
236:            public static Rectangle getRectangle(String name, Rectangle def) {
237:                String s = preferences.getProperty(name);
238:
239:                if ((s == null) || s.equals("")) {
240:                    return def;
241:                } else {
242:                    StringTokenizer st = new StringTokenizer(s, ",");
243:                    Rectangle r = new Rectangle();
244:
245:                    try {
246:                        r.x = Integer.parseInt(st.nextToken());
247:                        r.y = Integer.parseInt(st.nextToken());
248:                        r.width = Integer.parseInt(st.nextToken());
249:                        r.height = Integer.parseInt(st.nextToken());
250:                    } catch (NumberFormatException nfe) {
251:                        log.warn("Preference is " + name
252:                                + " is badly formatted", nfe);
253:                    }
254:
255:                    return r;
256:                }
257:            }
258:
259:            /**
260:             *
261:             *
262:             * @param name
263:             * @param val
264:             */
265:            public static void putRectangle(String name, Rectangle val) {
266:                preferences.put(name, (val == null) ? "" : (val.x + "," + val.y
267:                        + "," + val.width + "," + val.height));
268:            }
269:
270:            /**
271:             *
272:             *
273:             * @param name
274:             * @param def
275:             *
276:             * @return
277:             */
278:            public static int getInt(String name, int def) {
279:                String s = preferences.getProperty(name);
280:
281:                if ((s != null) && !s.equals("")) {
282:                    try {
283:                        return Integer.parseInt(s);
284:                    } catch (NumberFormatException nfe) {
285:                        log.warn("Preference is " + name
286:                                + " is badly formatted", nfe);
287:                    }
288:                }
289:
290:                return def;
291:            }
292:
293:            /**
294:             *
295:             *
296:             * @param name
297:             * @param def
298:             *
299:             * @return
300:             */
301:            public static double getDouble(String name, double def) {
302:                String s = preferences.getProperty(name);
303:
304:                if ((s != null) && !s.equals("")) {
305:                    try {
306:                        return Double.parseDouble(s);
307:                    } catch (NumberFormatException nfe) {
308:                        log.warn("Preference is " + name
309:                                + " is badly formatted", nfe);
310:                    }
311:                }
312:
313:                return def;
314:            }
315:
316:            /**
317:             *
318:             *
319:             * @param name
320:             * @param val
321:             */
322:            public static void putInt(String name, int val) {
323:                preferences.put(name, String.valueOf(val));
324:            }
325:
326:            /**
327:             *
328:             *
329:             * @param name
330:             * @param val
331:             */
332:            public static void putDouble(String name, double val) {
333:                preferences.put(name, String.valueOf(val));
334:            }
335:
336:            /**
337:             *
338:             *
339:             * @param name
340:             * @param def
341:             *
342:             * @return
343:             */
344:            public static boolean getBoolean(String name, boolean def) {
345:                return get(name, String.valueOf(def)).equals("true");
346:            }
347:
348:            /**
349:             *
350:             *
351:             * @param name
352:             * @param val
353:             */
354:            public static void putBoolean(String name, boolean val) {
355:                preferences.put(name, String.valueOf(val));
356:            }
357:
358:            /**
359:             *
360:             *
361:             * @param name
362:             *
363:             * @return
364:             */
365:            public static boolean preferenceExists(String name) {
366:                return preferences.containsKey(name);
367:            }
368:
369:            /**
370:             *
371:             *
372:             * @param name
373:             *
374:             * @return
375:             */
376:            public static boolean removePreference(String name) {
377:                boolean exists = preferenceExists(name);
378:                preferences.remove(name);
379:
380:                return exists;
381:            }
382:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.