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 Properties (" + ( 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  	public JComponent getComponent()
61  	{
62  		if( panel != null )
63  			return panel;
64  
65  		headersTableModel = new StringToStringMapTableModel( model.getJMSProperties(), "Key", "Value", !model
66  				.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  
77  		headersTable = new JTable( headersTableModel );
78  
79  		panel = new JPanel( new BorderLayout() );
80  		panel.add( new JScrollPane( headersTable ), BorderLayout.CENTER );
81  
82  		if( !model.isReadOnly() )
83  		{
84  			headersTable.setSurrendersFocusOnKeystroke( true );
85  			headersTable.putClientProperty( "terminateEditOnFocusLost", Boolean.TRUE );
86  
87  			JXToolBar builder = UISupport.createSmallToolbar();
88  			builder.addFixed( UISupport.createToolbarButton( new AddAction() ) );
89  			removeButton = UISupport.createToolbarButton( new RemoveAction() );
90  			builder.addFixed( removeButton );
91  			builder.addGlue();
92  			// builder.addFixed( UISupport.createToolbarButton( new
93  			// ShowOnlineHelpAction( HelpUrls.HEADERS_HELP_URL ) ) );
94  
95  			panel.add( builder, BorderLayout.NORTH );
96  
97  			headersTable.getSelectionModel().addListSelectionListener( new ListSelectionListener()
98  			{
99  
100 				public void valueChanged( ListSelectionEvent e )
101 				{
102 					removeButton.setEnabled( headersTable.getSelectedRow() != -1 );
103 				}
104 			} );
105 
106 			if( headersTable.getRowCount() > 0 )
107 				headersTable.setRowSelectionInterval( 0, 0 );
108 			else
109 				removeButton.setEnabled( false );
110 		}
111 
112 		return panel;
113 	}
114 
115 	public JTable getHeadersTable()
116 	{
117 		return headersTable;
118 	}
119 
120 	@Override
121 	public void release()
122 	{
123 		super.release();
124 		model.release();
125 		model.removePropertyChangeListener( this );
126 	}
127 
128 	public void propertyChange( PropertyChangeEvent evt )
129 	{
130 		if( !changing )
131 			headersTableModel.setData( model.getJMSProperties() );
132 	}
133 
134 	private final class RemoveAction extends AbstractAction
135 	{
136 		private RemoveAction()
137 		{
138 			super();
139 			putValue( AbstractAction.SMALL_ICON, UISupport.createImageIcon( "/remove_property.gif" ) );
140 			putValue( AbstractAction.SHORT_DESCRIPTION, "Removes the selected custom JMS Property from this message" );
141 		}
142 
143 		public void actionPerformed( ActionEvent arg0 )
144 		{
145 			int row = headersTable.getSelectedRow();
146 			if( row != -1 && UISupport.confirm( "Delete selected JMS Property?", "Remove JMS Property" ) )
147 			{
148 				changing = true;
149 				headersTableModel.remove( row );
150 				changing = false;
151 			}
152 		}
153 	}
154 
155 	private final class AddAction extends AbstractAction
156 	{
157 		private AddAction()
158 		{
159 			super();
160 			putValue( AbstractAction.SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ) );
161 			putValue( AbstractAction.SHORT_DESCRIPTION, "Adds a custom JMS Property to this message" );
162 		}
163 
164 		public void actionPerformed( ActionEvent arg0 )
165 		{
166 			Object header = UISupport.prompt( "Specify name of JMS Property to add", "Add JMS Property", "" );
167 			if( header != null )
168 			{
169 				changing = true;
170 				headersTableModel.add( header.toString(), "" );
171 				SwingUtilities.invokeLater( new Runnable()
172 				{
173 					public void run()
174 					{
175 						int row = headersTable.getRowCount() - 1;
176 						headersTable.scrollRectToVisible( headersTable.getCellRect( row, 1, true ) );
177 						headersTable.setRowSelectionInterval( row, row );
178 
179 						SwingUtilities.invokeLater( new Runnable()
180 						{
181 							public void run()
182 							{
183 								headersTable.editCellAt( headersTable.getRowCount() - 1, 1 );
184 								headersTable.getEditorComponent().requestFocusInWindow();
185 							}
186 						} );
187 					}
188 				} );
189 
190 				changing = false;
191 			}
192 		}
193 	}
194 
195 	@Override
196 	public boolean isEnabledFor( EditorView<XmlDocument> view )
197 	{
198 		return !view.getViewId().equals( RawXmlEditorFactory.VIEW_ID );
199 	}
200 }