001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.lib.client.components.tabbedpane;
015:
016: import org.jetbrains.annotations.NotNull;
017: import org.jetbrains.annotations.Nullable;
018: import org.pentaho.reportdesigner.lib.client.components.Category;
019:
020: import javax.swing.*;
021: import java.awt.*;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.beans.PropertyChangeEvent;
025: import java.beans.PropertyChangeListener;
026: import java.util.ArrayList;
027: import java.util.HashMap;
028: import java.util.Set;
029:
030: /**
031: * User: Martin
032: * Date: 12.03.2005
033: * Time: 13:57:59
034: */
035: public class ButtonTabbedPane extends JPanel {
036:
037: public static void main(@NotNull
038: String[] args) {
039: UIManager.put("swing.boldMetal", Boolean.FALSE);//NON-NLS
040:
041: try {
042: UIManager.setLookAndFeel(UIManager
043: .getInstalledLookAndFeels()[0].getClassName());
044: } catch (Throwable e) {
045: //noinspection CallToPrintStackTrace
046: e.printStackTrace();
047: }
048:
049: JFrame frame = new JFrame("ButtonTabbedPane");//NON-NLS
050:
051: ButtonTabbedPane buttonTabbedPane = new ButtonTabbedPane(true);
052: final Category<?> category1 = new Category<JComponent>("books",
053: new ImageIcon(ButtonTabbedPane.class
054: .getResource("/res/icons/book-icon.gif")),
055: new ImageIcon(ButtonTabbedPane.class
056: .getResource("/res/icons/book-icon.gif")),
057: "Bücher", new JScrollPane(new JTree()));//NON-NLS
058: final Category<?> category2 = new Category<JComponent>(
059: "books1", new ImageIcon(ButtonTabbedPane.class
060: .getResource("/res/icons/book-icon.gif")),
061: new ImageIcon(ButtonTabbedPane.class
062: .getResource("/res/icons/book-icon.gif")),
063: "Bücher1", new JScrollPane(new JTree()));//NON-NLS
064: Category<?> category3 = new Category<JComponent>("books2",
065: new ImageIcon(ButtonTabbedPane.class
066: .getResource("/res/icons/book-icon.gif")),
067: new ImageIcon(ButtonTabbedPane.class
068: .getResource("/res/icons/book-icon.gif")),
069: "Bücher2", new JScrollPane(new JTree()));//NON-NLS
070: Category<?> category4 = new Category<JComponent>("books3",
071: new ImageIcon(ButtonTabbedPane.class
072: .getResource("/res/icons/book-icon.gif")),
073: new ImageIcon(ButtonTabbedPane.class
074: .getResource("/res/icons/book-icon.gif")),
075: "Bücher3", new JScrollPane(new JTree()));//NON-NLS
076: Category<?> category5 = new Category<JComponent>("dvd",
077: new ImageIcon(ButtonTabbedPane.class
078: .getResource("/res/icons/dvd-icon.gif")),
079: new ImageIcon(ButtonTabbedPane.class
080: .getResource("/res/icons/dvd-icon.gif")),
081: "DVD", new JScrollPane(new JLabel("blub")));//NON-NLS
082:
083: JButton testButton = new JButton("Test");//NON-NLS
084: testButton.addActionListener(new ActionListener() {
085: public void actionPerformed(@NotNull
086: ActionEvent e) {
087: category1.setTitle("Hugo");//NON-NLS
088: category2
089: .setIconBig(new ImageIcon(
090: ButtonTabbedPane.class
091: .getResource("/res/icons/dvd-icon.gif")));//NON-NLS
092: }
093: });
094: Category<?> category6 = new Category<JComponent>("music",
095: new ImageIcon(ButtonTabbedPane.class
096: .getResource("/res/icons/music-icon.gif")),
097: new ImageIcon(ButtonTabbedPane.class
098: .getResource("/res/icons/music-icon.gif")),
099: "Musik", new JScrollPane(testButton));//NON-NLS
100:
101: buttonTabbedPane.addCard(category1);
102: buttonTabbedPane.addCard(category2);
103: buttonTabbedPane.addCard(category3);
104: buttonTabbedPane.addCard(category4);
105: buttonTabbedPane.addCard(category5);
106: buttonTabbedPane.addCard(category6);
107:
108: frame.getContentPane().setLayout(new BorderLayout());
109: frame.getContentPane().add(buttonTabbedPane,
110: BorderLayout.CENTER);
111: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
112: frame.pack();
113: frame.setVisible(true);
114: }
115:
116: @NotNull
117: private ArrayList<Category<?>> categories;
118: @NotNull
119: private HashMap<Object, JToggleButton> buttonMap;
120: @NotNull
121: private JPanel leftPanel;
122: @NotNull
123: private JScrollPane scrollPane;
124: @NotNull
125: private JPanel buttonPanel;
126: @NotNull
127: private JPanel buttonHelperPanel;
128: @NotNull
129: private JPanel cardPanel;
130:
131: @NotNull
132: private CardLayout cardLayout;
133:
134: public ButtonTabbedPane(boolean border) {
135: setLayout(new BorderLayout(0, 0));
136: setOpaque(false);
137:
138: categories = new ArrayList<Category<?>>();
139:
140: buttonMap = new HashMap<Object, JToggleButton>();
141: buttonPanel = new JPanel(new GridLayout(0, 1));
142: buttonPanel.setOpaque(false);
143:
144: buttonHelperPanel = new ScrollablePanel(new BorderLayout());
145: buttonHelperPanel.setOpaque(true);
146:
147: cardLayout = new CardLayout();
148: cardPanel = new JPanel(cardLayout);
149:
150: buttonHelperPanel.add(buttonPanel, BorderLayout.NORTH);
151:
152: JPanel helperPanel = new JPanel(new BorderLayout());
153: scrollPane = new JScrollPane(buttonHelperPanel);
154: if (!border) {
155: scrollPane.setBorder(BorderFactory.createEmptyBorder());
156: }
157: leftPanel = new JPanel(new BorderLayout());
158: leftPanel.add(scrollPane, BorderLayout.CENTER);
159: helperPanel.add(leftPanel, BorderLayout.WEST);
160: helperPanel.add(Box.createHorizontalStrut(5),
161: BorderLayout.CENTER);
162: add(helperPanel, BorderLayout.WEST);
163:
164: add(cardPanel, BorderLayout.CENTER);
165:
166: cardPanel.add(new JPanel(), "EMPTY_PANEL");//NON-NLS
167: }
168:
169: public void addCard(@NotNull
170: final Category<?> buttonCategory) {
171: if (buttonMap.containsKey(buttonCategory.getKey().toString())) {
172: throw new IllegalArgumentException("a category with key='"
173: + buttonCategory.getKey().toString()
174: + "' is already registered");
175: }
176:
177: categories.add(buttonCategory);
178: cardPanel.add(buttonCategory.getMainComponent(), buttonCategory
179: .getKey().toString());
180:
181: final HoverToggleButton toggleButton = new HoverToggleButton(
182: buttonCategory.getTitle(), buttonCategory.getIconBig());
183: toggleButton.setHorizontalTextPosition(JToggleButton.CENTER);
184: toggleButton.setVerticalTextPosition(JToggleButton.BOTTOM);
185:
186: toggleButton.addActionListener(new ActionListener() {
187: public void actionPerformed(@NotNull
188: ActionEvent e) {
189: setSelectedComponent(buttonCategory.getKey());
190: }
191: });
192:
193: buttonCategory
194: .addPropertyChangeListener(new PropertyChangeListener() {
195: public void propertyChange(@NotNull
196: PropertyChangeEvent evt) {
197: if ("title".equals(evt.getPropertyName()))//NON-NLS
198: {
199: toggleButton.setText(buttonCategory
200: .getTitle());
201: } else if ("iconBig".equals(evt
202: .getPropertyName()))//NON-NLS
203: {
204: toggleButton.setIcon(buttonCategory
205: .getIconBig());
206: } else if ("mainComponent".equals(evt
207: .getPropertyName()))//NON-NLS
208: {
209: cardPanel.remove((Component) evt
210: .getOldValue());
211: cardPanel.add(buttonCategory
212: .getMainComponent());
213:
214: cardPanel.revalidate();
215: cardPanel.repaint();
216: }
217: }
218: });
219:
220: buttonMap.put(buttonCategory.getKey(), toggleButton);
221:
222: JPanel helperPanel = new JPanel(new BorderLayout());
223: helperPanel.setOpaque(false);
224: helperPanel.setBorder(BorderFactory.createEmptyBorder(2, 4, 2,
225: 4));
226: helperPanel.add(toggleButton, BorderLayout.CENTER);
227: buttonPanel.add(helperPanel);
228: }
229:
230: public void setSelectedComponent(@NotNull
231: Object key) {
232: Set<Object> keys = buttonMap.keySet();
233: for (Object k : keys) {
234: buttonMap.get(k).setSelected(false);
235: }
236:
237: JToggleButton jToggleButton = buttonMap.get(key);
238: jToggleButton.setSelected(true);
239: jToggleButton.requestFocusInWindow();
240: cardLayout.show(cardPanel, key.toString());
241: }
242:
243: public void setSelectedIndex(int index) {
244: setSelectedComponent(categories.get(index).getKey());
245: }
246:
247: @Nullable
248: public Category<?> getSelectedCategory() {
249: for (Category<?> category : categories) {
250: if (buttonMap.get(category.getKey()).isSelected()) {
251: return category;
252: }
253: }
254: return null;
255: }
256:
257: public int getSelectedIndex() {
258: return categories.indexOf(getSelectedCategory());
259: }
260:
261: @NotNull
262: public JPanel getButtonPanel() {
263: return leftPanel;
264: }
265:
266: @Nullable
267: public JComponent getSelectedComponent() {
268: Category<?> category = getSelectedCategory();
269: if (category != null) {
270: return category.getMainComponent();
271: }
272: return null;
273: }
274:
275: public void setSelectionTabWidth(int width) {
276: leftPanel.setPreferredSize(new Dimension(width, leftPanel
277: .getPreferredSize().height));
278: }
279:
280: private class ScrollablePanel extends JPanel implements Scrollable {
281: private ScrollablePanel(@NotNull
282: LayoutManager layout) {
283: super (layout);
284: }
285:
286: @NotNull
287: public Dimension getPreferredScrollableViewportSize() {
288: return buttonHelperPanel.getPreferredSize();
289: }
290:
291: public int getScrollableUnitIncrement(@NotNull
292: Rectangle visibleRect, int orientation, int direction) {
293: return 20;
294: }
295:
296: public int getScrollableBlockIncrement(@NotNull
297: Rectangle visibleRect, int orientation, int direction) {
298: return 20;
299: }
300:
301: public boolean getScrollableTracksViewportWidth() {
302: return true;
303: }
304:
305: public boolean getScrollableTracksViewportHeight() {
306: return false;
307: }
308: }
309: }
|