1 19 20 package org.netbeans.modules.diff.builtin.visualizer.editable; 21 22 import org.netbeans.api.diff.Difference; 23 import org.netbeans.spi.editor.highlighting.HighlightsContainer; 24 import org.netbeans.spi.editor.highlighting.HighlightsSequence; 25 import org.netbeans.spi.editor.highlighting.HighlightsChangeListener; 26 import org.netbeans.spi.editor.highlighting.HighlightsChangeEvent; 27 28 import javax.swing.*; 29 import javax.swing.text.AttributeSet ; 30 import javax.swing.text.DefaultEditorKit ; 31 import javax.swing.text.EditorKit ; 32 import javax.accessibility.AccessibleContext ; 33 import java.awt.*; 34 import java.util.*; 35 import java.util.List ; 36 37 42 class DiffContentPanel extends JComponent implements HighlightsContainer { 43 44 private final EditableDiffView master; 45 private final boolean isFirst; 46 47 private final DecoratedEditorPane editorPane; 48 private final JScrollPane scrollPane; 49 private final LineNumbersActionsBar linesActions; 50 private final JScrollPane actionsScrollPane; 51 52 private Difference[] currentDiff; 53 54 public DiffContentPanel(EditableDiffView master, boolean isFirst) { 55 this.master = master; 56 this.isFirst = isFirst; 57 58 setLayout(new BorderLayout()); 59 60 editorPane = new DecoratedEditorPane(this); 61 editorPane.setEditable(false); 62 scrollPane = new JScrollPane(editorPane); 63 add(scrollPane); 64 65 linesActions = new LineNumbersActionsBar(this, master.isActionsEnabled()); 66 actionsScrollPane = new JScrollPane(linesActions); 67 actionsScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 68 actionsScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); 69 actionsScrollPane.setBorder(null); 70 add(actionsScrollPane, isFirst ? BorderLayout.LINE_END : BorderLayout.LINE_START); 71 72 editorPane.putClientProperty(DiffHighlightsLayerFactory.HIGHLITING_LAYER_ID, this); 73 } 74 75 void initActions() { 76 ActionMap paneMap = editorPane.getActionMap(); 78 ActionMap am = getActionMap(); 79 am.setParent(paneMap); 80 paneMap.put(DefaultEditorKit.cutAction, getAction(DefaultEditorKit.cutAction)); 81 paneMap.put(DefaultEditorKit.copyAction, getAction(DefaultEditorKit.copyAction)); 82 paneMap.put("delete", getAction(DefaultEditorKit.deleteNextCharAction)); paneMap.put(DefaultEditorKit.pasteAction, getAction(DefaultEditorKit.pasteAction)); 84 } 85 86 private Action getAction(String key) { 87 if (key == null) { 88 return null; 89 } 90 91 EditorKit kit = editorPane.getEditorKit(); 93 94 if (kit == null) { 96 return null; 97 } 98 99 Action[] actions = kit.getActions(); 100 101 for (int i = 0; i < actions.length; i++) { 102 if (key.equals(actions[i].getValue(Action.NAME))) { 103 return actions[i]; 104 } 105 } 106 107 return null; 108 } 109 110 LineNumbersActionsBar getLinesActions() { 111 return linesActions; 112 } 113 114 public JScrollPane getActionsScrollPane() { 115 return actionsScrollPane; 116 } 117 118 public JScrollPane getScrollPane() { 119 return scrollPane; 120 } 121 122 public Difference[] getCurrentDiff() { 123 return currentDiff; 124 } 125 126 public boolean isFirst() { 127 return isFirst; 128 } 129 130 public void setCurrentDiff(Difference[] currentDiff) { 131 this.currentDiff = currentDiff; 132 editorPane.setDifferences(currentDiff); 133 linesActions.onDiffSetChanged(); 134 fireHilitingChanged(); 135 } 137 138 public Dimension getPreferredSize() { 139 Dimension d = super.getPreferredSize(); 140 Container parent = getParent(); 141 if (parent instanceof JViewport) { 142 if (parent.getWidth() > d.width) { 143 d = new Dimension(parent.getWidth(), d.height); 144 } 145 } 146 return d; 147 } 148 149 public DecoratedEditorPane getEditorPane() { 150 return editorPane; 151 } 152 153 public AccessibleContext getAccessibleContext() { 154 return editorPane.getAccessibleContext(); 155 } 156 157 public EditableDiffView getMaster() { 158 return master; 159 } 160 161 163 HighlightsContainer getHighlightsContainer() { 164 return this; 165 } 166 167 public HighlightsSequence getHighlights(int start, int end) { 168 return new DiffHighlightsSequence(start, end); 169 } 170 171 private final List <HighlightsChangeListener> listeners = new ArrayList<HighlightsChangeListener>(1); 172 173 public void addHighlightsChangeListener(HighlightsChangeListener listener) { 174 synchronized(listeners) { 175 listeners.add(listener); 176 } 177 } 178 179 public void removeHighlightsChangeListener(HighlightsChangeListener listener) { 180 synchronized(listeners) { 181 listeners.remove(listener); 182 } 183 } 184 185 private void fireHilitingChanged() { 186 synchronized(listeners) { 187 for (HighlightsChangeListener listener : listeners) { 188 listener.highlightChanged(new HighlightsChangeEvent(this, 0, Integer.MAX_VALUE)); 189 } 190 } 191 } 192 193 void onUISettingsChanged() { 194 editorPane.repaint(); 195 linesActions.onUISettingsChanged(); 196 actionsScrollPane.revalidate(); 197 actionsScrollPane.repaint(); 198 revalidate(); 199 repaint(); 200 } 201 202 205 private class DiffHighlightsSequence implements HighlightsSequence { 206 207 private final int endOffset; 208 private final int startOffset; 209 210 private int currentHiliteIndex = -1; 211 private DiffViewManager.HighLight [] hilites; 212 213 public DiffHighlightsSequence(int start, int end) { 214 this.startOffset = start; 215 this.endOffset = end; 216 lookupHilites(); 217 } 218 219 private void lookupHilites() { 220 List <DiffViewManager.HighLight> list = new ArrayList<DiffViewManager.HighLight>(); 221 DiffViewManager.HighLight[] allHilites = isFirst ? master.getManager().getFirstHighlights() : master.getManager().getSecondHighlights(); 222 for (DiffViewManager.HighLight hilite : allHilites) { 223 if (hilite.getEndOffset() < startOffset) continue; 224 if (hilite.getStartOffset() > endOffset) break; 225 list.add(hilite); 226 } 227 hilites = list.toArray(new DiffViewManager.HighLight[list.size()]); 228 } 229 230 public boolean moveNext() { 231 if (currentHiliteIndex >= hilites.length - 1) return false; 232 currentHiliteIndex++; 233 return true; 234 } 235 236 public int getStartOffset() { 237 return hilites[currentHiliteIndex].getStartOffset(); 238 } 239 240 public int getEndOffset() { 241 return hilites[currentHiliteIndex].getEndOffset(); 242 } 243 244 public AttributeSet getAttributes() { 245 return hilites[currentHiliteIndex].getAttrs(); 246 } 247 } 248 } 249 | Popular Tags |