// {{{ getPrintJob() method private static PrinterJob getPrintJob(String jobName) { job = PrinterJob.getPrinterJob(); format = new HashPrintRequestAttributeSet(); String settings = jEdit.getSettingsDirectory(); if (settings != null) { String printSpecPath = MiscUtilities.constructPath(settings, "printspec"); File filePrintSpec = new File(printSpecPath); if (filePrintSpec.exists()) { try { FileInputStream fileIn = new FileInputStream(filePrintSpec); ObjectInputStream obIn = new ObjectInputStream(fileIn); format = (HashPrintRequestAttributeSet) obIn.readObject(); } catch (Exception e) { Log.log(Log.ERROR, BufferPrinter1_4.class, e); } // for backwards compatibility, the color variable is stored also as a property if (jEdit.getBooleanProperty("print.color")) format.add(Chromaticity.COLOR); else format.add(Chromaticity.MONOCHROME); // no need to always keep the same job name for every printout. format.add(new JobName(jobName, null)); } } return job; } // }}}
/** * Add the given file name to the list of recent files in the given Properties object, trimming * the length of the list to 10. If the given name is already in the list, move it to the top. */ public static void addRecentFile(Properties preferences, String newName) { // - if newName isn't already in the list, we add it to the top of the list. // - if it's already in the list, we move it to the top. ArrayList<String> list = MiscUtilities.parseRecentFiles(preferences); if (list.contains(newName)) { // move it to the top : list.remove(newName); list.add(0, newName); } else { list.add(0, newName); } // store to preferences : for (int i = 0; i < MAX_RECENT_FILES && i < list.size(); i++) { String key = KEY_RECENT_FILE + "." + Integer.toString(i + 1); // starts from 1 String val = list.get(i); preferences.setProperty(key, val); } }
// {{{ savePrintSpec() method private static void savePrintSpec() { String settings = jEdit.getSettingsDirectory(); if (settings == null) return; String printSpecPath = MiscUtilities.constructPath(settings, "printspec"); File filePrintSpec = new File(printSpecPath); try { FileOutputStream fileOut = new FileOutputStream(filePrintSpec); ObjectOutputStream obOut = new ObjectOutputStream(fileOut); obOut.writeObject(format); // for backwards compatibility, the color variable is stored also as a property Chromaticity cc = (Chromaticity) format.get(Chromaticity.class); if (cc != null) jEdit.setBooleanProperty("print.color", cc.getValue() == Chromaticity.COLOR.getValue()); } catch (Exception e) { e.printStackTrace(); } }
/** * Copy a file to another using VFS. * * @param progress the progress observer. It could be null if you don't want to monitor progress. * If not null you should probably launch this command in a WorkThread * @param sourceVFS the source VFS * @param sourceSession the VFS session * @param sourcePath the source path * @param targetVFS the target VFS * @param targetSession the target session * @param targetPath the target path * @param comp comp The component that will parent error dialog boxes * @param canStop could this copy be stopped ? * @return true if the copy was successful * @throws IOException IOException If an I/O error occurs * @since jEdit 4.3pre3 */ public static boolean copy( ProgressObserver progress, VFS sourceVFS, Object sourceSession, String sourcePath, VFS targetVFS, Object targetSession, String targetPath, Component comp, boolean canStop) throws IOException { if (progress != null) progress.setStatus("Initializing"); InputStream in = null; OutputStream out = null; try { VFSFile sourceVFSFile = sourceVFS._getFile(sourceSession, sourcePath, comp); if (sourceVFSFile == null) throw new FileNotFoundException(sourcePath); if (progress != null) { progress.setMaximum(sourceVFSFile.getLength()); } VFSFile targetVFSFile = targetVFS._getFile(targetSession, targetPath, comp); if (targetVFSFile.getType() == VFSFile.DIRECTORY) { if (targetVFSFile.getPath().equals(sourceVFSFile.getPath())) return false; targetPath = MiscUtilities.constructPath(targetPath, sourceVFSFile.getName()); } in = new BufferedInputStream( sourceVFS._createInputStream(sourceSession, sourcePath, false, comp)); out = new BufferedOutputStream(targetVFS._createOutputStream(targetSession, targetPath, comp)); boolean copyResult = IOUtilities.copyStream(IOBUFSIZE, progress, in, out, canStop); VFSManager.sendVFSUpdate(targetVFS, targetPath, true); return copyResult; } finally { IOUtilities.closeQuietly(in); IOUtilities.closeQuietly(out); } }
// {{{ run() method public void run() { /* if the VFS supports renaming files, we first * save to #<filename>#save#, then rename that * to <filename>, so that if the save fails, * data will not be lost. * * as of 4.1pre7 we now call vfs.getTwoStageSaveName() * instead of constructing the path directly * since some VFS's might not allow # in filenames. */ boolean vfsRenameCap = (vfs.getCapabilities() & VFS.RENAME_CAP) != 0; boolean wantTwoStage = wantTwoStageSave(buffer); boolean twoStageSave = vfsRenameCap && wantTwoStage; try { String[] args = {vfs.getFileName(path)}; setStatus(jEdit.getProperty("vfs.status.save", args)); // the entire save operation can be aborted... setAbortable(true); path = vfs._canonPath(session, path, view); if (!MiscUtilities.isURL(path)) path = MiscUtilities.resolveSymlinks(path); String savePath; if (twoStageSave) { savePath = vfs.getTwoStageSaveName(path); if (savePath == null) { throw new IOException("Can't get a temporary path for two-stage save: " + path); } } else { makeBackup(); savePath = path; } OutputStream out = vfs._createOutputStream(session, savePath, view); if (out == null) { buffer.setBooleanProperty(ERROR_OCCURRED, true); return; } try { // this must be after the stream is created or // we deadlock with SSHTools. buffer.readLock(); try { // Can't use buffer.getName() here because // it is not changed until the save is // complete if (path.endsWith(".gz")) buffer.setBooleanProperty(Buffer.GZIPPED, true); else if (buffer.getName().endsWith(".gz")) { // The path do not ends with gz. // The buffer name was .gz. // So it means it's blabla.txt.gz -> blabla.txt, I remove // the gz property buffer.setBooleanProperty(Buffer.GZIPPED, false); } if (buffer.getBooleanProperty(Buffer.GZIPPED)) out = new GZIPOutputStream(out); write(buffer, out); } finally { buffer.readUnlock(); } } finally { IOUtilities.closeQuietly(out); } if (twoStageSave) { makeBackup(); if (!vfs._rename(session, savePath, path, view)) throw new IOException("Rename failed: " + savePath); } if (!twoStageSave) VFSManager.sendVFSUpdate(vfs, path, true); } catch (FileNotFoundException e) { Log.log(Log.ERROR, this, "Unable to save buffer " + e); String[] pp = {e.getMessage()}; VFSManager.error(view, path, "ioerror.write-error", pp); buffer.setBooleanProperty(ERROR_OCCURRED, true); } catch (UnsupportedCharsetException e) { Log.log(Log.ERROR, this, e, e); String[] pp = {e.getCharsetName()}; VFSManager.error(view, path, "ioerror.unsupported-encoding-error", pp); buffer.setBooleanProperty(ERROR_OCCURRED, true); } catch (Exception e) { Log.log(Log.ERROR, this, e); String[] pp = {e.toString()}; VFSManager.error(view, path, "ioerror.write-error", pp); buffer.setBooleanProperty(ERROR_OCCURRED, true); } catch (WorkThread.Abort a) { buffer.setBooleanProperty(ERROR_OCCURRED, true); } finally { try { vfs._saveComplete(session, buffer, path, view); if (twoStageSave) { vfs._finishTwoStageSave(session, buffer, path, view); } // clean up left-over markers file if (!jEdit.getBooleanProperty("persistentMarkers")) vfs._delete(session, Buffer.getMarkersPath(vfs, path), view); vfs._endVFSSession(session, view); } catch (Exception e) { Log.log(Log.ERROR, this, e); String[] pp = {e.toString()}; VFSManager.error(view, path, "ioerror.write-error", pp); buffer.setBooleanProperty(ERROR_OCCURRED, true); } catch (WorkThread.Abort a) { buffer.setBooleanProperty(ERROR_OCCURRED, true); } } } // }}}
/** * Returns a temporary file name based on the given path. * * <p>By default jEdit first saves a file to <code>#<i>name</i>#save#</code> and then renames it * to the original file. However some virtual file systems might not support the <code>#</code> * character in filenames, so this method permits the VFS to override this behavior. * * <p>If this method returns <code>null</code>, two stage save will not be used for that * particular file (introduced in jEdit 4.3pre1). * * @param path The path name * @since jEdit 4.1pre7 */ public String getTwoStageSaveName(String path) { return MiscUtilities.constructPath(getParentOfPath(path), '#' + getFileName(path) + "#save#"); } // }}}
// {{{ recursive listFiles() method private void listFiles( Object session, Collection<String> stack, List<String> files, String directory, VFSFileFilter filter, boolean recursive, Component comp, boolean skipBinary, boolean skipHidden) throws IOException { String resolvedPath = directory; if (recursive && !MiscUtilities.isURL(directory)) { resolvedPath = MiscUtilities.resolveSymlinks(directory); /* * If looking at a symlink, do not traverse the * resolved path more than once. */ if (!directory.equals(resolvedPath)) { if (stack.contains(resolvedPath)) { Log.log(Log.ERROR, this, "Recursion in listFiles(): " + directory); return; } stack.add(resolvedPath); } } Thread ct = Thread.currentThread(); WorkThread wt = null; if (ct instanceof WorkThread) { wt = (WorkThread) ct; } VFSFile[] _files = _listFiles(session, directory, comp); if (_files == null || _files.length == 0) return; for (int i = 0; i < _files.length; i++) { if (wt != null && wt.isAborted()) break; VFSFile file = _files[i]; if (skipHidden && (file.isHidden() || MiscUtilities.isBackup(file.getName()))) continue; if (!filter.accept(file)) continue; if (file.getType() == VFSFile.DIRECTORY || file.getType() == VFSFile.FILESYSTEM) { if (recursive) { String canonPath = _canonPath(session, file.getPath(), comp); listFiles( session, stack, files, canonPath, filter, recursive, comp, skipBinary, skipHidden); } } else // It's a regular file { if (skipBinary) { try { if (file.isBinary(session)) { Log.log(Log.NOTICE, this, file.getPath() + ": skipped as a binary file"); continue; } } catch (IOException e) { Log.log(Log.ERROR, this, e); // may be not binary... } } files.add(file.getPath()); } } } // }}}