001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.gsfret.navigation.base;
043:
044: import java.awt.Dimension;
045: import java.awt.Insets;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import java.util.ArrayList;
049: import java.util.HashMap;
050: import java.util.List;
051: import java.util.Map;
052: import javax.swing.Box;
053: import javax.swing.BoxLayout;
054: import javax.swing.Icon;
055: import javax.swing.JComponent;
056: import javax.swing.JToggleButton;
057: import javax.swing.JToolBar;
058: import javax.swing.SwingUtilities;
059: import javax.swing.border.EmptyBorder;
060: import javax.swing.event.ChangeEvent;
061:
062: /**
063: * This file is originally from Retouche, the Java Support
064: * infrastructure in NetBeans. I have modified the file as little
065: * as possible to make merging Retouche fixes back as simple as
066: * possible.
067: * <p>
068: * Handles creation and manipulation with boolean state filters.
069: *
070: * @author Dafe Simomek
071: */
072: public final class FiltersManager {
073:
074: private FiltersComponent comp;
075:
076: static FiltersManager create(FiltersDescription descr) {
077: return new FiltersManager(descr);
078: }
079:
080: /** Returns true when given filter is selected, false otherwise.
081: * Note that this method is thread safe, can be called from any thread
082: * (and usually will, as clients will call this from loadContent naturally)
083: */
084: public boolean isSelected(String filterName) {
085: return comp.isSelected(filterName);
086: }
087:
088: /** Sets boolean value of filter with given name. True means filter is
089: * selected (enabled), false otherwise. Note, must be called from AWT thread.
090: */
091: public void setSelected(String filterName, boolean value) {
092: comp.setFilterSelected(filterName, value);
093: }
094:
095: /** @return component instance visually representing filters */
096: public JComponent getComponent() {
097: return comp;
098: }
099:
100: /** @return Filters description */
101: public FiltersDescription getDescription() {
102: return comp.getDescription();
103: }
104:
105: /** Assigns listener for listening to filter changes */
106: public void hookChangeListener(FilterChangeListener l) {
107: comp.hookFilterChangeListener(l);
108: }
109:
110: /** Interface for listening to changes of filter states contained in FIltersPanel
111: */
112: public interface FilterChangeListener {
113: /** Called whenever some changes in state of filters contained in
114: * filters panel is triggered
115: */
116: public void filterStateChanged(ChangeEvent e);
117:
118: } // end of FilterChangeListener
119:
120: /** Private, creation managed by factory method 'create' */
121: private FiltersManager(FiltersDescription descr) {
122: comp = new FiltersComponent(descr);
123: }
124:
125: /** Swing component representing filters in panel filled with toggle buttons.
126: * Provides thread safe access to the states of filters by copying states
127: * into private map, properly sync'ed.
128: */
129: private class FiltersComponent extends Box implements
130: ActionListener {
131:
132: /** list of <JToggleButton> visually representing filters */
133: private List<JToggleButton> toggles;
134: /** description of filters */
135: private final FiltersDescription filtersDesc;
136:
137: /** lock for listener */
138: private Object L_LOCK = new Object();
139: /** listener */
140: private FilterChangeListener clientL;
141:
142: /** lock for map of filter states */
143: private Object STATES_LOCK = new Object();
144: /** copy of filter states for accessing outside AWT */
145: private Map<String, Boolean> filterStates;
146:
147: /** Returns selected state of given filter, thread safe.
148: */
149: public boolean isSelected(String filterName) {
150: Boolean result;
151: synchronized (STATES_LOCK) {
152: if (filterStates == null) {
153: // Swing toggles not initialized yet
154: int index = filterIndexForName(filterName);
155: if (index < 0) {
156: return false;
157: } else {
158: return filtersDesc.isSelected(index);
159: }
160: }
161: result = filterStates.get(filterName);
162: }
163:
164: if (result == null) {
165: throw new IllegalArgumentException("Filter "
166: + filterName + " not found.");
167: }
168: return result.booleanValue();
169: }
170:
171: /** Sets filter value, AWT only */
172: public void setFilterSelected(String filterName, boolean value) {
173: assert SwingUtilities.isEventDispatchThread();
174:
175: int index = filterIndexForName(filterName);
176: if (index < 0) {
177: throw new IllegalArgumentException("Filter "
178: + filterName + " not found.");
179: }
180: // update both swing control and states map
181: toggles.get(index).setSelected(value);
182: synchronized (STATES_LOCK) {
183: filterStates.put(filterName, Boolean.valueOf(value));
184: }
185: // notify
186: fireChange();
187: }
188:
189: public void hookFilterChangeListener(FilterChangeListener l) {
190: synchronized (L_LOCK) {
191: clientL = l;
192: }
193: }
194:
195: public FiltersDescription getDescription() {
196: return filtersDesc;
197: }
198:
199: /** Not public, instances created using factory method createPanel */
200: FiltersComponent(FiltersDescription descr) {
201: super (BoxLayout.X_AXIS);
202: this .filtersDesc = descr;
203: // always create swing content in AWT thread
204: if (!SwingUtilities.isEventDispatchThread()) {
205: SwingUtilities.invokeLater(new Runnable() {
206: public void run() {
207: initPanel();
208: }
209: });
210: } else {
211: initPanel();
212: }
213: }
214:
215: /** Called only from AWT */
216: private void initPanel() {
217: setBorder(new EmptyBorder(1, 2, 3, 5));
218:
219: // configure toolbar
220: JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
221: toolbar.setFloatable(false);
222: toolbar.setRollover(true);
223: toolbar.setBorderPainted(false);
224: // create toggle buttons
225: int filterCount = filtersDesc.getFilterCount();
226: toggles = new ArrayList<JToggleButton>(filterCount);
227: JToggleButton toggleButton = null;
228:
229: Map<String, Boolean> fStates = new HashMap<String, Boolean>(
230: filterCount * 2);
231:
232: for (int i = 0; i < filterCount; i++) {
233: toggleButton = createToggle(fStates, i);
234: toggles.add(toggleButton);
235: }
236:
237: // add toggle buttons
238: JToggleButton curToggle;
239: Dimension space = new Dimension(3, 0);
240: for (int i = 0; i < toggles.size(); i++) {
241: curToggle = toggles.get(i);
242: curToggle.addActionListener(this );
243: toolbar.add(curToggle);
244: if (i != toggles.size() - 1) {
245: toolbar.addSeparator(space);
246: }
247: }
248:
249: add(toolbar);
250:
251: // initialize member states map
252: synchronized (STATES_LOCK) {
253: filterStates = fStates;
254: }
255: }
256:
257: private JToggleButton createToggle(
258: Map<String, Boolean> fStates, int index) {
259: boolean isSelected = filtersDesc.isSelected(index);
260: Icon icon = filtersDesc.getSelectedIcon(index);
261: // ensure small size, just for the icon
262: JToggleButton result = new JToggleButton(icon, isSelected);
263: Dimension size = new Dimension(icon.getIconWidth() + 6,
264: icon.getIconHeight() + 4);
265: result.setPreferredSize(size);
266: result.setMargin(new Insets(2, 3, 2, 3));
267: result.setToolTipText(filtersDesc.getTooltip(index));
268:
269: fStates.put(filtersDesc.getName(index), Boolean
270: .valueOf(isSelected));
271:
272: return result;
273: }
274:
275: /** Finds and returns index of filter with given name or -1 if no
276: * such filter exists.
277: */
278: private int filterIndexForName(String filterName) {
279: int filterCount = filtersDesc.getFilterCount();
280: String curName;
281: for (int i = 0; i < filterCount; i++) {
282: curName = filtersDesc.getName(i);
283: if (filterName.equals(curName)) {
284: return i;
285: }
286: }
287: return -1;
288: }
289:
290: /** Reactions to toggle button click, */
291: public void actionPerformed(ActionEvent e) {
292: // copy changed state first
293: JToggleButton toggle = (JToggleButton) e.getSource();
294: int index = toggles.indexOf(e.getSource());
295: synchronized (STATES_LOCK) {
296: filterStates.put(filtersDesc.getName(index), Boolean
297: .valueOf(toggle.isSelected()));
298: }
299: // notify
300: fireChange();
301: }
302:
303: private void fireChange() {
304: FilterChangeListener lCopy;
305: synchronized (L_LOCK) {
306: // no listener = no notification
307: if (clientL == null) {
308: return;
309: }
310: lCopy = clientL;
311: }
312:
313: // notify listener
314: lCopy.filterStateChanged(new ChangeEvent(
315: FiltersManager.this ));
316: }
317:
318: } // end of FiltersComponent
319:
320: }
|