001/* 002 * Copyright (c) 2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2013-02-21 23:15:22 +0000 (Thu, 21 Feb 2013) $' 007 * '$Revision: 31481 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030/** 031 * 032 */ 033package org.kepler.gui.component; 034 035import java.io.File; 036import java.io.FileInputStream; 037import java.io.FileNotFoundException; 038import java.io.FileOutputStream; 039import java.io.IOException; 040import java.io.InputStream; 041import java.io.ObjectInput; 042import java.io.ObjectInputStream; 043import java.io.ObjectOutputStream; 044import java.io.OutputStream; 045 046import org.apache.commons.logging.Log; 047import org.apache.commons.logging.LogFactory; 048import org.kepler.util.DotKeplerManager; 049 050/** 051 * @author Aaron Schultz 052 * 053 */ 054public class ShowFolders { 055 private static final Log log = LogFactory.getLog(ShowFolders.class 056 .getName()); 057 private static final boolean isDebugging = log.isDebugEnabled(); 058 059 /** 060 * location of the persistent file on disk 061 */ 062 private String _saveFileName; 063 064 /** 065 * whether or not to show folders in the component tab 066 */ 067 private Boolean _showFolders = null; 068 069 /** 070 * Constructor 071 */ 072 public ShowFolders() { 073 074 // Set up file name for storing boolean 075 File modDir = DotKeplerManager.getInstance() 076 .getTransientModuleDirectory("gui"); 077 if (modDir != null) { 078 _saveFileName = modDir.toString(); 079 } else { 080 _saveFileName = DotKeplerManager.getDotKeplerPath(); 081 } 082 if (!_saveFileName.endsWith(File.separator)) { 083 _saveFileName += File.separator; 084 } 085 _saveFileName += "ShowFolders"; 086 087 init(); 088 089 } 090 091 /** 092 * Load saved state 093 */ 094 private void init() { 095 File saveFile = new File(_saveFileName); 096 097 if (saveFile.exists()) { 098 if (isDebugging) { 099 log.debug("ShowFolders exists: " + saveFile.toString()); 100 } 101 102 try { 103 InputStream is = null; 104 ObjectInput oi = null; 105 try { 106 is = new FileInputStream(saveFile); 107 oi = new ObjectInputStream(is); 108 Object newObj = oi.readObject(); 109 _showFolders = (Boolean) newObj; 110 return; 111 } finally { 112 if(oi != null) { 113 oi.close(); 114 } 115 if(is != null) { 116 is.close(); 117 } 118 } 119 } catch (Exception e1) { 120 // problem reading file, try to delete it 121 log.warn("Exception while reading localSaveRepoFile: " 122 + e1.getMessage()); 123 try { 124 saveFile.delete(); 125 } catch (Exception e2) { 126 log.warn("Unable to delete localSaveRepoFile: " 127 + e2.getMessage()); 128 } 129 } 130 } else { 131 // default to true 132 set(true); 133 } 134 135 } 136 137 /** 138 * Set whether or not folders should be shown in the Component Tab. 139 * 140 * @param showFolders 141 * true if the folders should show up in the component tab 142 */ 143 public void set(Boolean showFolders) { 144 if (_showFolders != null && showFolders.equals(_showFolders)) { 145 // do nothing 146 } else { 147 _showFolders = showFolders; 148 serializeToDisk(); 149 } 150 } 151 152 /** 153 * Set whether or not folders should be shown in the Component Tab. 154 * 155 * @param showFolders 156 * true if the folders should show up in the component tab 157 */ 158 public void set(boolean showFolders) { 159 set(new Boolean(showFolders)); 160 } 161 162 /** 163 * 164 * @return true if the folders should show up in the component tab 165 */ 166 public boolean show() { 167 return _showFolders.booleanValue(); 168 } 169 170 /** 171 * Save state to the module read/write area as a Java serialized object 172 */ 173 private void serializeToDisk() { 174 if (isDebugging) 175 log.debug("serializeToDisk()"); 176 177 File saveFile = new File(_saveFileName); 178 if (saveFile.exists()) { 179 if (isDebugging) 180 log.debug("delete " + saveFile); 181 saveFile.delete(); 182 } 183 try { 184 ObjectOutputStream oos = null; 185 try { 186 OutputStream os = new FileOutputStream(saveFile); 187 oos = new ObjectOutputStream(os); 188 oos.writeObject(_showFolders); 189 oos.flush(); 190 if (isDebugging) { 191 log.debug("wrote " + saveFile); 192 } 193 } finally { 194 if(oos != null) { 195 oos.close(); 196 } 197 } 198 } catch (FileNotFoundException e) { 199 e.printStackTrace(); 200 } catch (IOException e) { 201 e.printStackTrace(); 202 } 203 } 204}