KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > wizard > PersistenceClientEntitySelectionVisual


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.wizard;
21
22 import java.awt.Component JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.swing.AbstractListModel JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.JList JavaDoc;
33 import javax.swing.ListCellRenderer JavaDoc;
34 import javax.swing.ListModel JavaDoc;
35 import javax.swing.event.ChangeEvent JavaDoc;
36 import javax.swing.event.ChangeListener JavaDoc;
37 import javax.swing.event.ListSelectionEvent JavaDoc;
38 import javax.swing.event.ListSelectionListener JavaDoc;
39 import org.netbeans.api.project.Project;
40 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope;
41 import org.netbeans.modules.j2ee.persistence.dd.ORMMetadata;
42 import org.netbeans.modules.j2ee.persistence.wizard.unit.PersistenceUnitWizardPanel.TableGeneration;
43 import org.netbeans.modules.j2ee.persistence.dd.PersistenceMetadata;
44 import org.netbeans.modules.j2ee.persistence.dd.PersistenceUtils;
45 import org.netbeans.modules.j2ee.persistence.api.metadata.orm.Entity;
46 import org.netbeans.modules.j2ee.persistence.api.metadata.orm.EntityMappings;
47 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Persistence;
48 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
49 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil;
50 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.ChangeSupport;
51 import org.netbeans.spi.project.ui.templates.support.Templates;
52 import org.openide.ErrorManager;
53 import org.openide.WizardDescriptor;
54 import org.openide.filesystems.FileObject;
55 import org.openide.util.NbBundle;
56 import org.openide.util.RequestProcessor;
57
58 /**
59  *
60  * @author Pavel Buzek
61  */

