KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > common > ExportSelectionDialog


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.common;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Component JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.FlowLayout JavaDoc;
36 import java.awt.Frame JavaDoc;
37 import java.awt.Toolkit JavaDoc;
38 import java.awt.event.ActionEvent JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Arrays JavaDoc;
41 import java.util.Collections JavaDoc;
42
43 import javax.swing.DefaultComboBoxModel JavaDoc;
44 import javax.swing.DefaultListCellRenderer JavaDoc;
45 import javax.swing.JButton JavaDoc;
46 import javax.swing.JComboBox JavaDoc;
47 import javax.swing.JDialog JavaDoc;
48 import javax.swing.JLabel JavaDoc;
49 import javax.swing.JList JavaDoc;
50 import javax.swing.JPanel JavaDoc;
51
52 import com.genimen.djeneric.language.Messages;
53 import com.genimen.djeneric.repository.DjExtent;
54 import com.genimen.djeneric.repository.DjPackage;
55 import com.genimen.djeneric.tools.modeler.ModelEditor;
56 import com.genimen.djeneric.tools.modeler.dialogs.SelectionPanel;
57 import com.genimen.djeneric.ui.Util;
58 import com.genimen.djeneric.util.DjExtentComparator;
59 import com.genimen.djeneric.util.DjLogger;
60 import com.genimen.djeneric.util.DjStringComparator;
61
62 public class ExportSelectionDialog extends JDialog JavaDoc
63 {
64   private static final long serialVersionUID = 1L;
65   JPanel JavaDoc panel1 = new JPanel JavaDoc();
66   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
67   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
68   JButton JavaDoc _butOk = new JButton JavaDoc();
69   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
70   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
71   JButton JavaDoc _butCancel = new JButton JavaDoc();
72   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
73   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
74   boolean _canceled = true;
75   SelectionPanel _pnlExtents = new SelectionPanel();
76   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
77   FlowLayout JavaDoc flowLayout1 = new FlowLayout JavaDoc();
78   JLabel JavaDoc _lblDomains = new JLabel JavaDoc();
79   JComboBox JavaDoc _cbbDomainsHow = new JComboBox JavaDoc();
80   boolean _showDomainChoice = true;
81   JPanel JavaDoc jPanel5 = new JPanel JavaDoc();
82   JButton JavaDoc _butMinusPackage = new JButton JavaDoc();
83   FlowLayout JavaDoc flowLayout2 = new FlowLayout JavaDoc();
84   JComboBox JavaDoc _cbbPackageName = new JComboBox JavaDoc();
85   JButton JavaDoc _butPlusPackage = new JButton JavaDoc();
86   DjPackage _defaultPackage = new DjPackage("Default");
87
88   public ExportSelectionDialog(Frame JavaDoc frame, DjExtent[] extents, boolean showDomainChoice, boolean selectAll)
89   {
90     super(frame, Messages.getString("ExportSelectionDialog.ExportModelSelection"), true); //$NON-NLS-1$
91
try
92     {
93
94       ArrayList JavaDoc allPackages = new ArrayList JavaDoc();
95       for (int i = 0; i < extents.length; i++)
96       {
97         DjPackage pck = extents[i].getPackage();
98         if (pck == null) pck = _defaultPackage;
99         if (!allPackages.contains(pck)) allPackages.add(pck);
100       }
101       Collections.sort(allPackages, new DjStringComparator(false));
102       _cbbPackageName.setModel(new DefaultComboBoxModel JavaDoc(allPackages.toArray()));
103
104       _showDomainChoice = showDomainChoice;
105       jbInit();
106       _cbbDomainsHow.setModel(new DefaultComboBoxModel JavaDoc(new String JavaDoc[]{Messages.getString("ExportSelectionDialog.All"),
107           Messages.getString("ExportSelectionDialog.UsedOnly")}));
108
109       _pnlExtents.setCellRenderer(new ExtentRenderer());
110       _pnlExtents.setComparator(new DjExtentComparator(true));
111
112       _pnlExtents.setAvailable(Arrays.asList(extents));
113
114       setSize(new Dimension JavaDoc(544, 400));
115
116       Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
117       Dimension JavaDoc frameSize = getSize();
118       setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
119
120       if (selectAll)
121       {
122         _pnlExtents.selectAll();
123       }
124
125       setVisible(true);
126     }
127     catch (Exception JavaDoc ex)
128     {
129       DjLogger.log(ex);
130     }
131   }
132
133   public DjExtent[] getSelectedExtents()
134   {
135     return (DjExtent[]) _pnlExtents.getSelection().toArray(new DjExtent[0]);
136   }
137
138   public DjExtent[] getUnSelectedExtents()
139   {
140     return (DjExtent[]) _pnlExtents.getNegatedSelection().toArray(new DjExtent[0]);
141   }
142
143   public boolean shouldAddAllDomains()
144   {
145     return _cbbDomainsHow.getSelectedIndex() == 0;
146   }
147
148   private void jbInit() throws Exception JavaDoc
149   {
150     panel1.setLayout(borderLayout1);
151     jPanel1.setLayout(borderLayout2);
152     _butOk.setText(Messages.getString("global.Ok")); //$NON-NLS-1$
153
_butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
154     {
155       public void actionPerformed(ActionEvent JavaDoc e)
156       {
157         _butOk_actionPerformed(e);
158       }
159     });
160     _butCancel.setText(Messages.getString("global.Cancel")); //$NON-NLS-1$
161
_butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
162     {
163       public void actionPerformed(ActionEvent JavaDoc e)
164       {
165         _butCancel_actionPerformed(e);
166       }
167     });
168     jPanel3.setLayout(borderLayout3);
169     jPanel2.setLayout(flowLayout1);
170     _lblDomains.setText(Messages.getString("ExportSelectionDialog.Domains"));
171     flowLayout1.setAlignment(FlowLayout.LEFT);
172     jPanel5.setLayout(flowLayout2);
173     flowLayout2.setHgap(0);
174     flowLayout2.setVgap(0);
175     _butMinusPackage.setPreferredSize(new Dimension JavaDoc(24, 24));
176     _butMinusPackage.setText("");
177     _butMinusPackage.addActionListener(new java.awt.event.ActionListener JavaDoc()
178     {
179       public void actionPerformed(ActionEvent JavaDoc e)
180       {
181         _butMinusPackage_actionPerformed(e);
182       }
183     });
184     _butPlusPackage.setPreferredSize(new Dimension JavaDoc(24, 24));
185     _butPlusPackage.setText("");
186     _butPlusPackage.addActionListener(new java.awt.event.ActionListener JavaDoc()
187     {
188       public void actionPerformed(ActionEvent JavaDoc e)
189       {
190         _butPlusPackage_actionPerformed(e);
191       }
192     });
193     _pnlExtents.setPreferredSize(new Dimension JavaDoc(534, 161));
194     getContentPane().add(panel1);
195     panel1.add(jPanel1, BorderLayout.SOUTH);
196     jPanel1.add(jPanel3, BorderLayout.SOUTH);
197     jPanel3.add(jPanel4, BorderLayout.EAST);
198     jPanel4.add(_butCancel, null);
199     jPanel4.add(_butOk, null);
200     jPanel3.add(jPanel2, BorderLayout.CENTER);
201     jPanel1.add(jPanel5, BorderLayout.NORTH);
202     jPanel5.add(_butMinusPackage, null);
203     jPanel5.add(_cbbPackageName, null);
204     jPanel5.add(_butPlusPackage, null);
205     if (_showDomainChoice)
206     {
207       jPanel2.add(_lblDomains, null);
208       jPanel2.add(_cbbDomainsHow, null);
209     }
210
211     _butMinusPackage.setIcon(ModelEditor.getImageIcon("remove.gif"));
212     _butPlusPackage.setIcon(ModelEditor.getImageIcon("add.gif"));
213
214     panel1.add(_pnlExtents, BorderLayout.CENTER);//$NON-NLS-1$//$NON-NLS-1$//$NON-NLS-1$
215
Util.sizeButtons(jPanel4);
216   }
217
218   public boolean wasCanceled()
219   {
220     return _canceled;
221   }
222
223   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
224   {
225     setVisible(false);
226   }
227
228   void _butOk_actionPerformed(ActionEvent JavaDoc e)
229   {
230     _canceled = false;
231     setVisible(false);
232   }
233
234   void _butMinusPackage_actionPerformed(ActionEvent JavaDoc e)
235   {
236     DjPackage pck = (DjPackage) _cbbPackageName.getSelectedItem();
237     if (pck != null)
238     {
239       if (pck == _defaultPackage) pck = null;
240
241       ArrayList JavaDoc rems = new ArrayList JavaDoc();
242       ArrayList JavaDoc sels = _pnlExtents.getSelection();
243       for (int i = 0; i < sels.size(); i++)
244       {
245         DjExtent ext = (DjExtent) sels.get(i);
246         if (ext.getPackage() == pck) rems.add(ext);
247       }
248       sels.removeAll(rems);
249       _pnlExtents.setSelection(sels);
250     }
251   }
252
253   void _butPlusPackage_actionPerformed(ActionEvent JavaDoc e)
254   {
255     DjPackage pck = (DjPackage) _cbbPackageName.getSelectedItem();
256     if (pck != null)
257     {
258       if (pck == _defaultPackage) pck = null;
259
260       ArrayList JavaDoc sels = _pnlExtents.getSelection();
261       ArrayList JavaDoc notSels = _pnlExtents.getNegatedSelection();
262       for (int i = 0; i < notSels.size(); i++)
263       {
264         DjExtent ext = (DjExtent) notSels.get(i);
265         if (ext.getPackage() == pck) sels.add(ext);
266       }
267       _pnlExtents.setSelection(sels);
268     }
269   }
270 }
271
272 class ExtentRenderer extends DefaultListCellRenderer JavaDoc
273 {
274   private static final long serialVersionUID = 1L;
275
276   public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected,
277                                                 boolean cellHasFocus)
278   {
279     super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
280
281     String JavaDoc title = ((DjExtent) value).getQualifiedExtentName();
282     setText(title);
283     return this;
284   }
285 }
Popular Tags