KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > ItemOptionHelper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.multiview;
21
22 import javax.swing.*;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.util.Collections JavaDoc;
26
27 /**
28  * The class simplifies use of an option button group to show/set value of an item
29  *
30  * @author pfiala
31  */

32 public abstract class ItemOptionHelper implements ActionListener JavaDoc, Refreshable {
33
34     private final AbstractButton[] buttons;
35     private final AbstractButton unmatchedOption;
36     private XmlMultiViewDataSynchronizer synchronizer;
37
38     /**
39      * Constructor initializes object by button group which will be handled
40      *
41      * @param synchronizer
42      * @param group handled ButtonGroup.
43      * If the group contains at least one button that has empty text value
44      * (see {@link #getOptionText(javax.swing.AbstractButton)}, the last one of such buttons
45      * is used as "unmatched option". The "unmatched option" is selected,
46      */

47     public ItemOptionHelper(XmlMultiViewDataSynchronizer synchronizer, ButtonGroup group) {
48         
49         this.synchronizer = synchronizer;
50         buttons = (AbstractButton[]) Collections.list(group.getElements()).toArray(new AbstractButton[0]);
51         AbstractButton unmatchedOption = null;
52         for (int i = 0; i < buttons.length; i++) {
53             final AbstractButton button = buttons[i];
54             button.addActionListener(this);
55             if (getOptionText(button) == null) {
56                 unmatchedOption = button;
57             }
58         }
59         this.unmatchedOption = unmatchedOption;
60         setOption(getItemValue());
61     }
62
63     /**
64      * Invoked when an action occurs on an option button.
65      */

66     public final void actionPerformed(ActionEvent JavaDoc e) {
67         final String JavaDoc option = getOption();
68         if (!option.equals(getItemValue())) {
69             setItemValue(getOption());
70             synchronizer.requestUpdateData();
71         }
72     }
73
74     /**
75      * Selects option matched the item value.
76      * If no option matches the value the unmatchedOption option is selected,
77      * if the "unmatchedOption" uption exists.
78      * See {@link #ItemOptionHelper(XmlMultiViewDataSynchronizer, ButtonGroup)}
79      *
80      * @param itemValue value of item to be selected in button group
81      */

82     public void setOption(String JavaDoc itemValue) {
83         AbstractButton matchingButton = getMatchingButton(itemValue);
84         if (matchingButton != null && !matchingButton.isSelected()) {
85             matchingButton.setSelected(true);
86         }
87         return;
88     }
89
90     private AbstractButton getMatchingButton(String JavaDoc itemValue) {
91         AbstractButton matchingButton = null;
92         for (int i = 0; i < buttons.length; i++) {
93             final AbstractButton button = buttons[i];
94             if (getOptionText(button).equals(itemValue)) {
95                 matchingButton = button;
96                 break;
97             }
98         }
99         if (matchingButton == null && unmatchedOption != null) {
100             matchingButton = unmatchedOption;
101         }
102         return matchingButton;
103     }
104
105     private String JavaDoc getOptionText(AbstractButton button) {
106         String JavaDoc fixedValue = (String JavaDoc)button.getClientProperty(PROPERTY_FIXED_VALUE);
107         if (fixedValue!=null) return fixedValue;
108         else return button.getText();
109     }
110
111     /**
112      * Retrieves the text value represented by the selected option.
113      *
114      * @return client property:prop_fixed_value of the button representing the selected option.
115      * If the client property is null, a text property of the button is used.
116      */

117     public String JavaDoc getOption() {
118         for (int i = 0; i < buttons.length; i++) {
119             AbstractButton button = buttons[i];
120             if (button.isSelected()) {
121                 return getOptionText(button);
122             }
123         }
124         return null;
125     }
126
127     /**
128      * Called by the helper in order to retrieve the value of the item.
129      *
130      * @return value of the handled item.
131      */

132     public abstract String JavaDoc getItemValue();
133
134     /**
135      * Called by the helper in order to set the value of the item
136      *
137      * @param value new value of the hanlded item
138      */

139     public abstract void setItemValue(String JavaDoc value);
140
141     public void refresh() {
142         setOption(getItemValue());
143     }
144 }
145
Popular Tags