00001 # Copyright (C) 1997-2004 The CDG Team <cdg@nats.informatik.uni-hamburg.de> 00002 # 00003 # This file is free software; as a special exception the author gives 00004 # unlimited permission to copy and/or distribute it, with or without 00005 # modifications, as long as this notice is preserved. 00006 # 00007 # This program is distributed in the hope that it will be useful, but 00008 # WITHOUT ANY WARRANTY, to the extent permitted by law; without even the 00009 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00010 00011 ## ---------------------------------------------------------------------------- 00012 ## YadaPlainDocument - abstract yada document. 00013 ## \ingroup YadaDocuments 00014 ## 00015 ## \author Michael Daum 00016 ## 00017 ## $Id: YadaPlainDocument.tcl,v 1.11 2004/05/11 14:30:47 ddreyer Exp $ 00018 ## ---------------------------------------------------------------------------- 00019 class YadaPlainDocument { 00020 00021 # variables ---------------------------------------------------------------- 00022 00023 ## name of the document 00024 public variable name "" 00025 00026 ## flag indicating whether this object functions as combined model-view object (value "true"), 00027 ## or as a pure model object (value "false": has to be set explicitly when instantiating object) 00028 public variable hasView "true" 00029 00030 ## flag indicating initialization. 00031 ## Possible falues are 0 and 1. init() sets this to 1 when it is called 00032 ## in the init chain calls of inheriting classes, 00033 ## i.e. YadaConfigDocument::init() or YadaRunnableDocument::init(). 00034 protected variable _isInitialized 0 00035 00036 protected variable _modified 0 00037 00038 ## Denotes time of most recent modification of document's XML file 00039 ## CAUSED by this object. This allows recognizing cases where a second 00040 ## object modifies the same file concurrently, in which case the need 00041 ## may arise to synchronize the object with the new file contents. 00042 protected variable _lastModificationTime 0 00043 00044 00045 # methods ------------------------------------------------------------------ 00046 public method load {} 00047 public method save {} 00048 public method toDOM {}; ## virtual 00049 public method fromDOM {node}; ## virtual \type domNode 00050 public method activationHandle {}; ## \virtual 00051 public method deActivationHandle {}; ## \virtual 00052 public method init {} ;## \virtual 00053 public method setToggle {name {value ""}}; ## \type TclString, TclRef, \virtual 00054 00055 public method hasSavedState {} 00056 public method isModified {} 00057 public method setModified {} 00058 public method resetModified {} 00059 public method displayTitle {} 00060 public method getPersistanceFileName {} ; ## virtual 00061 public method lastChanged {} 00062 00063 protected method updateModificationTime {} 00064 00065 constructor {} {} 00066 00067 }; 00068 00069 ## ---------------------------------------------------------------------------- 00070 ## constructor 00071 ## ---------------------------------------------------------------------------- 00072 body YadaPlainDocument::constructor {} { 00073 00074 } 00075 00076 ## ---------------------------------------------------------------------------- 00077 ## activationHandle 00078 ## ---------------------------------------------------------------------------- 00079 body YadaPlainDocument::activationHandle {} { 00080 displayTitle 00081 00082 } 00083 00084 ## ---------------------------------------------------------------------------- 00085 ## deActivationHandle 00086 ## ---------------------------------------------------------------------------- 00087 body YadaPlainDocument::deActivationHandle {} { 00088 } 00089 00090 ## ---------------------------------------------------------------------------- 00091 ## init 00092 ## ---------------------------------------------------------------------------- 00093 body YadaPlainDocument::init {} { 00094 if {!$_isInitialized} { 00095 set _isInitialized 1 00096 } 00097 } 00098 00099 ## ---------------------------------------------------------------------------- 00100 ## setToggle 00101 ## ---------------------------------------------------------------------------- 00102 body YadaPlainDocument::setToggle {name {value ""}} { 00103 } 00104 00105 00106 ## ---------------------------------------------------------------------------- 00107 ## Loads current document from file (file name depends on document name) 00108 ## ---------------------------------------------------------------------------- 00109 body YadaPlainDocument::load {} { 00110 set _lastUpdate 0 ; ## reset in case there is some value left from last load operation 00111 00112 set oldCursors [setBusy] 00113 00114 set zippedFileName [getPersistanceFileName].gz 00115 if { ![file exists $zippedFileName]} { 00116 resetBusy $oldCursors 00117 error "File $zippedFileName not found." 00118 } 00119 00120 set fileID [open "|zcat $zippedFileName" {RDONLY}] 00121 set xmlData [read $fileID] 00122 close $fileID 00123 set document [dom parse $xmlData] 00124 resetModified 00125 ## Modified state may be changed by overridden fromDOM methods 00126 fromDOM [$document documentElement] 00127 00128 resetBusy $oldCursors 00129 displayTitle 00130 updateModificationTime 00131 } 00132 00133 ## ---------------------------------------------------------------------------- 00134 ## Saves current document to file (file name depends on document name) 00135 ## ---------------------------------------------------------------------------- 00136 body YadaPlainDocument::save {} { 00137 set oldCursors [setBusy] 00138 set domNode [toDOM] 00139 set fileNameToSave [getPersistanceFileName] 00140 00141 if { [file exists ${fileNameToSave}.gz] } { 00142 file delete ${fileNameToSave}.gz 00143 } 00144 00145 set fileID [open $fileNameToSave {CREAT WRONLY}] 00146 puts $fileID [$domNode asXML] 00147 close $fileID 00148 00149 if {[catch {exec gzip -f $fileNameToSave} errMsg]} { 00150 _resetBusy $oldCursors 00151 error "Saving failed: $errMsg" 00152 } 00153 resetModified 00154 resetBusy $oldCursors 00155 displayTitle 00156 00157 updateModificationTime 00158 } 00159 00160 00161 ## ---------------------------------------------------------------------------- 00162 ## Predicate returning 1, if configuration data has changed, 0 otherwise 00163 ## ---------------------------------------------------------------------------- 00164 body YadaPlainDocument::isModified {} { 00165 return $_modified 00166 } 00167 00168 ## ---------------------------------------------------------------------------- 00169 ## Marks item as modified 00170 ## ---------------------------------------------------------------------------- 00171 body YadaPlainDocument::setModified {} { 00172 set _modified 1 00173 displayTitle 00174 } 00175 00176 ## ---------------------------------------------------------------------------- 00177 ## Removes modification mark 00178 ## ---------------------------------------------------------------------------- 00179 body YadaPlainDocument::resetModified {} { 00180 set _modified 0 00181 displayTitle 00182 } 00183 00184 ## ---------------------------------------------------------------------------- 00185 ## Predicate returning 1, if file containing persistant state of YadaGrammar 00186 ## exists, otherwise 0 00187 ## ---------------------------------------------------------------------------- 00188 body YadaPlainDocument::hasSavedState {} { 00189 set zippedFileName [getPersistanceFileName].gz 00190 if { [file exists $zippedFileName] } { 00191 return 1 00192 } else { 00193 return 0 00194 } 00195 } 00196 00197 00198 00199 00200 ## ---------------------------------------------------------------------------- 00201 ## Displays title in main window (reflecting modification state of document) 00202 ## ---------------------------------------------------------------------------- 00203 body YadaPlainDocument::displayTitle {} { 00204 if { [isModified] && $name != "<none>" } { 00205 wm title . "$name *** changed *** " 00206 } else { 00207 wm title . "$name" 00208 } 00209 } 00210 00211 ## ---------------------------------------------------------------------------- 00212 ## Returns time of last update to document XML file or an empty string, 00213 ## if there is no such file 00214 ## ---------------------------------------------------------------------------- 00215 body YadaPlainDocument::lastChanged {} { 00216 if { [hasSavedState] } { 00217 set zippedFileName [getPersistanceFileName].gz 00218 file stat $zippedFileName fileAttribs 00219 return $fileAttribs(ctime) 00220 } else { 00221 return "" 00222 } 00223 00224 } 00225 00226 ## ---------------------------------------------------------------------------- 00227 ## Synchronizes internal modification attribute with modification time of 00228 ## document's XML file 00229 ## ---------------------------------------------------------------------------- 00230 body YadaPlainDocument::updateModificationTime {} { 00231 file stat [getPersistanceFileName].gz fileAttribs 00232 set _lastModificationTime $fileAttribs(ctime) 00233 } 00234