/plugins/LogViewer/tags/rel-0-8/src/java/logviewer/LogViewerAttributes.java

# · Java · 370 lines · 176 code · 22 blank · 172 comment · 9 complexity · 6581cd653287f3c0ad38dc97b8a22949 MD5 · raw file

  1. /*
  2. * Copyright (C) 2003 Don Brown (mrdon@techie.com)
  3. * Copyright (C) 2000, 2001 Greg Merrill (greghmerrill@yahoo.com)
  4. * This file is part of Log Viewer, a plugin for jEdit (http://www.jedit.org).
  5. * It is heavily based off Follow (http://follow.sf.net).
  6. * Log Viewer is free software; you can redistribute it and/or modify
  7. * it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. * Log Viewer is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with Log Viewer; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. package logviewer;
  18. import java.awt.Font;
  19. import java.awt.GraphicsEnvironment;
  20. import java.io.BufferedInputStream;
  21. import java.io.BufferedOutputStream;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.FileOutputStream;
  25. import java.io.IOException;
  26. import java.util.ArrayList;
  27. import java.util.Iterator;
  28. import java.util.List;
  29. import javax.swing.JOptionPane;
  30. import javax.swing.JTabbedPane;
  31. /**
  32. * Facade for log viewer settings
  33. *
  34. * @author <a href="mailto:mrdon@techie.com">Don Brown</a>
  35. * @author <a href="mailto:greghmerrill@yahoo.com">Greg Merrill</a>
  36. */
  37. class LogViewerAttributes {
  38. final static String userHome = System.getProperty("user.home");
  39. final static String propertyPrototypeFileName =
  40. "followApp.properties.prototype";
  41. final static int bufferSize = 32768;
  42. final static String followedFilesKey = "followedFiles";
  43. final static String tabPlacementKey = "tabs.placement";
  44. final static String selectedTabIndexKey = "tabs.selectedIndex";
  45. final static String lastFileChooserDirKey = "fileChooser.lastDir";
  46. final static String bufferSizeKey = "bufferSize";
  47. final static String latencyKey = "latency";
  48. final static String attributesVersionKey = "attributesVersion";
  49. final static String fontFamilyKey = "fontFamily";
  50. final static String fontStyleKey = "fontStyle";
  51. final static String fontSizeKey = "fontSize";
  52. final static String confirmDeleteKey = "confirmDelete";
  53. final static String confirmDeleteAllKey = "confirmDeleteAll";
  54. final static String wordWrapKey = "wordWrap";
  55. final static String autoScrollKey = "autoScroll";
  56. EnumeratedProperties properties_;
  57. private EnumeratedProperties defaultProperties_;
  58. private LogViewerAttributes defaultAttributes_;
  59. /** Constructor */
  60. LogViewerAttributes() {
  61. properties_ = new EnumeratedProperties();
  62. }
  63. /**
  64. * Whether to confirm deletes
  65. *
  66. * @return True if they should be confirmed
  67. */
  68. public boolean confirmDelete() {
  69. return getBoolean(confirmDeleteKey);
  70. }
  71. /**
  72. * Sets the confirmDelete flag
  73. *
  74. * @param value The new confirmDelete value
  75. */
  76. public void setConfirmDelete(boolean value) {
  77. setBoolean(confirmDeleteKey, value);
  78. }
  79. public boolean wordWrap() {
  80. return getBoolean(wordWrapKey);
  81. }
  82. public void setWordWrap(boolean value) {
  83. setBoolean(wordWrapKey, value);
  84. }
  85. /**
  86. * Whether to confirm delete all actions
  87. *
  88. * @return True if should be confirmed
  89. */
  90. public boolean confirmDeleteAll() {
  91. return getBoolean(confirmDeleteAllKey);
  92. }
  93. /**
  94. * Sets the confirmDeleteAll flag
  95. *
  96. * @param value The new confirmDeleteAll value
  97. */
  98. public void setConfirmDeleteAll(boolean value) {
  99. setBoolean(confirmDeleteAllKey, value);
  100. }
  101. /**
  102. * Whether to autoscroll
  103. *
  104. * @return True if should autoscroll
  105. */
  106. public boolean autoScroll() {
  107. return getBoolean(autoScrollKey);
  108. }
  109. /**
  110. * Sets the autoScroll flag
  111. *
  112. * @param value The new autoScroll value
  113. */
  114. public void setAutoScroll(boolean value) {
  115. setBoolean(autoScrollKey, value);
  116. }
  117. /**
  118. * Gets a list of followed files
  119. *
  120. * @return The iterator of the list
  121. */
  122. Iterator getFollowedFiles() {
  123. List fileNames = properties_.getEnumeratedProperty(followedFilesKey);
  124. List files = new ArrayList();
  125. Iterator i = fileNames.iterator();
  126. while (i.hasNext()) {
  127. files.add(new File((String) i.next()));
  128. }
  129. return files.iterator();
  130. }
  131. /**
  132. * Whether the file is being followed
  133. *
  134. * @param file The file to check
  135. * @return true if any File in the List of followed Files
  136. * (getFollowedFiles()) has the same Canonical Path as the supplied
  137. * File
  138. * @exception IOException If something goes wrong
  139. */
  140. boolean followedFileListContains(File file)
  141. throws IOException {
  142. Iterator i = getFollowedFiles();
  143. while (i.hasNext()) {
  144. File nextFile = (File) i.next();
  145. if (nextFile.getCanonicalPath().equals(file.getCanonicalPath())) {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. /**
  152. * Adds a followed file
  153. *
  154. * @param file The file to add
  155. */
  156. void addFollowedFile(File file) {
  157. List fileNames = properties_.getEnumeratedProperty(followedFilesKey);
  158. fileNames.add(file.getAbsolutePath());
  159. properties_.setEnumeratedProperty(
  160. followedFilesKey,
  161. fileNames
  162. );
  163. }
  164. /**
  165. * Removes the followed file
  166. *
  167. * @param file The file
  168. */
  169. void removeFollowedFile(File file) {
  170. List fileNames = properties_.getEnumeratedProperty(followedFilesKey);
  171. fileNames.remove(file.getAbsolutePath());
  172. properties_.setEnumeratedProperty(
  173. followedFilesKey,
  174. fileNames
  175. );
  176. }
  177. /**
  178. * Gets where the tabs should be placed
  179. *
  180. * @return Where the tabs should be placed
  181. */
  182. int getTabPlacement() {
  183. return getInt(tabPlacementKey, JTabbedPane.TOP);
  184. }
  185. /**
  186. * Sets where the tabs should be placed
  187. *
  188. * @param tabPlacement The new tab location
  189. */
  190. void setTabPlacement(int tabPlacement) {
  191. setInt(tabPlacementKey, tabPlacement);
  192. }
  193. /**
  194. * Gets the index of the selected tab
  195. *
  196. * @return The selectedTabIndex value
  197. */
  198. int getSelectedTabIndex() {
  199. try {
  200. return getInt(selectedTabIndexKey, 0);
  201. }
  202. catch (NumberFormatException e) {
  203. setSelectedTabIndex(0);
  204. return 0;
  205. }
  206. }
  207. /**
  208. * Sets which tab is selected
  209. *
  210. * @param selectedTabIndex The new selectedTabIndex value
  211. */
  212. void setSelectedTabIndex(int selectedTabIndex) {
  213. setInt(selectedTabIndexKey, selectedTabIndex);
  214. }
  215. /**
  216. * Gets the last used directory
  217. *
  218. * @return The directory
  219. */
  220. File getLastFileChooserDirectory() {
  221. String dir = LogViewer.getProperty(lastFileChooserDirKey);
  222. if (dir == null) {
  223. dir = userHome;
  224. }
  225. return new File(dir);
  226. }
  227. /**
  228. * Sets the last used directory
  229. *
  230. * @param file The directory
  231. */
  232. void setLastFileChooserDirectory(File file) {
  233. if (file != null)
  234. LogViewer.setProperty(lastFileChooserDirKey, file.getAbsolutePath());
  235. }
  236. /**
  237. * Gets the buffer size
  238. *
  239. * @return The bufferSize value
  240. */
  241. int getBufferSize() {
  242. return getInt(bufferSizeKey, 100);
  243. }
  244. /**
  245. * Sets the buffer size
  246. *
  247. * @param bufferSize The new bufferSize value
  248. */
  249. void setBufferSize(int bufferSize) {
  250. setInt(bufferSizeKey, bufferSize);
  251. }
  252. /**
  253. * Sets the buffer size
  254. *
  255. * @param bufferSize The new bufferSize value
  256. */
  257. void setBufferSize(String bufferSize) {
  258. setBufferSize(Integer.parseInt(bufferSize));
  259. }
  260. /**
  261. * Gets the latency
  262. *
  263. * @return The latency value
  264. */
  265. int getLatency() {
  266. return getInt(latencyKey, 1000);
  267. }
  268. /**
  269. * Sets the latency
  270. *
  271. * @param latency The new latency value
  272. */
  273. void setLatency(int latency) {
  274. setInt(latencyKey, latency);
  275. }
  276. /**
  277. * Sets the latency
  278. *
  279. * @param latency The new latency value
  280. */
  281. void setLatency(String latency) {
  282. setLatency(Integer.parseInt(latency));
  283. }
  284. /**
  285. * Gets the font
  286. *
  287. * @return The font value
  288. */
  289. Font getFont() {
  290. Font font = new Font(
  291. LogViewer.getProperty(fontFamilyKey),
  292. getInt(fontStyleKey, 0),
  293. getInt(fontSizeKey, 0)
  294. );
  295. return font;
  296. }
  297. /**
  298. * Sets the font
  299. *
  300. * @param font The new font value
  301. */
  302. void setFont(Font font) {
  303. LogViewer.setProperty(fontFamilyKey, font.getFontName());
  304. setInt(fontStyleKey, font.getStyle());
  305. setInt(fontSizeKey, font.getSize());
  306. }
  307. /**
  308. * Gets the property as an int
  309. *
  310. * @param key The key of the property
  311. * @param def The value to use if none is found
  312. * @return The int value
  313. */
  314. private int getInt(String key, int def) {
  315. String val = LogViewer.getProperty(key);
  316. if (val != null) {
  317. def = Integer.parseInt(LogViewer.getProperty(key));
  318. }
  319. return def;
  320. }
  321. /**
  322. * Sets the property
  323. *
  324. * @param key The property key
  325. * @param value The new int value
  326. */
  327. private void setInt(String key, int value) {
  328. LogViewer.setProperty(key, String.valueOf(value));
  329. }
  330. /**
  331. * Gets the boolean property
  332. *
  333. * @param key The property key
  334. * @return The boolean value
  335. */
  336. private boolean getBoolean(String key) {
  337. return "true".equals(LogViewer.getProperty(key));
  338. }
  339. /**
  340. * Sets the boolean property
  341. *
  342. * @param key The property key
  343. * @param value The new boolean value
  344. */
  345. private void setBoolean(String key, boolean value) {
  346. LogViewer.setProperty(key, String.valueOf(value));
  347. }
  348. }