FilterNode.java revision 1472:c18cbe5936b8
196920Sgrog/*
296920Sgrog * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
396920Sgrog * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
497099Sru *
596920Sgrog * This code is free software; you can redistribute it and/or modify it
696920Sgrog * under the terms of the GNU General Public License version 2 only, as
796920Sgrog * published by the Free Software Foundation.
896920Sgrog *
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.filterwindow.actions.MoveFilterDownAction;
27import com.sun.hotspot.igv.filterwindow.actions.MoveFilterUpAction;
28import com.sun.hotspot.igv.filterwindow.actions.RemoveFilterAction;
29import com.sun.hotspot.igv.filter.Filter;
30import com.sun.hotspot.igv.filter.FilterChain;
31import com.sun.hotspot.igv.data.ChangedListener;
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 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            public void changed(Filter source) {
66                update();
67            }
68        });
69
70        update();
71
72        Lookup.Template<FilterChain> tpl = new Lookup.Template<FilterChain>(FilterChain.class);
73        result = Utilities.actionsGlobalContext().lookup(tpl);
74        result.addLookupListener(this);
75
76        FilterTopComponent.findInstance().getFilterSettingsChangedEvent().addListener(this);
77        resultChanged(null);
78    }
79
80    private void update() {
81        this.setDisplayName(filter.getName());
82    }
83
84    public Filter getFilter() {
85        return filter;
86    }
87
88    @Override
89    protected Sheet createSheet() {
90        Sheet s = super.createSheet();
91        PropertiesSheet.initializeSheet(getFilter().getProperties(), s);
92        return s;
93    }
94
95    @Override
96    public Action[] getActions(boolean b) {
97        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)};
98    }
99
100    @Override
101    public Action getPreferredAction() {
102        return OpenAction.get(OpenAction.class).createContextAwareInstance(Utilities.actionsGlobalContext());
103    }
104
105    public void resultChanged(LookupEvent lookupEvent) {
106        changed(FilterTopComponent.findInstance());
107    }
108
109    public void changed(FilterTopComponent source) {
110        setSelected(source.getFilterChain().containsFilter(filter));
111    }
112}
113