KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > actions > CssRuleNavigationView


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.css.actions;
21
22 import org.netbeans.modules.css.loader.CssDataObject;
23 import org.netbeans.modules.css.visual.model.CssMetaModel;
24 import org.netbeans.modules.css.visual.model.CssMetaModel.CssModelChangeEvent;
25 import org.netbeans.modules.css.visual.model.CssRuleData;
26 import java.awt.Component JavaDoc;
27 import java.awt.Image JavaDoc;
28 import javax.swing.DefaultListCellRenderer JavaDoc;
29 import javax.swing.ImageIcon JavaDoc;
30 import javax.swing.JLabel JavaDoc;
31 import javax.swing.JList JavaDoc;
32 import javax.swing.ListSelectionModel JavaDoc;
33 import javax.swing.event.ListSelectionEvent JavaDoc;
34 import javax.swing.event.ListSelectionListener JavaDoc;
35 import org.openide.util.Utilities;
36
37 /**
38  * CSS rule navigation list that appear in the Navigator window
39  * @author Winston Prakash
40  * @version 1.0
41  */

42 public final class CssRuleNavigationView extends JList JavaDoc implements CssMetaModel.CssModelListener, ListSelectionListener JavaDoc{
43
44     private static final String JavaDoc CSS_RULE_ICON = "org/netbeans/modules/css/resources/css_rule.png";
45     private static ImageIcon JavaDoc ruleIcon = null;
46     // xxx Hack fix it - for adding and removing listeners
47
private CssMetaModel currentCssMetaModel = CssMetaModel.getInstance();
48
49     public CssRuleNavigationView() {
50         ruleIcon = new ImageIcon JavaDoc(Utilities.loadImage(CSS_RULE_ICON));
51         initComponents();
52         setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
53         updateRules();
54         currentCssMetaModel.addModelListener(this);
55     }
56
57     // Implementation of the method of ListSelectionListener
58
public void valueChanged(ListSelectionEvent JavaDoc evt) {
59         JList JavaDoc list = (JList JavaDoc)evt.getSource();
60         CssRuleData cssRule = (CssRuleData)list.getSelectedValue();
61         if(cssRule != null){
62             int docLen = cssRule.getTextComponent().getDocument().getLength();
63             if(cssRule.getOffset()+2 < docLen){
64                 cssRule.getTextComponent().getCaret().setDot(cssRule.getOffset()+2);
65             }else{
66                 cssRule.getTextComponent().getCaret().setDot(docLen - 1);
67             }
68         }
69     }
70
71     private void initComponents(){
72         setCellRenderer(new CustomListCellRenderer());
73     }
74
75     public void updateRules(){
76         removeListSelectionListener(this);
77         CssRuleData[] rules = CssMetaModel.getInstance().getRuleSet();
78         setListData(rules);
79         setSelectedValue(CssMetaModel.getInstance().getSelectedRule(), true);
80         validate();
81         repaint();
82         addListSelectionListener(this);
83     }
84
85     public void updateModel(CssDataObject cssDataObject) {
86         if (currentCssMetaModel != null) {
87             currentCssMetaModel.removeModelListener(this);
88         }
89         currentCssMetaModel = CssMetaModel.getInstance(cssDataObject);
90         if (currentCssMetaModel != null) {
91             currentCssMetaModel.addModelListener(this);
92         }
93         updateRules();
94     }
95
96     public void clearRules() {
97         setListData(new Object JavaDoc[0]);
98         if (currentCssMetaModel != null) {
99             currentCssMetaModel.removeModelListener(this);
100             currentCssMetaModel = null;
101         }
102     }
103
104     //Imlementation of CssMetaModel.CssModelListener
105
public void ruleAdded(CssModelChangeEvent evt){
106         updateRules();
107     }
108
109     public void ruleRemoved(CssModelChangeEvent evt){
110         //removeItem(evt.getRule());
111
}
112     public void selectionChanged(CssModelChangeEvent evt){
113         removeListSelectionListener(this);
114         setSelectedValue(evt.getRule(), true);
115         addListSelectionListener(this);
116     }
117
118     public void viewChanged(CssModelChangeEvent evt) {
119     }
120
121
122     private static class CustomListCellRenderer extends DefaultListCellRenderer JavaDoc {
123
124         public CustomListCellRenderer() {
125             setHorizontalAlignment(JLabel.LEFT);
126             setVerticalAlignment(JLabel.CENTER);
127         }
128         
129         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
130             
131             if (isSelected) {
132                 setBackground(list.getSelectionBackground());
133                 setForeground(list.getSelectionForeground());
134             } else {
135                 setBackground(list.getBackground());
136                 setForeground(list.getForeground());
137             }
138             
139             String JavaDoc name = null;
140             boolean expimp = false;
141             if(value instanceof CssRuleData){
142                 setIcon(ruleIcon);
143                 setText(((CssRuleData)value).getRule());
144             }
145             
146             return this;
147         }
148     }
149 }
150
Popular Tags