~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to core/swing/tabcontrol/src/org/netbeans/swing/tabcontrol/plaf/WinClassicEditorTabDisplayerUI.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
/*
 
42
 * WinClassicEditorTabDisplayerUI.java
 
43
 *
 
44
 * Created on 09 December 2003, 16:53
 
45
 */
 
46
 
 
47
package org.netbeans.swing.tabcontrol.plaf;
 
48
 
 
49
import java.awt.Component;
 
50
import java.awt.Container;
 
51
import java.awt.Dimension;
 
52
import java.awt.FontMetrics;
 
53
import java.awt.GradientPaint;
 
54
import java.awt.Graphics;
 
55
import java.awt.Graphics2D;
 
56
import java.awt.Insets;
 
57
import java.awt.Rectangle;
 
58
import java.util.HashMap;
 
59
import java.util.Map;
 
60
import javax.swing.Icon;
 
61
import javax.swing.JComponent;
 
62
import javax.swing.UIManager;
 
63
import org.netbeans.swing.tabcontrol.TabDisplayer;
 
64
 
 
65
import javax.swing.plaf.ComponentUI;
 
66
 
 
67
/**
 
68
 * Windows classic impl of tabs ui
 
69
 *
 
70
 * @author Tim Boudreau
 
71
 */
 
