View Javadoc

1   /*
2    * $Id: SelectablePanel.java,v 1.6 2007/02/15 09:02:28 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  
23  package org.xmlhammer.gui.util;
24  
25  import java.awt.LayoutManager;
26  import java.awt.SystemColor;
27  
28  import javax.swing.JPanel;
29  import javax.swing.border.Border;
30  import javax.swing.border.CompoundBorder;
31  import javax.swing.border.MatteBorder;
32  
33  /***
34   * Put comment...
35   * 
36   * @version $Revision: 1.6 $, $Date: 2007/02/15 09:02:28 $
37   * @author Edwin Dankert <edankert@gmail.com>
38   */
39  
40  public class SelectablePanel extends JPanel {
41      private static final long serialVersionUID = -6907086273550806994L;
42  
43      private static final Border UNSELECTED_BORDER = 
44  //      new CompoundBorder(
45            new CompoundBorder(
46                new MatteBorder( 1, 1, 0, 0, SystemColor.controlDkShadow),
47                new MatteBorder(0, 0, 1, 1, SystemColor.controlLtHighlight));
48  
49  //		new CompoundBorder(
50  //			new CompoundBorder(
51  //				new MatteBorder( 1, 1, 0, 0, SystemColor.controlDkShadow),
52  //				new MatteBorder(0, 0, 1, 1, SystemColor.controlLtHighlight)),
53  //			new CompoundBorder( 
54  //				new EmptyBorder( 1, 1, 1, 1),
55  //				new EmptyBorder( 0, 1, 1, 1)));
56  
57  	private static final Border SELECTED_BORDER = UNSELECTED_BORDER;
58  //		new CompoundBorder(
59  //			new CompoundBorder(
60  //				new MatteBorder( 1, 1, 0, 0, SystemColor.controlDkShadow),
61  //				new MatteBorder(0, 0, 1, 1, SystemColor.controlLtHighlight)),
62  //			new CompoundBorder( 
63  //				new MatteBorder( 1, 1, 1, 1, UIManager.getColor( "TabbedPane.focus")),
64  //				new EmptyBorder( 0, 1, 1, 1)));
65  
66      private Border insideBorder = null;
67  	private boolean selected = false;
68  	private SelectablePanelGroup group = null;
69  
70  	/***
71  	 * @param layout
72  	 * @param isDoubleBuffered
73  	 */
74  	public SelectablePanel( LayoutManager layout, boolean isDoubleBuffered) {
75  		super( layout, isDoubleBuffered);
76  		
77  		init();
78  	}
79  
80  	/***
81  	 * @param layout
82  	 */
83  	public SelectablePanel( LayoutManager layout) {
84  		super( layout);
85  		
86  		init();
87  	}
88  
89  	/***
90  	 * @param isDoubleBuffered
91  	 */
92  	public SelectablePanel( boolean isDoubleBuffered) {
93  		super( isDoubleBuffered);
94  
95  		init();
96  	}
97  
98  	/***
99  	 * 
100 	 */
101 	public SelectablePanel() {
102 		super();
103 
104 		init();
105 	}
106 	
107 	private void init() {
108         updateBorder();
109 	}
110     
111     public void setBorder(Border border) {
112         insideBorder = border;
113 
114         updateBorder();
115     }
116 	
117 	public void setSelected( boolean selected) {
118 		if ( selected && group != null && !group.isSelected( this)) {
119 			group.setSelected( this, selected);
120 		} 
121 
122 		this.selected = selected;
123         
124         updateBorder();
125 	}
126 	
127 	public boolean isSelected() {
128 		return selected;
129 	}
130 
131 	public void setGroup( SelectablePanelGroup group) {
132 		this.group = group;
133 	}
134     
135     private void updateBorder() {
136         Border border = null;
137         
138         if (insideBorder != null) {
139             if (selected) {
140                 border = new CompoundBorder(SELECTED_BORDER, insideBorder);
141             } else {
142                 border = new CompoundBorder(UNSELECTED_BORDER, insideBorder);
143             }
144         } else {
145             if (selected) {
146                 border = SELECTED_BORDER;
147             } else {
148                 border = UNSELECTED_BORDER;
149             }
150         }
151         
152         super.setBorder(border);
153     }
154 }