/tools/plugins/com.liferay.ide.portlet.ui/src/com/liferay/ide/portlet/ui/navigator/PortletsNode.java

https://gitlab.com/4615833/liferay-ide · Java · 167 lines · 113 code · 33 blank · 21 comment · 11 complexity · 27f926ba33f73ab3c912f5e93e860d39 MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. *
  14. * Contributors:
  15. * Kamesh Sampath - initial implementation
  16. * Gregory Amerson - initial implementation review and ongoing maintenance
  17. *******************************************************************************/
  18. package com.liferay.ide.portlet.ui.navigator;
  19. import com.liferay.ide.portlet.core.model.Portlet;
  20. import com.liferay.ide.portlet.core.model.PortletApp;
  21. import com.liferay.ide.portlet.ui.PortletUIPlugin;
  22. import com.liferay.ide.project.core.util.ProjectUtil;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import org.eclipse.core.resources.IFile;
  26. import org.eclipse.sapphire.modeling.xml.RootXmlResource;
  27. import org.eclipse.sapphire.modeling.xml.XmlResourceStore;
  28. import org.eclipse.wst.sse.core.StructuredModelManager;
  29. import org.eclipse.wst.sse.core.internal.provisional.IModelStateListener;
  30. import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
  31. /**
  32. * @author <a href="mailto:kamesh.sampath@hotmail.com">Kamesh Sampath</a>
  33. * @author Gregory Amerson
  34. */
  35. @SuppressWarnings( "restriction" )
  36. public class PortletsNode
  37. {
  38. private static final Object[] EMPTY = new Object[] {};
  39. private PortletApp modelElement = null;
  40. private PortletResourcesRootNode parent;
  41. public PortletsNode( PortletResourcesRootNode parent )
  42. {
  43. this.parent = parent;
  44. }
  45. public Object[] getChildren()
  46. {
  47. if( this.getPortletAppModelElement() != null )
  48. {
  49. final List<PortletNode> portletNodes = new ArrayList<PortletNode>();
  50. for( Portlet portlet : this.getPortletAppModelElement().getPortlets() )
  51. {
  52. portletNodes.add( new PortletNode( this, portlet ) );
  53. }
  54. return portletNodes.toArray( new PortletNode[0] );
  55. }
  56. return EMPTY;
  57. }
  58. public PortletResourcesRootNode getParent()
  59. {
  60. return this.parent;
  61. }
  62. private PortletApp getPortletAppModelElement()
  63. {
  64. if( this.modelElement == null )
  65. {
  66. IFile portletXmlFile = ProjectUtil.getPortletXmlFile( this.parent.getProject() );
  67. if( portletXmlFile != null && portletXmlFile.exists() )
  68. {
  69. try
  70. {
  71. final IStructuredModel portletXmlModel =
  72. StructuredModelManager.getModelManager().getModelForRead( portletXmlFile );
  73. IModelStateListener listener = new IModelStateListener()
  74. {
  75. public void modelAboutToBeChanged( IStructuredModel model )
  76. {
  77. }
  78. public void modelAboutToBeReinitialized( IStructuredModel structuredModel )
  79. {
  80. }
  81. public void modelChanged( IStructuredModel model )
  82. {
  83. refresh();
  84. }
  85. public void modelDirtyStateChanged( IStructuredModel model, boolean isDirty )
  86. {
  87. refresh();
  88. }
  89. public void modelReinitialized( IStructuredModel structuredModel )
  90. {
  91. refresh();
  92. }
  93. public void modelResourceDeleted( IStructuredModel model )
  94. {
  95. refresh();
  96. }
  97. public void modelResourceMoved( IStructuredModel oldModel, IStructuredModel newModel )
  98. {
  99. refresh();
  100. }
  101. private void refresh()
  102. {
  103. portletXmlModel.removeModelStateListener( this );
  104. if( !PortletsNode.this.modelElement.disposed() )
  105. {
  106. PortletsNode.this.modelElement.dispose();
  107. }
  108. PortletsNode.this.modelElement = null;
  109. PortletsNode.this.parent.refresh();
  110. }
  111. };
  112. portletXmlModel.addModelStateListener( listener );
  113. modelElement =
  114. PortletApp.TYPE.instantiate( new RootXmlResource( new XmlResourceStore(
  115. portletXmlFile.getContents() ) ) );
  116. }
  117. catch( Exception e )
  118. {
  119. PortletUIPlugin.logError( e );
  120. }
  121. }
  122. }
  123. return this.modelElement;
  124. }
  125. public boolean hasChildren()
  126. {
  127. PortletApp model = getPortletAppModelElement();
  128. if( model != null )
  129. {
  130. return model.getPortlets().size() > 0;
  131. }
  132. return false;
  133. }
  134. }