/plugins/Console/branches/Console_4_3_2/console/options/ErrorsOptionPane.java

# · Java · 292 lines · 168 code · 54 blank · 70 comment · 17 complexity · b6fb445c763d73ac86d3c495d634da08 MD5 · raw file

  1. /*
  2. * ErrorsOptionPane.java - Error pattern option pane
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2003, 2005 Slava Pestov, Alan Ezust
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package console.options;
  23. //{{{ Imports
  24. import java.awt.BorderLayout;
  25. import java.awt.CardLayout;
  26. import java.awt.Dimension;
  27. import java.awt.GridBagConstraints;
  28. import java.awt.event.ActionEvent;
  29. import java.awt.event.ActionListener;
  30. import java.awt.event.MouseAdapter;
  31. import java.awt.event.MouseEvent;
  32. import java.util.HashMap;
  33. import javax.swing.Box;
  34. import javax.swing.BoxLayout;
  35. import javax.swing.DefaultListModel;
  36. import javax.swing.JButton;
  37. import javax.swing.JLabel;
  38. import javax.swing.JList;
  39. import javax.swing.JOptionPane;
  40. import javax.swing.JPanel;
  41. import javax.swing.JScrollPane;
  42. import javax.swing.JSplitPane;
  43. import javax.swing.ListSelectionModel;
  44. import javax.swing.border.EmptyBorder;
  45. import javax.swing.border.TitledBorder;
  46. import javax.swing.event.ListSelectionEvent;
  47. import javax.swing.event.ListSelectionListener;
  48. import org.gjt.sp.jedit.AbstractOptionPane;
  49. import org.gjt.sp.jedit.OptionPane;
  50. import org.gjt.sp.jedit.jEdit;
  51. import console.ConsolePlugin;
  52. import console.ErrorMatcher;
  53. import console.gui.PanelStack;
  54. //}}}
  55. //{{{ ErrorsOptionPane class
  56. /**
  57. * Shows a list of the current ErrorMatchers which can be used, and permits the easy
  58. * editing of them.
  59. */
  60. public class ErrorsOptionPane extends AbstractOptionPane
  61. {
  62. //{{{ Instance variables
  63. // Model for storing all of the ErrorMatchers
  64. private DefaultListModel errorListModel;
  65. // View for the list of errors
  66. private JList errorList;
  67. PanelStack panelStack;
  68. private JButton add;
  69. private JButton remove;
  70. //}}}
  71. // {{{ Public members
  72. //{{{ ErrorsOptionPane constructor
  73. public ErrorsOptionPane()
  74. {
  75. super("console.errors");
  76. } //}}}
  77. // }}}
  78. //{{{ Protected members
  79. //{{{ _init() method
  80. protected void _init()
  81. {
  82. setLayout(new BorderLayout());
  83. addComponent(Box.createVerticalStrut(6));
  84. errorListModel = createMatcherListModel();
  85. errorList = new JList(errorListModel);
  86. JScrollPane jsp =new JScrollPane(errorList);
  87. jsp.setMinimumSize(new Dimension(125, 300));
  88. errorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  89. errorList.addListSelectionListener(new ListHandler());
  90. errorList.addMouseListener(new MouseHandler());
  91. errorList.setVisibleRowCount(5);
  92. // JSplitPane errors = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
  93. // errors.add(jsp);
  94. String title = jEdit.getProperty("options.console.errors.caption");
  95. jsp.setBorder(new TitledBorder(title));
  96. Box westBox = new Box(BoxLayout.Y_AXIS);
  97. westBox.add(jsp);
  98. // add(jsp, BorderLayout.WEST);
  99. panelStack = new PanelStack();
  100. // errors.add(panelStack);
  101. //add(errors, BorderLayout.CENTER);
  102. add(panelStack, BorderLayout.CENTER);
  103. JPanel buttons = new JPanel();
  104. buttons.setBorder(new EmptyBorder(6,0,0,0));
  105. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  106. buttons.add(Box.createGlue());
  107. buttons.add(add = new JButton(jEdit.getProperty(
  108. "options.console.errors.add")));
  109. add.addActionListener(new ActionHandler());
  110. buttons.add(Box.createHorizontalStrut(6));
  111. buttons.add(Box.createHorizontalStrut(6));
  112. buttons.add(remove = new JButton(jEdit.getProperty(
  113. "options.console.errors.remove")));
  114. remove.addActionListener(new ActionHandler());
  115. buttons.add(Box.createHorizontalStrut(6));
  116. buttons.add(Box.createGlue());
  117. westBox.add(buttons);
  118. // add(buttons, BorderLayout.SOUTH);
  119. add(westBox, BorderLayout.WEST);
  120. errorList.setSelectedIndex(1);
  121. updateButtons();
  122. } //}}}
  123. //{{{ _save() method
  124. protected void _save()
  125. {
  126. StringBuffer list = new StringBuffer();
  127. for(int i = 0; i < errorListModel.getSize(); i++)
  128. {
  129. ErrorMatcher matcher = (ErrorMatcher)errorListModel.getElementAt(i);
  130. JPanel panel = panelStack.get(matcher.internalName());
  131. if (panel != null) {
  132. OptionPane pane = (OptionPane) panel;
  133. pane.save();
  134. }
  135. if(matcher.user)
  136. {
  137. if(i != 0)
  138. list.append(' ');
  139. list.append(matcher.internalName());
  140. }
  141. }
  142. jEdit.setProperty("console.error.user",list.toString());
  143. } //}}}
  144. //}}}
  145. //{{{ Private members
  146. //{{{ createMatcherListModel() method
  147. private DefaultListModel createMatcherListModel()
  148. {
  149. DefaultListModel listModel = new DefaultListModel();
  150. ErrorMatcher[] matchers = ConsolePlugin.loadErrorMatchers();
  151. for(int i = 0; i < matchers.length; i++)
  152. {
  153. // listModel.addElement(matchers[i].clone());
  154. String internalName = matchers[i].internalName();
  155. listModel.addElement(matchers[i]);
  156. }
  157. return listModel;
  158. } //}}}
  159. //{{{ updateButtons() method
  160. private void updateButtons()
  161. {
  162. int index = errorList.getSelectedIndex();
  163. if(index == -1)
  164. {
  165. index = 1;
  166. }
  167. ErrorMatcher matcher = (ErrorMatcher)errorList.getSelectedValue();
  168. String internalName = matcher.internalName();
  169. if (!panelStack.raise(internalName)) {
  170. ErrorMatcherPanel panel = new ErrorMatcherPanel(internalName, matcher);
  171. panelStack.add(internalName, panel);
  172. panelStack.raise(internalName);
  173. validateTree();
  174. }
  175. remove.setEnabled(matcher.user);
  176. } //}}}
  177. //}}}
  178. //{{{ ActionHandler class
  179. class ActionHandler implements ActionListener
  180. {
  181. public void actionPerformed(ActionEvent evt)
  182. {
  183. Object source = evt.getSource();
  184. if(source == add)
  185. {
  186. /* Open a dialog and ask for the name: */
  187. String matcherNamePrompt = jEdit.getProperty("options.console.errors.name");
  188. String matcherName = JOptionPane.showInputDialog(matcherNamePrompt);
  189. ErrorMatcher matcher = new ErrorMatcher();
  190. matcher.name = matcherName;
  191. matcher.user = true;
  192. int index = errorList.getSelectedIndex() + 1;
  193. errorListModel.insertElementAt(matcher,index);
  194. errorList.setSelectedIndex(index);
  195. }
  196. else if(source == remove)
  197. {
  198. errorListModel.removeElementAt(errorList.getSelectedIndex());
  199. updateButtons();
  200. }
  201. /* else if(source == up)
  202. {
  203. int index = errorList.getSelectedIndex();
  204. Object selected = errorList.getSelectedValue();
  205. errorListModel.removeElementAt(index);
  206. errorListModel.insertElementAt(selected,index-1);
  207. errorList.setSelectedIndex(index-1);
  208. }
  209. else if(source == down)
  210. {
  211. int index = errorList.getSelectedIndex();
  212. Object selected = errorList.getSelectedValue();
  213. errorListModel.removeElementAt(index);
  214. errorListModel.insertElementAt(selected,index+1);
  215. errorList.setSelectedIndex(index+1);
  216. } */
  217. }
  218. } //}}}
  219. //{{{ ListHandler class
  220. class ListHandler implements ListSelectionListener
  221. {
  222. public void valueChanged(ListSelectionEvent evt)
  223. {
  224. updateButtons();
  225. }
  226. } //}}}
  227. //{{{ MouseHandler class
  228. class MouseHandler extends MouseAdapter
  229. {
  230. public void mouseClicked(MouseEvent evt)
  231. {
  232. if(evt.getClickCount() == 2)
  233. {
  234. updateButtons();
  235. }
  236. }
  237. } //}}}
  238. } //}}}