KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > ComponentSelectionManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * ComponentSelectionManager.java
22  *
23  * Created on June 2, 2006, 11:27 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.schema.abe;
30
31 import java.awt.Component JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.List JavaDoc;
35 import org.openide.nodes.AbstractNode;
36 import org.openide.nodes.Children;
37 import org.openide.nodes.Node;
38 import org.openide.windows.TopComponent;
39
40 /**
41  *
42  * @author girix
43  */

44 public class ComponentSelectionManager {
45     InstanceUIContext context;
46     private ArrayList JavaDoc<ABEBaseDropPanel> selectedComponentList = new ArrayList JavaDoc<ABEBaseDropPanel>();
47     private Node fauxRoot;
48     /** Creates a new instance of ComponentSelectionManager */
49     public ComponentSelectionManager(InstanceUIContext context) {
50         this.context = context;
51         fauxRoot = new AbstractNode(new Children.Array());
52     }
53     
54     //will be called for single selection
55
/**
56      * Call to set a Single selection (Sinlgle click).
57      * @param component
58      */

59     public void setSelectedComponent(ABEBaseDropPanel component) {
60         clearPreviousSelectedComponents(false);
61         addToSelectedComponents(component);
62     }
63     
64     /**
65      * Call to add to the multiple selection list(ctrl+click).
66      * @param component
67      */

68     public void addToSelectedComponents(ABEBaseDropPanel component) {
69         if(getSelectedComponentList().contains(component)){
70             //if its already selected, just unselect it.
71
clearPreviousSelectedSingleComponent(component, true);
72         }else{
73             getSelectedComponentList().add(component);
74             selectComponent(component);
75         }
76     }
77     
78     /**
79      * See if the component is selected.
80      * @param component
81      * @return
82      */

83     public boolean isSelected(Component JavaDoc component){
84         int index = getSelectedComponentList().indexOf(component);
85         return index == -1 ? false : true;
86     }
87     
88     private void selectComponent(final Component JavaDoc component){
89         ArrayList JavaDoc<Node> nodes = new ArrayList JavaDoc<Node>();
90         for(ABEBaseDropPanel comp: getSelectedComponentList()){
91             comp.setSelected(true);
92             if(comp.getNBNode() != null){
93                 nodes.add(comp.getNBNode());
94             }
95         }
96         //set selected nodes
97
if(nodes.size() > 0) {
98             fauxRoot.getChildren().remove(fauxRoot.getChildren().getNodes(true));
99             Node[] selectedNodes = nodes.toArray(new Node[nodes.size()]);
100             fauxRoot.getChildren().add(selectedNodes);
101             context.getTopComponent().setActivatedNodes(selectedNodes);
102         }
103         
104         Component JavaDoc parent = null;
105         if( (parent = context.getTopComponent().getParent()) != null )
106             findParentTopComponentSpecial(parent).requestActive();
107         component.requestFocusInWindow();
108         component.repaint();
109     }
110     
111     public void clearPreviousSelectedComponents(boolean clearNode){
112         NBGlassPaneAccessSupport.forceDisposeNBGlassPane();
113         if(clearNode) {
114             context.getTopComponent().setActivatedNodes(new Node[0]);
115             fauxRoot.getChildren().remove(fauxRoot.getChildren().getNodes(true));
116         }
117         List JavaDoc<ABEBaseDropPanel> clearedList = getSelectedComponentList();
118         selectedComponentList = new ArrayList JavaDoc<ABEBaseDropPanel>();
119         for(ABEBaseDropPanel comp: clearedList){
120             comp.setSelected(false);
121             comp.repaint();
122         }
123     }
124     
125     public void clearPreviousSelectedSingleComponent(ABEBaseDropPanel comp, boolean clearNode){
126         if(clearNode) {
127             if(comp.getNBNode() != null){
128                 Node node = comp.getNBNode();
129                 Node nodes[] = context.getTopComponent().getActivatedNodes();
130                 ArrayList JavaDoc<Node> nodeList = new ArrayList JavaDoc<Node>(Arrays.asList(nodes));
131                 nodeList.remove(node);
132                 nodes = nodeList.toArray(new Node[nodeList.size()]);
133                 context.getTopComponent().setActivatedNodes(nodes);
134                 fauxRoot.getChildren().remove(fauxRoot.getChildren().getNodes(true));
135                 fauxRoot.getChildren().add(nodes);
136             }
137         }
138         getSelectedComponentList().remove(comp);
139         comp.setSelected(false);
140         comp.repaint();
141     }
142     
143     public ArrayList JavaDoc<ABEBaseDropPanel> getSelectedComponentList() {
144         return selectedComponentList;
145     }
146     
147     public void refreshFocus() {
148         for(ABEBaseDropPanel component: getSelectedComponentList()){
149             component.requestFocus();
150             component.requestFocusInWindow();
151         }
152     }
153     
154     private TopComponent findParentTopComponentSpecial(Component JavaDoc parent) {
155         while (parent!=null) {
156             if (parent instanceof TopComponent)
157                 return (TopComponent)parent;
158             else
159                 parent=parent.getParent();
160         }
161         
162         return null;
163     }
164 }
165
Popular Tags