/plugins/SVNPlugin/tags/1.6.0/src/ise/plugin/svn/gui/RepositoryComboBox.java

# · Java · 237 lines · 152 code · 24 blank · 61 comment · 52 complexity · c2f9993d097704198532227a40fc2af7 MD5 · raw file

  1. /*
  2. Copyright (c) 2007, Dale Anson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the name of the author nor the names of its contributors
  12. may be used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package ise.plugin.svn.gui;
  26. import javax.swing.*;
  27. import java.util.*;
  28. import org.gjt.sp.jedit.jEdit;
  29. import ise.plugin.svn.data.RepositoryData;
  30. /**
  31. * ComboBox to choose a repository. Fills itself from jEdit properties like
  32. * this:
  33. * propertyPrefix.name.1=repository name (friendly name)
  34. * propertyPrefix.url.1=repository url
  35. * propertyPrefix.username.1=username for repository, can be null
  36. * propertyPrefix.password.1=password for username
  37. * The value of propertyPrefix.name will be displayed in the combo box dropdown.
  38. */
  39. public class RepositoryComboBox extends JComboBox {
  40. // default prefix
  41. private static final String propertyPrefix = "ise.plugins.svn.repository.";
  42. public static final String SELECT = "-- Select --";
  43. DefaultComboBoxModel dropdownModel = null; // holds the display names
  44. // holds the repository info, keyed by name
  45. HashMap < String, RepositoryData > propertyMap = new HashMap < String, RepositoryData > ();
  46. // load a list of key/hashtable pairs
  47. public RepositoryComboBox( ) {
  48. load( null );
  49. }
  50. private void load( RepositoryData selected ) {
  51. // model for the previous values
  52. Stack<String> names = new Stack<String>();
  53. // fetch the property values
  54. String url = null;
  55. int i = 0;
  56. do {
  57. String name = jEdit.getProperty( propertyPrefix + "name." + i );
  58. url = jEdit.getProperty( propertyPrefix + "url." + i );
  59. String username = jEdit.getProperty( propertyPrefix + "username." + i );
  60. String password = jEdit.getProperty( propertyPrefix + "password." + i );
  61. // must have url at minimum, this also signals the end of the loop
  62. if ( url == null ) {
  63. break;
  64. }
  65. // set name to be same as url, this is partly for backward compatibility
  66. // since the first release didn't allow naming a repository
  67. if ( name == null ) {
  68. name = url;
  69. }
  70. // add to the stack and the property lookup table
  71. if ( names.search( name ) == -1 ) {
  72. RepositoryData data = new RepositoryData( name, url, username, password );
  73. propertyMap.put( name, data );
  74. names.push( name );
  75. }
  76. ++i;
  77. }
  78. while ( url != null );
  79. // sort and fill the combo box model
  80. Collections.sort( names );
  81. dropdownModel = new DefaultComboBoxModel( ( Vector ) names );
  82. // add and choose the 'select' choice
  83. if ( dropdownModel.getSize() > 0 && dropdownModel.getIndexOf( SELECT ) < 0 ) {
  84. dropdownModel.insertElementAt( SELECT, 0 );
  85. }
  86. setModel( dropdownModel );
  87. if ( dropdownModel.getSize() > 0 ) {
  88. if ( selected != null ) {
  89. setSelectedItem( selected.getName() );
  90. }
  91. else {
  92. setSelectedIndex( 0 );
  93. }
  94. }
  95. }
  96. @Override
  97. public void setEditable( boolean editable ) {
  98. // never editable
  99. super.setEditable( false );
  100. }
  101. // item might be a repository url, so lookup the corresponding repository name
  102. @Override
  103. public void setSelectedItem(Object item) {
  104. if (item == null) {
  105. return;
  106. }
  107. String url = item.toString();
  108. Set<Map.Entry<String, RepositoryData>> set = propertyMap.entrySet();
  109. for (Map.Entry<String, RepositoryData> entry : set) {
  110. RepositoryData rd = entry.getValue();
  111. String comp_url = rd.getURL();
  112. if (url.equals(comp_url) || url.startsWith(comp_url) || comp_url.startsWith(url)) {
  113. super.setSelectedItem(entry.getKey());
  114. return;
  115. }
  116. }
  117. super.setSelectedItem(item);
  118. }
  119. public void addRepository( RepositoryData data ) {
  120. addItem( data );
  121. }
  122. /**
  123. * Adds a value to the combo box.
  124. * @param value a value to add to the list
  125. */
  126. @Override
  127. public void addItem( Object value ) {
  128. if ( value == null || !( value instanceof RepositoryData ) ) {
  129. return ;
  130. }
  131. RepositoryData data = ( RepositoryData ) value;
  132. if ( data.getURL() == null ) {
  133. return ;
  134. }
  135. if ( data.getName() == null ) {
  136. data.setName( data.getURL() );
  137. }
  138. propertyMap.put( data.getName(), data );
  139. save( data );
  140. }
  141. public void removeRepository( RepositoryData value ) {
  142. if ( value == null || !( value instanceof RepositoryData ) ) {
  143. return ;
  144. }
  145. RepositoryData data = ( RepositoryData ) value;
  146. if ( data.getURL() == null ) {
  147. return ;
  148. }
  149. if ( data.getName() == null ) {
  150. data.setName( data.getURL() );
  151. }
  152. super.removeItem( data.getName() );
  153. propertyMap.remove( data.getName() );
  154. save( null );
  155. }
  156. // could return null if SELECT is the current selection
  157. public RepositoryData getSelectedRepository() {
  158. String name = ( String ) super.getSelectedItem();
  159. return propertyMap.get( name );
  160. }
  161. /**
  162. * Saves the current list in the combo box to the jEdit property file.
  163. */
  164. public void save( RepositoryData selected ) {
  165. if (selected != null) {
  166. String name = selected.getName() == null ? selected.getURL() : selected.getName();
  167. propertyMap.put(name, selected);
  168. }
  169. // clear the old property values, this removes the property values from
  170. // the jEdit property file for all repository data, new values will be
  171. // written below.
  172. String url = null;
  173. int i = 0;
  174. do {
  175. jEdit.unsetProperty( propertyPrefix + "name." + i );
  176. jEdit.unsetProperty( propertyPrefix + "url." + i );
  177. jEdit.unsetProperty( propertyPrefix + "username." + i );
  178. jEdit.unsetProperty( propertyPrefix + "password." + i );
  179. ++i;
  180. }
  181. while ( url != null );
  182. // save the new values
  183. i = 0;
  184. Set < Map.Entry < String, RepositoryData >> set = propertyMap.entrySet();
  185. for ( Map.Entry me : set ) {
  186. String key = ( String ) me.getKey();
  187. if ( key != null ) {
  188. RepositoryData value = ( RepositoryData ) me.getValue();
  189. if ( value != null ) {
  190. if ( value.getURL() == null ) {
  191. continue;
  192. }
  193. if ( value.getName() == null ) {
  194. value.setName( value.getURL() );
  195. }
  196. jEdit.setProperty( propertyPrefix + "name." + i, value.getName() );
  197. jEdit.setProperty( propertyPrefix + "url." + i, value.getURL() );
  198. if ( value.getUsername() != null ) {
  199. jEdit.setProperty( propertyPrefix + "username." + i, value.getUsername() );
  200. }
  201. if ( value.getPassword() != null ) {
  202. jEdit.setProperty( propertyPrefix + "password." + i, value.getPassword() );
  203. }
  204. }
  205. }
  206. ++i;
  207. }
  208. load( selected );
  209. }
  210. }