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