pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1705561 [6/6] - in /pdfbox/trunk: ./ debugger/ debugger/src/ debugger/src/main/ debugger/src/main/java/ debugger/src/main/java/org/ debugger/src/main/java/org/apache/ debugger/src/main/java/org/apache/pdfbox/ debugger/src/main/java/org/apa...
Date Sun, 27 Sep 2015 18:04:44 GMT
Added: pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/tools/util/RecentFiles.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/tools/util/RecentFiles.java?rev=1705561&view=auto
==============================================================================
--- pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/tools/util/RecentFiles.java (added)
+++ pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/tools/util/RecentFiles.java Sun
Sep 27 18:04:42 2015
@@ -0,0 +1,205 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.tools.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Queue;
+import java.util.prefs.Preferences;
+
+/**
+ * A class to save recent file history in preference using java Preference api.
+ */
+public class RecentFiles
+{
+    private static final String KEY = "recent_files_";
+    private static final String PATH_KEY = "recent_files_%d_%d";
+    private static final String PIECES_LENGTH_KEY = "recent_files_%d_length";
+    private static final String HISTORY_LENGTH = "history_length";
+
+    private final Preferences pref;
+    private Queue<String> filePaths;
+    private final int maximum;
+
+    /**
+     * Constructor.
+     *
+     * @param className the class for which this Recentfiles object is created and it will
be used
+     * to create preference instance.
+     * @param maximumFile the number of recent files to remember.
+     */
+    public RecentFiles(Class className, int maximumFile)
+    {
+        this.maximum = maximumFile;
+        this.pref = Preferences.userNodeForPackage(className);
+        filePaths = readHistoryFromPref();
+        if (filePaths == null)
+        {
+            filePaths = new ArrayDeque<String>();
+        }
+    }
+
+    /**
+     * Clear the previous recent file history.
+     */
+    public void removeAll()
+    {
+        filePaths.clear();
+    }
+
+    /**
+     * Check if file history is empty.
+     *
+     * @return if history is empty return true otherwise return false.
+     */
+    public boolean isEmpty()
+    {
+        return filePaths.isEmpty();
+    }
+
+    /**
+     * Add a new file in recent file history.
+     *
+     * @param path path to the file. this path means File#getPath() method returned String.
+     */
+    public void addFile(String path)
+    {
+        if (filePaths.size() >= maximum + 1 && path != null)
+        {
+            filePaths.remove();
+        }
+
+        filePaths.add(path);
+    }
+
+    /**
+     * Remove a file from recent file history.
+     *
+     * @param path path string to the file. this path means File#getPath() method returned
String.
+     */
+    public void removeFile(String path)
+    {
+        if (filePaths.contains(path))
+        {
+            filePaths.remove(path);
+        }
+    }
+
+    /**
+     * This gives the file in descending order where order is according to the time it is
added.
+     * This checks for file's existence in file history.
+     *
+     * @return return the file paths in a List.
+     */
+    public List<String> getFiles()
+    {
+        if (!isEmpty())
+        {
+            List<String> files = new ArrayList<String>();
+            for (String path : filePaths)
+            {
+                File file = new File(path);
+                if (file.exists())
+                {
+                    files.add(path);
+                }
+            }
+            if (files.size() > maximum)
+            {
+                files.remove(0);
+            }
+            return files;
+        }
+        return null;
+    }
+
+    /**
+     * This method save the present recent file history in the preference. To get the recent
file
+     * history in next session this method must be called.
+     *
+     * @throws IOException if saving in preference doesn't success.
+     */
+    public void close() throws IOException
+    {
+        writeHistoryToPref(filePaths);
+    }
+
+    private String[] breakString(String fullPath)
+    {
+        int allowedStringLength = Preferences.MAX_VALUE_LENGTH;
+        List<String> pieces = new ArrayList<String>();
+        int beginIndex = 0;
+        int remainingLength = fullPath.length();
+        int endIndex = 0;
+        while (remainingLength > 0)
+        {
+            endIndex += remainingLength >= allowedStringLength ? allowedStringLength :
remainingLength;
+            pieces.add(fullPath.substring(beginIndex, endIndex));
+            beginIndex = endIndex;
+            remainingLength = fullPath.length() - endIndex;
+        }
+        return pieces.toArray(new String[pieces.size()]);
+    }
+
+    private void writeHistoryToPref(Queue<String> filePaths)
+    {
+        if (filePaths.size() == 0)
+        {
+            return;
+        }
+        Preferences node = pref.node(KEY);
+        node.putInt(HISTORY_LENGTH, filePaths.size());
+        int fileCount = 1;
+        for (String path : filePaths)
+        {
+            String[] pieces = breakString(path);
+            node.putInt(String.format(PIECES_LENGTH_KEY, fileCount), pieces.length);
+            for (int i = 0; i < pieces.length; i++)
+            {
+                node.put(String.format(PATH_KEY, fileCount, i), pieces[i]);
+            }
+            fileCount++;
+        }
+    }
+
+    private Queue<String> readHistoryFromPref()
+    {
+        Preferences node = pref.node(KEY);
+        int historyLength = node.getInt(HISTORY_LENGTH, 0);
+        if (historyLength == 0)
+        {
+            return null;
+        }
+        Queue<String> history = new ArrayDeque<String>();
+
+        for (int i = 1; i <= historyLength; i++)
+        {
+            int totalPieces = node.getInt(String.format(PIECES_LENGTH_KEY, i), 0);
+            StringBuilder stringBuilder = new StringBuilder();
+            for (int j = 0; j < totalPieces; j++)
+            {
+                String piece = node.get(String.format(PATH_KEY, i, j), "");
+                stringBuilder.append(piece);
+            }
+            history.add(stringBuilder.toString());
+        }
+        return history;
+    }
+}

