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(JTabbedPane, 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(JTabbedPane,
055: * org.jvnet.substance.tabbed.BaseTabCloseListener)
056: */
057: public class RegisterTabCloseChangeListener_SpecificMultiple extends
058: JFrame {
059: /**
060: * Creates the main frame for <code>this</code> sample.
061: */
062: public RegisterTabCloseChangeListener_SpecificMultiple() {
063: super ("Register tab close listener");
064:
065: this .setLayout(new BorderLayout());
066:
067: final JTabbedPane jtp = new JTabbedPane();
068: jtp.addTab("tab1", Check.getIcon("flag_sweden"), new JPanel());
069: jtp.addTab("tab2", Check.getIcon("flag_mexico"), new JPanel());
070: jtp.addTab("tab3", Check.getIcon("flag_hong_kong"),
071: new JPanel());
072:
073: jtp
074: .putClientProperty(
075: SubstanceLookAndFeel.TABBED_PANE_CLOSE_BUTTONS_PROPERTY,
076: Boolean.TRUE);
077:
078: // create a custom implementation of TabCloseCallback interface.
079: TabCloseCallback closeCallback = new TabCloseCallback() {
080: public TabCloseKind onAreaClick(JTabbedPane tabbedPane,
081: int tabIndex, MouseEvent mouseEvent) {
082: if (mouseEvent.getButton() != MouseEvent.BUTTON3)
083: return TabCloseKind.NONE;
084: if (mouseEvent.isShiftDown()) {
085: return TabCloseKind.ALL;
086: }
087: return TabCloseKind.THIS;
088: }
089:
090: public TabCloseKind onCloseButtonClick(
091: JTabbedPane tabbedPane, int tabIndex,
092: MouseEvent mouseEvent) {
093: if (mouseEvent.isAltDown()) {
094: return TabCloseKind.ALL_BUT_THIS;
095: }
096: if (mouseEvent.isShiftDown()) {
097: return TabCloseKind.ALL;
098: }
099: return TabCloseKind.THIS;
100: }
101:
102: public String getAreaTooltip(JTabbedPane tabbedPane,
103: int tabIndex) {
104: return null;
105: }
106:
107: public String getCloseButtonTooltip(JTabbedPane tabbedPane,
108: int tabIndex) {
109: StringBuffer result = new StringBuffer();
110: result.append("<html><body>");
111: result.append("Mouse click closes <b>"
112: + tabbedPane.getTitleAt(tabIndex) + "</b> tab");
113: result
114: .append("<br><b>Alt</b>-Mouse click closes all tabs but <b>"
115: + tabbedPane.getTitleAt(tabIndex)
116: + "</b> tab");
117: result
118: .append("<br><b>Shift</b>-Mouse click closes all tabs");
119: result.append("</body></html>");
120: return result.toString();
121: }
122: };
123:
124: // register the callback on the tabbed pane
125: jtp.putClientProperty(
126: SubstanceLookAndFeel.TABBED_PANE_CLOSE_CALLBACK,
127: closeCallback);
128:
129: // register tab close listener on the specific tabbed pane.
130: SubstanceLookAndFeel.registerTabCloseChangeListener(jtp,
131: new MultipleTabCloseListener() {
132: public void tabsClosing(JTabbedPane tabbedPane,
133: Set<Component> tabComponents) {
134: StringBuffer sb = new StringBuffer("Tab (s)");
135: String sep = " [";
136: for (Component comp : tabComponents) {
137: sb.append(sep);
138: sep = ", ";
139: sb.append(tabbedPane.getTitleAt(tabbedPane
140: .indexOfComponent(comp)));
141: }
142: sb.append("] closing");
143: System.out.println(sb.toString());
144: }
145:
146: public void tabsClosed(JTabbedPane tabbedPane,
147: Set<Component> tabComponents) {
148: System.out.println(tabComponents.size()
149: + " tab(s) closed");
150: }
151: });
152:
153: this .add(jtp, BorderLayout.CENTER);
154:
155: this .setSize(400, 200);
156: this .setLocationRelativeTo(null);
157: this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
158: }
159:
160: /**
161: * The main method for <code>this</code> sample. The arguments are
162: * ignored.
163: *
164: * @param args
165: * Ignored.
166: * @throws Exception
167: * If some exception occured. Note that there is no special
168: * treatment of exception conditions in <code>this</code>
169: * sample code.
170: */
171: public static void main(String[] args) throws Exception {
172: UIManager
173: .setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
174: JFrame.setDefaultLookAndFeelDecorated(true);
175: SwingUtilities.invokeLater(new Runnable() {
176: public void run() {
177: new RegisterTabCloseChangeListener_SpecificMultiple()
178: .setVisible(true);
179: }
180: });
181: }
182: }
|