1 19 20 package org.netbeans.modules.j2ee.ddloaders.client; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.awt.Image ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.beans.PropertyChangeEvent ; 28 import java.beans.PropertyChangeListener ; 29 import java.io.IOException ; 30 import java.util.ArrayList ; 31 import java.util.Enumeration ; 32 import java.util.List ; 33 import java.util.Vector ; 34 import javax.swing.JButton ; 35 import javax.swing.event.ChangeEvent ; 36 import javax.swing.event.ChangeListener ; 37 import org.netbeans.api.java.project.JavaProjectConstants; 38 import org.netbeans.api.project.FileOwnerQuery; 39 import org.netbeans.api.project.Project; 40 import org.netbeans.api.project.ProjectUtils; 41 import org.netbeans.api.project.SourceGroup; 42 import org.netbeans.api.project.Sources; 43 import org.netbeans.api.xml.cookies.CheckXMLCookie; 44 import org.netbeans.api.xml.cookies.ValidateXMLCookie; 45 import org.netbeans.core.spi.multiview.MultiViewElement; 46 import org.netbeans.modules.j2ee.dd.api.client.AppClient; 47 import org.netbeans.modules.j2ee.dd.api.client.DDProvider; 48 import org.netbeans.modules.j2ee.dd.api.common.RootInterface; 49 import org.netbeans.modules.j2ee.dd.impl.client.ClientParseUtils; 50 import org.netbeans.modules.j2ee.dd.impl.client.AppClientProxy; 51 import org.netbeans.modules.j2ee.ddloaders.multiview.DDMultiViewDataObject; 52 import org.netbeans.modules.j2ee.ddloaders.web.DDDataObject; 53 import org.netbeans.modules.web.api.webmodule.WebModule; 54 import org.netbeans.modules.xml.multiview.DesignMultiViewDesc; 55 import org.netbeans.spi.xml.cookies.CheckXMLSupport; 56 import org.netbeans.spi.xml.cookies.DataObjectAdapters; 57 import org.netbeans.spi.xml.cookies.ValidateXMLSupport; 58 import org.openide.DialogDescriptor; 59 import org.openide.DialogDisplayer; 60 import org.openide.ErrorManager; 61 import org.openide.filesystems.FileAttributeEvent; 62 import org.openide.filesystems.FileChangeListener; 63 import org.openide.filesystems.FileEvent; 64 import org.openide.filesystems.FileObject; 65 import org.openide.filesystems.FileRenameEvent; 66 import org.openide.filesystems.FileUtil; 67 import org.openide.loaders.DataLoaderPool; 68 import org.openide.loaders.DataObjectExistsException; 69 import org.openide.loaders.OperationAdapter; 70 import org.openide.loaders.OperationEvent; 71 import org.openide.loaders.OperationListener; 72 import org.openide.util.HelpCtx; 73 import org.openide.util.NbBundle; 74 import org.openide.util.RequestProcessor; 75 import org.xml.sax.InputSource ; 76 import org.xml.sax.SAXException ; 77 import org.xml.sax.SAXParseException ; 78 79 83 public class ClientDataObject extends DDMultiViewDataObject 84 implements DDChangeListener, ChangeListener , PropertyChangeListener { 85 86 private transient AppClient appClient; 87 private transient FileObject srcRoots[]; 88 private transient FileObjectObserver fileListener; 89 90 91 private Vector updates; 92 93 private transient RequestProcessor.Task updateTask; 94 95 96 public static final String PROP_DOCUMENT_DTD = "documentDTD"; 98 99 public ClientDataObject(FileObject pf, ClientDataLoader loader) throws DataObjectExistsException { 100 super(pf, loader); 101 init(pf,loader); 102 } 103 104 private void init(FileObject fo, ClientDataLoader loader) { 105 InputSource in = DataObjectAdapters.inputSource(this); 107 CheckXMLCookie checkCookie = new CheckXMLSupport(in); 108 getCookieSet().add(checkCookie); 109 ValidateXMLCookie validateCookie = new ValidateXMLSupport(in); 110 getCookieSet().add(validateCookie); 111 112 fileListener = new FileObjectObserver(fo); 113 114 Project project = FileOwnerQuery.getOwner(getPrimaryFile()); 115 if (project != null) { 116 Sources sources = ProjectUtils.getSources(project); 117 sources.addChangeListener(this); 118 } 119 refreshSourceFolders(); 120 addPropertyChangeListener(this); 121 } 122 123 private void refreshSourceFolders() { 124 ArrayList srcRootList = new ArrayList (); 125 126 Project project = FileOwnerQuery.getOwner(getPrimaryFile()); 127 if (project != null) { 128 Sources sources = ProjectUtils.getSources(project); 129 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 130 for (int i = 0; i < groups.length; i++) { 131 if (WebModule.getWebModule(groups [i].getRootFolder()) != null) { 132 srcRootList.add(groups [i].getRootFolder()); 133 DataLoaderPool.getDefault().removeOperationListener(operationListener); DataLoaderPool.getDefault().addOperationListener(operationListener); 135 } 136 } 137 } 138 srcRoots = (FileObject []) srcRootList.toArray(new FileObject [srcRootList.size()]); 139 } 140 141 private String getPackageName(FileObject clazz) { 142 for (int i = 0; i < srcRoots.length; i++) { 143 String rp = FileUtil.getRelativePath(srcRoots [i], clazz); 144 if (rp != null) { 145 if (clazz.getExt().length() > 0) { 146 rp = rp.substring(0, rp.length() - clazz.getExt().length() - 1); 147 } 148 return rp.replace('/', '.'); 149 } 150 } 151 return null; 152 } 153 154 159 public void deploymentChange(DDChangeEvent evt) { 160 166 167 synchronized (this) { 168 if (updates == null) { 169 updates = new Vector (); 170 } 171 updates.addElement(evt); 172 } 173 174 if (updateTask == null) { 176 updateTask = RequestProcessor.getDefault().post(new Runnable () { 177 public void run() { 178 java.util.List changes = null; 179 synchronized (ClientDataObject.this) { 180 if (!ClientDataObject.this.isValid()) { 181 return; 182 } 183 if (updates != null) { 184 changes = updates; 185 updates = null; 186 } 187 } 188 if (changes != null) { 189 showDDChangesDialog(changes); 190 } 191 } 192 }, 2000, Thread.MIN_PRIORITY); 193 } else { 194 updateTask.schedule(2000); 195 } 196 } 197 198 204 public void stateChanged(ChangeEvent e) { 205 refreshSourceFolders (); 206 } 207 208 214 public void propertyChange(PropertyChangeEvent evt) { 215 if (ClientDataObject.PROP_DOCUMENT_VALID.equals (evt.getPropertyName ())) { 216 ((ClientDataNode)getNodeDelegate()).iconChanged(); 217 } 218 } 219 220 protected String getPrefixMark() { 221 return "<application-client"; 222 } 223 224 225 228 protected DesignMultiViewDesc[] getMultiViewDesc() { 229 230 return new DesignMultiViewDesc[] {}; 231 } 232 233 private class DesignMultiViewDescImpl extends DesignMultiViewDesc { 234 public MultiViewElement createElement() { 235 return null; 236 } 237 public Image getIcon() { 238 return null; 239 } 240 public String preferredID() { 241 return null; 242 } 243 } 244 249 protected void validateDocument() throws IOException { 250 parseDocument(false); 252 } 253 254 259 protected void parseDocument() throws IOException { 260 if (appClient == null || ((AppClientProxy) appClient).getOriginal() == null) { 261 try { 262 appClient = DDProvider.getDefault().getDDRoot(getPrimaryFile()); 263 } catch (IOException e) { 264 if (appClient == null) { 265 appClient = new AppClientProxy(null, null); 266 } 267 } 268 } 269 parseDocument(true); 271 } 272 273 277 protected boolean isModelCreated() { 278 return (appClient!=null && ((org.netbeans.modules.j2ee.dd.impl.client.AppClientProxy)appClient).getOriginal()!=null); 279 } 280 281 285 protected boolean isDocumentParseable() { 286 return AppClient.STATE_INVALID_UNPARSABLE != getAppClient().getStatus(); 287 } 288 289 294 protected RootInterface getDDModel() { 295 return getAppClient(); 296 } 297 298 protected org.openide.nodes.Node createNodeDelegate () { 299 return new ClientDataNode(this); 300 } 301 302 303 private void showDDChangesDialog(List changes) { 304 final JButton processButton; 305 final JButton processAllButton; 306 final JButton closeButton; 307 final DDChangesPanel connectionPanel; 308 final DialogDescriptor confirmChangesDescriptor; 309 final Dialog confirmChangesDialog[] = { null }; 310 311 processButton = new JButton (NbBundle.getMessage(DDDataObject.class, "LAB_processButton")); 312 processButton.setMnemonic(NbBundle.getMessage(DDDataObject.class, "LAB_processButton_Mnemonic").charAt(0)); 313 processButton.setToolTipText(NbBundle.getMessage(DDDataObject.class, "ACS_processButtonA11yDesc")); 314 processAllButton = new JButton (NbBundle.getMessage(DDDataObject.class, "LAB_processAllButton")); 315 processAllButton.setMnemonic(NbBundle.getMessage(DDDataObject.class, "LAB_processAllButton_Mnemonic").charAt(0)); 316 processAllButton.setToolTipText(NbBundle.getMessage(DDDataObject.class, "ACS_processAllButtonA11yDesc")); 317 closeButton = new JButton (NbBundle.getMessage(DDDataObject.class, "LAB_closeButton")); 318 closeButton.setMnemonic(NbBundle.getMessage(DDDataObject.class, "LAB_closeButton_Mnemonic").charAt(0)); 319 closeButton.setToolTipText(NbBundle.getMessage(DDDataObject.class, "ACS_closeButtonA11yDesc")); 320 final Object [] options = new Object [] { 321 processButton, 322 processAllButton 323 }; 324 final Object [] additionalOptions = new Object [] { 325 closeButton 326 }; 327 WebModule wm = WebModule.getWebModule(getPrimaryFile()); 328 String fsname=""; if (wm!=null) { 330 fsname=wm.getContextPath(); 331 } 332 String caption = NbBundle.getMessage(DDDataObject.class, "MSG_SynchronizeCaption", fsname); 333 connectionPanel = new DDChangesPanel(caption, processButton); 334 confirmChangesDescriptor = new DialogDescriptor( 335 connectionPanel, 336 NbBundle.getMessage(DDDataObject.class, "LAB_ConfirmDialog"), 337 true, 338 options, 339 processButton, 340 DialogDescriptor.RIGHT_ALIGN, 341 HelpCtx.DEFAULT_HELP, 342 new ActionListener () { 343 public void actionPerformed(ActionEvent e) { 344 if (e.getSource() instanceof Component ) { 345 Component root; 346 347 root = javax.swing.SwingUtilities.getRoot((Component )e.getSource()); 349 if (!root.isDisplayable()) { 350 return; 351 } 352 } 353 if (options[0].equals(e.getSource())) { 354 int min = connectionPanel.changesList.getMinSelectionIndex(); 355 int max = connectionPanel.changesList.getMaxSelectionIndex(); 356 for (int i = max; i >= min; i--) { 357 if (connectionPanel.changesList.isSelectedIndex(i)) { 358 final DDChangeEvent ev = (DDChangeEvent)connectionPanel.listModel.getElementAt(i); 359 processDDChangeEvent(ev); 360 connectionPanel.listModel.removeElementAt(i); 361 } 362 } 363 if (connectionPanel.listModel.isEmpty()) { 364 confirmChangesDialog[0].setVisible(false); 365 } else { 366 processButton.setEnabled(false); 367 } 368 } else if (options[1].equals(e.getSource())) { 369 Enumeration en = connectionPanel.listModel.elements(); 370 while (en.hasMoreElements()) { 371 processDDChangeEvent((DDChangeEvent)en.nextElement()); 372 } 373 confirmChangesDialog[0].setVisible(false); 374 connectionPanel.setChanges(null); 375 } else if (additionalOptions[0].equals(e.getSource())) { 376 confirmChangesDialog[0].setVisible(false); 377 connectionPanel.setChanges(null); 378 } 379 } 380 } 381 ); 382 confirmChangesDescriptor.setAdditionalOptions(additionalOptions); 383 384 processButton.setEnabled(false); 385 processAllButton.requestFocus(); 386 connectionPanel.setChanges(changes); 387 388 try { 389 confirmChangesDialog[0] = DialogDisplayer.getDefault().createDialog(confirmChangesDescriptor); 390 confirmChangesDialog[0].setVisible(true); 391 } finally { 392 confirmChangesDialog[0].dispose(); 393 } 394 } 395 396 private void processDDChangeEvent(DDChangeEvent evt) { 397 System.err.println("ClientDataObject.processDDChangeEvent"); 398 if (!isValid()) { 399 return; 400 } 401 402 515 } 516 517 private void parseDocument(boolean updateWebApp) throws IOException { 518 AppClientProxy webAppProxy = (AppClientProxy) appClient; 519 try { 520 SAXParseException error = ClientParseUtils.parse(new InputSource (createReader())); 522 setSaxError(error); 523 524 String version = ClientParseUtils.getVersion(new InputSource (createReader())); 525 AppClientProxy app = new AppClientProxy(org.netbeans.modules.j2ee.dd.impl.common.DDUtils.createAppClient( 527 createInputStream(), version), version); 528 if (updateWebApp) { 529 String webAppProxyVersion = webAppProxy.getVersion() != null ? webAppProxy.getVersion().toString() : ""; 530 if (version.equals(webAppProxyVersion) && webAppProxy.getOriginal() != null) { 531 appClient.merge(app, AppClient.MERGE_UPDATE); 532 } else if (app.getOriginal() != null) { 533 appClient = webAppProxy = app; 534 } 535 } 536 webAppProxy.setStatus(error != null ? AppClient.STATE_INVALID_PARSABLE : AppClient.STATE_VALID); 537 webAppProxy.setError(error); 538 } catch (SAXException ex) { 539 webAppProxy.setStatus(AppClient.STATE_INVALID_UNPARSABLE); 540 if (ex instanceof SAXParseException ) { 541 webAppProxy.setError((SAXParseException ) ex); 542 } else if (ex.getException() instanceof SAXParseException ) { 543 webAppProxy.setError((SAXParseException ) ex.getException()); 544 } 545 setSaxError(ex); 546 } 547 } 548 549 public AppClient getAppClient() { 550 if (appClient == null) { 551 try { 552 appClient = createWebApp(); 553 } catch (IOException ex) { 554 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 555 } 556 } 557 return appClient; 558 } 559 560 private AppClient createWebApp() throws java.io.IOException { 561 AppClient webApp = DDProvider.getDefault().getDDRoot(getPrimaryFile()); 562 if (webApp != null) { 563 setSaxError(webApp.getError()); 564 } 565 return webApp; 566 } 567 568 private OperationListener operationListener = new OperationAdapter() { 569 public void operationDelete(OperationEvent ev) { 570 FileObject fo = ev.getObject().getPrimaryFile(); 571 String resourceName = getPackageName(fo); 572 if (resourceName != null && "java".equals(fo.getExt())) { boolean foundElement=false; 574 608 } 609 } 610 }; 611 612 614 private class FileObjectObserver implements FileChangeListener { 615 FileObjectObserver(FileObject fo) { 616 fo.addFileChangeListener((FileChangeListener)org.openide.util.WeakListeners.create( 617 FileChangeListener.class, this, fo)); 618 } 619 620 public void fileAttributeChanged(FileAttributeEvent fileAttributeEvent) { 621 } 622 623 public void fileChanged(FileEvent fileEvent) { 624 637 } 638 639 public void fileDataCreated(FileEvent fileEvent) { 640 } 641 642 public void fileDeleted(FileEvent fileEvent) { 643 } 644 645 public void fileFolderCreated(FileEvent fileEvent) { 646 } 647 648 public void fileRenamed(FileRenameEvent fileRenameEvent) { 649 } 650 } 651 652 } 653 | Popular Tags |