1 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 ; 27 import java.awt.Image ; 28 import javax.swing.DefaultListCellRenderer ; 29 import javax.swing.ImageIcon ; 30 import javax.swing.JLabel ; 31 import javax.swing.JList ; 32 import javax.swing.ListSelectionModel ; 33 import javax.swing.event.ListSelectionEvent ; 34 import javax.swing.event.ListSelectionListener ; 35 import org.openide.util.Utilities; 36 37 42 public final class CssRuleNavigationView extends JList implements CssMetaModel.CssModelListener, ListSelectionListener { 43 44 private static final String CSS_RULE_ICON = "org/netbeans/modules/css/resources/css_rule.png"; 45 private static ImageIcon ruleIcon = null; 46 private CssMetaModel currentCssMetaModel = CssMetaModel.getInstance(); 48 49 public CssRuleNavigationView() { 50 ruleIcon = new ImageIcon (Utilities.loadImage(CSS_RULE_ICON)); 51 initComponents(); 52 setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 53 updateRules(); 54 currentCssMetaModel.addModelListener(this); 55 } 56 57 public void valueChanged(ListSelectionEvent evt) { 59 JList list = (JList )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 [0]); 98 if (currentCssMetaModel != null) { 99 currentCssMetaModel.removeModelListener(this); 100 currentCssMetaModel = null; 101 } 102 } 103 104 public void ruleAdded(CssModelChangeEvent evt){ 106 updateRules(); 107 } 108 109 public void ruleRemoved(CssModelChangeEvent evt){ 110 } 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 { 123 124 public CustomListCellRenderer() { 125 setHorizontalAlignment(JLabel.LEFT); 126 setVerticalAlignment(JLabel.CENTER); 127 } 128 129 public Component getListCellRendererComponent(JList list, Object 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 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 |