001: /*
002: * @(#)ColorChooserMainPanel.java 1.4 2005-12-18
003: *
004: * Copyright (c) 2005 Werner Randelshofer
005: * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
006: * All rights reserved.
007: *
008: * This software is the confidential and proprietary information of
009: * Werner Randelshofer. ("Confidential Information"). You shall not
010: * disclose such Confidential Information and shall use it only in
011: * accordance with the terms of the license agreement you entered into
012: * with Werner Randelshofer.
013: */
014:
015: package contrib.ch.randelshofer.quaqua.colorchooser;
016:
017: import java.awt.*;
018: import java.awt.event.ItemEvent;
019: import java.awt.event.ItemListener;
020:
021: import javax.swing.*;
022: import javax.swing.colorchooser.AbstractColorChooserPanel;
023:
024: import contrib.ch.randelshofer.quaqua.util.Methods;
025:
026: /**
027: * The main panel of the color chooser UI.
028: *
029: * @author Werner Randelshofer
030: * @version 1.4 2005-12-18 ColorPicker added. <br>
031: * 1.3 2005-09-05 Get font from UIManager. <br>
032: * 1.2 2005-08-27 Remember chooser panel. <br>
033: * 1.1 2005-04-23 Removed main method. <br>
034: * 1.0 30 March 2005 Created.
035: */
036: public class ColorChooserMainPanel extends javax.swing.JPanel {
037: /**
038: * We store here the name of the last selected chooser. When the
039: * ColorChooserMainPanel is recreated multiple times in the same applicatin,
040: * the application 'remembers' which panel the user had opened before.
041: */
042: private static String lastSelectedChooserName = null;
043:
044: /** Creates new form. */
045: public ColorChooserMainPanel() {
046: initComponents();
047: toolBar.putClientProperty("Quaqua.ToolBar.isDividerDrawn",
048: Boolean.TRUE);
049:
050: }
051:
052: public void setPreviewPanel(JComponent c) {
053: previewPanelHolder.removeAll();
054: if (c != null) {
055: previewPanelHolder.add(c);
056: }
057: }
058:
059: public void addColorChooserPanel(final AbstractColorChooserPanel ccp) {
060: final String displayName = ccp.getDisplayName();
061:
062: if (displayName.equals("Color Picker")) {
063: northPanel.add(ccp, BorderLayout.WEST);
064: } else {
065: Icon displayIcon = ccp.getLargeDisplayIcon();
066: JToggleButton tb = new JToggleButton(null, displayIcon);
067: tb.setToolTipText(displayName);
068: Methods.invokeIfExists(tb, "setFocusable", false);
069: tb.setHorizontalTextPosition(SwingConstants.CENTER);
070: tb.setVerticalTextPosition(SwingConstants.BOTTOM);
071: tb.setFont(UIManager.getFont("ColorChooser.font"));
072: tb.putClientProperty("Quaqua.Button.style", "toolBarTab");
073: JPanel centerView = new JPanel(new BorderLayout());
074: centerView.add(ccp);
075: chooserPanelHolder.add(centerView, displayName);
076: toolBarButtonGroup.add(tb);
077: toolBar.add(tb);
078:
079: if (toolBar.getComponentCount() == 1
080: || lastSelectedChooserName != null
081: && lastSelectedChooserName.equals(displayName)) {
082: tb.setSelected(true);
083: CardLayout cl = (CardLayout) chooserPanelHolder
084: .getLayout();
085: cl.show(chooserPanelHolder, displayName);
086: }
087:
088: tb.addItemListener(new ItemListener() {
089: public void itemStateChanged(ItemEvent evt) {
090: if (evt.getStateChange() == ItemEvent.SELECTED) {
091: CardLayout cl = (CardLayout) chooserPanelHolder
092: .getLayout();
093: cl.show(chooserPanelHolder, displayName);
094: lastSelectedChooserName = displayName;
095: }
096: }
097: });
098: }
099: }
100:
101: public void removeAllColorChooserPanels() {
102: Component[] tb = toolBar.getComponents();
103: for (int i = 0; i < tb.length; i++) {
104: if (tb[i] instanceof AbstractButton) {
105: toolBarButtonGroup.remove((AbstractButton) tb[i]);
106: }
107: }
108: toolBar.removeAll();
109: chooserPanelHolder.removeAll();
110:
111: northPanel.removeAll();
112: northPanel.add(previewPanelHolder);
113: }
114:
115: /**
116: * This method is called from within the constructor to initialize the form.
117: * WARNING: Do NOT modify this code. The content of this method is always
118: * regenerated by the Form Editor.
119: */
120: private void initComponents() {// GEN-BEGIN:initComponents
121: toolBarButtonGroup = new javax.swing.ButtonGroup();
122: toolBar = new javax.swing.JToolBar();
123: mainPanel = new javax.swing.JPanel();
124: northPanel = new javax.swing.JPanel();
125: previewPanelHolder = new javax.swing.JPanel();
126: chooserPanelHolder = new javax.swing.JPanel();
127:
128: setLayout(new java.awt.BorderLayout());
129:
130: toolBar.setFloatable(false);
131: add(toolBar, java.awt.BorderLayout.NORTH);
132:
133: mainPanel.setLayout(new java.awt.BorderLayout());
134:
135: mainPanel.setBorder(new javax.swing.border.EmptyBorder(
136: new java.awt.Insets(5, 4, 7, 4)));
137: northPanel.setLayout(new java.awt.BorderLayout());
138:
139: previewPanelHolder.setLayout(new java.awt.BorderLayout());
140:
141: northPanel
142: .add(previewPanelHolder, java.awt.BorderLayout.CENTER);
143:
144: mainPanel.add(northPanel, java.awt.BorderLayout.NORTH);
145:
146: chooserPanelHolder.setLayout(new java.awt.CardLayout());
147:
148: chooserPanelHolder
149: .setBorder(new javax.swing.border.EmptyBorder(
150: new java.awt.Insets(5, 0, 0, 0)));
151: mainPanel.add(chooserPanelHolder, java.awt.BorderLayout.CENTER);
152:
153: add(mainPanel, java.awt.BorderLayout.CENTER);
154:
155: }// GEN-END:initComponents
156:
157: // Variables declaration - do not modify//GEN-BEGIN:variables
158: private javax.swing.JPanel chooserPanelHolder;
159: private javax.swing.JPanel mainPanel;
160: private javax.swing.JPanel northPanel;
161: private javax.swing.JPanel previewPanelHolder;
162: private javax.swing.JToolBar toolBar;
163: private javax.swing.ButtonGroup toolBarButtonGroup;
164: // End of variables declaration//GEN-END:variables
165:
166: }
|