1/*
2 * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24package com.sun.hotspot.igv.filterwindow;
25
26import com.sun.hotspot.igv.data.ChangedListener;
27import com.sun.hotspot.igv.filter.Filter;
28import com.sun.hotspot.igv.filter.FilterChain;
29import com.sun.hotspot.igv.filterwindow.actions.MoveFilterDownAction;
30import com.sun.hotspot.igv.filterwindow.actions.MoveFilterUpAction;
31import com.sun.hotspot.igv.filterwindow.actions.RemoveFilterAction;
32import com.sun.hotspot.igv.util.PropertiesSheet;
33import javax.swing.Action;
34import org.openide.actions.OpenAction;
35import org.openide.nodes.Children;
36import org.openide.nodes.Sheet;
37import org.openide.util.Lookup;
38import org.openide.util.LookupEvent;
39import org.openide.util.LookupListener;
40import org.openide.util.Utilities;
41import org.openide.util.lookup.AbstractLookup;
42import org.openide.util.lookup.InstanceContent;
43
44/**
45 *
46 * @author Thomas Wuerthinger
47 */
48public class FilterNode extends CheckNode implements LookupListener, ChangedListener<FilterTopComponent> {
49
50    private Filter filter;
51    private Lookup.Result<FilterChain> result;
52
53    public FilterNode(Filter filter) {
54        this(filter, new InstanceContent());
55    }
56
57    private FilterNode(Filter filter, InstanceContent content) {
58        super(Children.LEAF, new AbstractLookup(content));
59        content.add(filter);
60
61        content.add(filter.getEditor());
62        this.filter = filter;
63        filter.getChangedEvent().addListener(new ChangedListener<Filter>() {
64
65            @Override
66            public void changed(Filter source) {
67                update();
68            }
69        });
70
71        update();
72
73        Lookup.Template<FilterChain> tpl = new Lookup.Template<>(FilterChain.class);
74        result = Utilities.actionsGlobalContext().lookup(tpl);
75        result.addLookupListener(this);
76
77        FilterTopComponent.findInstance().getFilterSettingsChangedEvent().addListener(this);
78        resultChanged(null);
79
80        setShortDescription("Double-click to open filter");
81    }
82
83    private void update() {
84        this.setDisplayName(filter.getName());
85    }
86
87    public Filter getFilter() {
88        return filter;
89    }
90
91    @Override
92    protected Sheet createSheet() {
93        Sheet s = super.createSheet();
94        PropertiesSheet.initializeSheet(getFilter().getProperties(), s);
95        return s;
96    }
97
98    @Override
99    public Action[] getActions(boolean b) {
100        return new Action[]{(Action) OpenAction.findObject(OpenAction.class, true), (Action) MoveFilterUpAction.findObject(MoveFilterUpAction.class, true), (Action) MoveFilterDownAction.findObject(MoveFilterDownAction.class, true), (Action) RemoveFilterAction.findObject(RemoveFilterAction.class, true)};
101    }
102
103    @Override
104    public Action getPreferredAction() {
105        return OpenAction.get(OpenAction.class).createContextAwareInstance(Utilities.actionsGlobalContext());
106    }
107
108    @Override
109    public void resultChanged(LookupEvent lookupEvent) {
110        changed(FilterTopComponent.findInstance());
111    }
112
113    @Override
114    public void changed(FilterTopComponent source) {
115        setSelected(source.getFilterChain().containsFilter(filter));
116    }
117}
118