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 javax.swing.event.ListSelectionEvent;
022: import javax.swing.event.ListSelectionListener;
023: import java.awt.*;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.beans.PropertyChangeEvent;
027: import java.beans.PropertyChangeListener;
028: import java.util.HashMap;
029:
030: /**
031: * User: Martin
032: * Date: 12.03.2005
033: * Time: 13:57:59
034: */
035: public class ListTabbedPane extends JPanel {
036: @NotNull
037: private static final String EMPTY_PANEL = "EMPTY_PANEL";
038:
039: public static void main(@NotNull
040: String[] args) {
041: UIManager.put("swing.boldMetal", Boolean.FALSE);//NON-NLS
042:
043: try {
044: UIManager.setLookAndFeel(UIManager
045: .getInstalledLookAndFeels()[0].getClassName());
046: } catch (Throwable e) {
047: //noinspection CallToPrintStackTrace
048: e.printStackTrace();
049: }
050:
051: JFrame frame = new JFrame("ButtonTabbedPane");//NON-NLS
052:
053: ListTabbedPane listTabbedPane = new ListTabbedPane(true);
054: final Category<?> category1 = new Category<JComponent>("books",
055: new ImageIcon(ListTabbedPane.class
056: .getResource("/res/icons/book-icon.gif")),
057: new ImageIcon(ListTabbedPane.class
058: .getResource("/res/icons/book-icon.gif")),
059: "1. Bücher", new JScrollPane(new JTree()));//NON-NLS
060: final Category<?> category2 = new Category<JComponent>(
061: "books1", new ImageIcon(ListTabbedPane.class
062: .getResource("/res/icons/book-icon.gif")),
063: new ImageIcon(ListTabbedPane.class
064: .getResource("/res/icons/book-icon.gif")),
065: "2. Bücher1", new JScrollPane(new JTree()));//NON-NLS
066: Category<?> category3 = new Category<JComponent>("books2",
067: new ImageIcon(ListTabbedPane.class
068: .getResource("/res/icons/book-icon.gif")),
069: new ImageIcon(ListTabbedPane.class
070: .getResource("/res/icons/book-icon.gif")),
071: "3. Bücher2", new JScrollPane(new JTree()));//NON-NLS
072: Category<?> category4 = new Category<JComponent>("books3",
073: new ImageIcon(ListTabbedPane.class
074: .getResource("/res/icons/book-icon.gif")),
075: new ImageIcon(ListTabbedPane.class
076: .getResource("/res/icons/book-icon.gif")),
077: "4. Bücher3", new JScrollPane(new JTree()));//NON-NLS
078: Category<?> category5 = new Category<JComponent>("dvd",
079: new ImageIcon(ListTabbedPane.class
080: .getResource("/res/icons/dvd-icon.gif")),
081: new ImageIcon(ListTabbedPane.class
082: .getResource("/res/icons/dvd-icon.gif")),
083: "5. DVD", new JScrollPane(new JLabel("blub")));//NON-NLS
084:
085: JButton testButton = new JButton("Test");//NON-NLS
086: testButton.addActionListener(new ActionListener() {
087: public void actionPerformed(@NotNull
088: ActionEvent e) {
089: category1.setTitle("Hugo");//NON-NLS
090: category2.setIconBig(new ImageIcon(ListTabbedPane.class
091: .getResource("/res/icons/dvd-icon.gif")));//NON-NLS
092: }
093: });
094: Category<?> category6 = new Category<JComponent>("music",
095: new ImageIcon(ListTabbedPane.class
096: .getResource("/res/icons/music-icon.gif")),
097: new ImageIcon(ListTabbedPane.class
098: .getResource("/res/icons/music-icon.gif")),
099: "6. Musik", new JScrollPane(testButton));//NON-NLS
100:
101: listTabbedPane.addCard(category1);
102: listTabbedPane.addCard(category2);
103: listTabbedPane.addCard(category3);
104: listTabbedPane.addCard(category4);
105: listTabbedPane.addCard(category5);
106: listTabbedPane.addCard(category6);
107:
108: frame.getContentPane().setLayout(new BorderLayout());
109: frame.getContentPane().add(listTabbedPane, BorderLayout.CENTER);
110: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
111: frame.pack();
112: frame.setVisible(true);
113: }
114:
115: @NotNull
116: private HashMap<Object, Category<?>> hashMap;
117: @NotNull
118: private JList list;
119: @NotNull
120: private ButtonTabbedPaneListModel buttonTabbedPaneListModel;
121: @NotNull
122: private JPanel buttonPanel;
123: @NotNull
124: private JPanel cardPanel;
125:
126: @NotNull
127: private CardLayout cardLayout;
128:
129: public ListTabbedPane(boolean border) {
130: setLayout(new BorderLayout(5, 0));
131: setOpaque(false);
132:
133: buttonPanel = new JPanel(new BorderLayout());
134: buttonPanel.setOpaque(false);
135:
136: hashMap = new HashMap<Object, Category<?>>();
137:
138: buttonTabbedPaneListModel = new ButtonTabbedPaneListModel();
139: list = new JList(buttonTabbedPaneListModel);
140: list.setCellRenderer(new ListTabbedPaneListCellRenderer());
141: list.setVisibleRowCount(4);
142:
143: list.setBackground(buttonPanel.getBackground());
144: if (!border) {
145: list.setBorder(BorderFactory.createEmptyBorder());
146: }
147: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
148:
149: list.addListSelectionListener(new ListSelectionListener() {
150: public void valueChanged(@NotNull
151: ListSelectionEvent e) {
152: if (!e.getValueIsAdjusting()) {
153: showSelectedCard();
154: }
155: }
156: });
157:
158: JScrollPane scrollPane = new JScrollPane(list);
159: if (!border) {
160: scrollPane.setBorder(BorderFactory.createEmptyBorder());
161: }
162: buttonPanel.add(scrollPane, BorderLayout.CENTER);
163:
164: cardLayout = new CardLayout();
165: cardPanel = new JPanel(cardLayout);
166:
167: add(buttonPanel, BorderLayout.WEST);
168:
169: add(cardPanel, BorderLayout.CENTER);
170:
171: cardPanel.add(new JPanel(), EMPTY_PANEL);
172: }
173:
174: public void setSelectionTabWidth(int width) {
175: list.setFixedCellWidth(width);
176: }
177:
178: @NotNull
179: public JPanel getButtonPanel() {
180: return buttonPanel;
181: }
182:
183: private void showSelectedCard() {
184: Category<?> category = (Category<?>) list.getSelectedValue();
185: if (category != null) {
186: cardLayout.show(cardPanel, category.getKey().toString());
187: cardPanel.revalidate();
188: cardPanel.repaint();
189: } else {
190: cardLayout.show(cardPanel, EMPTY_PANEL);
191: cardPanel.revalidate();
192: cardPanel.repaint();
193: }
194: }
195:
196: public int getSelectedIndex() {
197: return list.getSelectedIndex();
198: }
199:
200: public void setSelectedIndex(int index) {
201: list.setSelectedIndex(index);
202: showSelectedCard();
203: }
204:
205: public void addCard(@NotNull
206: final Category<?> category) {
207: if (hashMap.containsKey(category.getKey())) {
208: throw new IllegalArgumentException("a category with key='"
209: + category.getKey() + "' is already registered");
210: }
211:
212: buttonTabbedPaneListModel.addElement(category);
213:
214: cardPanel.add(category.getMainComponent(), category.getKey()
215: .toString());
216:
217: category
218: .addPropertyChangeListener(new PropertyChangeListener() {
219: public void propertyChange(@NotNull
220: PropertyChangeEvent evt) {
221: if ("title".equals(evt.getPropertyName()))//NON-NLS
222: {
223: buttonTabbedPaneListModel
224: .fireContentsChanged();
225: } else if ("iconBig".equals(evt
226: .getPropertyName()))//NON-NLS
227: {
228: buttonTabbedPaneListModel
229: .fireContentsChanged();
230: } else if ("mainComponent".equals(evt
231: .getPropertyName()))//NON-NLS
232: {
233: cardPanel.remove((Component) evt
234: .getOldValue());
235: cardPanel.add(category.getMainComponent(),
236: category.getKey().toString());
237:
238: cardPanel.revalidate();
239: cardPanel.repaint();
240: }
241: }
242: });
243:
244: hashMap.put(category.getKey(), category);
245: }
246:
247: @Nullable
248: public Category<?> getSelectedCategory() {
249: if (getSelectedIndex() != -1) {
250: return (Category<?>) list.getSelectedValue();
251: }
252: return null;
253: }
254:
255: @Nullable
256: public JComponent getSelectedComponent() {
257: if (getSelectedIndex() != -1) {
258: return ((Category<?>) list.getSelectedValue())
259: .getMainComponent();
260: }
261: return null;
262: }
263: }
|