View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.support.editor.inspectors.jms.property;
14  
15  import java.awt.BorderLayout;
16  import java.awt.event.ActionEvent;
17  import java.beans.PropertyChangeEvent;
18  import java.beans.PropertyChangeListener;
19  
20  import javax.swing.AbstractAction;
21  import javax.swing.JButton;
22  import javax.swing.JComponent;
23  import javax.swing.JPanel;
24  import javax.swing.JScrollPane;
25  import javax.swing.JTable;
26  import javax.swing.SwingUtilities;
27  import javax.swing.event.ListSelectionEvent;
28  import javax.swing.event.ListSelectionListener;
29  import javax.swing.event.TableModelEvent;
30  import javax.swing.event.TableModelListener;
31  
32  import com.eviware.soapui.impl.wsdl.panels.request.StringToStringMapTableModel;
33  import com.eviware.soapui.support.UISupport;
34  import com.eviware.soapui.support.components.JXToolBar;
35  import com.eviware.soapui.support.editor.EditorView;
36  import com.eviware.soapui.support.editor.inspectors.AbstractXmlInspector;
37  import com.eviware.soapui.support.editor.views.xml.raw.RawXmlEditorFactory;
38  import com.eviware.soapui.support.editor.xml.XmlDocument;
39  
40  public class JMSPropertyInspector extends AbstractXmlInspector implements PropertyChangeListener
41  {
42  	private StringToStringMapTableModel headersTableModel;
43  	private final JMSPropertyInspectorModel model;
44  	private JTable headersTable;
45  	private JPanel panel;
46  	private JButton removeButton;
47  	public boolean changing;
48  
49  	protected JMSPropertyInspector( JMSPropertyInspectorModel model )
50  	{ 
51  		super( "JMS Property (" + ( model.getJMSProperties() == null ? "0" : model.getJMSProperties().size() ) + ")",
52  				"Additional JMS Property for this message", true, JMSPropertyInspectorFactory.INSPECTOR_ID );
53  		
54  		this.model = model;
55  
56  		model.addPropertyChangeListener( this );
57  		model.setInspector(this);
58  	}
59  
60  	
61  	public JComponent getComponent()
62  	{
63  		if( panel != null )
64  			return panel;
65  
66  		headersTableModel = new StringToStringMapTableModel( model.getJMSProperties(), "Key", "Value", !model.isReadOnly() );
67  		headersTableModel.addTableModelListener( new TableModelListener()
68  		{
69  			public void tableChanged( TableModelEvent arg0 )
70  			{
71  				model.setJMSProperties( headersTableModel.getData() );
72  				setTitle( "JMS Property (" + ( model.getJMSProperties() == null ? "0" : model.getJMSProperties().size() ) + ")" );
73  			}
74  		} );
75  
76  		headersTable = new JTable( headersTableModel );
77  
78  		panel = new JPanel( new BorderLayout() );
79  		panel.add( new JScrollPane( headersTable ), BorderLayout.CENTER );
80  
81  		if( !model.isReadOnly() )
82  		{
83  			headersTable.setSurrendersFocusOnKeystroke( true );
84  			headersTable.putClientProperty( "terminateEditOnFocusLost", Boolean.TRUE );
85  
86  			JXToolBar builder = UISupport.createSmallToolbar();
87  			builder.addFixed( UISupport.createToolbarButton( new AddAction() ) );
88  			removeButton = UISupport.createToolbarButton( new RemoveAction() );
89  			builder.addFixed( removeButton );
90  			builder.addGlue();
91  //			builder.addFixed( UISupport.createToolbarButton( new ShowOnlineHelpAction( HelpUrls.HEADERS_HELP_URL ) ) );
92  
93  			panel.add( builder, BorderLayout.NORTH );
94  
95  			headersTable.getSelectionModel().addListSelectionListener( new ListSelectionListener()
96  			{
97  
98  				public void valueChanged( ListSelectionEvent e )
99  				{
100 					removeButton.setEnabled( headersTable.getSelectedRow() != -1 );
101 				}
102 			} );
103 
104 			if( headersTable.getRowCount() > 0 )
105 				headersTable.setRowSelectionInterval( 0, 0 );
106 			else
107 				removeButton.setEnabled( false );
108 		}
109 
110 		return panel;
111 	}
112 
113 	public JTable getHeadersTable()
114 	{
115 		return headersTable;
116 	}
117 
118 	@Override
119 	public void release()
120 	{
121 		super.release();
122 		model.release();
123 		model.removePropertyChangeListener( this );
124 	}
125 
126 	public void propertyChange( PropertyChangeEvent evt )
127 	{
128 		if( !changing )
129 			headersTableModel.setData( model.getJMSProperties() );
130 	}
131 
132 	private final class RemoveAction extends AbstractAction
133 	{
134 		private RemoveAction()
135 		{
136 			super();
137 			putValue( AbstractAction.SMALL_ICON, UISupport.createImageIcon( "/remove_property.gif" ) );
138 			putValue( AbstractAction.SHORT_DESCRIPTION, "Removes the selected custom JMS Property from this message" );
139 		}
140 
141 		public void actionPerformed( ActionEvent arg0 )
142 		{
143 			int row = headersTable.getSelectedRow();
144 			if( row != -1 && UISupport.confirm( "Delete selected JMS Property?", "Remove JMS Property" ) )
145 			{
146 				changing = true;
147 				headersTableModel.remove( row );
148 				changing = false;
149 			}
150 		}
151 	}
152 
153 	private final class AddAction extends AbstractAction
154 	{
155 		private AddAction()
156 		{
157 			super();
158 			putValue( AbstractAction.SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ) );
159 			putValue( AbstractAction.SHORT_DESCRIPTION, "Adds a custom JMS Property to this message" );
160 		}
161 
162 		public void actionPerformed( ActionEvent arg0 )
163 		{
164 			Object header = UISupport.prompt( "Specify name of JMS Property to add", "Add JMS Property", "" );
165 			if( header != null )
166 			{
167 				changing = true;
168 				headersTableModel.add( header.toString(), "" );
169 				SwingUtilities.invokeLater( new Runnable()
170 				{
171 					public void run()
172 					{
173 						int row = headersTable.getRowCount() - 1;
174 						headersTable.scrollRectToVisible( headersTable.getCellRect( row, 1, true ) );
175 						headersTable.setRowSelectionInterval( row, row );
176 
177 						SwingUtilities.invokeLater( new Runnable()
178 						{
179 							public void run()
180 							{
181 								headersTable.editCellAt( headersTable.getRowCount() - 1, 1 );
182 								headersTable.getEditorComponent().requestFocusInWindow();
183 							}
184 						} );
185 					}
186 				} );
187 
188 				changing = false;
189 			}
190 		}
191 	}
192 
193 	@Override
194 	public boolean isEnabledFor( EditorView<XmlDocument> view )
195 	{
196 		return !view.getViewId().equals( RawXmlEditorFactory.VIEW_ID );
197 	}
198 }