~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to ide/welcome/src/org/netbeans/modules/welcome/ui/Tabs.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-2007 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
package org.netbeans.modules.welcome.ui;
 
43
 
 
44
import java.awt.BorderLayout;
 
45
import java.awt.CardLayout;
 
46
import java.awt.Cursor;
 
47
import java.awt.Graphics;
 
48
import java.awt.GridBagConstraints;
 
49
import java.awt.GridBagLayout;
 
50
import java.awt.GridLayout;
 
51
import java.awt.Insets;
 
52
import java.awt.event.ActionEvent;
 
53
import java.awt.event.ActionListener;
 
54
import java.awt.event.MouseEvent;
 
55
import javax.swing.JComponent;
 
56
import javax.swing.JPanel;
 
57
import org.netbeans.modules.welcome.content.Constants;
 
58
import java.awt.Image;
 
59
import java.awt.event.MouseListener;
 
60
import javax.swing.BorderFactory;
 
61
import javax.swing.JLabel;
 
62
import javax.swing.JScrollPane;
 
63
import org.netbeans.modules.welcome.WelcomeOptions;
 
64
import org.netbeans.modules.welcome.content.Utils;
 
65
import org.openide.util.Utilities;
 
66
 
 
67
/**
 
68
 *
 
69
 * @author S. Aubrecht
 
70
 */
 
