Skip to content

Commit

Permalink
Move LuceneSentenceIndex out of util to patterns (only place where used)
Browse files Browse the repository at this point in the history
  • Loading branch information
manning committed Jul 29, 2024
1 parent 404adab commit 154fd14
Show file tree
Hide file tree
Showing 5 changed files with 1,048 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.stanford.nlp.util;
package edu.stanford.nlp.patterns;

import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.IndexOptions;
Expand Down
2 changes: 0 additions & 2 deletions src/edu/stanford/nlp/patterns/LuceneSentenceIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import edu.stanford.nlp.pipeline.CoreNLPProtos;
import edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer;
import edu.stanford.nlp.util.CollectionValuedMap;
import edu.stanford.nlp.util.LuceneFieldType;
import edu.stanford.nlp.util.ArgumentParser;
import edu.stanford.nlp.util.ArgumentParser.Option;
import edu.stanford.nlp.util.logging.Redwood;
Expand All @@ -18,7 +17,6 @@
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import java.io.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import edu.stanford.nlp.patterns.Pattern;
import edu.stanford.nlp.util.ArgumentParser;
import edu.stanford.nlp.util.ArgumentParser.Option;
import edu.stanford.nlp.util.LuceneFieldType;
import edu.stanford.nlp.patterns.LuceneFieldType;
import edu.stanford.nlp.util.logging.Redwood;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.KeywordAnalyzer;
Expand All @@ -16,7 +16,6 @@
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.NIOFSDirectory;
import org.apache.lucene.util.Version;

import java.io.*;
import java.util.*;
Expand Down
127 changes: 127 additions & 0 deletions src/edu/stanford/nlp/trees/tregex/gui/OSXAdapter.future
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package edu.stanford.nlp.trees.tregex.gui;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowListener;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


/**
* Class to do macOS-specific things for TregexGUI. This was initially written to use the (pre-2014, OS X)
* {@code com.apple.eawt.*} library (AppleJavaExtensions.jar), but now uses the Java 9+ methods in
* the {@code java.awt.Desktop} class. However, rather than calling them directly, we do things via
* reflection, so that the CoreNLP code still compiles and runs under Java 8 (except for this macOS-specific
* code for {@code TregexGUI}.
*
* @author rafferty
* @author Christopher Manning
*/
public class OSXAdapter {

private static OSXAdapter adapter;
private static Desktop desktop;
private static TregexGUI mainApp;

private OSXAdapter (TregexGUI inApp) {
mainApp = inApp;
}

// implemented handler methods. These are basically hooks into existing
// functionality from the main app, as if it came over from another platform.
public static void handleAbout() {
if (mainApp != null) {
mainApp.about();
} else {
throw new IllegalStateException("handleAbout: TregexGUI instance detached from listener");
}
}

public static void handlePreferences() {
if (mainApp != null) {
mainApp.doPreferences();
} else {
throw new IllegalStateException("handlePreferences: TregexGUI instance detached from listener");
}
}

public static void handleQuit() {
if (mainApp != null) {
/*
/ You MUST setHandled(false) if you want to delay or cancel the quit.
/ This is important for cross-platform development -- have a universal quit
/ routine that chooses whether or not to quit, so the functionality is identical
/ on all platforms. This example simply cancels the AppleEvent-based quit and
/ defers to that universal method.
*/
TregexGUI.doQuit();
} else {
throw new IllegalStateException("handleQuit: TregexGUI instance detached from listener");
}
}


/** The main entry-point for this class. This is the only method
* that needs to be called at runtime, and it can easily be done using reflection.
*/
public static void registerMacOSXApplication(TregexGUI inApp) {
System.err.println("The registerMacOSXApplication method was called!!!");
try {
desktop = Desktop.getDesktop();

Class<?> aboutEventInterface = Class.forName("java.awt.desktop.AboutEvent");
Object aboutEvent = aboutEventInterface.getDeclaredConstructor().newInstance();
Class<?> aboutHandler = Class.forName("java.awt.desktop.AboutHandler");
Object aboutHandlerInstance = Proxy.newProxyInstance(aboutHandler.getClassLoader(), new Class<?>[]{aboutHandler}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

//Handle the invocations
if (method.getName().equals("handleAbout")) {
OSXAdapter.handleAbout();
return null;
}
else return null;
}
});
Method setAboutMethod = Desktop.class.getMethod("setAboutHandler", aboutHandler);
setAboutMethod.invoke(aboutHandlerInstance, aboutHandlerInstance);
} catch (ClassNotFoundException cnfe) {
// ignore
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
/*
WindowListener windowListener = new WindowAdapter() { };
Object oo = new Object();
windowListener.windowActivated(oo);

Object x = new Object();
desktop.setPreferencesHandler(x);

// desktop.setAboutHandler(e -> handleAbout());
Class<?> aboutEvent = Proxy.newProxyInstance(OSXAdapter.class.getClassLoader(),
new Class[] { Foo.class },
handler);

Class<?>[] defArgs = { Class.forName("java.awt.desktop.AboutHandler")};
Method registerMethod = osxAdapter.getDeclaredMethod("registerMacOSXApplication", defArgs);
if (registerMethod != null) {
Object[] args = { this };
registerMethod.invoke(osxAdapter, args);
}

desktop.setPreferencesHandler(e -> handlePreferences());
desktop.setQuitHandler((e,r) -> handleQuit());
*/
}

}
Loading

0 comments on commit 154fd14

Please sign in to comment.