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-2007 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: /*
043: * StyleEditor.java
044: *
045: * Created on October 13, 2004, 12:26 PM
046: */
047:
048: package org.netbeans.modules.css.visual.ui;
049:
050: import java.util.logging.Level;
051: import javax.swing.text.BadLocationException;
052: import org.netbeans.modules.css.editor.model.CssRuleContent;
053: import java.beans.PropertyChangeEvent;
054: import java.beans.PropertyChangeListener;
055: import java.beans.PropertyChangeSupport;
056: import java.util.logging.Logger;
057: import javax.swing.Icon;
058: import javax.swing.JPanel;
059: import org.netbeans.modules.css.visual.api.CssRuleContext;
060: import org.netbeans.modules.css.visual.ui.preview.CssPreviewable;
061: import org.openide.util.Exceptions;
062:
063: /**
064: * Super class for all Style editors
065: * @author Winston Prakash
066: * @version 1.0
067: */
068: abstract public class StyleEditor extends JPanel {
069:
070: private PropertyChangeSupport cssPropertyChangeSupport;
071:
072: CssPropertyChangeListener cssPropertyChangeListener = new CssPropertyChangeListener();
073:
074: boolean listenerAdded = false;
075:
076: private CssRuleContext content;
077:
078: /** Called by StyleBuilderPanel to set the UI panel property values. */
079: public void setContent(CssRuleContext content) {
080: this .content = content;
081: setCssPropertyValues(content.selectedRule().ruleContent());
082: }
083:
084: protected CssRuleContext content() {
085: return content;
086: }
087:
088: /**
089: * Overriden by the subclasses
090: * - Remove the property change listener
091: * - Set the values from CSS data to GUI elements
092: * - Set back the CSS property change listener
093: */
094: abstract protected void setCssPropertyValues(
095: CssRuleContent styleData);
096:
097: PropertyChangeSupport cssPropertyChangeSupport() {
098: if (cssPropertyChangeSupport == null) {
099: cssPropertyChangeSupport = new PropertyChangeSupport(this );
100: }
101: return cssPropertyChangeSupport;
102: }
103:
104: /**
105: * Set the CSS property change listener
106: */
107: public void setCssPropertyChangeListener(CssRuleContent styleData) {
108: // We don't want the property change listener added more than
109: // once accidently
110: synchronized (StyleEditor.class) {
111: if (!listenerAdded) {
112: listenerAdded = true;
113: cssPropertyChangeListener.setCssStyleData(styleData);
114: cssPropertyChangeSupport().addPropertyChangeListener(
115: cssPropertyChangeListener);
116: }
117: }
118: }
119:
120: /**
121: * Remove the CSS property change listener
122: */
123: public void removeCssPropertyChangeListener() {
124: synchronized (StyleEditor.class) {
125: if (listenerAdded) {
126: listenerAdded = false;
127: cssPropertyChangeSupport()
128: .removePropertyChangeListener(
129: cssPropertyChangeListener);
130: }
131: }
132: }
133:
134: /**
135: * Holds value of property displayName.
136: */
137: private String displayName;
138:
139: /**
140: * Holds value of property icon.
141: */
142: private Icon icon;
143:
144: /**
145: * Getter for property displayName.
146: * @return Value of property displayName.
147: */
148: public String getDisplayName() {
149: return this .displayName;
150: }
151:
152: /**
153: * Setter for property displayName.
154: * @param displayName New value of property displayName.
155: */
156: public void setDisplayName(String displayName) {
157: this .displayName = displayName;
158: }
159:
160: /**
161: * Getter for property icon.
162: * @return Value of property icon.
163: */
164: public Icon getIcon() {
165: return this .icon;
166: }
167:
168: /**
169: * Setter for property icon.
170: * @param icon New value of property icon.
171: */
172: public void setIcon(Icon icon) {
173: this .icon = icon;
174: }
175:
176: static class CssPropertyChangeListener implements
177: PropertyChangeListener {
178: CssRuleContent cssStyleData;
179:
180: public CssPropertyChangeListener() {
181: }
182:
183: public CssPropertyChangeListener(CssRuleContent styleData) {
184: cssStyleData = styleData;
185: }
186:
187: public void setCssStyleData(CssRuleContent styleData) {
188: cssStyleData = styleData;
189: }
190:
191: public void propertyChange(PropertyChangeEvent evt) {
192: try {
193: cssStyleData.modifyProperty(evt.getPropertyName(),
194: (String) evt.getNewValue());
195: } catch (BadLocationException ex) {
196: Logger.getLogger("global").log(Level.WARNING,
197: "CssModel inconsistency!", ex); //NOI18N
198: }
199: }
200: }
201: }
|