71
class Tabs extends JPanel implements Constants {
 
72
 
 
73
    private JScrollPane leftComp;
 
74
    private JScrollPane rightComp;
 
75
    private JComponent leftTab;
 
76
    private JComponent rightTab;
 
77
    private JPanel tabContent;
 
78
    
 
79
    private Image imgStripWest;
 
80
    private Image imgStripCenter;
 
81
    private Image imgStripEast;
 
82
    
 
83
    public Tabs( String leftTabTitle, JComponent leftTab, 
 
84
            String rightTabTitle, final JComponent rightTab) {
 
85
        
 
86
        super( new BorderLayout() );
 
87
        setOpaque( false );
 
88
 
 
89
        this.leftTab = leftTab;
 
90
        this.rightTab = rightTab;
 
91
        
 
92
        this.imgStripCenter = Utilities.loadImage( IMAGE_STRIP_BOTTOM_CENTER );
 
93
        this.imgStripWest = Utilities.loadImage( IMAGE_STRIP_BOTTOM_WEST );
 
94
        this.imgStripEast = Utilities.loadImage( IMAGE_STRIP_BOTTOM_EAST );
 
95
        
 
96
        final Tab leftButton = new Tab( leftTabTitle, true );
 
97
        final Tab rightButton = new Tab( rightTabTitle, false );
 
98
        
 
99
        ActionListener al = new ActionListener() {
 
100
            public void actionPerformed(ActionEvent e) {
 
101
                boolean isLeftTabSelected = e.getSource() == leftButton;
 
102
                leftButton.setSelected( isLeftTabSelected );
 
103
                rightButton.setSelected( !isLeftTabSelected );
 
104
                switchTab( isLeftTabSelected );
 
105
                WelcomeOptions.getDefault().setLastActiveTab( isLeftTabSelected ? 0 : 1 );
 
106
            }
 
107
        };
 
108
        
 
109
        leftButton.addActionListener( al );
 
110
        rightButton.addActionListener( al );
 
111
        
 
112
        JPanel buttons = new JPanel( new GridLayout(1,2) );
 
113
        buttons.setOpaque(true);
 
114
        buttons.add( leftButton );
 
115
        buttons.add( rightButton );
 
116
        buttons.setBackground( Utils.getColor(COLOR_TAB_UNSEL_BACKGROUND) );
 
117
        
 
118
        add( buttons, BorderLayout.NORTH );
 
119
        
 
120
        tabContent = new JPanel( new CardLayout() );
 
121
        tabContent.setOpaque( false );
 
122
 
 
123
        add( tabContent, BorderLayout.CENTER );
 
124
        int activeTabIndex = WelcomeOptions.getDefault().getLastActiveTab();
 
125
        boolean selectLeftTab = activeTabIndex <= 0;
 
126
        if( WelcomeOptions.getDefault().isSecondStart() && activeTabIndex < 0 ) {
 
127
            selectLeftTab = false;
 
128
            WelcomeOptions.getDefault().setLastActiveTab( 1 );
 
129
        }
 
130
        leftButton.setSelected( selectLeftTab );
 
131
        rightButton.setSelected( !selectLeftTab );
 
132
        switchTab( selectLeftTab );
 
133
    }
 
134
 
 
135
    private void switchTab( boolean showLeftTab ) {
 
136
        JScrollPane compToShow = showLeftTab ? leftComp : rightComp;
 
137
        JScrollPane compToHide = showLeftTab ? rightComp : leftComp;
 
138
 
 
139
        if( null == compToShow ) {
 
140
            compToShow = new JScrollPane( showLeftTab ? leftTab : rightTab );
 
141
            compToShow.setOpaque( false );
 
142
            compToShow.getViewport().setOpaque( false );
 
143
            compToShow.setBorder( BorderFactory.createEmptyBorder() );
 
144
 
 
145
            if( showLeftTab ) {
 
146
                leftComp = compToShow;
 
147
                tabContent.add( leftComp, "left" ); //NOI18N
 
148
            } else {
 
149
                rightComp = compToShow;
 
150
                tabContent.add( rightComp, "right" ); //NOI18N
 
151
            }
 
152
        }
 
153
 
 
154
        if( null != compToHide )
 
155
            compToHide.setVisible( false );
 
156
        
 
157
        compToShow.setVisible( true );
 
158
        
 
159
        invalidate();
 
160
        revalidate();
 
161
        repaint();
 
162
    }
 
163
    
 
164
    private static class Tab extends JPanel {
 
165
        private boolean isLeftTab;
 
166
        private Image imgUnselBottom;
 
167
        private Image imgSelLeft;
 
168
        private Image imgSelUpperLeft;
 
169
        private Image imgSelLowerLeft;
 
170
        private Image imgSelRight;
 
171
        private Image imgSelUpperRight;
 
172
        private Image imgSelLowerRight;
 
173
        private boolean isSelected = false;
 
174
        private ActionListener actionListener;
 
175
        private JLabel lbl;
 
176
        
 
177
        
 
178
        public Tab( String title, boolean isLeftTab ) {
 
179
            super( new GridBagLayout() );
 
180
            this.isLeftTab = isLeftTab;
 
181
            imgUnselBottom = Utilities.loadImage( IMAGE_TAB_UNSEL );
 
182
            if( isLeftTab ) {
 
183
                imgSelLeft = Utilities.loadImage( IMAGE_TAB_SEL_RIGHT );
 
184
                imgSelUpperLeft = Utilities.loadImage( IMAGE_TAB_SEL_UPPER_RIGHT );
 
185
                imgSelLowerLeft = Utilities.loadImage( IMAGE_TAB_SEL_LOWER_RIGHT );
 
186
            } else {
 
187
                imgSelRight = Utilities.loadImage( IMAGE_TAB_SEL_LEFT );
 
188
                imgSelUpperRight = Utilities.loadImage( IMAGE_TAB_SEL_UPPER_LEFT );
 
189
                imgSelLowerRight = Utilities.loadImage( IMAGE_TAB_SEL_LOWER_LEFT );
 
190
            }
 
191
            lbl = new JLabel(title);
 
192
            lbl.setOpaque( false );
 
193
            add( lbl, new GridBagConstraints(0,0,1,1,1.0,1.0,
 
194
                    GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(5,5,4,0),0,0) );
 
195
            lbl.setFont( TAB_FONT );
 
196
            lbl.setForeground( Utils.getColor( isSelected
 
197
                    ? COLOR_TAB_SEL_FOREGROUND 
 
198
                    : COLOR_TAB_UNSEL_FOREGROUND ) );
 
199
            lbl.setHorizontalAlignment( JLabel.CENTER );
 
200
            
 
201
            addMouseListener( new MouseListener() {
 
202
                public void mouseClicked(MouseEvent e) {
 
203
                    setSelected( !isSelected );
 
204
                    if( null != actionListener ) {
 
205
                        actionListener.actionPerformed( new ActionEvent( Tab.this, 0, "clicked") );
 
206
                    }
 
207
                }
 
208
 
 
209
                public void mousePressed(MouseEvent e) {
 
210
                }
 
211
 
 
212
                public void mouseReleased(MouseEvent e) {
 
213
                }
 
214
 
 
215
                public void mouseEntered(MouseEvent e) {
 
216
                    if( !isSelected ) {
 
217
                        setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ) );
 
218
                        lbl.setForeground( Utils.getColor( MOUSE_OVER_TAB_COLOR  )  );
 
219
                    } else {
 
220
                        setCursor( Cursor.getDefaultCursor() );
 
221
//                        lbl.setForeground( Utils.getColor( COLOR_TAB_UNSEL_FOREGROUND ) );
 
222
                    }
 
223
                }
 
224
 
 
225
                public void mouseExited(MouseEvent e) {
 
226
                    setCursor( Cursor.getDefaultCursor() );
 
227
                    lbl.setForeground( Utils.getColor( isSelected
 
228
                            ? COLOR_TAB_SEL_FOREGROUND 
 
229
                            : COLOR_TAB_UNSEL_FOREGROUND ) );
 
230
                }
 
231
            });
 
232
        }
 