Propchange: pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/tools/util/RecentFiles.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/array.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/array.png?rev=1705561&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/array.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Added: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/boolean.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/boolean.png?rev=1705561&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/boolean.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Added: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/dict.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/dict.png?rev=1705561&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/dict.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Added: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/hex.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/hex.png?rev=1705561&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/hex.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Added: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/indirect.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/indirect.png?rev=1705561&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/indirect.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Added: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/integer.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/integer.png?rev=1705561&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/integer.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Added: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/name.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/name.png?rev=1705561&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/name.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Added: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/null.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/null.png?rev=1705561&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/null.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Added: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/page.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/page.png?rev=1705561&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/page.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Added: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/pdf.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/pdf.png?rev=1705561&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/pdf.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Added: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/real.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/real.png?rev=1705561&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/real.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Added: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/stream-dict.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/stream-dict.png?rev=1705561&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/stream-dict.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Added: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/string.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/string.png?rev=1705561&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/debugger/src/main/resources/org/apache/pdfbox/tools/pdfdebugger/string.png
------------------------------------------------------------------------------
    svn:mime-type = image/png

Modified: pdfbox/trunk/pom.xml
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pom.xml?rev=1705561&r1=1705560&r2=1705561&view=diff
==============================================================================
--- pdfbox/trunk/pom.xml (original)
+++ pdfbox/trunk/pom.xml Sun Sep 27 18:04:42 2015
@@ -49,6 +49,7 @@
     <module>pdfbox</module>
     <module>preflight</module>
     <module>preflight-app</module>
+    <module>debugger</module>
     <module>tools</module>
     <module>app</module>
     <module>examples</module>

Modified: pdfbox/trunk/tools/pom.xml
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/pom.xml?rev=1705561&r1=1705560&r2=1705561&view=diff
==============================================================================
--- pdfbox/trunk/tools/pom.xml (original)
+++ pdfbox/trunk/tools/pom.xml Sun Sep 27 18:04:42 2015
@@ -45,7 +45,7 @@
     </dependency>
     <dependency>
         <groupId>${project.groupId}</groupId>
-        <artifactId>pdfbox</artifactId>
+        <artifactId>pdfbox-debugger</artifactId>
         <version>${project.version}</version>
     </dependency>
     <dependency>

Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFReader.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFReader.java?rev=1705561&r1=1705560&r2=1705561&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFReader.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFReader.java Sun Sep 27 18:04:42
2015
@@ -48,6 +48,7 @@ import javax.swing.AbstractAction;
 import org.apache.pdfbox.pdmodel.PDPageTree;
 import org.apache.pdfbox.printing.PDFPageable;
 import org.apache.pdfbox.rendering.PDFRenderer;
+import org.apache.pdfbox.tools.util.ExtensionFileFilter;
 import org.apache.pdfbox.tools.util.RecentFiles;
 import org.apache.pdfbox.tools.gui.PageWrapper;
 import org.apache.pdfbox.tools.gui.ReaderBottomPanel;



Mime
View raw message