62 public class PersistenceClientEntitySelectionVisual extends javax.swing.JPanel JavaDoc {
63     
64     private WizardDescriptor wizard;
65     private ChangeSupport changeSupport = new ChangeSupport(this);
66     private Project project;
67     boolean waitingForScan;
68     boolean waitingForEntities;
69     private List JavaDoc<EntityMappings> waitForMappings = new ArrayList JavaDoc<EntityMappings>();
70     private PersistenceUnit persistenceUnit;
71     // TODO: RETOUCHE
72
// private EntityClosure entityClosure;
73

74     
75     
76     /** Creates new form CrudSetupPanel */
77     public PersistenceClientEntitySelectionVisual(String JavaDoc name, WizardDescriptor wizard) {
78         setName(name);
79         this.wizard = wizard;
80         initComponents();
81         ListSelectionListener JavaDoc selectionListener = new ListSelectionListener JavaDoc() {
82             public void valueChanged(ListSelectionEvent JavaDoc e) {
83                 updateButtons();
84             }
85         };
86         listAvailable.getSelectionModel().addListSelectionListener(selectionListener);
87         listSelected.getSelectionModel().addListSelectionListener(selectionListener);
88     }
89     
90     /**
91      * @return PersistenceUnit that was created by invoking the PU wizard from
92      * this wizard or <code>null</code> if there was already a persistence unit, i.e.
93      * there was no reason to create a new persistence unit from this wizard.
94      */

95     public PersistenceUnit getPersistenceUnit() {
96         return persistenceUnit;
97     }
98     
99     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
100
private void initComponents() {
101         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
102         listAvailable = new javax.swing.JList JavaDoc();
103         jScrollPane2 = new javax.swing.JScrollPane JavaDoc();
104         listSelected = new javax.swing.JList JavaDoc();
105         cbAddRelated = new javax.swing.JCheckBox JavaDoc();
106         labelAvailableEntities = new javax.swing.JLabel JavaDoc();
107         panelButtons = new javax.swing.JPanel JavaDoc();
108         buttonRemove = new javax.swing.JButton JavaDoc();
109         buttonAdd = new javax.swing.JButton JavaDoc();
110         buttonAddAll = new javax.swing.JButton JavaDoc();
111         buttonRemoveAll = new javax.swing.JButton JavaDoc();
112         labelSelectedEntities = new javax.swing.JLabel JavaDoc();
113         createPUButton = new javax.swing.JButton JavaDoc();
114
115         listAvailable.setCellRenderer(ENTITY_LIST_RENDERER);
116         jScrollPane1.setViewportView(listAvailable);
117         listAvailable.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("LBL_AvailableEntitiesList"));
118         listAvailable.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("ACSD_AvailableEntitiesList"));
119
120         listSelected.setCellRenderer(ENTITY_LIST_RENDERER);
121         jScrollPane2.setViewportView(listSelected);
122         listSelected.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("LBL_SelectedEntitiesList"));
123         listSelected.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("ACSD_SelectedEntitiesList"));
124
125         cbAddRelated.setMnemonic(org.openide.util.NbBundle.getMessage(PersistenceClientEntitySelectionVisual.class, "MNE_InludeRelated").charAt(0));
126         cbAddRelated.setSelected(true);
127         cbAddRelated.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("LBL_IncludeReferenced"));
128         cbAddRelated.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
129         cbAddRelated.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
130         cbAddRelated.addActionListener(new java.awt.event.ActionListener JavaDoc() {
131             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
132                 cbAddRelatedActionPerformed(evt);
133             }
134         });
135
136         cbAddRelated.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("LBL_IncludeReferencedCheckbox"));
137         cbAddRelated.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("ACSD_IncludeReferencedCheckbox"));
138
139         labelAvailableEntities.setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("MNE_AvailableEntityClasses").charAt(0));
140         labelAvailableEntities.setLabelFor(listAvailable);
141         labelAvailableEntities.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("LBL_AvailableEntities"));
142
143         buttonRemove.setMnemonic(org.openide.util.NbBundle.getMessage(PersistenceClientEntitySelectionVisual.class, "MNE_Remove").charAt(0));
144         buttonRemove.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("LBL_Remove"));
145         buttonRemove.setActionCommand("< &Remove");
146         buttonRemove.addActionListener(new java.awt.event.ActionListener JavaDoc() {
147             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
148                 buttonRemoveActionPerformed(evt);
149             }
150         });
151
152         buttonAdd.setMnemonic(org.openide.util.NbBundle.getMessage(PersistenceClientEntitySelectionVisual.class, "MNE_Add").charAt(0));
153         buttonAdd.setText(org.openide.util.NbBundle.getMessage(PersistenceClientEntitySelectionVisual.class, "LBL_Add"));
154         buttonAdd.setActionCommand("&Add >");
155         buttonAdd.addActionListener(new java.awt.event.ActionListener JavaDoc() {
156             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
157                 buttonAddActionPerformed(evt);
158             }
159         });
160
161         buttonAddAll.setMnemonic(org.openide.util.NbBundle.getMessage(PersistenceClientEntitySelectionVisual.class, "MNE_AddAll").charAt(0));
162         buttonAddAll.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("LBL_AddAll"));
163         buttonAddAll.setActionCommand("Add A&ll >>");
164         buttonAddAll.addActionListener(new java.awt.event.ActionListener JavaDoc() {
165             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
166                 buttonAddAllActionPerformed(evt);
167             }
168         });
169
170         buttonRemoveAll.setMnemonic(org.openide.util.NbBundle.getMessage(PersistenceClientEntitySelectionVisual.class, "MNE_RemoveAll").charAt(0));
171         buttonRemoveAll.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("LBL_RemoveAll"));
172         buttonRemoveAll.setActionCommand("<< Re&moveAll");
173         buttonRemoveAll.addActionListener(new java.awt.event.ActionListener JavaDoc() {
174             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
175                 buttonRemoveAllActionPerformed(evt);
176             }
177         });
178
179         org.jdesktop.layout.GroupLayout panelButtonsLayout = new org.jdesktop.layout.GroupLayout(panelButtons);
180         panelButtons.setLayout(panelButtonsLayout);
181         panelButtonsLayout.setHorizontalGroup(
182             panelButtonsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
183             .add(panelButtonsLayout.createSequentialGroup()
184                 .addContainerGap()
185                 .add(panelButtonsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
186                     .add(org.jdesktop.layout.GroupLayout.TRAILING, buttonAdd, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
187                     .add(org.jdesktop.layout.GroupLayout.TRAILING, buttonRemove, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
188                     .add(org.jdesktop.layout.GroupLayout.TRAILING, buttonRemoveAll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
189                     .add(org.jdesktop.layout.GroupLayout.TRAILING, buttonAddAll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))
190                 .addContainerGap())
191         );
192         panelButtonsLayout.setVerticalGroup(
193             panelButtonsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
194             .add(panelButtonsLayout.createSequentialGroup()
195                 .add(67, 67, 67)
196                 .add(buttonAdd)
197                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
198                 .add(buttonRemove)
199                 .add(20, 20, 20)
200                 .add(buttonAddAll)
201                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
202                 .add(buttonRemoveAll)
203                 .addContainerGap(103, Short.MAX_VALUE))
204         );
205
206         labelSelectedEntities.setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("MNE_SelectedEntityClasses").charAt(0));
207         labelSelectedEntities.setLabelFor(listSelected);
208         labelSelectedEntities.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("LBL_SelectedEntities"));
209
210         createPUButton.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("MNE_CreatePersistenceUnit").charAt(0));
211         createPUButton.setText(org.openide.util.NbBundle.getMessage(PersistenceClientEntitySelectionVisual.class, "LBL_CreatePersistenceUnit"));
212         createPUButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
213             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
214                 createPUButtonActionPerformed(evt);
215             }
216         });
217
218         createPUButton.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("LBL_CreatePersistenceUnitButton"));
219         createPUButton.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/persistence/wizard/Bundle").getString("ACSD_CreatePersistenceUnitButton"));
220
221         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
222         this.setLayout(layout);
223         layout.setHorizontalGroup(
224             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
225             .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
226                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
227                     .add(layout.createSequentialGroup()
228                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
229                             .add(labelAvailableEntities)
230                             .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE))
231                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
232                         .add(panelButtons, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
233                         .add(0, 0, 0))
234                     .add(layout.createSequentialGroup()
235                         .add(createPUButton)
236                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
237                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
238                     .add(cbAddRelated)
239                     .add(labelSelectedEntities)
240                     .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE))
241                 .addContainerGap())
242         );
243         layout.setVerticalGroup(
244             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
245             .add(layout.createSequentialGroup()
246                 .addContainerGap()
247                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
248                     .add(labelSelectedEntities)
249                     .add(labelAvailableEntities))
250                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
251                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
252                     .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
253                     .add(panelButtons, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
254                     .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE))
255                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
256                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
257                     .add(layout.createSequentialGroup()
258                         .add(cbAddRelated)
259                         .add(31, 31, 31))
260                     .add(layout.createSequentialGroup()
261                         .add(createPUButton)
262                         .addContainerGap())))
263         );
264     }// </editor-fold>//GEN-END:initComponents
265

