Skip to content

Commit

Permalink
Use ChildModelFilter to implement index windows
Browse files Browse the repository at this point in the history
Issue: #162
  • Loading branch information
mlopatkin committed May 14, 2024
1 parent 14acb0c commit 8034066
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2024 the Andlogview authors
*
* Licensed 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 name.mlopatkin.andlogview.filters;

/**
* A Filter that has a submodel.
*/
public interface ChildModelFilter extends Filter {
// TODO {@link FilterModel} should know about these filters and may provide a "parent" model for it.

/**
* Returns the child model of this filter.
*
* @return the model of this filter
*/
FilterModel getFilters();
// TODO(mlopatkin) FilterModel can subscribe to child model upon adding a filter and unsubscribe afterwards.
// This way, the notifications can still be sent from the main model, so clients do not have to care about watching
// for children and subscribing themselves.
// TODO(mlopatkin) The returned model doesn't have to be mutable, but some filters may implement it as such.
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ public String toString() {
}

public FilterFromDialog toFilter(boolean enabled) throws RequestCompilationException {
if (mode == FilteringMode.WINDOW) {
return new IndexWindowFilter(enabled, this);
}
return new FilterFromDialogImpl(enabled, new FilterFromDialogData(this));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2024 the Andlogview authors
*
* Licensed 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 name.mlopatkin.andlogview.ui.filterdialog;

import name.mlopatkin.andlogview.filters.AbstractFilter;
import name.mlopatkin.andlogview.filters.ChildModelFilter;
import name.mlopatkin.andlogview.filters.FilterModel;
import name.mlopatkin.andlogview.filters.FilteringMode;
import name.mlopatkin.andlogview.filters.PredicateFilter;
import name.mlopatkin.andlogview.logmodel.LogRecord;
import name.mlopatkin.andlogview.search.RequestCompilationException;

import com.google.common.collect.ImmutableSet;

import java.util.Objects;
import java.util.function.Predicate;

public class IndexWindowFilter extends AbstractFilter<IndexWindowFilter> implements FilterFromDialog, ChildModelFilter {
private final FilterFromDialogData filterData;
private final FilterModel model;

public IndexWindowFilter(boolean enabled, FilterFromDialogData filterData) throws RequestCompilationException {
super(FilteringMode.WINDOW, enabled);
this.model = FilterModel.create(ImmutableSet.of(predicateToHideFilter(filterData.compilePredicate())));
this.filterData = filterData;
}

private IndexWindowFilter(boolean enabled, IndexWindowFilter orig) {
super(FilteringMode.WINDOW, enabled);
this.model = orig.model;
this.filterData = orig.filterData;
}

private static PredicateFilter predicateToHideFilter(Predicate<? super LogRecord> filterPredicate) {
class HideFilter extends AbstractFilter<HideFilter> implements PredicateFilter {
protected HideFilter() {
super(FilteringMode.HIDE, true);
}

@Override
protected HideFilter copy(boolean enabled) {
throw new UnsupportedOperationException("Cannot disable!");
}

@Override
public boolean test(LogRecord logRecord) {
return filterPredicate.test(logRecord);
}
}

return new HideFilter();
}

@Override
protected IndexWindowFilter copy(boolean enabled) {
return new IndexWindowFilter(enabled, this);
}

@Override
public FilterFromDialogData getData() {
// TODO(mlopatkin) defensive copy?
return filterData;
}

@Override
public FilterModel getFilters() {
return model;
}

@Override
public int hashCode() {
return Objects.hash(isEnabled(), filterData);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof IndexWindowFilter that) {
return that.isEnabled() == isEnabled() && Objects.equals(that.filterData, filterData);
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import name.mlopatkin.andlogview.filters.FilterCollection;
import name.mlopatkin.andlogview.filters.FilterModel;
import name.mlopatkin.andlogview.filters.FilteringMode;
import name.mlopatkin.andlogview.filters.PredicateFilter;
import name.mlopatkin.andlogview.ui.filterdialog.IndexWindowFilter;
import name.mlopatkin.andlogview.utils.events.ScopedObserver;

import org.checkerframework.checker.nullness.qual.Nullable;
Expand All @@ -30,8 +30,8 @@

import javax.inject.Inject;

public class IndexFilterCollection implements FilterCollection<PredicateFilter> {
private final Map<PredicateFilter, IndexFilterController> controllerMap = new HashMap<>();
public class IndexFilterCollection implements FilterCollection<IndexWindowFilter> {
private final Map<IndexWindowFilter, IndexFilterController> controllerMap = new HashMap<>();
private final IndexFilterController.Factory controllerFactory;

@Inject
Expand All @@ -47,12 +47,12 @@ public ScopedObserver setModel(FilterModel model) {
}

@Override
public @Nullable PredicateFilter transformFilter(Filter filter) {
return FilteringMode.WINDOW.equals(filter.getMode()) ? (PredicateFilter) filter : null;
public @Nullable IndexWindowFilter transformFilter(Filter filter) {
return FilteringMode.WINDOW.equals(filter.getMode()) ? (IndexWindowFilter) filter : null;
}

@Override
public void addFilter(PredicateFilter filter) {
public void addFilter(IndexWindowFilter filter) {
if (!filter.isEnabled()) {
return;
}
Expand All @@ -62,7 +62,7 @@ public void addFilter(PredicateFilter filter) {
}

@Override
public void removeFilter(PredicateFilter filter) {
public void removeFilter(IndexWindowFilter filter) {
if (filter.isEnabled()) {
controllerMap.remove(filter).close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

import static name.mlopatkin.andlogview.ui.mainframe.MainFrameDependencies.FOR_MAIN_FRAME;

import name.mlopatkin.andlogview.filters.ChildModelFilter;
import name.mlopatkin.andlogview.filters.CompoundFilterModel;
import name.mlopatkin.andlogview.filters.MutableFilterModel;
import name.mlopatkin.andlogview.filters.PredicateFilter;
import name.mlopatkin.andlogview.ui.indexframe.AbstractIndexController;
import name.mlopatkin.andlogview.ui.indexframe.DaggerIndexFrameDi_IndexFrameComponent;
import name.mlopatkin.andlogview.ui.indexframe.IndexFrame;
Expand All @@ -38,7 +39,7 @@

public class IndexFilterController extends AbstractIndexController implements AutoCloseable {
private final MutableFilterModel filterModel;
private final PredicateFilter filter;
private final ChildModelFilter filter;
private final IndexFrame frame;
private final IndexFilter logModelFilter;

Expand All @@ -48,12 +49,12 @@ public class IndexFilterController extends AbstractIndexController implements Au
DialogFactory dialogFactory,
MutableFilterModel parentFilterModel,
@Named(FOR_MAIN_FRAME) JTable mainTable,
@Assisted PredicateFilter filter) {
@Assisted ChildModelFilter filter) {
super(mainTable);
this.filterModel = parentFilterModel;

this.filter = filter;
logModelFilter = new IndexFilter(IndexFilterModel.createIndexFilterModel(parentFilterModel, filter));
logModelFilter = new IndexFilter(new CompoundFilterModel(parentFilterModel, filter.getFilters()));

IndexFrameDi.IndexFrameComponent component = DaggerIndexFrameDi_IndexFrameComponent.builder()
.logRecordTableModel(logModel)
Expand Down Expand Up @@ -84,6 +85,6 @@ public void onWindowClosed() {

@AssistedFactory
public interface Factory {
IndexFilterController create(PredicateFilter filter);
IndexFilterController create(ChildModelFilter filter);
}
}
73 changes: 0 additions & 73 deletions src/name/mlopatkin/andlogview/ui/indexfilter/IndexFilterModel.java

This file was deleted.

This file was deleted.

0 comments on commit 8034066

Please sign in to comment.