72
public final class WinClassicEditorTabDisplayerUI extends BasicScrollingTabDisplayerUI {
 
73
    
 
74
    private static final Rectangle scratch5 = new Rectangle();
 
75
    private static Map<Integer, String[]> buttonIconPaths;
 
76
    
 
77
    private static boolean isGenericUI = !"Windows".equals( //NOI18N
 
78
        UIManager.getLookAndFeel().getID());
 
79
 
 
80
    /**
 
81
     * Creates a new instance of WinClassicEditorTabDisplayerUI
 
82
     */
 
83
    public WinClassicEditorTabDisplayerUI(TabDisplayer displayer) {
 
84
        super (displayer);
 
85
    }
 
86
    
 
87
    public static ComponentUI createUI(JComponent c) {
 
88
        return new WinClassicEditorTabDisplayerUI ((TabDisplayer) c);
 
89
    }
 
90
 
 
91
    public Rectangle getTabRect(int idx, Rectangle rect) {
 
92
        Rectangle r = super.getTabRect (idx, rect);
 
93
        //For win classic, take up the full space, even the insets, to match
 
94
        //earlier appearance
 
95
        r.y = 0;
 
96
        r.height = displayer.getHeight();
 
97
        return r;
 
98
    }  
 
99
 
 
100
    public void install() {
 
101
        super.install();
 
102
        if (!isGenericUI) {
 
103
            displayer.setBackground( UIManager.getColor("tab_unsel_fill") );
 
104
            displayer.setOpaque(true);
 
105
        }
 
106
    }
 
107
 
 
108
    public Dimension getPreferredSize(JComponent c) {
 
109
        int prefHeight = 28;
 
110
        Graphics g = BasicScrollingTabDisplayerUI.getOffscreenGraphics();
 
111
        if (g != null) {
 
112
            FontMetrics fm = g.getFontMetrics(displayer.getFont());
 
113
            Insets ins = getTabAreaInsets();
 
114
            prefHeight = fm.getHeight() + ins.top + ins.bottom + (isGenericUI ? 5 : 6);
 
115
        }
 
116
        return new Dimension(displayer.getWidth(), prefHeight);
 
117
    }
 
118
 
 
119
    private void genericPaintAfterTabs (Graphics g) {
 
120
        g.setColor (UIManager.getColor("controlShadow")); //NOI18N
 
121
        Insets ins = displayer.getInsets();
 
122
        Rectangle r = new Rectangle();
 
123
        getTabsVisibleArea(r);
 
124
        r.width = displayer.getWidth();
 
125
        int selEnd = 0;
 
126
        int last = getLastVisibleTab();
 
127
        if (last > -1) {
 
128
            getTabRect (last, scratch5);
 
129
            g.drawLine (scratch5.x + scratch5.width, displayer.getHeight() -1, 
 
130
                displayer.getWidth() - (ins.left + ins.right) - 4, 
 
131
                displayer.getHeight() - 1);
 
132
            g.drawLine (0, displayer.getHeight() - 2, 2, displayer.getHeight() -2);
 
133
            //TODO remove when a specific GTK l&f UI class is available
 
134
            if ("GTK".equals(UIManager.getLookAndFeel().getID())) {
 
135
                boolean sel = last == displayer.getSelectionModel().getSelectedIndex();
 
136
                //paint a fading shadow to match the view tabs
 
137
                int x = scratch5.x + scratch5.width;
 
138
                g.setColor (sel ? UIManager.getColor("controlShadow") :
 
139
                    ColorUtil.adjustTowards(g.getColor(), 20,
 
140
                    UIManager.getColor("control"))); //NOI18N
 
141
                g.drawLine (x, 
 
142
                    scratch5.y + 5, x,
 
143
                    scratch5.y + scratch5.height -2);
 
144
                g.setColor (ColorUtil.adjustTowards(g.getColor(), 20,
 
145
                    UIManager.getColor("control"))); //NOI18N
 
146
                g.drawLine (x + 1, 
 
147
                    scratch5.y + 6, x + 1,
 
148
                    scratch5.y + scratch5.height -2);
 
149
            }
 
150
            if ((tabState.getState(getFirstVisibleTab()) & TabState.CLIP_LEFT)
 
151
                !=0 && getFirstVisibleTab() != 
 
152
                displayer.getSelectionModel().getSelectedIndex()) {
 
153
                    //Draw a small gradient line continuing the left edge of
 
154
                    //the displayer up the left side of a left clipped tab
 
155
                GradientPaint gp = ColorUtil.getGradientPaint(
 
156
                    0, displayer.getHeight() / 2, UIManager.getColor("control"),
 
157
                    0, displayer.getHeight(), UIManager.getColor("controlShadow"));
 
158
                ((Graphics2D) g).setPaint(gp);
 
159
                g.drawLine (0, displayer.getHeight() / 2, 0, displayer.getHeight());
 
160
            } else {
 
161
                //Fill the small gap between the top of the content displayer
 
162
                //and the bottom of the tabs, caused by the tab area bottom inset
 
163
                g.setColor (UIManager.getColor("controlShadow"));
 
164
                g.drawLine (0, displayer.getHeight(), 0, displayer.getHeight() - 2);
 
165
            }
 
166
            if ((tabState.getState(getLastVisibleTab()) & TabState.CLIP_RIGHT) != 0
 
167
                && getLastVisibleTab() != 
 
168
                displayer.getSelectionModel().getSelectedIndex()) {
 
169
                GradientPaint gp = ColorUtil.getGradientPaint(
 
170
                    0, displayer.getHeight() / 2, UIManager.getColor("control"),
 
171
                    0, displayer.getHeight(), UIManager.getColor("controlShadow"));
 
172
                ((Graphics2D) g).setPaint(gp);
 
173
                getTabRect (getLastVisibleTab(), scratch5);
 
174
                g.drawLine (scratch5.x + scratch5.width, displayer.getHeight() / 2, 
 
175
                    scratch5.x + scratch5.width, displayer.getHeight());
 
176
            }
 
177
            
 
178
        } else {
 
179
            g.drawLine(r.x, displayer.getHeight() - ins.bottom, r.x + r.width - 4,
 
180
                       displayer.getHeight() - ins.bottom);
 
181
        }
 
182
    }
 
183
    
 
184
    protected void paintAfterTabs(Graphics g) {
 
185
        if (isGenericUI) {
 
186
            genericPaintAfterTabs(g);
 
187
            return;
 
188
        }
 
189
        Rectangle r = new Rectangle();
 
190
        getTabsVisibleArea(r);
 
191
        r.width = displayer.getWidth();
 
192
        g.setColor(displayer.isActive() ?
 
193
                   defaultRenderer.getSelectedActivatedBackground() :
 
194
                   defaultRenderer.getSelectedBackground());
 
195
 
 
196
        Insets ins = getTabAreaInsets();
 
197
        ins.bottom++;
 
198
        g.fillRect(r.x, r.y + r.height, r.x + r.width,
 
199
                   displayer.getHeight() - (r.y + r.height));
 
200
 
 
201
        g.setColor(UIManager.getColor("controlLtHighlight")); //NOI18N
 
202
 
 
203
        int selEnd = 0;
 
204
        int i = selectionModel.getSelectedIndex();
 
205
        if (i != -1) {
 
206
            getTabRect(i, scratch5);
 
207
            if (scratch5.width != 0) {
 
208
                if (r.x < scratch5.x) {
 
209
                    g.drawLine(r.x, displayer.getHeight() - ins.bottom,
 
210
                               scratch5.x - 1,
 
211
                               displayer.getHeight() - ins.bottom);
 
212
                }
 
213
                if (scratch5.x + scratch5.width < r.x + r.width) {
 
214
                    selEnd = scratch5.x + scratch5.width;
 
215
                    //If the last tab is not clipped, the final tab is one
 
216
                    //pixel smaller; we need to overwrite one pixel of the
 
217
                    //border or there will be a small stub sticking down
 
218
                    if (!scroll().isLastTabClipped()) {
 
219
                        selEnd--;
 
220
                    }
 
221
                    g.drawLine(selEnd, displayer.getHeight() - ins.bottom,
 
222
                               r.x + r.width,
 
223
                               displayer.getHeight() - ins.bottom);
 
224
                }
 
225
            }
 
226
            return;
 
227
        }
 
228
 
 
229
        g.drawLine(r.x, displayer.getHeight() - ins.bottom, r.x + r.width,
 
230
                   displayer.getHeight() - ins.bottom);
 
231
    }
 
232
 
 
233
    protected TabCellRenderer createDefaultRenderer() {
 
234
        return new WinClassicEditorTabCellRenderer();
 
235
    }
 
236
 
 
237
    private static void initIcons() {
 
238
        if( null == buttonIconPaths ) {
 
239
            buttonIconPaths = new HashMap<Integer, String[]>(7);
 
240
            
 
241
            //left button
 
242
            String[] iconPaths = new String[4];
 
243
            iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_scrollleft_enabled.png"; // NOI18N
 
244
            iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/win_scrollleft_disabled.png"; // NOI18N
 
245
            iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_scrollleft_rollover.png"; // NOI18N
 
246
            iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_scrollleft_pressed.png"; // NOI18N
 
247
            buttonIconPaths.put( TabControlButton.ID_SCROLL_LEFT_BUTTON, iconPaths );
 
248
            
 
249
            //right button
 
250
            iconPaths = new String[4];
 
251
            iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_scrollright_enabled.png"; // NOI18N
 
252
            iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/win_scrollright_disabled.png"; // NOI18N
 
253
            iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_scrollright_rollover.png"; // NOI18N
 
254
            iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_scrollright_pressed.png"; // NOI18N
 
255
            buttonIconPaths.put( TabControlButton.ID_SCROLL_RIGHT_BUTTON, iconPaths );
 
256
            
 
257
            //drop down button
 
258
            iconPaths = new String[4];
 
259
            iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_popup_enabled.png"; // NOI18N
 
260
            iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/win_popup_disabled.png"; // NOI18N
 
261
            iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_popup_rollover.png"; // NOI18N
 
262
            iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_popup_pressed.png"; // NOI18N
 
263
            buttonIconPaths.put( TabControlButton.ID_DROP_DOWN_BUTTON, iconPaths );
 
264
            
 
265
            //maximize/restore button
 
266
            iconPaths = new String[4];
 
267
            iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_maximize_enabled.png"; // NOI18N
 
268
            iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/win_maximize_disabled.png"; // NOI18N
 
269
            iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_maximize_rollover.png"; // NOI18N
 
270
            iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_maximize_pressed.png"; // NOI18N
 
271
            buttonIconPaths.put( TabControlButton.ID_MAXIMIZE_BUTTON, iconPaths );
 
272
            
 
273
            iconPaths = new String[4];
 
274
            iconPaths[TabControlButton.STATE_DEFAULT] = "org/netbeans/swing/tabcontrol/resources/win_restore_enabled.png"; // NOI18N
 
275
            iconPaths[TabControlButton.STATE_DISABLED] = "org/netbeans/swing/tabcontrol/resources/win_restore_disabled.png"; // NOI18N
 
276
            iconPaths[TabControlButton.STATE_ROLLOVER] = "org/netbeans/swing/tabcontrol/resources/win_restore_rollover.png"; // NOI18N
 
277
            iconPaths[TabControlButton.STATE_PRESSED] = "org/netbeans/swing/tabcontrol/resources/win_restore_pressed.png"; // NOI18N
 
278
            buttonIconPaths.put( TabControlButton.ID_RESTORE_BUTTON, iconPaths );
 
279
        }
 
280
    }
 
281
 
 
282
    public Icon getButtonIcon(int buttonId, int buttonState) {
 
283
        Icon res = null;
 
284
        initIcons();
 
285
        String[] paths = buttonIconPaths.get( buttonId );
 
286
        if( null != paths && buttonState >=0 && buttonState < paths.length ) {
 
287
            res = TabControlButtonFactory.getIcon( paths[buttonState] );
 
288
        }
 
289
        return res;
 
290
    }
 
291
    
 
292
    protected Rectangle getControlButtonsRectangle( Container parent ) {
 
293
        Component c = getControlButtons();
 
294
        return new Rectangle( parent.getWidth()-c.getWidth()-4, 4, c.getWidth(), c.getHeight() );
 
295
    }
 
296
 
 
297
    public Insets getTabAreaInsets() {
 
298
        Insets retValue = super.getTabAreaInsets();
 
299
        retValue.right += 4;
 
300
        return retValue;
 
301
    }
 
302
}