001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package test.samples.substance.api;
031:
032: import java.awt.BorderLayout;
033: import java.awt.Component;
034: import java.awt.event.MouseEvent;
035: import java.util.Set;
036:
037: import javax.swing.*;
038:
039: import org.jvnet.substance.SubstanceLookAndFeel;
040: import org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel;
041: import org.jvnet.substance.tabbed.MultipleTabCloseListener;
042: import org.jvnet.substance.tabbed.TabCloseCallback;
043: import org.jvnet.substance.utils.SubstanceConstants.TabCloseKind;
044:
045: import test.Check;
046:
047: /**
048: * Test application that shows the use of the
049: * {@link SubstanceLookAndFeel#registerTabCloseChangeListener(org.jvnet.substance.tabbed.BaseTabCloseListener)}
050: * API with registering a tab close listener that listens on multiple tab
051: * closing on a specific tabbed pane.
052: *
053: * @author Kirill Grouchnikov
054: * @see SubstanceLookAndFeel#registerTabCloseChangeListener(org.jvnet.substance.tabbed.BaseTabCloseListener)
055: */
056: public class RegisterTabCloseChangeListener_GeneralMultiple extends
057: JFrame {
058: /**
059: * Creates the main frame for <code>this</code> sample.
060: */
061: public RegisterTabCloseChangeListener_GeneralMultiple() {
062: super ("Register tab close listener");
063:
064: this .setLayout(new BorderLayout());
065:
066: final JTabbedPane jtp = new JTabbedPane();
067: jtp.addTab("tab1", Check.getIcon("flag_sweden"), new JPanel());
068: jtp.addTab("tab2", Check.getIcon("flag_mexico"), new JPanel());
069: jtp.addTab("tab3", Check.getIcon("flag_hong_kong"),
070: new JPanel());
071:
072: jtp
073: .putClientProperty(
074: SubstanceLookAndFeel.TABBED_PANE_CLOSE_BUTTONS_PROPERTY,
075: Boolean.TRUE);
076:
077: // create a custom implementation of TabCloseCallback interface.
078: TabCloseCallback closeCallback = new TabCloseCallback() {
079: public TabCloseKind onAreaClick(JTabbedPane tabbedPane,
080: int tabIndex, MouseEvent mouseEvent) {
081: if (mouseEvent.getButton() != MouseEvent.BUTTON3)
082: return TabCloseKind.NONE;
083: if (mouseEvent.isShiftDown()) {
084: return TabCloseKind.ALL;
085: }
086: return TabCloseKind.THIS;
087: }
088:
089: public TabCloseKind onCloseButtonClick(
090: JTabbedPane tabbedPane, int tabIndex,
091: MouseEvent mouseEvent) {
092: if (mouseEvent.isAltDown()) {
093: return TabCloseKind.ALL_BUT_THIS;
094: }
095: if (mouseEvent.isShiftDown()) {
096: return TabCloseKind.ALL;
097: }
098: return TabCloseKind.THIS;
099: }
100:
101: public String getAreaTooltip(JTabbedPane tabbedPane,
102: int tabIndex) {
103: return null;
104: }
105:
106: public String getCloseButtonTooltip(JTabbedPane tabbedPane,
107: int tabIndex) {
108: StringBuffer result = new StringBuffer();
109: result.append("<html><body>");
110: result.append("Mouse click closes <b>"
111: + tabbedPane.getTitleAt(tabIndex) + "</b> tab");
112: result
113: .append("<br><b>Alt</b>-Mouse click closes all tabs but <b>"
114: + tabbedPane.getTitleAt(tabIndex)
115: + "</b> tab");
116: result
117: .append("<br><b>Shift</b>-Mouse click closes all tabs");
118: result.append("</body></html>");
119: return result.toString();
120: }
121: };
122:
123: // register the callback on the tabbed pane
124: jtp.putClientProperty(
125: SubstanceLookAndFeel.TABBED_PANE_CLOSE_CALLBACK,
126: closeCallback);
127:
128: // register tab close listener on all tabbed panes.
129: SubstanceLookAndFeel
130: .registerTabCloseChangeListener(new MultipleTabCloseListener() {
131: public void tabsClosing(JTabbedPane tabbedPane,
132: Set<Component> tabComponents) {
133: StringBuffer sb = new StringBuffer("Tab (s)");
134: String sep = " [";
135: for (Component comp : tabComponents) {
136: sb.append(sep);
137: sep = ", ";
138: sb.append(tabbedPane.getTitleAt(tabbedPane
139: .indexOfComponent(comp)));
140: }
141: sb.append("] closing");
142: System.out.println(sb.toString());
143: }
144:
145: public void tabsClosed(JTabbedPane tabbedPane,
146: Set<Component> tabComponents) {
147: System.out.println(tabComponents.size()
148: + " tab(s) closed");
149: }
150: });
151:
152: this .add(jtp, BorderLayout.CENTER);
153:
154: this .setSize(400, 200);
155: this .setLocationRelativeTo(null);
156: this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
157: }
158:
159: /**
160: * The main method for <code>this</code> sample. The arguments are
161: * ignored.
162: *
163: * @param args
164: * Ignored.
165: * @throws Exception
166: * If some exception occured. Note that there is no special
167: * treatment of exception conditions in <code>this</code>
168: * sample code.
169: */
170: public static void main(String[] args) throws Exception {
171: UIManager
172: .setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
173: JFrame.setDefaultLookAndFeelDecorated(true);
174: SwingUtilities.invokeLater(new Runnable() {
175: public void run() {
176: new RegisterTabCloseChangeListener_GeneralMultiple()
177: .setVisible(true);
178: }
179: });
180: }
181: }
|