Skip to content

Content Stream Search Feature #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<artifactId>kernel</artifactId>
<version>${itext.version}</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>search</artifactId>
<version>${itext.version}</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
Expand Down Expand Up @@ -309,18 +314,6 @@
<groupId>sh.tak.appbundler</groupId>
<artifactId>appbundle-maven-plugin</artifactId>
<version>1.2.0</version>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-archiver</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>package</phase>
Expand All @@ -329,6 +322,18 @@
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-archiver</artifactId>
<version>3.6.0</version>
</dependency>
</dependencies>
<configuration>
<mainClass>com.itextpdf.rups.RupsLauncher</mainClass>
<dictionaryFile>Info.plist</dictionaryFile>
Expand All @@ -340,4 +345,4 @@
</build>
</profile>
</profiles>
</project>
</project>
28 changes: 27 additions & 1 deletion src/main/java/com/itextpdf/rups/RupsConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,27 @@ public enum RupsConfiguration {
INSTANCE;

private static final String DEFAULT_CONFIG_PATH = "/config/default.properties";
private static final String ICON_CONFIG_PATH = "/config/icon.properties";
private static final String DEFAULT_HOME_VALUE = "home";
private static final String CLOSE_OPERATION_KEY = "ui.closeoperation";
private static final String DUPLICATE_OPEN_FILES_KEY = "rups.duplicatefiles";
private static final String HOME_FOLDER_KEY = "user.home";
private static final String LOCALE_KEY = "user.locale";
private static final String LOOK_AND_FEEL_KEY = "ui.lookandfeel";
private static final String ICON_KEY = "ui.lookandfeel";

private final Preferences systemPreferences;
private final Properties defaultProperties;
private final Properties temporaryProperties;

private final Properties iconProperties;


RupsConfiguration() {
this.defaultProperties = loadDefaultProperties();
this.temporaryProperties = new Properties();
this.systemPreferences = Preferences.userNodeForPackage(RupsConfiguration.class);
this.iconProperties = loadIconProperties();

initializeSystemDefaults(this.defaultProperties, this.systemPreferences);
}
Expand Down Expand Up @@ -307,6 +313,22 @@ private Properties loadDefaultProperties() {
return properties;
}

private Properties loadIconProperties() {
final InputStream resourceAsStream = RupsConfiguration.class.getResourceAsStream(ICON_CONFIG_PATH);
final Properties properties = new Properties();

if (resourceAsStream != null) {
try {
properties.load(resourceAsStream);
resourceAsStream.close();
} catch (IOException e) {
LoggerHelper.error(Language.ERROR_LOADING_ICON_DEFINITIONS.getString(), e, RupsConfiguration.class);
}
}

return properties;
}

private void initializeSystemDefaults(Properties defaultProperties, Preferences systemPreferences) {
try {
final String[] keys = systemPreferences.keys();
Expand Down Expand Up @@ -337,4 +359,8 @@ private String getValueFromSystemPreferences(String key, String defaultValue) {
private String getValueFromSystemPreferences(String key) {
return this.systemPreferences.get(key, this.defaultProperties.getProperty(key));
}
}

public String getIconFor(String key){
return this.iconProperties.getProperty(key, "default");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ This file is part of the iText (R) project.
*/
package com.itextpdf.rups.controller;

import com.itextpdf.kernel.pdf.PdfObject;
import com.itextpdf.search.ISearchHandler;
import com.itextpdf.rups.model.PdfFile;

import java.awt.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ This file is part of the iText (R) project.
package com.itextpdf.rups.controller;

import com.itextpdf.rups.RupsConfiguration;
import com.itextpdf.search.ISearchHandler;
import com.itextpdf.rups.event.AllFilesClosedEvent;
import com.itextpdf.rups.event.OpenFileEvent;
import com.itextpdf.rups.event.RupsEvent;
Expand Down Expand Up @@ -77,6 +78,8 @@ public class RupsController extends Observable
public RupsController(Dimension dimension, RupsTabbedPane rupsTabbedPane) {
this.rupsTabbedPane = rupsTabbedPane;

//TODO: SearchHandler -> TabbedPane -> InstanceController, SearchHandler -> SearchBar.handler

this.dimension = dimension;
}

Expand All @@ -90,6 +93,10 @@ public Component getMasterComponent() {
return rupsTabbedPane.getJTabbedPane();
}

public ISearchHandler getSearchHandler(){
return rupsTabbedPane.getCurrentController().getSearchHandler();
}

@Override
public final void update(Observable o, Object arg) {
//Events that have come from non observable classes: ObjectLoader and FileChooserAction
Expand Down Expand Up @@ -127,6 +134,7 @@ public final void closeCurrentFile() {
}
}


@Override
public final PdfFile getCurrentFile() {
return this.rupsTabbedPane.getCurrentFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ This file is part of the iText (R) project.
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.utils.CompareTool;
import com.itextpdf.search.ISearchHandler;
import com.itextpdf.rups.controller.search.PdfSearchHandler;
import com.itextpdf.rups.event.CloseDocumentEvent;
import com.itextpdf.rups.event.PostCompareEvent;
import com.itextpdf.rups.event.RupsEvent;
Expand All @@ -57,6 +59,8 @@ This file is part of the iText (R) project.
import com.itextpdf.rups.view.Console;
import com.itextpdf.rups.view.Language;
import com.itextpdf.rups.view.PageSelectionListener;
import com.itextpdf.rups.view.RupsPanel;
import com.itextpdf.rups.view.RupsSearchBar;
import com.itextpdf.rups.view.contextmenu.ConsoleContextMenu;
import com.itextpdf.rups.view.contextmenu.ContextMenuMouseListener;
import com.itextpdf.rups.view.itext.treenodes.PdfObjectTreeNode;
Expand Down Expand Up @@ -105,6 +109,9 @@ public class RupsInstanceController extends Observable
*/
private final PdfReaderController readerController;

private final ISearchHandler searchHandler;

private RupsSearchBar searchBar;
/**
* Contains all other components: the page panel, the outline tree, etc.
*/
Expand Down Expand Up @@ -134,6 +141,13 @@ public RupsInstanceController(Dimension dimension, JPanel owner) {
readerController = new PdfReaderController(this, this);
addObserver(readerController);

searchHandler = new PdfSearchHandler();
searchBar = RupsSearchBar.getSearchBar(readerController.pdfTree);

//TODO: Find a neater way of identifying document section in focus.
readerController.getPdfTree().addTreeSelectionListener((TreeSelectionListener) searchHandler);
searchBar.setSearchHandler(((RupsPanel) owner).getRupsSearchHandler());

// creating the master component
masterComponent = new JSplitPane();
masterComponent.setOrientation(JSplitPane.VERTICAL_SPLIT);
Expand All @@ -158,6 +172,7 @@ public RupsInstanceController(Dimension dimension, JPanel owner) {
info.add(readerController.getObjectPanel(), JSplitPane.LEFT);
final JTabbedPane editorPane = readerController.getEditorTabs();
final JScrollPane cons = new JScrollPane(console.getTextArea());

console.getTextArea().addMouseListener(
new ContextMenuMouseListener(ConsoleContextMenu.getPopupMenu(console.getTextArea()),
console.getTextArea()));
Expand Down Expand Up @@ -362,6 +377,10 @@ public void valueChanged(TreeSelectionEvent evt) {
}
}

public ISearchHandler getSearchHandler() {
return searchHandler;
}

// page navigation

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.itextpdf.rups.controller.search;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfIndirectReference;
import com.itextpdf.kernel.pdf.PdfObject;
import com.itextpdf.search.model.ESearchScope;
import com.itextpdf.search.model.ISearchFilter;
import com.itextpdf.search.model.ISearchResult;
import com.itextpdf.search.model.SearchContext;

import javax.swing.event.TreeSelectionEvent;
import java.util.HashMap;
import java.util.Map;

public class PdfSearchHandler extends RupsSearchHandler<PdfIndirectReference> {

Map<PdfIndirectReference,Map<ESearchScope, SearchContext>> contexts;

PdfIndirectReference currentTarget;

public PdfSearchHandler(){
super();
contexts = new HashMap<>();
}


@Override
public ISearchResult find(SearchContext<PdfIndirectReference> searchContext, ISearchFilter filter) {
if((null == filter || (null == searchContext.getSearchScope())))
return null; //TODO: Replace this.

switch(searchContext.getSearchScope()) {
case ALL_DOCUMENTS:
break;
case DOCUMENT:
PdfDocument testdoc = searchContext.getTarget().getIndirectReference().getDocument();
break;
default:
case SELECTION:
PdfObject testobj = searchContext.getTarget().getIndirectReference().getRefersTo(true);
}
return null;
}

public ISearchFilter getNewFilter() {
return new PdfStreamSearchFilter();
}

@Override
public SearchContext<PdfIndirectReference> getNewContext(ESearchScope itemScope){
SearchContext<PdfIndirectReference> newContext = super.getNewContext();
newContext.setTarget(getCurrentTarget());
Map<ESearchScope, SearchContext> scopeContexts = contexts.getOrDefault(currentTarget, new HashMap<ESearchScope, SearchContext>());
scopeContexts.put(itemScope, newContext);
contexts.put(getCurrentTarget(), scopeContexts);
return getContext(currentTarget, ESearchScope.SELECTION);

}

@Override
public SearchContext<PdfIndirectReference> getContext(PdfIndirectReference currentTarget, ESearchScope selectedItem) {
Map<ESearchScope, SearchContext> scopeContexts = contexts.getOrDefault(currentTarget, new HashMap<ESearchScope, SearchContext>());
return scopeContexts.getOrDefault(currentTarget,getNewContext(selectedItem));
}

@Override
public PdfIndirectReference getCurrentTarget() {
return currentTarget;
}

@Override
public void valueChanged(TreeSelectionEvent e) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.itextpdf.rups.controller.search;

import com.itextpdf.search.model.ESearchOptions;
import com.itextpdf.search.model.ISearchFilter;

import java.util.Arrays;
import java.util.Collection;

public class PdfStreamSearchFilter implements ISearchFilter {
String query = "";
ESearchOptions[] options = {};
@Override
public boolean setQuery(String inputQuery) {
//TODO: Properly Validate input...
if(null != inputQuery)
this.query = inputQuery;
return (null !=query) && (query == inputQuery);
}

@Override
public boolean setOptions(ESearchOptions[] searchOptions) {
options = searchOptions;
return (null != options) && (options == searchOptions);
}

@Override
public String getQuery() {
return query;
}

@Override
public Collection<ESearchOptions> getOptions() {
return Arrays.asList(options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.itextpdf.rups.controller.search;

import com.itextpdf.kernel.pdf.PdfIndirectReference;
import com.itextpdf.kernel.pdf.PdfObject;
import com.itextpdf.rups.view.RupsPanel;
import com.itextpdf.search.model.ESearchOptions;
import com.itextpdf.search.model.ESearchScope;
import com.itextpdf.search.model.ISearchFilter;
import com.itextpdf.search.ISearchHandler;
import com.itextpdf.search.model.ISearchResult;
import com.itextpdf.search.model.SearchContext;
import com.itextpdf.search.SearchHandler;

import javax.swing.event.TreeSelectionListener;
import java.util.concurrent.atomic.AtomicBoolean;


public abstract class RupsSearchHandler<T extends PdfObject> extends SearchHandler<T> implements TreeSelectionListener {
private static ISearchHandler INSTANCE;

public RupsSearchHandler() {
if (null == INSTANCE) {
INSTANCE = this;
}

}

@Override
public ISearchResult find(SearchContext<T> context, ISearchFilter filter) {
AtomicBoolean case_sensitive = new AtomicBoolean(false);
AtomicBoolean regex = new AtomicBoolean(false);
AtomicBoolean word = new AtomicBoolean(false);

filter.getOptions().forEach((ESearchOptions option) -> {
case_sensitive.set(case_sensitive.get() || option == ESearchOptions.CASE_SENSITIVE);
regex.set((!word.get()) && (regex.get() || option == ESearchOptions.REGEX));
word.set((!regex.get()) && (word.get() || option == ESearchOptions.REGEX));
});

if(regex.get()){
// Regex Evaluation
} else if (word.get()) {
// Whole Word Matches only
} else {
// Default Search
}
// new ThreadedSearch

return null;
}

public static ISearchHandler getInstance(RupsPanel rupsPanel) {
return INSTANCE;
}

public abstract SearchContext<PdfIndirectReference> getNewContext(ESearchScope itemScope);
}
Loading