[jboss-cvs] jbosside/as/plugins/org.jboss.ide.eclipse.as.ui/jbossui/org/jboss/ide/eclipse/as/ui/dialogs ...

Robert Stryker rawblem at gmail.com
Mon Dec 4 12:45:30 EST 2006


  User: rawb    
  Date: 06/12/04 12:45:30

  Modified:    as/plugins/org.jboss.ide.eclipse.as.ui/jbossui/org/jboss/ide/eclipse/as/ui/dialogs  
                        XPathDialogs.java
  Added:       as/plugins/org.jboss.ide.eclipse.as.ui/jbossui/org/jboss/ide/eclipse/as/ui/dialogs  
                        ConvertNodeToXPathDialog.java
  Log:
  Changes to dialog allow you to turn a selection from an xml file into an xpath to be used in the properties view
  
  Revision  Changes    Path
  1.9       +299 -134  jbosside/as/plugins/org.jboss.ide.eclipse.as.ui/jbossui/org/jboss/ide/eclipse/as/ui/dialogs/XPathDialogs.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: XPathDialogs.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbosside/as/plugins/org.jboss.ide.eclipse.as.ui/jbossui/org/jboss/ide/eclipse/as/ui/dialogs/XPathDialogs.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -b -r1.8 -r1.9
  --- XPathDialogs.java	28 Nov 2006 17:31:48 -0000	1.8
  +++ XPathDialogs.java	4 Dec 2006 17:45:30 -0000	1.9
  @@ -21,17 +21,21 @@
    */
   package org.jboss.ide.eclipse.as.ui.dialogs;
   
  +import java.lang.reflect.InvocationTargetException;
   import java.util.ArrayList;
   import java.util.Arrays;
   import java.util.HashMap;
   
  +import org.eclipse.core.runtime.IProgressMonitor;
   import org.eclipse.core.runtime.Path;
   import org.eclipse.jface.dialogs.Dialog;
   import org.eclipse.jface.dialogs.IDialogConstants;
  +import org.eclipse.jface.dialogs.ProgressMonitorDialog;
   import org.eclipse.jface.fieldassist.ContentProposalAdapter;
   import org.eclipse.jface.fieldassist.IContentProposal;
   import org.eclipse.jface.fieldassist.IContentProposalProvider;
   import org.eclipse.jface.fieldassist.TextContentAdapter;
  +import org.eclipse.jface.operation.IRunnableWithProgress;
   import org.eclipse.jface.viewers.ITreeContentProvider;
   import org.eclipse.jface.viewers.TreeViewer;
   import org.eclipse.jface.viewers.Viewer;
  @@ -43,9 +47,13 @@
   import org.eclipse.swt.layout.FormAttachment;
   import org.eclipse.swt.layout.FormData;
   import org.eclipse.swt.layout.FormLayout;
  +import org.eclipse.swt.layout.GridData;
  +import org.eclipse.swt.layout.GridLayout;
   import org.eclipse.swt.widgets.Button;
  +import org.eclipse.swt.widgets.Combo;
   import org.eclipse.swt.widgets.Composite;
   import org.eclipse.swt.widgets.Control;
  +import org.eclipse.swt.widgets.Display;
   import org.eclipse.swt.widgets.Label;
   import org.eclipse.swt.widgets.Shell;
   import org.eclipse.swt.widgets.Text;
  @@ -53,6 +61,8 @@
   import org.eclipse.swt.widgets.TreeColumn;
   import org.eclipse.ui.ISharedImages;
   import org.eclipse.ui.PlatformUI;
  +import org.eclipse.wst.server.core.IServer;
  +import org.jboss.ide.eclipse.as.core.JBossServerCore;
   import org.jboss.ide.eclipse.as.core.model.DescriptorModel;
   import org.jboss.ide.eclipse.as.core.model.DescriptorModel.ServerDescriptorModel;
   import org.jboss.ide.eclipse.as.core.model.DescriptorModel.ServerDescriptorModel.XPathTreeItem;
  @@ -154,37 +164,43 @@
   	
   	public static class XPathDialog extends Dialog {
   
  -		private Label errorImage, errorLabel;
  -		private Text nameText, xpathText, attributeText;
  -		private Label nameLabel, xpathLabel, attributeLabel;
  -		private Button previewButton;
  +		protected Label errorImage, errorLabel, descriptionLabel;
  +		protected Text nameText, xpathText, attributeText;
  +		protected Combo categoryCombo, serverCombo;
  +		protected Label nameLabel, serverLabel, categoryLabel, xpathLabel, attributeLabel;
  +		protected Button previewButton;
   		
  -		private SimpleTreeItem tree;
  -		private String category;
  -		private JBossServer server;
  -		private String name, xpath, attribute;
  -		private String originalName = null;
  -		private XPathPreferenceTreeItem original = null;
  -		int previewId = 48879;
  -		
  -		private Tree previewTree;
  -		private TreeColumn column, column2, column3;
  -		private TreeViewer previewTreeViewer;
  -		private Composite main;
  +		protected XPathProposalProvider proposalProvider;
   		
  -		public XPathDialog(Shell parentShell, SimpleTreeItem tree, String categoryName, JBossServer server) {
  -			super(parentShell);
  -			this.tree = tree;
  -			this.category = categoryName;
  -			this.server = server;
  +		protected SimpleTreeItem tree;
  +		protected JBossServer server;
  +		protected String name, xpath, attribute, category;
  +		protected String originalName = null;
  +		protected XPathPreferenceTreeItem original = null;
  +		protected int previewId = 48879;
  +		
  +		protected Tree previewTree;
  +		protected TreeColumn column, column2, column3;
  +		protected TreeViewer previewTreeViewer;
  +		protected Composite main;
  +		
  +		public XPathDialog(Shell parentShell) {
  +			this(parentShell, null);
  +		}
  +		public XPathDialog(Shell parentShell, JBossServer server) {
  +			this(parentShell, server, null);
  +		}
  +		public XPathDialog(Shell parentShell, JBossServer server, String categoryName) {
  +			this(parentShell, server, categoryName, null);
   		}
   
  -		public XPathDialog(Shell parentShell, SimpleTreeItem tree, String categoryName, JBossServer server, String originalName) {
  +		public XPathDialog(Shell parentShell, JBossServer server, String categoryName, String originalName) {
   			super(parentShell);
  -			this.tree = tree;
   			this.category = categoryName;
   			this.server = server;
   			this.originalName = this.name = originalName;
  +			if( server != null )
  +				this.tree = server.getAttributeHelper().getXPathPreferenceTree();
   		}
   
   		protected void configureShell(Shell shell) {
  @@ -199,8 +215,12 @@
   			super.createButtonsForButtonBar(parent);
   			previewButton = createButton(parent, previewId, "Preview", true);
   			if( name == null ) getButton(IDialogConstants.OK_ID).setEnabled(false);
  -
   			addListeners();
  +			try {
  +				checkErrors();
  +			} catch( Exception e ) {
  +				e.printStackTrace();
  +			}
   		}
   
   		
  @@ -208,24 +228,40 @@
   			main = (Composite)super.createDialogArea(parent);
   			main.setLayout(new FormLayout());
   			layoutWidgets(main);
  -			
  +			fillCombos();
   			if( name != null ) nameText.setText(name);
   			if( attribute != null ) attributeText.setText(attribute);
   			if( xpath != null ) xpathText.setText(xpath);
   			
  +			proposalProvider = new XPathProposalProvider();
  +			proposalProvider.setServer(server);
   			 ContentProposalAdapter adapter = new
   			 ContentProposalAdapter(xpathText, new TextContentAdapter(),
  -			                                 new XPathProposalProvider(server), null, null);
  +					proposalProvider, null, null);
   			                
   			 adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
  -
   			return main;
   		} 
   		
  +		protected void fillCombos() {
  +			if( serverCombo != null ) {
  +				IServer servers[] = JBossServerCore.getIServerJBossServers();
  +				String[] names = new String[servers.length];
  +				for( int i = 0; i < servers.length; i++ ) {
  +					names[i] = servers[i].getName();
  +				}
  +				serverCombo.setItems(names);
  +			}
  +			
  +			if( categoryCombo != null && tree != null ) {
  +				refreshCategoryCombo();
  +			}
  +		}
  +		
   		protected void addListeners() {
   			nameText.addModifyListener(new ModifyListener() {
   				public void modifyText(ModifyEvent e) {
  -					verifyName();
  +					checkErrors();
   					name = nameText.getText();
   				} 
   			});
  @@ -247,17 +283,94 @@
   					previewPressed();
   				} 
   			});
  +			
  +			
  +			if( serverCombo != null ) {
  +				serverCombo.addSelectionListener(new SelectionListener() {
  +					public void widgetDefaultSelected(SelectionEvent e) {
  +					}
  +					public void widgetSelected(SelectionEvent e) {
  +						int index = serverCombo.getSelectionIndex();
  +						String val = serverCombo.getItem(index);
  +						JBossServer[] serverList = JBossServerCore.getAllJBossServers();
  +						for( int i = 0; i < serverList.length; i++ ) {
  +							if( serverList[i].getServer().getName().equals(val)) {
  +								setServer(serverList[i]);
  +								return;
  +							}
  +						}
  +					} 
  +				});
   		}
  -		protected void verifyName() {
   			
  -			if( nameText.getText().equals("")) {
  -				errorLabel.setText(Messages.XPathNameEmpty);
  -				errorLabel.setVisible(true);
  -				errorImage.setVisible(true);
  +			if( categoryCombo != null ) {
  +				categoryCombo.addModifyListener(new ModifyListener() {
  +					public void modifyText(ModifyEvent e) {
  +						category = categoryCombo.getText();
  +						checkErrors();
  +					} 
  +				});
  +				categoryCombo.addSelectionListener(new SelectionListener() {
  +					public void widgetDefaultSelected(SelectionEvent e) {
  +					}
  +					public void widgetSelected(SelectionEvent e) {
  +						category = categoryCombo.getText();
  +						checkErrors();
  +					}
  +				});
  +			}
  +		}
  +		
  +		
  +		protected void checkErrors() {
  +			ArrayList errorList = getErrors();
  +			if( errorList.size() == 0 ) { 
  +				setError(null); 
  +				getButton(IDialogConstants.OK_ID).setEnabled(true);
  +				return; 
  +			}
  +			setError((String)errorList.get(0));
  +			if( getButton(IDialogConstants.OK_ID) != null ) 
   				getButton(IDialogConstants.OK_ID).setEnabled(false);
   				return;
   			}
  +		protected ArrayList getErrors() {
  +			ArrayList list = new ArrayList();
  +			String serverError = getServerError(); if( serverError != null ) list.add(serverError);
  +			String nameError = getNameError(); if( nameError != null ) list.add(nameError);
  +			String categoryError = getCategoryError(); if( categoryError != null ) list.add(categoryError);
  +			return list;
  +		}
  +		
  +		protected String getServerError() {
  +			if( server == null ) return "Please Select a Server";
  +			return null;
  +		}
   			
  +		protected String getCategoryError() {
  +			if( "".equals(category)) {
  +				return "Category must not be blank";
  +			}
  +			return null;
  +		}
  +		
  +		protected void setError(String message) {
  +			if( message == null ) {
  +				errorImage.setVisible(false);
  +				errorLabel.setVisible(false);
  +				errorLabel.setText("");
  +			} else {
  +				errorImage.setVisible(true);
  +				errorLabel.setVisible(true);
  +				errorLabel.setText(message);
  +			}
  +		}
  +		
  +		protected String getNameError() {
  +			if( nameText.getText().equals("")) {
  +				return Messages.XPathNameEmpty;
  +			}
  +			if( tree != null ) {
   			SimpleTreeItem[] categories = tree.getChildren();
   			SimpleTreeItem categoryItem = null;
   			for( int i = 0; i < categories.length; i++ ) {
  @@ -271,51 +384,80 @@
   					if(nameText.getText().equals( ((XPathPreferenceTreeItem)xpathNames[i]).getName())) {
   						
   						if( originalName == null || !nameText.getText().equals(originalName)) 
  -							found = true;
  +								return Messages.XPathNameInUse;
   					}
   				}
  -				if( found ) {
  -					// error, name in use
  -					errorLabel.setText(Messages.XPathNameInUse);
  -					errorLabel.setVisible(true);
  -					errorImage.setVisible(true);
  -					getButton(IDialogConstants.OK_ID).setEnabled(false);
  -				} else {
  -					errorLabel.setVisible(false);
  -					errorImage.setVisible(false);
  -					getButton(IDialogConstants.OK_ID).setEnabled(true);
   				}
   			}
  +			return null;
  +		}
  +
  +		
  +		protected void setServer(JBossServer s) {
  +			server = s;
  +			tree = server.getAttributeHelper().getXPathPreferenceTree();
  +			proposalProvider.setServer(s);
  +			refreshCategoryCombo();
  +			checkErrors();
   		}
  +		
  +		protected void refreshCategoryCombo() {
  +			SimpleTreeItem[] categories = tree.getChildren();
  +			String[] categoryNames = new String[categories.length];
  +			for( int i = 0; i < categories.length; i++ ) {
  +				categoryNames[i] = (String)categories[i].getData();
  +			}
  +			categoryCombo.setItems(categoryNames);
  +		}
  +		
   		protected void previewPressed() {
  +			if( server == null ) {
  +				checkErrors();
  +				return;
  +			}
  +			
  +			IRunnableWithProgress op = new IRunnableWithProgress() {
  +				public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
  +					final IProgressMonitor monitor2 = monitor;
  +					server.getDescriptorModel().refreshDescriptors(monitor2);
   			XPathTreeItem[] item = server.getDescriptorModel().getXPath(xpathText.getText(), attributeText.getText());
  -			ArrayList list = new ArrayList();
  +					final ArrayList list = new ArrayList();
   			list.addAll(Arrays.asList(item));
  +					Display.getDefault().asyncExec(new Runnable() { 
  +						public void run() {
   			previewTreeViewer.setInput(list);
  -			
   			if( list.size() == 0 ) {
   				errorImage.setVisible(true);
   				errorLabel.setText("No XML elements matched your search.");
   				errorLabel.setVisible(true);
   			} else {
  -				errorImage.setVisible(false);
  -				errorLabel.setVisible(false);
  +								checkErrors();
   			}
  -			
   			main.layout();
   		}
  +					});
  +				}
  +			};
  +			try {
  +				new ProgressMonitorDialog(new Shell()).run(false, true, op);
  +			} catch( Exception e) {
  +				e.printStackTrace();
  +			}
  +		}
   		protected void layoutWidgets(Composite c) {
   			// create widgets
  +			descriptionLabel = new Label(c, SWT.WRAP);
  +			descriptionLabel.setText("An XPath is a way to find a specific XML element inside an xml file. \n" + 
  +					"This dialog will help you create one. These XPaths' values can then be modified\n"
  +					+ "by using the JBoss Servers View with the Properties View.");
  +			descriptionLabel.setVisible(true);
   			errorLabel = new Label(c, SWT.NONE);
   			errorImage = new Label(c, SWT.NONE);
   			errorImage.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_ERROR_TSK));
   			
  -			nameLabel = new Label(c, SWT.NONE);
  -			xpathLabel = new Label(c, SWT.NONE);
  -			attributeLabel = new Label(c, SWT.NONE);
  -			nameText= new Text(c, SWT.BORDER);
  -			xpathText = new Text(c, SWT.BORDER);
  -			attributeText = new Text(c, SWT.BORDER);
  +			
  +			Composite middleComposite = createMiddleComposite(c);
  +			
   
   			// Now do the tree and viewer
   			previewTree = new Tree(c, SWT.BORDER);
  @@ -336,76 +478,46 @@
   			previewTreeViewer = new TreeViewer(previewTree);
   
   			
  -			// set some text
  -			nameLabel.setText(Messages.XPathName);
  -			xpathLabel.setText(Messages.XPathPattern);
  -			attributeLabel.setText(Messages.XPathAttribute);
  +
  +			
   
   			c.layout();
   			int pixel = Math.max(Math.max(nameLabel.getSize().x, xpathLabel.getSize().x), attributeLabel.getSize().x);
   			pixel += 5;
   			
   			// Lay them out
  -			//errorLabel.setText("Category Name In Use.");
  +			FormData descriptionData = new FormData();
  +			descriptionData.left = new FormAttachment(0, 5);
  +			descriptionData.right = new FormAttachment(100, -5);
  +			descriptionData.top = new FormAttachment(0,5);
  +			descriptionLabel.setLayoutData(descriptionData);
  +			
   			FormData errorData = new FormData();
   			errorData.left = new FormAttachment(errorImage,5);
  -			errorData.top = new FormAttachment(0,5);
  +			errorData.top = new FormAttachment(descriptionLabel,5);
   			errorData.right = new FormAttachment(0,300);
   			errorLabel.setLayoutData(errorData);
   			errorLabel.setVisible(false);
   			
   			FormData errorImageData = new FormData();
   			errorImageData.left = new FormAttachment(0,5);
  -			errorImageData.top = new FormAttachment(0,5);
  +			errorImageData.top = new FormAttachment(descriptionLabel,5);
   			errorImage.setLayoutData(errorImageData);
   			errorImage.setVisible(false);
   
   			
  -			/* Name */
  -			FormData nameLabelData = new FormData();
  -			nameLabelData.left = new FormAttachment(0,5);
  -			nameLabelData.top = new FormAttachment(errorLabel,5);
  -			nameLabel.setLayoutData(nameLabelData);
  -
  -			FormData nameTextData = new FormData();
  -			nameTextData.left = new FormAttachment(0,pixel);
  -			nameTextData.right = new FormAttachment(100,-5);
  -			nameTextData.top = new FormAttachment(errorLabel,4);
  -			nameText.setLayoutData(nameTextData);
  -
  -
  -			/* XPath */
  -			FormData xpathLabelData = new FormData();
  -			xpathLabelData.left = new FormAttachment(0,5);
  -			xpathLabelData.top = new FormAttachment(nameText,5);
  -			xpathLabel.setLayoutData(xpathLabelData);
  -
  -			FormData xpathTextData = new FormData();
  -			xpathTextData.left = new FormAttachment(0,pixel);
  -			xpathTextData.right = new FormAttachment(100,-5);
  -			xpathTextData.top = new FormAttachment(nameText,4);
  -			xpathText.setLayoutData(xpathTextData);
  -
  -
  -			/* Attribute */
  -			FormData attributeLabelData = new FormData();
  -			attributeLabelData.left = new FormAttachment(0,5);
  -			attributeLabelData.top = new FormAttachment(xpathText,5);
  -			attributeLabel.setLayoutData(attributeLabelData);
  -
  -			FormData attributeTextData = new FormData();
  -			attributeTextData.left = new FormAttachment(0, pixel);
  -			attributeTextData.right = new FormAttachment(100,-5);
  -			attributeTextData.top = new FormAttachment(xpathText,4);
  -			
  -			attributeText.setLayoutData(attributeTextData);
   			
  +			FormData middleCompositeData = new FormData();
  +			middleCompositeData.left = new FormAttachment(0,5);
  +			middleCompositeData.right = new FormAttachment(100, -5);
  +			middleCompositeData.top = new FormAttachment(errorLabel, 5);
  +			middleComposite.setLayoutData(middleCompositeData);
   			
   			// Tree layout data
   			FormData previewTreeData = new FormData();
   			previewTreeData.left = new FormAttachment(0,5);
   			previewTreeData.right = new FormAttachment(100,-5);
  -			previewTreeData.top = new FormAttachment(attributeText,5);
  +			previewTreeData.top = new FormAttachment(middleComposite,5);
   			previewTreeData.bottom = new FormAttachment(100,-5);
   			previewTree.setLayoutData(previewTreeData);
   			
  @@ -455,6 +567,44 @@
   			});
   			
   			previewTreeViewer.setLabelProvider(new XPathPropertyLabelProvider());
  +			
  +		}
  +
  +		protected Composite createMiddleComposite(Composite c) {
  +			Composite gridComposite = new Composite(c, SWT.NONE);
  +			gridComposite.setLayout(new GridLayout(2, false));
  +			
  +			nameLabel = new Label(gridComposite, SWT.NONE);
  +			nameText= new Text(gridComposite, SWT.BORDER);
  +			nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
  +			
  +			if( server == null ) {
  +				serverLabel = new Label(gridComposite, SWT.NONE);
  +				serverCombo = new Combo(gridComposite, SWT.BORDER | SWT.READ_ONLY);
  +				serverCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
  +			}
  +			
  +			if( category == null ) {
  +				categoryLabel = new Label(gridComposite, SWT.NONE);
  +				categoryCombo = new Combo(gridComposite, SWT.BORDER);
  +				categoryCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
  +			}
  +			
  +			xpathLabel = new Label(gridComposite, SWT.NONE);
  +			xpathText = new Text(gridComposite, SWT.BORDER);
  +			xpathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
  +			attributeLabel = new Label(gridComposite, SWT.NONE);
  +			attributeText = new Text(gridComposite, SWT.BORDER);
  +			attributeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
  +
  +
  +			// set some text
  +			nameLabel.setText(Messages.XPathName);
  +			if( serverLabel != null ) serverLabel.setText("Server: ");
  +			if( categoryLabel != null ) categoryLabel.setText("Category: ");
  +			xpathLabel.setText(Messages.XPathPattern);
  +			attributeLabel.setText(Messages.XPathAttribute);
  +			return gridComposite;
   		}
   
   		public String getAttribute() {
  @@ -469,6 +619,13 @@
   			return xpath;
   		}
   
  +		public String getCategory() {
  +			return category;
  +		}
  +		public JBossServer getServer() {
  +			return server;
  +		}
  +
   		public void setAttribute(String attribute) {
   			this.attribute = attribute;
   			if( attributeText != null && !attributeText.isDisposed())
  @@ -503,12 +660,20 @@
   		
   		private JBossServer server;
   		private ServerDescriptorModel model;
  -		public XPathProposalProvider(JBossServer server) {
  +		public XPathProposalProvider() {
  +		}
  +		public void setServer(JBossServer server) {
   			this.server = server;
  +			if( server != null ) {
   			String serverConfDir = server.getConfigDirectory(false);
   			model = DescriptorModel.getDefault().getServerModel(new Path(serverConfDir));
  +			} else {
  +				model = null;
  +			}
   		}
   		public IContentProposal[] getProposals(String contents, int position) {
  +			if( model == null ) return new IContentProposal[]{};
  +			
   			if( contents.equals("") || contents.equals("/") || contents.equals(" ")) {
   				return new IContentProposal[] { new XPathContentProposal("/server/", "/server/".length(), null, null)};
   			}
  @@ -520,7 +685,7 @@
   			if( type == IN_ATTRIBUTE ) return getAttributeNameProposals(contents);
   			if( type == NEW_ATTRIBUTE_VALUE ) return getAttributeValueProposals(contents, "");
   			if( type == IN_ATTRIBUTE_VALUE ) return getAttributeValueProposals(contents);
  -			return null;
  +			return new IContentProposal[]{};
   		}
   		
   		protected XPathTreeItem2[] getXPath(String xpath) {
  
  
  
  1.1      date: 2006/12/04 17:45:30;  author: rawb;  state: Exp;jbosside/as/plugins/org.jboss.ide.eclipse.as.ui/jbossui/org/jboss/ide/eclipse/as/ui/dialogs/ConvertNodeToXPathDialog.java
  
  Index: ConvertNodeToXPathDialog.java
  ===================================================================
  /**
   * JBoss, a Division of Red Hat
   * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
  * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.jboss.ide.eclipse.as.ui.dialogs;
  
  import java.util.ArrayList;
  
  import org.eclipse.jface.text.DocumentEvent;
  import org.eclipse.jface.text.IDocument;
  import org.eclipse.jface.text.ITextViewer;
  import org.eclipse.jface.text.contentassist.ICompletionProposal;
  import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
  import org.eclipse.jface.text.contentassist.IContextInformation;
  import org.eclipse.jface.window.Window;
  import org.eclipse.swt.SWT;
  import org.eclipse.swt.events.SelectionEvent;
  import org.eclipse.swt.events.SelectionListener;
  import org.eclipse.swt.graphics.Image;
  import org.eclipse.swt.graphics.Point;
  import org.eclipse.swt.layout.FormAttachment;
  import org.eclipse.swt.layout.FormData;
  import org.eclipse.swt.layout.FormLayout;
  import org.eclipse.swt.layout.GridData;
  import org.eclipse.swt.layout.GridLayout;
  import org.eclipse.swt.widgets.Combo;
  import org.eclipse.swt.widgets.Composite;
  import org.eclipse.swt.widgets.Control;
  import org.eclipse.swt.widgets.Group;
  import org.eclipse.swt.widgets.Label;
  import org.eclipse.swt.widgets.Shell;
  import org.eclipse.wst.xml.core.internal.document.AttrImpl;
  import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
  import org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest;
  import org.eclipse.wst.xml.ui.internal.contentassist.XMLContentAssistProcessor;
  import org.jboss.ide.eclipse.as.core.server.JBossServer;
  import org.jboss.ide.eclipse.as.core.server.ServerAttributeHelper;
  import org.jboss.ide.eclipse.as.core.server.ServerAttributeHelper.SimpleXPathPreferenceTreeItem;
  import org.jboss.ide.eclipse.as.core.server.ServerAttributeHelper.XPathPreferenceTreeItem;
  import org.jboss.ide.eclipse.as.core.util.SimpleTreeItem;
  import org.jboss.ide.eclipse.as.ui.Messages;
  import org.jboss.ide.eclipse.as.ui.dialogs.XPathDialogs.XPathDialog;
  import org.jboss.ide.eclipse.as.ui.mbeans.editors.proposals.IServiceXMLQuickFixProposalProvider;
  import org.jboss.ide.eclipse.as.ui.views.server.JBossServerView;
  import org.w3c.dom.NamedNodeMap;
  import org.w3c.dom.Node;
  
  /**
   *
   * @author rob.stryker at jboss.com
   */
  public class ConvertNodeToXPathDialog extends XPathDialog {
  	private Node node;
  	private ArrayList keyRows;
  	
  	private static final String NO_ATTRIBUTE = "NONE";
  	
  	protected ConvertNodeToXPathDialog(Shell parentShell, Node node, String attributeName) {
  		super(parentShell);
  		this.node = node;
  		setAttribute(attributeName == null ? "" : attributeName);
  	}
  	
  	protected void configureShell(Shell shell) {
  		super.configureShell(shell);
  		setShellStyle(getShellStyle() | SWT.RESIZE);
  		shell.setText(Messages.XPathNewXpath);
  		shell.setBounds(shell.getLocation().x, shell.getLocation().y, 550, 550);
  	}
  
  	protected Control createDialogArea(Composite parent) {
  		Control c = super.createDialogArea(parent);
  		refreshXPath();
  		return c;
  	}
  	
  	protected Composite createMiddleComposite(Composite c) {
  		Composite middleComposite = new Composite(c, SWT.NONE);
  		middleComposite.setLayout(new FormLayout());
  		
  		Composite gridComposite = super.createMiddleComposite(middleComposite);
  		FormData gridData = new FormData();
  		gridData.left = new FormAttachment(0,5);
  		gridData.right = new FormAttachment(100, -5);
  		gridData.top = new FormAttachment(0,5);
  		gridComposite.setLayoutData(gridData);
  		
  		// now add my stuff?
  		Composite keyComposite = createKeyComposite(middleComposite);
  		FormData keyData = new FormData();
  		keyData.left = new FormAttachment(0,5);
  		keyData.right = new FormAttachment(100,-5);
  		keyData.top = new FormAttachment(gridComposite, 5);
  		keyComposite.setLayoutData(keyData);
  		
  		return middleComposite;
  	}
  	
  	protected Composite createKeyComposite(Composite parent) {
  		Group main = new Group(parent, SWT.NONE);
  		main.setLayout(new GridLayout(2, false));
  		keyRows = new ArrayList();
  		Node localNode = node;
  		String pathText = "";
  		while( localNode != null && !(localNode instanceof IDOMDocument)) {
  			if( !localNode.getNodeName().equals("#text")) {
  				keyRows.add(0, new NameComboPair(main, localNode));
  			}
  			localNode = localNode.getParentNode();
  		}
  
  		for( int i = 0; i < keyRows.size(); i++ ) {
  			((NameComboPair)keyRows.get(i)).create();
  		}
  		
  		main.setText("Keyed Attributes");
  		
  		
  		// The very last row should not key off the same attribute name that they wish to change
  		final NameComboPair lastRow = (NameComboPair)keyRows.get(keyRows.size()-1);
  		lastRow.combo.addSelectionListener(new SelectionListener() {
  			public void widgetDefaultSelected(SelectionEvent e) {
  			}
  			public void widgetSelected(SelectionEvent e) {
  				checkErrors();
  			} 
  		});
  		
  		return main;
  	}
  	protected ArrayList getErrors() {
  		ArrayList errors = super.getErrors();
  		NameComboPair pair = (NameComboPair)keyRows.get(keyRows.size()-1);
  		String val = pair.combo.getItem(pair.combo.getSelectionIndex());
  		if(attribute != null && val.startsWith(attribute + "=")) {
  			errors.add("You should not key off the value you wish to change");
  		}
  		return errors;
  	}
  
  	
  	public void refreshXPath() {
  		String finalString = "";
  		for( int i = 0; i < keyRows.size(); i++ ) {
  			finalString += ((NameComboPair)keyRows.get(i)).toString();
  		}
  		setXpath(finalString);
  		previewPressed();
  	}
  	
  	protected class NameComboPair {
  		private Node node;
  		private Composite parent;
  		private Label label;
  		private Combo combo;
  		public NameComboPair(Composite parent, Node node) {
  			this.node = node;
  			this.parent = parent;
  		}
  		public void create() {
  			label = new Label(parent, SWT.NONE);
  			label.setText(node.getNodeName());
  			combo = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
  			label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
  			combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
  			
  			// fill the combo
  			NamedNodeMap map = node.getAttributes();
  			ArrayList list = new ArrayList();
  			list.add(NO_ATTRIBUTE);
  			int selectedIndex = 0;
  			for( int i = 0; i < map.getLength(); i++ ) {
  				Node attr = map.item(i);
  				if( attr instanceof AttrImpl ) {
  					AttrImpl impl = ((AttrImpl)attr);
  					list.add(impl.getName() + "='" + impl.getValue() + "'");
  					if( impl.getName().equals("name") && !attribute.equals("name"))
  						selectedIndex = i+1;
  				}
  			}
  			
  			combo.setItems((String[]) list.toArray(new String[list.size()]));
  			combo.select(selectedIndex);
  			
  			
  			// selection listener
  			combo.addSelectionListener(new SelectionListener() {
  				public void widgetDefaultSelected(SelectionEvent e) {
  				}
  				public void widgetSelected(SelectionEvent e) {
  					refreshXPath();
  				} 
  			});
  		}
  		
  		public String toString() {
  			String ret = "/" + node.getNodeName();
  			String comboSelection = combo.getItem(combo.getSelectionIndex());
  			if( !comboSelection.equals(NO_ATTRIBUTE))
  				ret += "[@" + comboSelection + "]";
  			return ret;
  		}
  	}
  
  	
  	public static class ConvertNodeToXPathDialogProvider extends XMLContentAssistProcessor implements IServiceXMLQuickFixProposalProvider {
  
  		public ICompletionProposal[] getProposals(ITextViewer viewer, int offset) {
  			return computeCompletionProposals(viewer, offset);
  		}
  		
  		protected void addAttributeNameProposals(ContentAssistRequest contentAssistRequest) {
  			Node n = contentAssistRequest.getNode();
  			String name = n.getNodeName();
  			String attName = ((AttrImpl)n.getAttributes().getNamedItem(contentAssistRequest.getText())).getName();
  			contentAssistRequest.addProposal(createProposal(n, attName));
  		}
  		
  		protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest) {
  			String elementName = contentAssistRequest.getNode().getNodeName();
  			String match = contentAssistRequest.getMatchString();
  			String text = contentAssistRequest.getText();
  			int beginPos = contentAssistRequest.getReplacementBeginPosition();
  
  			// find the attribute we're inside of, because the contentAssistRequester only returns the element (BOO!)
  			NamedNodeMap map = contentAssistRequest.getNode().getAttributes();
  			
  			boolean found = false;
  			AttrImpl attribute = null;
  			for( int i = 0; i < map.getLength() && !found; i++ ) {
  				Node tmp = map.item(i);
  				if( tmp instanceof AttrImpl ) {
  					int start = ((AttrImpl)tmp).getStartOffset();
  					int end = ((AttrImpl)tmp).getEndOffset();
  					if( beginPos > start && beginPos < end ) {
  						found = true;
  						attribute = (AttrImpl)tmp;
  					}
  				}
  			}
  			if( found ) {
  				contentAssistRequest.addProposal(createProposal(contentAssistRequest.getNode(), attribute.getName()));
  			}
  		}
  		
  		protected void addEmptyDocumentProposals(ContentAssistRequest contentAssistRequest) {
  		}
  		
  		protected void addTagInsertionProposals(ContentAssistRequest contentAssistRequest, int childPosition) {
  			contentAssistRequest.addProposal(createProposal(contentAssistRequest.getNode(), null));
  		}
  		
  		
  		protected ICompletionProposal createProposal(Node node, String attributeName) {
  			return new OpenXPathDialogProposal(node, attributeName);
  		}
  	}
  	
  	public static class OpenXPathDialogProposal implements ICompletionProposal, ICompletionProposalExtension2 {
  		private Node node;
  		private String attributeName;
  		public OpenXPathDialogProposal(Node node, String attributeName ) {
  			this.node = node;
  			this.attributeName = attributeName;
  		}
  		public void apply(IDocument document) {
  		}
  
  		public String getAdditionalProposalInfo() {
  			return null;
  		}
  
  		public IContextInformation getContextInformation() {
  			return null;
  		}
  
  		public String getDisplayString() {
  			return "Add to XPaths";
  		}
  
  		public Image getImage() {
  			return null;
  		}
  
  		public Point getSelection(IDocument document) {
  			return null;
  		}
  
  		public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
  			ConvertNodeToXPathDialog d = new ConvertNodeToXPathDialog(new Shell(), node, attributeName);
  			int result = d.open();
  			if( result == Window.OK) {
  				JBossServer s = d.getServer();
  				SimpleTreeItem tree = s.getAttributeHelper().getXPathPreferenceTree();
  				String category = d.getCategory();
  				SimpleTreeItem[] categories = tree.getChildren();
  				SimpleTreeItem categoryItem = null;
  				for( int i = 0; i < categories.length; i++ ) {
  					if( categories[i].getData().equals(category)) 
  						categoryItem = categories[i];
  				}
  				
  				// If the category doesn't exist, create it
  				if( categoryItem == null ) {
  					categoryItem = new SimpleXPathPreferenceTreeItem(tree, category);
  				}
  				
  				XPathPreferenceTreeItem dsfa = new XPathPreferenceTreeItem(categoryItem, d.getName(), d.getXpath(), d.getAttribute());
  				ServerAttributeHelper helper = s.getAttributeHelper();
  				helper.saveXPathPreferenceTree(tree);
  				helper.save();
  				JBossServerView.getDefault().refreshJBTree(null);
  			}
  		}
  
  		public void selected(ITextViewer viewer, boolean smartToggle) {
  		}
  
  		public void unselected(ITextViewer viewer) {
  		}
  
  		public boolean validate(IDocument document, int offset, DocumentEvent event) {
  			return false;
  		}
  		
  	}
  	
  }
  
  
  



More information about the jboss-cvs-commits mailing list