Skip to content

Commit 154fd14

Browse files
committed
Move LuceneSentenceIndex out of util to patterns (only place where used)
1 parent 404adab commit 154fd14

File tree

5 files changed

+1048
-5
lines changed

5 files changed

+1048
-5
lines changed

src/edu/stanford/nlp/util/LuceneFieldType.java src/edu/stanford/nlp/patterns/LuceneFieldType.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package edu.stanford.nlp.util;
1+
package edu.stanford.nlp.patterns;
22

33
import org.apache.lucene.document.FieldType;
44
import org.apache.lucene.index.IndexOptions;

src/edu/stanford/nlp/patterns/LuceneSentenceIndex.java

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import edu.stanford.nlp.pipeline.CoreNLPProtos;
77
import edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer;
88
import edu.stanford.nlp.util.CollectionValuedMap;
9-
import edu.stanford.nlp.util.LuceneFieldType;
109
import edu.stanford.nlp.util.ArgumentParser;
1110
import edu.stanford.nlp.util.ArgumentParser.Option;
1211
import edu.stanford.nlp.util.logging.Redwood;
@@ -18,7 +17,6 @@
1817
import org.apache.lucene.search.*;
1918
import org.apache.lucene.store.Directory;
2019
import org.apache.lucene.store.FSDirectory;
21-
import org.apache.lucene.util.Version;
2220

2321
import java.io.*;
2422

src/edu/stanford/nlp/patterns/surface/PatternsForEachTokenLucene.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import edu.stanford.nlp.patterns.Pattern;
55
import edu.stanford.nlp.util.ArgumentParser;
66
import edu.stanford.nlp.util.ArgumentParser.Option;
7-
import edu.stanford.nlp.util.LuceneFieldType;
7+
import edu.stanford.nlp.patterns.LuceneFieldType;
88
import edu.stanford.nlp.util.logging.Redwood;
99
import org.apache.lucene.analysis.Analyzer;
1010
import org.apache.lucene.analysis.core.KeywordAnalyzer;
@@ -16,7 +16,6 @@
1616
import org.apache.lucene.store.Directory;
1717
import org.apache.lucene.store.FSDirectory;
1818
import org.apache.lucene.store.NIOFSDirectory;
19-
import org.apache.lucene.util.Version;
2019

2120
import java.io.*;
2221
import java.util.*;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package edu.stanford.nlp.trees.tregex.gui;
2+
3+
import java.awt.*;
4+
import java.awt.event.WindowAdapter;
5+
import java.awt.event.WindowListener;
6+
import java.lang.reflect.InvocationHandler;
7+
import java.lang.reflect.InvocationTargetException;
8+
import java.lang.reflect.Method;
9+
import java.lang.reflect.Proxy;
10+
11+
12+
/**
13+
* Class to do macOS-specific things for TregexGUI. This was initially written to use the (pre-2014, OS X)
14+
* {@code com.apple.eawt.*} library (AppleJavaExtensions.jar), but now uses the Java 9+ methods in
15+
* the {@code java.awt.Desktop} class. However, rather than calling them directly, we do things via
16+
* reflection, so that the CoreNLP code still compiles and runs under Java 8 (except for this macOS-specific
17+
* code for {@code TregexGUI}.
18+
*
19+
* @author rafferty
20+
* @author Christopher Manning
21+
*/
22+
public class OSXAdapter {
23+
24+
private static OSXAdapter adapter;
25+
private static Desktop desktop;
26+
private static TregexGUI mainApp;
27+
28+
private OSXAdapter (TregexGUI inApp) {
29+
mainApp = inApp;
30+
}
31+
32+
// implemented handler methods. These are basically hooks into existing
33+
// functionality from the main app, as if it came over from another platform.
34+
public static void handleAbout() {
35+
if (mainApp != null) {
36+
mainApp.about();
37+
} else {
38+
throw new IllegalStateException("handleAbout: TregexGUI instance detached from listener");
39+
}
40+
}
41+
42+
public static void handlePreferences() {
43+
if (mainApp != null) {
44+
mainApp.doPreferences();
45+
} else {
46+
throw new IllegalStateException("handlePreferences: TregexGUI instance detached from listener");
47+
}
48+
}
49+
50+
public static void handleQuit() {
51+
if (mainApp != null) {
52+
/*
53+
/ You MUST setHandled(false) if you want to delay or cancel the quit.
54+
/ This is important for cross-platform development -- have a universal quit
55+
/ routine that chooses whether or not to quit, so the functionality is identical
56+
/ on all platforms. This example simply cancels the AppleEvent-based quit and
57+
/ defers to that universal method.
58+
*/
59+
TregexGUI.doQuit();
60+
} else {
61+
throw new IllegalStateException("handleQuit: TregexGUI instance detached from listener");
62+
}
63+
}
64+
65+
66+
/** The main entry-point for this class. This is the only method
67+
* that needs to be called at runtime, and it can easily be done using reflection.
68+
*/
69+
public static void registerMacOSXApplication(TregexGUI inApp) {
70+
System.err.println("The registerMacOSXApplication method was called!!!");
71+
try {
72+
desktop = Desktop.getDesktop();
73+
74+
Class<?> aboutEventInterface = Class.forName("java.awt.desktop.AboutEvent");
75+
Object aboutEvent = aboutEventInterface.getDeclaredConstructor().newInstance();
76+
Class<?> aboutHandler = Class.forName("java.awt.desktop.AboutHandler");
77+
Object aboutHandlerInstance = Proxy.newProxyInstance(aboutHandler.getClassLoader(), new Class<?>[]{aboutHandler}, new InvocationHandler() {
78+
@Override
79+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
80+
81+
//Handle the invocations
82+
if (method.getName().equals("handleAbout")) {
83+
OSXAdapter.handleAbout();
84+
return null;
85+
}
86+
else return null;
87+
}
88+
});
89+
Method setAboutMethod = Desktop.class.getMethod("setAboutHandler", aboutHandler);
90+
setAboutMethod.invoke(aboutHandlerInstance, aboutHandlerInstance);
91+
} catch (ClassNotFoundException cnfe) {
92+
// ignore
93+
} catch (InvocationTargetException e) {
94+
e.printStackTrace();
95+
} catch (InstantiationException e) {
96+
e.printStackTrace();
97+
} catch (IllegalAccessException e) {
98+
e.printStackTrace();
99+
} catch (NoSuchMethodException e) {
100+
e.printStackTrace();
101+
}
102+
/*
103+
WindowListener windowListener = new WindowAdapter() { };
104+
Object oo = new Object();
105+
windowListener.windowActivated(oo);
106+
107+
Object x = new Object();
108+
desktop.setPreferencesHandler(x);
109+
110+
// desktop.setAboutHandler(e -> handleAbout());
111+
Class<?> aboutEvent = Proxy.newProxyInstance(OSXAdapter.class.getClassLoader(),
112+
new Class[] { Foo.class },
113+
handler);
114+
115+
Class<?>[] defArgs = { Class.forName("java.awt.desktop.AboutHandler")};
116+
Method registerMethod = osxAdapter.getDeclaredMethod("registerMacOSXApplication", defArgs);
117+
if (registerMethod != null) {
118+
Object[] args = { this };
119+
registerMethod.invoke(osxAdapter, args);
120+
}
121+
122+
desktop.setPreferencesHandler(e -> handlePreferences());
123+
desktop.setQuitHandler((e,r) -> handleQuit());
124+
*/
125+
}
126+
127+
}

0 commit comments

Comments
 (0)