PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/antlr-3.4/gunit/src/main/java/org/antlr/gunit/swingui/RuleListController.java

https://bitbucket.org/cyanogenmod/android_external_antlr
Java | 160 lines | 98 code | 29 blank | 33 comment | 8 complexity | 52f57f1d9df3251b8840d86334264d2a MD5 | raw file
  1. /*
  2. [The "BSD licence"]
  3. Copyright (c) 2009 Shaoting Cai
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. The name of the author may not be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. package org.antlr.gunit.swingui;
  27. import javax.swing.event.ListDataListener;
  28. import org.antlr.gunit.swingui.model.Rule;
  29. import org.antlr.gunit.swingui.ImageFactory;
  30. import java.awt.Color;
  31. import java.awt.Component;
  32. import java.awt.Dimension;
  33. import java.awt.event.MouseAdapter;
  34. import java.awt.event.MouseEvent;
  35. import java.util.List;
  36. import javax.swing.BorderFactory;
  37. import javax.swing.DefaultListModel;
  38. import javax.swing.JLabel;
  39. import javax.swing.JList;
  40. import javax.swing.JScrollPane;
  41. import javax.swing.ListCellRenderer;
  42. import javax.swing.ListModel;
  43. import javax.swing.ListSelectionModel;
  44. import javax.swing.event.ListSelectionListener;
  45. import org.antlr.gunit.swingui.model.TestSuite;
  46. public class RuleListController implements IController {
  47. /* Sub-controls */
  48. private final JList list = new JList();
  49. private final JScrollPane scroll = new JScrollPane( list,
  50. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  51. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  52. /* Model */
  53. private ListModel model = null;
  54. private TestSuite testSuite = null;
  55. public RuleListController() {
  56. this.initComponents();
  57. }
  58. public JScrollPane getView() {
  59. return scroll;
  60. }
  61. private void setTestSuite(TestSuite newTestSuite) {
  62. testSuite = newTestSuite;
  63. model = new RuleListModel();
  64. list.setModel(model);
  65. }
  66. public void initialize(TestSuite ts) {
  67. setTestSuite(ts);
  68. if(model.getSize() > 0) list.setSelectedIndex(0);
  69. list.updateUI();
  70. }
  71. /**
  72. * Initialize view.
  73. */
  74. private void initComponents() {
  75. scroll.setViewportBorder(BorderFactory.createEtchedBorder());
  76. scroll.setBorder(BorderFactory.createTitledBorder(
  77. BorderFactory.createEmptyBorder(), "Rules"));
  78. scroll.setOpaque(false);
  79. list.setOpaque(false);
  80. list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
  81. list.setLayoutOrientation(JList.VERTICAL);
  82. list.setCellRenderer(new RuleListItemRenderer());
  83. }
  84. public void setListSelectionListener(ListSelectionListener l) {
  85. this.list.addListSelectionListener(l);
  86. }
  87. public Object getModel() {
  88. return model;
  89. }
  90. /* ITEM RENDERER */
  91. private class RuleListItemRenderer extends JLabel implements ListCellRenderer{
  92. public RuleListItemRenderer() {
  93. this.setPreferredSize(new Dimension(50, 18));
  94. }
  95. public Component getListCellRendererComponent(
  96. JList list, Object value, int index,
  97. boolean isSelected, boolean hasFocus) {
  98. if(value instanceof Rule) {
  99. final Rule item = (Rule) value;
  100. setText(item.toString());
  101. setForeground(list.getForeground());
  102. setIcon(item.getNotEmpty() ? ImageFactory.getSingleton().FAV16 : null);
  103. if(list.getSelectedValue() == item ) {
  104. setBackground(Color.LIGHT_GRAY);
  105. setOpaque(true);
  106. } else {
  107. setOpaque(false);
  108. }
  109. } else {
  110. this.setText("Error!");
  111. }
  112. return this;
  113. }
  114. }
  115. private class RuleListModel implements ListModel {
  116. public RuleListModel() {
  117. if(testSuite == null)
  118. throw new NullPointerException("Null test suite");
  119. }
  120. public int getSize() {
  121. return testSuite.getRuleCount();
  122. }
  123. public Object getElementAt(int index) {
  124. return testSuite.getRule(index);
  125. }
  126. public void addListDataListener(ListDataListener l) {}
  127. public void removeListDataListener(ListDataListener l) {}
  128. }
  129. }