001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.welcome.ui;
043:
044: import java.awt.BorderLayout;
045: import java.awt.CardLayout;
046: import java.awt.Cursor;
047: import java.awt.Graphics;
048: import java.awt.GridBagConstraints;
049: import java.awt.GridBagLayout;
050: import java.awt.GridLayout;
051: import java.awt.Insets;
052: import java.awt.event.ActionEvent;
053: import java.awt.event.ActionListener;
054: import java.awt.event.FocusEvent;
055: import java.awt.event.KeyEvent;
056: import java.awt.event.MouseEvent;
057: import javax.swing.JComponent;
058: import javax.swing.JPanel;
059: import org.netbeans.modules.welcome.content.Constants;
060: import java.awt.Image;
061: import java.awt.event.FocusListener;
062: import java.awt.event.KeyListener;
063: import java.awt.event.MouseListener;
064: import javax.swing.BorderFactory;
065: import javax.swing.JLabel;
066: import javax.swing.JScrollPane;
067: import org.netbeans.modules.welcome.WelcomeOptions;
068: import org.netbeans.modules.welcome.content.Utils;
069: import org.openide.util.Utilities;
070:
071: /**
072: *
073: * @author S. Aubrecht
074: */
075: class Tabs extends JPanel implements Constants {
076:
077: private JScrollPane leftComp;
078: private JScrollPane rightComp;
079: private JComponent leftTab;
080: private JComponent rightTab;
081: private JPanel tabContent;
082:
083: private Image imgStripWest;
084: private Image imgStripCenter;
085: private Image imgStripEast;
086:
087: public Tabs(String leftTabTitle, JComponent leftTab,
088: String rightTabTitle, final JComponent rightTab) {
089:
090: super (new BorderLayout());
091: setOpaque(false);
092:
093: this .leftTab = leftTab;
094: this .rightTab = rightTab;
095:
096: this .imgStripCenter = Utilities
097: .loadImage(IMAGE_STRIP_BOTTOM_CENTER);
098: this .imgStripWest = Utilities
099: .loadImage(IMAGE_STRIP_BOTTOM_WEST);
100: this .imgStripEast = Utilities
101: .loadImage(IMAGE_STRIP_BOTTOM_EAST);
102:
103: final Tab leftButton = new Tab(leftTabTitle, true);
104: final Tab rightButton = new Tab(rightTabTitle, false);
105:
106: ActionListener al = new ActionListener() {
107: public void actionPerformed(ActionEvent e) {
108: boolean isLeftTabSelected = e.getSource() == leftButton;
109: leftButton.setSelected(isLeftTabSelected);
110: rightButton.setSelected(!isLeftTabSelected);
111: switchTab(isLeftTabSelected);
112: WelcomeOptions.getDefault().setLastActiveTab(
113: isLeftTabSelected ? 0 : 1);
114: }
115: };
116:
117: leftButton.addActionListener(al);
118: rightButton.addActionListener(al);
119:
120: JPanel buttons = new JPanel(new GridLayout(1, 2));
121: buttons.setOpaque(true);
122: buttons.add(leftButton);
123: buttons.add(rightButton);
124: buttons.setBackground(Utils
125: .getColor(COLOR_TAB_UNSEL_BACKGROUND));
126:
127: add(buttons, BorderLayout.NORTH);
128:
129: tabContent = new JPanel(new CardLayout());
130: tabContent.setOpaque(false);
131:
132: add(tabContent, BorderLayout.CENTER);
133: int activeTabIndex = WelcomeOptions.getDefault()
134: .getLastActiveTab();
135: boolean selectLeftTab = activeTabIndex <= 0;
136: if (WelcomeOptions.getDefault().isSecondStart()
137: && activeTabIndex < 0) {
138: selectLeftTab = false;
139: WelcomeOptions.getDefault().setLastActiveTab(1);
140: }
141: leftButton.setSelected(selectLeftTab);
142: rightButton.setSelected(!selectLeftTab);
143: switchTab(selectLeftTab);
144: }
145:
146: private void switchTab(boolean showLeftTab) {
147: JScrollPane compToShow = showLeftTab ? leftComp : rightComp;
148: JScrollPane compToHide = showLeftTab ? rightComp : leftComp;
149:
150: if (null == compToShow) {
151: compToShow = new JScrollPane(showLeftTab ? leftTab
152: : rightTab);
153: compToShow.setOpaque(false);
154: compToShow.getViewport().setOpaque(false);
155: compToShow.setBorder(BorderFactory.createEmptyBorder());
156:
157: if (showLeftTab) {
158: leftComp = compToShow;
159: tabContent.add(leftComp, "left"); //NOI18N
160: } else {
161: rightComp = compToShow;
162: tabContent.add(rightComp, "right"); //NOI18N
163: }
164: }
165:
166: if (null != compToHide)
167: compToHide.setVisible(false);
168:
169: compToShow.setVisible(true);
170: compToShow.requestFocusInWindow();
171:
172: invalidate();
173: revalidate();
174: repaint();
175: }
176:
177: private static class Tab extends JPanel {
178: private boolean isLeftTab;
179: private Image imgUnselBottom;
180: private Image imgSelLeft;
181: private Image imgSelUpperLeft;
182: private Image imgSelLowerLeft;
183: private Image imgSelRight;
184: private Image imgSelUpperRight;
185: private Image imgSelLowerRight;
186: private boolean isSelected = false;
187: private ActionListener actionListener;
188: private JLabel lbl;
189:
190: public Tab(String title, boolean isLeftTab) {
191: super (new GridBagLayout());
192: this .isLeftTab = isLeftTab;
193: imgUnselBottom = Utilities.loadImage(IMAGE_TAB_UNSEL);
194: if (isLeftTab) {
195: imgSelLeft = Utilities.loadImage(IMAGE_TAB_SEL_RIGHT);
196: imgSelUpperLeft = Utilities
197: .loadImage(IMAGE_TAB_SEL_UPPER_RIGHT);
198: imgSelLowerLeft = Utilities
199: .loadImage(IMAGE_TAB_SEL_LOWER_RIGHT);
200: } else {
201: imgSelRight = Utilities.loadImage(IMAGE_TAB_SEL_LEFT);
202: imgSelUpperRight = Utilities
203: .loadImage(IMAGE_TAB_SEL_UPPER_LEFT);
204: imgSelLowerRight = Utilities
205: .loadImage(IMAGE_TAB_SEL_LOWER_LEFT);
206: }
207: lbl = new JLabel(title);
208: lbl.setOpaque(false);
209: add(lbl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
210: GridBagConstraints.CENTER, GridBagConstraints.BOTH,
211: new Insets(5, 5, 4, 0), 0, 0));
212: lbl.setFont(TAB_FONT);
213: lbl.setForeground(Utils
214: .getColor(isSelected ? COLOR_TAB_SEL_FOREGROUND
215: : COLOR_TAB_UNSEL_FOREGROUND));
216: lbl.setHorizontalAlignment(JLabel.CENTER);
217: lbl.setFocusable(true);
218:
219: lbl.addKeyListener(new KeyListener() {
220:
221: public void keyTyped(KeyEvent e) {
222: }
223:
224: public void keyPressed(KeyEvent e) {
225: if (e.getKeyCode() == KeyEvent.VK_SPACE
226: || e.getKeyCode() == KeyEvent.VK_ENTER) {
227: setSelected(!isSelected);
228: if (null != actionListener) {
229: actionListener
230: .actionPerformed(new ActionEvent(
231: Tab.this , 0, "clicked"));
232: }
233: }
234: }
235:
236: public void keyReleased(KeyEvent e) {
237: }
238: });
239:
240: addMouseListener(new MouseListener() {
241: public void mouseClicked(MouseEvent e) {
242: setSelected(!isSelected);
243: if (null != actionListener) {
244: actionListener.actionPerformed(new ActionEvent(
245: Tab.this , 0, "clicked"));
246: }
247: }
248:
249: public void mousePressed(MouseEvent e) {
250: }
251:
252: public void mouseReleased(MouseEvent e) {
253: }
254:
255: public void mouseEntered(MouseEvent e) {
256: if (!isSelected) {
257: setCursor(Cursor
258: .getPredefinedCursor(Cursor.HAND_CURSOR));
259: lbl.setForeground(Utils
260: .getColor(MOUSE_OVER_TAB_COLOR));
261: } else {
262: setCursor(Cursor.getDefaultCursor());
263: // lbl.setForeground( Utils.getColor( COLOR_TAB_UNSEL_FOREGROUND ) );
264: }
265: }
266:
267: public void mouseExited(MouseEvent e) {
268: setCursor(Cursor.getDefaultCursor());
269: lbl
270: .setForeground(Utils
271: .getColor(isSelected ? COLOR_TAB_SEL_FOREGROUND
272: : COLOR_TAB_UNSEL_FOREGROUND));
273: }
274: });
275:
276: lbl.addFocusListener(new FocusListener() {
277:
278: public void focusGained(FocusEvent e) {
279: lbl.setForeground(Utils
280: .getColor(MOUSE_OVER_LINK_COLOR));
281: }
282:
283: public void focusLost(FocusEvent e) {
284: lbl
285: .setForeground(Utils
286: .getColor(isSelected ? COLOR_TAB_SEL_FOREGROUND
287: : COLOR_TAB_UNSEL_FOREGROUND));
288: }
289: });
290: }
291:
292: public void addActionListener(ActionListener l) {
293: assert null == actionListener;
294: this .actionListener = l;
295: }
296:
297: public void setSelected(boolean sel) {
298: this .isSelected = sel;
299: lbl
300: .setForeground(Utils
301: .getColor(lbl.isFocusOwner() ? MOUSE_OVER_LINK_COLOR
302: : isSelected ? COLOR_TAB_SEL_FOREGROUND
303: : COLOR_TAB_UNSEL_FOREGROUND));
304:
305: lbl.setFocusable(!sel);
306: repaint();
307: }
308:
309: @Override
310: protected void paintComponent(Graphics g) {
311: super .paintComponent(g);
312: g.setColor(Utils
313: .getColor(isSelected ? COLOR_TAB_SEL_BACKGROUND
314: : COLOR_TAB_UNSEL_BACKGROUND));
315: int width = getWidth();
316: int height = getHeight();
317:
318: g.fillRect(0, 0, width, height);
319:
320: if (isSelected) {
321: if (isLeftTab) {
322:
323: g.setColor(Utils
324: .getColor(COLOR_TAB_UNSEL_BACKGROUND));
325: g.fillRect(width - imgSelUpperLeft.getWidth(null),
326: 0, width, height);
327:
328: g
329: .setColor(Utils
330: .getColor(COLOR_TAB_SEL_BACKGROUND));
331: int rightImageWidth = imgSelLeft.getWidth(null);
332: g.drawImage(imgSelUpperLeft, width
333: - imgSelUpperLeft.getWidth(null), 0, null);
334: for (int i = 0; i < (height - imgSelUpperLeft
335: .getHeight(null)/*-imgSelLowerLeft.getHeight(null)*/); i++) {
336: g.drawImage(imgSelLeft, width - rightImageWidth
337: - 1, imgSelUpperLeft.getHeight(null)
338: + i, null);
339: }
340: g.fillRect(width - imgSelUpperLeft.getWidth(null),
341: imgSelUpperLeft.getHeight(null),
342: imgSelUpperLeft.getWidth(null)
343: - rightImageWidth, height
344: - imgSelUpperLeft.getHeight(null));
345: // g.drawImage(imgSelLowerLeft, width-imgSelLowerLeft.getWidth(null)-1, height-imgSelLowerLeft.getHeight(null), null);
346:
347: } else {
348:
349: g.setColor(Utils
350: .getColor(COLOR_TAB_UNSEL_BACKGROUND));
351: g.fillRect(0, 0, imgSelUpperRight.getWidth(null),
352: height);
353:
354: g
355: .setColor(Utils
356: .getColor(COLOR_TAB_SEL_BACKGROUND));
357: g.drawImage(imgSelUpperRight, 0, 0, null);
358: for (int i = 0; i < (height - imgSelUpperRight
359: .getHeight(null)/*-imgSelLowerRight.getHeight(null)*/); i++) {
360: g.drawImage(imgSelRight, 1, imgSelUpperRight
361: .getHeight(null)
362: + i, null);
363: }
364: g.fillRect(imgSelRight.getWidth(null),
365: imgSelUpperRight.getHeight(null),
366: imgSelUpperRight.getWidth(null)
367: - imgSelRight.getWidth(null),
368: height - imgSelUpperRight.getHeight(null));
369: // g.drawImage(imgSelLowerRight, 0, height-imgSelLowerRight.getHeight(null), null);
370: // g.fillRect(imgSelLowerRight.getWidth(null), height-imgSelLowerRight.getHeight(null),
371: // imgSelUpperRight.getWidth(null)-imgSelLowerRight.getWidth(null), imgSelLowerRight.getHeight(null));
372: }
373: } else {
374: int imgWidth = imgUnselBottom.getWidth(null);
375: int imgHeight = imgUnselBottom.getHeight(null);
376: for (int i = 0; i < width / imgWidth + 1; i++) {
377: g.drawImage(imgUnselBottom, i * imgWidth, height
378: - imgHeight, null);
379: }
380: }
381: }
382:
383: @Override
384: protected void paintBorder(Graphics g) {
385: }
386: }
387: }
|