KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > FoldingToolTip


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.Rectangle JavaDoc;
26 import java.awt.Toolkit JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.border.LineBorder JavaDoc;
30 import javax.swing.text.AbstractDocument JavaDoc;
31 import javax.swing.text.JTextComponent JavaDoc;
32 import javax.swing.text.View JavaDoc;
33 import org.netbeans.editor.view.spi.LockView;
34
35 /**
36  * Component for displaying folded part of code in tooltip
37  *
38  * @author Martin Roskanin
39  */

40 public class FoldingToolTip extends JPanel JavaDoc {
41
42     View JavaDoc view;
43     EditorUI editorUI;
44     public static final int BORDER_WIDTH = 2;
45     
46     /** Creates a new instance of FoldingToolTip */
47     public FoldingToolTip(View JavaDoc view, EditorUI editorUI) {
48         this.view = view;
49         this.editorUI = editorUI;
50         Coloring defColoring = editorUI.getColoring(SettingsNames.DEFAULT_COLORING);
51         Color JavaDoc foreColor = (defColoring != null) ? defColoring.getForeColor() : null;
52         setBorder(new LineBorder JavaDoc((foreColor == null) ? Color.BLACK : foreColor));
53         setOpaque(true);
54     }
55     
56
57     public void setSize(Dimension JavaDoc d){
58         setSize(d.width, d.height);
59     }
60
61    /** Setting size of popup panel. The height and size is computed to fit the best place on the screen */
62     public void setSize(int width, int height){
63         int viewHeight = (int) view.getPreferredSpan(View.Y_AXIS);
64         int viewWidth = (int) view.getPreferredSpan(View.X_AXIS);
65         if (height<30) {
66             putClientProperty(PopupManager.Placement.class, null);
67         }else{
68             height = Math.min(height, viewHeight);
69         }
70         
71         height += 2*BORDER_WIDTH;
72         
73         width = Math.min(width, viewWidth);
74         
75         super.setSize(width,height);
76     }
77     
78     private void updateRenderingHints(Graphics JavaDoc g){
79         JTextComponent JavaDoc comp = editorUI.getComponent();
80         if (comp == null) return;
81         Object JavaDoc value = (Map JavaDoc)(Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints")); //NOI18N
82
//Don't bother seeing if the hints are explicitly turned off (if they
83
//even can be) as in EditorUI - it's a tooltip, desktop default is
84
//fine
85
if (value == null) {
86             value = Settings.getValue(Utilities.getKitClass(comp), SettingsNames.RENDERING_HINTS);
87         }
88         Map JavaDoc renderingHints = (value instanceof Map JavaDoc) ? (java.util.Map JavaDoc)value : null;
89
90         // Possibly apply the rendering hints
91
if (renderingHints != null) {
92             ((java.awt.Graphics2D JavaDoc)g).setRenderingHints(renderingHints);
93         }
94     }
95     
96     
97     protected void paintComponent(Graphics JavaDoc g) {
98
99         updateRenderingHints(g);
100         
101         Rectangle JavaDoc shape = new Rectangle JavaDoc(getSize());
102         Rectangle JavaDoc clip = g.getClipBounds();
103         
104         Coloring defaultColoring = editorUI.getColoring(SettingsNames.DEFAULT_COLORING);
105         if (defaultColoring!=null && defaultColoring.getBackColor()!=null){
106             g.setColor(defaultColoring.getBackColor());
107         }else{
108             g.setColor(SettingsDefaults.defaultBackColor);
109         }
110         g.fillRect(clip.x, clip.y, clip.width, clip.height);
111
112         g.translate(BORDER_WIDTH, BORDER_WIDTH);
113
114         JTextComponent JavaDoc component = editorUI.getComponent();
115         if (component == null) return;
116         int sideBarWidth = editorUI.getSideBarWidth();
117
118         GlyphGutter gg = editorUI.getGlyphGutter();
119         if (gg!=null){
120             View JavaDoc docView = null;
121             if (view.getViewCount() == 1){//lockview
122
docView = view.getView(0);
123             }
124             int y = 0;
125             if (docView!=null){
126                 AbstractDocument JavaDoc doc = (AbstractDocument JavaDoc)docView.getDocument();
127                 doc.readLock();
128                 try {
129                     LockView lockView = LockView.get(docView);
130                     if (lockView != null) {
131                         lockView.lock();
132                         try {
133                             for (int i = 0; i<docView.getViewCount(); i++ ){
134                                 gg.paintGutterForView(g, docView.getView(i), y);
135                                 y += editorUI.getLineHeight();
136                             }
137                         } finally {
138                             lockView.unlock();
139                         }
140                     }
141                 } finally {
142                     doc.readUnlock();
143                 }
144             }else{
145                 gg.paintGutterForView(g, view, 0);
146             }
147
148             g.translate(sideBarWidth,0);
149         }
150
151         view.paint(g, shape);
152
153         if (gg!=null){
154             g.translate(-sideBarWidth,0);
155         }
156
157         g.translate(-BORDER_WIDTH, -BORDER_WIDTH);
158
159         if (defaultColoring!=null && defaultColoring.getBackColor()!=null){
160             g.setColor(defaultColoring.getBackColor());
161         }else{
162             g.setColor(SettingsDefaults.defaultBackColor);
163         }
164         for (int i = 1; i<=BORDER_WIDTH; i++){
165             g.drawRect(clip.x+i,clip.y+i,clip.width-i*2-1,clip.height-i*2-1);
166         }
167     }
168
169     
170 }
171
Popular Tags