LUCENE-10261: Remove preset analyzer panel from Luke Analysis UI. (#475)

This commit is contained in:
Tomoko Uchida 2021-11-25 20:30:36 +09:00 committed by GitHub
parent 800f002e44
commit 40b38438c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 6 additions and 266 deletions

View File

@ -27,15 +27,11 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
@ -48,8 +44,6 @@ import org.apache.lucene.luke.app.desktop.components.dialog.analysis.TokenAttrib
import org.apache.lucene.luke.app.desktop.components.dialog.documents.AddDocumentDialogOperator;
import org.apache.lucene.luke.app.desktop.components.fragments.analysis.CustomAnalyzerPanelOperator;
import org.apache.lucene.luke.app.desktop.components.fragments.analysis.CustomAnalyzerPanelProvider;
import org.apache.lucene.luke.app.desktop.components.fragments.analysis.PresetAnalyzerPanelOperator;
import org.apache.lucene.luke.app.desktop.components.fragments.analysis.PresetAnalyzerPanelProvider;
import org.apache.lucene.luke.app.desktop.components.fragments.analysis.SimpleAnalyzeResultPanelOperator;
import org.apache.lucene.luke.app.desktop.components.fragments.analysis.SimpleAnalyzeResultPanelProvider;
import org.apache.lucene.luke.app.desktop.components.fragments.analysis.StepByStepAnalyzeResultPanelOperator;
@ -63,15 +57,10 @@ import org.apache.lucene.luke.app.desktop.util.StyleConstants;
import org.apache.lucene.luke.models.analysis.Analysis;
import org.apache.lucene.luke.models.analysis.AnalysisFactory;
import org.apache.lucene.luke.models.analysis.CustomAnalyzerConfig;
import org.apache.lucene.util.NamedThreadFactory;
/** Provider of the Analysis panel */
public final class AnalysisPanelProvider implements AnalysisTabOperator {
private static final String TYPE_PRESET = "preset";
private static final String TYPE_CUSTOM = "custom";
private final ComponentOperatorRegistry operatorRegistry;
private final AnalysisChainDialogFactory analysisChainDialogFactory;
@ -82,14 +71,8 @@ public final class AnalysisPanelProvider implements AnalysisTabOperator {
private final JPanel mainPanel = new JPanel();
private final JPanel preset;
private final JPanel custom;
private final JRadioButton presetRB = new JRadioButton();
private final JRadioButton customRB = new JRadioButton();
private final JLabel analyzerNameLbl = new JLabel();
private final JLabel showChainLbl = new JLabel();
@ -109,7 +92,6 @@ public final class AnalysisPanelProvider implements AnalysisTabOperator {
private Analysis analysisModel;
public AnalysisPanelProvider() throws IOException {
this.preset = new PresetAnalyzerPanelProvider().get();
this.custom = new CustomAnalyzerPanelProvider().get();
this.operatorRegistry = ComponentOperatorRegistry.getInstance();
@ -126,20 +108,13 @@ public final class AnalysisPanelProvider implements AnalysisTabOperator {
operatorRegistry.register(AnalysisTabOperator.class, this);
operatorRegistry
.get(PresetAnalyzerPanelOperator.class)
.get(CustomAnalyzerPanelOperator.class)
.ifPresent(
operator -> {
// Scanning all Analyzer types will take time...
ExecutorService executorService =
Executors.newFixedThreadPool(
1, new NamedThreadFactory("load-preset-analyzer-types"));
executorService.execute(
() -> {
operator.setPresetAnalyzers(analysisModel.getPresetAnalyzerTypes());
operator.setSelectedAnalyzer(analysisModel.currentAnalyzer().getClass());
});
executorService.shutdown();
operator.setAnalysisModel(analysisModel);
operator.resetAnalysisComponents();
});
stepByStepCB.setVisible(true);
}
public JPanel get() {
@ -161,38 +136,11 @@ public final class AnalysisPanelProvider implements AnalysisTabOperator {
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
mainPanel.add(initSwitcherPanel(), BorderLayout.PAGE_START);
mainPanel.add(preset, BorderLayout.CENTER);
mainPanel.add(custom, BorderLayout.CENTER);
return mainPanel;
}
private JPanel initSwitcherPanel() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING));
panel.setOpaque(false);
presetRB.setText(MessageUtils.getLocalizedMessage("analysis.radio.preset"));
presetRB.setActionCommand(TYPE_PRESET);
presetRB.addActionListener(listeners::toggleMainPanel);
presetRB.setOpaque(false);
presetRB.setSelected(true);
customRB.setText(MessageUtils.getLocalizedMessage("analysis.radio.custom"));
customRB.setActionCommand(TYPE_CUSTOM);
customRB.addActionListener(listeners::toggleMainPanel);
customRB.setOpaque(false);
customRB.setSelected(false);
ButtonGroup group = new ButtonGroup();
group.add(presetRB);
group.add(customRB);
panel.add(presetRB);
panel.add(customRB);
return panel;
}
private JPanel initLowerPanel() {
JPanel inner1 = new JPanel(new BorderLayout());
inner1.setOpaque(false);
@ -236,7 +184,7 @@ public final class AnalysisPanelProvider implements AnalysisTabOperator {
stepByStepCB.setText(MessageUtils.getLocalizedMessage("analysis.checkbox.step_by_step"));
stepByStepCB.setSelected(false);
stepByStepCB.setOpaque(false);
stepByStepCB.setVisible(false);
stepByStepCB.setVisible(true);
input.add(stepByStepCB);
JButton clearBtn = new JButton(MessageUtils.getLocalizedMessage("button.clear"));
@ -265,38 +213,6 @@ public final class AnalysisPanelProvider implements AnalysisTabOperator {
}
// control methods
void toggleMainPanel(String command) {
if (command.equalsIgnoreCase(TYPE_PRESET)) {
mainPanel.remove(custom);
mainPanel.add(preset, BorderLayout.CENTER);
operatorRegistry
.get(PresetAnalyzerPanelOperator.class)
.ifPresent(
operator -> {
operator.setPresetAnalyzers(analysisModel.getPresetAnalyzerTypes());
operator.setSelectedAnalyzer(analysisModel.currentAnalyzer().getClass());
});
stepByStepCB.setSelected(false);
stepByStepCB.setVisible(false);
} else if (command.equalsIgnoreCase(TYPE_CUSTOM)) {
mainPanel.remove(preset);
mainPanel.add(custom, BorderLayout.CENTER);
operatorRegistry
.get(CustomAnalyzerPanelOperator.class)
.ifPresent(
operator -> {
operator.setAnalysisModel(analysisModel);
operator.resetAnalysisComponents();
});
stepByStepCB.setVisible(true);
}
mainPanel.setVisible(false);
mainPanel.setVisible(true);
}
void executeAnalysis() {
String text = inputArea.getText();
if (Objects.isNull(text) || text.isEmpty()) {
@ -392,10 +308,6 @@ public final class AnalysisPanelProvider implements AnalysisTabOperator {
private class ListenerFunctions {
void toggleMainPanel(ActionEvent e) {
AnalysisPanelProvider.this.toggleMainPanel(e.getActionCommand());
}
void showAnalysisChain(MouseEvent e) {
AnalysisPanelProvider.this.showAnalysisChainDialog();
}

View File

@ -1,29 +0,0 @@
/*
* 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.lucene.luke.app.desktop.components.fragments.analysis;
import java.util.Collection;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.luke.app.desktop.components.ComponentOperatorRegistry;
/** Operator of the preset analyzer panel */
public interface PresetAnalyzerPanelOperator extends ComponentOperatorRegistry.ComponentOperator {
void setPresetAnalyzers(Collection<Class<? extends Analyzer>> presetAnalyzers);
void setSelectedAnalyzer(Class<? extends Analyzer> analyzer);
}

View File

@ -1,94 +0,0 @@
/*
* 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.lucene.luke.app.desktop.components.fragments.analysis;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.Collection;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.luke.app.desktop.components.AnalysisTabOperator;
import org.apache.lucene.luke.app.desktop.components.ComponentOperatorRegistry;
import org.apache.lucene.luke.app.desktop.util.MessageUtils;
/** Provider of the preset analyzer panel */
public final class PresetAnalyzerPanelProvider implements PresetAnalyzerPanelOperator {
private final ComponentOperatorRegistry operatorRegistry;
private final JComboBox<String> analyzersCB = new JComboBox<>();
private final ListenerFunctions listeners = new ListenerFunctions();
public PresetAnalyzerPanelProvider() {
this.operatorRegistry = ComponentOperatorRegistry.getInstance();
operatorRegistry.register(PresetAnalyzerPanelOperator.class, this);
}
public JPanel get() {
JPanel panel = new JPanel(new BorderLayout());
panel.setOpaque(false);
panel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
JLabel header = new JLabel(MessageUtils.getLocalizedMessage("analysis_preset.label.preset"));
panel.add(header, BorderLayout.PAGE_START);
JPanel center = new JPanel(new FlowLayout(FlowLayout.LEADING));
center.setOpaque(false);
center.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
center.setPreferredSize(new Dimension(400, 40));
analyzersCB.addActionListener(listeners::setAnalyzer);
analyzersCB.setEnabled(false);
center.add(analyzersCB);
panel.add(center, BorderLayout.CENTER);
return panel;
}
// control methods
@Override
public void setPresetAnalyzers(Collection<Class<? extends Analyzer>> presetAnalyzers) {
String[] analyzerNames = presetAnalyzers.stream().map(Class::getName).toArray(String[]::new);
ComboBoxModel<String> model = new DefaultComboBoxModel<>(analyzerNames);
analyzersCB.setModel(model);
analyzersCB.setEnabled(true);
}
@Override
public void setSelectedAnalyzer(Class<? extends Analyzer> analyzer) {
analyzersCB.setSelectedItem(analyzer.getName());
}
private class ListenerFunctions {
void setAnalyzer(ActionEvent e) {
operatorRegistry
.get(AnalysisTabOperator.class)
.ifPresent(
operator -> operator.setAnalyzerByType((String) analyzersCB.getSelectedItem()));
}
}
}

View File

@ -133,9 +133,6 @@ public interface Analysis {
}
}
/** Returns built-in {@link Analyzer}s. */
Collection<Class<? extends Analyzer>> getPresetAnalyzerTypes();
/** Returns available char filter names. */
Collection<String> getAvailableCharFilters();

View File

@ -20,7 +20,6 @@ package org.apache.lucene.luke.models.analysis;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.FileSystems;
@ -28,14 +27,12 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.CharFilterFactory;
@ -46,7 +43,6 @@ import org.apache.lucene.analysis.TokenizerFactory;
import org.apache.lucene.analysis.custom.CustomAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.luke.models.LukeException;
import org.apache.lucene.luke.util.reflection.ClassScanner;
import org.apache.lucene.util.AttributeImpl;
import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.IOUtils;
@ -54,8 +50,6 @@ import org.apache.lucene.util.IOUtils;
/** Default implementation of {@link AnalysisImpl} */
public final class AnalysisImpl implements Analysis {
private List<Class<? extends Analyzer>> presetAnalyzerTypes;
private Analyzer analyzer;
@Override
@ -84,25 +78,6 @@ public final class AnalysisImpl implements Analysis {
TokenFilterFactory.reloadTokenFilters(classLoader);
}
@Override
public Collection<Class<? extends Analyzer>> getPresetAnalyzerTypes() {
if (Objects.isNull(presetAnalyzerTypes)) {
List<Class<? extends Analyzer>> types = new ArrayList<>();
for (Class<? extends Analyzer> clazz : getInstantiableSubTypesBuiltIn(Analyzer.class)) {
try {
// add to presets if no args constructor is available
clazz.getConstructor();
types.add(clazz);
} catch (
@SuppressWarnings("unused")
NoSuchMethodException e) {
}
}
presetAnalyzerTypes = List.copyOf(types);
}
return presetAnalyzerTypes;
}
@Override
public Collection<String> getAvailableCharFilters() {
return CharFilterFactory.availableCharFilters().stream().sorted().collect(Collectors.toList());
@ -120,17 +95,6 @@ public final class AnalysisImpl implements Analysis {
.collect(Collectors.toList());
}
private <T> List<Class<? extends T>> getInstantiableSubTypesBuiltIn(Class<T> superType) {
ClassScanner scanner =
new ClassScanner("org.apache.lucene.analysis", getClass().getClassLoader());
Set<Class<? extends T>> types = scanner.scanSubTypes(superType);
return types.stream()
.filter(type -> !Modifier.isAbstract(type.getModifiers()))
.filter(type -> !type.getSimpleName().startsWith("Mock"))
.sorted(Comparator.comparing(Class::getName))
.collect(Collectors.toList());
}
@Override
public List<Token> analyze(String text) {
Objects.requireNonNull(text);

View File

@ -34,16 +34,6 @@ import org.junit.Test;
public class TestAnalysisImpl extends LuceneTestCase {
@Test
public void testGetPresetAnalyzerTypes() throws Exception {
AnalysisImpl analysis = new AnalysisImpl();
Collection<Class<? extends Analyzer>> analyerTypes = analysis.getPresetAnalyzerTypes();
assertNotNull(analyerTypes);
for (Class<? extends Analyzer> clazz : analyerTypes) {
clazz.getConstructor().newInstance();
}
}
@Test
public void testGetAvailableCharFilters() {
AnalysisImpl analysis = new AnalysisImpl();