Skip to content

Commit

Permalink
#20 Optimize Preferences dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkosertic committed Oct 6, 2017
1 parent ddbc2e5 commit 6b54082
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 219 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>8.0.6</version>
<version>9.0.0</version>
</dependency>
<dependency>
<groupId>com.artofsolving</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* FreeDesktopSearch - A Search Engine for your Desktop
* Copyright (C) 2013 Mirko Sertic
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
package de.mirkosertic.desktopsearch;

import javafx.beans.value.ObservableValue;
import javafx.scene.control.CheckBox;
import org.controlsfx.control.PropertySheet;
import org.controlsfx.property.editor.AbstractPropertyEditor;

public class BooleanPropertyEditor extends AbstractPropertyEditor<Boolean, CheckBox> {

public BooleanPropertyEditor(PropertySheet.Item property) {
super(property, new CheckBox());
}

@Override
protected ObservableValue<Boolean> getObservableValue() {
return getEditor().selectedProperty();
}

@Override
public void setValue(Boolean aBoolean) {
getEditor().selectedProperty().setValue(aBoolean);
}
}
225 changes: 84 additions & 141 deletions src/main/java/de/mirkosertic/desktopsearch/ConfigurationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,65 +15,22 @@
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ListView;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import org.controlsfx.control.PropertySheet;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.*;

