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.view.actions;
25
26import com.sun.hotspot.igv.data.ChangedListener;
27import com.sun.hotspot.igv.util.ContextAction;
28import com.sun.hotspot.igv.view.DiagramViewModel;
29import javax.swing.Action;
30import javax.swing.ImageIcon;
31import org.openide.util.*;
32
33/**
34 *
35 * @author Thomas Wuerthinger
36 */
37public final class PrevDiagramAction extends ContextAction<DiagramViewModel> implements ChangedListener<DiagramViewModel> {
38
39    private DiagramViewModel model;
40
41    public PrevDiagramAction() {
42        this(Utilities.actionsGlobalContext());
43    }
44
45    public PrevDiagramAction(Lookup lookup) {
46        putValue(Action.SHORT_DESCRIPTION, "Show previous graph of current group");
47        putValue(Action.SMALL_ICON, new ImageIcon(ImageUtilities.loadImage("com/sun/hotspot/igv/view/images/prev_diagram.png")));
48    }
49
50    @Override
51    public String getName() {
52        return NbBundle.getMessage(PrevDiagramAction.class, "CTL_PrevDiagramAction");
53    }
54
55    @Override
56    public HelpCtx getHelpCtx() {
57        return HelpCtx.DEFAULT_HELP;
58    }
59
60    @Override
61    public Class<DiagramViewModel> contextClass() {
62        return DiagramViewModel.class;
63    }
64
65    @Override
66    public void performAction(DiagramViewModel model) {
67        int fp = model.getFirstPosition();
68        int sp = model.getSecondPosition();
69        if (fp != 0) {
70            int nfp = fp - 1;
71            int nsp = sp - 1;
72            model.setPositions(nfp, nsp);
73        }
74    }
75
76    @Override
77    public void update(DiagramViewModel model) {
78        super.update(model);
79
80        if (this.model != model) {
81            if (this.model != null) {
82                this.model.getDiagramChangedEvent().removeListener(this);
83            }
84
85            this.model = model;
86            if (this.model != null) {
87                this.model.getDiagramChangedEvent().addListener(this);
88            }
89        }
90    }
91
92    @Override
93    public boolean isEnabled(DiagramViewModel model) {
94        return model.getFirstPosition() != 0;
95    }
96
97    @Override
98    public Action createContextAwareInstance(Lookup arg0) {
99        return new PrevDiagramAction(arg0);
100    }
101
102    @Override
103    public void changed(DiagramViewModel source) {
104        update(source);
105    }
106}
107