KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > basic > editors > MemberTypesPanel


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  * MemberTypesPanel.java
22  *
23  * Created on January 25, 2006, 12:50 PM
24  */

25
26 package org.netbeans.modules.xml.schema.ui.basic.editors;
27
28 import java.awt.Dialog JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Collection JavaDoc;
35 import javax.swing.AbstractListModel JavaDoc;
36 import javax.swing.event.ListSelectionEvent JavaDoc;
37 import javax.swing.event.ListSelectionListener JavaDoc;
38 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
39 import org.netbeans.modules.xml.schema.model.SchemaModel;
40 import org.netbeans.modules.xml.schema.ui.nodes.schema.UnionNode;
41 import org.openide.DialogDescriptor;
42 import org.openide.DialogDisplayer;
43 import org.openide.ErrorManager;
44 import org.openide.util.NbBundle;
45
46 /**
47  *
48  * @author Administrator
49  */

50 public class MemberTypesPanel extends javax.swing.JPanel JavaDoc
51     implements ActionListener JavaDoc, ListSelectionListener JavaDoc {
52     
53     static final long serialVersionUID = 1L;
54     private transient SchemaModel model;
55
56     /**
57      * Creates new form MemberTypesPanel
58      */

59     public MemberTypesPanel(SchemaModel model,
60             Collection JavaDoc<GlobalSimpleType> initialSelection) {
61         this.model = model;
62         initComponents();
63         memberTypesList.setModel(new MemberTypesModel(initialSelection));
64     }
65     
66     /** This method is called from within the constructor to
67      * initialize the form.
68      * WARNING: Do NOT modify this code. The content of this method is
69      * always regenerated by the Form Editor.
70      */

71     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
72
private void initComponents() {
73         addButton = new javax.swing.JButton JavaDoc();
74         removeButton = new javax.swing.JButton JavaDoc();
75         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
76         memberTypesList = new javax.swing.JList JavaDoc();
77         upButton = new javax.swing.JButton JavaDoc();
78         downButton = new javax.swing.JButton JavaDoc();
79         jLabel1 = new javax.swing.JLabel JavaDoc();
80
81         org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(MemberTypesPanel.class, "LBL_Add"));
82         addButton.setToolTipText(org.openide.util.NbBundle.getMessage(MemberTypesPanel.class, "HINT_Add"));
83         addButton.addActionListener(this);
84
85         org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(MemberTypesPanel.class, "LBL_Remove"));
86         removeButton.setToolTipText(org.openide.util.NbBundle.getMessage(MemberTypesPanel.class, "HINT_Remove"));
87         removeButton.addActionListener(this);
88         removeButton.setEnabled(false);
89
90         memberTypesList.addListSelectionListener(this);
91         jScrollPane1.setViewportView(memberTypesList);
92
93         org.openide.awt.Mnemonics.setLocalizedText(upButton, org.openide.util.NbBundle.getMessage(MemberTypesPanel.class, "LBL_Up"));
94         upButton.setToolTipText(org.openide.util.NbBundle.getMessage(MemberTypesPanel.class, "HINT_Up"));
95         upButton.setEnabled(false);
96         upButton.addActionListener(this);
97
98         org.openide.awt.Mnemonics.setLocalizedText(downButton, org.openide.util.NbBundle.getMessage(MemberTypesPanel.class, "LBL_Down"));
99         downButton.setToolTipText(org.openide.util.NbBundle.getMessage(MemberTypesPanel.class, "HINT_Down"));
100         downButton.setEnabled(false);
101         downButton.addActionListener(this);
102
103         jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
104         jLabel1.setLabelFor(memberTypesList);
105         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(MemberTypesPanel.class, "LBL_CurrentlySelectedMemberTypes"));
106
107         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
108         this.setLayout(layout);
109         layout.setHorizontalGroup(
110             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
111             .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
112                 .add(26, 26, 26)
113                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
114                     .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 117, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
115                     .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 209, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
116                 .add(19, 19, 19)
117                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
118                     .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
119                         .add(addButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
120                         .add(removeButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
121                     .add(downButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
122                     .add(upButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))
123                 .add(87, 87, 87))
124         );
125         layout.setVerticalGroup(
126             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
127             .add(layout.createSequentialGroup()
128                 .add(26, 26, 26)
129                 .add(jLabel1)
130                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
131                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
132                     .add(layout.createSequentialGroup()
133                         .add(addButton)
134                         .add(13, 13, 13)
135                         .add(removeButton)
136                         .add(15, 15, 15)
137                         .add(upButton)
138                         .add(15, 15, 15)
139                         .add(downButton))
140                     .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
141                 .addContainerGap(44, Short.MAX_VALUE))
142         );
143     }// </editor-fold>//GEN-END:initComponents
144