public class ConfigurationController {

@FXML
CheckBox showSimilarResults;

@FXML
Slider numberDocuments;

@FXML
ListView indexedDirectories;

@FXML
Button buttonAdd;
private static String CATEGORY_COMMON = "Common";
private static String CATEGORY_SUGGEST = "Suggestion";

@FXML
Button buttonRemove;

@FXML
Button buttonCancel;
PropertySheet propertySheet;

@FXML
Button buttonOk;

@FXML
VBox enabledDocumentFormats;

@FXML
VBox enabledLanguages;

@FXML
Slider numberSuggestions;

@FXML
Slider windowBeforeSpan;

@FXML
Slider windowAfterSpan;

@FXML
Slider slopForSuggestionSpan;

@FXML
CheckBox suggestionsInOrder;

private ConfigurationManager configurationManager;
private Stage stage;

Expand All @@ -84,116 +41,102 @@ public class ConfigurationController {
private final Map<SupportedLanguage, CheckBox> supportedLanguages = new HashMap<>();

public void initialize(ConfigurationManager aConfigurationManager, Stage aStage) {
Objects.requireNonNull(showSimilarResults);
Objects.requireNonNull(numberDocuments);
Objects.requireNonNull(indexedDirectories);
Objects.requireNonNull(buttonAdd);
Objects.requireNonNull(buttonRemove);
Objects.requireNonNull(buttonCancel);
Objects.requireNonNull(propertySheet);
Objects.requireNonNull(buttonOk);
Objects.requireNonNull(enabledDocumentFormats);
Objects.requireNonNull(enabledLanguages);
Objects.requireNonNull(numberSuggestions);
Objects.requireNonNull(windowBeforeSpan);
Objects.requireNonNull(windowAfterSpan);
Objects.requireNonNull(slopForSuggestionSpan);
Objects.requireNonNull(suggestionsInOrder);

buttonRemove.setOnAction(actionEvent -> removeSelectedLocation());
buttonAdd.setOnAction(actionEvent -> addNewLocation());

buttonOk.setOnAction(actionEvent -> ok());
buttonCancel.setOnAction(actionEvent -> cancel());

stage = aStage;
configurationManager = aConfigurationManager;

Configuration theConfiguration = configurationManager.getConfiguration();

showSimilarResults.setSelected(theConfiguration.isShowSimilarDocuments());
numberDocuments.setValue(theConfiguration.getNumberOfSearchResults());
numberSuggestions.setValue(theConfiguration.getNumberOfSuggestions());
windowBeforeSpan.setValue(theConfiguration.getSuggestionWindowBefore());
windowAfterSpan.setValue(theConfiguration.getSuggestionWindowAfter());
slopForSuggestionSpan.setValue(theConfiguration.getSuggestionSlop());
indexedDirectories.getItems().addAll(theConfiguration.getCrawlLocations());
suggestionsInOrder.setSelected(theConfiguration.isSuggestionInOrder());

// Ok, we build the document type selections
for (SupportedDocumentType theType : SupportedDocumentType.values()) {
CheckBox theCheckBox = new CheckBox(theType.getDisplayName(Locale.ENGLISH));
theCheckBox.setSelected(theConfiguration.getEnabledDocumentTypes().contains(theType));
enabledDocumentFormats.getChildren().add(theCheckBox);

supportedDocuments.put(theType, theCheckBox);
}

// And also the languages
for (SupportedLanguage theLanguage : SupportedLanguage.values()) {
CheckBox theCheckbox = new CheckBox(theLanguage.toLocale().getDisplayName(Locale.ENGLISH));
theCheckbox.setSelected(theConfiguration.getEnabledLanguages().contains(theLanguage));
enabledLanguages.getChildren().add(theCheckbox);

supportedLanguages.put(theLanguage, theCheckbox);
}
}
propertySheet.getItems().add(new PropertyEditorItem(Integer.class, CATEGORY_COMMON, "Max number of documents in search result", SpinnerPropertyEditor.class) {

private void ok() {
@Override
public Object getValue() {
return configurationManager.getConfiguration().getNumberOfSearchResults();
}

Configuration theConfiguration = configurationManager.getConfiguration();

for (Configuration.CrawlLocation theLocation : addedLocations) {
theConfiguration = theConfiguration.addLocation(theLocation);
}
for (Configuration.CrawlLocation theRemovedLocation : removedLocations) {
theConfiguration = theConfiguration.removeLocation(theRemovedLocation);
}
theConfiguration = theConfiguration.updateIncludeSimilarDocuments(showSimilarResults.isSelected());
theConfiguration = theConfiguration.updateNumberOfSearchResults((int) numberDocuments.getValue());
theConfiguration = theConfiguration.updateNumberOfSuggestions((int) numberSuggestions.getValue());
theConfiguration = theConfiguration.updateSuggestionWindowBefore((int) windowBeforeSpan.getValue());
theConfiguration = theConfiguration.updateSuggestionWindowAfter((int) windowAfterSpan.getValue());
theConfiguration = theConfiguration.updateSuggestionSlop((int) slopForSuggestionSpan.getValue());
theConfiguration = theConfiguration.updateSuggestionsInOrder(suggestionsInOrder.isSelected());

for (Map.Entry<SupportedDocumentType, CheckBox> theEntry : supportedDocuments.entrySet()) {
if (theEntry.getValue().isSelected()) {
theConfiguration = theConfiguration.enableDocumentType(theEntry.getKey());
} else {
theConfiguration = theConfiguration.disableDocumentType(theEntry.getKey());
@Override
public void setValue(Object o) {
configurationManager.updateConfiguration(configurationManager.getConfiguration().updateNumberOfSearchResults((Integer) o));
}
}
});
propertySheet.getItems().add(new PropertyEditorItem(boolean.class, CATEGORY_COMMON, "Show similar documents", BooleanPropertyEditor.class) {

for (Map.Entry<SupportedLanguage, CheckBox> theEntry : supportedLanguages.entrySet()) {
if (theEntry.getValue().isSelected()) {
theConfiguration = theConfiguration.enableLanguage(theEntry.getKey());
} else {
theConfiguration = theConfiguration.disableLanguage(theEntry.getKey());
@Override
public Object getValue() {
return configurationManager.getConfiguration().isShowSimilarDocuments();
}
}

@Override
public void setValue(Object o) {
configurationManager.updateConfiguration(configurationManager.getConfiguration().updateIncludeSimilarDocuments((Boolean) o));
}
});
propertySheet.getItems().add(new PropertyEditorItem(Integer.class, CATEGORY_SUGGEST, "Max number of suggestions", SpinnerPropertyEditor.class) {

configurationManager.updateConfiguration(theConfiguration);
stage.hide();
}
@Override
public Object getValue() {
return configurationManager.getConfiguration().getNumberOfSuggestions();
}

private void cancel() {
stage.hide();
}
@Override
public void setValue(Object o) {
configurationManager.updateConfiguration(configurationManager.getConfiguration().updateNumberOfSuggestions((Integer) o));
}
});
propertySheet.getItems().add(new PropertyEditorItem(Integer.class, CATEGORY_SUGGEST, "Number of words before suggestion window", SpinnerPropertyEditor.class) {

@Override
public Object getValue() {
return configurationManager.getConfiguration().getSuggestionWindowBefore();
}

private void removeSelectedLocation() {
removedLocations.addAll(indexedDirectories.getSelectionModel().getSelectedItems());
indexedDirectories.getItems().removeAll(indexedDirectories.getSelectionModel().getSelectedItems());
@Override
public void setValue(Object o) {
configurationManager.updateConfiguration(configurationManager.getConfiguration().updateSuggestionWindowBefore((Integer) o));
}
});
propertySheet.getItems().add(new PropertyEditorItem(Integer.class, CATEGORY_SUGGEST, "Number of words after suggestion window", SpinnerPropertyEditor.class) {

@Override
public Object getValue() {
return configurationManager.getConfiguration().getSuggestionWindowAfter();
}

@Override
public void setValue(Object o) {
configurationManager.updateConfiguration(configurationManager.getConfiguration().updateSuggestionWindowAfter((Integer) o));
}
});
propertySheet.getItems().add(new PropertyEditorItem(Integer.class, CATEGORY_SUGGEST, "Suggestion slop", SpinnerPropertyEditor.class) {

@Override
public Object getValue() {
return configurationManager.getConfiguration().getSuggestionSlop();
}

@Override
public void setValue(Object o) {
configurationManager.updateConfiguration(configurationManager.getConfiguration().updateSuggestionSlop((Integer) o));
}
});
propertySheet.getItems().add(new PropertyEditorItem(boolean.class, CATEGORY_SUGGEST, "Show suggestions in order", BooleanPropertyEditor.class) {

@Override
public Object getValue() {
return configurationManager.getConfiguration().isSuggestionInOrder();
}

@Override
public void setValue(Object o) {
configurationManager.updateConfiguration(configurationManager.getConfiguration().updateSuggestionsInOrder((Boolean) o));
}
});
}

private void addNewLocation() {
DirectoryChooser theChooser = new DirectoryChooser();
theChooser.setTitle("Add new crawl location");
File theFile = theChooser.showDialog(stage.getOwner());
if (theFile != null) {
Configuration.CrawlLocation theNewLocation = new Configuration.CrawlLocation(UUID.randomUUID().toString(), theFile);
indexedDirectories.getItems().add(theNewLocation);
private void ok() {

addedLocations.add(theNewLocation);
}
stage.hide();
}
}
63 changes: 63 additions & 0 deletions src/main/java/de/mirkosertic/desktopsearch/PropertyEditorItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* FreeDesktopSearch - A Search Engine for your Desktop
* Copyright (C) 2013 Mirko Sertic
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
package de.mirkosertic.desktopsearch;

import javafx.beans.value.ObservableValue;
import org.controlsfx.control.PropertySheet;
import org.controlsfx.property.editor.PropertyEditor;

import java.util.Optional;

public abstract class PropertyEditorItem implements PropertySheet.Item {

private final Class type;
private final String category;
private final String name;
private final Class propertyEditor;

public PropertyEditorItem(Class aType, String aCategory, String aName, Class aPropertyEditor) {
type = aType;
category = aCategory;
name = aName;
propertyEditor = aPropertyEditor;
}

@Override
public Class<?> getType() {
return type;
}

@Override
public String getCategory() {
return category;
}

@Override
public String getName() {
return name;
}

@Override
public String getDescription() {
return "";
}

@Override
public Optional<ObservableValue<? extends Object>> getObservableValue() {
return Optional.empty();
}

public Optional<Class<? extends PropertyEditor<?>>> getPropertyEditorClass() {
return Optional.of(propertyEditor);
}
}
Loading

0 comments on commit 6b54082

Please sign in to comment.