266     private void cbAddRelatedActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_cbAddRelatedActionPerformed
267
listSelected.clearSelection();
268         listAvailable.clearSelection();
269         // entityClosure.setClosureEnabled(cbAddRelated.isSelected());
270

271         changeSupport.fireChange();
272     }//GEN-LAST:event_cbAddRelatedActionPerformed
273

274     private void createPUButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_createPUButtonActionPerformed
275
persistenceUnit = Util.buildPersistenceUnitUsingWizard(project, null, TableGeneration.CREATE);
276         if (persistenceUnit != null){
277             updatePersistenceUnitButton();
278             // TODO: RETOUCHE
279
//waitForMappings.add(PersistenceUtils.getAnnotationEntityMappings(project));
280
changeSupport.fireChange();
281         }
282     }//GEN-LAST:event_createPUButtonActionPerformed
283

284     private void buttonRemoveAllActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_buttonRemoveAllActionPerformed
285
// entityClosure.removeAllEntities();
286
listSelected.clearSelection();
287         updateButtons();
288         changeSupport.fireChange();
289     }//GEN-LAST:event_buttonRemoveAllActionPerformed
290

291     private void buttonAddAllActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_buttonAddAllActionPerformed
292
// entityClosure.addAllEntities();
293
listAvailable.clearSelection();
294         updateButtons();
295         changeSupport.fireChange();
296     }//GEN-LAST:event_buttonAddAllActionPerformed
297

