~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to xml/nbprefuse/src/org/netbeans/modules/xml/nbprefuse/NodeExpansionMouseControl.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
/*
 
43
 * NodeExpansionMouseControl.java
 
44
 *
 
45
 * Created on January 17, 2006, 12:50 PM
 
46
 *
 
47
 * To change this template, choose Tools | Template Manager
 
48
 * and open the template in the editor.
 
49
 */
 
50
 
 
51
package org.netbeans.modules.xml.nbprefuse;
 
52
 
 
53
 
 
54
import java.awt.event.ActionEvent;
 
55
import java.awt.event.ActionListener;
 
56
import java.awt.event.MouseEvent;
 
57
import java.util.Iterator;
 
58
import javax.swing.JMenuItem;
 
59
import javax.swing.JPopupMenu;
 
60
import org.netbeans.modules.xml.nbprefuse.util.GraphUtilities;
 
61
import org.openide.util.NbBundle;
 
62
import prefuse.Visualization;
 
63
import prefuse.controls.ControlAdapter;
 
64
import prefuse.visual.AggregateItem;
 
65
import prefuse.visual.NodeItem;
 
66
import prefuse.visual.VisualItem;
 
67
 
 
68
/**
 
69
 *
 
70
 * @author Jeri Lockhart
 
71
 */
 
72
public final class NodeExpansionMouseControl extends ControlAdapter {
 
73
    
 
74
    private JPopupMenu popup;
 
75
    private VisualItem vItem;
 
76
    private JMenuItem menuItem;
 
77
    
 
78
    public NodeExpansionMouseControl(final Visualization viz, final String activity) {
 
79
        popup = new JPopupMenu();
 
80
        menuItem = new JMenuItem();
 
81
        menuItem.addActionListener(new ActionListener() {
 
82
            public void actionPerformed(ActionEvent e){
 
83
                if (vItem != null && vItem instanceof VisualItem){
 
84
                    NodeItem fileNode = null;
 
85
                    if (vItem.canGetBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE) &&
 
86
                            vItem.getBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE)){
 
87
                        
 
88
                        fileNode = findFileNode(vItem.getInt(AnalysisConstants.ID),
 
89
                                AggregateItem.class.cast(vItem));
 
90
                    } else {
 
91
                        fileNode = NodeItem.class.cast(vItem);
 
92
                    }
 
93
                    if (fileNode.canGetBoolean(AnalysisConstants.IS_FILE_NODE) &&
 
94
                            fileNode.getBoolean(AnalysisConstants.IS_FILE_NODE)){
 
95
                        GraphUtilities.expandCollapseFileNode(fileNode);
 
96
                        if (activity != null){
 
97
                            viz.run(activity);
 
98
                        }
 
99
                    }
 
100
                }
 
101
            }
 
102
        });
 
103
        popup.add(menuItem);
 
104
        
 
105
    }
 
106
    
 
107
    public void itemReleased(VisualItem gi, MouseEvent e) {
 
108
        
 
109
        super.itemReleased(gi, e);
 
110
        vItem = gi;
 
111
        maybeShowPopup(e);
 
112
    }
 
113
    
 
114
    public void itemPressed(VisualItem gi, MouseEvent e) {
 
115
        
 
116
        super.itemPressed(gi, e);
 
117
        vItem = gi;
 
118
        maybeShowPopup(e);
 
119
    }
 
120
    
 
121
    
 
122
    
 
123
    private void maybeShowPopup(MouseEvent e) {
 
124
        if (e.isPopupTrigger()) {
 
125
            NodeItem fileNode = null;
 
126
            if (vItem.canGetBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE) &&
 
127
                    vItem.getBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE)){
 
128
                fileNode = findFileNode(vItem.getInt(AnalysisConstants.ID),
 
129
                        AggregateItem.class.cast(vItem));
 
130
            }
 
131
            if (vItem.canGetBoolean(AnalysisConstants.IS_FILE_NODE) &&
 
132
                    vItem.getBoolean(AnalysisConstants.IS_FILE_NODE)){
 
133
                fileNode = NodeItem.class.cast(vItem);
 
134
            }
 
135
            if (fileNode == null){
 
136
                return;
 
137
            }
 
138
            boolean isExpanded = fileNode.getBoolean(AnalysisConstants.IS_EXPANDED);
 
139
            menuItem.setText(NbBundle.getMessage(NodeExpansionMouseControl.class,(isExpanded?"LBL_collapse":"LBL_expand")));
 
140
            popup.show(e.getComponent(),
 
141
                    e.getX(), e.getY());
 
142
        }
 
143
    }
 
144
    
 
145
    /**
 
146
     * Finds the schema File Node in the AggregateItem
 
147
     * The AggregateItem contains the schema file node and
 
148
     *  the SchemaComponent nodes for the file
 
149
     *
 
150
     */
 
151
    private NodeItem findFileNode(final int aggregateItemFileGroup, final VisualItem item ) {
 
152
        NodeItem fileNode = null;
 
153
        AggregateItem agIt = AggregateItem.class.cast(item);
 
154
        Iterator agItems = agIt.items();
 
155
        while(agItems.hasNext()){
 
156
            VisualItem agItem = (VisualItem)agItems.next();
 
157
            int fileGroup = -1;
 
158
            if (agItem.canGetInt(AnalysisConstants.FILE_NODE_FILE_GROUP)) {
 
159
                fileGroup = agItem.getInt(AnalysisConstants.FILE_NODE_FILE_GROUP);
 
160
                if (fileGroup == aggregateItemFileGroup &&
 
161
                        agItem.canGetBoolean(AnalysisConstants.IS_EXPANDED)){
 
162
                    fileNode =  NodeItem.class.cast(agItem);
 
163
                    break;
 
164
                }
 
165
            }
 
166
        }
 
167
        return fileNode;
 
168
    }
 
169
    
 
170
}
 
171
 
 
172