145     public void actionPerformed(ActionEvent JavaDoc e) {
146         if(e.getSource().equals(addButton)) {
147             Dialog JavaDoc selector = getSelectorDialog();
148             selector.setVisible(true);
149         } else if(e.getSource().equals(removeButton)) {
150             int[] indices = memberTypesList.getSelectedIndices();
151             for (int ctr = indices.length;ctr>0;ctr--) {
152                 MemberTypesModel model = (MemberTypesModel) memberTypesList.getModel();
153                 model.removeMember(indices[ctr-1]);
154             }
155             memberTypesList.setSelectedIndex(0);
156         } else if(e.getSource().equals(upButton)) {
157             int selectedIndex = memberTypesList.getSelectedIndex();
158             MemberTypesModel model = (MemberTypesModel) memberTypesList.getModel();
159             model.moveMember(selectedIndex,true);
160             memberTypesList.setSelectedIndex(selectedIndex-1);
161         } else if(e.getSource().equals(downButton)) {
162             int selectedIndex = memberTypesList.getSelectedIndex();
163             MemberTypesModel model = (MemberTypesModel) memberTypesList.getModel();
164             model.moveMember(selectedIndex,false);
165             memberTypesList.setSelectedIndex(selectedIndex+1);
166         }
167     }
168     
169     public void valueChanged(ListSelectionEvent JavaDoc e) {
170         if(e.getSource().equals(memberTypesList)) {
171             if(memberTypesList.getSelectedIndices()==null || memberTypesList.getModel().getSize()==0) {
172                 removeButton.setEnabled(false);
173                 upButton.setEnabled(false);
174                 downButton.setEnabled(false);
175             } else {
176                 removeButton.setEnabled(true);
177                 if(memberTypesList.getSelectedIndices().length>1) {
178                     upButton.setEnabled(false);
179                     downButton.setEnabled(false);
180                 } else {
181                     int selectedIndex = memberTypesList.getSelectedIndex();
182                     if(selectedIndex==0){
183                         upButton.setEnabled(false);
184                     } else {
185                         upButton.setEnabled(true);
186                     }
187                     if(selectedIndex==memberTypesList.getModel().getSize()-1) {
188                         downButton.setEnabled(false);
189                     } else {
190                         downButton.setEnabled(true);
191                     }
192                 }
193             }
194         }
195     }
196     
197     private Dialog JavaDoc getSelectorDialog() {
198         final SchemaComponentSelectionPanel panel = new SchemaComponentSelectionPanel
199                 <GlobalSimpleType>(
200                 model, GlobalSimpleType.class,
201                 null, Collections.unmodifiableCollection(getCurrentSelection()), true);
202         DialogDescriptor descriptor = new SchemaComponentSelDialogDesc(panel,
203                 NbBundle.getMessage(GlobalReferenceEditor.class,
204                 "LBL_Custom_Property_Editor_Title",
205                 new Object JavaDoc[] {NbBundle.getMessage(UnionNode.class, "LBL_UnionNode_TypeDisplayName")
206                         , NbBundle.getMessage(UnionNode.class, "PROP_MemberTypes_DisplayName")}),
207                 true,
208                 new ActionListener JavaDoc() {
209             public void actionPerformed(ActionEvent JavaDoc evt) {
210                 if (evt.getSource().equals(DialogDescriptor.OK_OPTION)) {
211                     try {
212                         MemberTypesModel model = (MemberTypesModel) memberTypesList.getModel();
213                         model.addMember((GlobalSimpleType)panel.getCurrentSelection());
214                     } catch (IllegalArgumentException JavaDoc iae) {
215                         ErrorManager.getDefault().annotate(iae, ErrorManager.USER,
216                                 iae.getMessage(), iae.getLocalizedMessage(),
217                                 null, new java.util.Date JavaDoc());
218                         throw iae;
219                     }
220                 }
221             }
222         }
223         );
224         
225         Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(descriptor);
226         return dlg;
227         
228     }
229
230     public List JavaDoc<GlobalSimpleType> getCurrentSelection() {
231         return ((MemberTypesModel)memberTypesList.getModel()).getMembers();
232     }
233
234     // Variables declaration - do not modify//GEN-BEGIN:variables
235
private javax.swing.JButton JavaDoc addButton;
236     private javax.swing.JButton JavaDoc downButton;
237     private javax.swing.JLabel JavaDoc jLabel1;
238     private javax.swing.JScrollPane JavaDoc jScrollPane1;
239     private javax.swing.JList JavaDoc memberTypesList;
240     private javax.swing.JButton JavaDoc removeButton;
241     private javax.swing.JButton JavaDoc upButton;
242     // End of variables declaration//GEN-END:variables
243

244     public static class MemberTypesModel extends AbstractListModel JavaDoc {
245     static final long serialVersionUID = 1L;
246         private List JavaDoc<GlobalSimpleType> members;
247         public MemberTypesModel(Collection JavaDoc<GlobalSimpleType> members) {
248             super();
249             this.members = new ArrayList JavaDoc<GlobalSimpleType>();
250             if(members != null)
251                 this.members.addAll(members);
252         }
253
254         public int getSize() {
255             return members.size();
256         }
257
258         public Object JavaDoc getElementAt(int index) {
259             return members.isEmpty()?"":members.get(index)==null?"":members.get(index).getName();
260         }
261         
262         public List JavaDoc<GlobalSimpleType> getMembers() {
263             return members;
264         }
265         
266         public void addMember(GlobalSimpleType member) {
267             members.add(member);
268             super.fireIntervalAdded(this,getSize(),getSize());
269         }
270
271         public void removeMember(int index) {
272             if(index>-1) {
273                 members.remove(index);
274                 super.fireIntervalRemoved(this,index,index);
275             }
276         }
277         
278         public void moveMember(int index, boolean up) {
279             GlobalSimpleType movedMember = members.remove(index);
280             int newIndex = up?index-1:index+1;
281             members.add(newIndex,movedMember);
282             super.fireContentsChanged(this,index,newIndex);
283         }
284     }
285 }
286
Popular Tags