298     private void buttonRemoveActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_buttonRemoveActionPerformed
299
Object JavaDoc selected[] = listSelected.getSelectedValues();
300         Set JavaDoc<Entity> sel = new HashSet JavaDoc<Entity>();
301         for (int i = 0; i < selected.length; i++) {
302             sel.add((Entity) selected[i]);
303         }
304         // entityClosure.removeEntities(sel);
305
listSelected.clearSelection();
306         updateButtons();
307         
308         changeSupport.fireChange();
309     }//GEN-LAST:event_buttonRemoveActionPerformed
310

311     private void buttonAddActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_buttonAddActionPerformed
312
Object JavaDoc selected[] = listAvailable.getSelectedValues();
313         Set JavaDoc<Entity> sel = new HashSet JavaDoc<Entity>();
314         for (int i = 0; i < selected.length; i++) {
315             sel.add((Entity) selected[i]);
316         }
317         // entityClosure.addEntities(sel);
318
listAvailable.clearSelection();
319         updateButtons();
320         
321         changeSupport.fireChange();
322     }//GEN-LAST:event_buttonAddActionPerformed
323

324     
325     // Variables declaration - do not modify//GEN-BEGIN:variables
326
private javax.swing.JButton JavaDoc buttonAdd;
327     private javax.swing.JButton JavaDoc buttonAddAll;
328     private javax.swing.JButton JavaDoc buttonRemove;
329     private javax.swing.JButton JavaDoc buttonRemoveAll;
330     private javax.swing.JCheckBox JavaDoc cbAddRelated;
331     private javax.swing.JButton JavaDoc createPUButton;
332     private javax.swing.JScrollPane JavaDoc jScrollPane1;
333     private javax.swing.JScrollPane JavaDoc jScrollPane2;
334     private javax.swing.JLabel JavaDoc labelAvailableEntities;
335     private javax.swing.JLabel JavaDoc labelSelectedEntities;
336     private javax.swing.JList JavaDoc listAvailable;
337     private javax.swing.JList JavaDoc listSelected;
338     private javax.swing.JPanel JavaDoc panelButtons;
339     // End of variables declaration//GEN-END:variables
340

