1 19 package org.netbeans.modules.j2ee.persistence.unit; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.util.List ; 24 import javax.swing.Action ; 25 import org.netbeans.api.project.FileOwnerQuery; 26 import org.netbeans.api.project.Project; 27 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Persistence; 28 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 29 import org.netbeans.modules.j2ee.persistence.provider.InvalidPersistenceXmlException; 30 import org.netbeans.modules.j2ee.persistence.wizard.Util; 31 import org.netbeans.modules.j2ee.persistence.wizard.entity.WrapperPanel; 32 import org.netbeans.modules.j2ee.persistence.wizard.unit.PersistenceUnitWizardDescriptor; 33 import org.netbeans.modules.j2ee.persistence.wizard.unit.PersistenceUnitWizardPanel; 34 import org.netbeans.modules.j2ee.persistence.wizard.unit.PersistenceUnitWizardPanelDS; 35 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 36 import org.netbeans.modules.j2ee.persistence.wizard.unit.PersistenceUnitWizardPanelJdbc; 37 import org.netbeans.modules.xml.multiview.*; 38 import org.netbeans.modules.xml.multiview.ui.*; 39 import org.openide.DialogDisplayer; 40 import org.openide.NotifyDescriptor; 41 import org.openide.nodes.*; 42 import org.netbeans.modules.xml.multiview.Error; 43 import org.openide.util.HelpCtx; 44 import org.openide.util.NbBundle; 45 import org.openide.util.RequestProcessor; 46 47 53 public class PersistenceToolBarMVElement extends ToolBarMultiViewElement implements PropertyChangeListener { 54 55 private ToolBarDesignEditor comp; 56 private PersistenceView view; 57 private PUDataObject puDataObject; 58 private PersistenceUnitPanelFactory factory; 59 private Action addAction, removeAction; 60 private Project project; 61 private boolean needInit = true; 62 private RequestProcessor.Task repaintingTask; 63 64 65 public PersistenceToolBarMVElement(PUDataObject dObj) { 66 super(dObj); 67 this.puDataObject=dObj; 68 this.project = FileOwnerQuery.getOwner(dObj.getPrimaryFile()); 69 addAction = new AddAction(NbBundle.getMessage(PersistenceToolBarMVElement.class,"LBL_Add")); 70 removeAction = new RemoveAction(NbBundle.getMessage(PersistenceToolBarMVElement.class,"LBL_Remove")); 71 72 comp = new ToolBarDesignEditor(); 73 factory=new PersistenceUnitPanelFactory(comp,dObj); 74 setVisualEditor(comp); 75 repaintingTask = RequestProcessor.getDefault().create(new Runnable () { 76 public void run() { 77 javax.swing.SwingUtilities.invokeLater(new Runnable () { 78 public void run() { 79 repaintView(); 80 } 81 }); 82 } 83 }); 84 85 } 86 87 public SectionView getSectionView() { 88 return view; 89 } 90 91 public void componentOpened() { 92 super.componentOpened(); 93 dObj.addPropertyChangeListener(this); 94 } 95 96 public void componentClosed() { 97 super.componentClosed(); 98 dObj.removePropertyChangeListener(this); 99 } 100 101 public void componentShowing() { 102 super.componentShowing(); 103 if (needInit){ 104 repaintView(); 105 needInit = false; 106 } 107 puDataObject.viewCanBeDisplayed(); 108 } 109 110 private void repaintView(){ 111 view = new PersistenceView(puDataObject); 112 comp.setContentView(view); 113 Object lastActive = comp.getLastActive(); 114 if (lastActive!=null) { 115 ((SectionView)view).openPanel(lastActive); 116 } else { 117 Node initialNode = view.getPersistenceUnitsNode(); 118 Children ch = initialNode.getChildren(); 119 if (ch.getNodesCount()>0) { 120 initialNode = ch.getNodes()[0]; 121 } 122 view.selectNode(initialNode); 123 } 124 view.checkValidity(); 125 126 } 127 128 public void propertyChange(PropertyChangeEvent evt) { 129 String name = evt.getPropertyName(); 130 131 if (puDataObject.PROPERTY_DATA_MODIFIED.equals(name) || puDataObject.PROPERTY_DATA_UPDATED.equals(name)){ 132 133 if (this.equals(puDataObject.getActiveMultiViewElement0())){ 134 repaintingTask.schedule(100); 135 } else { 136 needInit = true; 137 } 138 } 139 } 140 141 142 private class PersistenceView extends SectionView { 143 144 private SectionContainer persistenceUnitsCont; 145 private Node persistenceUnitsNode; 146 147 public SectionContainer getPersistenceUnitsCont(){ 148 return persistenceUnitsCont; 149 } 150 public Node getPersistenceUnitsNode(){ 151 return persistenceUnitsNode; 152 } 153 154 PersistenceView(PUDataObject dObj) { 155 super(factory); 156 Persistence persistence = dObj.getPersistence(); 157 158 PersistenceUnit[] persistenceUnits = persistence.getPersistenceUnit(); 159 Node[] persistenceUnitNode = new Node[persistenceUnits.length]; 160 Children ch = new Children.Array(); 161 for (int i=0;i<persistenceUnits.length;i++) { 162 persistenceUnitNode[i] = new PersistenceUnitNode(persistenceUnits[i]); 163 } 164 ch.add(persistenceUnitNode); 165 persistenceUnitsNode = new SectionContainerNode(ch); 166 persistenceUnitsNode.setDisplayName(NbBundle.getMessage(PersistenceToolBarMVElement.class,"LBL_PersistenceUnits")); 167 persistenceUnitsCont = new SectionContainer(this,persistenceUnitsNode, 169 NbBundle.getMessage(PersistenceToolBarMVElement.class,"LBL_PersistenceUnits")); 170 persistenceUnitsCont.setHeaderActions(new javax.swing.Action []{addAction}); 171 172 Children rootChildren = new Children.Array(); 173 rootChildren.add(new Node[]{persistenceUnitsNode}); 174 Node root = new AbstractNode(rootChildren); 175 176 SectionPanel[] pan = new SectionPanel[persistenceUnits.length]; 178 for (int i=0; i < persistenceUnits.length; i++) { 179 pan[i] = new SectionPanel(this, persistenceUnitNode[i], 180 persistenceUnitNode[i].getDisplayName(), persistenceUnits[i], false, false); 181 pan[i].setHeaderActions(new javax.swing.Action []{removeAction}); 182 persistenceUnitsCont.addSection(pan[i]); 183 } 184 addSection(persistenceUnitsCont); 185 setRoot(root); 186 } 187 188 public Error validateView() { 189 PersistenceValidator validator = new PersistenceValidator((PUDataObject)dObj); 190 List <Error > result = validator.validate(); 191 if (!result.isEmpty()){ 192 return result.get(0); 193 } 194 return null; 195 } 196 } 197 198 199 private class PersistenceUnitNode extends org.openide.nodes.AbstractNode { 200 PersistenceUnitNode(PersistenceUnit persistenceUnit) { 201 super(org.openide.nodes.Children.LEAF); 202 setDisplayName(persistenceUnit.getName()); 203 setIconBaseWithExtension("org/netbeans/modules/j2ee/persistence/unit/PersistenceIcon.gif"); } 205 206 public HelpCtx getHelpCtx() { 207 return new HelpCtx(PUDataObject.HELP_ID_DESIGN_PERSISTENCE_UNIT); } 209 210 } 211 212 215 private class AddAction extends javax.swing.AbstractAction { 216 217 AddAction(String actionName) { 218 super(actionName); 219 } 220 221 public void actionPerformed(java.awt.event.ActionEvent evt) { 222 boolean isContainer = Util.isSupportedJavaEEVersion(project); 223 final PersistenceUnitWizardPanel panel; 224 if (isContainer) { 225 panel = new PersistenceUnitWizardPanelDS(project, null, true); 226 } else { 227 panel = new PersistenceUnitWizardPanelJdbc(project, null, true); 228 } 229 230 final NotifyDescriptor nd = new NotifyDescriptor( 231 new WrapperPanel(panel), 232 NbBundle.getMessage(PersistenceToolBarMVElement.class, "LBL_NewPersistenceUnit"), 233 NotifyDescriptor.OK_CANCEL_OPTION, 234 NotifyDescriptor.PLAIN_MESSAGE, 235 null, null 236 ); 237 panel.addPropertyChangeListener(new PropertyChangeListener () { 238 public void propertyChange(PropertyChangeEvent evt) { 239 if (evt.getPropertyName().equals(PersistenceUnitWizardPanel.IS_VALID)) { 240 Object newvalue = evt.getNewValue(); 241 if (newvalue != null && newvalue instanceof Boolean ) { 242 validateUnitName(panel); 243 nd.setValid((Boolean ) newvalue); 244 245 } 246 } 247 } 248 }); 249 if (!panel.isValidPanel()) { 250 validateUnitName(panel); 251 nd.setValid(false); 252 } 253 Object result = DialogDisplayer.getDefault().notify(nd); 254 255 if (result == NotifyDescriptor.OK_OPTION) { 256 PersistenceUnit punit = new PersistenceUnit(); 257 258 if (isContainer) { 259 PersistenceUnitWizardPanelDS puPanel = (PersistenceUnitWizardPanelDS) panel; 260 if (puPanel.getDatasource() != null && !"".equals(puPanel.getDatasource().trim())){ 261 if (puPanel.isJTA()) { 262 punit.setJtaDataSource(puPanel.getDatasource()); 263 } else { 264 punit.setNonJtaDataSource(puPanel.getDatasource()); 265 punit.setTransactionType("RESOURCE_LOCAL"); 266 } 267 } 268 if (puPanel.isNonDefaultProviderEnabled()) { 269 punit.setProvider(puPanel.getNonDefaultProvider()); 270 } 271 } else { 272 PersistenceUnitWizardPanelJdbc puJdbc = (PersistenceUnitWizardPanelJdbc) panel; 273 punit = ProviderUtil.buildPersistenceUnit(puJdbc.getPersistenceUnitName(), puJdbc.getSelectedProvider(), puJdbc.getPersistenceConnection()); 274 punit.setTransactionType("RESOURCE_LOCAL"); 275 if (puJdbc.getPersistenceLibrary() != null){ 276 Util.addLibraryToProject(project, puJdbc.getPersistenceLibrary()); 277 } 278 } 279 280 punit.setName(panel.getPersistenceUnitName()); 281 ProviderUtil.setTableGeneration(punit, panel.getTableGeneration(), project); 282 puDataObject.addPersistenceUnit(punit); 283 comp.setLastActive(punit); 284 } 285 } 286 } 287 288 292 private void validateUnitName(PersistenceUnitWizardPanel panel){ 293 try{ 294 if (!panel.isNameUnique()){ 295 panel.setErrorMessage(NbBundle.getMessage(PersistenceUnitWizardDescriptor.class,"ERR_PersistenceUnitNameNotUnique")); 296 } else { 297 panel.setErrorMessage(null); 298 } 299 } catch (InvalidPersistenceXmlException ipx){ 300 panel.setErrorMessage(NbBundle.getMessage(PersistenceUnitWizardDescriptor.class,"ERR_InvalidPersistenceXml", ipx.getPath())); 301 } 302 303 } 304 305 308 private class RemoveAction extends javax.swing.AbstractAction { 309 310 RemoveAction(String actionName) { 311 super(actionName); 312 } 313 314 public void actionPerformed(java.awt.event.ActionEvent evt) { 315 SectionPanel sectionPanel = ((SectionPanel.HeaderButton)evt.getSource()).getSectionPanel(); 316 PersistenceUnit punit = (PersistenceUnit) sectionPanel.getKey(); 317 org.openide.DialogDescriptor desc = new ConfirmDialog(NbBundle.getMessage(PersistenceToolBarMVElement.class,"LBL_ConfirmRemove", punit.getName())); 318 java.awt.Dialog dialog = org.openide.DialogDisplayer.getDefault().createDialog(desc); 319 dialog.setVisible(true); 320 if (org.openide.DialogDescriptor.OK_OPTION.equals(desc.getValue())) { 321 sectionPanel.getSectionView().removeSection(sectionPanel.getNode()); 322 puDataObject.removePersistenceUnit(punit); 323 } 324 } 325 } 326 } 327 | Popular Tags |