233
        
 
234
        public void addActionListener( ActionListener l ) {
 
235
            assert null == actionListener;
 
236
            this.actionListener = l;
 
237
        }
 
238
        
 
239
        public void setSelected( boolean sel ) {
 
240
            this.isSelected = sel;
 
241
            lbl.setForeground( Utils.getColor( isSelected
 
242
                    ? COLOR_TAB_SEL_FOREGROUND 
 
243
                    : COLOR_TAB_UNSEL_FOREGROUND ) );
 
244
            repaint();
 
245
        }
 
246
 
 
247
        @Override
 
248
        protected void paintComponent(Graphics g) {
 
249
            super.paintComponent(g);
 
250
            g.setColor( Utils.getColor( isSelected ? COLOR_TAB_SEL_BACKGROUND : COLOR_TAB_UNSEL_BACKGROUND ) );
 
251
            int width = getWidth();
 
252
            int height = getHeight();
 
253
            
 
254
            g.fillRect( 0, 0, width, height );
 
255
            
 
256
            if( isSelected ) {
 
257
                if( isLeftTab ) {
 
258
                    
 
259
                    g.setColor( Utils.getColor( COLOR_TAB_UNSEL_BACKGROUND ) );
 
260
                    g.fillRect( width-imgSelUpperLeft.getWidth(null), 0, width, height );
 
261
                    
 
262
                    g.setColor( Utils.getColor( COLOR_TAB_SEL_BACKGROUND ) );
 
263
                    int rightImageWidth = imgSelLeft.getWidth(null);
 
264
                    g.drawImage(imgSelUpperLeft, width-imgSelUpperLeft.getWidth(null), 0, null);
 
265
                    for( int i=0; i<(height-imgSelUpperLeft.getHeight(null)/*-imgSelLowerLeft.getHeight(null)*/); i++ ) {
 
266
                        g.drawImage( imgSelLeft, width-rightImageWidth-1, imgSelUpperLeft.getHeight(null)+i, null);
 
267
                    }
 
268
                    g.fillRect(width-imgSelUpperLeft.getWidth(null), imgSelUpperLeft.getHeight(null), imgSelUpperLeft.getWidth(null)-rightImageWidth, height-imgSelUpperLeft.getHeight(null));
 
269
//                    g.drawImage(imgSelLowerLeft, width-imgSelLowerLeft.getWidth(null)-1, height-imgSelLowerLeft.getHeight(null), null);
 
270
                    
 
271
                } else {
 
272
                    
 
273
                    g.setColor( Utils.getColor( COLOR_TAB_UNSEL_BACKGROUND ) );
 
274
                    g.fillRect( 0, 0, imgSelUpperRight.getWidth(null), height );
 
275
                    
 
276
                    g.setColor( Utils.getColor( COLOR_TAB_SEL_BACKGROUND ) );
 
277
                    g.drawImage(imgSelUpperRight, 0, 0, null);
 
278
                    for( int i=0; i<(height-imgSelUpperRight.getHeight(null)/*-imgSelLowerRight.getHeight(null)*/); i++ ) {
 
279
                        g.drawImage( imgSelRight, 1, imgSelUpperRight.getHeight(null)+i, null);
 
280
                    }
 
281
                    g.fillRect(imgSelRight.getWidth(null), imgSelUpperRight.getHeight(null), 
 
282
                            imgSelUpperRight.getWidth(null)-imgSelRight.getWidth(null), height-imgSelUpperRight.getHeight(null));
 
283
//                    g.drawImage(imgSelLowerRight, 0, height-imgSelLowerRight.getHeight(null), null);
 
284
//                    g.fillRect(imgSelLowerRight.getWidth(null), height-imgSelLowerRight.getHeight(null), 
 
285
//                            imgSelUpperRight.getWidth(null)-imgSelLowerRight.getWidth(null), imgSelLowerRight.getHeight(null));
 
286
                }
 
287
            } else {
 
288
                int imgWidth = imgUnselBottom.getWidth(null);
 
289
                int imgHeight = imgUnselBottom.getHeight(null);
 
290
                for( int i=0; i<width/imgWidth+1; i++ ) {
 
291
                    g.drawImage( imgUnselBottom, i*imgWidth, height-imgHeight, null );
 
292
                }
 
293
            }
 
294
        }
 
295
 
 
296
        @Override
 
297
        protected void paintBorder(Graphics g) {
 
298
        }
 
299
    }
 
300
}