341     public void addChangeListener(ChangeListener JavaDoc listener) {
342         changeSupport.addChangeListener(listener);
343     }
344     
345     boolean valid(WizardDescriptor wizard) {
346         // check PU - not just warning, required
347
if (createPUButton.isVisible()) {
348             wizard.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(PersistenceClientEntitySelectionVisual.class, "ERR_NoPersistenceUnit"));
349             return false;
350         }
351         
352         // TODO: RETOUCHE
353
// if (JavaMetamodel.getManager().isScanInProgress()) {
354
if (false){
355             if (!waitingForScan) {
356                 waitingForScan = true;
357                 RequestProcessor.Task task = RequestProcessor.getDefault().create(new Runnable JavaDoc() {
358                     public void run() {
359                         // TODO: RETOUCHE
360
//JavaMetamodel.getManager().waitScanFinished();
361
waitingForScan = false;
362                         changeSupport.fireChange();
363                         updateButtons();
364                     }
365                 });
366                 wizard.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(PersistenceClientEntitySelectionVisual.class, "scanning-in-progress"));
367                 task.schedule(0);
368             }
369             return false;
370         }
371         
372         if(ORMMetadata.getDefault().isScanInProgress()) {
373             if (!waitingForEntities) {
374                 waitingForEntities = true;
375                 RequestProcessor.Task task = RequestProcessor.getDefault().create(new Runnable JavaDoc() {
376                     public void run() {
377                         ORMMetadata.getDefault().waitScanFinished();
378                         synchronized (this) {
379                             // TODO: RETOUCHE
380
// entityClosure.addAvaliableEntities(PersistenceUtils.getEntityClasses(waitForMappings));
381
waitForMappings.clear();
382                         }
383                         waitingForEntities = false;
384                         changeSupport.fireChange();
385                         updateButtons();
386                     }
387                 });
388                 wizard.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(PersistenceClientEntitySelectionVisual.class, "scanning-in-progress"));
389                 task.schedule(0);
390             }
391             return false;
392         } else {
393             synchronized (this) {
394                 if (waitForMappings.size() > 0) {
395                     // entityClosure.addAvaliableEntities(PersistenceUtils.getEntityClasses(waitForMappings));
396
waitForMappings.clear();
397                     updateButtons();
398                 }
399             }
400         }
401         if (listSelected.getModel().getSize() == 0) {
402             wizard.putProperty("WizardPanel_errorMessage",
403                     NbBundle.getMessage(PersistenceClientEntitySelectionVisual.class, "MSG_NoEntityClassesSelected"));
404             return false;
405         }
406         wizard.putProperty("WizardPanel_errorMessage", " "); //NOI18N
407
return true;
408     }
409     
410     void read(WizardDescriptor settings) {
411         project = Templates.getProject(settings);
412         // try {
413
synchronized (this) {
414             waitForMappings.clear();
415             if (getPersistenceUnit() == null){
416                 // TODO: RETOUCHE
417
// waitForMappings.addAll(PersistenceUtils.getEntityMappings(project));
418
} else {
419                 // there are no persistence units in the project but one was created from this wizard
420
// TODO: RETOUCHE
421
// waitForMappings.add(PersistenceUtils.getAnnotationEntityMappings(project));
422
}
423         }
424         // } catch (IOException e) {
425
// ErrorManager.getDefault().notify(e);
426
// }
427
// entityClosure = new EntityClosure(Collections.<Entity>emptySet());
428
// entityClosure.setClosureEnabled(cbAddRelated.isSelected());
429
// listAvailable.setModel(new EntityListModel(entityClosure, true));
430
// listSelected.setModel(new EntityListModel(entityClosure, false));
431
List JavaDoc<Entity> entites = (List JavaDoc<Entity>) settings.getProperty(WizardProperties.ENTITY_CLASS);
432         if (entites == null) {
433             entites = new ArrayList JavaDoc();
434         }
435         // entityClosure.addEntities(new HashSet<Entity>(entites));
436

437         updateButtons();
438         updatePersistenceUnitButton();
439     }
440     
441     void store(WizardDescriptor settings) {
442         ListModel JavaDoc model = listSelected.getModel();
443         if (model instanceof EntityListModel) {
444             EntityListModel elm = (EntityListModel) model;
445             settings.putProperty(WizardProperties.ENTITY_CLASS, elm.getEntityClasses());
446         }
447     }
448     
449     private void updateButtons() {
450         buttonAdd.setEnabled(listAvailable.getSelectedValues().length > 0);
451         // buttonAddAll.setEnabled(entityClosure.getAvailableEntities().size() > 0);
452
buttonRemove.setEnabled(listSelected.getSelectedValues().length > 0);
453         // buttonRemoveAll.setEnabled(entityClosure.getSelectedEntities().size() > 0);
454
}
455     
456     public void updatePersistenceUnitButton() {
457         boolean visible = getPersistenceUnit() == null;
458         if (ProviderUtil.isValidServerInstanceOrNone(project) && visible) {
459             PersistenceScope scopes[] = PersistenceUtils.getPersistenceScopes(project);
460             for (int i = 0; i < scopes.length; i++) {
461                 FileObject persistenceXml = scopes[i].getPersistenceXml();
462                 if (persistenceXml != null) {
463                     try {
464                         Persistence persistence = PersistenceMetadata.getDefault().getRoot(persistenceXml);
465                         // TODO: should ask the user which pu should be used
466
if (persistence.getPersistenceUnit().length > 0) {
467                             visible = false;
468                             break;
469                         }
470                     } catch (IOException JavaDoc ex) {
471                         ErrorManager.getDefault().notify(ex);
472                     }
473                 }
474             }
475         }
476         createPUButton.setVisible(visible);
477     }
478     
479     private static final class EntityComparator implements Comparator JavaDoc<Entity> {
480         public int compare(Entity o1, Entity o2) {
481             return o1.getName().compareTo(o2.getName());
482         }
483     }
484     private static final EntityComparator ENTITY_COMPARATOR = new EntityComparator();
485     private final ListCellRenderer JavaDoc ENTITY_LIST_RENDERER = new EntityListCellRenderer();
486     
487     private class EntityListModel extends AbstractListModel JavaDoc implements ChangeListener JavaDoc {
488         // private EntityClosure entityClosure;
489
private List JavaDoc <Entity> entities = new ArrayList JavaDoc<Entity>();
490         private boolean available;
491         
492         // TODO: RETOUCHE
493
EntityListModel(/*EntityClosure entityClosure*/ Object JavaDoc entityClosure, boolean available) {
494             // this.entityClosure = entityClosure;
495
this.available = available;
496             // entityClosure.addChangeListener(this);
497
refresh();
498         }
499         
500         public int getSize() {
501             return entities.size();
502         }
503         
504         public Object JavaDoc getElementAt(int index) {
505             return entities.get(index);
506         }
507         
508         public List JavaDoc<Entity> getEntityClasses() {
509             return entities;
510         }
511         
512         public void stateChanged(ChangeEvent JavaDoc e) {
513             refresh();
514         }
515         
516         private void refresh() {
517             int oldSize = getSize();
518             // entities = new ArrayList(available ? entityClosure.getAvailableEntities() : entityClosure.getSelectedEntities());
519
Collections.sort(entities, ENTITY_COMPARATOR);
520             fireContentsChanged(this, 0, Math.max(oldSize, getSize()));
521         }
522     }
523     
524     private final class EntityListCellRenderer extends JLabel JavaDoc implements ListCellRenderer JavaDoc {
525         public EntityListCellRenderer() {
526             setOpaque(true);
527         }
528         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
529             String JavaDoc text = null;
530             if (value instanceof Entity) {
531                 text = ((Entity) value).getClass2();
532                 if (text != null) {
533                     String JavaDoc simpleName = Util.simpleClassName(text);
534                     String JavaDoc packageName = text.length() > simpleName.length() ? text.substring(0, text.length() - simpleName.length() -1 ) : "<default package>";
535                     text = simpleName + " (" + packageName + ")";
536                 } else {
537                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Entity:" + value + " returns null from getClass2(); see IZ 80024"); //NOI18N
538
}
539             }
540             if (text == null) {
541                 text = value.toString();
542             }
543             if (isSelected) {
544                 setBackground(list.getSelectionBackground());
545                 setForeground(list.getSelectionForeground());
546             } else {
547                 setBackground(list.getBackground());
548                 setForeground(list.getForeground());
549             }
550             // setEnabled(entityClosure.getAvailableEntities().contains(value) || entityClosure.getWantedEntities().contains(value));
551
setFont(list.getFont());
552             setText(text);
553             return this;
554         }
555     }
556 }
557
Popular Tags