KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > ShowWhitespaceAction


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.compare.internal;
12
13 import java.util.*;
14
15 import org.eclipse.jface.text.WhitespaceCharacterPainter;
16 import org.eclipse.ui.texteditor.AbstractTextEditor;
17
18 public class ShowWhitespaceAction extends TextEditorPropertyAction {
19
20     private Map fPainters;
21     private boolean isWhitespaceShowing;
22     
23     public ShowWhitespaceAction(MergeSourceViewer[] viewers) {
24         super(CompareMessages.ShowWhitespaceAction_0, viewers, AbstractTextEditor.PREFERENCE_SHOW_WHITESPACE_CHARACTERS);
25     }
26     
27     protected void toggleState(boolean checked) {
28         if (checked) {
29             showWhitespace();
30         } else {
31             hideWhitespace();
32         }
33     }
34     
35     private synchronized Map getPainters() {
36         if (fPainters == null)
37             fPainters = new HashMap();
38         return fPainters;
39     }
40     
41     private void showWhitespace() {
42         if (isWhitespaceShowing)
43             return;
44         try {
45             Map painters = getPainters();
46             MergeSourceViewer[] viewers = getViewers();
47             for (int i = 0; i < viewers.length; i++) {
48                 MergeSourceViewer viewer = viewers[i];
49                 WhitespaceCharacterPainter painter= new WhitespaceCharacterPainter(viewer);
50                 viewer.addPainter(painter);
51                 painters.put(viewer, painter);
52             }
53         } finally {
54             isWhitespaceShowing = true;
55         }
56     }
57     
58     private void hideWhitespace() {
59         Map painters = getPainters();
60         for (Iterator iterator = painters.keySet().iterator(); iterator.hasNext();) {
61             MergeSourceViewer viewer = (MergeSourceViewer) iterator.next();
62             WhitespaceCharacterPainter painter = (WhitespaceCharacterPainter)painters.get(viewer);
63             if (painter != null) {
64                 viewer.removePainter(painter);
65                 painter.deactivate(true);
66             }
67         }
68         painters.clear();
69         isWhitespaceShowing = false;
70     }
71         
72 }
73
Popular Tags