📄 mindmapmapmodel.java
字号:
return (MindMapNodeModel) mapElement.getMapChild(); } /** Returns pMinimumLength bytes of the files content. * @param file * @param pMinimumLength * @return * @throws FileNotFoundException * @throws IOException */ private StringBuffer readFileStart(File file, int pMinimumLength) { BufferedReader in=null; StringBuffer buffer = new StringBuffer(); try { // get the file start into the memory: in = new BufferedReader(new FileReader(file)); String str; while ((str = in.readLine()) != null) { buffer.append(str); if (buffer.length() >= pMinimumLength) break; } in.close(); } catch (Exception e) { e.printStackTrace(); return new StringBuffer(); } return buffer; } /** Creates a reader that pipes the input file through a XSLT-Script that * updates the version to the current. * @param file * @return * @throws IOException */ private Reader getUpdateReader(File file) throws IOException { StringWriter writer = null; InputStream inputStream = null; logger.info("Updating the file "+file.getName()+" to the current version."); try{ // try to convert map with xslt: URL updaterUrl=null; updaterUrl = getFrame().getResource("freemind/modes/mindmapmode/freemind_version_updater.xslt"); if(updaterUrl == null) { throw new IllegalArgumentException("freemind_version_updater.xslt not found."); } Source xsltSource=null; inputStream = updaterUrl.openStream(); xsltSource = new StreamSource(inputStream); // get output: writer = new StringWriter(); Result result = new StreamResult(writer); // create an instance of TransformerFactory TransformerFactory transFact = TransformerFactory.newInstance(); Transformer trans = transFact.newTransformer(xsltSource); trans.transform(new StreamSource(file), result); logger.info("Updating the file "+file.getName()+" to the current version. Done."); } catch(Exception ex) { ex.printStackTrace(); // exception: we take the file itself: return getActualReader(file); } finally { if(inputStream!= null) { inputStream.close(); } if(writer != null) { writer.close(); } } return new StringReader(writer.getBuffer().toString()); } /** Creates a default reader that just reads the given file. * @param file * @return * @throws FileNotFoundException */ private Reader getActualReader(File file) throws FileNotFoundException { return new BufferedReader(new FileReader(file)); } // // cut'n'paste //// (PN) see: super.cut(node) ... it does exactly the same!!!// public Transferable cut(MindMapNode node) {// Transferable transfer = copy(node);// super.cut(node);// return transfer;// } public Transferable copy(MindMapNode node) { StringWriter stringWriter = new StringWriter(); try { ((MindMapNodeModel)node).save(stringWriter, this.getLinkRegistry()); } catch (IOException e) {} return new MindMapNodesSelection(stringWriter.toString(), null, null, null, null, null); } private class LockManager extends TimerTask { File lockedSemaphoreFile = null; Timer lockTimer = null; final long lockUpdatePeriod = 4*60*1000; // four minutes final long lockSafetyPeriod = 5*60*1000; // five minutes String lockingUserOfOldLock = null; private File getSemaphoreFile(File mapFile) { return new File(mapFile.getParent()+System.getProperty("file.separator")+ "$~"+mapFile.getName()+"~"); } public synchronized String popLockingUserOfOldLock() { String toReturn = lockingUserOfOldLock; lockingUserOfOldLock = null; return toReturn; } private void writeSemaphoreFile(File inSemaphoreFile) throws Exception { FileOutputStream semaphoreOutputStream = new FileOutputStream(inSemaphoreFile); FileLock lock = null; try { lock = semaphoreOutputStream.getChannel().tryLock(); if (lock == null) { semaphoreOutputStream.close(); System.err.println("Locking failed."); throw new Exception(); }} // locking failed catch (UnsatisfiedLinkError eUle) {} // This may come with Windows95. We don't insist on detailed locking in that case. catch (NoClassDefFoundError eDcdf) {} // ^ just like above. // ^ On Windows95, the necessary libraries are missing. semaphoreOutputStream.write(System.getProperty("user.name").getBytes()); semaphoreOutputStream.write('\n'); semaphoreOutputStream.write(String.valueOf(System.currentTimeMillis()).getBytes()); semaphoreOutputStream.close(); semaphoreOutputStream = null; Tools.setHidden(inSemaphoreFile, true, /*synchro=*/false); // Exception free if (lock != null) lock.release(); } public synchronized String tryToLock(File file) throws Exception { // Locking should work for opening as well as for saving as. // We are especially carefull when it comes to exclusivity of writing. File semaphoreFile = getSemaphoreFile(file); if (semaphoreFile == lockedSemaphoreFile) { return null ; } try { BufferedReader semaphoreReader = new BufferedReader(new FileReader(semaphoreFile)); String lockingUser = semaphoreReader.readLine(); long lockTime = new Long (semaphoreReader.readLine()).longValue(); long timeDifference = System.currentTimeMillis() - lockTime; //catch (NumberFormatException enf) {} // This means that the time was not written at all - lock is corrupt if (timeDifference > lockSafetyPeriod) { // the lock is old semaphoreReader.close(); lockingUserOfOldLock = lockingUser; semaphoreFile.delete(); } else return lockingUser; } catch (FileNotFoundException e) {} writeSemaphoreFile(semaphoreFile); if (lockTimer == null) { lockTimer = new Timer(); lockTimer.schedule(this, lockUpdatePeriod, lockUpdatePeriod); } releaseLock(); lockedSemaphoreFile = semaphoreFile; return null; } public synchronized void releaseLock() { if (lockedSemaphoreFile != null) { lockedSemaphoreFile.delete(); lockedSemaphoreFile = null; }} // this may fail, TODO: ensure real deletion public synchronized void releaseTimer() { if (lockTimer != null) { lockTimer.cancel(); lockTimer = null; }} public synchronized void run() { // update semaphore file if (lockedSemaphoreFile == null) { System.err.println("unexpected: lockedSemaphoreFile is null upon lock update"); return; } try { Tools.setHidden(lockedSemaphoreFile, false, /*synchro=*/true); // Exception free // ^ We unhide the file before overwriting because JavaRE1.4.2 does // not let us open hidden files for writing. This is a workaround for Java bug, // I guess. writeSemaphoreFile(lockedSemaphoreFile); } catch (Exception e) {e.printStackTrace();}} } private class DummyLockManager extends LockManager { public synchronized String popLockingUserOfOldLock() { return null; } public synchronized String tryToLock(File file) throws Exception { return null; } public synchronized void releaseLock() {} public synchronized void releaseTimer() {} public synchronized void run() {} } private class doAutomaticSave extends TimerTask { private MindMapMapModel model; private Vector tempFileStack; private int numberOfFiles; private boolean filesShouldBeDeletedAfterShutdown; private File pathToStore; /** This value is compared with the result of getNumberOfChangesSinceLastSave(). If the values coincide, no further automatic saving is performed until the value changes again.*/ private int changeState; doAutomaticSave(MindMapMapModel model, int numberOfTempFiles, boolean filesShouldBeDeletedAfterShutdown, File pathToStore) { this.model = model; tempFileStack = new Vector(); numberOfFiles = ((numberOfTempFiles > 0)? numberOfTempFiles: 1); this.filesShouldBeDeletedAfterShutdown = filesShouldBeDeletedAfterShutdown; this.pathToStore = pathToStore; changeState = 0; } public void run() { /* Map is dirty enough? */ if(model.getNumberOfChangesSinceLastSave() == changeState) return; changeState = model.getNumberOfChangesSinceLastSave(); if(model.getNumberOfChangesSinceLastSave() == 0) { /* map was recently saved.*/ return; } try { EventQueue.invokeAndWait(new Runnable(){ public void run() { /* Now, it is dirty, we save it.*/ File tempFile; if(tempFileStack.size() >= numberOfFiles) tempFile = (File) tempFileStack.remove(0); // pop else { try { tempFile = File.createTempFile("FM_"+((model.toString()==null)?"unnamed":model.toString()), ".mm", pathToStore); if(filesShouldBeDeletedAfterShutdown) tempFile.deleteOnExit(); } catch (Exception e) { System.err.println("Error in automatic MindMapMapModel.save(): "+e.getMessage()); e.printStackTrace(); return; } } try { model.saveInternal(tempFile, true /*=internal call*/); model.getFrame().out("Map was automatically saved (using the file name "+tempFile+") ..."); } catch (Exception e) { System.err.println("Error in automatic MindMapMapModel.save(): "+e.getMessage()); e.printStackTrace(); } tempFileStack.add(tempFile); // add at the back. } }); } catch (InterruptedException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -