/plugins/SQL/tags/v_0_99/sql/preprocessors/SpecialCommentRemover.java

# · Java · 346 lines · 210 code · 61 blank · 75 comment · 13 complexity · bbe600052384143b3c6849854a90d447 MD5 · raw file

  1. /**
  2. * SpecialCommentRemover.java - Sql Plugin
  3. * Copyright (C) 2001 Sergey V. Udaltsov
  4. * svu@users.sourceforge.net
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package sql.preprocessors;
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import java.util.*;
  25. import javax.swing.*;
  26. import javax.swing.border.*;
  27. import javax.swing.event.*;
  28. import org.gjt.sp.jedit.*;
  29. import org.gjt.sp.util.*;
  30. import sql.*;
  31. /**
  32. * Description of the Class
  33. *
  34. * @author svu
  35. * @created 22 ??????? 2002 ?.
  36. */
  37. public class SpecialCommentRemover extends Preprocessor
  38. {
  39. protected final static String PROP_NAME =
  40. SpecialCommentRemover.class.getName() + ".tokens";
  41. /**
  42. * Gets the OptionPane attribute of the SpecialCommentRemover object
  43. *
  44. * @return The OptionPane value
  45. */
  46. public OptionPane getOptionPane()
  47. {
  48. return new CommentOptionPane();
  49. }
  50. /**
  51. *Description of the Method
  52. *
  53. * @param text Description of Parameter
  54. * @return Description of the Returned Value
  55. * @since
  56. */
  57. public String doProcess( String text )
  58. {
  59. ArrayList comments = (ArrayList) this.getAllSpecialComments();
  60. Iterator iter = comments.iterator();
  61. while ( iter.hasNext() )
  62. {
  63. final String comment = ( (String) iter.next() );
  64. int curPos = text.indexOf( comment );
  65. while ( curPos > -1 )
  66. {
  67. text = substituteFragment( text, curPos, comment.length() );
  68. curPos = text.indexOf( comment, curPos + comment.length() );
  69. }
  70. }
  71. return text;
  72. }
  73. /**
  74. * Description of the Method
  75. *
  76. * @param text Description of Parameter
  77. * @param pos Description of Parameter
  78. * @param substitutionSize Description of Parameter
  79. * @return Description of the Returned Value
  80. */
  81. protected String substituteFragment( String text, int pos, int substitutionSize )
  82. {
  83. return text.substring( 0, pos ) +
  84. text.substring( pos + substitutionSize );
  85. }
  86. /**
  87. * Gets the AllSpecialComments attribute of the SpecialCommentRemover class
  88. *
  89. * @return The AllSpecialComments value
  90. */
  91. public static java.util.List getAllSpecialComments()
  92. {
  93. final ArrayList lst = new ArrayList();
  94. try
  95. {
  96. final String prop = SqlPlugin.getGlobalProperty( PROP_NAME );
  97. final StringTokenizer strTkn = new StringTokenizer( prop, "?" );
  98. while ( strTkn.hasMoreTokens() )
  99. {
  100. lst.add( strTkn.nextToken() );
  101. }
  102. } catch ( NullPointerException ex )
  103. {
  104. //!! just ignore missing property
  105. }
  106. return lst;
  107. }
  108. /**
  109. * Description of the Method
  110. *
  111. * @param comments Description of Parameter
  112. */
  113. public static void save( java.util.List comments )
  114. {
  115. final Iterator iter = comments.iterator();
  116. final StringBuffer text = new StringBuffer();
  117. while ( iter.hasNext() )
  118. {
  119. text.append( (String) iter.next() + "?" );
  120. }
  121. final String stext = text.toString();
  122. SqlPlugin.setGlobalProperty( PROP_NAME,
  123. stext.length() > 0 ?
  124. stext.substring( 0,
  125. stext.length() - 1 ) : "" );
  126. }
  127. public static class CommentOptionPane extends SqlOptionPane
  128. {
  129. private JList allSpecialCommentsLst;
  130. private JButton addSpecialCommentBtn;
  131. private JButton editSpecialCommentBtn;
  132. private JButton delSpecialCommentBtn;
  133. private java.util.List allSpecialComments;
  134. private JTextField specialCommentField;
  135. private JFrame parentFrame = null;
  136. /**
  137. * Constructor for the SqlOptionPane object
  138. *
  139. * @since
  140. */
  141. public CommentOptionPane()
  142. {
  143. super( "sql.preprocessors.specialCommentRemover" );
  144. }
  145. /**
  146. *Description of the Method
  147. *
  148. * @since
  149. */
  150. public void _init()
  151. {
  152. super._init();
  153. JPanel panel = new JPanel( new BorderLayout( 5, 5 ) );
  154. {
  155. panel.add( new JLabel( jEdit.getProperty( "sql.options.specialCommentRemover.title.label" ) ),
  156. BorderLayout.NORTH );
  157. JPanel vp = new JPanel( new BorderLayout( 5, 5 ) );
  158. {
  159. vp.setBorder( new BevelBorder( BevelBorder.LOWERED ) );
  160. allSpecialCommentsLst = new JList();
  161. vp.add( allSpecialCommentsLst, BorderLayout.CENTER );
  162. }
  163. panel.add( vp, BorderLayout.CENTER );
  164. vp = new JPanel( new BorderLayout( 5, 5 ) );
  165. {
  166. JPanel hp = new JPanel( new BorderLayout( 5, 5 ) );
  167. {
  168. hp.add( new JLabel( jEdit.getProperty( "sql.options.specialCommentRemover.label" ) ),
  169. BorderLayout.WEST );
  170. hp.add( specialCommentField = new JTextField( "" ),
  171. BorderLayout.CENTER );
  172. }
  173. vp.add( hp, BorderLayout.NORTH );
  174. hp = new JPanel( new FlowLayout( FlowLayout.CENTER, 5, 5 ) );
  175. JPanel bp = new JPanel( new GridLayout( 1, 0, 5, 5 ) );
  176. {
  177. addSpecialCommentBtn = new JButton( jEdit.getProperty( "sql.options.specialCommentRemover.addBtn.label" ) );
  178. bp.add( addSpecialCommentBtn );
  179. delSpecialCommentBtn = new JButton( jEdit.getProperty( "sql.options.specialCommentRemover.delBtn.label" ) );
  180. bp.add( delSpecialCommentBtn );
  181. }
  182. hp.add( bp );
  183. vp.add( hp, BorderLayout.SOUTH );
  184. }
  185. panel.add( vp, BorderLayout.SOUTH );
  186. }
  187. add( panel, BorderLayout.NORTH );
  188. specialCommentField.addKeyListener(
  189. new KeyAdapter()
  190. {
  191. public void keyReleased( KeyEvent e )
  192. {
  193. updateSpecialCommentsButtons();
  194. }
  195. } );
  196. allSpecialCommentsLst.addListSelectionListener(
  197. new ListSelectionListener()
  198. {
  199. public void valueChanged( ListSelectionEvent evt )
  200. {
  201. updateSpecialCommentsButtons();
  202. }
  203. } );
  204. addSpecialCommentBtn.addActionListener(
  205. new ActionListener()
  206. {
  207. public void actionPerformed( ActionEvent evt )
  208. {
  209. final String text = specialCommentField.getText();
  210. allSpecialComments.add( text );
  211. MiscUtilities.quicksort( allSpecialComments,
  212. new Comparator()
  213. {
  214. public int compare( Object o1, Object o2 )
  215. {
  216. final String s1 = o1.toString();
  217. final String s2 = o2.toString();
  218. if ( s1.length() > s2.length() )
  219. return -1;
  220. if ( s1.length() < s2.length() )
  221. return 1;
  222. return s1.compareTo( s2 );
  223. }
  224. } );
  225. updateSpecialCommentList();
  226. }
  227. } );
  228. delSpecialCommentBtn.addActionListener(
  229. new ActionListener()
  230. {
  231. public void actionPerformed( ActionEvent evt )
  232. {
  233. final String text = (String) allSpecialCommentsLst.getSelectedValue();
  234. if ( text == null )
  235. return;
  236. allSpecialComments.remove( text );
  237. updateSpecialCommentList();
  238. }
  239. } );
  240. allSpecialComments = SpecialCommentRemover.getAllSpecialComments();
  241. updateSpecialCommentList();
  242. Component cp = this;
  243. while ( cp != null )
  244. {
  245. cp = cp.getParent();
  246. if ( cp instanceof JFrame )
  247. {
  248. parentFrame = (JFrame) cp;
  249. break;
  250. }
  251. }
  252. }
  253. /**
  254. * Description of the Method
  255. *
  256. * @since
  257. */
  258. public void _save()
  259. {
  260. SpecialCommentRemover.save( allSpecialComments );
  261. }
  262. /**
  263. * Description of the Method
  264. */
  265. protected void updateSpecialCommentList()
  266. {
  267. allSpecialCommentsLst.setListData( allSpecialComments.toArray() );
  268. updateSpecialCommentsButtons();
  269. }
  270. private void updateSpecialCommentsButtons()
  271. {
  272. final boolean isAny = allSpecialCommentsLst.getSelectedIndex() != -1;
  273. boolean isText = false;
  274. try
  275. {
  276. isText = specialCommentField.getText().indexOf( "?" ) == -1 ? true : false;
  277. } catch ( NullPointerException ex )
  278. {
  279. Log.log( Log.ERROR, SpecialCommentRemover.CommentOptionPane.class, ex );
  280. }
  281. delSpecialCommentBtn.setEnabled( isAny );
  282. addSpecialCommentBtn.setEnabled( isText );
  283. }
  284. }
  285. }